forked from ianstormtaylor/slate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform.js
409 lines (340 loc) · 8.83 KB
/
transform.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import includes from 'lodash/includes'
import xor from 'lodash/xor'
import { List, Record } from 'immutable'
/**
* Snapshot, with a state-like shape.
*/
const Snapshot = new Record({
document: null,
selection: null,
steps: new List()
})
/**
* Step.
*/
const Step = new Record({
type: null,
args: null
})
/**
* Document range transforms.
*/
const DOCUMENT_RANGE_TRANSFORMS = [
'deleteAtRange',
'deleteBackwardAtRange',
'deleteForwardAtRange',
'insertBlockAtRange',
'insertFragmentAtRange',
'insertInlineAtRange',
'insertTextAtRange',
'addMarkAtRange',
'setBlockAtRange',
'setInlineAtRange',
'splitBlockAtRange',
'splitInlineAtRange',
'removeMarkAtRange',
'toggleMarkAtRange',
'unwrapBlockAtRange',
'unwrapInlineAtRange',
'wrapBlockAtRange',
'wrapInlineAtRange',
'wrapTextAtRange'
]
/**
* Document node transforms.
*/
const DOCUMENT_NODE_TRANSFORMS = [
'removeNodeByKey',
'setNodeByKey',
]
/**
* Selection transforms.
*/
const SELECTION_TRANSFORMS = [
'blur',
'collapseToAnchor',
'collapseToEnd',
'collapseToEndOf',
'collapseToFocus',
'collapseToStart',
'collapseToStartOf',
'extendBackward',
'extendForward',
'extendToEndOf',
'extendToStartOf',
'focus',
'moveBackward',
'moveForward',
'moveToOffsets',
'moveToRangeOf'
]
/**
* State-level document transforms.
*/
const STATE_DOCUMENT_TRANSFORMS = [
'delete',
'deleteBackward',
'deleteForward',
'insertBlock',
'insertFragment',
'insertInline',
'insertText',
'addMark',
'setBlock',
'setInline',
'splitBlock',
'splitInline',
'removeMark',
'toggleMark',
'unwrapBlock',
'unwrapInline',
'wrapBlock',
'wrapInline',
'wrapText'
]
/**
* State selection transforms.
*/
const STATE_SELECTION_TRANSFORMS = [
'collapseToEndOfNextBlock',
'collapseToEndOfNextText',
'collapseToEndOfPreviousBlock',
'collapseToEndOfPreviousText',
'collapseToStartOfNextBlock',
'collapseToStartOfNextText',
'collapseToStartOfPreviousBlock',
'collapseToStartOfPreviousText',
'moveTo',
]
/**
* All state-level transforms.
*/
const STATE_TRANSFORMS = []
.concat(STATE_DOCUMENT_TRANSFORMS)
.concat(STATE_SELECTION_TRANSFORMS)
/**
* All transforms.
*/
const TRANSFORMS = []
.concat(DOCUMENT_RANGE_TRANSFORMS)
.concat(DOCUMENT_NODE_TRANSFORMS)
.concat(SELECTION_TRANSFORMS)
.concat(STATE_TRANSFORMS)
/**
* Defaults.
*/
const DEFAULT_PROPERTIES = {
state: null,
steps: new List()
}
/**
* Transform.
*/
class Transform extends new Record(DEFAULT_PROPERTIES) {
/**
* Get the kind.
*
* @return {String} kind
*/
get kind() {
return 'transform'
}
/**
* Apply the transform and return the new state.
*
* @param {Object} options
* @property {Boolean} isNative
* @property {Boolean} snapshot
* @return {State} state
*/
apply(options = {}) {
const transform = this
let { state, steps } = transform
let { cursorMarks, history, selection } = state
let { undos, redos } = history
// Determine whether we need to create a new snapshot.
const shouldSnapshot = options.snapshot == null
? this.shouldSnapshot()
: options.snapshot
// If we should, save a snapshot into the history before transforming.
if (shouldSnapshot) {
const snapshot = transform.snapshot()
undos = undos.push(snapshot)
if (undos.size > 100) undos = undos.take(100)
redos = redos.clear()
history = history.merge({ undos, redos })
state = state.merge({ history })
}
// Apply each of the steps in the transform, arriving at a new state.
state = steps.reduce((memo, step) => this.applyStep(memo, step), state)
// If there are cursor marks and they haven't changed, remove them.
if (state.cursorMarks && state.cursorMarks == cursorMarks) {
state = state.merge({
cursorMarks: null
})
}
// Apply the "isNative" flag, which is used to allow for natively-handled
// content changes to skip rerendering the editor for performance.
state = state.merge({
isNative: !!options.isNative
})
return state
}
/**
* Apply a single `step` to a `state`, differentiating between types.
*
* @param {State} state
* @param {Step} step
* @return {State} state
*/
applyStep(state, step) {
const { type, args } = step
if (includes(DOCUMENT_RANGE_TRANSFORMS, type)) {
let { document, selection } = state
let [ range, ...rest ] = args
range = range.normalize(document)
document = document[type](range, ...rest)
selection = selection.normalize(document)
state = state.merge({ document, selection })
return state
}
else if (includes(DOCUMENT_NODE_TRANSFORMS, type)) {
let { document, selection } = state
document = document[type](...args)
selection = selection.normalize(document)
state = state.merge({ document, selection })
return state
}
else if (includes(SELECTION_TRANSFORMS, type)) {
let { document, selection } = state
selection = selection[type](...args)
selection = selection.normalize(document)
state = state.merge({ selection })
return state
}
else if (includes(STATE_TRANSFORMS, type)) {
state = state[type](...args)
return state
}
}
/**
* Check whether the current transform steps should create a snapshot.
*
* @return {Boolean}
*/
shouldSnapshot() {
const transform = this
const { state, steps } = transform
const { cursorMarks, history, selection } = state
const { undos, redos } = history
const previous = undos.peek()
// If the only steps applied are selection transforms, don't snapshot.
const onlySelections = steps.every((step) => {
return (
includes(SELECTION_TRANSFORMS, step.type) ||
includes(STATE_SELECTION_TRANSFORMS, step.type)
)
})
if (onlySelections) return false
// If there isn't a previous state, snapshot.
if (!previous) return true
// If there is a previous state but the steps are different, snapshot.
const types = steps.map(step => step.type)
const prevTypes = previous.steps.map(step => step.type)
const diff = xor(types.toArray(), prevTypes.toArray())
if (diff.length) return true
// If the current steps aren't one of the "combinable" types, snapshot.
const allCombinable = (
steps.every(step => step.type == 'insertText') ||
steps.every(step => step.type == 'deleteForward') ||
steps.every(step => step.type == 'deleteBackward')
)
if (!allCombinable) return true
// Otherwise, don't snapshot.
return false
}
/**
* Create a history-ready snapshot of the current state.
*
* @return {Snapshot} snapshot
*/
snapshot() {
let { state, steps } = this
let { document, selection } = state
return new Snapshot({ document, selection, steps })
}
/**
* Undo to the previous state in the history.
*
* @return {State} state
*/
undo() {
const transform = this
let { state } = transform
let { history } = state
let { undos, redos } = history
// If there's no previous snapshot, return the current state.
let previous = undos.peek()
if (!previous) return state
// Remove the previous snapshot from the undo stack.
undos = undos.pop()
// Snapshot the current state, and move it into the redos stack.
let snapshot = transform.snapshot()
redos = redos.push(snapshot)
// Return the previous state, with the updated history.
let { document, selection } = previous
history = history.merge({ undos, redos })
state = state.merge({
document,
selection,
history,
isNative: false
})
return state
}
/**
* Redo to the next state in the history.
*
* @return {State} state
*/
redo() {
const transform = this
let { state } = transform
let { history } = state
let { undos, redos } = history
// If there's no next snapshot, return the current state.
let next = redos.peek()
if (!next) return state
// Remove the next history from the redo stack.
redos = redos.pop()
// Snapshot the current state, and move it into the undos stack.
let snapshot = transform.snapshot()
undos = undos.push(snapshot)
// Return the next state, with the updated history.
let { document, selection } = next
history = history.merge({ undos, redos })
state = state.merge({
document,
selection,
history,
isNative: false
})
return state
}
}
/**
* Add a step-creating method for each of the transforms.
*/
TRANSFORMS.forEach((type) => {
Transform.prototype[type] = function (...args) {
let transform = this
let { steps } = transform
steps = steps.push(new Step({ type, args }))
transform = transform.merge({ steps })
return transform
}
})
/**
* Export.
*/
export default Transform