Newer
Older
import React, { useContext } from 'react';
import {
WaxContext,
ComponentPlugin,
DocumentHelpers,
Icon,
} from 'wax-prosemirror-core';
import styled from 'styled-components';
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import FeedbackComponent from '../../MultipleChoiceQuestionService/components/FeedbackComponent';
const NumericalAnswerWrapper = styled.div`
margin: 0px 38px 15px 38px;
margin-top: 10px;
`;
const NumericalAnswerContainer = styled.div`
border: 3px solid #f5f5f7;
margin-bottom: 30px;
`;
const NumericalAnswerContainerTool = styled.div`
border: 3px solid #f5f5f7;
border-bottom: none;
span:first-of-type {
position: relative;
top: 3px;
}
`;
const ActionButton = styled.button`
background: transparent;
cursor: pointer;
margin-top: 16px;
border: none;
position: relative;
bottom: 14px;
left: -11px;
float: right;
`;
const StyledIconActionRemove = styled(Icon)`
height: 24px;
width: 24px;
`;
export default ({ node, view, getPos }) => {
const context = useContext(WaxContext);
const {
pmViews: { main },
} = context;
const FillTheGapTool = ComponentPlugin('fillTheGap');
const customProps = main.props.customValues;
const { testMode } = customProps;
const isEditable = main.props.editable(editable => {
return editable;
});
const readOnly = !isEditable;
const { feedback } = node.attrs;
const removeQuestion = () => {
const allNodes = getNodes(context.pmViews.main);
allNodes.forEach(singleNode => {
if (singleNode.node.attrs.id === node.attrs.id) {
context.pmViews.main.dispatch(
context.pmViews.main.state.tr.delete(
singleNode.pos,
singleNode.pos + singleNode.node.nodeSize,
),
);
}
});
};
return (
<NumericalAnswerWrapper>
<div>
{!testMode && !readOnly && (
<NumericalAnswerContainerTool>
<FillTheGapTool />
<ActionButton
aria-label="delete this question"
onClick={removeQuestion}
type="button"
>
<StyledIconActionRemove name="deleteOutlinedQuestion" />
</ActionButton>
</NumericalAnswerContainerTool>
)}
</div>
<NumericalAnswerContainer className="numerical-answer">
<EditorComponent getPos={getPos} node={node} view={view} />
{!testMode && !(readOnly && feedback === '') && (
<FeedbackComponent
getPos={getPos}
node={node}
readOnly={readOnly}
view={view}
/>
)}
</NumericalAnswerContainer>
</NumericalAnswerWrapper>
);
};
const getNodes = view => {
const allNodes = DocumentHelpers.findBlockNodes(view.state.doc);
const numericalAnswerpContainerNodes = [];
allNodes.forEach(node => {
if (node.node.type.name === 'numerical_answer_container') {
numericalAnswerpContainerNodes.push(node);
}
});
return numericalAnswerpContainerNodes;
};