From 8ff3f08d8cd76feb22d7f786356ec0f7e9e93934 Mon Sep 17 00:00:00 2001 From: Gigi Date: Mon, 20 Oct 2025 08:57:00 +0200 Subject: [PATCH] fix(highlights): restore FAB selection updates by listening to document selectionchange; keep clearing selection after creation --- src/hooks/useHighlightInteractions.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/hooks/useHighlightInteractions.ts b/src/hooks/useHighlightInteractions.ts index 024db59a..b0be1bc7 100644 --- a/src/hooks/useHighlightInteractions.ts +++ b/src/hooks/useHighlightInteractions.ts @@ -113,6 +113,32 @@ export const useHighlightInteractions = ({ }, 10) }, [onTextSelection, onClearSelection]) + // Listen to document-level selection changes to catch keyboard/touch/ios cases + useEffect(() => { + const handler = () => { + // Defer to allow DOM selection to settle + setTimeout(() => { + const selection = window.getSelection() + if (!selection || selection.rangeCount === 0) { + onClearSelection?.() + return + } + + const range = selection.getRangeAt(0) + const text = selection.toString().trim() + + if (text.length > 0 && contentRef.current?.contains(range.commonAncestorContainer)) { + onTextSelection?.(text) + } else { + onClearSelection?.() + } + }, 10) + } + + document.addEventListener('selectionchange', handler) + return () => document.removeEventListener('selectionchange', handler) + }, [onTextSelection, onClearSelection]) + return { contentRef, handleSelectionEnd } }