From fb37b116c308b61b9bbacace8655178db82c60e8 Mon Sep 17 00:00:00 2001
From: chris <kokosias@yahoo.gr>
Date: Fri, 10 Jul 2020 14:45:27 +0300
Subject: [PATCH] Link creation fixed

---
 editors/editoria/src/config/config.js         |  2 +-
 .../src/components/link/LinkComponent.js      |  3 ++-
 .../components/trackChanges/TrackChangeBox.js |  1 -
 .../src/OverlayService/usePosition.js         |  2 +-
 .../src/document/DocumentHelpers.js           | 26 ++++++++++++++++---
 5 files changed, 27 insertions(+), 7 deletions(-)

diff --git a/editors/editoria/src/config/config.js b/editors/editoria/src/config/config.js
index 319094f1a..6a29c0f79 100644
--- a/editors/editoria/src/config/config.js
+++ b/editors/editoria/src/config/config.js
@@ -71,9 +71,9 @@ export default {
     new TextBlockLevelService(),
     new TextToolGroupService(),
     new ListsService(),
+    new LinkService(),
     new TrackChangeService(),
     new CommentsService(),
-    new LinkService(),
     new PlaceholderService(),
     new ImageService(),
     new InlineAnnotationsService(),
diff --git a/wax-prosemirror-components/src/components/link/LinkComponent.js b/wax-prosemirror-components/src/components/link/LinkComponent.js
index c44c989fe..7fc43a202 100644
--- a/wax-prosemirror-components/src/components/link/LinkComponent.js
+++ b/wax-prosemirror-components/src/components/link/LinkComponent.js
@@ -43,6 +43,7 @@ const LinkComponent = ({ mark, setPosition, position }) => {
     const href = linkHref;
     const linkMark = state.schema.marks.link;
     const { tr } = state;
+
     dispatch(
       tr.addMark(
         mark.from,
@@ -86,7 +87,7 @@ const LinkComponent = ({ mark, setPosition, position }) => {
   const removeMarkIfEmptyHref = () => {
     const { selection: { $from, $to } } = state;
     const PMLinkMark = state.schema.marks["link"];
-    const actualMark = DocumentHelpers.findMark(state, PMLinkMark);
+    const actualMark = DocumentHelpers.getSelectionMark(state, PMLinkMark);
     setLLastLinkMark(actualMark);
 
     if (
diff --git a/wax-prosemirror-components/src/components/trackChanges/TrackChangeBox.js b/wax-prosemirror-components/src/components/trackChanges/TrackChangeBox.js
index 7c1fadeca..84d2d9b35 100644
--- a/wax-prosemirror-components/src/components/trackChanges/TrackChangeBox.js
+++ b/wax-prosemirror-components/src/components/trackChanges/TrackChangeBox.js
@@ -28,7 +28,6 @@ const TrackChangeBoxStyled = styled.div`
 `;
 
 export default ({ trackChange, view, top, dataBox }) => {
-  console.log(trackChange);
   const [animate, setAnimate] = useState(false);
   const { view: { main }, app, activeView } = useContext(WaxContext);
   let action;
diff --git a/wax-prosemirror-services/src/OverlayService/usePosition.js b/wax-prosemirror-services/src/OverlayService/usePosition.js
index 9768d0aa2..dc2738477 100644
--- a/wax-prosemirror-services/src/OverlayService/usePosition.js
+++ b/wax-prosemirror-services/src/OverlayService/usePosition.js
@@ -55,7 +55,7 @@ export default options => {
   const displayOnMark = (activeView, options) => {
     const { markType, followCursor } = options;
     const PMmark = activeView.state.schema.marks[markType];
-    mark = DocumentHelpers.findMark(activeView.state, PMmark);
+    mark = DocumentHelpers.getSelectionMark(activeView.state, PMmark);
 
     if (!isObject(mark)) return defaultOverlay;
     const { from, to } = followCursor ? activeView.state.selection : mark;
diff --git a/wax-prosemirror-utilities/src/document/DocumentHelpers.js b/wax-prosemirror-utilities/src/document/DocumentHelpers.js
index 37a09a354..1d9d80b91 100644
--- a/wax-prosemirror-utilities/src/document/DocumentHelpers.js
+++ b/wax-prosemirror-utilities/src/document/DocumentHelpers.js
@@ -10,8 +10,8 @@ const findMark = (state, PMmark, toArr = false) => {
       const actualMark = node.marks.find(mark => mark.type === PMmark);
       if (actualMark) {
         markFound = {
-          from,
-          to: from + node.nodeSize,
+          from: $from.pos,
+          to: $to.pos,
           attrs: actualMark.attrs,
           contained: !fromMark || !toMark || fromMark === toMark
         };
@@ -23,6 +23,25 @@ const findMark = (state, PMmark, toArr = false) => {
   return markFound;
 };
 
+const getSelectionMark = (state, PMmark) => {
+  const { selection: { $from, $to }, doc } = state;
+  let markFound;
+  doc.nodesBetween($from.pos, $to.pos, (node, from) => {
+    if (node.marks) {
+      const actualMark = node.marks.find(mark => mark.type === PMmark);
+      if (actualMark) {
+        markFound = {
+          from: $from.pos,
+          to: $to.pos,
+          attrs: actualMark.attrs
+        };
+      }
+    }
+  });
+
+  return markFound;
+};
+
 export const flatten = (node, descend = true) => {
   if (!node) {
     throw new Error('Invalid "node" parameter');
@@ -72,5 +91,6 @@ export default {
   findChildrenByType,
   findInlineNodes,
   findChildrenByMark,
-  findChildrenByAttr
+  findChildrenByAttr,
+  getSelectionMark
 };
-- 
GitLab