Skip to content

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

fix(editor): 支持 Tab 键缩进

tianma-if7682f2363ae6
diff --git a/apps/web/src/components/EditorPane.tsx b/apps/web/src/components/EditorPane.tsx
index c578cac2..14036ae2 100644
--- a/apps/web/src/components/EditorPane.tsx
+++ b/apps/web/src/components/EditorPane.tsx
@@ -162,7 +162,7 @@ import { NOTE_HTML_FULL_STYLES } from "@/lib/note-html-export-assets";
import { downloadNoteHtmlFile, getHtmlImageEmbedNoticeKind } from "@/lib/note-html-export";
import { openNotePrintPreview, serializeNoteDocumentForPrint } from "@/lib/note-print";
import type { NoteImageFormat } from "@/lib/note-image-export";
-import { getAiSlashCommandStart, saveAndSyncEditor, shouldOpenAiFromSpace } from "@/lib/editor-shortcuts";
+import { applyPlainTextTab, getAiSlashCommandStart, saveAndSyncEditor, shouldOpenAiFromSpace } from "@/lib/editor-shortcuts";
import {
AI_SPACE_SHORTCUT_CHANGED_EVENT,
readAiSpaceShortcutPreference,
@@ -1308,6 +1308,11 @@ const RichEditorPane = ({
handleKeyDown: (view, event) => {
const { selection } = view.state;
const currentNode = selection.$from.parent;
+ if (event.key === "Tab" && applyPlainTextTab(view.state, view.dispatch, event.shiftKey)) {
+ event.preventDefault();
+ return true;
+ }
+
if (aiSpaceShortcutEnabledRef.current && shouldOpenAiFromSpace({
altKey: event.altKey,
ctrlKey: event.ctrlKey,
diff --git a/apps/web/src/lib/editor-shortcuts.test.mjs b/apps/web/src/lib/editor-shortcuts.test.mjs
index 4d7502b7..81e9c69c 100644
--- a/apps/web/src/lib/editor-shortcuts.test.mjs
+++ b/apps/web/src/lib/editor-shortcuts.test.mjs
@@ -1,7 +1,80 @@
import { describe, expect, test } from "bun:test";
-import { getAiSlashCommandStart, saveAndSyncEditor, shouldOpenAiFromSpace } from "./editor-shortcuts.ts";
+import { EditorState, TextSelection } from "@tiptap/pm/state";
+import { Schema } from "@tiptap/pm/model";
+import { applyPlainTextTab, getAiSlashCommandStart, saveAndSyncEditor, shouldOpenAiFromSpace } from "./editor-shortcuts.ts";
+
+const schema = new Schema({
+ nodes: {
+ doc: { content: "block+" },
+ paragraph: { content: "text*", group: "block" },
+ codeBlock: { content: "text*", group: "block", code: true },
+ bulletList: { content: "listItem+", group: "block" },
+ listItem: { content: "paragraph block*" },
+ text: { group: "inline" },
+ },
+});
+
+const createState = (paragraphs, from, to = from) => {
+ const doc = schema.node("doc", null, paragraphs.map((text) =>
+ schema.node("paragraph", null, text ? schema.text(text) : null)
+ ));
+ return EditorState.create({ doc, selection: TextSelection.create(doc, from, to) });
+};
+
+const runTab = (state, shiftKey = false) => {
+ let nextState = state;
+ const handled = applyPlainTextTab(state, (transaction) => {
+ nextState = state.apply(transaction);
+ }, shiftKey);
+ return { handled, state: nextState };
+};
describe("editor shortcut actions", () => {
+ test("inserts a tab at the caret in a plain paragraph", () => {
+ const result = runTab(createState(["hello world"], 6));
+
+ expect(result.handled).toBe(true);
+ expect(result.state.doc.textContent).toBe("hello\t world");
+ });
+
+ test("indents each selected paragraph without replacing its text", () => {
+ const result = runTab(createState(["first", "second"], 2, 13));
+
+ expect(result.handled).toBe(true);
+ expect(result.state.doc.child(0).textContent).toBe("\tfirst");
+ expect(result.state.doc.child(1).textContent).toBe("\tsecond");
+ });
+
+ test("Shift-Tab removes indentation and keeps focus when no indentation remains", () => {
+ const indented = runTab(createState(["\thello"], 7), true);
+ const unchanged = runTab(createState(["hello"], 3), true);
+
+ expect(indented.handled).toBe(true);
+ expect(indented.state.doc.textContent).toBe("hello");
+ expect(unchanged.handled).toBe(true);
+ expect(unchanged.state.doc.textContent).toBe("hello");
+ });
+
+ test("inserts a tab in code blocks", () => {
+ const doc = schema.node("doc", null, [schema.node("codeBlock", null, schema.text("code"))]);
+ const state = EditorState.create({ doc, selection: TextSelection.create(doc, 3) });
+ const result = runTab(state);
+
+ expect(result.handled).toBe(true);
+ expect(result.state.doc.textContent).toBe("co\tde");
+ });
+
+ test("delegates list indentation to Tiptap's list keymap", () => {
+ const doc = schema.node("doc", null, [
+ schema.node("bulletList", null, [
+ schema.node("listItem", null, [schema.node("paragraph", null, schema.text("item"))]),
+ ]),
+ ]);
+ const state = EditorState.create({ doc, selection: TextSelection.create(doc, 4) });
+
+ expect(applyPlainTextTab(state, () => {}, false)).toBe(false);
+ });
+
test("recognizes /ai only at a text boundary", () => {
expect(getAiSlashCommandStart({ caretPosition: 2, insertedText: "i", textBefore: "/a" })).toBe(0);
expect(getAiSlashCommandStart({ caretPosition: 8, insertedText: "I", textBefore: "hello /a" })).toBe(6);
diff --git a/apps/web/src/lib/editor-shortcuts.ts b/apps/web/src/lib/editor-shortcuts.ts
index 33de4b8a..d626b7c4 100644
--- a/apps/web/src/lib/editor-shortcuts.ts
+++ b/apps/web/src/lib/editor-shortcuts.ts
@@ -1,3 +1,101 @@
+import type { EditorState, Transaction } from "@tiptap/pm/state";
+
+const TAB_DELEGATED_NODE_TYPES = new Set([
+ "listItem",
+ "taskItem",
+ "tableCell",
+ "tableHeader",
+]);
+
+const hasDelegatedTabAncestor = (state: EditorState, position: number) => {
+ const resolved = state.doc.resolve(position);
+ for (let depth = resolved.depth; depth >= 0; depth -= 1) {
+ if (TAB_DELEGATED_NODE_TYPES.has(resolved.node(depth).type.name)) {
+ return true;
+ }
+ }
+ return false;
+};
+
+const selectedTextBlockStarts = (state: EditorState) => {
+ const { from, to } = state.selection;
+ const starts: number[] = [];
+ const rangeEnd = Math.max(from, to - 1);
+
+ state.doc.nodesBetween(from, rangeEnd, (node, position) => {
+ if (!node.isTextblock) {
+ return true;
+ }
+ starts.push(position + 1);
+ return false;
+ });
+
+ return starts;
+};
+
+/**
+ * Handles Tab as text indentation while leaving structural contexts to
+ * Tiptap's list and table keymaps.
+ */
+export const applyPlainTextTab = (
+ state: EditorState,
+ dispatch: ((transaction: Transaction) => void) | undefined,
+ shiftKey = false,
+) => {
+ const { selection } = state;
+ if (
+ !selection.$from.parent.isTextblock
+ || !selection.$to.parent.isTextblock
+ || hasDelegatedTabAncestor(state, selection.from)
+ || hasDelegatedTabAncestor(state, selection.to)
+ ) {
+ return false;
+ }
+
+ if (!dispatch) {
+ return true;
+ }
+
+ if (selection.empty) {
+ if (!shiftKey) {
+ dispatch(state.tr.insertText("\t", selection.from));
+ return true;
+ }
+
+ const blockStart = selection.$from.start();
+ const precedingCharacter = selection.from > blockStart
+ ? state.doc.textBetween(selection.from - 1, selection.from)
+ : "";
+ if (precedingCharacter === "\t") {
+ dispatch(state.tr.delete(selection.from - 1, selection.from));
+ return true;
+ }
+
+ if (state.doc.textBetween(blockStart, blockStart + 1) === "\t") {
+ dispatch(state.tr.delete(blockStart, blockStart + 1));
+ }
+ return true;
+ }
+
+ const blockStarts = selectedTextBlockStarts(state);
+ if (blockStarts.some((position) => hasDelegatedTabAncestor(state, position))) {
+ return false;
+ }
+
+ const transaction = state.tr;
+ for (const position of blockStarts.reverse()) {
+ if (shiftKey) {
+ if (state.doc.textBetween(position, position + 1) === "\t") {
+ transaction.delete(position, position + 1);
+ }
+ } else {
+ transaction.insertText("\t", position);
+ }
+ }
+ dispatch(transaction);
+ return true;
+};
+
export const saveAndSyncEditor = async ({
hasUnsavedChanges,
save,
diff --git a/apps/web/src/styles/globals.css b/apps/web/src/styles/globals.css
index f959d8bf..d765d0e9 100644
--- a/apps/web/src/styles/globals.css
+++ b/apps/web/src/styles/globals.css
@@ -505,6 +505,8 @@ textarea {
min-height: 100%;
outline: none;
padding: 8px 0 56px;
+ tab-size: 4;
+ white-space: pre-wrap;
}
.edgeever-public-share .ProseMirror {