Skip to content

Commit

Permalink
Merge pull request #49 from github/kh-scope
Browse files Browse the repository at this point in the history
Add support for scoped hotkey
  • Loading branch information
khiga8 authored Sep 23, 2021
2 parents 04b4bcf + 4d532c3 commit e543eda
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
4 changes: 2 additions & 2 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

<body>

<button onclick="alert('clicked!')" data-hotkey="d">press d to click this button</button><br>
<textarea data-hotkey="t" rows="4" cols="40">press t to focus on this field</textarea><br>
<button onclick="alert('clicked!')" data-hotkey-scope="text-area-1" data-hotkey="Control+d,Meta+d">press meta+d or ctrl+d in text area to click this button</button><br>
<textarea id="text-area-1" data-hotkey="t" rows="4" cols="40">press t to focus on this field</textarea><br>
<label><input data-hotkey="r" type="checkbox">Press r to check/uncheck this checkbox</label><br>
<a href="#ok" data-hotkey="o k">Press <kbd>o k</kbd> click this link</a>

Expand Down
27 changes: 22 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ function resetTriePosition() {

function keyDownHandler(event: KeyboardEvent) {
if (event.defaultPrevented) return
if (event.target instanceof Node && isFormField(event.target)) return

if (!(event.target instanceof Node)) return
if (isFormField(event.target)) {
const target = event.target as HTMLElement
if (!target.id) return
if (!target.ownerDocument.querySelector(`[data-hotkey-scope=${target.id}]`)) return
}
if (resetTriePositionTimer != null) {
window.clearTimeout(resetTriePositionTimer)
}
Expand All @@ -31,10 +35,23 @@ function keyDownHandler(event: KeyboardEvent) {

currentTriePosition = newTriePosition
if (newTriePosition instanceof Leaf) {
fireDeterminedAction(newTriePosition.children[newTriePosition.children.length - 1])
event.preventDefault()
let shouldFire = true
const elementToFire = newTriePosition.children[newTriePosition.children.length - 1]
const hotkeyScope = elementToFire.getAttribute('data-hotkey-scope')
if (isFormField(event.target)) {
const target = event.target as HTMLElement
if (target.id !== elementToFire.getAttribute('data-hotkey-scope')) {
shouldFire = false
}
} else if (hotkeyScope) {
shouldFire = false
}

if (shouldFire) {
fireDeterminedAction(elementToFire)
event.preventDefault()
}
resetTriePosition()
return
}
}

Expand Down
34 changes: 34 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,46 @@ describe('hotkey', function () {
})
})

describe('data-hotkey-scope', function () {
it('allows hotkey action from form field', function () {
setHTML(`
<button id="button1" data-hotkey-scope="textfield" data-hotkey="Meta+b">Button 1</button>
<input id="textfield" />`)
document
.getElementById('textfield')
.dispatchEvent(new KeyboardEvent('keydown', {bubbles: true, metaKey: true, cancelable: true, key: 'b'}))
assert.include(elementsActivated, 'button1')
})

it('does nothing if `data-hotkey-scope` is set to non-form field', function () {
setHTML(`
<button id="button1" data-hotkey-scope="button2" data-hotkey="Meta+b">Button 1</button>
<button id="button2" />`)
document
.getElementById('button2')
.dispatchEvent(new KeyboardEvent('keydown', {bubbles: true, metaKey: true, cancelable: true, key: 'b'}))
assert.deepEqual(elementsActivated, [])
})

it('does nothing if `data-hotkey-scope` does not exist', function () {
setHTML(`
<button id="button1" data-hotkey-scope="bad-id" data-hotkey="b">Button 1</button>
<input id="textfield" />`)
document
.getElementById('textfield')
.dispatchEvent(new KeyboardEvent('keydown', {bubbles: true, cancelable: true, key: 'b'}))
assert.deepEqual(elementsActivated, [])
})
})

describe('eventToHotkeyString', function () {
const tests = [
['Control+J', {ctrlKey: true, shiftKey: true, code: 'KeyJ', key: 'J'}],
['Control+Shift+j', {ctrlKey: true, shiftKey: true, code: 'KeyJ', key: 'j'}],
['Control+j', {ctrlKey: true, code: 'KeyJ', key: 'j'}],
['Meta+Shift+p', {key: 'p', metaKey: true, shiftKey: true, code: 'KeyP'}],
['Meta+Shift+8', {key: '8', metaKey: true, shiftKey: true, code: 'Digit8'}],
['Control+Shift+7', {key: '7', ctrlKey: true, shiftKey: true, code: 'Digit7'}],
['J', {shiftKey: true, code: 'KeyJ', key: 'J'}],
['/', {key: '/', code: ''}],
['1', {key: '1', code: 'Digit1'}],
Expand Down

0 comments on commit e543eda

Please sign in to comment.