@@ -170,7 +170,7 @@ export const CommentsPlugin = Extension.create({
170170
171171 // Check for changes in the actively selected comment
172172 const trChangedActiveComment = meta ?. type === 'setActiveComment' ;
173- if ( ( ! tr . docChanged && tr . selectionSet ) || trChangedActiveComment ) {
173+ if ( ( ! tr . docChanged && tr . selectionSet ) || trChangedActiveComment ) {
174174 const { selection } = tr ;
175175 let currentActiveThread = getActiveCommentId ( newEditorState . doc , selection ) ;
176176 if ( trChangedActiveComment ) currentActiveThread = meta . activeThreadId ;
@@ -212,7 +212,7 @@ export const CommentsPlugin = Extension.create({
212212 const { state } = view
213213 const { doc, tr } = state
214214
215- if ( prevDoc && prevDoc . eq ( doc ) ) shouldUpdate = true ;
215+ if ( prevDoc && ! prevDoc . eq ( doc ) ) shouldUpdate = true ;
216216 if ( ! shouldUpdate ) return ;
217217 prevDoc = doc ;
218218 shouldUpdate = false ;
@@ -254,10 +254,15 @@ export const CommentsPlugin = Extension.create({
254254 decorations . push ( deco )
255255 } ) ;
256256
257- const trackedChangeNode = getTrackedChangeNode ( node ) ;
258- if ( trackedChangeNode ) {
257+ const trackedChangeMark = findTrackedMark ( {
258+ doc,
259+ from : pos ,
260+ to : pos + node . nodeSize ,
261+ } ) ;
262+
263+ if ( trackedChangeMark ) {
259264 const currentBounds = view . coordsAtPos ( pos ) ;
260- const { id } = trackedChangeNode . attrs ;
265+ const { id } = trackedChangeMark . mark . attrs ;
261266 updatePosition ( {
262267 allCommentPositions,
263268 threadId : id ,
@@ -355,9 +360,14 @@ const getActiveCommentId = (doc, selection) => {
355360 if ( ! nodeAtPos ) return ;
356361
357362 // If we have a tracked change, we can return it right away
358- const trackedChangeNode = getTrackedChangeNode ( nodeAtPos ) ;
359- if ( trackedChangeNode ) {
360- return trackedChangeNode . attrs . id ;
363+ const trackedChangeMark = findTrackedMark ( {
364+ doc,
365+ from : $from . pos ,
366+ to : $to . pos ,
367+ } ) ;
368+
369+ if ( trackedChangeMark ) {
370+ return trackedChangeMark . mark . attrs . id ;
361371 }
362372
363373 // Otherwise, we need to check for comment nodes
@@ -412,19 +422,34 @@ const getActiveCommentId = (doc, selection) => {
412422 return closestCommentMark ?. attrs ?. commentId || closestCommentMark ?. attrs ?. importedId ;
413423} ;
414424
425+ const findTrackedMark = ( {
426+ doc,
427+ from,
428+ to,
429+ offset = 1 , // To get non-inclusive marks.
430+ } ) => {
431+ const startPos = Math . max ( from - offset , 0 ) ;
432+ const endPos = Math . min ( to + offset , doc . content . size ) ;
415433
416- /**
417- * Check if this node is a tracked changes node
418- * @param {Node } node The node to check
419- * @returns {Node | null } Either a tracked change node (insert, delete) or null
420- */
421- const getTrackedChangeNode = ( node ) => {
422- if ( ! node ) return ;
423- const nodeMarks = node . marks ;
424- const trackedInsertMark = nodeMarks ?. find ( ( mark ) => mark . type . name === TrackInsertMarkName ) ;
425- const trackedDeleteMark = nodeMarks ?. find ( ( mark ) => mark . type . name === TrackDeleteMarkName ) ;
426- const trackedFormatMark = nodeMarks ?. find ( ( mark ) => mark . type . name === TrackFormatMarkName ) ;
427- return trackedInsertMark || trackedDeleteMark || trackedFormatMark ;
434+ let markFound ;
435+
436+ doc . nodesBetween ( startPos , endPos , ( node , pos ) => {
437+ if ( ! node || node ?. nodeSize === undefined ) {
438+ return ;
439+ }
440+
441+ const mark = node . marks . find ( ( mark ) => TRACK_CHANGE_MARKS . includes ( mark . type . name ) ) ;
442+
443+ if ( mark && ! markFound ) {
444+ markFound = {
445+ from : pos ,
446+ to : pos + node . nodeSize ,
447+ mark,
448+ } ;
449+ }
450+ } ) ;
451+
452+ return markFound ;
428453} ;
429454
430455const handleTrackedChangeTransaction = ( trackedChangeMeta , trackedChanges , newEditorState , editor ) => {
0 commit comments