From 3ea328d0a2d999a2cdc41788f432ad2c4da5fbfc Mon Sep 17 00:00:00 2001
From: chris <kokosias@yahoo.gr>
Date: Mon, 18 Oct 2021 13:20:52 +0300
Subject: [PATCH] set get content

---
 editors/demo/src/Editoria/Editoria.js         |  4 +-
 wax-prosemirror-core/src/Wax.js               | 17 +-------
 wax-prosemirror-core/src/WaxView.js           | 37 +++++++++--------
 .../src/helpers/GetDocContent.js              | 40 +++++++++++++++++++
 4 files changed, 66 insertions(+), 32 deletions(-)
 create mode 100644 wax-prosemirror-core/src/helpers/GetDocContent.js

diff --git a/editors/demo/src/Editoria/Editoria.js b/editors/demo/src/Editoria/Editoria.js
index 40c8dafce..5bd375095 100644
--- a/editors/demo/src/Editoria/Editoria.js
+++ b/editors/demo/src/Editoria/Editoria.js
@@ -43,7 +43,9 @@ const Editoria = () => {
   const EditoriaComponent = useMemo(
     () => (
       <>
-        <button onClick={() => editorRef.current.getContent()}>Click</button>
+        <button onClick={() => console.log(editorRef.current.getContent())}>
+          Click
+        </button>
 
         <Wax
           ref={editorRef}
diff --git a/wax-prosemirror-core/src/Wax.js b/wax-prosemirror-core/src/Wax.js
index 616026635..48dcad2f2 100644
--- a/wax-prosemirror-core/src/Wax.js
+++ b/wax-prosemirror-core/src/Wax.js
@@ -1,11 +1,5 @@
 /* eslint react/prop-types: 0 */
-import React, {
-  useEffect,
-  useState,
-  forwardRef,
-  useRef,
-  useImperativeHandle,
-} from 'react';
+import React, { useEffect, useState, forwardRef } from 'react';
 import { each } from 'lodash';
 import { DOMSerializer } from 'prosemirror-model';
 
@@ -38,12 +32,6 @@ const Wax = forwardRef((props, ref) => {
     return () => newApplication.resetApp();
   }, []);
 
-  useImperativeHandle(ref, () => ({
-    getContent() {
-      console.log('content');
-    },
-  }));
-
   const {
     autoFocus,
     browserSpellCheck,
@@ -51,7 +39,6 @@ const Wax = forwardRef((props, ref) => {
     debug,
     fileUpload,
     layout,
-    onBlur,
     placeholder,
     readonly,
     value,
@@ -111,10 +98,10 @@ const Wax = forwardRef((props, ref) => {
           browserSpellCheck={browserSpellCheck}
           debug={debug}
           fileUpload={fileUpload}
-          onBlur={onBlur || (v => true)}
           onChange={finalOnChange || (v => true)}
           placeholder={placeholder}
           readonly={readonly}
+          ref={ref}
           serializer={serializer}
           targetFormat={targetFormat}
           TrackChange={TrackChange}
diff --git a/wax-prosemirror-core/src/WaxView.js b/wax-prosemirror-core/src/WaxView.js
index 0b5e7043d..f169ee5d9 100644
--- a/wax-prosemirror-core/src/WaxView.js
+++ b/wax-prosemirror-core/src/WaxView.js
@@ -1,3 +1,4 @@
+/* eslint-disable react/prop-types */
 import React, {
   useRef,
   useContext,
@@ -5,6 +6,8 @@ import React, {
   useMemo,
   useEffect,
   useState,
+  forwardRef,
+  useImperativeHandle,
 } from 'react';
 import applyDevTools from 'prosemirror-dev-tools';
 import { EditorState } from 'prosemirror-state';
@@ -15,24 +18,26 @@ import { PortalContext } from './PortalContext';
 import transformPasted from './helpers/TransformPasted';
 import ComponentPlugin from './ComponentPlugin';
 import WaxOptions from './WaxOptions';
+import getDocContent from './helpers/GetDocContent';
 import './styles/styles.css';
 
 const WaxPortals = ComponentPlugin('waxPortals');
 
 let previousDoc;
 
-export default props => {
+const WaxView = forwardRef((props, ref) => {
+  let view;
   const {
     browserSpellCheck,
     readonly,
-    onBlur,
     debug,
     autoFocus,
     user,
     targetFormat,
+    serializer,
   } = props;
-  const editorRef = useRef();
-  let view;
+
+  const WaxEditorRef = useRef();
   const [mounted, setMounted] = useState(false);
   const context = useContext(WaxContext);
   const { createPortal } = useContext(PortalContext);
@@ -48,8 +53,8 @@ export default props => {
   const setEditorRef = useCallback(
     // eslint-disable-next-line consistent-return
     node => {
-      if (editorRef.current) {
-        // this is where you do cleanup if you have to. the editorRef.current will
+      if (WaxEditorRef.current) {
+        // this is where you do cleanup if you have to. the WaxEditorRef.current will
         // still point to the old ref, the old node. so you have some time here to
         // clean up the unmount if you need to.
       }
@@ -70,14 +75,6 @@ export default props => {
             user,
             scrollMargin: 200,
             scrollThreshold: 200,
-            handleDOMEvents: {
-              blur: onBlur
-                ? editorView => {
-                    const serialize = props.serializer(schema);
-                    onBlur(serialize(editorView.state.doc.content));
-                  }
-                : null,
-            },
             transformPasted: slice => {
               return transformPasted(slice, view);
             },
@@ -103,7 +100,7 @@ export default props => {
 
         return () => view.destroy();
       }
-      editorRef.current = node;
+      WaxEditorRef.current = node;
     },
     [readonly],
   );
@@ -112,6 +109,12 @@ export default props => {
     return () => (view = null);
   }, []);
 
+  useImperativeHandle(ref, () => ({
+    getContent() {
+      return getDocContent(schema, serializer, targetFormat, context);
+    },
+  }));
+
   const dispatchTransaction = transaction => {
     const { TrackChange } = props;
     const tr =
@@ -159,4 +162,6 @@ export default props => {
       }),
     [readonly],
   );
-};
+});
+
+export default WaxView;
diff --git a/wax-prosemirror-core/src/helpers/GetDocContent.js b/wax-prosemirror-core/src/helpers/GetDocContent.js
new file mode 100644
index 000000000..baa124a22
--- /dev/null
+++ b/wax-prosemirror-core/src/helpers/GetDocContent.js
@@ -0,0 +1,40 @@
+/* eslint-disable consistent-return */
+/* eslint-disable no-else-return */
+/* eslint-disable no-param-reassign */
+import { each } from 'lodash';
+
+const getDocContent = (schema, serializer, targetFormat, context) => {
+  /* HACK  alter toDOM of footnote, because of how PM treats inline nodes
+      with content */
+  let content = '';
+  const notes = [];
+  each(schema.nodes, node => {
+    if (node.groups.includes('notes')) notes.push(node);
+  });
+
+  if (notes.length > 0) {
+    notes.forEach(note => {
+      schema.nodes[note.name].spec.toDOM = singleNode => {
+        if (singleNode) return [note.name, singleNode.attrs, 0];
+      };
+    });
+  }
+
+  if (targetFormat === 'JSON') {
+    content = context.app.context.view.main.state.doc.content;
+  } else {
+    const serialize = serializer(schema);
+    content = serialize(context.app.context.view.main.state.doc.content);
+  }
+
+  if (notes.length > 0) {
+    notes.forEach(note => {
+      schema.nodes[note.name].spec.toDOM = sinlgeNode => {
+        if (sinlgeNode) return [note.name, sinlgeNode.attrs];
+      };
+    });
+  }
+  return content;
+};
+
+export default getDocContent;
-- 
GitLab