From fd70dc0b2c0d06edb9490874fb831161b9759cba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Stabell?= Date: Tue, 13 Apr 2021 08:14:30 -0700 Subject: [PATCH] Save some remove_text calls if the text is zero length (#4193) * Save some remove_text calls if the text is zero length. * Remove unnecessary !text check - text is always string, and add similar check to 'insert_text' as well * Create giant-adults-matter.md Co-authored-by: Ian Storm Taylor --- .changeset/giant-adults-matter.md | 5 +++++ packages/slate/src/transforms/general.ts | 2 ++ packages/slate/src/transforms/text.ts | 9 ++++++--- 3 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 .changeset/giant-adults-matter.md diff --git a/.changeset/giant-adults-matter.md b/.changeset/giant-adults-matter.md new file mode 100644 index 000000000..4d31449ef --- /dev/null +++ b/.changeset/giant-adults-matter.md @@ -0,0 +1,5 @@ +--- +slate: patch +--- + +Fixed insert and remove text operations to no-op without any text. diff --git a/packages/slate/src/transforms/general.ts b/packages/slate/src/transforms/general.ts index 49fd0757a..6ccbb9646 100644 --- a/packages/slate/src/transforms/general.ts +++ b/packages/slate/src/transforms/general.ts @@ -44,6 +44,7 @@ export const GeneralTransforms: GeneralTransforms = { case 'insert_text': { const { path, offset, text } = op + if (text.length === 0) break const node = Node.leaf(editor, path) const before = node.text.slice(0, offset) const after = node.text.slice(offset) @@ -167,6 +168,7 @@ export const GeneralTransforms: GeneralTransforms = { case 'remove_text': { const { path, offset, text } = op + if (text.length === 0) break const node = Node.leaf(editor, path) const before = node.text.slice(0, offset) const after = node.text.slice(offset + text.length) diff --git a/packages/slate/src/transforms/text.ts b/packages/slate/src/transforms/text.ts index 1d982b7ec..c58589a79 100644 --- a/packages/slate/src/transforms/text.ts +++ b/packages/slate/src/transforms/text.ts @@ -182,7 +182,8 @@ export const TextTransforms: TextTransforms = { const { path } = point const { offset } = start const text = node.text.slice(offset) - editor.apply({ type: 'remove_text', path, offset, text }) + if (text.length > 0) + editor.apply({ type: 'remove_text', path, offset, text }) } for (const pathRef of pathRefs) { @@ -196,7 +197,8 @@ export const TextTransforms: TextTransforms = { const { path } = point const offset = isSingleText ? start.offset : 0 const text = node.text.slice(offset, end.offset) - editor.apply({ type: 'remove_text', path, offset, text }) + if (text.length > 0) + editor.apply({ type: 'remove_text', path, offset, text }) } if ( @@ -483,7 +485,8 @@ export const TextTransforms: TextTransforms = { } const { path, offset } = at - editor.apply({ type: 'insert_text', path, offset, text }) + if (text.length > 0) + editor.apply({ type: 'insert_text', path, offset, text }) }) }, }