• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

worktile / slate-angular / 543eb906-9e4d-47ae-b0a3-bf4f680c78ba

pending completion
543eb906-9e4d-47ae-b0a3-bf4f680c78ba

push

circleci

Maple13
build: optimizing angular 15 upgrade

267 of 876 branches covered (30.48%)

Branch coverage included in aggregate %.

682 of 1485 relevant lines covered (45.93%)

30.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

22.85
/packages/src/components/editable/editable.component.ts
1
import {
2
    Component,
3
    OnInit,
4
    Input,
5
    ViewChild,
6
    HostBinding,
7
    Renderer2,
8
    ElementRef,
9
    ChangeDetectionStrategy,
10
    OnDestroy,
11
    ChangeDetectorRef,
12
    NgZone,
13
    Injector,
14
    forwardRef,
15
    OnChanges,
16
    SimpleChanges,
17
    AfterViewChecked,
18
    DoCheck
19
} from '@angular/core';
20
import { NODE_TO_ELEMENT, IS_FOCUSED, EDITOR_TO_ELEMENT, ELEMENT_TO_NODE, IS_READONLY, EDITOR_TO_ON_CHANGE, EDITOR_TO_WINDOW } from '../../utils/weak-maps';
21
import { Text as SlateText, Element, Transforms, Editor, Range, Path, NodeEntry, Node, Descendant } from 'slate';
22
import getDirection from 'direction';
23
import { AngularEditor } from '../../plugins/angular-editor';
24
import {
25
    DOMElement,
26
    DOMNode,
27
    isDOMNode,
28
    DOMStaticRange,
29
    DOMRange,
30
    isDOMElement,
31
    isPlainTextOnlyPaste,
32
    DOMSelection,
33
    getDefaultView
34
} from '../../utils/dom';
35
import { Subject } from 'rxjs';
36
import { IS_FIREFOX, IS_SAFARI, IS_EDGE_LEGACY, IS_CHROME_LEGACY, IS_CHROME, HAS_BEFORE_INPUT_SUPPORT } from '../../utils/environment';
37
import Hotkeys from '../../utils/hotkeys';
38
import { BeforeInputEvent, extractBeforeInputEvent } from '../../custom-event/BeforeInputEventPlugin';
39
import { BEFORE_INPUT_EVENTS } from '../../custom-event/before-input-polyfill';
40
import { SlateErrorCode } from '../../types/error';
41
import { SlateStringTemplateComponent } from '../string/template.component';
42
import { NG_VALUE_ACCESSOR } from '@angular/forms';
43
import { SlateChildrenContext, SlateViewContext } from '../../view/context';
44
import { ViewType } from '../../types/view';
45
import { HistoryEditor } from 'slate-history';
46
import { isDecoratorRangeListEqual, check, normalize } from '../../utils';
47
import { SlatePlaceholder } from '../../types/feature';
48

49
// not correctly clipboardData on beforeinput
50
const forceOnDOMPaste = IS_SAFARI;
1✔
51

52
@Component({
53
    selector: 'slate-editable',
54
    host: {
55
        class: 'slate-editable-container',
56
        '[attr.contenteditable]': 'readonly ? undefined : true',
57
        '[attr.role]': `readonly ? undefined : 'textbox'`,
58
        '[attr.spellCheck]': `!hasBeforeInputSupport ? false : spellCheck`,
59
        '[attr.autoCorrect]': `!hasBeforeInputSupport ? 'false' : autoCorrect`,
60
        '[attr.autoCapitalize]': `!hasBeforeInputSupport ? 'false' : autoCapitalize`
61
    },
62
    templateUrl: 'editable.component.html',
63
    changeDetection: ChangeDetectionStrategy.OnPush,
64
    providers: [{
65
        provide: NG_VALUE_ACCESSOR,
66
        useExisting: forwardRef(() => SlateEditableComponent),
14✔
67
        multi: true
68
    }]
69
})
70
export class SlateEditableComponent implements OnInit, OnChanges, OnDestroy, AfterViewChecked, DoCheck {
1✔
71
    viewContext: SlateViewContext;
72
    context: SlateChildrenContext;
73

74
    private destroy$ = new Subject();
14✔
75

76
    isComposing = false;
14✔
77
    isDraggingInternally = false;
14✔
78
    isUpdatingSelection = false;
14✔
79
    latestElement = null as DOMElement | null;
14✔
80

81
    protected manualListeners: (() => void)[] = [];
14✔
82

83
    private initialized: boolean;
84

85
    private onTouchedCallback: () => void = () => { };
14✔
86

87
    private onChangeCallback: (_: any) => void = () => { };
14✔
88

89
    @Input() editor: AngularEditor;
90

91
    @Input() renderElement: (element: Element) => ViewType | null;
92

93
    @Input() renderLeaf: (text: SlateText) => ViewType | null;
94

95
    @Input() renderText: (text: SlateText) => ViewType | null;
96

97
    @Input() decorate: (entry: NodeEntry) => Range[] = () => [];
208✔
98

99
    @Input() placeholderDecorate: (editor: Editor) => SlatePlaceholder[];
100

101
    @Input() isStrictDecorate: boolean = true;
14✔
102

103
    @Input() trackBy: (node: Element) => any = () => null;
196✔
104

105
    @Input() readonly = false;
14✔
106

107
    @Input() placeholder: string;
108

109
    //#region input event handler
110
    @Input() beforeInput: (event: Event) => void;
111
    @Input() blur: (event: Event) => void;
112
    @Input() click: (event: MouseEvent) => void;
113
    @Input() compositionEnd: (event: CompositionEvent) => void;
114
    @Input() compositionStart: (event: CompositionEvent) => void;
115
    @Input() copy: (event: ClipboardEvent) => void;
116
    @Input() cut: (event: ClipboardEvent) => void;
117
    @Input() dragOver: (event: DragEvent) => void;
118
    @Input() dragStart: (event: DragEvent) => void;
119
    @Input() dragEnd: (event: DragEvent) => void;
120
    @Input() drop: (event: DragEvent) => void;
121
    @Input() focus: (event: Event) => void;
122
    @Input() keydown: (event: KeyboardEvent) => void;
123
    @Input() paste: (event: ClipboardEvent) => void;
124
    //#endregion
125

126
    //#region DOM attr
127
    @Input() spellCheck = false;
14✔
128
    @Input() autoCorrect = false;
14✔
129
    @Input() autoCapitalize = false;
14✔
130

131
    @HostBinding('attr.data-slate-editor') dataSlateEditor = true;
14✔
132
    @HostBinding('attr.data-slate-node') dataSlateNode = 'value';
14✔
133
    @HostBinding('attr.data-gramm') dataGramm = false;
14✔
134

135
    get hasBeforeInputSupport() {
136
        return HAS_BEFORE_INPUT_SUPPORT;
288✔
137
    }
138
    //#endregion
139

140
    @ViewChild('templateComponent', { static: true }) templateComponent: SlateStringTemplateComponent;
141
    @ViewChild('templateComponent', { static: true, read: ElementRef }) templateElementRef: ElementRef<any>;
142

143
    constructor(
144
        public elementRef: ElementRef,
14✔
145
        public renderer2: Renderer2,
14✔
146
        public cdr: ChangeDetectorRef,
14✔
147
        private ngZone: NgZone,
14✔
148
        private injector: Injector
14✔
149
    ) { }
150

151
    ngOnInit() {
152
        this.editor.injector = this.injector;
14✔
153
        this.editor.children = [];
14✔
154
        let window = getDefaultView(this.elementRef.nativeElement);
14✔
155
        EDITOR_TO_WINDOW.set(this.editor, window);
14✔
156
        EDITOR_TO_ELEMENT.set(this.editor, this.elementRef.nativeElement);
14✔
157
        NODE_TO_ELEMENT.set(this.editor, this.elementRef.nativeElement);
14✔
158
        ELEMENT_TO_NODE.set(this.elementRef.nativeElement, this.editor);
14✔
159
        IS_READONLY.set(this.editor, this.readonly);
14✔
160
        EDITOR_TO_ON_CHANGE.set(this.editor, () => {
14✔
161
            this.ngZone.run(() => {
8✔
162
                this.onChange();
8✔
163
            });
164
        });
165
        this.ngZone.runOutsideAngular(() => {
14✔
166
            this.initialize();
14✔
167
        });
168
        this.initializeViewContext();
14✔
169
        this.initializeContext();
14✔
170

171
        // remove unused DOM, just keep templateComponent instance
172
        this.templateElementRef.nativeElement.remove();
14✔
173

174
        // add browser class
175
        let browserClass = IS_FIREFOX ? 'firefox' : (IS_SAFARI ? 'safari' : '');
14!
176
        browserClass && this.elementRef.nativeElement.classList.add(browserClass);
14!
177
    }
178

179
    ngOnChanges(simpleChanges: SimpleChanges) {
180
        if (!this.initialized) {
17✔
181
            return;
14✔
182
        }
183
        const decorateChange = simpleChanges['decorate'];
3✔
184
        if (decorateChange) {
3✔
185
            this.forceFlush();
2✔
186
        }
187
        const placeholderChange = simpleChanges['placeholder'];
3✔
188
        if (placeholderChange) {
3✔
189
            this.detectContext();
1✔
190
        }
191
        const readonlyChange = simpleChanges['readonly'];
3✔
192
        if (readonlyChange) {
3!
193
            IS_READONLY.set(this.editor, this.readonly);
×
194
            this.detectContext();
×
195
            this.toSlateSelection();
×
196
        }
197
    }
198

199
    registerOnChange(fn: any) {
200
        this.onChangeCallback = fn;
14✔
201
    }
202
    registerOnTouched(fn: any) {
203
        this.onTouchedCallback = fn;
14✔
204
    }
205

206
    writeValue(value: Element[]) {
207
        if (value && value.length) {
31✔
208
            if (check(value)) {
17!
209
                this.editor.children = value;
17✔
210
            } else {
211
                this.editor.onError({
×
212
                    code: SlateErrorCode.InvalidValueError,
213
                    name: 'initialize invalid data',
214
                    data: value
215
                });
216
                this.editor.children = normalize(value);
×
217
            }
218
            this.initializeContext();
17✔
219
            this.cdr.markForCheck();
17✔
220
        }
221
    }
222

223
    initialize() {
224
        this.initialized = true;
14✔
225
        const window = AngularEditor.getWindow(this.editor);
14✔
226
        this.addEventListener(
14✔
227
            'selectionchange',
228
            event => {
229
                this.toSlateSelection();
1✔
230
            },
231
            window.document
232
        );
233
        if (HAS_BEFORE_INPUT_SUPPORT) {
14✔
234
            this.addEventListener('beforeinput', this.onDOMBeforeInput.bind(this));
14✔
235
        }
236
        this.addEventListener('blur', this.onDOMBlur.bind(this));
14✔
237
        this.addEventListener('click', this.onDOMClick.bind(this));
14✔
238
        this.addEventListener('compositionend', this.onDOMCompositionEnd.bind(this));
14✔
239
        this.addEventListener('compositionstart', this.onDOMCompositionStart.bind(this));
14✔
240
        this.addEventListener('copy', this.onDOMCopy.bind(this));
14✔
241
        this.addEventListener('cut', this.onDOMCut.bind(this));
14✔
242
        this.addEventListener('dragover', this.onDOMDragOver.bind(this));
14✔
243
        this.addEventListener('dragstart', this.onDOMDragStart.bind(this));
14✔
244
        this.addEventListener('dragend', this.onDOMDragEnd.bind(this));
14✔
245
        this.addEventListener('drop', this.onDOMDrop.bind(this));
14✔
246
        this.addEventListener('focus', this.onDOMFocus.bind(this));
14✔
247
        this.addEventListener('keydown', this.onDOMKeydown.bind(this));
14✔
248
        this.addEventListener('paste', this.onDOMPaste.bind(this));
14✔
249
        BEFORE_INPUT_EVENTS.forEach(event => {
14✔
250
            this.addEventListener(event.name, () => { });
70✔
251
        });
252
    }
253

254
    toNativeSelection() {
255
        try {
10✔
256
            const { selection } = this.editor;
10✔
257
            const root = AngularEditor.findDocumentOrShadowRoot(this.editor);
10✔
258
            const { activeElement } = root;
10✔
259
            const domSelection = (root as Document).getSelection();
10✔
260

261
            if (this.isComposing || !domSelection || !AngularEditor.isFocused(this.editor)) {
10✔
262
                return;
10✔
263
            }
264

265
            const hasDomSelection = domSelection.type !== 'None';
×
266

267
            // If the DOM selection is properly unset, we're done.
268
            if (!selection && !hasDomSelection) {
×
269
                return;
×
270
            }
271

272
            // If the DOM selection is already correct, we're done.
273
            // verify that the dom selection is in the editor
274
            const editorElement = EDITOR_TO_ELEMENT.get(this.editor)!;
×
275
            let hasDomSelectionInEditor = false;
×
276
            if (editorElement.contains(domSelection.anchorNode) && editorElement.contains(domSelection.focusNode)) {
×
277
                hasDomSelectionInEditor = true;
×
278
            }
279

280
            // If the DOM selection is in the editor and the editor selection is already correct, we're done.
281
            if (
×
282
                hasDomSelection &&
×
283
                hasDomSelectionInEditor &&
284
                selection &&
285
                hasStringTarget(domSelection) &&
286
                Range.equals(AngularEditor.toSlateRange(this.editor, domSelection), selection)
287
            ) {
288
                return;
×
289
            }
290

291
            // prevent updating native selection when active element is void element
292
            if (isTargetInsideVoid(this.editor, activeElement)) {
×
293
                return;
×
294
            }
295

296
            // when <Editable/> is being controlled through external value
297
            // then its children might just change - DOM responds to it on its own
298
            // but Slate's value is not being updated through any operation
299
            // and thus it doesn't transform selection on its own
300
            if (selection && !AngularEditor.hasRange(this.editor, selection)) {
×
301
                this.editor.selection = AngularEditor.toSlateRange(this.editor, domSelection);
×
302
                return
×
303
            }
304

305
            // Otherwise the DOM selection is out of sync, so update it.
306
            const el = AngularEditor.toDOMNode(this.editor, this.editor);
×
307
            this.isUpdatingSelection = true;
×
308

309
            const newDomRange = selection && AngularEditor.toDOMRange(this.editor, selection);
×
310

311
            if (newDomRange) {
×
312
                // COMPAT: Since the DOM range has no concept of backwards/forwards
313
                // we need to check and do the right thing here.
314
                if (Range.isBackward(selection)) {
×
315
                    // eslint-disable-next-line max-len
316
                    domSelection.setBaseAndExtent(
×
317
                        newDomRange.endContainer,
318
                        newDomRange.endOffset,
319
                        newDomRange.startContainer,
320
                        newDomRange.startOffset
321
                    );
322
                } else {
323
                    // eslint-disable-next-line max-len
324
                    domSelection.setBaseAndExtent(
×
325
                        newDomRange.startContainer,
326
                        newDomRange.startOffset,
327
                        newDomRange.endContainer,
328
                        newDomRange.endOffset
329
                    );
330
                }
331
            } else {
332
                domSelection.removeAllRanges();
×
333
            }
334

335
            setTimeout(() => {
×
336
                // COMPAT: In Firefox, it's not enough to create a range, you also need
337
                // to focus the contenteditable element too. (2016/11/16)
338
                if (newDomRange && IS_FIREFOX) {
×
339
                    el.focus();
×
340
                }
341

342
                this.isUpdatingSelection = false;
×
343
            });
344
        } catch (error) {
345
            this.editor.onError({ code: SlateErrorCode.ToNativeSelectionError, nativeError: error })
×
346
        }
347
    }
348

349
    onChange() {
350
        this.forceFlush();
8✔
351
        this.onChangeCallback(this.editor.children);
8✔
352
    }
353

354
    ngAfterViewChecked() {
355
    }
356

357
    ngDoCheck() {
358
    }
359

360
    forceFlush() {
361
        this.detectContext();
10✔
362
        this.cdr.detectChanges();
10✔
363
        // repair collaborative editing when Chinese input is interrupted by other users' cursors
364
        // when the DOMElement where the selection is located is removed
365
        // the compositionupdate and compositionend events will no longer be fired
366
        // so isComposing needs to be corrected
367
        // need exec after this.cdr.detectChanges() to render HTML
368
        // need exec before this.toNativeSelection() to correct native selection
369
        if (this.isComposing) {
10!
370
            // Composition input text be not rendered when user composition input with selection is expanded
371
            // At this time, the following matching conditions are met, assign isComposing to false, and the status is wrong
372
            // this time condition is true and isComposiing is assigned false
373
            // Therefore, need to wait for the composition input text to be rendered before performing condition matching
374
            setTimeout(() => {
×
375
                const textNode = Node.get(this.editor, this.editor.selection.anchor.path);
×
376
                const textDOMNode = AngularEditor.toDOMNode(this.editor, textNode);
×
377
                let textContent = '';
×
378
                // skip decorate text
379
                textDOMNode.querySelectorAll('[editable-text]').forEach((stringDOMNode) => {
×
380
                    let text = stringDOMNode.textContent;
×
381
                    const zeroChar = '\uFEFF';
×
382
                    // remove zero with char
383
                    if (text.startsWith(zeroChar)) {
×
384
                        text = text.slice(1);
×
385
                    }
386
                    if (text.endsWith(zeroChar)) {
×
387
                        text = text.slice(0, text.length - 1);
×
388
                    }
389
                    textContent += text;
×
390
                });
391
                if (Node.string(textNode).endsWith(textContent)) {
×
392
                    this.isComposing = false;
×
393
                }
394
            }, 0);
395
        }
396
        this.toNativeSelection();
10✔
397
    }
398

399
    initializeContext() {
400
        this.context = {
31✔
401
            parent: this.editor,
402
            selection: this.editor.selection,
403
            decorations: this.generateDecorations(),
404
            decorate: this.decorate,
405
            readonly: this.readonly
406
        };
407
    }
408

409
    initializeViewContext() {
410
        this.viewContext = {
14✔
411
            editor: this.editor,
412
            renderElement: this.renderElement,
413
            renderLeaf: this.renderLeaf,
414
            renderText: this.renderText,
415
            trackBy: this.trackBy,
416
            isStrictDecorate: this.isStrictDecorate,
417
            templateComponent: this.templateComponent
418
        };
419
    }
420

421
    detectContext() {
422
        const decorations = this.generateDecorations();
12✔
423
        if (this.context.selection !== this.editor.selection ||
12✔
424
            this.context.decorate !== this.decorate ||
425
            this.context.readonly !== this.readonly ||
426
            !isDecoratorRangeListEqual(this.context.decorations, decorations)) {
427
            this.context = {
7✔
428
                parent: this.editor,
429
                selection: this.editor.selection,
430
                decorations: decorations,
431
                decorate: this.decorate,
432
                readonly: this.readonly
433
            };
434
        }
435
    }
436

437
    composePlaceholderDecorate(editor: Editor) {
438
        if (this.placeholderDecorate) {
41!
439
            return this.placeholderDecorate(editor) || [];
×
440
        }
441

442
        if (
41✔
443
            this.placeholder &&
52✔
444
            editor.children.length === 1 &&
445
            Array.from(Node.texts(editor)).length === 1 &&
446
            Node.string(editor) === ''
447
        ) {
448
            const start = Editor.start(editor, [])
3✔
449
            return [
3✔
450
                {
451
                    placeholder: this.placeholder,
452
                    anchor: start,
453
                    focus: start,
454
                },
455
            ]
456
        } else {
457
            return []
38✔
458
        }
459
    }
460

461
    generateDecorations() {
462
        const decorations = this.decorate([this.editor, []]);
43✔
463
        const placeholderDecorations = this.isComposing
43✔
464
            ? []
465
            : this.composePlaceholderDecorate(this.editor)
466
        decorations.push(...placeholderDecorations);
43✔
467
        return decorations;
43✔
468
    }
469

470
    //#region event proxy
471
    private addEventListener(eventName: string, listener: EventListener, target: HTMLElement | Document = this.elementRef.nativeElement) {
266✔
472
        this.manualListeners.push(
280✔
473
            this.renderer2.listen(target, eventName, (event: Event) => {
474
                const beforeInputEvent = extractBeforeInputEvent(event.type, null, event, event.target);
3✔
475
                if (beforeInputEvent) {
3!
476
                    this.onFallbackBeforeInput(beforeInputEvent);
×
477
                }
478
                listener(event);
3✔
479
            })
480
        );
481
    }
482

483
    private toSlateSelection() {
484
        if (!this.isComposing && !this.isUpdatingSelection && !this.isDraggingInternally) {
1!
485
            try {
×
486
                const root = AngularEditor.findDocumentOrShadowRoot(this.editor)
×
487
                const { activeElement } = root;
×
488
                const el = AngularEditor.toDOMNode(this.editor, this.editor);
×
489
                const domSelection = (root as Document).getSelection();
×
490

491
                if (activeElement === el) {
×
492
                    this.latestElement = activeElement;
×
493
                    IS_FOCUSED.set(this.editor, true);
×
494
                } else {
495
                    IS_FOCUSED.delete(this.editor);
×
496
                }
497

498
                if (!domSelection) {
×
499
                    return Transforms.deselect(this.editor);
×
500
                }
501

502
                const editorElement = EDITOR_TO_ELEMENT.get(this.editor);
×
503
                const hasDomSelectionInEditor = editorElement.contains(domSelection.anchorNode) && editorElement.contains(domSelection.focusNode);
×
504
                if (!hasDomSelectionInEditor) {
×
505
                    Transforms.deselect(this.editor);
×
506
                    return;
×
507
                }
508

509
                // try to get the selection directly, because some terrible case can be normalize for normalizeDOMPoint
510
                // for example, double-click the last cell of the table to select a non-editable DOM
511
                const range = AngularEditor.toSlateRange(this.editor, domSelection);
×
512
                if (this.editor.selection && Range.equals(range, this.editor.selection) && !hasStringTarget(domSelection)) {
×
513
                    if (!isTargetInsideVoid(this.editor, activeElement)) {
×
514
                        // force adjust DOMSelection
515
                        this.toNativeSelection();
×
516
                    }
517
                } else {
518
                    Transforms.select(this.editor, range);
×
519
                }
520
            } catch (error) {
521
                this.editor.onError({ code: SlateErrorCode.ToSlateSelectionError, nativeError: error })
×
522
            }
523
        }
524
    }
525

526
    private onDOMBeforeInput(
527
        event: Event & {
528
            inputType: string;
529
            isComposing: boolean;
530
            data: string | null;
531
            dataTransfer: DataTransfer | null;
532
            getTargetRanges(): DOMStaticRange[];
533
        }
534
    ) {
535
        const editor = this.editor;
×
536
        const root = AngularEditor.findDocumentOrShadowRoot(this.editor)
×
537
        const { activeElement } = root;
×
538
        if (!this.readonly && hasEditableTarget(editor, event.target) && !isTargetInsideVoid(editor, activeElement) && !this.isDOMEventHandled(event, this.beforeInput)) {
×
539
            try {
×
540
                const { selection } = editor;
×
541
                const { inputType: type } = event;
×
542
                const data = event.dataTransfer || event.data || undefined;
×
543
                event.preventDefault();
×
544

545
                // COMPAT: If the selection is expanded, even if the command seems like
546
                // a delete forward/backward command it should delete the selection.
547
                if (selection && Range.isExpanded(selection) && type.startsWith('delete')) {
×
548
                    const direction = type.endsWith('Backward') ? 'backward' : 'forward';
×
549
                    Editor.deleteFragment(editor, { direction });
×
550
                    return;
×
551
                }
552

553
                switch (type) {
×
554
                    case 'deleteByComposition':
555
                    case 'deleteByCut':
556
                    case 'deleteByDrag': {
557
                        Editor.deleteFragment(editor);
×
558
                        break;
×
559
                    }
560

561
                    case 'deleteContent':
562
                    case 'deleteContentForward': {
563
                        Editor.deleteForward(editor);
×
564
                        break;
×
565
                    }
566

567
                    case 'deleteContentBackward': {
568
                        Editor.deleteBackward(editor);
×
569
                        break;
×
570
                    }
571

572
                    case 'deleteEntireSoftLine': {
573
                        Editor.deleteBackward(editor, { unit: 'line' });
×
574
                        Editor.deleteForward(editor, { unit: 'line' });
×
575
                        break;
×
576
                    }
577

578
                    case 'deleteHardLineBackward': {
579
                        Editor.deleteBackward(editor, { unit: 'block' });
×
580
                        break;
×
581
                    }
582

583
                    case 'deleteSoftLineBackward': {
584
                        Editor.deleteBackward(editor, { unit: 'line' });
×
585
                        break;
×
586
                    }
587

588
                    case 'deleteHardLineForward': {
589
                        Editor.deleteForward(editor, { unit: 'block' });
×
590
                        break;
×
591
                    }
592

593
                    case 'deleteSoftLineForward': {
594
                        Editor.deleteForward(editor, { unit: 'line' });
×
595
                        break;
×
596
                    }
597

598
                    case 'deleteWordBackward': {
599
                        Editor.deleteBackward(editor, { unit: 'word' });
×
600
                        break;
×
601
                    }
602

603
                    case 'deleteWordForward': {
604
                        Editor.deleteForward(editor, { unit: 'word' });
×
605
                        break;
×
606
                    }
607

608
                    case 'insertLineBreak':
609
                    case 'insertParagraph': {
610
                        Editor.insertBreak(editor);
×
611
                        break;
×
612
                    }
613

614
                    case 'insertFromComposition': {
615
                        // COMPAT: in safari, `compositionend` event is dispatched after
616
                        // the beforeinput event with the inputType "insertFromComposition" has been dispatched.
617
                        // https://www.w3.org/TR/input-events-2/
618
                        // so the following code is the right logic
619
                        // because DOM selection in sync will be exec before `compositionend` event
620
                        // isComposing is true will prevent DOM selection being update correctly.
621
                        this.isComposing = false;
×
622
                        preventInsertFromComposition(event, this.editor);
×
623
                    }
624
                    case 'insertFromDrop':
625
                    case 'insertFromPaste':
626
                    case 'insertFromYank':
627
                    case 'insertReplacementText':
628
                    case 'insertText': {
629
                        // use a weak comparison instead of 'instanceof' to allow
630
                        // programmatic access of paste events coming from external windows
631
                        // like cypress where cy.window does not work realibly
632
                        if (data?.constructor.name === 'DataTransfer') {
×
633
                            AngularEditor.insertData(editor, data as DataTransfer);
×
634
                        } else if (typeof data === 'string') {
×
635
                            Editor.insertText(editor, data);
×
636
                        }
637
                        break;
×
638
                    }
639
                }
640
            } catch (error) {
641
                this.editor.onError({ code: SlateErrorCode.OnDOMBeforeInputError, nativeError: error });
×
642
            }
643
        }
644
    }
645

646
    private onDOMBlur(event: FocusEvent) {
647
        if (
×
648
            this.readonly ||
×
649
            this.isUpdatingSelection ||
650
            !hasEditableTarget(this.editor, event.target) ||
651
            this.isDOMEventHandled(event, this.blur)
652
        ) {
653
            return;
×
654
        }
655

656
        const window = AngularEditor.getWindow(this.editor);
×
657

658
        // COMPAT: If the current `activeElement` is still the previous
659
        // one, this is due to the window being blurred when the tab
660
        // itself becomes unfocused, so we want to abort early to allow to
661
        // editor to stay focused when the tab becomes focused again.
662
        const root = AngularEditor.findDocumentOrShadowRoot(this.editor);
×
663
        if (this.latestElement === root.activeElement) {
×
664
            return;
×
665
        }
666

667
        const { relatedTarget } = event;
×
668
        const el = AngularEditor.toDOMNode(this.editor, this.editor);
×
669

670
        // COMPAT: The event should be ignored if the focus is returning
671
        // to the editor from an embedded editable element (eg. an <input>
672
        // element inside a void node).
673
        if (relatedTarget === el) {
×
674
            return;
×
675
        }
676

677
        // COMPAT: The event should be ignored if the focus is moving from
678
        // the editor to inside a void node's spacer element.
679
        if (isDOMElement(relatedTarget) && relatedTarget.hasAttribute('data-slate-spacer')) {
×
680
            return;
×
681
        }
682

683
        // COMPAT: The event should be ignored if the focus is moving to a
684
        // non- editable section of an element that isn't a void node (eg.
685
        // a list item of the check list example).
686
        if (relatedTarget != null && isDOMNode(relatedTarget) && AngularEditor.hasDOMNode(this.editor, relatedTarget)) {
×
687
            const node = AngularEditor.toSlateNode(this.editor, relatedTarget);
×
688

689
            if (Element.isElement(node) && !this.editor.isVoid(node)) {
×
690
                return;
×
691
            }
692
        }
693

694
        IS_FOCUSED.delete(this.editor);
×
695
    }
696

697
    private onDOMClick(event: MouseEvent) {
698
        if (
×
699
            !this.readonly &&
×
700
            hasTarget(this.editor, event.target) &&
701
            !this.isDOMEventHandled(event, this.click) &&
702
            isDOMNode(event.target)
703
        ) {
704
            const node = AngularEditor.toSlateNode(this.editor, event.target);
×
705
            const path = AngularEditor.findPath(this.editor, node);
×
706
            const start = Editor.start(this.editor, path);
×
707
            const end = Editor.end(this.editor, path);
×
708

709
            const startVoid = Editor.void(this.editor, { at: start });
×
710
            const endVoid = Editor.void(this.editor, { at: end });
×
711

712
            if (startVoid && endVoid && Path.equals(startVoid[1], endVoid[1])) {
×
713
                const range = Editor.range(this.editor, start);
×
714
                Transforms.select(this.editor, range);
×
715
            }
716
        }
717
    }
718

719
    private onDOMCompositionEnd(event: CompositionEvent) {
720
        if (!event.data && !Range.isCollapsed(this.editor.selection)) {
×
721
            Transforms.delete(this.editor);
×
722
        }
723
        if (hasEditableTarget(this.editor, event.target) && !this.isDOMEventHandled(event, this.compositionEnd)) {
×
724
            // COMPAT: In Chrome/Firefox, `beforeinput` events for compositions
725
            // aren't correct and never fire the "insertFromComposition"
726
            // type that we need. So instead, insert whenever a composition
727
            // ends since it will already have been committed to the DOM.
728
            if (this.isComposing === true && !IS_SAFARI && event.data) {
×
729
                preventInsertFromComposition(event, this.editor);
×
730
                Editor.insertText(this.editor, event.data);
×
731
            }
732

733
            // COMPAT: In Firefox 87.0 CompositionEnd fire twice
734
            // so we need avoid repeat isnertText by isComposing === true,
735
            this.isComposing = false;
×
736
        }
737
        this.detectContext();
×
738
        this.cdr.detectChanges();
×
739
    }
740

741
    private onDOMCompositionStart(event: CompositionEvent) {
742
        const { selection } = this.editor;
1✔
743

744
        if (selection) {
1!
745
            // solve the problem of cross node Chinese input
746
            if (Range.isExpanded(selection)) {
×
747
                Editor.deleteFragment(this.editor);
×
748
                this.forceFlush();
×
749
            }
750
        }
751
        if (hasEditableTarget(this.editor, event.target) && !this.isDOMEventHandled(event, this.compositionStart)) {
1✔
752
            this.isComposing = true;
1✔
753
        }
754
        this.detectContext();
1✔
755
        this.cdr.detectChanges();
1✔
756
    }
757

758
    private onDOMCopy(event: ClipboardEvent) {
759
        const window = AngularEditor.getWindow(this.editor);
×
760
        const isOutsideSlate = !hasStringTarget(window.getSelection()) && isTargetInsideVoid(this.editor, event.target);
×
761
        if (!isOutsideSlate && hasTarget(this.editor, event.target) && !this.isDOMEventHandled(event, this.copy)) {
×
762
            event.preventDefault();
×
763
            AngularEditor.setFragmentData(this.editor, event.clipboardData, 'copy');
×
764
        }
765
    }
766

767
    private onDOMCut(event: ClipboardEvent) {
768
        if (!this.readonly && hasEditableTarget(this.editor, event.target) && !this.isDOMEventHandled(event, this.cut)) {
×
769
            event.preventDefault();
×
770
            AngularEditor.setFragmentData(this.editor, event.clipboardData, 'cut');
×
771
            const { selection } = this.editor;
×
772

773
            if (selection) {
×
774
                AngularEditor.deleteCutData(this.editor);
×
775
            }
776
        }
777
    }
778

779
    private onDOMDragOver(event: DragEvent) {
780
        if (hasTarget(this.editor, event.target) && !this.isDOMEventHandled(event, this.dragOver)) {
×
781
            // Only when the target is void, call `preventDefault` to signal
782
            // that drops are allowed. Editable content is droppable by
783
            // default, and calling `preventDefault` hides the cursor.
784
            const node = AngularEditor.toSlateNode(this.editor, event.target);
×
785

786
            if (Editor.isVoid(this.editor, node)) {
×
787
                event.preventDefault();
×
788
            }
789
        }
790
    }
791

792
    private onDOMDragStart(event: DragEvent) {
793
        if (!this.readonly && hasTarget(this.editor, event.target) && !this.isDOMEventHandled(event, this.dragStart)) {
×
794
            const node = AngularEditor.toSlateNode(this.editor, event.target);
×
795
            const path = AngularEditor.findPath(this.editor, node);
×
796
            const voidMatch =
797
                Editor.isVoid(this.editor, node) ||
×
798
                Editor.void(this.editor, { at: path, voids: true });
799

800
            // If starting a drag on a void node, make sure it is selected
801
            // so that it shows up in the selection's fragment.
802
            if (voidMatch) {
×
803
                const range = Editor.range(this.editor, path);
×
804
                Transforms.select(this.editor, range);
×
805
            }
806

807
            this.isDraggingInternally = true;
×
808

809
            AngularEditor.setFragmentData(this.editor, event.dataTransfer, 'drag');
×
810
        }
811
    }
812

813
    private onDOMDrop(event: DragEvent) {
814
        const editor = this.editor;
×
815
        if (!this.readonly && hasTarget(this.editor, event.target) && !this.isDOMEventHandled(event, this.drop)) {
×
816
            event.preventDefault();
×
817
            // Keep a reference to the dragged range before updating selection
818
            const draggedRange = editor.selection;
×
819

820
            // Find the range where the drop happened
821
            const range = AngularEditor.findEventRange(editor, event);
×
822
            const data = event.dataTransfer;
×
823

824
            Transforms.select(editor, range);
×
825

826
            if (this.isDraggingInternally) {
×
827
                if (draggedRange) {
×
828
                    Transforms.delete(editor, {
×
829
                        at: draggedRange,
830
                    });
831
                }
832

833
                this.isDraggingInternally = false;
×
834
            }
835

836
            AngularEditor.insertData(editor, data);
×
837

838
            // When dragging from another source into the editor, it's possible
839
            // that the current editor does not have focus.
840
            if (!AngularEditor.isFocused(editor)) {
×
841
                AngularEditor.focus(editor);
×
842
            }
843
        }
844
    }
845

846
    private onDOMDragEnd(event: DragEvent) {
847
        if (!this.readonly && this.isDraggingInternally && hasTarget(this.editor, event.target) && !this.isDOMEventHandled(event, this.dragEnd)) {
×
848
            this.isDraggingInternally = false;
×
849
        }
850
    }
851

852
    private onDOMFocus(event: Event) {
853
        if (
1✔
854
            !this.readonly &&
4✔
855
            !this.isUpdatingSelection &&
856
            hasEditableTarget(this.editor, event.target) &&
857
            !this.isDOMEventHandled(event, this.focus)
858
        ) {
859
            const el = AngularEditor.toDOMNode(this.editor, this.editor);
1✔
860
            const root = AngularEditor.findDocumentOrShadowRoot(this.editor);
1✔
861
            this.latestElement = root.activeElement
1✔
862

863
            // COMPAT: If the editor has nested editable elements, the focus
864
            // can go to them. In Firefox, this must be prevented because it
865
            // results in issues with keyboard navigation. (2017/03/30)
866
            if (IS_FIREFOX && event.target !== el) {
1!
867
                el.focus();
×
868
                return;
×
869
            }
870

871
            IS_FOCUSED.set(this.editor, true);
1✔
872
        }
873
    }
874

875
    private onDOMKeydown(event: KeyboardEvent) {
876
        const editor = this.editor;
×
877
        const root = AngularEditor.findDocumentOrShadowRoot(this.editor)
×
878
        const { activeElement } = root;
×
879
        if (
×
880
            !this.readonly &&
×
881
            hasEditableTarget(editor, event.target) &&
882
            !isTargetInsideVoid(editor, activeElement) && // stop fire keydown handle when focus void node
883
            !this.isComposing &&
884
            !this.isDOMEventHandled(event, this.keydown)
885
        ) {
886
            const nativeEvent = event;
×
887
            const { selection } = editor;
×
888

889
            const element =
890
                editor.children[
×
891
                selection !== null ? selection.focus.path[0] : 0
×
892
                ]
893
            const isRTL = getDirection(Node.string(element)) === 'rtl';
×
894

895
            try {
×
896
                // COMPAT: Since we prevent the default behavior on
897
                // `beforeinput` events, the browser doesn't think there's ever
898
                // any history stack to undo or redo, so we have to manage these
899
                // hotkeys ourselves. (2019/11/06)
900
                if (Hotkeys.isRedo(nativeEvent)) {
×
901
                    event.preventDefault();
×
902

903
                    if (HistoryEditor.isHistoryEditor(editor)) {
×
904
                        editor.redo();
×
905
                    }
906

907
                    return;
×
908
                }
909

910
                if (Hotkeys.isUndo(nativeEvent)) {
×
911
                    event.preventDefault();
×
912

913
                    if (HistoryEditor.isHistoryEditor(editor)) {
×
914
                        editor.undo();
×
915
                    }
916

917
                    return;
×
918
                }
919

920
                // COMPAT: Certain browsers don't handle the selection updates
921
                // properly. In Chrome, the selection isn't properly extended.
922
                // And in Firefox, the selection isn't properly collapsed.
923
                // (2017/10/17)
924
                if (Hotkeys.isMoveLineBackward(nativeEvent)) {
×
925
                    event.preventDefault();
×
926
                    Transforms.move(editor, { unit: 'line', reverse: true });
×
927
                    return;
×
928
                }
929

930
                if (Hotkeys.isMoveLineForward(nativeEvent)) {
×
931
                    event.preventDefault();
×
932
                    Transforms.move(editor, { unit: 'line' });
×
933
                    return;
×
934
                }
935

936
                if (Hotkeys.isExtendLineBackward(nativeEvent)) {
×
937
                    event.preventDefault();
×
938
                    Transforms.move(editor, {
×
939
                        unit: 'line',
940
                        edge: 'focus',
941
                        reverse: true
942
                    });
943
                    return;
×
944
                }
945

946
                if (Hotkeys.isExtendLineForward(nativeEvent)) {
×
947
                    event.preventDefault();
×
948
                    Transforms.move(editor, { unit: 'line', edge: 'focus' });
×
949
                    return;
×
950
                }
951

952
                // COMPAT: If a void node is selected, or a zero-width text node
953
                // adjacent to an inline is selected, we need to handle these
954
                // hotkeys manually because browsers won't be able to skip over
955
                // the void node with the zero-width space not being an empty
956
                // string.
957
                if (Hotkeys.isMoveBackward(nativeEvent)) {
×
958
                    event.preventDefault();
×
959

960
                    if (selection && Range.isCollapsed(selection)) {
×
961
                        Transforms.move(editor, { reverse: !isRTL });
×
962
                    } else {
963
                        Transforms.collapse(editor, { edge: 'start' });
×
964
                    }
965

966
                    return;
×
967
                }
968

969
                if (Hotkeys.isMoveForward(nativeEvent)) {
×
970
                    event.preventDefault();
×
971

972
                    if (selection && Range.isCollapsed(selection)) {
×
973
                        Transforms.move(editor, { reverse: isRTL });
×
974
                    } else {
975
                        Transforms.collapse(editor, { edge: 'end' });
×
976
                    }
977

978
                    return;
×
979
                }
980

981
                if (Hotkeys.isMoveWordBackward(nativeEvent)) {
×
982
                    event.preventDefault();
×
983

984
                    if (selection && Range.isExpanded(selection)) {
×
985
                        Transforms.collapse(editor, { edge: 'focus' })
×
986
                    }
987

988
                    Transforms.move(editor, { unit: 'word', reverse: !isRTL });
×
989
                    return;
×
990
                }
991

992
                if (Hotkeys.isMoveWordForward(nativeEvent)) {
×
993
                    event.preventDefault();
×
994

995
                    if (selection && Range.isExpanded(selection)) {
×
996
                        Transforms.collapse(editor, { edge: 'focus' })
×
997
                    }
998

999
                    Transforms.move(editor, { unit: 'word', reverse: isRTL });
×
1000
                    return;
×
1001
                }
1002

1003
                // COMPAT: Certain browsers don't support the `beforeinput` event, so we
1004
                // fall back to guessing at the input intention for hotkeys.
1005
                // COMPAT: In iOS, some of these hotkeys are handled in the
1006
                if (!HAS_BEFORE_INPUT_SUPPORT) {
×
1007
                    // We don't have a core behavior for these, but they change the
1008
                    // DOM if we don't prevent them, so we have to.
1009
                    if (Hotkeys.isBold(nativeEvent) || Hotkeys.isItalic(nativeEvent) || Hotkeys.isTransposeCharacter(nativeEvent)) {
×
1010
                        event.preventDefault();
×
1011
                        return;
×
1012
                    }
1013

1014
                    if (Hotkeys.isSplitBlock(nativeEvent)) {
×
1015
                        event.preventDefault();
×
1016
                        Editor.insertBreak(editor);
×
1017
                        return;
×
1018
                    }
1019

1020
                    if (Hotkeys.isDeleteBackward(nativeEvent)) {
×
1021
                        event.preventDefault();
×
1022

1023
                        if (selection && Range.isExpanded(selection)) {
×
1024
                            Editor.deleteFragment(editor, { direction: 'backward' });
×
1025
                        } else {
1026
                            Editor.deleteBackward(editor);
×
1027
                        }
1028

1029
                        return;
×
1030
                    }
1031

1032
                    if (Hotkeys.isDeleteForward(nativeEvent)) {
×
1033
                        event.preventDefault();
×
1034

1035
                        if (selection && Range.isExpanded(selection)) {
×
1036
                            Editor.deleteFragment(editor, { direction: 'forward' });
×
1037
                        } else {
1038
                            Editor.deleteForward(editor);
×
1039
                        }
1040

1041
                        return;
×
1042
                    }
1043

1044
                    if (Hotkeys.isDeleteLineBackward(nativeEvent)) {
×
1045
                        event.preventDefault();
×
1046

1047
                        if (selection && Range.isExpanded(selection)) {
×
1048
                            Editor.deleteFragment(editor, { direction: 'backward' });
×
1049
                        } else {
1050
                            Editor.deleteBackward(editor, { unit: 'line' });
×
1051
                        }
1052

1053
                        return;
×
1054
                    }
1055

1056
                    if (Hotkeys.isDeleteLineForward(nativeEvent)) {
×
1057
                        event.preventDefault();
×
1058

1059
                        if (selection && Range.isExpanded(selection)) {
×
1060
                            Editor.deleteFragment(editor, { direction: 'forward' });
×
1061
                        } else {
1062
                            Editor.deleteForward(editor, { unit: 'line' });
×
1063
                        }
1064

1065
                        return;
×
1066
                    }
1067

1068
                    if (Hotkeys.isDeleteWordBackward(nativeEvent)) {
×
1069
                        event.preventDefault();
×
1070

1071
                        if (selection && Range.isExpanded(selection)) {
×
1072
                            Editor.deleteFragment(editor, { direction: 'backward' });
×
1073
                        } else {
1074
                            Editor.deleteBackward(editor, { unit: 'word' });
×
1075
                        }
1076

1077
                        return;
×
1078
                    }
1079

1080
                    if (Hotkeys.isDeleteWordForward(nativeEvent)) {
×
1081
                        event.preventDefault();
×
1082

1083
                        if (selection && Range.isExpanded(selection)) {
×
1084
                            Editor.deleteFragment(editor, { direction: 'forward' });
×
1085
                        } else {
1086
                            Editor.deleteForward(editor, { unit: 'word' });
×
1087
                        }
1088

1089
                        return;
×
1090
                    }
1091
                } else {
1092
                    if (IS_CHROME || IS_SAFARI) {
×
1093
                        // COMPAT: Chrome and Safari support `beforeinput` event but do not fire
1094
                        // an event when deleting backwards in a selected void inline node
1095
                        if (
×
1096
                            selection &&
×
1097
                            (Hotkeys.isDeleteBackward(nativeEvent) ||
1098
                                Hotkeys.isDeleteForward(nativeEvent)) &&
1099
                            Range.isCollapsed(selection)
1100
                        ) {
1101
                            const currentNode = Node.parent(
×
1102
                                editor,
1103
                                selection.anchor.path
1104
                            )
1105
                            if (
×
1106
                                Element.isElement(currentNode) &&
×
1107
                                Editor.isVoid(editor, currentNode) &&
1108
                                Editor.isInline(editor, currentNode)
1109
                            ) {
1110
                                event.preventDefault()
×
1111
                                Editor.deleteBackward(editor, { unit: 'block' })
×
1112
                                return
×
1113
                            }
1114
                        }
1115
                    }
1116
                }
1117
            } catch (error) {
1118
                this.editor.onError({ code: SlateErrorCode.OnDOMKeydownError, nativeError: error });
×
1119
            }
1120
        }
1121
    }
1122

1123
    private onDOMPaste(event: ClipboardEvent) {
1124
        // COMPAT: Certain browsers don't support the `beforeinput` event, so we
1125
        // fall back to React's `onPaste` here instead.
1126
        // COMPAT: Firefox, Chrome and Safari are not emitting `beforeinput` events
1127
        // when "paste without formatting" option is used.
1128
        // This unfortunately needs to be handled with paste events instead.
1129
        if (
×
1130
            !this.isDOMEventHandled(event, this.paste) &&
×
1131
            (!HAS_BEFORE_INPUT_SUPPORT || isPlainTextOnlyPaste(event) || forceOnDOMPaste) &&
1132
            !this.readonly &&
1133
            hasEditableTarget(this.editor, event.target)
1134
        ) {
1135
            event.preventDefault();
×
1136
            AngularEditor.insertData(this.editor, event.clipboardData);
×
1137
        }
1138
    }
1139

1140
    private onFallbackBeforeInput(event: BeforeInputEvent) {
1141
        // COMPAT: Certain browsers don't support the `beforeinput` event, so we
1142
        // fall back to React's leaky polyfill instead just for it. It
1143
        // only works for the `insertText` input type.
1144
        if (
×
1145
            !HAS_BEFORE_INPUT_SUPPORT &&
×
1146
            !this.readonly &&
1147
            !this.isDOMEventHandled(event.nativeEvent, this.beforeInput) &&
1148
            hasEditableTarget(this.editor, event.nativeEvent.target)
1149
        ) {
1150
            event.nativeEvent.preventDefault();
×
1151
            try {
×
1152
                const text = event.data;
×
1153
                if (!Range.isCollapsed(this.editor.selection)) {
×
1154
                    Editor.deleteFragment(this.editor);
×
1155
                }
1156
                // just handle Non-IME input
1157
                if (!this.isComposing) {
×
1158
                    Editor.insertText(this.editor, text);
×
1159
                }
1160
            } catch (error) {
1161
                this.editor.onError({ code: SlateErrorCode.ToNativeSelectionError, nativeError: error });
×
1162
            }
1163
        }
1164
    }
1165

1166
    private isDOMEventHandled(event: Event, handler?: (event: Event) => void) {
1167
        if (!handler) {
2✔
1168
            return false;
2✔
1169
        }
1170
        handler(event);
×
1171
        return event.defaultPrevented;
×
1172
    }
1173
    //#endregion
1174

1175
    ngOnDestroy() {
1176
        NODE_TO_ELEMENT.delete(this.editor);
14✔
1177
        this.manualListeners.forEach(manualListener => {
14✔
1178
            manualListener();
280✔
1179
        });
1180
        this.destroy$.complete();
14✔
1181
        EDITOR_TO_ON_CHANGE.delete(this.editor);
14✔
1182
    }
1183
}
1184

1185
/**
1186
 * Check if the target is editable and in the editor.
1187
 */
1188

1189
const hasEditableTarget = (editor: AngularEditor, target: EventTarget | null): target is DOMNode => {
1✔
1190
    return isDOMNode(target) && AngularEditor.hasDOMNode(editor, target, { editable: true });
2✔
1191
};
1192

1193
/**
1194
 * Check if two DOM range objects are equal.
1195
 */
1196
const isRangeEqual = (a: DOMRange, b: DOMRange) => {
1✔
1197
    return (
×
1198
        (a.startContainer === b.startContainer &&
×
1199
            a.startOffset === b.startOffset &&
1200
            a.endContainer === b.endContainer &&
1201
            a.endOffset === b.endOffset) ||
1202
        (a.startContainer === b.endContainer &&
1203
            a.startOffset === b.endOffset &&
1204
            a.endContainer === b.startContainer &&
1205
            a.endOffset === b.startOffset)
1206
    );
1207
};
1208

1209
/**
1210
 * Check if the target is in the editor.
1211
 */
1212

1213
const hasTarget = (editor: AngularEditor, target: EventTarget | null): target is DOMNode => {
1✔
1214
    return isDOMNode(target) && AngularEditor.hasDOMNode(editor, target);
×
1215
};
1216

1217
/**
1218
 * Check if the target is inside void and in the editor.
1219
 */
1220

1221
const isTargetInsideVoid = (editor: AngularEditor, target: EventTarget | null): boolean => {
1✔
1222
    const slateNode = hasTarget(editor, target) && AngularEditor.toSlateNode(editor, target);
×
1223
    return Editor.isVoid(editor, slateNode);
×
1224
};
1225

1226
const hasStringTarget = (domSelection: DOMSelection) => {
1✔
1227
    return (domSelection.anchorNode.parentElement.hasAttribute('data-slate-string') || domSelection.anchorNode.parentElement.hasAttribute('data-slate-zero-width')) &&
×
1228
        (domSelection.focusNode.parentElement.hasAttribute('data-slate-string') || domSelection.focusNode.parentElement.hasAttribute('data-slate-zero-width'));
1229
}
1230

1231
/**
1232
 * remove default insert from composition
1233
 * @param text
1234
 */
1235
const preventInsertFromComposition = (event: Event, editor: AngularEditor) => {
1✔
1236
    const types = ['compositionend', 'insertFromComposition'];
×
1237
    if (!types.includes(event.type)) {
×
1238
        return;
×
1239
    }
1240
    const insertText = (event as CompositionEvent).data;
×
1241
    const window = AngularEditor.getWindow(editor);
×
1242
    const domSelection = window.getSelection();
×
1243
    // ensure text node insert composition input text
1244
    if (insertText && domSelection.anchorNode instanceof Text && domSelection.anchorNode.textContent.endsWith(insertText)) {
×
1245
        const textNode = domSelection.anchorNode;
×
1246
        textNode.splitText(textNode.length - insertText.length).remove();
×
1247
    }
1248
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc