@@ -6,24 +6,26 @@ const Undo = Module({
66 props : {
77 contentEditableElem : null ,
88 currentHistoryIndex : - 1 ,
9- history : [ ]
9+ history : [ ] ,
10+ ignoreSelectionChanges : false
1011 } ,
1112
1213 handlers : {
1314 events : {
14- 'contenteditable:mutation:observed' : 'handleMutation' ,
15- 'contenteditable:focus' : 'handleFocus'
15+ // 'contenteditable:mutation:observed': 'handleMutation',
16+ 'contenteditable:focus' : 'handleFocus' ,
17+ 'import:from:canvas:start' : 'handleImportStart' ,
18+ 'import:from:canvas:complete' : 'handleImportComplete' ,
19+ 'selection:change' : 'handleSelectionChange' ,
20+ 'export:to:canvas:start' : 'handleExportStart'
1621 }
1722 } ,
1823
1924 methods : {
2025 setup ( ) { } ,
21- init ( ) {
22- console . log ( 'Undo init' ) ;
23- } ,
26+ init ( ) { } ,
2427
2528 handleMutation ( ) {
26- console . log ( 'Undo handleMutation' ) ;
2729 const { props, mediator } = this ;
2830 const { history, currentHistoryIndex } = props ;
2931 const states = {
@@ -41,29 +43,20 @@ const Undo = Module({
4143 noChange
4244 } = this . analyzeStates ( states ) ;
4345
44- console . log (
45- JSON . parse (
46- JSON . stringify ( {
47- states,
48- currentHistoryIndex,
49- history,
50- isUndo,
51- isRedo,
52- noChange
53- } )
54- )
55- ) ;
56-
5746 if ( noChange ) {
58- props . history [ currentHistoryIndex ] = states . current ;
47+ return ;
5948 } else if ( ! isUndo && ! isRedo ) {
6049 props . history . length = currentHistoryIndex + 1 ;
6150 props . history . push ( states . current ) ;
6251 props . currentHistoryIndex += 1 ;
6352 } else if ( isUndo ) {
6453 props . currentHistoryIndex -= 1 ;
6554 mediator . exec ( 'format:clean' , props . contentEditableElem ) ;
66- mediator . exec ( 'selection:select:coordinates' , states . previous . selectionRangeCoordinates ) ;
55+ mediator . exec ( 'selection:select:coordinates' , states . beforePrevious . selectionRangeCoordinates ) ;
56+ } else if ( isRedo ) {
57+ props . currentHistoryIndex += 1 ;
58+ mediator . exec ( 'format:clean' , props . contentEditableElem ) ;
59+ mediator . exec ( 'selection:select:coordinates' , states . next . selectionRangeCoordinates ) ;
6760 }
6861 } ,
6962
@@ -80,24 +73,63 @@ const Undo = Module({
8073 }
8174 } ,
8275
76+ handleImportStart ( ) {
77+ const { props } = this ;
78+ props . ignoreSelectionChanges = true ;
79+ } ,
80+
81+ handleImportComplete ( ) {
82+ const { props } = this ;
83+ props . ignoreSelectionChanges = false ;
84+ } ,
85+
86+ handleExportStart ( ) {
87+ this . updateCurrentHistoryState ( ) ;
88+ } ,
89+
90+ handleSelectionChange ( ) {
91+ const { props } = this ;
92+ if ( ! props . ignoreSelectionChanges ) {
93+ this . updateCurrentHistoryState ( ) ;
94+ }
95+ } ,
96+
97+ updateCurrentHistoryState ( ) {
98+ const { props } = this ;
99+ const { history, currentHistoryIndex } = props ;
100+ const currentHistoryState = history [ currentHistoryIndex ] ;
101+
102+ if ( currentHistoryState ) {
103+ this . cacheSelectionRangeOnState ( currentHistoryState ) ;
104+ }
105+ } ,
106+
83107 createHistoryState ( ) {
84- const { mediator, props } = this ;
85- const editableContentString = DOM . nodesToHTMLString ( DOM . cloneNodes ( props . contentEditableElem , { trim : true } ) ) . replace ( / \u200B / g, '' ) . trim ( ) ;
86- const selectionRangeCoordinates = mediator . get ( 'selection:range:coordinates' ) ;
108+ const { props } = this ;
87109
88- return {
110+ if ( ! props . contentEditableElem ) { return ; }
111+
112+ const editableContentString = DOM . nodesToHTMLString ( DOM . cloneNodes ( props . contentEditableElem , { trim : true } ) ) . replace ( / \u200B / g, '' ) ;
113+ const historyState = {
89114 editableContentString,
90- selectionRangeCoordinates
91115 } ;
116+
117+ this . cacheSelectionRangeOnState ( historyState ) ;
118+
119+ return historyState ;
120+ } ,
121+
122+ cacheSelectionRangeOnState ( state ) {
123+ const { mediator } = this ;
124+ state . selectionRangeCoordinates = mediator . get ( 'selection:range:coordinates' ) ;
92125 } ,
93126
94127 analyzeStates ( states ) {
95128 const {
96129 current,
97130 previous,
98131 beforePrevious,
99- next,
100- afterNext
132+ next
101133 } = states ;
102134 let isUndo = beforePrevious && current . editableContentString === beforePrevious . editableContentString ;
103135 let isRedo = next && current . editableContentString === next . editableContentString ;
@@ -107,17 +139,13 @@ const Undo = Module({
107139 isRedo = isRedo || false ;
108140 noChange = noChange || false ;
109141
110- if ( ! isUndo && ! isRedo && ! noChange && previous ) {
111- console . log ( previous . editableContentString ) ;
112- console . log ( current . editableContentString ) ;
113- }
114142 return {
115143 isUndo,
116144 isRedo,
117145 noChange
118- }
146+ } ;
119147 }
120148 }
121- } )
149+ } ) ;
122150
123151export default Undo ;
0 commit comments