1
- import { Editor , Operation , Path } from 'slate'
1
+ import { Editor , Operation , Path , Range , Transforms } from 'slate'
2
2
3
3
import { HistoryEditor } from './history-editor'
4
4
@@ -24,9 +24,13 @@ export const withHistory = <T extends Editor>(editor: T) => {
24
24
if ( redos . length > 0 ) {
25
25
const batch = redos [ redos . length - 1 ]
26
26
27
+ if ( batch . selectionBefore ) {
28
+ Transforms . setSelection ( e , batch . selectionBefore )
29
+ }
30
+
27
31
HistoryEditor . withoutSaving ( e , ( ) => {
28
32
Editor . withoutNormalizing ( e , ( ) => {
29
- for ( const op of batch ) {
33
+ for ( const op of batch . operations ) {
30
34
e . apply ( op )
31
35
}
32
36
} )
@@ -46,11 +50,14 @@ export const withHistory = <T extends Editor>(editor: T) => {
46
50
47
51
HistoryEditor . withoutSaving ( e , ( ) => {
48
52
Editor . withoutNormalizing ( e , ( ) => {
49
- const inverseOps = batch . map ( Operation . inverse ) . reverse ( )
53
+ const inverseOps = batch . operations . map ( Operation . inverse ) . reverse ( )
50
54
51
55
for ( const op of inverseOps ) {
52
56
e . apply ( op )
53
57
}
58
+ if ( batch . selectionBefore ) {
59
+ Transforms . setSelection ( e , batch . selectionBefore )
60
+ }
54
61
} )
55
62
} )
56
63
@@ -63,8 +70,8 @@ export const withHistory = <T extends Editor>(editor: T) => {
63
70
const { operations, history } = e
64
71
const { undos } = history
65
72
const lastBatch = undos [ undos . length - 1 ]
66
- const lastOp = lastBatch && lastBatch [ lastBatch . length - 1 ]
67
- const overwrite = shouldOverwrite ( op , lastOp )
73
+ const lastOp =
74
+ lastBatch && lastBatch . operations [ lastBatch . operations . length - 1 ]
68
75
let save = HistoryEditor . isSaving ( e )
69
76
let merge = HistoryEditor . isMerging ( e )
70
77
@@ -79,28 +86,25 @@ export const withHistory = <T extends Editor>(editor: T) => {
79
86
} else if ( operations . length !== 0 ) {
80
87
merge = true
81
88
} else {
82
- merge = shouldMerge ( op , lastOp ) || overwrite
89
+ merge = shouldMerge ( op , lastOp )
83
90
}
84
91
}
85
92
86
93
if ( lastBatch && merge ) {
87
- if ( overwrite ) {
88
- lastBatch . pop ( )
89
- }
90
-
91
- lastBatch . push ( op )
94
+ lastBatch . operations . push ( op )
92
95
} else {
93
- const batch = [ op ]
96
+ const batch = {
97
+ operations : [ op ] ,
98
+ selectionBefore : e . selection ,
99
+ }
94
100
undos . push ( batch )
95
101
}
96
102
97
103
while ( undos . length > 100 ) {
98
104
undos . shift ( )
99
105
}
100
106
101
- if ( shouldClear ( op ) ) {
102
- history . redos = [ ]
103
- }
107
+ history . redos = [ ]
104
108
}
105
109
106
110
apply ( op )
@@ -114,10 +118,6 @@ export const withHistory = <T extends Editor>(editor: T) => {
114
118
*/
115
119
116
120
const shouldMerge = ( op : Operation , prev : Operation | undefined ) : boolean => {
117
- if ( op . type === 'set_selection' ) {
118
- return true
119
- }
120
-
121
121
if (
122
122
prev &&
123
123
op . type === 'insert_text' &&
@@ -146,36 +146,6 @@ const shouldMerge = (op: Operation, prev: Operation | undefined): boolean => {
146
146
*/
147
147
148
148
const shouldSave = ( op : Operation , prev : Operation | undefined ) : boolean => {
149
- if (
150
- op . type === 'set_selection' &&
151
- ( op . properties == null || op . newProperties == null )
152
- ) {
153
- return false
154
- }
155
-
156
- return true
157
- }
158
-
159
- /**
160
- * Check whether an operation should overwrite the previous one.
161
- */
162
-
163
- const shouldOverwrite = (
164
- op : Operation ,
165
- prev : Operation | undefined
166
- ) : boolean => {
167
- if ( prev && op . type === 'set_selection' && prev . type === 'set_selection' ) {
168
- return true
169
- }
170
-
171
- return false
172
- }
173
-
174
- /**
175
- * Check whether an operation should clear the redos stack.
176
- */
177
-
178
- const shouldClear = ( op : Operation ) : boolean => {
179
149
if ( op . type === 'set_selection' ) {
180
150
return false
181
151
}
0 commit comments