Merge pull request #264 from toeverything/fix/filter-empty-from-selection

fix: filter empty string when getContentBetween
This commit is contained in:
DarkSky
2022-08-16 19:06:07 +08:00
committed by GitHub
@@ -2,28 +2,28 @@
import { import {
Descendant, Descendant,
Editor, Editor,
Location,
Node as SlateNode,
Path,
Point, Point,
Transforms,
Range, Range,
Text, Text,
Location, Transforms,
Path,
Node as SlateNode,
} from 'slate'; } from 'slate';
import { ReactEditor } from 'slate-react'; import { ReactEditor } from 'slate-react';
import {
getCommentsIdsOnTextNode,
getEditorMarkForCommentId,
MARKDOWN_STYLE_MAP,
MatchRes,
} from './utils';
import { import {
fontBgColorPalette, fontBgColorPalette,
fontColorPalette, fontColorPalette,
type TextAlignOptions, type TextAlignOptions,
type TextStyleMark, type TextStyleMark,
} from './constants'; } from './constants';
import {
getCommentsIdsOnTextNode,
getEditorMarkForCommentId,
MARKDOWN_STYLE_MAP,
MatchRes,
} from './utils';
function isInlineAndVoid(editor: Editor, el: any) { function isInlineAndVoid(editor: Editor, el: any) {
return editor.isInline(el) && editor.isVoid(el); return editor.isInline(el) && editor.isVoid(el);
@@ -567,8 +567,51 @@ class SlateUtils {
anchor: point1, anchor: point1,
focus: point2, focus: point2,
}); });
// @ts-ignore if (!fragment.length) {
return fragment[0].children; 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() { public getSplitContentsBySelection() {