Repository navigation
fix(devtools): support the keyboard shortcut on non-QWERTY layouts #1136
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
Open
ahmetomerv
wants to merge
10
commits into
nuxt:main
Choose a base branch
from
ahmetomerv:fix/keyboard-shortcut-non-qwerty
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+354
−9
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7fbc461
fix(devtools): support the shortcut on non-QWERTY layouts
ahmetomerv 37f5fa9
fix(devtools): sync the shortcut with the dock state
ahmetomerv 789a829
chore(devtools): remove redundant shortcut comment
ahmetomerv 0929303
[autofix.ci] apply automated fixes
autofix-ci[bot] 4437d4d
fix(devtools): hide the panel when closing from the shortcut
ahmetomerv f2ac809
fix(devtools): ignore stale keyboard layout results
ahmetomerv 442ccb8
fix(devtools): open the panel when the shortcut is pressed
ahmetomerv 58599cf
fix(devtools): drop stale shortcut codes on refresh
ahmetomerv 498a339
fix(devtools): close the panel session when toggling
ahmetomerv ee20ff3
fix(devtools): keep shortcut codes empty during refresh
ahmetomerv 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 hidden or 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 hidden or 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 hidden or 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,125 @@ | ||
| // DevTools is toggled with `Shift + Alt + D`. `KeyboardEvent.code` describes | ||
| // the physical key position, so it only matches the letter D on a QWERTY | ||
| // layout. Users on Workman, Colemak, etc. press a different physical key, and | ||
| // macOS rewrites the produced character while Option is held (`d` -> `∂`, | ||
| // with Shift -> `Î`). Resolve the keys that actually type `d` in the active | ||
| // layout and fall back to the produced key and the legacy key code. | ||
|
|
||
| interface KeyboardLayoutMapLike { | ||
| forEach: (callback: (key: string, code: string) => void) => void | ||
| } | ||
|
|
||
| interface KeyboardLike { | ||
| getLayoutMap: () => Promise<KeyboardLayoutMapLike> | ||
| addEventListener?: (type: 'layoutchange', listener: () => void) => void | ||
| } | ||
|
|
||
| export interface DevtoolsShortcutEvent { | ||
| altKey: boolean | ||
| shiftKey: boolean | ||
| ctrlKey?: boolean | ||
| metaKey?: boolean | ||
| code?: string | ||
| key?: string | ||
| keyCode?: number | ||
| } | ||
|
|
||
| export interface DevtoolsShortcutOptions { | ||
| /** Require Shift to be held. Opening requires it; closing from the panel does not. */ | ||
| shift?: boolean | ||
| /** Physical codes that type `d` in the active layout. Exposed for testing. */ | ||
| codes?: ReadonlySet<string> | ||
| } | ||
|
|
||
| const D_KEY_CODE = 68 | ||
| const QWERTY_D_CODE = 'KeyD' | ||
|
|
||
| const shortcutCodes = new Set([QWERTY_D_CODE]) | ||
|
|
||
| function resetToQwertyShortcut() { | ||
| shortcutCodes.clear() | ||
| shortcutCodes.add(QWERTY_D_CODE) | ||
| } | ||
|
|
||
| export function resolveShortcutCodes(layout: KeyboardLayoutMapLike): Set<string> { | ||
| const codes = new Set<string>() | ||
| layout.forEach((key, code) => { | ||
| if (key.toLowerCase() === 'd') | ||
| codes.add(code) | ||
| }) | ||
| return codes | ||
| } | ||
|
|
||
| function getKeyboard(): KeyboardLike | undefined { | ||
| if (typeof navigator === 'undefined') | ||
| return | ||
| return (navigator as Navigator & { keyboard?: KeyboardLike }).keyboard | ||
| } | ||
|
|
||
| let watching = false | ||
| let refreshRequest = 0 | ||
|
|
||
| export function watchShortcutLayoutMap(): void { | ||
| if (watching) | ||
| return | ||
| watching = true | ||
|
|
||
| const keyboard = getKeyboard() | ||
| if (!keyboard?.getLayoutMap) | ||
| return | ||
|
|
||
| const refresh = () => { | ||
| const request = ++refreshRequest | ||
| shortcutCodes.clear() | ||
| try { | ||
| keyboard.getLayoutMap() | ||
| .then((layout) => { | ||
| if (request !== refreshRequest) | ||
| return | ||
| const codes = resolveShortcutCodes(layout) | ||
| if (!codes.size) { | ||
| resetToQwertyShortcut() | ||
| return | ||
| } | ||
| shortcutCodes.clear() | ||
| for (const code of codes) | ||
| shortcutCodes.add(code) | ||
| }) | ||
| .catch(() => { | ||
| if (request === refreshRequest) | ||
| resetToQwertyShortcut() | ||
| }) | ||
| } | ||
| catch { | ||
| // Blocked by a permissions policy. `code`, `key` and `keyCode` matching | ||
| // still work without the layout map. | ||
| resetToQwertyShortcut() | ||
| } | ||
| } | ||
|
|
||
| refresh() | ||
| keyboard.addEventListener?.('layoutchange', refresh) | ||
| } | ||
|
|
||
| export function isDevtoolsShortcut( | ||
| event: DevtoolsShortcutEvent, | ||
| options: DevtoolsShortcutOptions = {}, | ||
| ): boolean { | ||
| if (!event.altKey) | ||
| return false | ||
| if (options.shift && !event.shiftKey) | ||
| return false | ||
| if (event.ctrlKey || event.metaKey) | ||
| return false | ||
|
|
||
| const codes = options.codes ?? shortcutCodes | ||
| if (event.code && codes.has(event.code)) | ||
| return true | ||
|
|
||
| if (event.key?.toLowerCase() === 'd') | ||
| return true | ||
|
|
||
| // Deprecated, but it identifies the key produced by the active layout and | ||
| // ignores modifiers, which matters on macOS where Option rewrites `key`. | ||
| return event.keyCode === D_KEY_CODE | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.