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

worktile / slate-angular / 8d02bc48-1bbe-4c21-81df-a4a7d048c5c1

20 Nov 2023 07:33AM UTC coverage: 48.5% (+2.4%) from 46.148%
8d02bc48-1bbe-4c21-81df-a4a7d048c5c1

push

circleci

pubuzhixing8
chore: enter prerelease mode

378 of 964 branches covered (0.0%)

Branch coverage included in aggregate %.

948 of 1770 relevant lines covered (53.56%)

39.79 hits per line

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

91.67
/packages/src/components/string/string.component.ts
1
import { Component, OnInit, Input, ChangeDetectionStrategy, OnChanges, ElementRef, ViewContainerRef, AfterViewInit } from '@angular/core';
2
import { Node, Text } from 'slate';
3
import { ViewContainerItem } from '../../view/container-item';
4
import { SlateLeafContext, SlateStringContext } from '../../view/context';
5
import { SlateDefaultString } from './default-string.component';
6

7
@Component({
8
    selector: 'span[slateString]',
9
    template: '',
10
    changeDetection: ChangeDetectionStrategy.OnPush,
11
    standalone: true
12
})
13
export class SlateString extends ViewContainerItem<SlateStringContext> implements OnInit, OnChanges, AfterViewInit {
1✔
14
    @Input() context: SlateLeafContext;
15

16
    constructor(private elementRef: ElementRef<any>, protected viewContainerRef: ViewContainerRef) {
143✔
17
        super(viewContainerRef);
143✔
18
    }
19

20
    ngOnInit(): void {
21
        this.createView();
143✔
22
    }
23

24
    ngOnChanges() {
25
        if (!this.initialized) {
149✔
26
            return;
143✔
27
        }
28
        this.updateView();
6✔
29
    }
30

31
    ngAfterViewInit() {
32
        this.elementRef.nativeElement.remove();
143✔
33
    }
34

35
    // COMPAT: If this is the last text node in an empty block, render a zero-
36
    // width space that will convert into a line break when copying and pasting
37
    // to support expected plain text.
38
    isLineBreakEmptyString() {
39
        return (
297✔
40
            this.context.leaf.text === '' &&
360✔
41
            this.context.parent.children[this.context.parent.children.length - 1] === this.context.text &&
42
            !this.viewContext.editor.isInline(this.context.parent) &&
43
            // [list-render] performance optimization: reduce the number of calls to the `Editor.string(editor, path)` method
44
            isEmpty(this.viewContext.editor, this.context.parent)
45
        );
46
    }
47

48
    // COMPAT: If the text is empty, it's because it's on the edge of an inline
49
    // node, so we render a zero-width space so that the selection can be
50
    // inserted next to it still.
51
    isEmptyText() {
52
        return this.context.leaf.text === '';
138✔
53
    }
54

55
    // COMPAT: Browsers will collapse trailing new lines at the end of blocks,
56
    // so we need to add an extra trailing new lines to prevent that.
57
    isCompatibleString() {
58
        return this.context.isLast && this.context.leaf.text.slice(-1) === '\n';
138✔
59
    }
60

61
    // COMPAT: Render text inside void nodes with a zero-width space.
62
    // So the node can contain selection but the text is not visible.
63
    isVoid() {
64
        return this.viewContext.editor.isVoid(this.context.parent);
149✔
65
    }
66

67
    getViewType() {
68
        if (this.isVoid()) {
149✔
69
            return this.viewContext.templateComponent.voidStringTemplate;
1✔
70
        }
71

72
        if (this.isLineBreakEmptyString()) {
148✔
73
            return SlateDefaultString;
10✔
74
        }
75

76
        if (this.isEmptyText()) {
138!
77
            return this.viewContext.templateComponent.emptyTextTemplate;
×
78
        }
79

80
        if (this.isCompatibleString()) {
138!
81
            return this.viewContext.templateComponent.compatibleStringTemplate;
×
82
        }
83

84
        return SlateDefaultString;
138✔
85
    }
86

87
    getType(): SlateStringContext['type'] {
88
        if (this.isLineBreakEmptyString()) {
149✔
89
            return 'lineBreakEmptyString';
10✔
90
        }
91
        return 'string';
139✔
92
    }
93

94
    getContext(): SlateStringContext {
95
        const stringType = this.getType();
149✔
96
        return {
149✔
97
            text: this.context.leaf.text,
98
            elementStringLength: Node.string(this.context.parent).length,
99
            type: stringType
100
        };
101
    }
102

103
    memoizedContext(prev: SlateStringContext, next: SlateStringContext): boolean {
104
        return false;
6✔
105
    }
106
}
107

108
/**
109
 * TODO: remove when bump slate
110
 * copy from slate
111
 * @param editor
112
 * @param element
113
 * @returns
114
 */
115
export const isEmpty = (editor, element) => {
1✔
116
    const { children } = element;
21✔
117
    const [first] = children;
21✔
118
    return children.length === 0 || (children.length === 1 && Text.isText(first) && first.text === '' && !editor.isVoid(element));
21✔
119
};
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