Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

fix: wrap multiline selections in one code block

tianma58f845354d87
diff --git a/apps/web/src/components/EditorToolbar.tsx b/apps/web/src/components/EditorToolbar.tsx
index 59760894..cb66bfe2 100644
--- a/apps/web/src/components/EditorToolbar.tsx
+++ b/apps/web/src/components/EditorToolbar.tsx
@@ -54,6 +54,28 @@ const ToolbarDivider = () => <div className="h-6 w-px shrink-0 bg-slate-200" />;
const isToolbarEditorReady = (editor: Editor | null): editor is Editor =>
Boolean(editor && !editor.isDestroyed && (editor as { extensionManager?: unknown }).extensionManager);
+const toggleCodeBlock = (editor: Editor) => {
+ const { from, to, empty } = editor.state.selection;
+ const selectedText = editor.state.doc.textBetween(from, to, "\n", "\n");
+
+ if (empty || !selectedText.includes("\n")) {
+ editor.chain().focus().toggleCodeBlock().run();
+ return;
+ }
+
+ editor
+ .chain()
+ .focus()
+ .insertContentAt(
+ { from, to },
+ {
+ type: "codeBlock",
+ content: selectedText ? [{ type: "text", text: selectedText }] : undefined,
+ }
+ )
+ .run();
+};
+
export const EditorToolbar = ({ editor, readOnly }: { editor: Editor | null; readOnly: boolean }) => {
const editorReady = isToolbarEditorReady(editor);
const disabled = readOnly || !editorReady;
@@ -223,7 +245,7 @@ export const EditorToolbar = ({ editor, readOnly }: { editor: Editor | null; rea
title="代码块"
active={isActive("codeBlock")}
disabled={disabled}
- onClick={() => run((current) => current.chain().focus().toggleCodeBlock().run())}
+ onClick={() => run(toggleCodeBlock)}
>
<SquareCode className="h-4 w-4" />
</EditorToolbarButton>