-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
after.js
711 lines (578 loc) · 16.7 KB
/
after.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
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
711
import Base64 from 'slate-base64-serializer'
import Debug from 'debug'
import Hotkeys from 'slate-hotkeys'
import Plain from 'slate-plain-serializer'
import getWindow from 'get-window'
import { IS_IOS, IS_IE, IS_EDGE } from 'slate-dev-environment'
import cloneFragment from '../../utils/clone-fragment'
import getEventTransfer from '../../utils/get-event-transfer'
import setEventTransfer from '../../utils/set-event-transfer'
/**
* Debug.
*
* @type {Function}
*/
const debug = Debug('slate:after')
/**
* A plugin that adds the "after" browser-specific logic to the editor.
*
* @param {Object} options
* @return {Object}
*/
function AfterPlugin(options = {}) {
let isDraggingInternally = null
let isMouseDown = false
/**
* On before input.
*
* @param {Event} event
* @param {Editor} editor
* @param {Function} next
*/
function onBeforeInput(event, editor, next) {
const { value } = editor
const isSynthetic = !!event.nativeEvent
// If the event is synthetic, it's React's polyfill of `beforeinput` that
// isn't a true `beforeinput` event with meaningful information. It only
// gets triggered for character insertions, so we can just insert directly.
if (isSynthetic) {
event.preventDefault()
editor.insertText(event.data)
return next()
}
// Otherwise, we can use the information in the `beforeinput` event to
// figure out the exact change that will occur, and prevent it.
const [targetRange] = event.getTargetRanges()
if (!targetRange) return next()
debug('onBeforeInput', { event })
event.preventDefault()
const { document, selection } = value
const range = editor.findRange(targetRange)
switch (event.inputType) {
case 'deleteByDrag':
case 'deleteByCut':
case 'deleteContent':
case 'deleteContentBackward':
case 'deleteContentForward': {
editor.deleteAtRange(range)
break
}
case 'deleteWordBackward': {
editor.deleteWordBackwardAtRange(range)
break
}
case 'deleteWordForward': {
editor.deleteWordForwardAtRange(range)
break
}
case 'deleteSoftLineBackward':
case 'deleteHardLineBackward': {
editor.deleteLineBackwardAtRange(range)
break
}
case 'deleteSoftLineForward':
case 'deleteHardLineForward': {
editor.deleteLineForwardAtRange(range)
break
}
case 'insertLineBreak':
case 'insertParagraph': {
const hasVoidParent = document.hasVoidParent(
selection.start.path,
editor
)
if (hasVoidParent) {
editor.moveToStartOfNextText()
} else {
editor.splitBlockAtRange(range)
}
break
}
case 'insertFromYank':
case 'insertReplacementText':
case 'insertText': {
// COMPAT: `data` should have the text for the `insertText` input type
// and `dataTransfer` should have the text for the
// `insertReplacementText` input type, but Safari uses `insertText` for
// spell check replacements and sets `data` to `null`. (2018/08/09)
const text =
event.data == null
? event.dataTransfer.getData('text/plain')
: event.data
if (text == null) break
editor.insertTextAtRange(range, text, selection.marks)
// If the text was successfully inserted, and the selection had marks
// on it, unset the selection's marks.
if (selection.marks && value.document !== editor.value.document) {
editor.select({ marks: null })
}
break
}
}
next()
}
/**
* On blur.
*
* @param {Event} event
* @param {Editor} editor
* @param {Function} next
*/
function onBlur(event, editor, next) {
debug('onBlur', { event })
editor.blur()
next()
}
/**
* On click.
*
* @param {Event} event
* @param {Editor} editor
* @param {Function} next
*/
function onClick(event, editor, next) {
if (editor.readOnly) return next()
const { value } = editor
const { document } = value
const path = editor.findPath(event.target)
if (!path) return next()
debug('onClick', { event })
const node = document.getNode(path)
const ancestors = document.getAncestors(path)
const isVoid =
node && (editor.isVoid(node) || ancestors.some(a => editor.isVoid(a)))
if (isVoid) {
// COMPAT: In Chrome & Safari, selections that are at the zero offset of
// an inline node will be automatically replaced to be at the last offset
// of a previous inline node, which screws us up, so we always want to set
// it to the end of the node. (2016/11/29)
editor.focus().moveToEndOfNode(node)
}
next()
}
/**
* On copy.
*
* @param {Event} event
* @param {Editor} editor
* @param {Function} next
*/
function onCopy(event, editor, next) {
debug('onCopy', { event })
cloneFragment(event, editor)
next()
}
/**
* On cut.
*
* @param {Event} event
* @param {Editor} editor
* @param {Function} next
*/
function onCut(event, editor, next) {
debug('onCut', { event })
// Once the fake cut content has successfully been added to the clipboard,
// delete the content in the current selection.
cloneFragment(event, editor, () => {
// If user cuts a void block node or a void inline node,
// manually removes it since selection is collapsed in this case.
const { value } = editor
const { document, selection } = value
const { end, isCollapsed } = selection
let voidPath
if (isCollapsed) {
for (const [node, path] of document.ancestors(end.path)) {
if (editor.isVoid(node)) {
voidPath = path
break
}
}
}
if (voidPath) {
editor.removeNodeByKey(voidPath)
} else {
editor.delete()
}
})
next()
}
/**
* On drag end.
*
* @param {Event} event
* @param {Editor} editor
* @param {Function} next
*/
function onDragEnd(event, editor, next) {
debug('onDragEnd', { event })
isDraggingInternally = null
next()
}
/**
* On drag start.
*
* @param {Event} event
* @param {Editor} editor
* @param {Function} next
*/
function onDragStart(event, editor, next) {
debug('onDragStart', { event })
isDraggingInternally = true
const { value } = editor
const { document } = value
const path = editor.findPath(event.target)
const node = document.getNode(path)
const ancestors = document.getAncestors(path)
const isVoid =
node && (editor.isVoid(node) || ancestors.some(a => editor.isVoid(a)))
const selectionIncludesNode = value.blocks.some(block => block === node)
// If a void block is dragged and is not selected, select it (necessary for local drags).
if (isVoid && !selectionIncludesNode) {
editor.moveToRangeOfNode(node)
}
const fragment = editor.value.fragment
const encoded = Base64.serializeNode(fragment)
setEventTransfer(event, 'fragment', encoded)
next()
}
/**
* On drop.
*
* @param {Event} event
* @param {Editor} editor
* @param {Function} next
*/
function onDrop(event, editor, next) {
const { value } = editor
const { document, selection } = value
const window = getWindow(event.target)
let target = editor.findEventRange(event)
if (!target) {
return next()
}
debug('onDrop', { event })
const transfer = getEventTransfer(event)
const { type, fragment, text } = transfer
editor.focus()
// If the drag is internal and the target is after the selection, it
// needs to account for the selection's content being deleted.
if (
isDraggingInternally &&
selection.end.offset < target.end.offset &&
selection.end.path.equals(target.end.path)
) {
target = target.moveForward(
selection.start.path.equals(selection.end.path)
? 0 - selection.end.offset + selection.start.offset
: 0 - selection.end.offset
)
}
if (isDraggingInternally) {
editor.delete()
}
editor.select(target)
if (type === 'text' || type === 'html') {
const { anchor } = target
let hasVoidParent = document.hasVoidParent(anchor.path, editor)
if (hasVoidParent) {
let p = anchor.path
let n = document.getNode(anchor.path)
while (hasVoidParent) {
const [nxt] = document.texts({ path: p })
if (!nxt) {
break
}
;[n, p] = nxt
hasVoidParent = document.hasVoidParent(p, editor)
}
if (n) editor.moveToStartOfNode(n)
}
if (text) {
text.split('\n').forEach((line, i) => {
if (i > 0) editor.splitBlock()
editor.insertText(line)
})
}
}
if (type === 'fragment') {
editor.insertFragment(fragment)
}
// COMPAT: React's onSelect event breaks after an onDrop event
// has fired in a node: https://github.com/facebook/react/issues/11379.
// Until this is fixed in React, we dispatch a mouseup event on that
// DOM node, since that will make it go back to normal.
const el = editor.findDOMNode(target.focus.path)
if (el) {
el.dispatchEvent(
new MouseEvent('mouseup', {
view: window,
bubbles: true,
cancelable: true,
})
)
}
next()
}
/**
* On focus.
*
* @param {Event} event
* @param {Editor} editor
* @param {Function} next
*/
function onFocus(event, editor, next) {
debug('onFocus', { event })
// COMPAT: If the focus event is a mouse-based one, it will be shortly
// followed by a `selectionchange`, so we need to deselect here to prevent
// the old selection from being set by the `updateSelection` of `<Content>`,
// preventing the `selectionchange` from firing. (2018/11/07)
if (isMouseDown && !IS_IE && !IS_EDGE) {
editor.deselect().focus()
} else {
editor.focus()
}
next()
}
/**
* On input.
*
* @param {Event} event
* @param {Editor} editor
* @param {Function} next
*/
function onInput(event, editor, next) {
debug('onInput')
const window = getWindow(event.target)
const domSelection = window.getSelection()
const selection = editor.findSelection(domSelection)
if (selection) {
editor.select(selection)
} else {
editor.blur()
}
const { anchorNode } = domSelection
editor.reconcileDOMNode(anchorNode)
next()
}
/**
* On key down.
*
* @param {Event} event
* @param {Editor} editor
* @param {Function} next
*/
function onKeyDown(event, editor, next) {
debug('onKeyDown', { event })
const { value } = editor
const { document, selection } = value
const { start } = selection
const hasVoidParent = document.hasVoidParent(start.path, editor)
// COMPAT: In iOS, some of these hotkeys are handled in the
// `onNativeBeforeInput` handler of the `<Content>` component in order to
// preserve native autocorrect behavior, so they shouldn't be handled here.
if (Hotkeys.isSplitBlock(event) && !IS_IOS) {
return hasVoidParent
? editor.moveToStartOfNextText()
: editor.splitBlock()
}
if (Hotkeys.isDeleteBackward(event) && !IS_IOS) {
return editor.deleteCharBackward()
}
if (Hotkeys.isDeleteForward(event) && !IS_IOS) {
return editor.deleteCharForward()
}
if (Hotkeys.isDeleteLineBackward(event)) {
return editor.deleteLineBackward()
}
if (Hotkeys.isDeleteLineForward(event)) {
return editor.deleteLineForward()
}
if (Hotkeys.isDeleteWordBackward(event)) {
return editor.deleteWordBackward()
}
if (Hotkeys.isDeleteWordForward(event)) {
return editor.deleteWordForward()
}
if (Hotkeys.isRedo(event)) {
return editor.redo()
}
if (Hotkeys.isUndo(event)) {
return editor.undo()
}
// COMPAT: Certain browsers don't handle the selection updates properly. In
// Chrome, the selection isn't properly extended. And in Firefox, the
// selection isn't properly collapsed. (2017/10/17)
if (Hotkeys.isMoveLineBackward(event)) {
event.preventDefault()
return editor.moveToStartOfBlock()
}
if (Hotkeys.isMoveLineForward(event)) {
event.preventDefault()
return editor.moveToEndOfBlock()
}
if (Hotkeys.isExtendLineBackward(event)) {
event.preventDefault()
return editor.moveFocusToStartOfBlock()
}
if (Hotkeys.isExtendLineForward(event)) {
event.preventDefault()
return editor.moveFocusToEndOfBlock()
}
// COMPAT: If a void node is selected, or a zero-width text node adjacent to
// an inline is selected, we need to handle these hotkeys manually because
// browsers won't know what to do.
if (Hotkeys.isMoveBackward(event)) {
event.preventDefault()
if (!selection.isCollapsed) {
return editor.moveToStart()
}
return editor.moveBackward()
}
if (Hotkeys.isMoveForward(event)) {
event.preventDefault()
if (!selection.isCollapsed) {
return editor.moveToEnd()
}
return editor.moveForward()
}
if (Hotkeys.isMoveWordBackward(event)) {
event.preventDefault()
return editor.moveWordBackward()
}
if (Hotkeys.isMoveWordForward(event)) {
event.preventDefault()
return editor.moveWordForward()
}
if (Hotkeys.isExtendBackward(event)) {
const startText = document.getNode(start.path)
const [prevEntry] = document.texts({
path: start.path,
direction: 'backward',
})
let isPrevInVoid = false
if (prevEntry) {
const [, prevPath] = prevEntry
isPrevInVoid = document.hasVoidParent(prevPath, editor)
}
if (hasVoidParent || isPrevInVoid || startText.text === '') {
event.preventDefault()
return editor.moveFocusBackward()
}
}
if (Hotkeys.isExtendForward(event)) {
const startText = document.getNode(start.path)
const [nextEntry] = document.texts({ path: start.path })
let isNextInVoid = false
if (nextEntry) {
const [, nextPath] = nextEntry
isNextInVoid = document.hasVoidParent(nextPath, editor)
}
if (hasVoidParent || isNextInVoid || startText.text === '') {
event.preventDefault()
return editor.moveFocusForward()
}
}
next()
}
/**
* On mouse down.
*
* @param {Event} event
* @param {Editor} editor
* @param {Function} next
*/
function onMouseDown(event, editor, next) {
debug('onMouseDown', { event })
isMouseDown = true
next()
}
/**
* On mouse up.
*
* @param {Event} event
* @param {Editor} editor
* @param {Function} next
*/
function onMouseUp(event, editor, next) {
debug('onMouseUp', { event })
isMouseDown = false
next()
}
/**
* On paste.
*
* @param {Event} event
* @param {Editor} editor
* @param {Function} next
*/
function onPaste(event, editor, next) {
debug('onPaste', { event })
const { value } = editor
const transfer = getEventTransfer(event)
const { type, fragment, text } = transfer
if (type === 'fragment') {
editor.insertFragment(fragment)
}
if (type === 'text' || type === 'html') {
if (!text) return next()
const { document, selection, startBlock } = value
if (editor.isVoid(startBlock)) return next()
const defaultBlock = startBlock
const defaultMarks = document.getInsertMarksAtRange(selection)
const frag = Plain.deserialize(text, { defaultBlock, defaultMarks })
.document
editor.insertFragment(frag)
}
next()
}
/**
* On select.
*
* @param {Event} event
* @param {Editor} editor
* @param {Function} next
*/
function onSelect(event, editor, next) {
debug('onSelect', { event })
const window = getWindow(event.target)
const domSelection = window.getSelection()
const selection = editor.findSelection(domSelection)
if (selection) {
editor.select(selection)
} else {
editor.blur()
}
// COMPAT: reset the `isMouseDown` state here in case a `mouseup` event
// happens outside the editor. This is needed for `onFocus` handling.
isMouseDown = false
next()
}
/**
* Return the plugin.
*
* @type {Object}
*/
return {
onBeforeInput,
onBlur,
onClick,
onCopy,
onCut,
onDragEnd,
onDragStart,
onDrop,
onFocus,
onInput,
onKeyDown,
onMouseDown,
onMouseUp,
onPaste,
onSelect,
}
}
/**
* Export.
*
* @type {Function}
*/
export default AfterPlugin