Skip to content

Commit cc0e294

Browse files
authored
Merge pull request #408 from Harbour-Enterprises/nick/har-9300-floating-comments
Refactor floating comments
2 parents c61fc7e + 5d6f8a3 commit cc0e294

4 files changed

Lines changed: 194 additions & 232 deletions

File tree

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

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,16 @@ export const CommentsPlugin = Extension.create({
166166
};
167167

168168
// Generate decorations for comment highlights
169-
const { decorations, allCommentIds } = processDocumentComments(editor, doc, activeThreadId, oldState) || {};
169+
const {
170+
decorations,
171+
allCommentIds,
172+
allCommentPositions
173+
} = processDocumentComments(editor, doc, activeThreadId, oldState) || {};
170174
const decorationSet = DecorationSet.create(doc, decorations);
171175

172176
// Emit the comment-positions event which signals that comments might have changed
173177
// SuperDoc will use this to update floating comments as necessary
174-
editor.emit('comment-positions', allCommentIds);
178+
editor.emit('comment-positions', { allCommentIds, allCommentPositions });
175179

176180
hasInitialized = true;
177181
return {
@@ -225,6 +229,8 @@ const handleTrackedChangeTransaction = (trackedChangeMeta, trackedChanges, newEd
225229
};
226230
});
227231
}
232+
233+
const coords = editor.view.coordsAtPos(step.to);
228234
const emitParams = createOrUpdateTrackedChangeComment({
229235
documentId: editor.options.documentId,
230236
event: isNewChange ? 'add' : 'update',
@@ -236,6 +242,7 @@ const handleTrackedChangeTransaction = (trackedChangeMeta, trackedChanges, newEd
236242
deletionNodes,
237243
nodes: nodes,
238244
newEditorState,
245+
coords,
239246
});
240247

241248
if (emitParams) editor.emit('commentsUpdate', emitParams);
@@ -258,7 +265,15 @@ const getTrackedChangeText = ({ node, mark, trackedChangeType, isDeletionInserti
258265
}
259266
};
260267

261-
const createOrUpdateTrackedChangeComment = ({ event, marks, deletionNodes, nodes, newEditorState, documentId }) => {
268+
const createOrUpdateTrackedChangeComment = ({
269+
event,
270+
marks,
271+
deletionNodes,
272+
nodes,
273+
newEditorState,
274+
documentId,
275+
coords,
276+
}) => {
262277
const trackedMark = marks.insertedMark || marks.deletionMark || marks.formatMark;
263278
const { type, attrs } = trackedMark;
264279

@@ -306,6 +321,7 @@ const createOrUpdateTrackedChangeComment = ({ event, marks, deletionNodes, nodes
306321
author,
307322
authorEmail,
308323
date,
324+
coords,
309325
};
310326

311327
if (event === 'add') params.event = comments_module_events.ADD;
@@ -357,6 +373,8 @@ const trackCommentNodes = ({
357373
const threadId = attrs.commentId || attrs.importedId;
358374
const isInternal = attrs.internal;
359375
const color = getHighlightColor({ activeThreadId, threadId, isInternal, editor });
376+
377+
const bounds = editor.view.coordsAtPos(pos);
360378
const deco = Decoration.inline(
361379
pos,
362380
pos + node.nodeSize,
@@ -375,8 +393,23 @@ const trackCommentNodes = ({
375393
start: pos,
376394
end: pos + node.nodeSize,
377395
internal: isInternal,
396+
bounds,
378397
};
379398
});
399+
400+
const trackedChangeNode = getTrackedChangeNode(node);
401+
if (trackedChangeNode) {
402+
const threadId = trackedChangeNode.attrs.id;
403+
const bounds = editor.view.coordsAtPos(pos);
404+
405+
allCommentPositions[threadId] = {
406+
threadId,
407+
start: pos,
408+
end: pos + node.nodeSize,
409+
internal: false,
410+
bounds,
411+
};
412+
}
380413
};
381414

382415
/**
@@ -421,6 +454,7 @@ const processDocumentComments = (editor, doc, activeThreadId, pluginState) => {
421454
return {
422455
decorations,
423456
allCommentIds,
457+
allCommentPositions,
424458
};
425459
};
426460

packages/superdoc/src/SuperDoc.vue

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -258,27 +258,9 @@ const editorOptions = (doc) => {
258258
*
259259
* @returns {void}
260260
*/
261-
const onEditorCommentLocationsUpdate = (commentIds = []) => {
261+
const onEditorCommentLocationsUpdate = ({ allCommentIds: commentIds, allCommentPositions }) => {
262262
if (!proxy.$superdoc.config.modules?.comments) return;
263-
264-
// If we have not yet synced the collaboration comments, wait for the sync event
265-
const provider = proxy.$superdoc.provider;
266-
if (provider && !hasSyncedCollaborationComments.value) {
267-
const syncPositions = () => {
268-
handleEditorLocationsUpdate(layers.value, commentIds);
269-
provider.off('synced', syncPositions);
270-
};
271-
272-
provider.on('synced', syncPositions);
273-
setTimeout(() => {
274-
if (!hasSyncedCollaborationComments.value) hasSyncedCollaborationComments.value = true;
275-
}, 1000);
276-
}
277-
278-
// Otherwise, update the comment locations right away
279-
else {
280-
handleEditorLocationsUpdate(layers.value, commentIds);
281-
};
263+
handleEditorLocationsUpdate(allCommentPositions, commentIds);
282264
};
283265
284266
const onEditorCommentsUpdate = (params = {}) => {

0 commit comments

Comments
 (0)