Skip to content
Snippets Groups Projects
Commit 42f4030c authored by chris's avatar chris
Browse files

update correct group

parent 1fd76927
No related branches found
No related tags found
1 merge request!348Multiple single
...@@ -5,6 +5,7 @@ import { WaxContext } from 'wax-prosemirror-core'; ...@@ -5,6 +5,7 @@ import { WaxContext } from 'wax-prosemirror-core';
import { DocumentHelpers } from 'wax-prosemirror-utilities'; import { DocumentHelpers } from 'wax-prosemirror-utilities';
import styled from 'styled-components'; import styled from 'styled-components';
import Switch from '../../components/Switch'; import Switch from '../../components/Switch';
import { NodeSelection } from 'prosemirror-state';
const StyledSwitch = styled(Switch)` const StyledSwitch = styled(Switch)`
display: flex; display: flex;
...@@ -33,30 +34,48 @@ const CustomSwitch = ({ node, getPos }) => { ...@@ -33,30 +34,48 @@ const CustomSwitch = ({ node, getPos }) => {
const handleChange = () => { const handleChange = () => {
setChecked(!checked); setChecked(!checked);
const { tr } = main.state; main.dispatch(
main.state.tr.setSelection(
NodeSelection.create(main.state.doc, getPos()),
),
);
const parentContainer = findParentOfType(
main.state,
main.state.config.schema.nodes.multiple_choice_single_correct_container,
);
let parentPosition = 0;
main.state.doc.descendants((parentNode, parentPos) => { main.state.doc.descendants((parentNode, parentPos) => {
if (parentNode.type.name === 'multiple_choice_single_correct_container') { if (
parentNode.descendants((element, position) => { parentNode.type.name === 'multiple_choice_single_correct_container' &&
if ( parentNode.attrs.id === parentContainer.attrs.id
element.type.name === 'multiple_choice_single_correct' && ) {
element.attrs.id === node.attrs.id parentPosition = parentPos;
) { }
tr.setNodeMarkup(getPos(), undefined, { });
...element.attrs,
correct: !checked, const { tr } = main.state;
});
} else if ( parentContainer.descendants((element, position) => {
element.type.name === 'multiple_choice_single_correct' && if (
element.attrs.correct element.type.name === 'multiple_choice_single_correct' &&
) { element.attrs.id === node.attrs.id
tr.setNodeMarkup(parentPos + position + 1, undefined, { ) {
...element.attrs, tr.setNodeMarkup(getPos(), undefined, {
correct: false, ...element.attrs,
}); correct: !checked,
} });
} else if (
element.type.name === 'multiple_choice_single_correct' &&
element.attrs.correct
) {
tr.setNodeMarkup(parentPosition + position + 1, undefined, {
...element.attrs,
correct: false,
}); });
} }
}); });
main.dispatch(tr); main.dispatch(tr);
}; };
...@@ -84,3 +103,15 @@ const getNodes = view => { ...@@ -84,3 +103,15 @@ const getNodes = view => {
}; };
export default CustomSwitch; export default CustomSwitch;
const findParentOfType = (state, nodeType) => {
let nodeFound = '';
const predicate = node => node.type === nodeType;
for (let i = state.selection.$from.depth; i > 0; i -= 1) {
const node = state.selection.$from.node(i);
if (predicate(node)) {
nodeFound = node;
}
}
return nodeFound;
};
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