From 332e2a38c9b8108c706559bbeccd3c724c5dab99 Mon Sep 17 00:00:00 2001 From: Domagoj Kriskovic Date: Mon, 21 Oct 2024 10:57:45 +0200 Subject: [PATCH] Fix review panel entry focus handler (#21075) * Fix review panel entry focus handler * dont check position * remove event.target checks * Revert "remove event.target checks" This reverts commit 3f511e47b6922260666c6952b692f6f0a29d5912. GitOrigin-RevId: 9271df9cdb36a040d59a97c88f21d6e923ac0404 --- .../components/review-panel-entry.tsx | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/services/web/frontend/js/features/review-panel-new/components/review-panel-entry.tsx b/services/web/frontend/js/features/review-panel-new/components/review-panel-entry.tsx index c4196eca33..69f485380e 100644 --- a/services/web/frontend/js/features/review-panel-new/components/review-panel-entry.tsx +++ b/services/web/frontend/js/features/review-panel-new/components/review-panel-entry.tsx @@ -12,6 +12,8 @@ import { } from '@/features/source-editor/extensions/ranges' import { useEditorManagerContext } from '@/features/ide-react/context/editor-manager-context' import { useLayoutContext } from '@/shared/context/layout-context' +import { EditorSelection } from '@codemirror/state' +import { EditorView } from '@codemirror/view' export const ReviewPanelEntry: FC<{ position: number @@ -35,7 +37,7 @@ export const ReviewPanelEntry: FC<{ }) => { const state = useCodeMirrorStateContext() const view = useCodeMirrorViewContext() - const { openDocId } = useEditorManagerContext() + const { openDocId, getCurrentDocId } = useEditorManagerContext() const [focused, setFocused] = useState(false) const { setReviewPanelOpen } = useLayoutContext() @@ -50,20 +52,34 @@ export const ReviewPanelEntry: FC<{ if ( event.target instanceof HTMLButtonElement || event.target instanceof HTMLLinkElement || - event.target instanceof HTMLTextAreaElement || event.target instanceof HTMLAnchorElement ) { - // Don't focus if the click was on a button/link/textarea/anchor as we - // don't want to affect the behaviour of the button/link/textarea/anchor + // Don't focus if the click was on a button/link/anchor as we + // don't want to affect its behaviour return } - if (selectLineOnFocus) { - openDocId(docId, { gotoOffset: position, keepCurrentView: true }) - } setFocused(true) + + if (!selectLineOnFocus) { + return + } + + if (getCurrentDocId() !== docId) { + const focusIsOnTextarea = event.target instanceof HTMLTextAreaElement + if (focusIsOnTextarea === false) { + openDocId(docId, { gotoOffset: position, keepCurrentView: true }) + } + } else { + setTimeout(() => + view.dispatch({ + selection: EditorSelection.cursor(position), + effects: EditorView.scrollIntoView(position, { y: 'center' }), + }) + ) + } }, - [selectLineOnFocus, docId, openDocId, position] + [getCurrentDocId, docId, selectLineOnFocus, view, position, openDocId] ) // Clear op highlight on dismount