### Stacking Keymaps for Precedence in ProseMirror Source: https://context7.com/prosemirror/prosemirror-keymap/llms.txt Shows how to layer multiple keymap plugins to control shortcut priority. The plugin listed first in the `plugins` array takes precedence. ```typescript import { EditorState } from "prosemirror-state" import { EditorView } from "prosemirror-view" import { schema } from "prosemirror-schema-basic" import { baseKeymap } from "prosemirror-commands" import { keymap } from "prosemirror-keymap" // High-priority application shortcuts const appKeymap = keymap({ "Mod-s": (state, dispatch) => { console.log("App-level save — handled first") return true }, "Mod-Enter": (state, dispatch) => { console.log("Submit form") return true }, }) // Lower-priority editing shortcuts const editKeymap = keymap({ "Enter": (state, dispatch) => { // Split paragraph on Enter const { $from } = state.selection if (dispatch) { dispatch(state.tr.split($from.pos)) } return true }, }) // baseKeymap provides sensible defaults (Enter, Backspace, Delete, etc.) const state = EditorState.create({ schema, plugins: [ appKeymap, // checked first editKeymap, // checked second keymap(baseKeymap), // fallback defaults ], }) const view = new EditorView(document.querySelector("#editor")!, { state }) ``` -------------------------------- ### keymap(bindings) Source: https://context7.com/prosemirror/prosemirror-keymap/llms.txt Creates a ProseMirror `Plugin` instance from a map of key bindings. This plugin can be added to an editor's plugin list. Earlier plugins in the list have higher precedence. ```APIDOC ## keymap(bindings) ### Description Accepts a plain object mapping key name strings to ProseMirror commands and returns a `Plugin` instance ready to be added to an editor's plugin list. Multiple keymap plugins can be stacked; earlier plugins in the array have higher precedence. ### Parameters - **bindings** (Object) - An object where keys are key name strings (e.g., \"Mod-Enter\") and values are ProseMirror command functions. ### Returns - **Plugin** - A ProseMirror Plugin instance. ``` -------------------------------- ### Create a Keymap Plugin with `keymap` Source: https://context7.com/prosemirror/prosemirror-keymap/llms.txt Use `keymap` to create a ProseMirror plugin that binds keyboard shortcuts to commands. Define bindings in a plain object, mapping key strings like 'Mod-z' to command functions. Earlier plugins in the editor's plugin list have higher precedence. ```typescript import { EditorState } from "prosemirror-state" import { EditorView } from "prosemirror-view" import { schema } from "prosemirror-schema-basic" import { undo, redo } from "prosemirror-history" import { toggleMark } from "prosemirror-commands" import { keymap } from "prosemirror-keymap" // Define bindings using Mod- for cross-platform Cmd/Ctrl shortcuts const myKeymap = keymap({ // Undo / redo "Mod-z": undo, "Mod-y": redo, "Shift-Mod-z": redo, // Inline marks — Mod- resolves to Cmd- on Mac, Ctrl- elsewhere "Mod-b": toggleMark(schema.marks.strong), "Mod-i": toggleMark(schema.marks.em), "Mod-u": toggleMark(schema.marks.underline), // Custom command: insert a hard break on Shift-Enter "Shift-Enter": (state, dispatch) => { if (dispatch) { dispatch(state.tr.replaceSelectionWith(schema.nodes.hard_break.create()).scrollIntoView()) } return true }, // Space as an alias for the " " key "Ctrl-Space": (state, dispatch, view) => { console.log("Ctrl+Space pressed, view:", view) return false // returning false lets other keymaps handle it }, }) // Attach to an EditorView — earlier plugins take precedence const state = EditorState.create({ schema, plugins: [myKeymap], }) const view = new EditorView(document.querySelector("#editor"), { state }) ``` -------------------------------- ### Key Name Syntax and Aliases in ProseMirror Source: https://context7.com/prosemirror/prosemirror-keymap/llms.txt Demonstrates various ways to define key bindings using modifier aliases and special keys. Note that duplicate normalized bindings within a single keymap() call will throw an Error. ```typescript import { keymap } from "prosemirror-keymap" import { schema } from "prosemirror-schema-basic" const bindingsDemo = keymap({ // --- Modifier shorthands (all equivalent pairs) --- "Shift-Enter": () => { console.log("Shift+Enter"); return true }, "s-Enter": () => { console.log("Shift+Enter (short)"); return true }, // same key — would conflict! // Mod- → Cmd- on macOS, Ctrl- on Windows/Linux "Mod-k": () => { console.log("Cmd/Ctrl + k"); return true }, // Explicit platform modifiers "Ctrl-Alt-Delete": () => { console.log("Ctrl+Alt+Del"); return true }, "Meta-Shift-p": () => { console.log("Cmd+Shift+p (Mac only)"); return true }, // --- Special keys --- "Space": () => { console.log("Spacebar"); return true }, "ArrowUp": () => { console.log("Up arrow"); return true }, "F5": () => { console.log("F5"); return true }, "Backspace": () => { console.log("Backspace"); return true }, "Enter": () => { console.log("Enter"); return true }, // --- Shifted characters (Shift- is implied) --- // Binding "%" is equivalent to "Shift-5" on a US keyboard. // The handler resolves this automatically via keyCode fallback. "Ctrl-%": () => { console.log("Ctrl+Shift+5"); return true }, // --- Multiple modifiers (order in string does not matter) --- "Shift-Ctrl-Alt-x": () => { console.log("All three + x"); return true }, "Alt-Ctrl-Shift-x": () => { console.log("Same binding, different order"); return true }, // conflict! }) // Note: duplicate normalized bindings within a single keymap() call throw an Error. ``` -------------------------------- ### keydownHandler(bindings) Source: https://context7.com/prosemirror/prosemirror-keymap/llms.txt Returns a raw keydown event handler function. This is useful for embedding keymap logic within custom plugins or composing multiple handlers. ```APIDOC ## keydownHandler(bindings) ### Description Returns a raw `(view: EditorView, event: KeyboardEvent) => boolean` function instead of a full plugin. Use this when you need to embed keymap logic inside a custom plugin's `handleKeyDown` prop, compose multiple handlers, or share a handler between different plugin instances without creating separate `Plugin` objects. ### Parameters - **bindings** (Object) - An object where keys are key name strings (e.g., \"Mod-Enter\") and values are ProseMirror command functions. ### Returns - **Function** - A keydown event handler function `(view: EditorView, event: KeyboardEvent) => boolean`. ``` -------------------------------- ### Create a Standalone Keydown Handler with `keydownHandler` Source: https://context7.com/prosemirror/prosemirror-keymap/llms.txt Use `keydownHandler` to create a reusable keydown event handler function. This is useful for embedding keymap logic within custom plugins or sharing handlers across instances. The handler returns `true` if it consumed the event, `false` otherwise. ```typescript import { Plugin } from "prosemirror-state" import { EditorView } from "prosemirror-view" import { schema } from "prosemirror-schema-basic" import { keydownHandler } from "prosemirror-keymap" // Build a reusable handler const handler = keydownHandler({ "Mod-s": (state, dispatch) => { // Trigger a save action without a dispatch (read-only check) if (!dispatch) return true console.log("Saving document…") // e.g. call an external save function here return true }, "Escape": (state, dispatch, view) => { // Blur the editor on Escape ;(view as EditorView).dom.blur() return true }, }) // Embed inside a custom plugin alongside other handleKeyDown logic const customPlugin = new Plugin({ props: { handleKeyDown(view, event) { // Run custom pre-processing… if (event.key === "Tab") { event.preventDefault() return true } // Delegate remaining keys to the keymap handler return handler(view, event) }, }, }) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.