From de0c53b1b213b4510a8238392d61daecd0977243 Mon Sep 17 00:00:00 2001 From: lawvs <18554747+lawvs@users.noreply.github.com> Date: Mon, 15 Aug 2022 19:38:49 +0800 Subject: [PATCH 1/2] fix: filter empty string when getContentBetween --- .../common/src/lib/text/slate-utils.ts | 62 +++++++++++++++---- 1 file changed, 50 insertions(+), 12 deletions(-) diff --git a/libs/components/common/src/lib/text/slate-utils.ts b/libs/components/common/src/lib/text/slate-utils.ts index 1783d79cab..43a47ad85f 100644 --- a/libs/components/common/src/lib/text/slate-utils.ts +++ b/libs/components/common/src/lib/text/slate-utils.ts @@ -2,28 +2,28 @@ import { Descendant, Editor, + Location, + Node as SlateNode, + Path, Point, - Transforms, Range, Text, - Location, - Path, - Node as SlateNode, + Transforms, } from 'slate'; import { ReactEditor } from 'slate-react'; -import { - getCommentsIdsOnTextNode, - getEditorMarkForCommentId, - MARKDOWN_STYLE_MAP, - MatchRes, -} from './utils'; import { fontBgColorPalette, fontColorPalette, type TextAlignOptions, type TextStyleMark, } from './constants'; +import { + getCommentsIdsOnTextNode, + getEditorMarkForCommentId, + MARKDOWN_STYLE_MAP, + MatchRes, +} from './utils'; function isInlineAndVoid(editor: Editor, el: any) { return editor.isInline(el) && editor.isVoid(el); @@ -567,8 +567,46 @@ class SlateUtils { anchor: point1, focus: point2, }); - // @ts-ignore - return fragment[0].children; + if (!fragment.length) { + console.error('Debug information:', point1, point2, fragment); + throw new Error('Failed to get content between!'); + } + + if (fragment.length > 1) { + console.warn( + 'Fragment length is greater than one, ' + + 'please be careful if there is missing content!\n' + + 'Debug information:', + point1, + point2, + fragment + ); + } + + const firstFragment = fragment[0]; + if (!('type' in firstFragment)) { + console.error('Debug information:', point1, point2, fragment); + throw new Error( + 'Failed to get content between! type of firstFragment not found!' + ); + } + + const fragmentChildren = firstFragment.children; + + const textChildren: Text[] = []; + for (const child of fragmentChildren) { + if (!('text' in child)) { + console.error('Debug information:', point1, point2, fragment); + throw new Error('Fragment exists nested!'); + } + // Filter empty string + if (child.text === '') { + continue; + } + textChildren.push(child); + } + + return textChildren; } public getSplitContentsBySelection() { From 64952806b91826bcc6e18eaafb7c43ee464e74b4 Mon Sep 17 00:00:00 2001 From: lawvs <18554747+lawvs@users.noreply.github.com> Date: Tue, 16 Aug 2022 13:21:46 +0800 Subject: [PATCH 2/2] fix: slate cannot get the start point in the node at path [0] --- libs/components/common/src/lib/text/slate-utils.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/libs/components/common/src/lib/text/slate-utils.ts b/libs/components/common/src/lib/text/slate-utils.ts index 43a47ad85f..ec7e219810 100644 --- a/libs/components/common/src/lib/text/slate-utils.ts +++ b/libs/components/common/src/lib/text/slate-utils.ts @@ -605,6 +605,11 @@ class SlateUtils { } textChildren.push(child); } + // If nothing, should preserve empty string + // Fix Slate Cannot get the start point in the node at path [0] because it has no start text node. + if (!textChildren.length) { + textChildren.push({ text: '' }); + } return textChildren; }