Skip to content

[lexical-extension][lexical-playground] Feature: Compiled keyboard shortcut dispatch - #18

Closed
etrepum wants to merge 8 commits into
mainfrom
claude/keyboard-shortcuts-dispatch-6thty5
Closed

etrepum wants to merge 8 commits into
mainfrom
claude/keyboard-shortcuts-dispatch-6thty5

Conversation

@etrepum

@etrepum etrepum commented Jul 10, 2026

Copy link
Copy Markdown
Owner

Replace the O(n) if/else keyboard shortcut matching with a data-driven
scheme: shortcuts are declared as data (key + modifier mask + handler)
and compiled down to a Map keyed by the modifier bitmask and pressed
key, so dispatch is O(1) per keypress regardless of table size. The
matching semantics (including the event.code fallback for non-Latin
keyboard layouts) are identical to isExactShortcutMatch, verified by an
exhaustive parity test.

  • @lexical/extension: add compileKeyboardShortcuts,
    registerKeyboardShortcuts, and KeyboardShortcutsExtension. The
    extension holds a named shortcut table merged across the extension
    graph, so apps and other extensions can overlay it: configure an
    existing name to remap a shortcut, configure it to null to disable
    it, or add new names for new shortcuts. The config is exposed as
    signals so the table can also be remapped at runtime.
  • lexical: export the KeyboardEventModifiers and
    KeyboardEventModifierMask types.
  • lexical-playground: ShortcutsPlugin now declares its bindings in a
    SHORTCUT_BINDINGS table and registers them through
    registerKeyboardShortcuts instead of testing 27 predicates in
    sequence.

Co-Authored-By: Claude Fable 5 [email protected]

@vercel

vercel Bot commented Jul 10, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
lexical-lexical-website Ready Ready Preview, Comment Jul 20, 2026 8:27pm

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 10, 2026 •

Copy link
Copy Markdown

claude added 8 commits July 20, 2026 20:20
…ortcut dispatch

Replace the O(n) if/else keyboard shortcut matching with a data-driven
scheme: shortcuts are declared as data (key + modifier mask + handler)
and compiled down to a Map keyed by the modifier bitmask and pressed
key, so dispatch is O(1) per keypress regardless of table size. The
matching semantics (including the event.code fallback for non-Latin
keyboard layouts) are identical to isExactShortcutMatch, verified by an
exhaustive parity test.

- @lexical/extension: add compileKeyboardShortcuts,
  registerKeyboardShortcuts, and KeyboardShortcutsExtension. The
  extension holds a named shortcut table merged across the extension
  graph, so apps and other extensions can overlay it: configure an
  existing name to remap a shortcut, configure it to null to disable
  it, or add new names for new shortcuts. The config is exposed as
  signals so the table can also be remapped at runtime.
- lexical: export the KeyboardEventModifiers and
  KeyboardEventModifierMask types.
- lexical-playground: ShortcutsPlugin now declares its bindings in a
  SHORTCUT_BINDINGS table and registers them through
  registerKeyboardShortcuts instead of testing 27 predicates in
  sequence.

Co-Authored-By: Claude Fable 5 <[email protected]>
… in LexicalEvents

Move the compiled keyboard shortcut dispatcher into the lexical core
(LexicalKeyboardShortcuts.ts) so that the editor's own $handleKeyDown
uses the same code as KeyboardShortcutsExtension. The 28-branch
predicate chain in LexicalEvents is now a declarative table compiled
once (lazily) into the O(1) modifier-bitmask + key lookup, and the ~30
single-use is* predicates it called (isBold, isMoveForward, isRedo,
etc.) are deleted from LexicalUtils along with their CONTROL_OR_META /
CONTROL_OR_ALT constants.

- lexical: add CompiledKeyboardShortcuts, compileKeyboardShortcuts,
  registerKeyboardShortcuts, and the KeyboardShortcut types to the
  public API; rewrite $handleKeyDown on the compiled table.
- @lexical/extension: KeyboardShortcutsExtension now reuses the core
  implementation and re-exports it, shrinking the extension module to
  just the named-table config/overlay logic.
- Parity is verified by an exhaustive test that transcribes the old
  predicate chain and compares dispatched commands, payloads, and
  preventDefault against the new table for 25 key/code pairs x 16
  modifier states on both Apple and non-Apple platforms (including
  non-Latin layout code fallbacks).

Co-Authored-By: Claude Fable 5 <[email protected]>
… jsx build

The build pipeline parses .ts modules with the JSX plugin enabled, so
the generic arrow function `<T>(command, payload) => ...` in
buildKeyDownShortcuts was misparsed as a JSX tag and failed
`pnpm run build` in CI (tsc, eslint, and vitest all accepted it).
Use a function declaration instead.

Co-Authored-By: Claude Fable 5 <[email protected]>
…llup build

@babel/preset-react unconditionally enables JSX parsing for every file
it processes, so applying it to all extensions made the build parse .ts
modules as TSX, where a generic arrow function like `<T>(x: T) => x`
is misparsed as a JSX opening tag (tsc accepts it in .ts, so only the
build failed). Move the preset into a babel overrides block gated on
/\.[jt]sx$/ so .ts is parsed as plain TypeScript, and restore the
generic arrow in buildKeyDownShortcuts that previously had to be
written as a function declaration to work around this.

Co-Authored-By: Claude Fable 5 <[email protected]>
Replace the repeated {key, modifiers, onMatch} object literals in
buildKeyDownShortcuts with dispatch/prevent/enter/copyOrCut factories
so each binding is a one-line call; only the three entries with
genuinely custom behavior (Backspace, select-all, Apple Ctrl+O) remain
literals. Also derive getEventModifierBits from the shared
MODIFIER_BITS table instead of repeating each modifier constant.

Co-Authored-By: Claude Fable 5 <[email protected]>
…ed shortcuts with $disabled and $dispatch

Redesign KeyboardShortcut as pure data: a shortcut now maps a key
binding to a LexicalCommand<KeyboardEvent> that is dispatched with the
matched event as its payload, instead of holding an arbitrary handler
function. This keeps the table declarative - it can be extracted to
build a shortcuts menu (with the new formatKeyboardShortcut display
helper and optional description field), remapped through the
KeyboardShortcutsExtension signal, and the behavior lives in command
listeners that menus and buttons can share. Two escape hatches:
$disabled(selection, editor) is checked before dispatch (and can drive
disabled menu items), and $dispatch(command, event, $next, editor) is
optional middleware for shortcuts that must run additional code around
the dispatch without defining a wrapper command.

- lexical: export CONTROL_OR_META and CONTROL_OR_ALT (used by the core
  keydown table, the playground, and most shortcut definitions) and
  formatKeyboardShortcut.
- lexical-playground: migrate ShortcutsPlugin (React) to a
  ShortcutsExtension (no React): each action is a
  SHORTCUT_COMMANDS[name] command registered by the extension, the key
  bindings are contributed to the KeyboardShortcutsExtension signal
  table, blockType/fontSize/isLink are derived from the editor state
  instead of ToolbarContext, and the insert-link shortcut publishes an
  isLinkEditMode signal that Editor.tsx consumes in place of its React
  state.

Co-Authored-By: Claude Fable 5 <[email protected]>
… and move formatKeyboardShortcut

registerKeyboardShortcuts now calls the internal dispatchCommand
primitive instead of editor.dispatchCommand, and binds $next with
Function.prototype.bind (only when a $dispatch middleware is present)
instead of allocating an arrow closure per dispatch. Move
formatKeyboardShortcut and FormatKeyboardShortcutOptions out of the
lexical core into @lexical/extension - display formatting for menus
doesn't need to bloat the core.

Co-Authored-By: Claude Fable 5 <[email protected]>
A generator is unnecessary here since the match lists are so cheap to
materialize; an array is simpler for callers and menu builders.

Co-Authored-By: Claude Fable 5 <[email protected]>

This branch was successfully deployed

1 active deployment
Preview — 053ff264 Deployed Jul 20, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants