From a20748f66e4a249df72b63eaf00ae6982b5e0750 Mon Sep 17 00:00:00 2001
From: chris <kokosias@yahoo.gr>
Date: Fri, 3 Dec 2021 16:49:35 +0200
Subject: [PATCH] create default options

---
 .../MultipleChoiceQuestion.js                    | 15 +++------------
 .../MultipleChoiceSingleCorrectQuestion.js       | 15 +++------------
 .../TrueFalseQuestion.js                         | 15 +++------------
 .../helpers/helpers.js                           | 16 ++++++++++++++++
 4 files changed, 25 insertions(+), 36 deletions(-)

diff --git a/wax-prosemirror-services/src/MultipleChoiceQuestionService/MultipleChoiceQuestion.js b/wax-prosemirror-services/src/MultipleChoiceQuestionService/MultipleChoiceQuestion.js
index 163779b68..379e1cf0c 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 fda78ebf5..6707961da 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 073a04b9e..7f379bddc 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 f8cd0b5cc..5afaa1003 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,
 };
-- 
GitLab