From 0ef59a1a4f00aa3592df0193d7b459864716ee0b Mon Sep 17 00:00:00 2001 From: BitPhinix Date: Thu, 27 Jan 2022 17:33:41 +0100 Subject: [PATCH] fix: toSlatePoint suppressThrow leaf without text node --- .changeset/kind-olives-add.md | 5 ++ .../slate-react/src/plugin/react-editor.ts | 55 ++++++++++--------- 2 files changed, 34 insertions(+), 26 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 0000000000..c2c17b0da4 --- /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 027e981d51..83f2f2edaf 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) - - const contents = range.cloneContents() - const removals = [ - ...Array.prototype.slice.call( - contents.querySelectorAll('[data-slate-zero-width]') - ), - ...Array.prototype.slice.call( - contents.querySelectorAll('[contenteditable=false]') - ), - ] - - removals.forEach(el => { - el!.parentNode!.removeChild(el) - }) + textNode = leafNode.closest('[data-slate-node="text"]') + + if (textNode) { + const window = ReactEditor.getWindow(editor) + const range = window.document.createRange() + range.setStart(textNode, 0) + range.setEnd(nearestNode, nearestOffset) + + const contents = range.cloneContents() + const removals = [ + ...Array.prototype.slice.call( + contents.querySelectorAll('[data-slate-zero-width]') + ), + ...Array.prototype.slice.call( + contents.querySelectorAll('[contenteditable=false]') + ), + ] + + 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 + // 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.