Skip to content

Commit 7d5b051

Browse files
committed
fix: respect suppressThrow in toSlatePoint findPath calls
When toSlatePoint is called with suppressThrow: true (e.g., from toSlateRange during selection change handling), internal findPath calls could still throw "Unable to find the path for Slate node" errors during component unmount when node references become stale. Wrap both findPath calls in try-catch blocks that return null when suppressThrow is enabled, matching the existing error handling pattern in the rest of the function. Based on upstream slate-dom PR #6004.
1 parent 2b7b231 commit 7d5b051

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@portabletext/editor': patch
3+
---
4+
5+
fix: respect suppressThrow in toSlatePoint findPath calls

packages/editor/src/slate-dom/plugin/dom-editor.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -879,10 +879,16 @@ export const DOMEditor: DOMEditorInterface = {
879879

880880
if (node && DOMEditor.hasDOMNode(editor, node, {editable: true})) {
881881
const slateNode = DOMEditor.toSlateNode(editor, node)
882-
let {path, offset} = Editor.start(
883-
editor,
884-
DOMEditor.findPath(editor, slateNode),
885-
)
882+
let nodePath: Path
883+
try {
884+
nodePath = DOMEditor.findPath(editor, slateNode)
885+
} catch (e) {
886+
if (suppressThrow) {
887+
return null as T extends true ? Point | null : Point
888+
}
889+
throw e
890+
}
891+
let {path, offset} = Editor.start(editor, nodePath)
886892

887893
if (!node.querySelector('[data-slate-leaf]')) {
888894
offset = nearestOffset
@@ -905,7 +911,15 @@ export const DOMEditor: DOMEditorInterface = {
905911
// the select event fires twice, once for the old editor's `element`
906912
// first, and then afterwards for the correct `element`. (2017/03/03)
907913
const slateNode = DOMEditor.toSlateNode(editor, textNode!)
908-
const path = DOMEditor.findPath(editor, slateNode)
914+
let path: Path
915+
try {
916+
path = DOMEditor.findPath(editor, slateNode)
917+
} catch (e) {
918+
if (suppressThrow) {
919+
return null as T extends true ? Point | null : Point
920+
}
921+
throw e
922+
}
909923
return {path, offset} as T extends true ? Point | null : Point
910924
},
911925

0 commit comments

Comments
 (0)