diff --git a/packages/super-editor/src/extensions/comment/comments-plugin.js b/packages/super-editor/src/extensions/comment/comments-plugin.js index 6eada6eed2..6eebade63a 100644 --- a/packages/super-editor/src/extensions/comment/comments-plugin.js +++ b/packages/super-editor/src/extensions/comment/comments-plugin.js @@ -462,7 +462,9 @@ const handleTrackedChangeTransaction = (trackedChangeMeta, trackedChanges, newEd const getTrackedChangeText = ({ node, mark, trackedChangeType, isDeletionInsertion, deletionNodes = [] }) => { const deletionText = deletionNodes.length ? deletionNodes[0].text : null; - let trackedChangeText = isDeletionInsertion ? nextNode.text : node.text; + let nextNode = null; // + let trackedChangeText = isDeletionInsertion ? nextNode?.text : node?.text; + if (!trackedChangeText) trackedChangeText = ''; // If this is a format change, let's get the string of what changes were made const isFormatChange = trackedChangeType === TrackFormatMarkName; @@ -483,9 +485,12 @@ const createOrUpdateTrackedChangeComment = ({ event, marks, deletionNodes, nodes let id = attrs.id; - if (!nodes.length) return; + // if (!nodes.length) { + // return; + // } const node = nodes[0]; + const nextTrackedNode = null; // const isDeletionInsertion = ( trackedChangeType === TrackDeleteMarkName && nextTrackedNode?.type?.name === TrackInsertMarkName ); diff --git a/packages/super-editor/src/index.js b/packages/super-editor/src/index.js index d9e0907dfd..ba59c240fa 100644 --- a/packages/super-editor/src/index.js +++ b/packages/super-editor/src/index.js @@ -18,6 +18,8 @@ import Toolbar from './components/toolbar/Toolbar.vue'; import SuperInput from './components/SuperInput.vue'; import * as fieldAnnotationHelpers from './extensions/field-annotation/fieldAnnotationHelpers/index.js'; import * as trackChangesHelpers from './extensions/track-changes/trackChangesHelpers/index.js'; +import { TrackChangesBasePluginKey } from './extensions/track-changes/plugins/index.js'; +import { CommentsPluginKey } from './extensions/comment/comments-plugin.js'; const Extensions = { Node, @@ -60,4 +62,7 @@ export { // External extensions classes Extensions, + + TrackChangesBasePluginKey, + CommentsPluginKey, }; diff --git a/packages/superdoc/src/SuperDoc.vue b/packages/superdoc/src/SuperDoc.vue index 2e701587c3..72f6638d65 100644 --- a/packages/superdoc/src/SuperDoc.vue +++ b/packages/superdoc/src/SuperDoc.vue @@ -117,6 +117,7 @@ const onCommentsLoaded = ({ editor, comments, replacedFile }) => { nextTick(() => { commentsStore.processLoadedDocxComments({ superdoc: proxy.$superdoc, + editor, comments, documentId: editor.options.documentId }); diff --git a/packages/superdoc/src/stores/comments-store.js b/packages/superdoc/src/stores/comments-store.js index 1c60c02c4f..8d41c14afc 100644 --- a/packages/superdoc/src/stores/comments-store.js +++ b/packages/superdoc/src/stores/comments-store.js @@ -3,7 +3,7 @@ import { ref, reactive, computed } from 'vue'; import { comments_module_events } from '@harbour-enterprises/common'; import { useSuperdocStore } from '@superdoc/stores/superdoc-store'; import { syncCommentsToClients } from '../core/collaboration/helpers.js'; -import { Editor, } from '@harbour-enterprises/super-editor'; +import { Editor, trackChangesHelpers, TrackChangesBasePluginKey, CommentsPluginKey } from '@harbour-enterprises/super-editor'; import { getRichTextExtensions } from '@harbour-enterprises/super-editor'; import useComment from '@superdoc/components/CommentsLayer/use-comment'; @@ -441,7 +441,7 @@ export const useCommentsStore = defineStore('comments', () => { * @param {String} param0.documentId The document ID * @returns {void} */ - const processLoadedDocxComments = async ({ superdoc, comments, documentId }) => { + const processLoadedDocxComments = async ({ superdoc, editor, comments, documentId }) => { const document = superdocStore.getDocument(documentId); if (__IS_DEBUG__) console.debug('[processLoadedDocxComments] processing comments...', comments); @@ -478,6 +478,34 @@ export const useCommentsStore = defineStore('comments', () => { addComment({ superdoc, comment: newComment }); }); + + const trackedChanges = trackChangesHelpers.getTrackChanges(editor.state); + + // Create comments for tracked changes + // that do not have a corresponding comment (created in Word). + trackedChanges.forEach(({ mark }) => { + const foundComment = commentsList.value.find((i) => i.commentId === mark.attrs.id); + + if (foundComment) { + return; + } + + const { dispatch } = editor.view; + const { tr } = editor.view.state; + + const markMetaKeys = { + trackInsert: 'insertedMark', + trackDelete: 'deletionMark', + trackFormat: 'formatMark', + }; + + const markKeyName = markMetaKeys[mark.type.name]; + if (markKeyName) { + tr.setMeta(CommentsPluginKey, { type: 'force' }); + tr.setMeta(TrackChangesBasePluginKey, { [markKeyName]: mark }); + dispatch(tr); + } + }); } const translateCommentsForExport = () => {