Skip to content

Commit

Permalink
Rename all references from chord to sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
iansan5653 committed Sep 11, 2023
1 parent b2aa588 commit 6db787b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
32 changes: 16 additions & 16 deletions pages/hotkey_mapper.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ <h1 id="app-name">Hotkey Code</h1>
aria-roledescription="Input Capture"
autofocus
aria-labelledby="app-name"
aria-describedby="hint chord-hint"
aria-describedby="hint sequence-hint"
aria-live="assertive"
aria-atomic="true"
id="hotkey-code"
Expand All @@ -29,10 +29,10 @@ <h1 id="app-name">Hotkey Code</h1>
/>

<div class="position-absolute bottom-2 left-3 right-3 d-flex" style="align-items: center; gap: 8px">
<!-- This indicates that the input is listening for a chord press. Ideally we'd have a way to tell screen
<!-- This indicates that the input is listening for a sequence press. Ideally we'd have a way to tell screen
readers this too, but if we make this live and add more text it will get annoying because it will conflict
with the already-live input above. -->
<p id="chord-status" class="color-fg-subtle" style="margin: 0" aria-hidden hidden></p>
<p id="sequence-status" class="color-fg-subtle" style="margin: 0" aria-hidden hidden></p>

<span style="flex: 1"></span>

Expand All @@ -47,19 +47,19 @@ <h1 id="app-name">Hotkey Code</h1>

<script type="module">
import {eventToHotkeyString} from '../dist/index.js'
import ChordTracker from '../dist/chord.js'
import sequenceTracker from '../dist/sequence.js'

const hotkeyCodeElement = document.getElementById('hotkey-code')
const chordStatusElement = document.getElementById('chord-status')
const sequenceStatusElement = document.getElementById('sequence-status')
const resetButtonElement = document.getElementById('reset-button')

const chordTracker = new ChordTracker({
const sequenceTracker = new sequenceTracker({
onReset() {
chordStatusElement.hidden = true
sequenceStatusElement.hidden = true
}
})

let currentChord = null
let currentsequence = null

hotkeyCodeElement.addEventListener('keydown', event => {
if (event.key === "Tab")
Expand All @@ -68,22 +68,22 @@ <h1 id="app-name">Hotkey Code</h1>
event.preventDefault();
event.stopPropagation();

currentChord = eventToHotkeyString(event)
event.currentTarget.value = [...chordTracker.path, currentChord].join(' ');
currentsequence = eventToHotkeyString(event)
event.currentTarget.value = [...sequenceTracker.path, currentsequence].join(' ');
})

hotkeyCodeElement.addEventListener('keyup', () => {
// we don't just build the chord from the keyup event because keyups don't necessarily map to keydowns - for
// we don't just build the sequence from the keyup event because keyups don't necessarily map to keydowns - for
// example, the keyup event for meta+b is just meta.
if (currentChord) {
chordTracker.registerKeypress(currentChord)
chordStatusElement.hidden = false
currentChord = null
if (currentsequence) {
sequenceTracker.registerKeypress(currentsequence)
sequenceStatusElement.hidden = false
currentsequence = null
}
})

resetButtonElement.addEventListener('click', () => {
chordTracker.reset()
sequenceTracker.reset()
hotkeyCodeElement.value = ''
})
</script>
Expand Down
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {Leaf, RadixTrie} from './radix-trie'
import {fireDeterminedAction, expandHotkeyToEdges, isFormField} from './utils'
import eventToHotkeyString from './hotkey'
import ChordTracker from './chord'
import SequenceTracker from './sequence'

const hotkeyRadixTrie = new RadixTrie<HTMLElement>()
const elementsLeaves = new WeakMap<HTMLElement, Array<Leaf<HTMLElement>>>()
let currentTriePosition: RadixTrie<HTMLElement> | Leaf<HTMLElement> = hotkeyRadixTrie

const chordTracker = new ChordTracker({
const sequenceTracker = new SequenceTracker({
onReset() {
currentTriePosition = hotkeyRadixTrie
}
Expand All @@ -26,10 +26,10 @@ function keyDownHandler(event: KeyboardEvent) {
// they've pressed a wrong key-combo and we should reset the flow
const newTriePosition = (currentTriePosition as RadixTrie<HTMLElement>).get(eventToHotkeyString(event))
if (!newTriePosition) {
chordTracker.reset()
sequenceTracker.reset()
return
}
chordTracker.registerKeypress(eventToHotkeyString(event))
sequenceTracker.registerKeypress(eventToHotkeyString(event))

currentTriePosition = newTriePosition
if (newTriePosition instanceof Leaf) {
Expand All @@ -48,11 +48,11 @@ function keyDownHandler(event: KeyboardEvent) {
}

if (elementToFire && shouldFire) {
fireDeterminedAction(elementToFire, chordTracker.path)
fireDeterminedAction(elementToFire, sequenceTracker.path)
event.preventDefault()
}

chordTracker.reset()
sequenceTracker.reset()
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/chord.ts → src/sequence.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
interface ChordTrackerOptions {
interface SequenceTrackerOptions {
onReset?: () => void
}

export default class ChordTracker {
export default class SequenceTracker {
static readonly CHORD_TIMEOUT = 1500

private _path: readonly string[] = []
private timer: number | null = null
private onReset

constructor({onReset}: ChordTrackerOptions = {}) {
constructor({onReset}: SequenceTrackerOptions = {}) {
this.onReset = onReset
}

Expand Down Expand Up @@ -37,6 +37,6 @@ export default class ChordTracker {

private startTimer(): void {
this.killTimer()
this.timer = window.setTimeout(() => this.reset(), ChordTracker.CHORD_TIMEOUT)
this.timer = window.setTimeout(() => this.reset(), SequenceTracker.CHORD_TIMEOUT)
}
}

0 comments on commit 6db787b

Please sign in to comment.