fix(editor): range IndexSizeError on firefox (#10519)

This is a Firefox compat issue, I've manually confirmed this implementation works in latest Firefox build.
This commit is contained in:
doodlewind
2025-02-28 11:50:56 +00:00
parent 82dffcbf67
commit 12acf7e4a0

View File

@@ -28,7 +28,16 @@ export function caretRangeFromPoint(
}
// TODO handle caret is covered by popup
const range = document.createRange();
range.setStart(caret.offsetNode, caret.offset);
let offset = caret.offset;
if (caret.offsetNode.nodeType === Node.TEXT_NODE) {
const textNode = caret.offsetNode as Text;
offset = Math.max(0, Math.min(offset, textNode.length));
} else if (caret.offsetNode.nodeType === Node.ELEMENT_NODE) {
const elementNode = caret.offsetNode as Element;
offset = Math.max(0, Math.min(offset, elementNode.childNodes.length));
}
range.setStart(caret.offsetNode, offset);
return range;
}