forked from steemit/slate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection.js
More file actions
710 lines (601 loc) · 14.3 KB
/
selection.js
File metadata and controls
710 lines (601 loc) · 14.3 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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
import getLeafText from '../utils/get-leaf-text'
import warning from '../utils/warning'
import { Record } from 'immutable'
/**
* Start-end-and-edge convenience methods to auto-generate.
*/
const EDGE_METHODS = [
'has%AtStartOf',
'has%AtEndOf',
'has%Between',
'has%In',
]
/**
* Default properties.
*/
const DEFAULTS = {
anchorKey: null,
anchorOffset: 0,
focusKey: null,
focusOffset: 0,
isBackward: null,
isFocused: false,
marks: null,
}
/**
* Selection.
*/
class Selection extends new Record(DEFAULTS) {
/**
* Create a new `Selection` with `properties`.
*
* @param {Object} properties
* @return {Selection} selection
*/
static create(properties = {}) {
if (properties instanceof Selection) return properties
return new Selection(properties)
}
/**
* Get the kind.
*
* @return {String} kind
*/
get kind() {
return 'selection'
}
/**
* Get whether the selection is blurred.
*
* @return {Boolean} isBlurred
*/
get isBlurred() {
return !this.isFocused
}
/**
* Get whether the selection is collapsed.
*
* @return {Boolean} isCollapsed
*/
get isCollapsed() {
return (
this.anchorKey == this.focusKey &&
this.anchorOffset == this.focusOffset
)
}
/**
* Get whether the selection is expanded.
*
* @return {Boolean} isExpanded
*/
get isExpanded() {
return !this.isCollapsed
}
/**
* Get whether the selection is forward.
*
* @return {Boolean} isForward
*/
get isForward() {
return this.isBackward == null ? null : !this.isBackward
}
/**
* Check whether the selection's keys are not set.
*
* @return {Boolean}
*/
get isUnset() {
return this.anchorKey == null || this.focusKey == null
}
/**
* Get the start key.
*
* @return {String} startKey
*/
get startKey() {
return this.isBackward
? this.focusKey
: this.anchorKey
}
get startOffset() {
return this.isBackward
? this.focusOffset
: this.anchorOffset
}
get endKey() {
return this.isBackward
? this.anchorKey
: this.focusKey
}
get endOffset() {
return this.isBackward
? this.anchorOffset
: this.focusOffset
}
/**
* Check whether anchor point of the selection is at the start of a `node`.
*
* @param {Node} node
* @return {Boolean}
*/
hasAnchorAtStartOf(node) {
if (this.anchorOffset != 0) return false
const first = node.kind == 'text' ? node : node.getFirstText()
return this.anchorKey == first.key
}
/**
* Check whether anchor point of the selection is at the end of a `node`.
*
* @param {Node} node
* @return {Boolean}
*/
hasAnchorAtEndOf(node) {
const last = node.kind == 'text' ? node : node.getLastText()
return this.anchorKey == last.key && this.anchorOffset == last.length
}
/**
* Check whether the anchor edge of a selection is in a `node` and at an
* offset between `start` and `end`.
*
* @param {Node} node
* @param {Number} start
* @param {Number} end
* @return {Boolean}
*/
hasAnchorBetween(node, start, end) {
return (
this.anchorOffset <= end &&
start <= this.anchorOffset &&
this.hasAnchorIn(node)
)
}
/**
* Check whether the anchor edge of a selection is in a `node`.
*
* @param {Node} node
* @return {Boolean}
*/
hasAnchorIn(node) {
if (node.kind == 'text') {
return node.key === this.anchorKey
} else {
return node.hasDescendant(this.anchorKey)
}
}
/**
* Check whether focus point of the selection is at the end of a `node`.
*
* @param {Node} node
* @return {Boolean}
*/
hasFocusAtEndOf(node) {
const last = node.kind == 'text' ? node : node.getLastText()
return this.focusKey == last.key && this.focusOffset == last.length
}
/**
* Check whether focus point of the selection is at the start of a `node`.
*
* @param {Node} node
* @return {Boolean}
*/
hasFocusAtStartOf(node) {
if (this.focusOffset != 0) return false
const first = node.kind == 'text' ? node : node.getFirstText()
return this.focusKey == first.key
}
/**
* Check whether the focus edge of a selection is in a `node` and at an
* offset between `start` and `end`.
*
* @param {Node} node
* @param {Number} start
* @param {Number} end
* @return {Boolean}
*/
hasFocusBetween(node, start, end) {
return (
start <= this.focusOffset &&
this.focusOffset <= end &&
this.hasFocusIn(node)
)
}
/**
* Check whether the focus edge of a selection is in a `node`.
*
* @param {Node} node
* @return {Boolean}
*/
hasFocusIn(node) {
if (node.kind == 'text') {
return node.key === this.focusKey
} else {
return node.hasDescendant(this.focusKey)
}
}
/**
* Check whether the selection is at the start of a `node`.
*
* @param {Node} node
* @return {Boolean} isAtStart
*/
isAtStartOf(node) {
const { isExpanded, startKey, startOffset } = this
if (isExpanded) return false
if (startOffset != 0) return false
const first = node.kind == 'text' ? node : node.getFirstText()
return startKey == first.key
}
/**
* Check whether the selection is at the end of a `node`.
*
* @param {Node} node
* @return {Boolean} isAtEnd
*/
isAtEndOf(node) {
const { endKey, endOffset, isExpanded } = this
if (isExpanded) return false
const last = node.kind == 'text' ? node : node.getLastText()
return endKey == last.key && endOffset == last.length
}
/**
* Normalize the selection, relative to a `node`, ensuring that the anchor
* and focus nodes of the selection always refer to leaf text nodes.
*
* @param {Node} node
* @return {Selection} selection
*/
normalize(node) {
let selection = this
let { anchorKey, anchorOffset, focusKey, focusOffset, isBackward } = selection
// If the selection isn't formed yet or is malformed, ensure that it is
// properly zeroed out.
if (
anchorKey == null ||
focusKey == null ||
!node.hasDescendant(anchorKey) ||
!node.hasDescendant(focusKey)
) {
return selection.merge({
anchorKey: null,
anchorOffset: 0,
focusKey: null,
focusOffset: 0,
isBackward: false
})
}
// Get the anchor and focus nodes.
let anchorNode = node.getDescendant(anchorKey)
let focusNode = node.getDescendant(focusKey)
// If the anchor node isn't a text node, match it to one.
if (anchorNode.kind != 'text') {
warning('Selection anchor is on a non text node, matching to leaf')
let anchorText = anchorNode.getTextAtOffset(anchorOffset)
let offset = anchorNode.getOffset(anchorText)
anchorOffset = anchorOffset - offset
anchorNode = anchorText
}
// If the focus node isn't a text node, match it to one.
if (focusNode.kind != 'text') {
warning('Selection focus is on a non text node, matching to leaf')
let focusText = focusNode.getTextAtOffset(focusOffset)
let offset = focusNode.getOffset(focusText)
focusOffset = focusOffset - offset
focusNode = focusText
}
// If `isBackward` is not set, derive it.
if (isBackward == null) {
if (anchorNode.key === focusNode.key) {
isBackward = anchorOffset > focusOffset
} else {
isBackward = !node.areDescendantSorted(anchorNode.key, focusNode.key)
}
}
// Merge in any updated properties.
return selection.merge({
anchorKey: anchorNode.key,
anchorOffset,
focusKey: focusNode.key,
focusOffset,
isBackward
})
}
/**
* Focus the selection.
*
* @return {Selection} selection
*/
focus() {
return this.merge({
isFocused: true
})
}
/**
* Blur the selection.
*
* @return {Selection} selection
*/
blur() {
return this.merge({
isFocused: false
})
}
/**
* Move the focus point to the anchor point.
*
* @return {Selection} selection
*/
collapseToAnchor() {
return this.merge({
focusKey: this.anchorKey,
focusOffset: this.anchorOffset,
isBackward: false
})
}
/**
* Move the anchor point to the focus point.
*
* @return {Selection} selection
*/
collapseToFocus() {
return this.merge({
anchorKey: this.focusKey,
anchorOffset: this.focusOffset,
isBackward: false
})
}
/**
* Move the end point to the start point.
*
* @return {Selection} selection
*/
collapseToStart() {
return this.merge({
anchorKey: this.startKey,
anchorOffset: this.startOffset,
focusKey: this.startKey,
focusOffset: this.startOffset,
isBackward: false
})
}
/**
* Move the end point to the start point.
*
* @return {Selection} selection
*/
collapseToEnd() {
return this.merge({
anchorKey: this.endKey,
anchorOffset: this.endOffset,
focusKey: this.endKey,
focusOffset: this.endOffset,
isBackward: false
})
}
/**
* Move to the start of a `node`.
*
* @param {Node} node
* @return {Selection} selection
*/
collapseToStartOf(node) {
node = getLeafText(node)
return this.merge({
anchorKey: node.key,
anchorOffset: 0,
focusKey: node.key,
focusOffset: 0,
isBackward: false
})
}
/**
* Move to the end of a `node`.
*
* @return {Selection} selection
*/
collapseToEndOf(node) {
node = getLeafText(node)
return this.merge({
anchorKey: node.key,
anchorOffset: node.length,
focusKey: node.key,
focusOffset: node.length,
isBackward: false
})
}
/**
* Move to the entire range of `start` and `end` nodes.
*
* @param {Node} start
* @param {Node} end (optional)
* @param {Document} document
* @return {Selection} selection
*/
moveToRangeOf(start, end = start) {
start = getLeafText(start)
end = getLeafText(end)
return this.merge({
anchorKey: start.key,
anchorOffset: 0,
focusKey: end.key,
focusOffset: end.length,
isBackward: null,
})
}
/**
* Move the selection forward `n` characters.
*
* @param {Number} n (optional)
* @return {Selection} selection
*/
moveForward(n = 1) {
return this.merge({
anchorOffset: this.anchorOffset + n,
focusOffset: this.focusOffset + n
})
}
/**
* Move the selection backward `n` characters.
*
* @param {Number} n (optional)
* @return {Selection} selection
*/
moveBackward(n = 1) {
return this.merge({
anchorOffset: this.anchorOffset - n,
focusOffset: this.focusOffset - n
})
}
/**
* Move the selection to `anchor` and `focus` offsets.
*
* @param {Number} anchor
* @param {Number} focus (optional)
* @return {Selection} selection
*/
moveToOffsets(anchor, focus = anchor) {
const props = {}
props.anchorOffset = anchor
props.focusOffset = focus
if (this.anchorKey == this.focusKey) {
props.isBackward = anchor > focus
}
return this.merge(props)
}
/**
* Extend the focus point forward `n` characters.
*
* @param {Number} n (optional)
* @return {Selection} selection
*/
extendForward(n = 1) {
return this.merge({
focusOffset: this.focusOffset + n,
isBackward: null
})
}
/**
* Extend the focus point backward `n` characters.
*
* @param {Number} n (optional)
* @return {Selection} selection
*/
extendBackward(n = 1) {
return this.merge({
focusOffset: this.focusOffset - n,
isBackward: null
})
}
/**
* Extend the start point forward `n` characters.
*
* @param {Number} n (optional)
* @return {Selection} selection
*/
moveStartOffset(n = 1) {
return this.isBackward
? this.merge({ focusOffset: this.focusOffset + n })
: this.merge({ anchorOffset: this.anchorOffset + n })
}
/**
* Extend the end point forward `n` characters.
*
* @param {Number} n (optional)
* @return {Selection} selection
*/
moveEndOffset(n = 1) {
return this.isBackward
? this.merge({ anchorOffset: this.anchorOffset + n })
: this.merge({ focusOffset: this.focusOffset + n })
}
/**
* Move the start key, while preserving the direction
*
* @param {String} key
* @return {Selection} selection
*/
moveStartTo(key, offset = 0) {
return this.isBackward
? this.merge({ focusKey: key, focusOffset: offset })
: this.merge({ anchorKey: key, anchorOffset: offset })
}
/**
* Move the end key, while preserving the direction
*
* @param {String} key
* @return {Selection} selection
*/
moveEndTo(key, offset = 0) {
return this.isBackward
? this.merge({ anchorKey: key, anchorOffset: offset })
: this.merge({ focusKey: key, focusOffset: offset })
}
/**
* Extend the focus point to the start of a `node`.
*
* @param {Node} node
* @return {Selection} selection
*/
extendToStartOf(node) {
return this.merge({
focusKey: node.key,
focusOffset: 0,
isBackward: null
})
}
/**
* Extend the focus point to the end of a `node`.
*
* @param {Node} node
* @return {Selection} selection
*/
extendToEndOf(node) {
return this.merge({
focusKey: node.key,
focusOffset: node.length,
isBackward: null
})
}
/**
* Unset the selection
*
* @return {Selection} selection
*/
unset() {
return this.merge({
anchorKey: null,
anchorOffset: 0,
focusKey: null,
focusOffset: 0,
isFocused: false,
isBackward: false
})
}
}
/**
* Add start, end and edge convenience methods.
*/
EDGE_METHODS.forEach((pattern) => {
const [ p, s ] = pattern.split('%')
const anchor = `${p}Anchor${s}`
const edge = `${p}Edge${s}`
const end = `${p}End${s}`
const focus = `${p}Focus${s}`
const start = `${p}Start${s}`
Selection.prototype[start] = function (...args) {
return this.isBackward
? this[focus](...args)
: this[anchor](...args)
}
Selection.prototype[end] = function (...args) {
return this.isBackward
? this[anchor](...args)
: this[focus](...args)
}
Selection.prototype[edge] = function (...args) {
return this[anchor](...args) || this[focus](...args)
}
})
/**
* Export.
*/
export default Selection