From 9fd8dd47432799a1f0ecbe0bdd495b44fbd61b56 Mon Sep 17 00:00:00 2001
From: chris <kokosias@yahoo.gr>
Date: Thu, 2 Sep 2021 14:35:45 +0300
Subject: [PATCH] save feedback

---
 .../components/EditorComponent.js             |  3 ++-
 .../components/QuestionComponent.js           | 24 ++++++++++++++++++-
 .../helpers/helpers.js                        |  1 +
 .../schema/multipleChoiceNode.js              |  2 +-
 .../demo/src/HHMI/layout/EditorElements.js    |  4 ++++
 5 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/editors/demo/src/HHMI/MultipleChoiceQuestionService/components/EditorComponent.js b/editors/demo/src/HHMI/MultipleChoiceQuestionService/components/EditorComponent.js
index 5e40c26b5..c579e50c9 100644
--- a/editors/demo/src/HHMI/MultipleChoiceQuestionService/components/EditorComponent.js
+++ b/editors/demo/src/HHMI/MultipleChoiceQuestionService/components/EditorComponent.js
@@ -114,6 +114,7 @@ const EditorComponent = ({ node, view, getPos }) => {
               ),
             );
             context.updateView({}, questionId);
+            console.log(context.view.main.state.selection);
             // Kludge to prevent issues due to the fact that the whole
             // footnote is node-selected (and thus DOM-selected) when
             // the parent editor is focused.
@@ -139,7 +140,7 @@ const EditorComponent = ({ node, view, getPos }) => {
       },
       questionId,
     );
-    questionView.focus();
+    if (questionView.hasFocus()) questionView.focus();
   }, []);
 
   const dispatchTransaction = tr => {
diff --git a/editors/demo/src/HHMI/MultipleChoiceQuestionService/components/QuestionComponent.js b/editors/demo/src/HHMI/MultipleChoiceQuestionService/components/QuestionComponent.js
index ab009e895..0e51110d7 100644
--- a/editors/demo/src/HHMI/MultipleChoiceQuestionService/components/QuestionComponent.js
+++ b/editors/demo/src/HHMI/MultipleChoiceQuestionService/components/QuestionComponent.js
@@ -86,7 +86,7 @@ export default ({ node, view, getPos }) => {
     return editable;
   });
 
-  const [feadBack, setFeedBack] = useState('');
+  const [feadBack, setFeedBack] = useState(node.attrs.feedback);
 
   const feedBackRef = useRef(null);
 
@@ -95,6 +95,7 @@ export default ({ node, view, getPos }) => {
   };
 
   const handleKeyDown = e => {
+    e.stopPropagation();
     if (e.key === 'Backspace') {
       context.view.main.dispatch(
         context.view.main.state.tr.setSelection(
@@ -145,6 +146,25 @@ export default ({ node, view, getPos }) => {
     });
   };
 
+  const saveFeedBack = () => {
+    setTimeout(() => {
+      context.view.main.dispatch(
+        context.view.main.state.tr.setNodeMarkup(getPos(), undefined, {
+          ...node.attrs,
+          feedback: feadBack,
+        }),
+      );
+    }, 150);
+  };
+
+  const onFocus = () => {
+    context.view.main.dispatch(
+      context.view.main.state.tr.setSelection(
+        new TextSelection(context.view.main.state.tr.doc.resolve(0)),
+      ),
+    );
+  };
+
   const questionNumber = 1;
   const readOnly = !isEditable;
   const showAddIcon = true;
@@ -171,6 +191,8 @@ export default ({ node, view, getPos }) => {
               ref={feedBackRef}
               type="text"
               value={feadBack}
+              onBlur={saveFeedBack}
+              onFocus={onFocus}
             />
           </FeedBack>
         </QuestionWrapper>
diff --git a/editors/demo/src/HHMI/MultipleChoiceQuestionService/helpers/helpers.js b/editors/demo/src/HHMI/MultipleChoiceQuestionService/helpers/helpers.js
index 74a6d96c5..46f1fda20 100644
--- a/editors/demo/src/HHMI/MultipleChoiceQuestionService/helpers/helpers.js
+++ b/editors/demo/src/HHMI/MultipleChoiceQuestionService/helpers/helpers.js
@@ -24,6 +24,7 @@ const createEmptyParagraph = (context, newAnswerId) => {
       ),
     ),
   );
+  context.view[newAnswerId].focus();
 };
 
 export default {
diff --git a/editors/demo/src/HHMI/MultipleChoiceQuestionService/schema/multipleChoiceNode.js b/editors/demo/src/HHMI/MultipleChoiceQuestionService/schema/multipleChoiceNode.js
index 10503330c..371f84ada 100644
--- a/editors/demo/src/HHMI/MultipleChoiceQuestionService/schema/multipleChoiceNode.js
+++ b/editors/demo/src/HHMI/MultipleChoiceQuestionService/schema/multipleChoiceNode.js
@@ -2,7 +2,7 @@ const multipleChoiceNode = {
   attrs: {
     id: { default: '' },
     correct: { default: false },
-    feedback: { default: false },
+    feedback: { default: '' },
   },
   group: 'block',
   content: 'block*',
diff --git a/editors/demo/src/HHMI/layout/EditorElements.js b/editors/demo/src/HHMI/layout/EditorElements.js
index f3f4a0734..e0f3b57ac 100644
--- a/editors/demo/src/HHMI/layout/EditorElements.js
+++ b/editors/demo/src/HHMI/layout/EditorElements.js
@@ -111,6 +111,10 @@ export default css`
     }
   }
 
+  .mutiple-choice.ProseMirror-selectednode {
+    outline: none;
+  }
+
   sup,
   sub {
     line-height: 0;
-- 
GitLab