Skip to content

Commit b4b9137

Browse files
authored
Merge pull request #445 from Harbour-Enterprises/artem-HAR-9380-v3
HAR-9380 - show floating comments instantly for tracked changes
2 parents 9f2560d + 5639285 commit b4b9137

4 files changed

Lines changed: 51 additions & 20 deletions

File tree

packages/super-editor/src/extensions/comment/comments-plugin.js

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ export const CommentsPlugin = Extension.create({
170170

171171
// Check for changes in the actively selected comment
172172
const trChangedActiveComment = meta?.type === 'setActiveComment';
173-
if ((!tr.docChanged && tr.selectionSet) || trChangedActiveComment) {
173+
if ((!tr.docChanged && tr.selectionSet) || trChangedActiveComment) {
174174
const { selection } = tr;
175175
let currentActiveThread = getActiveCommentId(newEditorState.doc, selection);
176176
if (trChangedActiveComment) currentActiveThread = meta.activeThreadId;
@@ -212,7 +212,7 @@ export const CommentsPlugin = Extension.create({
212212
const { state } = view
213213
const { doc, tr } = state
214214

215-
if (prevDoc && prevDoc.eq(doc)) shouldUpdate = true;
215+
if (prevDoc && !prevDoc.eq(doc)) shouldUpdate = true;
216216
if (!shouldUpdate) return;
217217
prevDoc = doc;
218218
shouldUpdate = false;
@@ -254,10 +254,15 @@ export const CommentsPlugin = Extension.create({
254254
decorations.push(deco)
255255
});
256256

257-
const trackedChangeNode = getTrackedChangeNode(node);
258-
if (trackedChangeNode) {
257+
const trackedChangeMark = findTrackedMark({
258+
doc,
259+
from: pos,
260+
to: pos + node.nodeSize,
261+
});
262+
263+
if (trackedChangeMark) {
259264
const currentBounds = view.coordsAtPos(pos);
260-
const { id } = trackedChangeNode.attrs;
265+
const { id } = trackedChangeMark.mark.attrs;
261266
updatePosition({
262267
allCommentPositions,
263268
threadId: id,
@@ -355,9 +360,14 @@ const getActiveCommentId = (doc, selection) => {
355360
if (!nodeAtPos) return;
356361

357362
// If we have a tracked change, we can return it right away
358-
const trackedChangeNode = getTrackedChangeNode(nodeAtPos);
359-
if (trackedChangeNode) {
360-
return trackedChangeNode.attrs.id;
363+
const trackedChangeMark = findTrackedMark({
364+
doc,
365+
from: $from.pos,
366+
to: $to.pos,
367+
});
368+
369+
if (trackedChangeMark) {
370+
return trackedChangeMark.mark.attrs.id;
361371
}
362372

363373
// Otherwise, we need to check for comment nodes
@@ -412,19 +422,34 @@ const getActiveCommentId = (doc, selection) => {
412422
return closestCommentMark?.attrs?.commentId || closestCommentMark?.attrs?.importedId;
413423
};
414424

425+
const findTrackedMark = ({
426+
doc,
427+
from,
428+
to,
429+
offset = 1, // To get non-inclusive marks.
430+
}) => {
431+
const startPos = Math.max(from - offset, 0);
432+
const endPos = Math.min(to + offset, doc.content.size);
415433

416-
/**
417-
* Check if this node is a tracked changes node
418-
* @param {Node} node The node to check
419-
* @returns {Node | null} Either a tracked change node (insert, delete) or null
420-
*/
421-
const getTrackedChangeNode = (node) => {
422-
if (!node) return;
423-
const nodeMarks = node.marks;
424-
const trackedInsertMark = nodeMarks?.find((mark) => mark.type.name === TrackInsertMarkName);
425-
const trackedDeleteMark = nodeMarks?.find((mark) => mark.type.name === TrackDeleteMarkName);
426-
const trackedFormatMark = nodeMarks?.find((mark) => mark.type.name === TrackFormatMarkName);
427-
return trackedInsertMark || trackedDeleteMark || trackedFormatMark;
434+
let markFound;
435+
436+
doc.nodesBetween(startPos, endPos, (node, pos) => {
437+
if (!node || node?.nodeSize === undefined) {
438+
return;
439+
}
440+
441+
const mark = node.marks.find((mark) => TRACK_CHANGE_MARKS.includes(mark.type.name));
442+
443+
if (mark && !markFound) {
444+
markFound = {
445+
from: pos,
446+
to: pos + node.nodeSize,
447+
mark,
448+
};
449+
}
450+
});
451+
452+
return markFound;
428453
};
429454

430455
const handleTrackedChangeTransaction = (trackedChangeMeta, trackedChanges, newEditorState, editor) => {

packages/super-editor/src/extensions/track-changes/trackChangesHelpers/addMarkStep.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { TrackDeleteMarkName, TrackFormatMarkName } from '../constants.js';
55
import { v4 as uuidv4 } from 'uuid';
66
import { objectIncludes } from '@core/utilities/objectIncludes.js';
77
import { TrackChangesBasePluginKey } from '../plugins/trackChangesBasePlugin.js';
8+
import { CommentsPluginKey } from '../../comment/comments-plugin.js';
89

910
/**
1011
* Add mark step.
@@ -97,6 +98,7 @@ export const addMarkStep = ({ state, tr, step, newTr, map, doc, user, date }) =>
9798
meta.step = step;
9899

99100
newTr.setMeta(TrackChangesBasePluginKey, meta);
101+
newTr.setMeta(CommentsPluginKey, { type: 'force' });
100102
} else if (formatChangeMark) {
101103
newTr.removeMark(Math.max(step.from, pos), Math.min(step.to, pos + node.nodeSize), formatChangeMark);
102104
}

packages/super-editor/src/extensions/track-changes/trackChangesHelpers/removeMarkStep.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Node } from 'prosemirror-model';
44
import { v4 as uuidv4 } from 'uuid';
55
import { TrackDeleteMarkName, TrackFormatMarkName } from '../constants.js';
66
import { TrackChangesBasePluginKey } from '../plugins/trackChangesBasePlugin.js';
7+
import { CommentsPluginKey } from '../../comment/comments-plugin.js';
78

89
/**
910
* Remove mark step.
@@ -84,6 +85,7 @@ export const removeMarkStep = ({ state, tr, step, newTr, map, doc, user, date })
8485
meta.step = step;
8586

8687
newTr.setMeta(TrackChangesBasePluginKey, meta);
88+
newTr.setMeta(CommentsPluginKey, { type: 'force' });
8789
} else if (formatChangeMark) {
8890
newTr.removeMark(Math.max(step.from, pos), Math.min(step.to, pos + node.nodeSize), formatChangeMark);
8991
}

packages/super-editor/src/extensions/track-changes/trackChangesHelpers/replaceStep.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { markDeletion } from './markDeletion.js';
66
import { findMark } from '@core/helpers/index.js';
77
import { TrackDeleteMarkName } from '../constants.js';
88
import { TrackChangesBasePluginKey } from '../plugins/index.js';
9+
import { CommentsPluginKey } from '../../comment/comments-plugin.js';
910

1011
/**
1112
* Replace step.
@@ -88,4 +89,5 @@ export const replaceStep = ({ state, tr, step, newTr, map, doc, user, date, orig
8889

8990
// Add meta to the new transaction.
9091
newTr.setMeta(TrackChangesBasePluginKey, meta);
92+
newTr.setMeta(CommentsPluginKey, { type: 'force' });
9193
};

0 commit comments

Comments
 (0)