Skip to content

Commit

Permalink
Fix non-standard keyboard layour support for hot keys
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Jun 1, 2024
1 parent 35e564c commit 31f58ec
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
5 changes: 5 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import loguxTsConfig from '@logux/eslint-config/ts'
export default [
{ ignores: ['dist/'] },
...loguxTsConfig,
{
rules: {
'no-control-regex': 'off'
}
},
{
files: ['**/worker.ts'],
rules: {
Expand Down
21 changes: 18 additions & 3 deletions view/field/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,25 @@ function isSpecial(e: KeyboardEvent): boolean {
return e.ctrlKey || e.shiftKey || e.altKey || e.metaKey
}

function pressHotkey(code: string): boolean {
let input = hotkeys[code]
if (input) {
input.focus()
return true
} else {
return false
}
}

const NON_ENGLISH_LAYOUT = /^[^\x00-\x7F]$/

function onKeyDown(e: KeyboardEvent): void {
if (hotkeys[e.code] && !isSpecial(e)) {
e.preventDefault()
hotkeys[e.code]?.focus()
if (isSpecial(e)) return
if (!pressHotkey(e.key.toLowerCase())) {
if (NON_ENGLISH_LAYOUT.test(e.key) && /^Key.$/.test(e.code)) {
let enKey = e.code.replace(/^Key/, '').toLowerCase()
pressHotkey(enKey)
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions view/field/mixin.pug
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mixin field(label, hotkey, opts = {})
.field( class=opts.options ? 'is-options' : '' )
kbd.field_hotkey= hotkey
.field( class=opts.options ? 'is-options' : '' aria-keyshortcuts=hotkey )
kbd.field_hotkey( aria-hidden )= hotkey
input.field_input( aria-label=label )&attributes({ type: 'text', ...opts })
if (opts.role === 'spinbutton')
button.field_control.is-increase( tabindex="-1" aria-hidden="true" )
Expand Down

0 comments on commit 31f58ec

Please sign in to comment.