-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Desktop: Fix editor/viewer loses focus when visible panels are changed with ctrl-l #11029
Merged
laurent22
merged 7 commits into
laurent22:dev
from
personalizedrefrigerator:pr/desktop/fix-editor-refocus
Sep 12, 2024
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ce85c74
Desktop: Fix editor loses focus on viewer/editor hidden with ctrl-l
personalizedrefrigerator 44ec918
Merge remote-tracking branch 'upstream/dev' into pr/desktop/fix-edito…
personalizedrefrigerator 1700fa5
Add test and refactor
personalizedrefrigerator a09485d
Refactor and fix tests
personalizedrefrigerator 032e9ea
Add comment, remove unnecessary extra .focus
personalizedrefrigerator c39acb0
Re-add focus(webviewRef) -- it's needed by tests. Additionally, it may
personalizedrefrigerator 78f804f
Update comment with reason for both focus calls
personalizedrefrigerator File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previously, after
.focus
ing the viewer, the arrow keys didn't scroll the viewer's content.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you this comment in the code? I'm not a big fan of having two different ways to focus something but if it's an exception we can leave it but with a comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The originalEdit: Tests fail whenfocus('NoteTextViewer::focus', this.webviewRef_.current);
seems to be unnecessary — I've removed it (leavingthis.send('focus')
) and added a comment.focus(..., webviewRef)
is removed. Additionally, without it,.focus
ing the viewer doesn't seem to work when not triggered by user interaction.