diff --git a/src/components/views/rooms/wysiwyg_composer/components/Editor.tsx b/src/components/views/rooms/wysiwyg_composer/components/Editor.tsx index 4a2958cbc8..660681f913 100644 --- a/src/components/views/rooms/wysiwyg_composer/components/Editor.tsx +++ b/src/components/views/rooms/wysiwyg_composer/components/Editor.tsx @@ -34,8 +34,7 @@ export const Editor = memo( function Editor({ disabled, placeholder, leftComponent, rightComponent }: EditorProps, ref, ) { const isExpanded = useIsExpanded(ref as MutableRefObject, HEIGHT_BREAKING_POINT); - const { onFocus, onBlur, selectPreviousSelection } = - useSelection(ref as MutableRefObject); + const { onFocus, onBlur, selectPreviousSelection } = useSelection(); return
) { +export function useSelection() { const selectionRef = useRef({ + anchorNode: null, anchorOffset: 0, + focusNode: null, focusOffset: 0, }); const [isFocused, focusProps] = useFocus(); @@ -28,9 +30,10 @@ export function useSelection(ref: RefObject) { useEffect(() => { function onSelectionChange() { const selection = document.getSelection(); - console.log('selection', selection); selectionRef.current = { + anchorNode: selection.anchorNode, anchorOffset: selection.anchorOffset, + focusNode: selection.focusNode, focusOffset: selection.focusOffset, }; } @@ -44,11 +47,11 @@ export function useSelection(ref: RefObject) { const selectPreviousSelection = useCallback(() => { const range = new Range(); - range.setStart(ref.current.firstChild, selectionRef.current.anchorOffset); - range.setEnd(ref.current.firstChild, selectionRef.current.focusOffset); + range.setStart(selectionRef.current.anchorNode, selectionRef.current.anchorOffset); + range.setEnd(selectionRef.current.focusNode, selectionRef.current.focusOffset); document.getSelection().removeAllRanges(); document.getSelection().addRange(range); - }, [selectionRef, ref]); + }, [selectionRef]); return { ...focusProps, selectPreviousSelection }; }