Skip to content

Commit 0854d1f

Browse files
authored
Merge pull request #416 from Harbour-Enterprises/nick/fix-initial-comment-import-in-collab
Fix initial loading of imported comments issues in collaboration
2 parents 141f54c + a3a585b commit 0854d1f

4 files changed

Lines changed: 40 additions & 24 deletions

File tree

packages/super-editor/src/core/Editor.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -315,16 +315,16 @@ export class Editor extends EventEmitter {
315315
* If we are replacing data and have a valid provider, listen for synced event
316316
* so that we can initialize the data
317317
*/
318-
initializeCollaborationData(replacedFile = false) {
318+
initializeCollaborationData() {
319319
if (!this.options.isNewFile || !this.options.collaborationProvider) return;
320320
const { collaborationProvider: provider } = this.options;
321321

322322
const postSyncInit = () => {
323323
provider.off('synced', postSyncInit);
324-
this.#insertNewFileData(replacedFile);
324+
this.#insertNewFileData();
325325
};
326326

327-
if (provider.synced) this.#insertNewFileData(replacedFile);
327+
if (provider.synced) this.#insertNewFileData();
328328
// If we are not sync'd yet, wait for the event then insert the data
329329
else provider.on('synced', postSyncInit);
330330
}
@@ -333,17 +333,17 @@ export class Editor extends EventEmitter {
333333
* Replace the current document with new data. Necessary for initializing a new collaboration file,
334334
* since we need to insert the data only after the provider has synced.
335335
*/
336-
#insertNewFileData(replacedFile = false) {
336+
#insertNewFileData() {
337337
if (!this.options.isNewFile) return;
338338
this.options.isNewFile = false;
339-
const doc = this.#generatePmData();
339+
const doc = this.#generatePmData(true);
340340
const tr = this.state.tr.replaceWith(0, this.state.doc.content.size, doc);
341341
this.view.dispatch(tr);
342342

343-
if (this.options.collaborationIsReady) {
343+
setTimeout(() => {
344344
this.#initPagination();
345-
this.#initComments(replacedFile);
346-
}
345+
this.#initComments();
346+
}, 50);
347347
}
348348

349349
#registerPluginByNameIfNotExists(name) {
@@ -522,7 +522,7 @@ export class Editor extends EventEmitter {
522522
/**
523523
* Generate data from file
524524
*/
525-
#generatePmData() {
525+
#generatePmData(includeComments) {
526526
let doc;
527527

528528
try {
@@ -532,7 +532,7 @@ export class Editor extends EventEmitter {
532532
doc = createDocument(this.converter, this.schema, this);
533533

534534
// Perform any additional document processing prior to finalizing the doc here
535-
doc = this.#prepareDocumentForImport(doc);
535+
if (includeComments) doc = this.#prepareDocumentForImport(doc);
536536

537537
if (fragment && isHeadless) {
538538
doc = yXmlFragmentToProseMirrorRootNode(fragment, this.schema);
@@ -721,20 +721,25 @@ export class Editor extends EventEmitter {
721721

722722
this.options.onCollaborationReady({ editor, ydoc });
723723
this.options.collaborationIsReady = true;
724-
this.#initPagination();
725-
this.#initComments();
724+
725+
if (!this.options.isNewFile) {
726+
this.#initPagination();
727+
this.#initComments();
728+
}
726729
};
727730

728731
/**
729732
* Initialize comments plugin
730733
*/
731-
#initComments(replacedFile = false) {
734+
#initComments() {
732735
if (!this.options.isCommentsEnabled) return;
733736
if (this.options.isHeadless) return;
734737
if (!this.options.shouldLoadComments) return;
738+
const replacedFile = this.options.replacedFile;
735739
this.emit('commentsLoaded', { editor: this, replacedFile, comments: this.converter.comments || [] });
736740

737741
setTimeout(() => {
742+
this.options.replacedFile = false;
738743
const { state, dispatch } = this.view;
739744
const tr = state.tr.setMeta(CommentsPluginKey, { type: 'force' });
740745
dispatch(tr);
@@ -1084,6 +1089,7 @@ export class Editor extends EventEmitter {
10841089
fonts,
10851090
isNewFile: true,
10861091
shouldLoadComments: true,
1092+
replacedFile: true,
10871093
});
10881094

10891095
this.#createConverter();
@@ -1094,7 +1100,7 @@ export class Editor extends EventEmitter {
10941100

10951101
if (!this.options.ydoc) {
10961102
this.#initPagination();
1097-
this.#initComments(true);
1103+
this.#initComments();
10981104
};
10991105

11001106
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,11 @@ export const CommentsPlugin = Extension.create({
142142
if (isPaginationInit) shouldUpdate = true;
143143

144144
const meta = tr.getMeta(CommentsPluginKey);
145-
if (!isPaginationInit && meta && meta.decorations) {
145+
const { type } = meta || {};
146+
147+
if (type === 'force') shouldUpdate = true;
148+
149+
if (!isPaginationInit && !shouldUpdate && meta && meta.decorations) {
146150
return {
147151
...pluginState,
148152
decorations: meta.decorations,
@@ -164,7 +168,7 @@ export const CommentsPlugin = Extension.create({
164168

165169
// Check for changes in the actively selected comment
166170
const trChangedActiveComment = meta?.type === 'setActiveComment';
167-
if ((!tr.docChanged && tr.selectionSet) || trChangedActiveComment) {
171+
if ((!tr.docChanged && tr.selectionSet) || trChangedActiveComment) {
168172
const { selection } = tr;
169173
const currentActiveThread = getActiveCommentId(newEditorState.doc, selection);
170174
if (trChangedActiveComment) activeThreadId = meta.activeThreadId;
@@ -206,6 +210,7 @@ export const CommentsPlugin = Extension.create({
206210

207211
if (prevDoc && prevDoc.eq(doc) && !shouldUpdate) return;
208212
prevDoc = doc;
213+
shouldUpdate = false;
209214

210215
const decorations = []
211216
const allCommentPositions = {}
@@ -275,7 +280,6 @@ export const CommentsPlugin = Extension.create({
275280

276281
// Remember the new decorations for next time
277282
prevDecorations = decorationSet
278-
shouldUpdate = false;
279283
},
280284
}
281285
},

packages/superdoc/src/SuperDoc.vue

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ const {
5959
generalCommentIds,
6060
getFloatingComments,
6161
hasSyncedCollaborationComments,
62+
editorCommentPositions,
6263
hasInitializedLocations,
6364
} = storeToRefs(commentsStore);
6465
const { initialCheck, showAddComment, handleEditorLocationsUpdate, handleTrackedChangeUpdate } = commentsStore;
@@ -491,6 +492,13 @@ const handlePdfClick = (e) => {
491492
isDragging.value = true;
492493
handleSelectionStart(e);
493494
};
495+
496+
watch(getFloatingComments, () => {
497+
hasInitializedLocations.value = false;
498+
nextTick(() => {
499+
hasInitializedLocations.value = true;
500+
});
501+
});
494502
</script>
495503
496504
<template>

packages/superdoc/src/stores/comments-store.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ export const useCommentsStore = defineStore('comments', () => {
383383
};
384384

385385
const event = { type: COMMENT_EVENTS.ADD, comment: newComment.getValues() };
386-
386+
387387
// If collaboration is enabled, sync the comments to all clients
388388
syncCommentsToClients(superdoc, event);
389389

@@ -502,18 +502,16 @@ export const useCommentsStore = defineStore('comments', () => {
502502
* @returns {void}
503503
*/
504504
const handleEditorLocationsUpdate = (allCommentPositions, commentIds) => {
505-
hasInitializedLocations.value = false;
506505
editorCommentPositions.value = allCommentPositions;
507-
508-
setTimeout(() => {
509-
hasInitializedLocations.value = true;
510-
}, 50);
511506
};
512507

513508
const getFloatingComments = computed(() => {
514509
const comments = getGroupedComments.value?.parentComments
515510
.filter((c) => !c.resolvedTime)
516-
.filter((c) => Object.keys(editorCommentPositions.value).includes(c.commentId || c.importedId))
511+
.filter((c) => {
512+
const keys = Object.keys(editorCommentPositions.value);
513+
return keys.includes(c.commentId);
514+
});
517515
return comments;
518516
});
519517

0 commit comments

Comments
 (0)