From 9483d598fc52c509b7b47c7dccd0ed468c225b42 Mon Sep 17 00:00:00 2001 From: Steffen Becker Date: Fri, 31 Jul 2026 20:43:48 +0200 Subject: [PATCH] feat: keyboard shortcuts --- CHANGELOG.md | 1 + src/App.svelte | 55 ++++------ src/lib/components/AlignButton.svelte | 7 +- src/lib/components/DateTimePicker.svelte | 8 +- src/lib/components/Toolbar.svelte | 13 ++- src/lib/components/ToolbarExpanded.svelte | 31 +++--- src/lib/editor/extensions.ts | 4 + src/lib/editor/extensions/headerFooter.ts | 3 + src/lib/editor/extensions/indent.ts | 5 +- src/lib/editor/extensions/link.ts | 3 +- src/lib/editor/extensions/pageBreak.ts | 3 +- src/lib/editor/extensions/paragraphStyle.ts | 3 +- src/lib/editor/extensions/shortcuts.ts | 116 ++++++++++++++++++++ src/lib/editor/shortcuts.ts | 101 +++++++++++++++++ src/lib/i18n/locales/de.ts | 1 + src/lib/i18n/locales/en.ts | 1 + src/lib/i18n/shortcut.ts | 3 +- src/lib/utils/fontSize.ts | 14 +++ tests/unit/shortcuts.test.ts | 53 +++++++++ 19 files changed, 366 insertions(+), 59 deletions(-) create mode 100644 src/lib/editor/extensions/shortcuts.ts create mode 100644 src/lib/editor/shortcuts.ts create mode 100644 tests/unit/shortcuts.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f813b0..5a2f8b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -67,6 +67,7 @@ - Show formatting marks (spaces, tabs, paragraph marks) **Editing aids** +- Word/LibreOffice keyboard shortcuts throughout: Ctrl+L/E/R/J alignment, Ctrl+Alt+1–5 headings and Ctrl+Shift+N default style, Ctrl+1/2/5 line spacing, Ctrl+Shift+P / Ctrl+Shift+B super/subscript, Ctrl+Shift+. / Ctrl+Shift+, grow/shrink font, Ctrl+Space clear formatting, Ctrl+Shift+Space non-breaking space, Ctrl+Shift+- soft hyphen, Alt+Shift+D / Alt+Shift+T date & time field, Ctrl+O open, F3 / Shift+F3 find next/previous, Ctrl+F10 formatting marks — alongside the existing Ctrl+B/I/U, Ctrl+M, Ctrl+K, Ctrl+Enter, Ctrl+F/H, Ctrl+S/P and Tab/Shift+Tab. All bindings live in one table, so tooltips stay in sync and they can be remapped later - Undo / Redo with a labelled history dropdown - Search & Replace (Ctrl+F / Ctrl+H): live match highlighting, match count, next / previous, match-case and whole-word options, replace current / replace all - Spell check in English and German with squiggles and a suggestions context menu (add / ignore word); selectable document language. Powered by Hunspell (WASM), so German compound words (Fußgänger, Krankenversicherung …) are recognised; dictionaries are lazy-loaded per language on demand diff --git a/src/App.svelte b/src/App.svelte index fc5024e..b1f1780 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -35,6 +35,7 @@ import AboutDialog from './lib/components/AboutDialog.svelte'; import { t } from './lib/i18n/i18n.svelte'; import { withShortcut } from './lib/i18n/shortcut'; + import { DEFAULT_SHORTCUTS, matchesEvent, shortcutHint } from './lib/editor/shortcuts'; import { localizeImportMessage } from './lib/i18n/importMessages'; import { unavailableFonts } from './lib/utils/fontDetect'; import { registerEmbeddedFonts, clearEmbeddedFonts } from './lib/fonts/embeddedFonts'; @@ -670,38 +671,30 @@ // FontFace load fires 'loadingdone', which Editor.svelte re-paginates on. void loadEmbeddedFonts().then(registerEmbeddedFonts); + // Shortcuts that must work regardless of focus and that suppress the browser's + // own binding (save page, find, open, zoom). Everything editor-scoped lives in + // the Shortcuts extension instead. + const appActions: [string, () => void][] = [ + [DEFAULT_SHORTCUTS.save, handleSave], + [DEFAULT_SHORTCUTS.open, handleOpen], + [DEFAULT_SHORTCUTS.print, handlePrint], + [DEFAULT_SHORTCUTS.find, () => openFind('find')], + [DEFAULT_SHORTCUTS.replace, () => openFind('replace')], + // Closing the bar clears the search, so F3 with no bar starts one. + [DEFAULT_SHORTCUTS.findNext, () => (findOpen ? editor?.commands.findNext() : openFind('find'))], + [DEFAULT_SHORTCUTS.findPrevious, () => (findOpen ? editor?.commands.findPrevious() : openFind('find'))], + [DEFAULT_SHORTCUTS.formattingMarks, () => (showFormattingMarks = !showFormattingMarks)], + [DEFAULT_SHORTCUTS.zoomIn, () => setZoom(zoom + 10)], + [DEFAULT_SHORTCUTS.zoomOut, () => setZoom(zoom - 10)], + [DEFAULT_SHORTCUTS.zoomReset, () => setZoom(100)], + ]; + function onKeydown(e: KeyboardEvent) { - // Ctrl/Cmd+S → Save (suppress the browser's save-page dialog). - if ((e.ctrlKey || e.metaKey) && !e.shiftKey && (e.key === 's' || e.key === 'S')) { + for (const [combo, run] of appActions) { + if (!matchesEvent(e, combo)) continue; e.preventDefault(); - handleSave(); - } - // Ctrl/Cmd+P → Print the paginated document (not the whole app UI). - if ((e.ctrlKey || e.metaKey) && !e.shiftKey && (e.key === 'p' || e.key === 'P')) { - e.preventDefault(); - handlePrint(); - } - // Ctrl/Cmd+F → Find, Ctrl/Cmd+H → Find & Replace (suppress the browser's own find). - if ((e.ctrlKey || e.metaKey) && !e.shiftKey && (e.key === 'f' || e.key === 'F')) { - e.preventDefault(); - openFind('find'); - } - if ((e.ctrlKey || e.metaKey) && !e.shiftKey && (e.key === 'h' || e.key === 'H')) { - e.preventDefault(); - openFind('replace'); - } - // Ctrl/Cmd +/−/0 → zoom the document, not the browser window. - if ((e.ctrlKey || e.metaKey) && !e.shiftKey && !e.altKey) { - if (e.key === '+' || e.key === '=' || e.code === 'NumpadAdd') { - e.preventDefault(); - setZoom(zoom + 10); - } else if (e.key === '-' || e.code === 'NumpadSubtract') { - e.preventDefault(); - setZoom(zoom - 10); - } else if (e.key === '0' || e.code === 'Numpad0') { - e.preventDefault(); - setZoom(100); - } + run(); + return; } // Escape closes the bar (when it isn't handled inside an input). if (e.key === 'Escape' && findOpen) closeFind(); @@ -808,7 +801,7 @@ -