-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Desktop: Fix editor/viewer loses focus when visible panels are change…
…d with ctrl-l (#11029)
- Loading branch information
1 parent
c897cc1
commit bcb5218
Showing
10 changed files
with
110 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
.../app-desktop/gui/NoteEditor/NoteBody/CodeMirror/v6/utils/useRefocusOnVisiblePaneChange.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { RefObject, useRef, useEffect } from 'react'; | ||
import { focus } from '@joplin/lib/utils/focusHandler'; | ||
import CodeMirrorControl from '@joplin/editor/CodeMirror/CodeMirrorControl'; | ||
import NoteTextViewer from '../../../../../NoteTextViewer'; | ||
|
||
interface Props { | ||
editorRef: RefObject<CodeMirrorControl>; | ||
webviewRef: RefObject<NoteTextViewer>; | ||
visiblePanes: string[]; | ||
} | ||
|
||
const useRefocusOnVisiblePaneChange = ({ editorRef, webviewRef, visiblePanes }: Props) => { | ||
const lastVisiblePanes = useRef(visiblePanes); | ||
useEffect(() => { | ||
const editorHasFocus = editorRef.current?.cm6?.dom?.contains(document.activeElement); | ||
const viewerHasFocus = webviewRef.current?.hasFocus(); | ||
|
||
const lastHadViewer = lastVisiblePanes.current.includes('viewer'); | ||
const hasViewer = visiblePanes.includes('viewer'); | ||
const lastHadEditor = lastVisiblePanes.current.includes('editor'); | ||
const hasEditor = visiblePanes.includes('editor'); | ||
|
||
const viewerJustHidden = lastHadViewer && !hasViewer; | ||
if (viewerJustHidden && viewerHasFocus) { | ||
focus('CodeMirror/refocusEditor1', editorRef.current); | ||
} | ||
|
||
// Jump focus to the editor just after showing it -- this assumes that the user | ||
// shows the editor to start editing the note. | ||
const editorJustShown = !lastHadEditor && hasEditor; | ||
if (editorJustShown && viewerHasFocus) { | ||
focus('CodeMirror/refocusEditor2', editorRef.current); | ||
} | ||
|
||
const editorJustHidden = lastHadEditor && !hasEditor; | ||
if (editorJustHidden && editorHasFocus) { | ||
focus('CodeMirror/refocusViewer', webviewRef.current); | ||
} | ||
|
||
lastVisiblePanes.current = visiblePanes; | ||
}, [visiblePanes, editorRef, webviewRef]); | ||
}; | ||
|
||
export default useRefocusOnVisiblePaneChange; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters