Skip to content

Commit e9d8f28

Browse files
committed
create comments for imported tracked changes
1 parent 843b3f2 commit e9d8f28

4 files changed

Lines changed: 43 additions & 4 deletions

File tree

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,9 @@ const handleTrackedChangeTransaction = (trackedChangeMeta, trackedChanges, newEd
462462
const getTrackedChangeText = ({ node, mark, trackedChangeType, isDeletionInsertion, deletionNodes = [] }) => {
463463
const deletionText = deletionNodes.length ? deletionNodes[0].text : null;
464464

465-
let trackedChangeText = isDeletionInsertion ? nextNode.text : node.text;
465+
let nextNode = null; //
466+
let trackedChangeText = isDeletionInsertion ? nextNode?.text : node?.text;
467+
if (!trackedChangeText) trackedChangeText = '';
466468

467469
// If this is a format change, let's get the string of what changes were made
468470
const isFormatChange = trackedChangeType === TrackFormatMarkName;
@@ -483,9 +485,12 @@ const createOrUpdateTrackedChangeComment = ({ event, marks, deletionNodes, nodes
483485

484486
let id = attrs.id;
485487

486-
if (!nodes.length) return;
488+
// if (!nodes.length) {
489+
// return;
490+
// }
487491

488492
const node = nodes[0];
493+
const nextTrackedNode = null; //
489494
const isDeletionInsertion = (
490495
trackedChangeType === TrackDeleteMarkName && nextTrackedNode?.type?.name === TrackInsertMarkName
491496
);

packages/super-editor/src/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import Toolbar from './components/toolbar/Toolbar.vue';
1818
import SuperInput from './components/SuperInput.vue';
1919
import * as fieldAnnotationHelpers from './extensions/field-annotation/fieldAnnotationHelpers/index.js';
2020
import * as trackChangesHelpers from './extensions/track-changes/trackChangesHelpers/index.js';
21+
import { TrackChangesBasePluginKey } from './extensions/track-changes/plugins/index.js';
22+
import { CommentsPluginKey } from './extensions/comment/comments-plugin.js';
2123

2224
const Extensions = {
2325
Node,
@@ -60,4 +62,7 @@ export {
6062

6163
// External extensions classes
6264
Extensions,
65+
66+
TrackChangesBasePluginKey,
67+
CommentsPluginKey,
6368
};

packages/superdoc/src/SuperDoc.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ const onCommentsLoaded = ({ editor, comments, replacedFile }) => {
117117
nextTick(() => {
118118
commentsStore.processLoadedDocxComments({
119119
superdoc: proxy.$superdoc,
120+
editor,
120121
comments,
121122
documentId: editor.options.documentId
122123
});

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ref, reactive, computed } from 'vue';
33
import { comments_module_events } from '@harbour-enterprises/common';
44
import { useSuperdocStore } from '@superdoc/stores/superdoc-store';
55
import { syncCommentsToClients } from '../core/collaboration/helpers.js';
6-
import { Editor, } from '@harbour-enterprises/super-editor';
6+
import { Editor, trackChangesHelpers, TrackChangesBasePluginKey, CommentsPluginKey } from '@harbour-enterprises/super-editor';
77
import { getRichTextExtensions } from '@harbour-enterprises/super-editor';
88
import useComment from '@superdoc/components/CommentsLayer/use-comment';
99

@@ -441,7 +441,7 @@ export const useCommentsStore = defineStore('comments', () => {
441441
* @param {String} param0.documentId The document ID
442442
* @returns {void}
443443
*/
444-
const processLoadedDocxComments = async ({ superdoc, comments, documentId }) => {
444+
const processLoadedDocxComments = async ({ superdoc, editor, comments, documentId }) => {
445445
const document = superdocStore.getDocument(documentId);
446446

447447
if (__IS_DEBUG__) console.debug('[processLoadedDocxComments] processing comments...', comments);
@@ -478,6 +478,34 @@ export const useCommentsStore = defineStore('comments', () => {
478478

479479
addComment({ superdoc, comment: newComment });
480480
});
481+
482+
const trackedChanges = trackChangesHelpers.getTrackChanges(editor.state);
483+
484+
// Create comments for tracked changes
485+
// that do not have a corresponding comment (created in Word).
486+
trackedChanges.forEach(({ mark }) => {
487+
const foundComment = commentsList.value.find((i) => i.commentId === mark.attrs.id);
488+
489+
if (foundComment) {
490+
return;
491+
}
492+
493+
const { dispatch } = editor.view;
494+
const { tr } = editor.view.state;
495+
496+
const markMetaKeys = {
497+
trackInsert: 'insertedMark',
498+
trackDelete: 'deletionMark',
499+
trackFormat: 'formatMark',
500+
};
501+
502+
const markKeyName = markMetaKeys[mark.type.name];
503+
if (markKeyName) {
504+
tr.setMeta(CommentsPluginKey, { type: 'force' });
505+
tr.setMeta(TrackChangesBasePluginKey, { [markKeyName]: mark });
506+
dispatch(tr);
507+
}
508+
});
481509
}
482510

483511
const translateCommentsForExport = () => {

0 commit comments

Comments
 (0)