diff --git a/src/app/features/room/Room.tsx b/src/app/features/room/Room.tsx index fa66e8c8..6c5b046c 100644 --- a/src/app/features/room/Room.tsx +++ b/src/app/features/room/Room.tsx @@ -1,4 +1,4 @@ -import React, { useCallback } from 'react'; +import React, { useCallback, useRef } from 'react'; import { Box, Line } from 'folds'; import { useParams } from 'react-router-dom'; import { isKeyHotkey } from 'is-hotkey'; @@ -37,6 +37,7 @@ export function Room() { const activeThreadId = useObservableValueOptionally(timeline?.activeThread); const threads = useObservableValueOptionally(timeline?.threads); const threadTimeline = activeThreadId ? threads?.get(activeThreadId) : undefined; + const threadFocusRef = useRef(false); const [isDrawer] = useSetting(settingsAtom, 'isPeopleDrawer'); const [hideActivity] = useSetting(settingsAtom, 'hideActivity'); @@ -71,7 +72,12 @@ export function Room() { - + @@ -81,6 +87,7 @@ export function Room() { timeline.closeThread()} /> diff --git a/src/app/features/room/RoomTimeline.tsx b/src/app/features/room/RoomTimeline.tsx index 09acf673..237c9021 100644 --- a/src/app/features/room/RoomTimeline.tsx +++ b/src/app/features/room/RoomTimeline.tsx @@ -498,7 +498,6 @@ export function RoomTimeline({ eventId, roomInputRef, editor, timeline }: RoomTi }; const readReceipt = useUserReadReceipt(); - console.log(readReceipt, ct.roomToLatestEvent.read.get(roomId)); const { getDayDivider, getNewDivider, updateDivider, clearDayDivider, clearNewDivider } = useEventDivider(readReceipt?.ts); const { getCollapsed, updateCollapse } = useEventCollapse(); diff --git a/src/app/features/room/RoomView.tsx b/src/app/features/room/RoomView.tsx index 74e58816..b688b6c3 100644 --- a/src/app/features/room/RoomView.tsx +++ b/src/app/features/room/RoomView.tsx @@ -1,7 +1,5 @@ -import React, { useCallback, useRef } from 'react'; +import React, { MutableRefObject, useRef } from 'react'; import { Box, Text, config } from 'folds'; -import { ReactEditor } from 'slate-react'; -import { isKeyHotkey } from 'is-hotkey'; import { usePowerLevelsContext } from '../../hooks/usePowerLevels'; import { useEditor } from '../../components/editor'; import { RoomInputPlaceholder } from './RoomInputPlaceholder'; @@ -10,8 +8,6 @@ import { RoomTombstone } from './RoomTombstone'; import { RoomInput } from './RoomInput'; import { RoomViewFollowing, RoomViewFollowingPlaceholder } from './RoomViewFollowing'; import { Page } from '../../components/page'; -import { useKeyDown } from '../../hooks/useKeyDown'; -import { editableActiveElement } from '../../utils/dom'; import { settingsAtom } from '../../state/settings'; import { useSetting } from '../../state/hooks/settings'; import { useRoomPermissions } from '../../hooks/useRoomPermissions'; @@ -21,39 +17,14 @@ import { useObservableValue } from '../../hooks/useObservableValue'; import { useClient } from '../../hooks/useClient'; import { MessageEvent } from '../../../types/matrix/room'; import { Timeline } from '../../../client/room'; +import { useEditorAutoFocus } from './hooks/useEditorAutoFocus'; -const FN_KEYS_REGEX = /^F\d+$/; -const shouldFocusMessageField = (evt: KeyboardEvent): boolean => { - const { code } = evt; - if (evt.metaKey || evt.altKey || evt.ctrlKey) { - return false; - } - - if (FN_KEYS_REGEX.test(code)) return false; - - if ( - code.startsWith('OS') || - code.startsWith('Meta') || - code.startsWith('Shift') || - code.startsWith('Alt') || - code.startsWith('Control') || - code.startsWith('Arrow') || - code.startsWith('Page') || - code.startsWith('End') || - code.startsWith('Home') || - code === 'Tab' || - code === 'Space' || - code === 'Enter' || - code === 'NumLock' || - code === 'ScrollLock' - ) { - return false; - } - - return true; +type RoomViewProps = { + eventId?: string; + threadFocusRef: MutableRefObject; + timeline: Timeline; }; - -export function RoomView({ eventId, timeline }: { eventId?: string; timeline: Timeline }) { +export function RoomView({ eventId, timeline, threadFocusRef }: RoomViewProps) { const roomInputRef = useRef(null); const roomViewRef = useRef(null); @@ -72,22 +43,7 @@ export function RoomView({ eventId, timeline }: { eventId?: string; timeline: Ti const permissions = useRoomPermissions(creators, powerLevels); const canMessage = permissions.event(MessageEvent.RoomMessage, ct.session.userId); - useKeyDown( - window, - useCallback( - (evt) => { - if (editableActiveElement()) return; - const portalContainer = document.getElementById('portalContainer'); - if (portalContainer && portalContainer.children.length > 0) { - return; - } - if (shouldFocusMessageField(evt) || isKeyHotkey('mod+v', evt)) { - ReactEditor.focus(editor); - } - }, - [editor] - ) - ); + useEditorAutoFocus(editor, threadFocusRef, false); return ( diff --git a/src/app/features/room/hooks/useEditorAutoFocus.ts b/src/app/features/room/hooks/useEditorAutoFocus.ts new file mode 100644 index 00000000..312f0d32 --- /dev/null +++ b/src/app/features/room/hooks/useEditorAutoFocus.ts @@ -0,0 +1,100 @@ +import { MutableRefObject, RefObject, useCallback, useEffect } from 'react'; +import { isKeyHotkey } from 'is-hotkey'; +import { ReactEditor } from 'slate-react'; +import { Editor } from 'slate'; +import { useKeyDown } from '../../../hooks/useKeyDown'; +import { editableActiveElement } from '../../../utils/dom'; + +const FN_KEYS_REGEX = /^F\d+$/; + +const shouldFocusMessageField = (evt: KeyboardEvent): boolean => { + const { code } = evt; + + if (evt.metaKey || evt.altKey || evt.ctrlKey) { + return false; + } + + if (FN_KEYS_REGEX.test(code)) return false; + + if ( + code.startsWith('OS') || + code.startsWith('Meta') || + code.startsWith('Shift') || + code.startsWith('Alt') || + code.startsWith('Control') || + code.startsWith('Arrow') || + code.startsWith('Page') || + code.startsWith('End') || + code.startsWith('Home') || + code === 'Tab' || + code === 'Space' || + code === 'Enter' || + code === 'NumLock' || + code === 'ScrollLock' + ) { + return false; + } + + return true; +}; + +export const useEditorAutoFocus = ( + editor: Editor, + threadFocusRef: RefObject, + thread: boolean +) => { + useKeyDown( + window, + useCallback( + (evt) => { + if (editableActiveElement()) return; + + const portalContainer = document.getElementById('portalContainer'); + if (portalContainer && portalContainer.children.length > 0) { + return; + } + + if (!shouldFocusMessageField(evt) && !isKeyHotkey('mod+v', evt)) { + return; + } + + if (threadFocusRef.current && !thread) { + return; + } + + ReactEditor.focus(editor); + }, + [editor, threadFocusRef, thread] + ) + ); +}; + +export const useThreadFocus = ( + threadFocusRef: MutableRefObject, + viewElementRef: RefObject +) => { + useEffect(() => { + const handlePointerDown = (evt: PointerEvent) => { + const viewElement = viewElementRef.current; + if (!viewElement) return; + + const lastTarget = evt.target; + + const clickedInsideView = lastTarget instanceof Node && viewElement.contains(lastTarget); + // eslint-disable-next-line no-param-reassign + threadFocusRef.current = clickedInsideView; + }; + + // eslint-disable-next-line no-param-reassign + threadFocusRef.current = true; + window.addEventListener('pointerdown', handlePointerDown); + + return () => { + // eslint-disable-next-line no-param-reassign + threadFocusRef.current = true; + window.removeEventListener('pointerdown', handlePointerDown); + }; + }, [threadFocusRef, viewElementRef]); + + return threadFocusRef; +}; diff --git a/src/app/features/room/thread-view/ThreadView.tsx b/src/app/features/room/thread-view/ThreadView.tsx index 3f896ea7..dc4969ef 100644 --- a/src/app/features/room/thread-view/ThreadView.tsx +++ b/src/app/features/room/thread-view/ThreadView.tsx @@ -1,4 +1,4 @@ -import React, { useRef } from 'react'; +import React, { MutableRefObject, useRef } from 'react'; import classNames from 'classnames'; import { Box, config, Icon, IconButton, Icons, Text, Tooltip, TooltipProvider } from 'folds'; import FocusTrap from 'focus-trap-react'; @@ -10,19 +10,24 @@ import { useEditor } from '../../../components/editor'; import { RoomInput } from '../RoomInput'; import { RoomViewFollowingPlaceholder } from '../RoomViewFollowing'; import { TimelineView } from './TimelineView'; +import { useEditorAutoFocus, useThreadFocus } from '../hooks/useEditorAutoFocus'; type ThreadViewProps = { threadTimeline: ThreadTimeline; + threadFocusRef: MutableRefObject; requestClose: () => void; }; -export function ThreadView({ threadTimeline, requestClose }: ThreadViewProps) { +export function ThreadView({ threadTimeline, threadFocusRef, requestClose }: ThreadViewProps) { const screenSize = useScreenSizeContext(); const floating = screenSize !== ScreenSize.Desktop; const editor = useEditor(); const threadInputRef = useRef(null); const threadViewRef = useRef(null); + useThreadFocus(threadFocusRef, threadViewRef); + useEditorAutoFocus(editor, threadFocusRef, true); + return (