-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Desktop: Accessibility: Improve note title focus handling (#10932)
- Loading branch information
1 parent
2afc2ca
commit 74be949
Showing
14 changed files
with
146 additions
and
19 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
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
43 changes: 43 additions & 0 deletions
43
packages/app-desktop/gui/NoteEditor/NoteBody/TinyMCE/utils/useKeyboardRefocusHandler.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,43 @@ | ||
import CommandService from '@joplin/lib/services/CommandService'; | ||
import { useEffect } from 'react'; | ||
import type { Editor, EditorEvent } from 'tinymce'; | ||
|
||
const useKeyboardRefocusHandler = (editor: Editor) => { | ||
useEffect(() => { | ||
if (!editor) return () => {}; | ||
|
||
const isAtBeginningOf = (element: Node, parent: HTMLElement) => { | ||
if (!parent.contains(element)) return false; | ||
|
||
while ( | ||
element && | ||
element.parentNode !== parent && | ||
element.parentNode?.firstChild === element | ||
) { | ||
element = element.parentNode; | ||
} | ||
|
||
return !!element && element.parentNode?.firstChild === element; | ||
}; | ||
|
||
const eventHandler = (event: EditorEvent<KeyboardEvent>) => { | ||
if (!event.isDefaultPrevented() && event.key === 'ArrowUp') { | ||
const selection = editor.selection.getRng(); | ||
if (selection.startOffset === 0 && | ||
selection.collapsed && | ||
isAtBeginningOf(selection.startContainer, editor.dom.getRoot()) | ||
) { | ||
event.preventDefault(); | ||
void CommandService.instance().execute('focusElement', 'noteTitle'); | ||
} | ||
} | ||
}; | ||
|
||
editor.on('keydown', eventHandler); | ||
return () => { | ||
editor.off('keydown', eventHandler); | ||
}; | ||
}, [editor]); | ||
}; | ||
|
||
export default useKeyboardRefocusHandler; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { EditorState } from '@codemirror/state'; | ||
|
||
const isCursorAtBeginning = (state: EditorState) => { | ||
const selection = state.selection; | ||
return selection.ranges.length === 1 && selection.main.empty && selection.main.anchor === 0; | ||
}; | ||
|
||
export default isCursorAtBeginning; |
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