From 5e38c1126fc8c238870fa6c4f83ddf849bd8f3de Mon Sep 17 00:00:00 2001 From: chris <kokosias@yahoo.gr> Date: Wed, 15 Jul 2020 18:48:32 +0300 Subject: [PATCH] save on fragmented comment --- .../src/components/comments/Comment.js | 25 +++++++++++-------- .../src/document/DocumentHelpers.js | 25 +++++++++++++++++-- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/wax-prosemirror-components/src/components/comments/Comment.js b/wax-prosemirror-components/src/components/comments/Comment.js index e0b9f1205..c1672acb2 100644 --- a/wax-prosemirror-components/src/components/comments/Comment.js +++ b/wax-prosemirror-components/src/components/comments/Comment.js @@ -37,18 +37,21 @@ export default ({ comment, activeView, user }) => { const obj = { [user.username]: value }; commentAnnotation.attrs.conversation.push(obj); - const actualComment = DocumentHelpers.findMark(state, commentMark); - dispatch( - tr.addMark( - actualComment.from, - actualComment.to, - commentMark.create({ - ...((commentAnnotation && commentAnnotation.attrs) || {}), - conversation: commentAnnotation.attrs.conversation - }) - ) - ); + const allComments = DocumentHelpers.findAllCommentsWithSameId(state); + allComments.forEach(singleComment => { + dispatch( + tr.addMark( + singleComment.pos, + singleComment.pos + singleComment.nodeSize, + commentMark.create({ + ...((commentAnnotation && commentAnnotation.attrs) || {}), + conversation: commentAnnotation.attrs.conversation + }) + ) + ); + }); + setcommentInputValue(""); }; diff --git a/wax-prosemirror-utilities/src/document/DocumentHelpers.js b/wax-prosemirror-utilities/src/document/DocumentHelpers.js index b6c479445..f5fa3c1a2 100644 --- a/wax-prosemirror-utilities/src/document/DocumentHelpers.js +++ b/wax-prosemirror-utilities/src/document/DocumentHelpers.js @@ -1,6 +1,5 @@ const findMark = (state, PMmark, toArr = false) => { const { selection: { $from, $to }, doc } = state; - const fromMark = $from.marks().find(mark => mark.type === PMmark); const toMark = $to.marks().find(mark => mark.type === PMmark); let markFound; @@ -72,6 +71,27 @@ const findFragmentedMark = (state, PMmark) => { return markFound; }; +const findAllCommentsWithSameId = state => { + const commentMark = state.schema.marks["comment"]; + const commentOnSelection = findFragmentedMark(state, commentMark); + + const commentNodes = findChildrenByMark(state.doc, commentMark, true); + + const allCommentsWithSameId = []; + commentNodes.map(node => { + node.node.marks.filter(mark => { + if ( + mark.type.name === "comment" && + commentOnSelection.attrs.id === mark.attrs.id + ) { + allCommentsWithSameId.push(node); + } + }); + }); + console.log(allCommentsWithSameId); + return allCommentsWithSameId; +}; + export const flatten = (node, descend = true) => { if (!node) { throw new Error('Invalid "node" parameter'); @@ -123,5 +143,6 @@ export default { findChildrenByMark, findChildrenByAttr, getSelectionMark, - findFragmentedMark + findFragmentedMark, + findAllCommentsWithSameId }; -- GitLab