Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

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

doodlewind12acf7e4a020
diff --git a/blocksuite/affine/shared/src/utils/dom/point-to-range.ts b/blocksuite/affine/shared/src/utils/dom/point-to-range.ts
index 9d842f8159..d7e879c844 100644
--- a/blocksuite/affine/shared/src/utils/dom/point-to-range.ts
+++ b/blocksuite/affine/shared/src/utils/dom/point-to-range.ts
@@ -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;
}