diff --git a/libs/components/common/src/lib/text/slate-utils.ts b/libs/components/common/src/lib/text/slate-utils.ts index 1783d79cab..ec7e219810 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,51 @@ 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); + } + // 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; } public getSplitContentsBySelection() {