From 62c37b20c9b933251cfdf780471028008319dfe5 Mon Sep 17 00:00:00 2001
From: chris <kokosias@yahoo.gr>
Date: Fri, 2 Apr 2021 00:27:59 +0300
Subject: [PATCH] import/export also in pm JSON format

---
 editors/editoria/src/Editoria.js               | 10 ++++++++--
 .../MultipleChoiceQuestion.js                  |  2 +-
 .../src/QuestionsToolGroupService/Questions.js |  4 ++--
 wax-prosemirror-core/src/Wax.js                | 18 ++++++++++++++----
 wax-prosemirror-core/src/WaxView.js            | 17 ++++++++++++++++-
 5 files changed, 41 insertions(+), 10 deletions(-)

diff --git a/editors/editoria/src/Editoria.js b/editors/editoria/src/Editoria.js
index 9696eab0b..f652c315b 100644
--- a/editors/editoria/src/Editoria.js
+++ b/editors/editoria/src/Editoria.js
@@ -52,6 +52,11 @@ const Editoria = () => {
     key = 'editoriaMobile';
   }
 
+  const initialValue = {
+    type: 'doc',
+    content: [],
+  };
+
   const EditoriaComponent = useMemo(
     () => (
       <>
@@ -62,10 +67,11 @@ const Editoria = () => {
           autoFocus
           placeholder="Type Something..."
           fileUpload={file => renderImage(file)}
-          value={demo}
+          value={initialValue}
+          targetFormat="JSON"
           // readonly
           layout={layout}
-          // onChange={source => console.log(source)}
+          onChange={source => console.log(source)}
           user={user}
         />
       </>
diff --git a/editors/editoria/src/MultipleChoiceQuestionService/MultipleChoiceQuestion.js b/editors/editoria/src/MultipleChoiceQuestionService/MultipleChoiceQuestion.js
index a50f29656..7f7cbdec7 100644
--- a/editors/editoria/src/MultipleChoiceQuestionService/MultipleChoiceQuestion.js
+++ b/editors/editoria/src/MultipleChoiceQuestionService/MultipleChoiceQuestion.js
@@ -3,7 +3,7 @@ import { Tools } from 'wax-prosemirror-services';
 
 @injectable()
 class MultipleChoiceQuestion extends Tools {
-  title = 'Change to Multiple Choice';
+  title = 'Add Multiple Choice Question';
   label = 'Multiple Choice';
   name = 'Multiple Choice';
 
diff --git a/editors/editoria/src/QuestionsToolGroupService/Questions.js b/editors/editoria/src/QuestionsToolGroupService/Questions.js
index 7b4e927c7..51d123699 100644
--- a/editors/editoria/src/QuestionsToolGroupService/Questions.js
+++ b/editors/editoria/src/QuestionsToolGroupService/Questions.js
@@ -4,9 +4,9 @@ import { ToolGroup } from 'wax-prosemirror-services';
 @injectable()
 class Questions extends ToolGroup {
   tools = [];
-  constructor(@inject('Note') note) {
+  constructor(@inject('MultipleChoiceQuestion') multipleChoiceQuestion) {
     super();
-    this.tools = [note];
+    this.tools = [multipleChoiceQuestion];
   }
 }
 
diff --git a/wax-prosemirror-core/src/Wax.js b/wax-prosemirror-core/src/Wax.js
index 964e4f345..396eaca1c 100644
--- a/wax-prosemirror-core/src/Wax.js
+++ b/wax-prosemirror-core/src/Wax.js
@@ -65,6 +65,7 @@ const Wax = props => {
     value,
     user,
     onChange,
+    targetFormat,
   } = props;
 
   if (!application) return null;
@@ -83,8 +84,12 @@ const Wax = props => {
     plugins: finalPlugins,
   };
 
-  const parse = parser(schema);
-  WaxOptions.doc = parse(editorContent);
+  if (targetFormat === 'JSON') {
+    WaxOptions.doc = schema.nodeFromJSON(editorContent);
+  } else {
+    const parse = parser(schema);
+    WaxOptions.doc = parse(editorContent);
+  }
 
   const finalOnChange = debounce(
     value => {
@@ -98,8 +103,12 @@ const Wax = props => {
         };
       }
 
-      const serialize = serializer(schema);
-      WaxOnchange(serialize(value));
+      if (targetFormat === 'JSON') {
+        WaxOnchange(value);
+      } else {
+        const serialize = serializer(schema);
+        WaxOnchange(serialize(value));
+      }
 
       if (schema.nodes.footnote) {
         const old = schema.nodes.footnote.spec.toDOM;
@@ -129,6 +138,7 @@ const Wax = props => {
         options={WaxOptions}
         placeholder={placeholder}
         readonly={readonly}
+        targetFormat={targetFormat}
         TrackChange={TrackChange}
         user={user}
       >
diff --git a/wax-prosemirror-core/src/WaxView.js b/wax-prosemirror-core/src/WaxView.js
index cf1f28b53..41563bdb7 100644
--- a/wax-prosemirror-core/src/WaxView.js
+++ b/wax-prosemirror-core/src/WaxView.js
@@ -15,7 +15,16 @@ import BlockQuote from './BlockQuote';
 let previousDoc;
 
 export default props => {
-  const { readonly, onBlur, options, debug, autoFocus, user } = props;
+  const {
+    readonly,
+    onBlur,
+    options,
+    debug,
+    autoFocus,
+    user,
+    targetFormat,
+  } = props;
+
   const editorRef = useRef();
   let view;
   const context = useContext(WaxContext);
@@ -113,6 +122,12 @@ export default props => {
     }
     if (view.state.doc !== previousDoc || tr.getMeta('forceUpdate'))
       props.onChange(state.doc.content);
+
+    if (targetFormat === 'JSON') {
+      props.onChange(state.doc.toJSON());
+    } else {
+      props.onChange(state.doc.content);
+    }
   };
 
   const editor = <div ref={setEditorRef} />;
-- 
GitLab