@@ -344,7 +344,7 @@ const getActiveCommentId = (doc, selection) => {
344344 // If we have a tracked change, we can return it right away
345345 const trackedChangeNode = getTrackedChangeNode ( nodeAtPos ) ;
346346 if ( trackedChangeNode ) {
347- return trackedChangeNode . attrs . wid ;
347+ return trackedChangeNode . attrs . id ;
348348 }
349349
350350 // Otherwise, we need to check for comment nodes
@@ -408,19 +408,30 @@ const getActiveCommentId = (doc, selection) => {
408408const getTrackedChangeNode = ( node ) => {
409409 if ( ! node ) return ;
410410 const nodeMarks = node . marks ;
411- const trackedChangeMark = nodeMarks ?. find ( ( mark ) => mark . type . name === TrackInsertMarkName ) ;
411+ const trackedInsertMark = nodeMarks ?. find ( ( mark ) => mark . type . name === TrackInsertMarkName ) ;
412412 const trackedDeleteMark = nodeMarks ?. find ( ( mark ) => mark . type . name === TrackDeleteMarkName ) ;
413413 const trackedFormatMark = nodeMarks ?. find ( ( mark ) => mark . type . name === TrackFormatMarkName ) ;
414- return trackedChangeMark || trackedDeleteMark || trackedFormatMark ;
414+ return trackedInsertMark || trackedDeleteMark || trackedFormatMark ;
415415} ;
416416
417417const handleTrackedChangeTransaction = ( trackedChangeMeta , trackedChanges , newEditorState , editor ) => {
418- const { deletionMark, insertedMark, formatMark, deletionNodes } = trackedChangeMeta ;
419- if ( ! deletionMark && ! insertedMark && ! formatMark ) return ;
418+ const {
419+ insertedMark,
420+ deletionMark,
421+ formatMark,
422+ deletionNodes,
423+ } = trackedChangeMeta ;
424+
425+ if ( ! insertedMark && ! deletionMark && ! formatMark ) {
426+ return ;
427+ }
420428
421429 const newTrackedChanges = { ...trackedChanges } ;
422430 let id = insertedMark ?. attrs ?. id || deletionMark ?. attrs ?. id || formatMark ?. attrs ?. id ;
423- if ( ! id ) return trackedChanges ;
431+
432+ if ( ! id ) {
433+ return trackedChanges ;
434+ }
424435
425436 // Maintain a map of tracked changes with their inserted/deleted ids
426437 let isNewChange = false ;
@@ -446,6 +457,7 @@ const handleTrackedChangeTransaction = (trackedChangeMeta, trackedChanges, newEd
446457 } ;
447458 } ) ;
448459 }
460+
449461 const emitParams = createOrUpdateTrackedChangeComment ( {
450462 documentId : editor . options . documentId ,
451463 event : isNewChange ? 'add' : 'update' ,
@@ -455,7 +467,7 @@ const handleTrackedChangeTransaction = (trackedChangeMeta, trackedChanges, newEd
455467 formatMark,
456468 } ,
457469 deletionNodes,
458- nodes : nodes ,
470+ nodes,
459471 newEditorState,
460472 } ) ;
461473
@@ -464,21 +476,47 @@ const handleTrackedChangeTransaction = (trackedChangeMeta, trackedChanges, newEd
464476 return newTrackedChanges ;
465477} ;
466478
467- const getTrackedChangeText = ( { node, mark, trackedChangeType, isDeletionInsertion, deletionNodes = [ ] } ) => {
468- const deletionText = deletionNodes . length ? deletionNodes [ 0 ] . text : null ;
479+ const getTrackedChangeText = ( {
480+ state,
481+ node,
482+ mark,
483+ marks,
484+ trackedChangeType,
485+ isDeletionInsertion,
486+ deletionNodes = [ ] ,
487+ } ) => {
488+ let trackedChangeText = '' ;
489+ let deletionText = '' ;
469490
470- let nextNode = null ; //
471- let trackedChangeText = isDeletionInsertion ? nextNode ? .text : node ?. text ;
472- if ( ! trackedChangeText ) trackedChangeText = '' ;
491+ if ( trackedChangeType === TrackInsertMarkName ) {
492+ trackedChangeText = node ? .text ?? '' ;
493+ }
473494
474495 // If this is a format change, let's get the string of what changes were made
475- const isFormatChange = trackedChangeType === TrackFormatMarkName ;
476- if ( isFormatChange ) trackedChangeText = translateFormatChangesToEnglish ( mark . attrs )
496+ if ( trackedChangeType === TrackFormatMarkName ) {
497+ trackedChangeText = translateFormatChangesToEnglish ( mark . attrs ) ;
498+ }
499+
500+ if ( trackedChangeType === TrackDeleteMarkName || isDeletionInsertion ) {
501+ deletionText = node ?. text ?? '' ;
502+
503+ if ( isDeletionInsertion ) {
504+ let { id } = marks . deletionMark . attrs ;
505+ let deletionNode = findNode ( state . doc , ( node ) => {
506+ const { marks = [ ] } = node ;
507+ const changeMarks = marks . filter ( ( mark ) => TRACK_CHANGE_MARKS . includes ( mark . type . name ) ) ;
508+ if ( ! changeMarks . length ) return false ;
509+ const hasMatchingId = changeMarks . find ( ( mark ) => mark . attrs . id === id ) ;
510+ if ( hasMatchingId ) return true ;
511+ } ) ;
512+ deletionText = deletionNode ?. node . text ?? '' ;
513+ }
514+ }
477515
478516 return {
479517 deletionText,
480518 trackedChangeText,
481- }
519+ } ;
482520} ;
483521
484522const createOrUpdateTrackedChangeComment = ( { event, marks, deletionNodes, nodes, newEditorState, documentId } ) => {
@@ -487,39 +525,34 @@ const createOrUpdateTrackedChangeComment = ({ event, marks, deletionNodes, nodes
487525
488526 const { name : trackedChangeType } = type ;
489527 const { author, authorEmail, date } = attrs ;
490-
491- let id = attrs . id ;
492-
493- // if (!nodes.length) {
494- // return;
495- // }
528+ const id = attrs . id ;
496529
497530 const node = nodes [ 0 ] ;
498- const nextTrackedNode = null ; //
499- const isDeletionInsertion = (
500- trackedChangeType === TrackDeleteMarkName && nextTrackedNode ?. type ?. name === TrackInsertMarkName
501- ) ;
531+ const isDeletionInsertion = ! ! ( marks . insertedMark && marks . deletionMark ) ;
502532
503533 let existingNode ;
504534 newEditorState . doc . descendants ( ( node , pos ) => {
505535 const { marks = [ ] } = node ;
506536 const changeMarks = marks . filter ( ( mark ) => TRACK_CHANGE_MARKS . includes ( mark . type . name ) ) ;
507537 if ( ! changeMarks . length ) return ;
508-
509538 const hasMatchingId = changeMarks . find ( ( mark ) => mark . attrs . id === id ) ;
510539 if ( hasMatchingId ) existingNode = node ;
511- if ( ! existingNode ) return false ;
540+ if ( existingNode ) return false ;
512541 } ) ;
513542
514543 const { deletionText, trackedChangeText } = getTrackedChangeText ( {
544+ state : newEditorState ,
515545 node : existingNode || node ,
516546 mark : trackedMark ,
547+ marks,
517548 trackedChangeType,
518549 isDeletionInsertion,
519- deletionNodes
550+ deletionNodes,
520551 } ) ;
521552
522- if ( ! deletionText && ! trackedChangeText ) return ;
553+ if ( ! deletionText && ! trackedChangeText ) {
554+ return ;
555+ }
523556
524557 const params = {
525558 event : comments_module_events . ADD ,
@@ -536,5 +569,15 @@ const createOrUpdateTrackedChangeComment = ({ event, marks, deletionNodes, nodes
536569
537570 if ( event === 'add' ) params . event = comments_module_events . ADD ;
538571 else if ( event === 'update' ) params . event = comments_module_events . UPDATE ;
572+
539573 return params ;
540- } ;
574+ } ;
575+
576+ function findNode ( node , predicate ) {
577+ let found = null ;
578+ node . descendants ( ( node , pos ) => {
579+ if ( predicate ( node ) ) found = { node, pos } ;
580+ if ( found ) return false ;
581+ } ) ;
582+ return found ;
583+ }
0 commit comments