From 7d9c4b844104073aa602ec28ffb72653d80e9ce0 Mon Sep 17 00:00:00 2001
From: chris <kokosias@yahoo.gr>
Date: Fri, 24 Jul 2020 20:57:35 +0300
Subject: [PATCH] correct carret position on active

---
 .../src/components/comments/Comment.js        |  4 ++--
 .../src/components/comments/CommentBox.js     | 19 ++++++++++---------
 .../src/document/DocumentHelpers.js           |  1 -
 3 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/wax-prosemirror-components/src/components/comments/Comment.js b/wax-prosemirror-components/src/components/comments/Comment.js
index b8e257ea2..27d74073a 100644
--- a/wax-prosemirror-components/src/components/comments/Comment.js
+++ b/wax-prosemirror-components/src/components/comments/Comment.js
@@ -22,7 +22,7 @@ export default ({ comment, activeView, user }) => {
 
   const handleKeyDown = event => {
     if (event.key === 'Enter' || event.which === 13) {
-      saveComment();
+      saveComment(event);
     }
   };
 
@@ -74,7 +74,7 @@ export default ({ comment, activeView, user }) => {
   };
 
   const resolveComment = event => {
-    event.stopPropagation();
+    if (event) event.stopPropagation();
     let maxPos = comment.pos;
     let minPos = comment.pos;
 
diff --git a/wax-prosemirror-components/src/components/comments/CommentBox.js b/wax-prosemirror-components/src/components/comments/CommentBox.js
index 9cb81a741..bd4b9eaaf 100644
--- a/wax-prosemirror-components/src/components/comments/CommentBox.js
+++ b/wax-prosemirror-components/src/components/comments/CommentBox.js
@@ -1,6 +1,6 @@
 import React, { useState, useEffect, useContext } from 'react';
 import { TextSelection } from 'prosemirror-state';
-import { last } from 'lodash';
+import { last, maxBy } from 'lodash';
 
 import { Transition } from 'react-transition-group';
 import styled from 'styled-components';
@@ -59,18 +59,19 @@ export default ({ comment, top, dataBox }) => {
 
   const setCommentActive = () => {
     const viewId = comment.attrs.viewid;
-    let maxPos = comment.pos;
-    const allCommentsWithSameId = DocumentHelpers.findAllMarksWithSameId(view[viewId].state, comment);
 
-    allCommentsWithSameId.forEach(singleComment => {
-      const markPosition = DocumentHelpers.findMarkPosition(view[viewId], singleComment.pos, 'comment');
-      if (markPosition.to > maxPos) maxPos = markPosition.to;
-    });
+    if (active) {
+      view[viewId].focus();
+      return false;
+    }
+
+    const allCommentsWithSameId = DocumentHelpers.findAllMarksWithSameId(view[viewId].state, comment);
 
-    if (!active && allCommentsWithSameId.length > 1) maxPos += last(allCommentsWithSameId).node.nodeSize;
+    const maxPos = maxBy(allCommentsWithSameId, 'pos');
+    maxPos.pos += last(allCommentsWithSameId).node.nodeSize;
 
     view[viewId].dispatch(
-      view[viewId].state.tr.setSelection(new TextSelection(view[viewId].state.tr.doc.resolve(maxPos, maxPos))),
+      view[viewId].state.tr.setSelection(new TextSelection(view[viewId].state.tr.doc.resolve(maxPos.pos, maxPos.pos))),
     );
 
     view[viewId].focus();
diff --git a/wax-prosemirror-utilities/src/document/DocumentHelpers.js b/wax-prosemirror-utilities/src/document/DocumentHelpers.js
index ef355be3d..3e2caffbb 100644
--- a/wax-prosemirror-utilities/src/document/DocumentHelpers.js
+++ b/wax-prosemirror-utilities/src/document/DocumentHelpers.js
@@ -96,7 +96,6 @@ const findAllMarksWithSameId = (state, mark) => {
   return allMarksWithSameId;
 };
 
-// TODO Also find fragmented marks
 const findMarkPosition = (activeView, initialPos, markType) => {
   const $pos = activeView.state.tr.doc.resolve(initialPos);
   const { parent } = $pos;
-- 
GitLab