11// @ts -nocheck
2+ import { TextSelection } from 'prosemirror-state' ;
23import { history , redo as originalRedo , undo as originalUndo } from 'prosemirror-history' ;
34import { undo as yUndo , redo as yRedo , yUndoPlugin } from 'y-prosemirror' ;
45import { Extension } from '@core/Extension.js' ;
6+ import { CustomSelectionPluginKey , DEFAULT_SELECTION_STATE } from '../custom-selection/custom-selection.js' ;
7+
8+ function applySelectionCleanup ( editor , tr ) {
9+ let cleaned = tr . setMeta ( CustomSelectionPluginKey , DEFAULT_SELECTION_STATE ) ;
10+
11+ const sel = cleaned . selection ;
12+ if ( sel && sel instanceof TextSelection && ! sel . empty ) {
13+ try {
14+ const collapsed = TextSelection . create ( cleaned . doc , sel . head ) ;
15+ cleaned = cleaned . setSelection ( collapsed ) ;
16+ } catch {
17+ // Ignore collapse failures and fall back to original selection
18+ }
19+ }
20+
21+ editor . setOptions ( {
22+ preservedSelection : null ,
23+ lastSelection : null ,
24+ } ) ;
25+
26+ return cleaned ;
27+ }
28+
29+ function createHistoryDispatch ( editor , dispatch ) {
30+ if ( ! dispatch ) return dispatch ;
31+ return ( historyTr ) => {
32+ const cleaned = applySelectionCleanup ( editor , historyTr ) ;
33+ dispatch ( cleaned ) ;
34+ } ;
35+ }
36+
37+ function runSelectionCleanupAfterCollabHistory ( editor ) {
38+ const view = editor ?. view ;
39+ const state = editor ?. state ;
40+ if ( ! view || ! state ) return ;
41+
42+ let tr = applySelectionCleanup ( editor , state . tr ) ;
43+ // Avoid creating a new undo step for this synthetic cleanup transaction.
44+ tr = tr . setMeta ( 'addToHistory' , false ) ;
45+ view . dispatch ( tr ) ;
46+ }
547
648/**
749 * Configuration options for History
@@ -52,10 +94,13 @@ export const History = Extension.create({
5294 undo : ( ) => ( { state, dispatch, tr } ) => {
5395 if ( this . editor . options . collaborationProvider && this . editor . options . ydoc ) {
5496 tr . setMeta ( 'preventDispatch' , true ) ;
55- return yUndo ( state ) ;
97+ const result = yUndo ( state ) ;
98+ runSelectionCleanupAfterCollabHistory ( this . editor ) ;
99+ return result ;
56100 }
57101 tr . setMeta ( 'inputType' , 'historyUndo' ) ;
58- return originalUndo ( state , dispatch ) ;
102+ const wrappedDispatch = createHistoryDispatch ( this . editor , dispatch ) ;
103+ return originalUndo ( state , wrappedDispatch ) ;
59104 } ,
60105
61106 /**
@@ -68,10 +113,13 @@ export const History = Extension.create({
68113 redo : ( ) => ( { state, dispatch, tr } ) => {
69114 if ( this . editor . options . collaborationProvider && this . editor . options . ydoc ) {
70115 tr . setMeta ( 'preventDispatch' , true ) ;
71- return yRedo ( state ) ;
116+ const result = yRedo ( state ) ;
117+ runSelectionCleanupAfterCollabHistory ( this . editor ) ;
118+ return result ;
72119 }
73120 tr . setMeta ( 'inputType' , 'historyRedo' ) ;
74- return originalRedo ( state , dispatch ) ;
121+ const wrappedDispatch = createHistoryDispatch ( this . editor , dispatch ) ;
122+ return originalRedo ( state , wrappedDispatch ) ;
75123 } ,
76124 } ;
77125 } ,
0 commit comments