Skip to content

AI-native design editor. Open-source Figma alternative.

fix: follow history shortcuts across keyboard layouts (#1294)

Danny Ward70a2dfc89d31
diff --git a/changes/1294/fixed.md b/changes/1294/fixed.md
new file mode 100644
index 000000000..fef7b8242
--- /dev/null
+++ b/changes/1294/fixed.md
@@ -0,0 +1 @@
+- Keyboard shortcuts follow the letters printed on QWERTZ, AZERTY, and other Latin layouts, so Cmd/Ctrl+Z undoes on a German keyboard, and keep their key positions on Cyrillic and other non-Latin layouts.
diff --git a/src/app/shell/keyboard/document.ts b/src/app/shell/keyboard/document.ts
index b081bf899..9c62d92c9 100644
--- a/src/app/shell/keyboard/document.ts
+++ b/src/app/shell/keyboard/document.ts
@@ -3,6 +3,7 @@ import { createKeybindingsHandler, type KeyBindingMap } from 'tinykeys'
import { editorCommandMetadata, useEditorCommands, type EditorCommandId } from '@open-pencil/vue'
import { isEditing } from '@/app/shell/keyboard/focus'
+import { shortcutEvent } from '@/app/shell/keyboard/layout'
const DOCUMENT_COMMANDS = ['edit.undo', 'edit.redo'] as const satisfies EditorCommandId[]
@@ -25,5 +26,5 @@ export function useDocumentShortcuts(): (event: KeyboardEvent) => void {
}
}
const handler = createKeybindingsHandler(bindings)
- return (event) => handler(event)
+ return (event) => handler(shortcutEvent(event))
}
diff --git a/src/app/shell/keyboard/layout.ts b/src/app/shell/keyboard/layout.ts
new file mode 100644
index 000000000..3da5209df
--- /dev/null
+++ b/src/app/shell/keyboard/layout.ts
@@ -0,0 +1,23 @@
+const LATIN_LETTER = /^[a-z]$/i
+
+/**
+ * The key code a shortcut matches: the letter a Latin layout types, so Cmd+Z follows the printed
+ * Z on QWERTZ and AZERTY, or the physical key on layouts whose letter keys type no Latin letter,
+ * such as Russian, where Cmd+Я is Cmd+Z.
+ */
+export function shortcutCode(event: Pick<KeyboardEvent, 'key' | 'code'>): string {
+ return LATIN_LETTER.test(event.key) ? `Key${event.key.toUpperCase()}` : event.code
+}
+
+/** `event` with {@link shortcutCode} as its `code`, for tinykeys bindings written as key codes. */
+export function shortcutEvent(event: KeyboardEvent): KeyboardEvent {
+ const code = shortcutCode(event)
+ if (code === event.code) return event
+ return new Proxy(event, {
+ get(target, property) {
+ if (property === 'code') return code
+ const value: unknown = Reflect.get(target, property)
+ return typeof value === 'function' ? value.bind(target) : value
+ }
+ })
+}
diff --git a/src/app/shell/keyboard/registry.ts b/src/app/shell/keyboard/registry.ts
index 7d4745576..8bf6086b0 100644
--- a/src/app/shell/keyboard/registry.ts
+++ b/src/app/shell/keyboard/registry.ts
@@ -1,6 +1,6 @@
-import { tinykeys } from 'tinykeys'
+import { useEventListener } from '@vueuse/core'
+import { createKeybindingsHandler } from 'tinykeys'
import type { KeyBindingMap } from 'tinykeys'
-import { onScopeDispose } from 'vue'
import { editorCommandMetadata } from '@open-pencil/vue'
import type { EditorCommandId } from '@open-pencil/vue'
@@ -16,6 +16,7 @@ import { TOOL_SHORTCUTS } from '@/app/editor/session'
import { openSettingsDialog } from '@/app/settings/dialog'
import { setSnappingPreference } from '@/app/settings/preferences/apply'
import { isButtonActivation, isEditing } from '@/app/shell/keyboard/focus'
+import { shortcutEvent } from '@/app/shell/keyboard/layout'
import { bindSpaceHandTool } from '@/app/shell/keyboard/space-tool'
import type {
KeyboardShortcutOptions,
@@ -357,8 +358,7 @@ export function registerKeyboardShortcuts(options: KeyboardShortcutOptions) {
})
}
- const unsubscribeEditor = tinykeys(
- window,
+ const editorHandler = createKeybindingsHandler(
Object.fromEntries(
Object.entries(bindings).map(([keys, handler]) => [
keys,
@@ -368,14 +368,14 @@ export function registerKeyboardShortcuts(options: KeyboardShortcutOptions) {
handler(event)
}
])
- ),
- { capture: true }
+ )
)
+ const globalHandler = createKeybindingsHandler(globalBindings)
- const unsubscribeGlobal = tinykeys(window, globalBindings, { capture: true })
-
- onScopeDispose(() => {
- unsubscribeEditor()
- unsubscribeGlobal()
+ useEventListener(window, 'keydown', (event) => editorHandler(shortcutEvent(event)), {
+ capture: true
+ })
+ useEventListener(window, 'keydown', (event) => globalHandler(shortcutEvent(event)), {
+ capture: true
})
}
diff --git a/src/app/shell/keyboard/reserved.ts b/src/app/shell/keyboard/reserved.ts
index c5f14bfc0..1e3ee433f 100644
--- a/src/app/shell/keyboard/reserved.ts
+++ b/src/app/shell/keyboard/reserved.ts
@@ -1,4 +1,5 @@
import { isButtonActivation } from './focus'
+import { shortcutCode } from './layout'
const RESERVED_MOD_CODES = new Set([
'Backslash',
@@ -27,8 +28,9 @@ const RESERVED_MOD_CODES = new Set([
export function isReservedModShortcut(e: KeyboardEvent): boolean {
if (!(e.metaKey || e.ctrlKey)) return false
- if (e.altKey) return e.code === 'KeyB' || e.code === 'KeyK'
- return RESERVED_MOD_CODES.has(e.code)
+ const code = shortcutCode(e)
+ if (e.altKey) return code === 'KeyB' || code === 'KeyK'
+ return RESERVED_MOD_CODES.has(code)
}
export function preventReservedKeyboardDefaults(e: KeyboardEvent) {
diff --git a/src/app/shell/menu/shortcut.ts b/src/app/shell/menu/shortcut.ts
index 292776a35..d06a196dc 100644
--- a/src/app/shell/menu/shortcut.ts
+++ b/src/app/shell/menu/shortcut.ts
@@ -35,12 +35,14 @@ function normalizeShortcutToken(shortcut: string): string {
: shortcut.replaceAll('↩', 'Enter').replaceAll('⇞', 'PageUp').replaceAll('⇟', 'PageDown')
}
+/** A letter becomes its key code, which the keyboard registry matches by layout (`shortcutCode`). */
export function shortcutTokenToTinykeys(shortcut: string | undefined): string | undefined {
return shortcut
? normalizeShortcutToken(shortcut)
.replaceAll('MOD', '$mod')
.replaceAll('SHIFT', 'Shift')
.replaceAll('ALT', 'Alt')
+ .replace(/(^|\+)([A-Z])$/, '$1Key$2')
: undefined
}
diff --git a/src/components/commands/CommandPalette.vue b/src/components/commands/CommandPalette.vue
index 5cd2502f8..25888ba3c 100644
--- a/src/components/commands/CommandPalette.vue
+++ b/src/components/commands/CommandPalette.vue
@@ -13,6 +13,7 @@ import {
} from '@open-pencil/vue'
import { usePagePaletteGroup } from '@/app/editor/pages/palette'
+import { shortcutCode } from '@/app/shell/keyboard/layout'
import { useAppMenu } from '@/app/shell/menu/app-menu'
import IconButton from '@/components/ui/button/IconButton.vue'
import AppDialogRoot from '@/components/ui/dialog/AppDialogRoot.vue'
@@ -47,7 +48,8 @@ if (IS_BROWSER) {
const hasPlatformModifier = isMac
? event.metaKey && !event.ctrlKey
: event.ctrlKey && !event.metaKey
- if (!hasPlatformModifier || event.altKey || event.shiftKey || event.code !== 'KeyK') return
+ if (!hasPlatformModifier || event.altKey || event.shiftKey || shortcutCode(event) !== 'KeyK')
+ return
event.preventDefault()
open.value = true
})
diff --git a/src/components/comments/CommentComposer.vue b/src/components/comments/CommentComposer.vue
index 06a8c9587..3b3136e54 100644
--- a/src/components/comments/CommentComposer.vue
+++ b/src/components/comments/CommentComposer.vue
@@ -10,6 +10,7 @@ import {
formatComment,
type CommentEdit
} from '@/app/comments/format'
+import { shortcutCode } from '@/app/shell/keyboard/layout'
import AppButton from '@/components/ui/button/AppButton.vue'
import { comments } from '@/theme/comments'
@@ -67,7 +68,7 @@ function onKeydown(event: KeyboardEvent) {
return
}
const format = commentFormatForKey({
- code: event.code,
+ code: shortcutCode(event),
shiftKey: event.shiftKey,
altKey: event.altKey,
mod: mac ? event.metaKey : event.ctrlKey
diff --git a/src/components/variables/formula-field/useFormulaField.ts b/src/components/variables/formula-field/useFormulaField.ts
index f664eaefe..3958146ec 100644
--- a/src/components/variables/formula-field/useFormulaField.ts
+++ b/src/components/variables/formula-field/useFormulaField.ts
@@ -17,6 +17,7 @@ import { Annotation, EditorSelection, Prec } from '@codemirror/state'
import { EditorView, keymap, placeholder, tooltips } from '@codemirror/view'
import { getCurrentInstance, watch, type Ref, type ShallowRef } from 'vue'
+import { shortcutCode } from '@/app/shell/keyboard/layout'
import { useCodeMirror } from '@/components/code-editor/useCodeMirror'
import { formulaEditorTheme } from '@/theme/code/formula'
@@ -82,13 +83,11 @@ export function useFormulaField(
EditorView.domEventHandlers({
keydown(event, editor) {
if (event.isComposing) return false
- if (
- (event.metaKey || event.ctrlKey) &&
- (event.code === 'KeyZ' || event.code === 'KeyY')
- ) {
+ const code = shortcutCode(event)
+ if ((event.metaKey || event.ctrlKey) && (code === 'KeyZ' || code === 'KeyY')) {
event.preventDefault()
event.stopPropagation()
- if (event.shiftKey || event.code === 'KeyY') redo(editor)
+ if (event.shiftKey || code === 'KeyY') redo(editor)
else undo(editor)
return true
}
diff --git a/tests/e2e/keyboard/history.spec.ts b/tests/e2e/keyboard/history.spec.ts
new file mode 100644
index 000000000..6fe009ebc
--- /dev/null
+++ b/tests/e2e/keyboard/history.spec.ts
@@ -0,0 +1,68 @@
+import { expect, test, useEditorSetup } from '#tests/e2e/fixtures'
+import { getPageChildren } from '#tests/helpers/store'
+
+const editor = useEditorSetup()
+
+async function getPageChildCount() {
+ return (await getPageChildren(editor.page)).length
+}
+
+test.describe('undo/redo', () => {
+ test('⌘Z undoes last action', async () => {
+ await editor.canvas.clearCanvas()
+ await editor.canvas.drawRect(100, 100, 60, 60)
+ expect(await getPageChildCount()).toBe(1)
+
+ await editor.page.keyboard.press('ControlOrMeta+z')
+ await editor.canvas.waitForRender()
+
+ expect(await getPageChildCount()).toBe(0)
+ })
+
+ test('⌘⇧Z redoes undone action', async () => {
+ await editor.canvas.clearCanvas()
+ await editor.canvas.drawRect(100, 100, 60, 60)
+ await editor.page.keyboard.press('ControlOrMeta+z')
+ await editor.canvas.waitForRender()
+ expect(await getPageChildCount()).toBe(0)
+
+ await editor.page.keyboard.press('ControlOrMeta+Shift+z')
+ await editor.canvas.waitForRender()
+
+ expect(await getPageChildCount()).toBe(1)
+ })
+})
+
+test.describe('undo/redo across keyboard layouts', () => {
+ // Playwright cannot type one character from another key, so each press goes through CDP with
+ // the character a layout types and the physical key it sits on.
+ const presses = [
+ { layout: 'QWERTZ', label: 'Z', key: 'z', code: 'KeyY', shift: false, action: 'undo' },
+ { layout: 'QWERTZ', label: 'Shift+Z', key: 'Z', code: 'KeyY', shift: true, action: 'redo' },
+ { layout: 'QWERTZ', label: 'Y', key: 'y', code: 'KeyZ', shift: false, action: 'redo' },
+ { layout: 'AZERTY', label: 'Z', key: 'z', code: 'KeyW', shift: false, action: 'undo' },
+ { layout: 'Russian', label: 'Я', key: 'я', code: 'KeyZ', shift: false, action: 'undo' },
+ { layout: 'Russian', label: 'Shift+Я', key: 'Я', code: 'KeyZ', shift: true, action: 'redo' }
+ ] as const
+
+ for (const { layout, label, key, code, shift, action } of presses) {
+ test(`${layout} mod+${label} runs ${action}`, async () => {
+ await editor.canvas.clearCanvas()
+ await editor.canvas.drawRect(100, 100, 60, 60)
+ await editor.canvas.drawRect(200, 100, 60, 60)
+ await editor.page.keyboard.press('ControlOrMeta+z')
+ await editor.canvas.waitForRender()
+ expect(await getPageChildCount()).toBe(1)
+
+ const cdp = await editor.page.context().newCDPSession(editor.page)
+ const modifiers = (process.platform === 'darwin' ? 4 : 2) | (shift ? 8 : 0)
+ for (const type of ['rawKeyDown', 'keyUp'] as const) {
+ await cdp.send('Input.dispatchKeyEvent', { type, key, code, modifiers })
+ }
+ await cdp.detach()
+ await editor.canvas.waitForRender()
+
+ expect(await getPageChildCount()).toBe(action === 'undo' ? 0 : 2)
+ })
+ }
+})
diff --git a/tests/e2e/keyboard/shortcuts.spec.ts b/tests/e2e/keyboard/shortcuts.spec.ts
index 94c218236..0bc59aea0 100644
--- a/tests/e2e/keyboard/shortcuts.spec.ts
+++ b/tests/e2e/keyboard/shortcuts.spec.ts
@@ -514,26 +514,6 @@ test.describe('zoom shortcuts', () => {
})
})
-test.describe('undo/redo', () => {
- test('⌘Z undoes last action', async () => {
- await editor.canvas.clearCanvas()
- await editor.canvas.drawRect(100, 100, 60, 60)
- expect((await getPageChildren()).length).toBe(1)
-
- await editor.page.keyboard.press('Meta+z')
- await editor.canvas.waitForRender()
-
- expect((await getPageChildren()).length).toBe(0)
- })
-
- test('⌘⇧Z redoes undone action', async () => {
- await editor.page.keyboard.press('Meta+Shift+z')
- await editor.canvas.waitForRender()
-
- expect((await getPageChildren()).length).toBe(1)
- })
-})
-
test.describe('auto-layout shortcut', () => {
test('⇧A toggles auto-layout on frame', async () => {
await editor.canvas.clearCanvas()
diff --git a/tests/engine/app/shell/menu/shortcut.test.ts b/tests/engine/app/shell/menu/shortcut.test.ts
index 0148e1110..b77ebd7d9 100644
--- a/tests/engine/app/shell/menu/shortcut.test.ts
+++ b/tests/engine/app/shell/menu/shortcut.test.ts
@@ -14,7 +14,7 @@ describe('app menu shortcut conversion', () => {
})
test('converts shortcut tokens to tinykeys bindings', () => {
- expect(shortcutTokenToTinykeys('MOD+SHIFT+S')).toBe('$mod+Shift+S')
+ expect(shortcutTokenToTinykeys('MOD+SHIFT+S')).toBe('$mod+Shift+KeyS')
expect(shortcutTokenToTinykeys('ALT+Delete')).toBe('Alt+Delete')
expect(shortcutTokenToTinykeys('⌫')).toBe('Backspace')
})