Skip to content
Snippets Groups Projects
Commit 141c78ff authored by chris's avatar chris
Browse files

save exact answers

parent 5dd20f75
No related branches found
No related tags found
1 merge request!513Student numerical
...@@ -167,10 +167,11 @@ const Hhmi = () => { ...@@ -167,10 +167,11 @@ const Hhmi = () => {
ref={editorRef} ref={editorRef}
customValues={{ showFeedBack: submitted, testMode }} customValues={{ showFeedBack: submitted, testMode }}
fileUpload={file => renderImage(file)} fileUpload={file => renderImage(file)}
value={content} // value={content}
targetFormat="JSON"
readonly={readOnly} readonly={readOnly}
layout={HhmiLayout} layout={HhmiLayout}
// onChange={source => console.log(source)} onChange={source => console.log(source)}
/> />
</> </>
); );
......
import React, { useRef, useState } from 'react'; import React, { useRef, useState, useContext } from 'react';
import styled from 'styled-components'; import styled from 'styled-components';
import { DocumentHelpers } from 'wax-prosemirror-core'; import { DocumentHelpers, WaxContext } from 'wax-prosemirror-core';
const AnswerContainer = styled.div` const AnswerContainer = styled.div`
display: flex; display: flex;
...@@ -26,7 +26,9 @@ const ValueInnerContainer = styled.div` ...@@ -26,7 +26,9 @@ const ValueInnerContainer = styled.div`
flex-direction: column; flex-direction: column;
`; `;
const ExactAnswerComponent = () => { const ExactAnswerComponent = ({ getPos, node }) => {
const context = useContext(WaxContext);
const { activeView } = context;
const [exact, setExact] = useState(''); const [exact, setExact] = useState('');
const [marginError, setMarginError] = useState(''); const [marginError, setMarginError] = useState('');
...@@ -40,12 +42,37 @@ const ExactAnswerComponent = () => { ...@@ -40,12 +42,37 @@ const ExactAnswerComponent = () => {
.replace(/^0[^.]/, '0'); .replace(/^0[^.]/, '0');
}; };
const SaveValuesToNode = () => {
const allNodes = getNodes(context.pmViews.main);
allNodes.forEach(singleNode => {
if (singleNode.node.attrs.id === node.attrs.id) {
const obj = {
exactAnswer: onlyNumbers(exactRef.current.value),
marginError: onlyNumbers(errorRef.current.value),
};
context.pmViews.main.dispatch(
context.pmViews.main.state.tr.setNodeMarkup(
singleNode.pos,
undefined,
{
...singleNode.node.attrs,
answersExact: obj,
},
),
);
}
});
};
const onChangeExact = () => { const onChangeExact = () => {
setExact(onlyNumbers(exactRef.current.value)); setExact(onlyNumbers(exactRef.current.value));
SaveValuesToNode();
}; };
const onChangeError = () => { const onChangeError = () => {
setMarginError(onlyNumbers(errorRef.current.value)); setMarginError(onlyNumbers(errorRef.current.value));
SaveValuesToNode();
}; };
return ( return (
......
...@@ -106,13 +106,13 @@ export default ({ node, view, getPos }) => { ...@@ -106,13 +106,13 @@ export default ({ node, view, getPos }) => {
<NumericalAnswerOption> <NumericalAnswerOption>
{!options[node.attrs.id] && <>No Type Selected</>} {!options[node.attrs.id] && <>No Type Selected</>}
{options[node.attrs.id]?.numericalAnswer === 'exactAnswer' && ( {options[node.attrs.id]?.numericalAnswer === 'exactAnswer' && (
<ExactAnswerComponent /> <ExactAnswerComponent getPos={getPos} node={node} />
)} )}
{options[node.attrs.id]?.numericalAnswer === 'rangeAnswer' && ( {options[node.attrs.id]?.numericalAnswer === 'rangeAnswer' && (
<RangeAnswerComponent /> <RangeAnswerComponent getPos={getPos} node={node} />
)} )}
{options[node.attrs.id]?.numericalAnswer === 'preciseAnswer' && ( {options[node.attrs.id]?.numericalAnswer === 'preciseAnswer' && (
<PreciseAnswerComponent /> <PreciseAnswerComponent getPos={getPos} node={node} />
)} )}
</NumericalAnswerOption> </NumericalAnswerOption>
{!testMode && !(readOnly && feedback === '') && ( {!testMode && !(readOnly && feedback === '') && (
......
...@@ -33,7 +33,7 @@ const PreciseAnswerComponent = () => { ...@@ -33,7 +33,7 @@ const PreciseAnswerComponent = () => {
const onlyNumbers = value => { const onlyNumbers = value => {
return value return value
.replace(/[^0-9.]/g, '') .replace(/[^0-9.;]/g, '')
.replace(/(\..*?)\..*/g, '$1') .replace(/(\..*?)\..*/g, '$1')
.replace(/^0[^.]/, '0'); .replace(/^0[^.]/, '0');
}; };
......
...@@ -4,7 +4,7 @@ const NumericalAnswerContainerNode = { ...@@ -4,7 +4,7 @@ const NumericalAnswerContainerNode = {
class: { default: 'numerical-answer' }, class: { default: 'numerical-answer' },
feedback: { default: '' }, feedback: { default: '' },
answerType: { default: '' }, answerType: { default: '' },
answers: { default: [] }, answersExact: { default: [] },
}, },
group: 'block questions', group: 'block questions',
atom: true, atom: true,
...@@ -14,7 +14,7 @@ const NumericalAnswerContainerNode = { ...@@ -14,7 +14,7 @@ const NumericalAnswerContainerNode = {
tag: 'div.numerical-answer', tag: 'div.numerical-answer',
getAttrs(dom) { getAttrs(dom) {
return { return {
answers: JSON.parse(dom.getAttribute('answers')), answersExact: JSON.parse(dom.getAttribute('answersExact')),
id: dom.getAttribute('id'), id: dom.getAttribute('id'),
class: dom.getAttribute('class'), class: dom.getAttribute('class'),
feedback: dom.getAttribute('feedback'), feedback: dom.getAttribute('feedback'),
...@@ -29,7 +29,7 @@ const NumericalAnswerContainerNode = { ...@@ -29,7 +29,7 @@ const NumericalAnswerContainerNode = {
{ {
id: node.attrs.id, id: node.attrs.id,
class: node.attrs.class, class: node.attrs.class,
answers: JSON.stringify(node.attrs.answers), answersExact: JSON.stringify(node.attrs.answersExact),
feedback: node.attrs.feedback, feedback: node.attrs.feedback,
answerType: node.attrs.answerType, answerType: node.attrs.answerType,
}, },
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment