Skip to content

Add custom keybindings API to CodeMirror editor#6000

Draft
Jepson2k wants to merge 1 commit intozauberzeug:mainfrom
Jepson2k:cm-keybindings
Draft

Add custom keybindings API to CodeMirror editor#6000
Jepson2k wants to merge 1 commit intozauberzeug:mainfrom
Jepson2k:cm-keybindings

Conversation

@Jepson2k
Copy link
Copy Markdown

Motivation

ui.codemirror exposes no way to map keystrokes to Python callbacks today. Common in-app editor shortcuts — Mod-s to save, Mod-r to run, F5 to refresh, plus things like Mod-c where you want to react without overriding the browser's native copy — currently require forking the JS or wiring up DOM-level keyboard listeners that fight CodeMirror's own keymap.

Implementation

Adds:

  • keybindings={key: handler} constructor parameter and matching editor.on_keybinding(key, handler) / editor.remove_keybinding(key) for runtime updates. Keys follow CodeMirror's keymap syntaxMod-s, F5, Mod-Shift-d, etc. (Mod is Cmd on macOS, Ctrl elsewhere).
  • ui.codemirror.binding(callback, ...) factory for per-binding overrides: prevent_default=False (notify Python while letting the browser default fire — e.g. for Mod-c) and per-platform mac= / linux= / win= shortcut overrides. The factory keeps the constructor and method surfaces shape-identical (both accept either a bare callable or the wrapped spec).

JS side wires user bindings into a Compartment-backed keymap, wrapped in CM.Prec.high(...) so they win over basicSetup's defaults (e.g. you can override Mod-z). The dispatcher emits a single keybinding event with the matched key; Python looks the handler up by key and calls it. The run callback returns prevent_default, so opting out cleanly forwards the event to lower-precedence bindings and the browser's native handling — Mod-c notifies Python and the browser still copies.

Progress

  • The PR title is a short phrase starting with a verb like "Add ...", "Fix ...", "Update ...", "Remove ...", etc.
  • The implementation is complete.
  • This PR does not address a security issue.
  • Pytests have been added/updated or are not necessary.
  • Documentation has been added/updated or is not necessary.
  • No breaking changes to the public API or migration steps are described above.

Surfaces CodeMirror 6's keymap as a Python-facing dict — `keybindings={key: handler}`
in the constructor plus `on_keybinding(key, handler)` and `remove_keybinding(key)` for
runtime updates. Keys follow CM6's keymap syntax (`Mod-s`, `F5`, `Mod-Shift-d`, etc.).

A new `ui.codemirror.binding(callback, ...)` factory wraps callbacks with per-binding
overrides: `prevent_default=False` (notify Python while letting the browser default
fire — e.g. for `Mod-c`) and `mac=`/`linux=`/`win=` for per-platform shortcuts. The
factory keeps constructor and method surfaces shape-identical (both accept either a
bare callable or the wrapped spec).

JS side wires user bindings into a `Compartment`-backed keymap wrapped in
`Prec.high(...)` so they win over `basicSetup`'s defaults. The dispatcher emits a
single `keybinding` event with the matched key; Python looks the handler up by key
and calls it. The `run` callback returns `prevent_default` so opting out cleanly
forwards the event to lower-precedence bindings and the browser's native handler.

Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
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.

1 participant