Skip to content

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

feat(editor): open AI from empty block Space (#236)

tianma-iffef39f1a2421
diff --git a/apps/web/src/components/EditorPane.tsx b/apps/web/src/components/EditorPane.tsx
index f0483ba2..70ce1866 100644
--- a/apps/web/src/components/EditorPane.tsx
+++ b/apps/web/src/components/EditorPane.tsx
@@ -144,7 +144,7 @@ import { downloadMarkdownFile } from "@/lib/note-markdown-export";
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 { getAiSlashCommandStart, saveAndSyncEditor } from "@/lib/editor-shortcuts";
+import { getAiSlashCommandStart, saveAndSyncEditor, shouldOpenAiFromSpace } from "@/lib/editor-shortcuts";
import { isBrowserOffline } from "@/lib/network-status";
import {
EDITOR_LINK_OPEN_MODE_CHANGED_EVENT,
@@ -1206,6 +1206,25 @@ const RichEditorPane = ({
class: "prose prose-slate max-w-none focus:outline-none min-h-[300px] px-4 py-3 sm:px-7",
},
handleKeyDown: (view, event) => {
+ const { selection } = view.state;
+ const currentNode = selection.$from.parent;
+ if (shouldOpenAiFromSpace({
+ altKey: event.altKey,
+ ctrlKey: event.ctrlKey,
+ isComposing: event.isComposing,
+ isEmptyParagraph: currentNode.type.name === "paragraph" && currentNode.content.size === 0,
+ key: event.key,
+ keyCode: event.keyCode,
+ metaKey: event.metaKey,
+ repeat: event.repeat,
+ selectionEmpty: selection.empty,
+ shiftKey: event.shiftKey,
+ })) {
+ event.preventDefault();
+ window.requestAnimationFrame(() => openAiAssistantRef.current());
+ return true;
+ }
+
const shortcutKey = event.key.toLowerCase();
if ((event.ctrlKey || event.metaKey) && !event.altKey && !event.shiftKey && shortcutKey === "k") {
event.preventDefault();
@@ -1222,7 +1241,7 @@ const RichEditorPane = ({
return false;
}
- const { from, to } = view.state.selection;
+ const { from, to } = selection;
if (from === to) {
return false;
}
diff --git a/apps/web/src/lib/editor-shortcuts.test.mjs b/apps/web/src/lib/editor-shortcuts.test.mjs
index da94c1ee..4d7502b7 100644
--- a/apps/web/src/lib/editor-shortcuts.test.mjs
+++ b/apps/web/src/lib/editor-shortcuts.test.mjs
@@ -1,5 +1,5 @@
import { describe, expect, test } from "bun:test";
-import { getAiSlashCommandStart, saveAndSyncEditor } from "./editor-shortcuts.ts";
+import { getAiSlashCommandStart, saveAndSyncEditor, shouldOpenAiFromSpace } from "./editor-shortcuts.ts";
describe("editor shortcut actions", () => {
test("recognizes /ai only at a text boundary", () => {
@@ -9,6 +9,29 @@ describe("editor shortcut actions", () => {
expect(getAiSlashCommandStart({ caretPosition: 2, insertedText: "x", textBefore: "/a" })).toBeNull();
});
+ test("opens AI from Space only in an empty paragraph outside IME composition", () => {
+ const base = {
+ altKey: false,
+ ctrlKey: false,
+ isComposing: false,
+ isEmptyParagraph: true,
+ key: " ",
+ keyCode: 32,
+ metaKey: false,
+ repeat: false,
+ selectionEmpty: true,
+ shiftKey: false,
+ };
+
+ expect(shouldOpenAiFromSpace(base)).toBe(true);
+ expect(shouldOpenAiFromSpace({ ...base, isEmptyParagraph: false })).toBe(false);
+ expect(shouldOpenAiFromSpace({ ...base, selectionEmpty: false })).toBe(false);
+ expect(shouldOpenAiFromSpace({ ...base, isComposing: true })).toBe(false);
+ expect(shouldOpenAiFromSpace({ ...base, keyCode: 229 })).toBe(false);
+ expect(shouldOpenAiFromSpace({ ...base, ctrlKey: true })).toBe(false);
+ expect(shouldOpenAiFromSpace({ ...base, key: "Enter" })).toBe(false);
+ });
+
test("saves dirty editor content before starting sync", async () => {
const calls = [];
diff --git a/apps/web/src/lib/editor-shortcuts.ts b/apps/web/src/lib/editor-shortcuts.ts
index ed0c3e69..33de4b8a 100644
--- a/apps/web/src/lib/editor-shortcuts.ts
+++ b/apps/web/src/lib/editor-shortcuts.ts
@@ -29,3 +29,36 @@ export const getAiSlashCommandStart = ({
return caretPosition - 2;
};
+
+export const shouldOpenAiFromSpace = ({
+ altKey,
+ ctrlKey,
+ isComposing,
+ isEmptyParagraph,
+ key,
+ keyCode,
+ metaKey,
+ repeat,
+ selectionEmpty,
+ shiftKey,
+}: {
+ altKey: boolean;
+ ctrlKey: boolean;
+ isComposing: boolean;
+ isEmptyParagraph: boolean;
+ key: string;
+ keyCode: number;
+ metaKey: boolean;
+ repeat: boolean;
+ selectionEmpty: boolean;
+ shiftKey: boolean;
+}) => key === " "
+ && !altKey
+ && !ctrlKey
+ && !metaKey
+ && !shiftKey
+ && !repeat
+ && !isComposing
+ && keyCode !== 229
+ && selectionEmpty
+ && isEmptyParagraph;
diff --git a/packages/shared/src/i18n/en-US.ts b/packages/shared/src/i18n/en-US.ts
index 87976df1..42e9d33b 100644
--- a/packages/shared/src/i18n/en-US.ts
+++ b/packages/shared/src/i18n/en-US.ts
@@ -943,7 +943,7 @@ export const enUS = {
deleteDescription: "The attachment will be permanently removed from storage and this note. This cannot be undone.",
failed: "The attachment action failed. Try again.",
},
- placeholder: "Start writing...",
+ placeholder: "Press Space for AI, or type / for commands",
noteBodyAria: "Note body",
markdownSourceAria: "Markdown source",
loading: "Loading",
diff --git a/packages/shared/src/i18n/zh-CN.ts b/packages/shared/src/i18n/zh-CN.ts
index 1b7ea44a..f765866f 100644
--- a/packages/shared/src/i18n/zh-CN.ts
+++ b/packages/shared/src/i18n/zh-CN.ts
@@ -943,7 +943,7 @@ export const zhCN = {
deleteDescription: "附件会从存储空间和当前笔记中永久删除,此操作无法撤销。",
failed: "附件操作失败,请重试。",
},
- placeholder: "开始记录...",
+ placeholder: "按 Space 使用 AI,输入 / 浏览命令",
noteBodyAria: "笔记正文",
markdownSourceAria: "Markdown 源码",
loading: "加载中",
diff --git a/tests/e2e/ai-custom-prompts.spec.ts b/tests/e2e/ai-custom-prompts.spec.ts
index c25065fd..f8c95e87 100644
--- a/tests/e2e/ai-custom-prompts.spec.ts
+++ b/tests/e2e/ai-custom-prompts.spec.ts
@@ -183,6 +183,33 @@ test.describe("AI custom prompts", () => {
await expect(assistant).toBeVisible();
});
+ test("opens AI from Space in an empty block without hijacking normal spaces", async ({ page }) => {
+ const memo = await createMemo(page, `e2e-ai-space-${Date.now()}`, "空格入口测试");
+ await ensureAuthenticatedPage(page);
+ await page.getByRole("button", { name: new RegExp(notebookName) }).click();
+ await page.locator(`[data-memo-id="${memo.id}"]`).locator("button").first().click();
+ const editor = page.locator(".ProseMirror[contenteditable='true']");
+ await expect(editor).toBeVisible();
+
+ await editor.click();
+ await page.keyboard.press("End");
+ await page.keyboard.press("Enter");
+ const emptyParagraph = editor.locator("p").last();
+ await expect(emptyParagraph).toHaveClass(/is-empty/);
+ await expect(emptyParagraph).toHaveAttribute("data-placeholder", "按 Space 使用 AI,输入 / 浏览命令");
+
+ await page.keyboard.press("Space");
+ const assistant = page.getByRole("dialog", { name: "AI 笔记助手" });
+ await expect(assistant).toBeVisible();
+ await expect(emptyParagraph).toBeEmpty();
+
+ await assistant.getByRole("button", { name: "关闭" }).click();
+ await emptyParagraph.click();
+ await page.keyboard.type("正常 输入");
+ await expect(assistant).toBeHidden();
+ await expect(emptyParagraph).toHaveText("正常 输入");
+ });
+
test("sends temporary files with one AI request", async ({ page }) => {
const memo = await createMemo(page, `e2e-ai-attachment-${Date.now()}`, "请结合附件处理。 ");
let submittedBody: Record<string, unknown> | null = null;