From 42f62fd30cf9066a345aba3928bf4b9b56aa2925 Mon Sep 17 00:00:00 2001
From: chris <kokosias@yahoo.gr>
Date: Thu, 18 Jun 2020 19:15:20 +0300
Subject: [PATCH] show comments as one in plugin

---
 editors/editoria/src/Editoria.js              |  2 +-
 editors/editoria/src/config/config.js         |  4 +--
 .../src/comments/ActiveComment.js             | 33 ++++++++++++++++++-
 3 files changed, 35 insertions(+), 4 deletions(-)

diff --git a/editors/editoria/src/Editoria.js b/editors/editoria/src/Editoria.js
index 375f028f5..c4dc8f5c9 100644
--- a/editors/editoria/src/Editoria.js
+++ b/editors/editoria/src/Editoria.js
@@ -47,7 +47,7 @@ const Editoria = () => (
       autoFocus
       placeholder="Type Something..."
       fileUpload={file => renderImage(file)}
-      value={`<ul><li><p class="paragraph">this</p></li><li><p class="paragraph">that</p></li></ul><p class="paragraph">and a paragraph</p><p>more</p>`}
+      value={`<p class="paragraph"><span class="comment" data-id="834ba3c5-1fcf-4a42-8e2f-1f975f229716" data-conversation="[]" data-group="main">and a </span><span class="insertion" data-id="" data-user="1234" data-username="demo" data-date="26541557" data-group=""><span class="comment" data-id="834ba3c5-1fcf-4a42-8e2f-1f975f229716" data-conversation="[]" data-group="main">sdasdssd</span></span><span class="comment" data-id="834ba3c5-1fcf-4a42-8e2f-1f975f229716" data-conversation="[]" data-group="main">paragraph</span></p><p class="paragraph">more</p>`}
       layout={EditoriaLayout}
       TrackChange
       // onChange={source => console.log(source)}
diff --git a/editors/editoria/src/config/config.js b/editors/editoria/src/config/config.js
index 1e332ab95..95d93f699 100644
--- a/editors/editoria/src/config/config.js
+++ b/editors/editoria/src/config/config.js
@@ -59,6 +59,7 @@ export default {
   // Always load first CommentsService and LinkService,
   //as it matters on how PM treats nodes and marks
   services: [
+    new TrackChangeService(),
     new CommentsService(),
     new LinkService(),
     new PlaceholderService(),
@@ -77,7 +78,6 @@ export default {
     new TextToolGroupService(),
     new AnnotationToolGroupService(),
     new NoteToolGroupService(),
-    new ListToolGroupService(),
-    new TrackChangeService()
+    new ListToolGroupService()
   ]
 };
diff --git a/wax-prosemirror-plugins/src/comments/ActiveComment.js b/wax-prosemirror-plugins/src/comments/ActiveComment.js
index 0fc09d650..652ecfe12 100644
--- a/wax-prosemirror-plugins/src/comments/ActiveComment.js
+++ b/wax-prosemirror-plugins/src/comments/ActiveComment.js
@@ -1,3 +1,4 @@
+import { minBy, maxBy } from "lodash";
 import { Plugin, PluginKey } from "prosemirror-state";
 import { Decoration, DecorationSet } from "prosemirror-view";
 import { DocumentHelpers } from "wax-prosemirror-utilities";
@@ -6,7 +7,37 @@ const activeComment = new PluginKey("activeComment");
 
 const getComment = state => {
   const commentMark = state.schema.marks["comment"];
-  return DocumentHelpers.findMark(state, commentMark);
+  const commentOnSelection = DocumentHelpers.findMark(state, commentMark);
+  if (commentOnSelection) {
+    const allInlineNodes = DocumentHelpers.findInlineNodes(state.doc);
+    const allCommentsWithSameId = [];
+
+    allInlineNodes.map(node => {
+      node.node.marks.filter(mark => {
+        if (
+          mark.type.name === "comment" &&
+          commentOnSelection.attrs.id === mark.attrs.id
+        ) {
+          mark.from = node.pos;
+          mark.to = node.pos + node.node.text.length;
+          allCommentsWithSameId.push(mark);
+        }
+      });
+    });
+    if (allCommentsWithSameId.length > 1) {
+      const minPos = minBy(allCommentsWithSameId, "from");
+      const maxPos = maxBy(allCommentsWithSameId, "to");
+
+      return {
+        from: minPos.from,
+        to: maxPos.to,
+        attrs: commentOnSelection.attrs,
+        contained: commentOnSelection.contained
+      };
+    }
+  }
+
+  return commentOnSelection;
 };
 
 export default props => {
-- 
GitLab