From 17037f7d668e931fdd491de103481a59bf76930a Mon Sep 17 00:00:00 2001 From: chris <kokosias@yahoo.gr> Date: Mon, 13 Jul 2020 17:26:43 +0300 Subject: [PATCH] active comment even if it is fragmented --- .../src/comments/CommentPlugin.js | 20 ++++++++----- .../src/document/DocumentHelpers.js | 30 ++++++++++++++++++- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/wax-prosemirror-plugins/src/comments/CommentPlugin.js b/wax-prosemirror-plugins/src/comments/CommentPlugin.js index fbb0a241f..7597afd04 100644 --- a/wax-prosemirror-plugins/src/comments/CommentPlugin.js +++ b/wax-prosemirror-plugins/src/comments/CommentPlugin.js @@ -7,7 +7,19 @@ const commentPlugin = new PluginKey("commentPlugin"); const getComment = state => { const commentMark = state.schema.marks["comment"]; - const commentOnSelection = DocumentHelpers.findMark(state, commentMark); + const commentOnSelection = DocumentHelpers.findFragmentedMark( + state, + commentMark + ); + + // Don't allow Active comment if selection is not collapsed + if ( + state.selection.from !== state.selection.to && + commentOnSelection && + commentOnSelection.attrs.conversation.length + ) { + return; + } if (commentOnSelection) { const commentNodes = DocumentHelpers.findChildrenByMark( @@ -40,12 +52,6 @@ const getComment = state => { }; } } - if ( - state.selection.from !== state.selection.to && - commentOnSelection && - commentOnSelection.attrs.conversation.length - ) - return; return commentOnSelection; }; diff --git a/wax-prosemirror-utilities/src/document/DocumentHelpers.js b/wax-prosemirror-utilities/src/document/DocumentHelpers.js index f29e9bad9..c0a68f5b0 100644 --- a/wax-prosemirror-utilities/src/document/DocumentHelpers.js +++ b/wax-prosemirror-utilities/src/document/DocumentHelpers.js @@ -42,6 +42,33 @@ const getSelectionMark = (state, PMmark) => { return markFound; }; +const findFragmentedMark = (state, PMmark) => { + const { selection: { $from, $to }, doc } = state; + const fromPos = [$from.pos, $from.pos + 1]; + const toPos = [$to.pos, $to.pos + 1]; + let markFound; + + for (let i = 0; i < fromPos.length; i++) { + doc.nodesBetween(fromPos[i], toPos[i], (node, from) => { + if (node.marks) { + const actualMark = node.marks.find(mark => mark.type === PMmark); + if (actualMark) { + markFound = { + from, + to: from + node.nodeSize, + attrs: actualMark.attrs + }; + } + } + }); + if (markFound) { + return markFound; + break; + } + } + return markFound; +}; + export const flatten = (node, descend = true) => { if (!node) { throw new Error('Invalid "node" parameter'); @@ -92,5 +119,6 @@ export default { findInlineNodes, findChildrenByMark, findChildrenByAttr, - getSelectionMark + getSelectionMark, + findFragmentedMark }; -- GitLab