From 2d1c6edbcc2bd3306d93aa0a5e8fa44d79eb4713 Mon Sep 17 00:00:00 2001 From: pubuzhixing8 Date: Tue, 6 Jun 2023 15:32:45 +0800 Subject: [PATCH] chore: optimize --- .../src/components/string/string.component.ts | 90 ++++++++----------- .../components/string/template.component.html | 2 +- .../components/string/template.component.ts | 4 +- packages/src/view/context.ts | 2 +- 4 files changed, 41 insertions(+), 57 deletions(-) diff --git a/packages/src/components/string/string.component.ts b/packages/src/components/string/string.component.ts index e5a4b6a3..1607747b 100644 --- a/packages/src/components/string/string.component.ts +++ b/packages/src/components/string/string.component.ts @@ -32,79 +32,63 @@ export class SlateStringComponent extends ViewContainerItem this.elementRef.nativeElement.remove(); } - getViewType() { + // COMPAT: If this is the last text node in an empty block, render a zero- + // width space that will convert into a line break when copying and pasting + // to support expected plain text. + isLineBreakEmptyString() { const path = AngularEditor.findPath(this.viewContext.editor, this.context.text); const parentPath = Path.parent(path); - - // COMPAT: Render text inside void nodes with a zero-width space. - // So the node can contain selection but the text is not visible. - if (this.viewContext.editor.isVoid(this.context.parent)) { - return this.viewContext.templateComponent.voidStringTemplate; - } - - // COMPAT: If this is the last text node in an empty block, render a zero- - // width space that will convert into a line break when copying and pasting - // to support expected plain text. - if ( + return ( this.context.leaf.text === '' && this.context.parent.children[this.context.parent.children.length - 1] === this.context.text && !this.viewContext.editor.isInline(this.context.parent) && Editor.string(this.viewContext.editor, parentPath) === '' - ) { + ); + } + + // COMPAT: If the text is empty, it's because it's on the edge of an inline + // node, so we render a zero-width space so that the selection can be + // inserted next to it still. + isEmptyText() { + return this.context.leaf.text === ''; + } + + // COMPAT: Browsers will collapse trailing new lines at the end of blocks, + // so we need to add an extra trailing new lines to prevent that. + isCompatibleString() { + return this.context.isLast && this.context.leaf.text.slice(-1) === '\n'; + } + + // COMPAT: Render text inside void nodes with a zero-width space. + // So the node can contain selection but the text is not visible. + isVoid() { + return this.viewContext.editor.isVoid(this.context.parent); + } + + getViewType() { + if (this.isVoid()) { + return this.viewContext.templateComponent.voidStringTemplate; + } + + if (this.isLineBreakEmptyString()) { return SlateDefaultStringComponent; } - // COMPAT: If the text is empty, it's because it's on the edge of an inline - // node, so we render a zero-width space so that the selection can be - // inserted next to it still. - if (this.context.leaf.text === '') { + if (this.isEmptyText()) { return this.viewContext.templateComponent.emptyTextTemplate; } - // COMPAT: Browsers will collapse trailing new lines at the end of blocks, - // so we need to add an extra trailing new lines to prevent that. - if (this.context.isLast && this.context.leaf.text.slice(-1) === '\n') { - return this.viewContext.templateComponent.compatStringTemplate; + if (this.isCompatibleString()) { + return this.viewContext.templateComponent.compatibleStringTemplate; } return SlateDefaultStringComponent; } getType(): SlateStringContext['type'] { - const path = AngularEditor.findPath(this.viewContext.editor, this.context.text); - const parentPath = Path.parent(path); - - // COMPAT: Render text inside void nodes with a zero-width space. - // So the node can contain selection but the text is not visible. - if (this.viewContext.editor.isVoid(this.context.parent)) { - return 'voidString'; - } - - // COMPAT: If this is the last text node in an empty block, render a zero- - // width space that will convert into a line break when copying and pasting - // to support expected plain text. - if ( - this.context.leaf.text === '' && - this.context.parent.children[this.context.parent.children.length - 1] === this.context.text && - !this.viewContext.editor.isInline(this.context.parent) && - Editor.string(this.viewContext.editor, parentPath) === '' - ) { + if (this.isLineBreakEmptyString()) { return 'lineBreakEmptyString'; } - - // COMPAT: If the text is empty, it's because it's on the edge of an inline - // node, so we render a zero-width space so that the selection can be - // inserted next to it still. - if (this.context.leaf.text === '') { - return 'emptyText'; - } - - // COMPAT: Browsers will collapse trailing new lines at the end of blocks, - // so we need to add an extra trailing new lines to prevent that. - if (this.context.isLast && this.context.leaf.text.slice(-1) === '\n') { - return 'compatString'; - } - return 'string'; } diff --git a/packages/src/components/string/template.component.html b/packages/src/components/string/template.component.html index e8931964..1a981ccc 100644 --- a/packages/src/components/string/template.component.html +++ b/packages/src/components/string/template.component.html @@ -1,4 +1,4 @@ - + {{ context.text }}{{ '\uFEFF' }}; + @ViewChild('compatibleStringTemplate', { read: TemplateRef, static: true }) + compatibleStringTemplate: TemplateRef; @ViewChild('voidStringTemplate', { read: TemplateRef, static: true }) voidStringTemplate: TemplateRef; diff --git a/packages/src/view/context.ts b/packages/src/view/context.ts index 1f24b157..b688e0d1 100644 --- a/packages/src/view/context.ts +++ b/packages/src/view/context.ts @@ -57,5 +57,5 @@ export interface SlateElementAttributes { export interface SlateStringContext { text: string; elementStringLength: number; - type: 'string' | 'lineBreakEmptyString' | 'emptyText' | 'voidString' | 'compatString'; + type: 'string' | 'lineBreakEmptyString'; }