From 84d840362799d844e58205f93366e8fc87b0562d Mon Sep 17 00:00:00 2001
From: chris <kokosias@yahoo.gr>
Date: Sat, 18 Dec 2021 14:48:38 +0200
Subject: [PATCH] true false question

---
 .../QuestionTrueFalseNodeView.js              | 55 +++++++++++++++++++
 .../TrueFalseQuestion.js                      |  3 +-
 .../TrueFalseQuestionService.js               | 17 +++++-
 ...uestionComponent.js => AnswerComponent.js} |  0
 .../schema/questionTrueFalseNode.js           | 27 +++++++++
 5 files changed, 99 insertions(+), 3 deletions(-)
 create mode 100644 wax-prosemirror-services/src/MultipleChoiceQuestionService/TrueFalseQuestionService/QuestionTrueFalseNodeView.js
 rename wax-prosemirror-services/src/MultipleChoiceQuestionService/TrueFalseQuestionService/components/{QuestionComponent.js => AnswerComponent.js} (100%)
 create mode 100644 wax-prosemirror-services/src/MultipleChoiceQuestionService/TrueFalseQuestionService/schema/questionTrueFalseNode.js

diff --git a/wax-prosemirror-services/src/MultipleChoiceQuestionService/TrueFalseQuestionService/QuestionTrueFalseNodeView.js b/wax-prosemirror-services/src/MultipleChoiceQuestionService/TrueFalseQuestionService/QuestionTrueFalseNodeView.js
new file mode 100644
index 000000000..55c935358
--- /dev/null
+++ b/wax-prosemirror-services/src/MultipleChoiceQuestionService/TrueFalseQuestionService/QuestionTrueFalseNodeView.js
@@ -0,0 +1,55 @@
+import AbstractNodeView from '../../PortalService/AbstractNodeView';
+
+export default class QuestionNodeView extends AbstractNodeView {
+  constructor(
+    node,
+    view,
+    getPos,
+    decorations,
+    createPortal,
+    Component,
+    context,
+  ) {
+    super(node, view, getPos, decorations, createPortal, Component, context);
+
+    this.node = node;
+    this.outerView = view;
+    this.getPos = getPos;
+    this.context = context;
+  }
+
+  static name() {
+    return 'question_node_true_false';
+  }
+
+  update(node) {
+    this.node = node;
+    if (this.context.view[node.attrs.id]) {
+      const { state } = this.context.view[node.attrs.id];
+      const start = node.content.findDiffStart(state.doc.content);
+      if (start != null) {
+        let { a: endA, b: endB } = node.content.findDiffEnd(state.doc.content);
+        const overlap = start - Math.min(endA, endB);
+        if (overlap > 0) {
+          endA += overlap;
+          endB += overlap;
+        }
+        this.context.view[node.attrs.id].dispatch(
+          state.tr
+            .replace(start, endB, node.slice(start, endA))
+            .setMeta('fromOutside', true),
+        );
+      }
+    }
+
+    return true;
+  }
+
+  stopEvent(event) {
+    if (event.target.type === 'text') {
+      return true;
+    }
+    const innerView = this.context.view[this.node.attrs.id];
+    return innerView && innerView.dom.contains(event.target);
+  }
+}
diff --git a/wax-prosemirror-services/src/MultipleChoiceQuestionService/TrueFalseQuestionService/TrueFalseQuestion.js b/wax-prosemirror-services/src/MultipleChoiceQuestionService/TrueFalseQuestionService/TrueFalseQuestion.js
index c8b5de6ec..66ea3613b 100644
--- a/wax-prosemirror-services/src/MultipleChoiceQuestionService/TrueFalseQuestionService/TrueFalseQuestion.js
+++ b/wax-prosemirror-services/src/MultipleChoiceQuestionService/TrueFalseQuestionService/TrueFalseQuestion.js
@@ -19,8 +19,9 @@ class TrueFalseQuestion extends Tools {
       helpers.createOptions(
         view,
         context,
-        view.state.config.schema.nodes.true_false,
         view.state.config.schema.nodes.true_false_container,
+        view.state.config.schema.nodes.question_node_true_false,
+        view.state.config.schema.nodes.true_false,
       );
     };
   }
diff --git a/wax-prosemirror-services/src/MultipleChoiceQuestionService/TrueFalseQuestionService/TrueFalseQuestionService.js b/wax-prosemirror-services/src/MultipleChoiceQuestionService/TrueFalseQuestionService/TrueFalseQuestionService.js
index 6455c52b1..1ee73119c 100644
--- a/wax-prosemirror-services/src/MultipleChoiceQuestionService/TrueFalseQuestionService/TrueFalseQuestionService.js
+++ b/wax-prosemirror-services/src/MultipleChoiceQuestionService/TrueFalseQuestionService/TrueFalseQuestionService.js
@@ -1,9 +1,12 @@
 import Service from '../../Service';
 import TrueFalseQuestion from './TrueFalseQuestion';
 import trueFalseNode from './schema/trueFalseNode';
+import questionTrueFalseNode from './schema/questionTrueFalseNode';
 import trueFalseContainerNode from './schema/trueFalseContainerNode';
-import QuestionComponent from './components/QuestionComponent';
+import AnswerComponent from './components/AnswerComponent';
 import TrueFalseNodeView from './TrueFalseNodeView';
+import QuestionTrueFalseNodeView from './QuestionTrueFalseNodeView';
+import QuestionComponent from '../components/QuestionComponent';
 
 class TrueFalseQuestionService extends Service {
   register() {
@@ -19,11 +22,21 @@ class TrueFalseQuestionService extends Service {
       true_false: trueFalseNode,
     });
 
+    createNode({
+      question_node_true_false: questionTrueFalseNode,
+    });
+
     addPortal({
-      nodeView: TrueFalseNodeView,
+      nodeView: QuestionTrueFalseNodeView,
       component: QuestionComponent,
       context: this.app,
     });
+
+    addPortal({
+      nodeView: TrueFalseNodeView,
+      component: AnswerComponent,
+      context: this.app,
+    });
   }
 }
 
diff --git a/wax-prosemirror-services/src/MultipleChoiceQuestionService/TrueFalseQuestionService/components/QuestionComponent.js b/wax-prosemirror-services/src/MultipleChoiceQuestionService/TrueFalseQuestionService/components/AnswerComponent.js
similarity index 100%
rename from wax-prosemirror-services/src/MultipleChoiceQuestionService/TrueFalseQuestionService/components/QuestionComponent.js
rename to wax-prosemirror-services/src/MultipleChoiceQuestionService/TrueFalseQuestionService/components/AnswerComponent.js
diff --git a/wax-prosemirror-services/src/MultipleChoiceQuestionService/TrueFalseQuestionService/schema/questionTrueFalseNode.js b/wax-prosemirror-services/src/MultipleChoiceQuestionService/TrueFalseQuestionService/schema/questionTrueFalseNode.js
new file mode 100644
index 000000000..7dc512466
--- /dev/null
+++ b/wax-prosemirror-services/src/MultipleChoiceQuestionService/TrueFalseQuestionService/schema/questionTrueFalseNode.js
@@ -0,0 +1,27 @@
+import { v4 as uuidv4 } from 'uuid';
+
+const questionTrueFalseNode = {
+  attrs: {
+    class: { default: 'true-false-question' },
+    id: { default: uuidv4() },
+  },
+  group: 'block questions',
+  content: 'block*',
+  defining: true,
+
+  // atom: true,
+  parseDOM: [
+    {
+      tag: 'div.true-false-question',
+      getAttrs(dom) {
+        return {
+          id: dom.getAttribute('id'),
+          class: dom.getAttribute('class'),
+        };
+      },
+    },
+  ],
+  toDOM: node => ['div', node.attrs, 0],
+};
+
+export default questionTrueFalseNode;
-- 
GitLab