fix: preserve native word and line deletion around file chips

This commit is contained in:
amosblomqvist
2026-10-28 19:07:25 +02:00
parent c4e934c9dc
commit 0b576d5973
3 changed files with 165 additions and 3 deletions
+12
View File
@@ -110,6 +110,18 @@ export function pathInputOffset(root: HTMLElement, node: Node, offset: number):
return total;
}
/** Validate a browser beforeinput StaticRange before projecting its exact endpoints. */
export function pathInputStaticRange(root: HTMLElement, range: Pick<StaticRange, "startContainer" | "startOffset" | "endContainer" | "endOffset">): TextSelection | null {
function valid(node: Node, offset: number) {
return (node === root || root.contains(node)) && Number.isInteger(offset) && offset >= 0
&& offset <= (node.nodeType === 3 ? (node as Text).length : node.childNodes.length);
}
if (!valid(range.startContainer, range.startOffset) || !valid(range.endContainer, range.endOffset)) return null;
const anchor = pathInputOffset(root, range.startContainer, range.startOffset);
const focus = pathInputOffset(root, range.endContainer, range.endOffset);
return anchor === null || focus === null || anchor >= focus ? null : { anchor, focus };
}
export function readPathSelection(root: HTMLElement): TextSelection | null {
const selection = root.ownerDocument.getSelection?.();
if (!selection?.anchorNode || !selection.focusNode) return null;