diff --git a/wax-prosemirror-services/src/MultipleChoiceQuestionService/MultipleChoiceQuestion.js b/wax-prosemirror-services/src/MultipleChoiceQuestionService/MultipleChoiceQuestion.js index 163779b6891088d67582b248e3176675ba9e52b1..379e1cf0c58d5637771bdbc56e6e881269d2ae3f 100644 --- a/wax-prosemirror-services/src/MultipleChoiceQuestionService/MultipleChoiceQuestion.js +++ b/wax-prosemirror-services/src/MultipleChoiceQuestionService/MultipleChoiceQuestion.js @@ -10,14 +10,6 @@ import helpers from './helpers/helpers'; import Tools from '../lib/Tools'; import ToolBarBtn from './components/ToolBarBtn'; -const checkifEmpty = view => { - const { state } = view; - const { from, to } = state.selection; - state.doc.nodesBetween(from, to, (node, pos) => { - if (node.textContent !== ' ') Commands.simulateKey(view, 13, 'Enter'); - }); -}; - const createOption = (main, context) => { const { state, dispatch } = main; /* Create Wrapping */ @@ -41,9 +33,6 @@ const createOption = (main, context) => { Fragment.empty, ); dispatch(main.state.tr.replaceSelectionWith(firstOption)); - setTimeout(() => { - helpers.createEmptyParagraph(context, firstOption.attrs.id); - }, 50); /* create Second Option */ const secondOption = main.state.config.schema.nodes.multiple_choice.create( @@ -51,8 +40,10 @@ const createOption = (main, context) => { Fragment.empty, ); dispatch(main.state.tr.replaceSelectionWith(secondOption)); + setTimeout(() => { helpers.createEmptyParagraph(context, secondOption.attrs.id); + helpers.createEmptyParagraph(context, firstOption.attrs.id); }, 50); }; @@ -65,7 +56,7 @@ class MultipleChoiceQuestion extends Tools { get run() { return (view, context) => { - checkifEmpty(view); + helpers.checkifEmpty(view); createOption(view, context); }; } diff --git a/wax-prosemirror-services/src/MultipleChoiceQuestionService/MultipleChoiceSingleCorrectQuestionService/MultipleChoiceSingleCorrectQuestion.js b/wax-prosemirror-services/src/MultipleChoiceQuestionService/MultipleChoiceSingleCorrectQuestionService/MultipleChoiceSingleCorrectQuestion.js index fda78ebf5d72ef37f92f472e61d8685b300c3369..6707961dae63555400a11f03f82714e83843a105 100644 --- a/wax-prosemirror-services/src/MultipleChoiceQuestionService/MultipleChoiceSingleCorrectQuestionService/MultipleChoiceSingleCorrectQuestion.js +++ b/wax-prosemirror-services/src/MultipleChoiceQuestionService/MultipleChoiceSingleCorrectQuestionService/MultipleChoiceSingleCorrectQuestion.js @@ -10,14 +10,6 @@ import ToolBarBtn from '../components/ToolBarBtn'; import helpers from '../helpers/helpers'; import Tools from '../../lib/Tools'; -const checkifEmpty = view => { - const { state } = view; - const { from, to } = state.selection; - state.doc.nodesBetween(from, to, (node, pos) => { - if (node.textContent !== ' ') Commands.simulateKey(view, 13, 'Enter'); - }); -}; - const createOption = (main, context) => { const { state, dispatch } = main; /* Create Wrapping */ @@ -41,9 +33,6 @@ const createOption = (main, context) => { Fragment.empty, ); dispatch(main.state.tr.replaceSelectionWith(firstOption)); - setTimeout(() => { - helpers.createEmptyParagraph(context, firstOption.attrs.id); - }, 50); /* create Second Option */ const secondOption = main.state.config.schema.nodes.multiple_choice_single_correct.create( @@ -51,8 +40,10 @@ const createOption = (main, context) => { Fragment.empty, ); dispatch(main.state.tr.replaceSelectionWith(secondOption)); + setTimeout(() => { helpers.createEmptyParagraph(context, secondOption.attrs.id); + helpers.createEmptyParagraph(context, firstOption.attrs.id); }, 50); }; @@ -65,7 +56,7 @@ class MultipleChoiceSingleCorrectQuestion extends Tools { get run() { return (view, context) => { - checkifEmpty(view); + helpers.checkifEmpty(view); createOption(view, context); }; } diff --git a/wax-prosemirror-services/src/MultipleChoiceQuestionService/TrueFalseQuestionService/TrueFalseQuestion.js b/wax-prosemirror-services/src/MultipleChoiceQuestionService/TrueFalseQuestionService/TrueFalseQuestion.js index 073a04b9efe4753cdac617175b66a167dfb088b6..7f379bddcf52dc87f4893b75e0e6a44f22b53070 100644 --- a/wax-prosemirror-services/src/MultipleChoiceQuestionService/TrueFalseQuestionService/TrueFalseQuestion.js +++ b/wax-prosemirror-services/src/MultipleChoiceQuestionService/TrueFalseQuestionService/TrueFalseQuestion.js @@ -10,16 +10,9 @@ import ToolBarBtn from '../components/ToolBarBtn'; import helpers from '../helpers/helpers'; import Tools from '../../lib/Tools'; -const checkifEmpty = view => { - const { state } = view; - const { from, to } = state.selection; - state.doc.nodesBetween(from, to, (node, pos) => { - if (node.textContent !== ' ') Commands.simulateKey(view, 13, 'Enter'); - }); -}; - const createOption = (main, context) => { const { state, dispatch } = main; + /* Create Wrapping */ const { $from, $to } = state.selection; const range = $from.blockRange($to); @@ -41,9 +34,6 @@ const createOption = (main, context) => { Fragment.empty, ); dispatch(main.state.tr.replaceSelectionWith(firstOption)); - setTimeout(() => { - helpers.createEmptyParagraph(context, firstOption.attrs.id); - }, 50); /* create Second Option */ const secondOption = main.state.config.schema.nodes.true_false.create( @@ -53,6 +43,7 @@ const createOption = (main, context) => { dispatch(main.state.tr.replaceSelectionWith(secondOption)); setTimeout(() => { helpers.createEmptyParagraph(context, secondOption.attrs.id); + helpers.createEmptyParagraph(context, firstOption.attrs.id); }, 50); }; @@ -65,7 +56,7 @@ class MultipleChoiceQuestion extends Tools { get run() { return (view, context) => { - checkifEmpty(view); + helpers.checkifEmpty(view); createOption(view, context); }; } diff --git a/wax-prosemirror-services/src/MultipleChoiceQuestionService/helpers/helpers.js b/wax-prosemirror-services/src/MultipleChoiceQuestionService/helpers/helpers.js index f8cd0b5cc1845889fa892d9c36a719aa9f6336d0..5afaa10032863a5ae70d5dad077e7f9143afbddb 100644 --- a/wax-prosemirror-services/src/MultipleChoiceQuestionService/helpers/helpers.js +++ b/wax-prosemirror-services/src/MultipleChoiceQuestionService/helpers/helpers.js @@ -1,4 +1,5 @@ import { TextSelection } from 'prosemirror-state'; +import { Commands } from 'wax-prosemirror-utilities'; const createEmptyParagraph = (context, newAnswerId) => { if (context.view[newAnswerId]) { @@ -28,6 +29,21 @@ const createEmptyParagraph = (context, newAnswerId) => { } }; +const checkifEmpty = view => { + const { state } = view; + const { from, to } = state.selection; + state.doc.nodesBetween(from, to, (node, pos) => { + if (node.textContent !== ' ') Commands.simulateKey(view, 13, 'Enter'); + }); + if (state.selection.constructor.name === 'GapCursor') { + Commands.simulateKey(view, 13, 'Enter'); + setTimeout(() => { + view.focus(); + }); + } +}; + export default { createEmptyParagraph, + checkifEmpty, };