fix(shortcuts): share one shortcut label formatter between settings and the session picker

This commit is contained in:
Chris Scott committed 2026-10-14 01:14:55 -04:00
1 parent 18102a3ce8
commit c41eff304c
5 files changed
+43 -19

No files matched your search

+8 -1
View File
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest'
import { DEFAULT_DIRECT_SHORTCUTS, DEFAULT_KEYBOARD_SHORTCUTS, DEFAULT_LEADER_KEY } from '@/api/types/settings'
import { formatShortcutEvent, getDefaultKeyboardShortcuts, normalizeShortcut, resolveDirectShortcuts, resolveShortcutBindings } from './keyboardShortcuts'
import { formatShortcutEvent, formatShortcutLabel, getDefaultKeyboardShortcuts, normalizeShortcut, resolveDirectShortcuts, resolveShortcutBindings } from './keyboardShortcuts'
describe('keyboardShortcuts', () => {
it('normalizes key aliases, modifier order and bare symbol keys', () => {
@@ -11,6 +11,13 @@ describe('keyboardShortcuts', () => {
expect(normalizeShortcut('+')).toBe('+')
})
it('formats empty, direct and leader bindings as labels', () => {
expect(formatShortcutLabel('', true, 'Ctrl+X')).toBe('')
expect(formatShortcutLabel('', false, 'Ctrl+X')).toBe('')
expect(formatShortcutLabel('Shift+Ctrl+z', true, 'Ctrl+X')).toBe('Ctrl+Shift+Z')
expect(formatShortcutLabel('G', false, 'Ctrl+Enter')).toBe('Ctrl+Return → G')
})
it('reads letters from the physical key when Alt rewrites the character', () => {
expect(formatShortcutEvent(new KeyboardEvent('keydown', { key: '†', code: 'KeyT', altKey: true }))).toBe('Alt+T')
expect(formatShortcutEvent(new KeyboardEvent('keydown', { key: 'Shift', shiftKey: true }))).toBeNull()
+10
View File
@@ -75,6 +75,16 @@ export function normalizeShortcut(shortcut: string): string {
return [...orderedModifiers, mainKey].join('+')
}
/**
* Formats a stored binding for display: empty keys render as an empty string, a direct binding as its normalized keys,
* and a leader binding as `leader → keys`, with both the leader and the keys normalized.
*/
export function formatShortcutLabel(keys: string, isDirect: boolean, leaderKey: string): string {
if (!keys) return ''
if (isDirect) return normalizeShortcut(keys)
return `${normalizeShortcut(leaderKey)} → ${normalizeShortcut(keys)}`
}
export function hasCommandModifier(shortcut: string): boolean {
return /(^|\+)(Ctrl|Cmd|Alt)\+./.test(shortcut)
}