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 = () => {
ref={editorRef}
customValues={{ showFeedBack: submitted, testMode }}
fileUpload={file => renderImage(file)}
value={content}
// value={content}
targetFormat="JSON"
readonly={readOnly}
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 { DocumentHelpers } from 'wax-prosemirror-core';
import { DocumentHelpers, WaxContext } from 'wax-prosemirror-core';
const AnswerContainer = styled.div`
display: flex;
......@@ -26,7 +26,9 @@ const ValueInnerContainer = styled.div`
flex-direction: column;
`;
const ExactAnswerComponent = () => {
const ExactAnswerComponent = ({ getPos, node }) => {
const context = useContext(WaxContext);
const { activeView } = context;
const [exact, setExact] = useState('');
const [marginError, setMarginError] = useState('');
......@@ -40,12 +42,37 @@ const ExactAnswerComponent = () => {
.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 = () => {
setExact(onlyNumbers(exactRef.current.value));
SaveValuesToNode();
};
const onChangeError = () => {
setMarginError(onlyNumbers(errorRef.current.value));
SaveValuesToNode();
};
return (
......
......@@ -106,13 +106,13 @@ export default ({ node, view, getPos }) => {
<NumericalAnswerOption>
{!options[node.attrs.id] && <>No Type Selected</>}
{options[node.attrs.id]?.numericalAnswer === 'exactAnswer' && (
<ExactAnswerComponent />
<ExactAnswerComponent getPos={getPos} node={node} />
)}
{options[node.attrs.id]?.numericalAnswer === 'rangeAnswer' && (
<RangeAnswerComponent />
<RangeAnswerComponent getPos={getPos} node={node} />
)}
{options[node.attrs.id]?.numericalAnswer === 'preciseAnswer' && (
<PreciseAnswerComponent />
<PreciseAnswerComponent getPos={getPos} node={node} />
)}
</NumericalAnswerOption>
{!testMode && !(readOnly && feedback === '') && (
......
......@@ -33,7 +33,7 @@ const PreciseAnswerComponent = () => {
const onlyNumbers = value => {
return value
.replace(/[^0-9.]/g, '')
.replace(/[^0-9.;]/g, '')
.replace(/(\..*?)\..*/g, '$1')
.replace(/^0[^.]/, '0');
};
......
......@@ -4,7 +4,7 @@ const NumericalAnswerContainerNode = {
class: { default: 'numerical-answer' },
feedback: { default: '' },
answerType: { default: '' },
answers: { default: [] },
answersExact: { default: [] },
},
group: 'block questions',
atom: true,
......@@ -14,7 +14,7 @@ const NumericalAnswerContainerNode = {
tag: 'div.numerical-answer',
getAttrs(dom) {
return {
answers: JSON.parse(dom.getAttribute('answers')),
answersExact: JSON.parse(dom.getAttribute('answersExact')),
id: dom.getAttribute('id'),
class: dom.getAttribute('class'),
feedback: dom.getAttribute('feedback'),
......@@ -29,7 +29,7 @@ const NumericalAnswerContainerNode = {
{
id: node.attrs.id,
class: node.attrs.class,
answers: JSON.stringify(node.attrs.answers),
answersExact: JSON.stringify(node.attrs.answersExact),
feedback: node.attrs.feedback,
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