export function wordAt(text: string, offset: number) { for (const match of text.matchAll(/[\p{L}\p{M}]+(?:['’][\p{L}\p{M}]+)*/gu)) { if (match.index <= offset && match.index + match[0].length >= offset) { return { word: match[0], from: match.index, to: match.index + match[0].length }; } } return null; } type WordTarget = { word: string; replace(value: string): void; valid(): boolean }; export function spellingWord(target: HTMLElement, x: number, y: number): WordTarget | null { const control = target instanceof HTMLTextAreaElement || target instanceof HTMLInputElement; if (control) { const original = target.value; let offset = target.selectionStart ?? 0; // A transparent mirror resolves the pointer position in wrapped and scrolled inputs. if (x || y) { const box = target.getBoundingClientRect(), css = getComputedStyle(target); const mirror = document.createElement("div"); for (const key of Array.from(css)) mirror.style.setProperty(key, css.getPropertyValue(key)); Object.assign(mirror.style, { position: "fixed", left: `${box.left}px`, top: `${box.top}px`, width: `${box.width}px`, height: `${box.height}px`, margin: "0", opacity: "0", zIndex: "2147483647", pointerEvents: "auto", overflow: "hidden", whiteSpace: target instanceof HTMLInputElement ? "pre" : "pre-wrap", overflowWrap: "break-word" }); mirror.textContent = original || " "; (target.closest("dialog[open]") ?? document.body).append(mirror); mirror.scrollTop = target.scrollTop; mirror.scrollLeft = target.scrollLeft; const hit = document.caretRangeFromPoint(x, y); if (hit?.startContainer === mirror.firstChild) offset = hit.startOffset; mirror.remove(); } const found = wordAt(original, offset); if (!found) return null; const valid = () => target.isConnected && target.value === original && !target.disabled && !target.readOnly; return { word: found.word, valid, replace(value) { if (!valid()) return; target.focus({ preventScroll: true }); target.setSelectionRange(found.from, found.to); if (!document.execCommand("insertText", false, value)) { target.setRangeText(value, found.from, found.to, "end"); target.dispatchEvent(new InputEvent("input", { bubbles: true, inputType: "insertReplacementText", data: value })); } } }; } const range = x || y ? document.caretRangeFromPoint(x, y) : document.getSelection()?.rangeCount ? document.getSelection()!.getRangeAt(0).cloneRange() : null; if (!range || range.startContainer.nodeType !== Node.TEXT_NODE || !target.contains(range.startContainer)) return null; const node = range.startContainer, original = node.textContent ?? ""; const found = wordAt(original, range.startOffset); if (!found) return null; range.setStart(node, found.from); range.setEnd(node, found.to); const valid = () => node.isConnected && target.isConnected && node.textContent === original; return { word: found.word, valid, replace(value) { if (!valid()) return; target.focus({ preventScroll: true }); document.getSelection()?.removeAllRanges(); document.getSelection()?.addRange(range); document.execCommand("insertText", false, value); } }; }