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): convert Markdown pasted with Shift+Insert

tianma-if28b581d8f2ee
diff --git a/apps/web/src/lib/markdown-text-paste.test.mjs b/apps/web/src/lib/markdown-text-paste.test.mjs
index 240500a7..2a67dbe8 100644
--- a/apps/web/src/lib/markdown-text-paste.test.mjs
+++ b/apps/web/src/lib/markdown-text-paste.test.mjs
@@ -21,12 +21,13 @@ const clipboard = (entries) => ({
// The unmounted Tiptap view throws on fields it does not have, such as
// `input`, so tests pass the state, dispatch and Shift state ProseMirror keeps.
-const viewOf = (editor, shiftKey = false) => ({
+const viewOf = (editor, shiftKey = false, lastKeyCode = 86) => ({
get state() { return editor.state; },
dispatch: (transaction) => editor.view.dispatch(transaction),
- input: { shiftKey },
+ input: { shiftKey, lastKeyCode },
});
-const paste = (editor, entries, shiftKey) => pasteBlockMarkdown(viewOf(editor, shiftKey), clipboard(entries));
+const paste = (editor, entries, shiftKey, lastKeyCode) =>
+ pasteBlockMarkdown(viewOf(editor, shiftKey, lastKeyCode), clipboard(entries));
// Excerpt of the sample attached to issue #660.
const ISSUE_SAMPLE = `# 一级标题
@@ -174,4 +175,24 @@ describe("pasting plain-text Markdown in rich text", () => {
expect(editor.getJSON().content.map((node) => node.type)).toContain("table");
editor.destroy();
});
+
+ test("converts Shift+Insert pastes for plain text and VS Code Markdown", () => {
+ for (const entries of [
+ { "text/plain": "# title\n- item" },
+ {
+ "text/plain": "# title\n- item",
+ "text/html": "<div># title</div>",
+ "vscode-editor-data": JSON.stringify({ mode: "markdown" }),
+ },
+ ]) {
+ const editor = createEditor();
+ try {
+ expect(paste(editor, entries, true, 45)).toBe(true);
+ expect(editor.getJSON().content.map((node) => node.type)).toEqual(["heading", "bulletList"]);
+ expect(editor.getText()).not.toContain("# title");
+ } finally {
+ editor.destroy();
+ }
+ }
+ });
});
diff --git a/apps/web/src/lib/markdown-text-paste.ts b/apps/web/src/lib/markdown-text-paste.ts
index 21843a6d..98635121 100644
--- a/apps/web/src/lib/markdown-text-paste.ts
+++ b/apps/web/src/lib/markdown-text-paste.ts
@@ -58,8 +58,10 @@ export const pasteBlockMarkdown = (
view: { state: EditorState; dispatch: (transaction: Transaction) => void },
data: DataTransfer | null,
) => {
- // ProseMirror tracks Shift for Cmd/Ctrl+Shift+V, which pastes the literal text.
- if ((view as { input?: { shiftKey?: boolean } }).input?.shiftKey) return false;
+ // Match ProseMirror's plain-text paste check: Shift+Insert (45) is a normal
+ // paste, while Cmd/Ctrl+Shift+V keeps the literal text.
+ const input = (view as { input?: { shiftKey?: boolean; lastKeyCode?: number | null } }).input;
+ if (input?.shiftKey && input.lastKeyCode !== 45) return false;
if (!data || !isPlainMarkdownSource(data)) return false;
const { $from } = view.state.selection;
if ($from.parent.type.spec.code) return false;
diff --git a/tests/e2e/markdown-text-paste.spec.ts b/tests/e2e/markdown-text-paste.spec.ts
index 491743da..f277a798 100644
--- a/tests/e2e/markdown-text-paste.spec.ts
+++ b/tests/e2e/markdown-text-paste.spec.ts
@@ -27,10 +27,9 @@ const dispatchPaste = async (editor: Locator, entries: Record<string, string>) =
}, entries);
};
-const openMemo = async (page: Page, memo: { id: string }, title: string) => {
+const openMemo = async (page: Page, memo: { id: string }) => {
await page.goto("/");
await page.getByRole("button", { name: "全部笔记", exact: true }).click();
- await page.getByPlaceholder("搜索笔记").fill(title);
await page.locator(`[data-memo-id="${memo.id}"]`).locator("button").first().click();
const editor = page.locator(".ProseMirror[contenteditable='true']");
await expect(editor).toBeEditable();
@@ -66,7 +65,7 @@ test("renders Markdown pasted as plain text in the rich-text editor", async ({ p
const memo = await createMemo(page, title);
try {
- const editor = await openMemo(page, memo, title);
+ const editor = await openMemo(page, memo);
await dispatchPaste(editor, { "text/plain": MARKDOWN });
await expect(editor.locator("h1")).toHaveText("一级标题");
@@ -118,7 +117,7 @@ test("keeps escaped delimiters, paste as plain text and tables without outer pip
const memo = await createMemo(page, title);
try {
- const editor = await openMemo(page, memo, title);
+ const editor = await openMemo(page, memo);
// Escaped delimiters stay literal; paste rules do not parse the result again.
await dispatchPaste(editor, { "text/plain": "# Escaped\n\n\\*not italic\\*\n\n\\`not code\\`" });
@@ -163,3 +162,48 @@ test("keeps escaped delimiters, paste as plain text and tables without outer pip
await deleteMemo(page, memo.id);
}
});
+
+test("converts Shift+Insert Markdown pastes and keeps Ctrl+Shift+V literal", async ({ page }) => {
+ const title = `Markdown Shift+Insert ${Date.now()}`;
+ const memo = await createMemo(page, title);
+ try {
+ const editor = await openMemo(page, memo);
+ for (const vscode of [false, true]) {
+ const entries: Record<string, string> = { "text/plain": "# Insert heading\n- Insert item" };
+ if (vscode) {
+ entries["text/html"] = "<div># Insert heading</div>";
+ entries["vscode-editor-data"] = JSON.stringify({ mode: "markdown" });
+ }
+ // Deliver the shortcut key state without reading the host clipboard.
+ await editor.dispatchEvent("keydown", {
+ key: "Insert", code: "Insert", keyCode: 45, shiftKey: true,
+ });
+ await dispatchPaste(editor, entries);
+ await editor.dispatchEvent("keyup", { key: "Shift", code: "ShiftLeft", keyCode: 16 });
+ await expect(editor.locator("h1")).toHaveText("Insert heading");
+ await expect(editor.locator("ul > li")).toHaveText("Insert item");
+ await expect(editor).not.toContainText("# Insert heading");
+ await page.keyboard.press("ControlOrMeta+z");
+ await expect(editor).toHaveText("");
+ await page.keyboard.press("ControlOrMeta+Shift+z");
+ await expect(editor.locator("h1")).toHaveText("Insert heading");
+ await expect(editor.locator("ul > li")).toHaveText("Insert item");
+ await page.keyboard.press("ControlOrMeta+z");
+ await expect(editor).toHaveText("");
+
+ // Update the last key after Insert: Shift+V must still take the plain path.
+ await editor.dispatchEvent("keydown", {
+ key: "V", code: "KeyV", keyCode: 86, ctrlKey: true, shiftKey: true,
+ });
+ await dispatchPaste(editor, entries);
+ await editor.dispatchEvent("keyup", { key: "Shift", code: "ShiftLeft", keyCode: 16 });
+ await expect(editor).toContainText("# Insert heading");
+ await expect(editor).toContainText("- Insert item");
+ await expect(editor.locator("h1, ul")).toHaveCount(0);
+ await page.keyboard.press("ControlOrMeta+z");
+ await expect(editor).toHaveText("");
+ }
+ } finally {
+ await deleteMemo(page, memo.id);
+ }
+});