Skip to content

Commit

Permalink
chore: optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
pubuzhixing8 committed Jun 6, 2023
1 parent f7ae6c4 commit 2d1c6ed
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 57 deletions.
90 changes: 37 additions & 53 deletions packages/src/components/string/string.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,79 +32,63 @@ export class SlateStringComponent extends ViewContainerItem<SlateStringContext>
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';
}

Expand Down
2 changes: 1 addition & 1 deletion packages/src/components/string/template.component.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<ng-template #compatStringTemplate let-context="context" let-viewContext="viewContext">
<ng-template #compatibleStringTemplate let-context="context" let-viewContext="viewContext">
<!-- Compatible with Chinese input in Chrome with \n -->
<span editable-text data-slate-string="true"
>{{ context.text }}<span data-slate-zero-width>{{ '\uFEFF' }}</span></span
Expand Down
4 changes: 2 additions & 2 deletions packages/src/components/string/template.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { Component, ChangeDetectionStrategy, ViewChild, TemplateRef } from '@ang
changeDetection: ChangeDetectionStrategy.OnPush
})
export class SlateStringTemplateComponent {
@ViewChild('compatStringTemplate', { read: TemplateRef, static: true })
compatStringTemplate: TemplateRef<any>;
@ViewChild('compatibleStringTemplate', { read: TemplateRef, static: true })
compatibleStringTemplate: TemplateRef<any>;

@ViewChild('voidStringTemplate', { read: TemplateRef, static: true })
voidStringTemplate: TemplateRef<any>;
Expand Down
2 changes: 1 addition & 1 deletion packages/src/view/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,5 @@ export interface SlateElementAttributes {
export interface SlateStringContext {
text: string;
elementStringLength: number;
type: 'string' | 'lineBreakEmptyString' | 'emptyText' | 'voidString' | 'compatString';
type: 'string' | 'lineBreakEmptyString';
}

0 comments on commit 2d1c6ed

Please sign in to comment.