From a5fd62ddd646553841a54616f7a5528e310bfd22 Mon Sep 17 00:00:00 2001 From: Eric Meier Date: Fri, 28 Jan 2022 02:09:05 +0100 Subject: [PATCH] fix: toSlatePoint suppressThrow leaf without text node (#4813) --- .changeset/kind-olives-add.md | 5 ++ .../slate-react/src/plugin/react-editor.ts | 51 ++++++++++--------- 2 files changed, 32 insertions(+), 24 deletions(-) create mode 100644 .changeset/kind-olives-add.md diff --git a/.changeset/kind-olives-add.md b/.changeset/kind-olives-add.md new file mode 100644 index 000000000..c2c17b0da --- /dev/null +++ b/.changeset/kind-olives-add.md @@ -0,0 +1,5 @@ +--- +'slate-react': patch +--- + +Don't throw in toSlatePoint while using supressThrow if leaf has no text node diff --git a/packages/slate-react/src/plugin/react-editor.ts b/packages/slate-react/src/plugin/react-editor.ts index 027e981d5..83f2f2eda 100644 --- a/packages/slate-react/src/plugin/react-editor.ts +++ b/packages/slate-react/src/plugin/react-editor.ts @@ -491,33 +491,36 @@ export const ReactEditor = { // Calculate how far into the text node the `nearestNode` is, so that we // can determine what the offset relative to the text node is. if (leafNode) { - textNode = leafNode.closest('[data-slate-node="text"]')! - const window = ReactEditor.getWindow(editor) - const range = window.document.createRange() - range.setStart(textNode, 0) - range.setEnd(nearestNode, nearestOffset) + textNode = leafNode.closest('[data-slate-node="text"]') - const contents = range.cloneContents() - const removals = [ - ...Array.prototype.slice.call( - contents.querySelectorAll('[data-slate-zero-width]') - ), - ...Array.prototype.slice.call( - contents.querySelectorAll('[contenteditable=false]') - ), - ] + if (textNode) { + const window = ReactEditor.getWindow(editor) + const range = window.document.createRange() + range.setStart(textNode, 0) + range.setEnd(nearestNode, nearestOffset) - removals.forEach(el => { - el!.parentNode!.removeChild(el) - }) + const contents = range.cloneContents() + const removals = [ + ...Array.prototype.slice.call( + contents.querySelectorAll('[data-slate-zero-width]') + ), + ...Array.prototype.slice.call( + contents.querySelectorAll('[contenteditable=false]') + ), + ] - // COMPAT: Edge has a bug where Range.prototype.toString() will - // convert \n into \r\n. The bug causes a loop when slate-react - // attempts to reposition its cursor to match the native position. Use - // textContent.length instead. - // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/10291116/ - offset = contents.textContent!.length - domNode = textNode + removals.forEach(el => { + el!.parentNode!.removeChild(el) + }) + + // COMPAT: Edge has a bug where Range.prototype.toString() will + // convert \n into \r\n. The bug causes a loop when slate-react + // attempts to reposition its cursor to match the native position. Use + // textContent.length instead. + // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/10291116/ + offset = contents.textContent!.length + domNode = textNode + } } else if (voidNode) { // For void nodes, the element with the offset key will be a cousin, not an // ancestor, so find it by going down from the nearest void parent.