From a0039b2a09efbe8b56f03a99682485ef57f55745 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Fri, 20 Mar 2026 23:25:43 -0400 Subject: [PATCH] Add keyboard access to mermaid diagram editing (#11834) --- app/editor/index.tsx | 2 +- app/editor/menus/code.tsx | 2 ++ shared/editor/extensions/Mermaid.ts | 25 +++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/app/editor/index.tsx b/app/editor/index.tsx index 8d9170b379..d115975706 100644 --- a/app/editor/index.tsx +++ b/app/editor/index.tsx @@ -443,8 +443,8 @@ export class Editor extends React.PureComponent< schema: this.schema, doc, plugins: [ - ...this.keymaps, ...this.plugins, + ...this.keymaps, anchorPlugin(), dropCursor({ color: this.props.theme.cursor, diff --git a/app/editor/menus/code.tsx b/app/editor/menus/code.tsx index fd1979b66b..f3d3826542 100644 --- a/app/editor/menus/code.tsx +++ b/app/editor/menus/code.tsx @@ -14,6 +14,7 @@ import { import { isMermaid } from "@shared/editor/lib/isCode"; import type { MenuItem } from "@shared/editor/types"; import type { Dictionary } from "~/hooks/useDictionary"; +import { metaDisplay } from "@shared/utils/keyboard"; export default function codeMenuItems( state: EditorState, @@ -67,6 +68,7 @@ export default function codeMenuItems( name: "edit_mermaid", icon: , tooltip: dictionary.editDiagram, + shortcut: `${metaDisplay} Enter`, visible: isMermaid(node) && !isEditingMermaid && !readOnly, }, { diff --git a/shared/editor/extensions/Mermaid.ts b/shared/editor/extensions/Mermaid.ts index 644f13b50e..1361af6513 100644 --- a/shared/editor/extensions/Mermaid.ts +++ b/shared/editor/extensions/Mermaid.ts @@ -439,6 +439,31 @@ export default function Mermaid({ decorations(state) { return this.getState(state)?.decorationSet; }, + handleKeyDown(view, event) { + if (event.key === "Enter" && event.metaKey && !editor.props.readOnly) { + const { selection } = view.state; + const isNodeSel = selection instanceof NodeSelection; + const isMermaidNode = + isNodeSel && isMermaid((selection as NodeSelection).node); + if (isNodeSel && isMermaidNode) { + editor.commands.edit_mermaid(); + return true; + } + } + + if (event.key === "Escape") { + const mermaidState = pluginKey.getState(view.state) as MermaidState; + const codeBlock = findParentNode(isCode)(view.state.selection); + + if (mermaidState?.editingId) { + if (codeBlock && isMermaid(codeBlock.node)) { + editor.commands.edit_mermaid(); + return true; + } + } + } + return false; + }, handleDOMEvents: { click(_view, event: MouseEvent) { const target = event.target as HTMLElement;