diff --git a/wax-prosemirror-services/src/FillTheGapQuestionService/components/FillTheGapContainerComponent.js b/wax-prosemirror-services/src/FillTheGapQuestionService/components/FillTheGapContainerComponent.js index 7c9dd04895dd16ce58fe461003f7008fd2b4bae2..0320b05d69c47d585e0bb9964ea820e90a41fa62 100644 --- a/wax-prosemirror-services/src/FillTheGapQuestionService/components/FillTheGapContainerComponent.js +++ b/wax-prosemirror-services/src/FillTheGapQuestionService/components/FillTheGapContainerComponent.js @@ -1,5 +1,10 @@ import React, { useContext, useRef, useState } from 'react'; -import { WaxContext, ComponentPlugin, Icon } from 'wax-prosemirror-core'; +import { + WaxContext, + ComponentPlugin, + DocumentHelpers, + Icon, +} from 'wax-prosemirror-core'; import { th } from '@pubsweet/ui-toolkit'; import styled from 'styled-components'; @@ -97,15 +102,18 @@ export default ({ node, view, getPos }) => { }; const removeQuestion = () => { - const nodeFound = findNodes(context.pmViews.main.state, getPos(), node); - if (nodeFound) { - context.pmViews.main.dispatch( - context.pmViews.main.state.tr.delete( - getPos(), - getPos() + nodeFound.nodeSize, - ), - ); - } + 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 ( @@ -152,15 +160,13 @@ export default ({ node, view, getPos }) => { ); }; -const findNodes = (state, pos, fillTheGapNode) => { - let nodeFound = ''; - state.doc.nodesBetween(pos, pos + 2, (node, from) => { - if ( - node.type.name === 'fill_the_gap_container' && - node.attrs.id === fillTheGapNode.attrs.id - ) { - nodeFound = node; +const getNodes = view => { + const allNodes = DocumentHelpers.findBlockNodes(view.state.doc); + const fillTheGapContainerNodes = []; + allNodes.forEach(node => { + if (node.node.type.name === 'fill_the_gap_container') { + fillTheGapContainerNodes.push(node); } }); - return nodeFound; + return fillTheGapContainerNodes; }; diff --git a/wax-prosemirror-services/src/MatchingService/components/MatchingContainerComponent.js b/wax-prosemirror-services/src/MatchingService/components/MatchingContainerComponent.js index c1ea11dc9c92398efe2bceeaca710bc718ea4c74..9eb97d8a962ee62f1547920598c1c746fed38e94 100644 --- a/wax-prosemirror-services/src/MatchingService/components/MatchingContainerComponent.js +++ b/wax-prosemirror-services/src/MatchingService/components/MatchingContainerComponent.js @@ -264,7 +264,20 @@ export default ({ node, view, getPos }) => { const { testMode } = customProps; const { feedback } = node.attrs; - const removeQuestion = () => {}; + 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 ( <MatchingWrapper> diff --git a/wax-prosemirror-services/src/MultipleDropDownService/components/MultipleDropDownContainerComponent.js b/wax-prosemirror-services/src/MultipleDropDownService/components/MultipleDropDownContainerComponent.js index a2e6efe91e2732facd7ca1123d8a4458ed8a351d..8959bb0a1ef6d9c47b601f055067429850e7e76e 100644 --- a/wax-prosemirror-services/src/MultipleDropDownService/components/MultipleDropDownContainerComponent.js +++ b/wax-prosemirror-services/src/MultipleDropDownService/components/MultipleDropDownContainerComponent.js @@ -1,5 +1,10 @@ import React, { useContext } from 'react'; -import { WaxContext, ComponentPlugin, Icon } from 'wax-prosemirror-core'; +import { + WaxContext, + ComponentPlugin, + DocumentHelpers, + Icon, +} from 'wax-prosemirror-core'; import styled from 'styled-components'; import ContainerEditor from './ContainerEditor'; import FeedbackComponent from '../../MultipleChoiceQuestionService/components/FeedbackComponent'; @@ -58,7 +63,20 @@ export default ({ node, view, getPos }) => { const { testMode } = customProps; const { feedback } = node.attrs; - const removeQuestion = () => {}; + 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 ( <MultipleDropDownpWrapper> @@ -91,3 +109,14 @@ export default ({ node, view, getPos }) => { </MultipleDropDownpWrapper> ); }; + +const getNodes = view => { + const allNodes = DocumentHelpers.findBlockNodes(view.state.doc); + const multipleDropContainerNodes = []; + allNodes.forEach(node => { + if (node.node.type.name === 'multiple_drop_down_container') { + multipleDropContainerNodes.push(node); + } + }); + return multipleDropContainerNodes; +};