From eea5f8ee973218c3784266cb0a8162894cfc3dd3 Mon Sep 17 00:00:00 2001
From: chris <kokosias@yahoo.gr>
Date: Mon, 18 Oct 2021 19:59:47 +0300
Subject: [PATCH] add copy paste comment plugin

---
 editors/demo/src/Editoria/Editoria.js         |  5 --
 editors/demo/src/HHMI/HHMI.js                 |  2 +-
 wax-prosemirror-core/src/PmPlugins.js         |  2 +-
 .../src/helpers/TransformPasted.js            | 31 +-----------
 wax-prosemirror-plugins/index.js              |  2 +
 .../src/comments/CopyPasteCommentPlugin.js    | 50 +++++++++++++++++++
 .../src/CommentsService/CommentsService.js    |  6 ++-
 .../src/NoteService/Editor.js                 |  4 --
 .../NoteService/helpers/TransformPasted.js    | 38 --------------
 9 files changed, 60 insertions(+), 80 deletions(-)
 create mode 100644 wax-prosemirror-plugins/src/comments/CopyPasteCommentPlugin.js
 delete mode 100644 wax-prosemirror-services/src/NoteService/helpers/TransformPasted.js

diff --git a/editors/demo/src/Editoria/Editoria.js b/editors/demo/src/Editoria/Editoria.js
index 5bd375095..4293bf1a1 100644
--- a/editors/demo/src/Editoria/Editoria.js
+++ b/editors/demo/src/Editoria/Editoria.js
@@ -43,10 +43,6 @@ const Editoria = () => {
   const EditoriaComponent = useMemo(
     () => (
       <>
-        <button onClick={() => console.log(editorRef.current.getContent())}>
-          Click
-        </button>
-
         <Wax
           ref={editorRef}
           key={key}
@@ -60,7 +56,6 @@ const Editoria = () => {
           // onChange={debounce(source => {
           //   console.log(JSON.stringify(source));
           // }, 200)}
-          // onBlur={source => console.log(source)}
           user={user}
         />
       </>
diff --git a/editors/demo/src/HHMI/HHMI.js b/editors/demo/src/HHMI/HHMI.js
index bbd32b6ea..ef7845bf4 100644
--- a/editors/demo/src/HHMI/HHMI.js
+++ b/editors/demo/src/HHMI/HHMI.js
@@ -24,7 +24,7 @@ const Hhmi = () => {
         config={config}
         autoFocus
         fileUpload={file => renderImage(file)}
-        value={t}
+        value=""
         // readonly
         layout={HhmiLayout}
         // onChange={source => console.log(source)}
diff --git a/wax-prosemirror-core/src/PmPlugins.js b/wax-prosemirror-core/src/PmPlugins.js
index ac5b5bf56..98ccb800c 100644
--- a/wax-prosemirror-core/src/PmPlugins.js
+++ b/wax-prosemirror-core/src/PmPlugins.js
@@ -1,4 +1,4 @@
-import { injectable } from "inversify";
+import { injectable } from 'inversify';
 @injectable()
 export default class PmPlugins {
   _plugins = new Map();
diff --git a/wax-prosemirror-core/src/helpers/TransformPasted.js b/wax-prosemirror-core/src/helpers/TransformPasted.js
index 9a4ce541f..4bdfdd9b9 100644
--- a/wax-prosemirror-core/src/helpers/TransformPasted.js
+++ b/wax-prosemirror-core/src/helpers/TransformPasted.js
@@ -1,40 +1,11 @@
 /* eslint-disable array-callback-return */
 /* eslint-disable no-param-reassign */
-import { forEach, each } from 'lodash';
+import { each } from 'lodash';
 import { v4 as uuidv4 } from 'uuid';
 import { DocumentHelpers } from 'wax-prosemirror-utilities';
 
 const transformPasted = (slice, view) => {
   const { content } = slice;
-  if (view.state.schema.marks.comment) {
-    const commentNodes = DocumentHelpers.findChildrenByMark(
-      content,
-      view.state.schema.marks.comment,
-      true,
-    );
-
-    const allComments = [];
-
-    commentNodes.map(node => {
-      node.node.marks.map(comment => {
-        if (comment.type.name === 'comment') {
-          allComments.push(comment);
-        }
-      });
-    });
-
-    const groupedCommentsById = allComments.reduce((obj, mark) => {
-      obj[mark.attrs.id] = [...(obj[mark.attrs.id] || []), mark];
-      return obj;
-    }, {});
-
-    forEach(Object.keys(groupedCommentsById), key => {
-      const id = uuidv4();
-      forEach(groupedCommentsById[key], comment => {
-        comment.attrs.id = id;
-      });
-    });
-  }
 
   const schemaNotes = [];
   each(view.state.schema.nodes, node => {
diff --git a/wax-prosemirror-plugins/index.js b/wax-prosemirror-plugins/index.js
index d0a198c76..431000b44 100644
--- a/wax-prosemirror-plugins/index.js
+++ b/wax-prosemirror-plugins/index.js
@@ -2,6 +2,8 @@ export { default as TrackChangePlugin } from './src/trackChanges/TrackChangePlug
 export { default as HideShowPlugin } from './src/trackChanges/HideShowPlugin';
 
 export { default as CommentPlugin } from './src/comments/CommentPlugin';
+export { default as CopyPasteCommentPlugin } from './src/comments/CopyPasteCommentPlugin';
+
 export { default as WaxSelectionPlugin } from './src/WaxSelectionPlugin';
 export { default as highlightPlugin } from './src/highlightPlugin';
 
diff --git a/wax-prosemirror-plugins/src/comments/CopyPasteCommentPlugin.js b/wax-prosemirror-plugins/src/comments/CopyPasteCommentPlugin.js
new file mode 100644
index 000000000..00c5d6379
--- /dev/null
+++ b/wax-prosemirror-plugins/src/comments/CopyPasteCommentPlugin.js
@@ -0,0 +1,50 @@
+/* eslint-disable */
+import { forEach } from 'lodash';
+import { v4 as uuidv4 } from 'uuid';
+import { Plugin, PluginKey } from 'prosemirror-state';
+import { DocumentHelpers } from 'wax-prosemirror-utilities';
+
+const copyPasteCommentPugin = new PluginKey('copyPasteCommentPugin');
+
+export default (props, context) => {
+  return new Plugin({
+    key: copyPasteCommentPugin,
+    props: {
+      transformPasted: slice => {
+        const { activeView } = context.app.context;
+        console.log(activeView);
+        const { content } = slice;
+        console.log(slice);
+        const commentNodes = DocumentHelpers.findChildrenByMark(
+          content,
+          activeView.state.schema.marks.comment,
+          true,
+        );
+
+        const allComments = [];
+
+        commentNodes.map(node => {
+          node.node.marks.map(comment => {
+            if (comment.type.name === 'comment') {
+              allComments.push(comment);
+            }
+          });
+        });
+
+        const groupedCommentsById = allComments.reduce((obj, mark) => {
+          obj[mark.attrs.id] = [...(obj[mark.attrs.id] || []), mark];
+          return obj;
+        }, {});
+
+        forEach(Object.keys(groupedCommentsById), key => {
+          const id = uuidv4();
+          forEach(groupedCommentsById[key], comment => {
+            comment.attrs.id = id;
+          });
+        });
+
+        return slice;
+      },
+    },
+  });
+};
diff --git a/wax-prosemirror-services/src/CommentsService/CommentsService.js b/wax-prosemirror-services/src/CommentsService/CommentsService.js
index 6a2b16d5f..11e5ca070 100644
--- a/wax-prosemirror-services/src/CommentsService/CommentsService.js
+++ b/wax-prosemirror-services/src/CommentsService/CommentsService.js
@@ -1,6 +1,6 @@
 import { commentMark } from 'wax-prosemirror-schema';
 import { RightArea, CommentBubbleComponent } from 'wax-prosemirror-components';
-import { CommentPlugin } from 'wax-prosemirror-plugins';
+import { CommentPlugin, CopyPasteCommentPlugin } from 'wax-prosemirror-plugins';
 import Service from '../Service';
 
 const PLUGIN_KEY = 'commentPlugin';
@@ -8,6 +8,10 @@ const PLUGIN_KEY = 'commentPlugin';
 export default class CommentsService extends Service {
   boot() {
     this.app.PmPlugins.add(PLUGIN_KEY, CommentPlugin(PLUGIN_KEY));
+    this.app.PmPlugins.add(
+      'copyPasteCommentPlugin',
+      CopyPasteCommentPlugin('copyPasteCommentPlugin', this.app.context),
+    );
     const createOverlay = this.container.get('CreateOverlay');
     const layout = this.container.get('Layout');
     createOverlay(
diff --git a/wax-prosemirror-services/src/NoteService/Editor.js b/wax-prosemirror-services/src/NoteService/Editor.js
index 5fffc0b55..30d736d6e 100644
--- a/wax-prosemirror-services/src/NoteService/Editor.js
+++ b/wax-prosemirror-services/src/NoteService/Editor.js
@@ -11,7 +11,6 @@ import { undo, redo } from 'prosemirror-history';
 import { WaxContext } from 'wax-prosemirror-core';
 import { NoteEditorContainer } from 'wax-prosemirror-components';
 import { DocumentHelpers } from 'wax-prosemirror-utilities';
-import transformPasted from './helpers/TransformPasted';
 import trackedTransaction from '../TrackChangeService/track-changes/trackedTransaction';
 
 export default ({ node, view }) => {
@@ -63,9 +62,6 @@ export default ({ node, view }) => {
         handleTextInput: (editorView, from, to, text) => {
           typing = true;
         },
-        transformPasted: slice => {
-          return transformPasted(slice, noteView);
-        },
 
         attributes: {
           spellcheck: 'false',
diff --git a/wax-prosemirror-services/src/NoteService/helpers/TransformPasted.js b/wax-prosemirror-services/src/NoteService/helpers/TransformPasted.js
deleted file mode 100644
index 353c11a4b..000000000
--- a/wax-prosemirror-services/src/NoteService/helpers/TransformPasted.js
+++ /dev/null
@@ -1,38 +0,0 @@
-import { forEach } from "lodash";
-import { v4 as uuidv4 } from "uuid";
-import { DocumentHelpers } from "wax-prosemirror-utilities";
-
-const transformPasted = (slice, view) => {
-  const { content } = slice;
-  const commentNodes = DocumentHelpers.findChildrenByMark(
-    content,
-    view.state.schema.marks.comment,
-    true
-  );
-
-  const allComments = [];
-
-  commentNodes.map(node => {
-    node.node.marks.map(comment => {
-      if (comment.type.name === "comment") {
-        allComments.push(comment);
-      }
-    });
-  });
-
-  let groupedCommentsById = allComments.reduce((obj, mark) => {
-    obj[mark.attrs.id] = [...(obj[mark.attrs.id] || []), mark];
-    return obj;
-  }, {});
-
-  forEach(Object.keys(groupedCommentsById), key => {
-    let id = uuidv4();
-    forEach(groupedCommentsById[key], comment => {
-      comment.attrs.id = id;
-    });
-  });
-
-  return slice;
-};
-
-export default transformPasted;
-- 
GitLab