forked from steemit/slate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.js
More file actions
439 lines (351 loc) · 7.32 KB
/
state.js
File metadata and controls
439 lines (351 loc) · 7.32 KB
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import Document from './document'
import Selection from './selection'
import Transform from './transform'
import { Record, Set, Stack, List } from 'immutable'
/**
* History.
*/
const History = new Record({
undos: new Stack(),
redos: new Stack()
})
/**
* Default properties.
*/
const DEFAULTS = {
document: new Document(),
selection: new Selection(),
history: new History(),
isNative: false
}
/**
* State.
*/
class State extends new Record(DEFAULTS) {
/**
* Create a new `State` with `properties`.
*
* @param {Object} properties
* @return {State} state
*/
static create(properties = {}) {
if (properties instanceof State) return properties
let document = Document.create(properties.document)
let selection = Selection.create(properties.selection)
if (selection.isUnset) {
const text = document.getFirstText()
selection = selection.collapseToStartOf(text)
}
const state = new State({ document, selection })
return state.transform({ normalized: false })
.normalize()
.apply({ save: false })
}
/**
* Get the kind.
*
* @return {String} kind
*/
get kind() {
return 'state'
}
/**
* Are there undoable events?
*
* @return {Boolean} hasUndos
*/
get hasUndos() {
return this.history.undos.size > 0
}
/**
* Are there redoable events?
*
* @return {Boolean} hasRedos
*/
get hasRedos() {
return this.history.redos.size > 0
}
/**
* Is the current selection blurred?
*
* @return {Boolean} isBlurred
*/
get isBlurred() {
return this.selection.isBlurred
}
/**
* Is the current selection focused?
*
* @return {Boolean} isFocused
*/
get isFocused() {
return this.selection.isFocused
}
/**
* Is the current selection collapsed?
*
* @return {Boolean} isCollapsed
*/
get isCollapsed() {
return this.selection.isCollapsed
}
/**
* Is the current selection expanded?
*
* @return {Boolean} isExpanded
*/
get isExpanded() {
return this.selection.isExpanded
}
/**
* Is the current selection backward?
*
* @return {Boolean} isBackward
*/
get isBackward() {
return this.selection.isBackward
}
/**
* Is the current selection forward?
*
* @return {Boolean} isForward
*/
get isForward() {
return this.selection.isForward
}
/**
* Get the current start key.
*
* @return {String} startKey
*/
get startKey() {
return this.selection.startKey
}
/**
* Get the current end key.
*
* @return {String} endKey
*/
get endKey() {
return this.selection.endKey
}
/**
* Get the current start offset.
*
* @return {String} startOffset
*/
get startOffset() {
return this.selection.startOffset
}
/**
* Get the current end offset.
*
* @return {String} endOffset
*/
get endOffset() {
return this.selection.endOffset
}
/**
* Get the current anchor key.
*
* @return {String} anchorKey
*/
get anchorKey() {
return this.selection.anchorKey
}
/**
* Get the current focus key.
*
* @return {String} focusKey
*/
get focusKey() {
return this.selection.focusKey
}
/**
* Get the current anchor offset.
*
* @return {String} anchorOffset
*/
get anchorOffset() {
return this.selection.anchorOffset
}
/**
* Get the current focus offset.
*
* @return {String} focusOffset
*/
get focusOffset() {
return this.selection.focusOffset
}
/**
* Get the current start text node's closest block parent.
*
* @return {Block} block
*/
get startBlock() {
return this.document.getClosestBlock(this.selection.startKey)
}
/**
* Get the current end text node's closest block parent.
*
* @return {Block} block
*/
get endBlock() {
return this.document.getClosestBlock(this.selection.endKey)
}
/**
* Get the current anchor text node's closest block parent.
*
* @return {Block} block
*/
get anchorBlock() {
return this.document.getClosestBlock(this.selection.anchorKey)
}
/**
* Get the current focus text node's closest block parent.
*
* @return {Block} block
*/
get focusBlock() {
return this.document.getClosestBlock(this.selection.focusKey)
}
/**
* Get the current start text node's closest inline parent.
*
* @return {Inline} inline
*/
get startInline() {
return this.document.getClosestInline(this.selection.startKey)
}
/**
* Get the current end text node's closest inline parent.
*
* @return {Inline} inline
*/
get endInline() {
return this.document.getClosestInline(this.selection.endKey)
}
/**
* Get the current anchor text node's closest inline parent.
*
* @return {Inline} inline
*/
get anchorInline() {
return this.document.getClosestInline(this.selection.anchorKey)
}
/**
* Get the current focus text node's closest inline parent.
*
* @return {Inline} inline
*/
get focusInline() {
return this.document.getClosestInline(this.selection.focusKey)
}
/**
* Get the current start text node.
*
* @return {Text} text
*/
get startText() {
return this.document.getDescendant(this.selection.startKey)
}
/**
* Get the current end node.
*
* @return {Text} text
*/
get endText() {
return this.document.getDescendant(this.selection.endKey)
}
/**
* Get the current anchor node.
*
* @return {Text} text
*/
get anchorText() {
return this.document.getDescendant(this.selection.anchorKey)
}
/**
* Get the current focus node.
*
* @return {Text} text
*/
get focusText() {
return this.document.getDescendant(this.selection.focusKey)
}
/**
* Get the characters in the current selection.
*
* @return {List} characters
*/
get characters() {
return this.document.getCharactersAtRange(this.selection)
}
/**
* Get the marks of the current selection.
*
* @return {Set} marks
*/
get marks() {
return this.selection.isUnset
? new Set()
: this.selection.marks || this.document.getMarksAtRange(this.selection)
}
/**
* Get the block nodes in the current selection.
*
* @return {List} nodes
*/
get blocks() {
return this.selection.isUnset
? new List()
: this.document.getBlocksAtRange(this.selection)
}
/**
* Get the fragment of the current selection.
*
* @return {List} nodes
*/
get fragment() {
return this.selection.isUnset
? Document.create()
: this.document.getFragmentAtRange(this.selection)
}
/**
* Get the inline nodes in the current selection.
*
* @return {List} nodes
*/
get inlines() {
return this.selection.isUnset
? new List()
: this.document.getInlinesAtRange(this.selection)
}
/**
* Get the text nodes in the current selection.
*
* @return {List} nodes
*/
get texts() {
return this.selection.isUnset
? new List()
: this.document.getTextsAtRange(this.selection)
}
/**
* Return a new `Transform` with the current state as a starting point.
*
* @param {Object} properties
* @return {Transform} transform
*/
transform(properties = {}) {
const state = this
return new Transform({
...properties,
state
})
}
}
/**
* Export.
*/
export default State