AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.
HTML ai-agentcloudflarecloudflare-d1cloudflare-r2cloudflare-workersdockerelectronevernoteevernote-alternativeknowledge-basemcpnote-takingobsidian-alternativeoffline-firstpersonal-knowledge-basepwaself-hostedsqlitetiptapweb-clipper
fix(editor): prevent excessive blank lines in copied notes
tianma-if958f0acff80a
diff --git a/apps/web/src/components/EditorPane.tsx b/apps/web/src/components/EditorPane.tsxindex 17b3c612..a31d5ff5 100644--- a/apps/web/src/components/EditorPane.tsx+++ b/apps/web/src/components/EditorPane.tsx@@ -1,4 +1,5 @@import { AI_ASSISTANT_LAUNCHER_POSITION } from "@/lib/ai-assistant-launcher";+import { editorClipboardTextSerializer } from "@/lib/editor-clipboard";import { useRef, useState, useEffect, useLayoutEffect, useCallback, useMemo, lazy, Suspense, type CSSProperties, type FocusEvent as ReactFocusEvent, type MouseEvent as ReactMouseEvent, type ReactNode } from "react";import { useQuery, useQueryClient, useMutation } from "@tanstack/react-query";import { useEditor, EditorContent, type Editor } from "@tiptap/react";@@ -1286,6 +1287,7 @@ const RichEditorPane = ({: { type: "doc", content: [{ type: "paragraph" }] },editable: Boolean(memo && !effectiveReadOnly && editorIsHydratedForCurrentMemo),editorProps: {+ clipboardTextSerializer: editorClipboardTextSerializer,attributes: {class: "edgeever-note-rich-editor prose prose-slate max-w-none focus:outline-none min-h-[240px] lg:min-h-[180px]",spellcheck: spellcheckEnabled ? "true" : "false",diff --git a/apps/web/src/lib/editor-clipboard.test.mjs b/apps/web/src/lib/editor-clipboard.test.mjsnew file mode 100644index 00000000..5928b114--- /dev/null+++ b/apps/web/src/lib/editor-clipboard.test.mjs@@ -0,0 +1,69 @@+import { expect, test } from "bun:test";+import { getTextBetween } from "@tiptap/core";+import { Schema } from "@tiptap/pm/model";+import { AllSelection, EditorState, TextSelection } from "@tiptap/pm/state";+import { CellSelection, tableNodes } from "@tiptap/pm/tables";+import { editorClipboardTextSerializer, serializeEditorTextRange } from "./editor-clipboard.ts";++const schema = new Schema({+ nodes: {+ doc: { content: "block+" },+ text: { group: "inline" },+ paragraph: { content: "inline*", group: "block" },+ blockquote: { content: "block+", group: "block" },+ bulletList: { content: "listItem+", group: "block" },+ listItem: { content: "paragraph block*" },+ codeBlock: { content: "text*", group: "block", code: true },+ hardBreak: { inline: true, group: "inline", toText: () => "\n" },+ ...tableNodes({ tableGroup: "block", cellContent: "block+" }),+ },+});+const node = (type, content = []) => schema.node(type, null, content);+const p = (text) => node("paragraph", text ? [schema.text(text)] : []);+const item = (text) => node("listItem", [p(text)]);+const copy = (doc, selection = new AllSelection(doc)) => editorClipboardTextSerializer(+ selection.content(), { state: EditorState.create({ doc, selection }) },+);++test("quoted lists reproduce accumulated default whitespace and copy with one newline per text block", () => {+ const doc = node("doc", [node("blockquote", [+ p("4 个月的数据:"),+ node("bulletList", [item("GitHub 突破 3,000 Star"), item("全服第一")]),+ p("鸣谢背后的神仙战友:"),+ node("bulletList", [item("感谢小伙伴"), item("感谢波兰和德国老哥")]),+ ])]);+ expect(getTextBetween(doc, { from: 0, to: doc.content.size })).toContain("\n\n\n\n");+ expect(copy(doc)).toBe("4 个月的数据:\nGitHub 突破 3,000 Star\n全服第一\n鸣谢背后的神仙战友:\n感谢小伙伴\n感谢波兰和德国老哥");+});++test("nested lists and quotes do not create leading or repeated separators", () => {+ const doc = node("doc", [node("blockquote", [node("bulletList", [+ node("listItem", [p("Parent"), node("bulletList", [item("Child")])]), item("Next"),+ ])])]);+ expect(copy(doc)).toBe("Parent\nChild\nNext");+});++test("intentional empty paragraphs, hard breaks and code whitespace survive", () => {+ const doc = node("doc", [p("First"), p(""),+ node("paragraph", [schema.text("A"), node("hardBreak"), schema.text("B")]),+ node("codeBlock", [schema.text(" code\n\n indented")]), p(""),+ ]);+ expect(copy(doc)).toBe("First\n\nA\nB\n code\n\n indented\n");+});++test("partial selections copy only selected text, including across paragraphs", () => {+ const doc = node("doc", [p("Alpha"), p("Beta")]);+ expect(copy(doc, TextSelection.create(doc, 3, 10))).toBe("pha\nBe");+ expect(serializeEditorTextRange(doc, 2, 4)).toBe("lp");+});++test("table cell selections omit unselected cells and keep document order", () => {+ const cell = (text) => node("table_cell", [p(text)]);+ const doc = node("doc", [node("table", [+ node("table_row", [cell("A"), cell("skip1")]),+ node("table_row", [cell("B"), cell("skip2")]),+ ])]);+ const positions = [];+ doc.descendants((n, pos) => { if (n.type.name === "table_cell") positions.push(pos); });+ expect(copy(doc, CellSelection.create(doc, positions[2], positions[0]))).toBe("A\nB");+});diff --git a/apps/web/src/lib/editor-clipboard.ts b/apps/web/src/lib/editor-clipboard.tsnew file mode 100644index 00000000..d529e84b--- /dev/null+++ b/apps/web/src/lib/editor-clipboard.ts@@ -0,0 +1,36 @@+import { getTextSerializersFromSchema, type TextSerializer } from "@tiptap/core";+import type { Node } from "@tiptap/pm/model";+import type { EditorProps } from "@tiptap/pm/view";++/** Container nodes (quotes, lists, items) must not each add a blank line. */+export const serializeEditorTextRange = (doc: Node, from: number, to: number): string => {+ const serializers: Partial<Record<string, TextSerializer>> = getTextSerializersFromSchema(doc.type.schema);+ let text = "";+ let firstBlock = true;+ doc.nodesBetween(from, to, (node, pos, parent, index) => {+ const serializer = serializers[node.type.name];+ if (node.isBlock && (node.isTextblock || serializer || (node.isLeaf && node.type.spec.leafText))) {+ if (!firstBlock) text += "\n";+ firstBlock = false;+ }+ if (serializer && parent) {+ text += serializer({ node, pos, parent, index, range: { from, to } });+ return false;+ }+ if (node.isText) {+ text += node.text!.slice(Math.max(from, pos) - pos, to - pos);+ } else if (node.isLeaf && node.type.spec.leafText) {+ text += node.type.spec.leafText(node);+ }+ });+ return text;+};++export const editorClipboardTextSerializer: NonNullable<EditorProps["clipboardTextSerializer"]> = (_slice, view) => {+ const { doc, selection } = view.state;+ // Cell selections expose disjoint ranges: do not include the cells between them.+ return [...selection.ranges]+ .sort((a, b) => a.$from.pos - b.$from.pos)+ .map(({ $from, $to }) => serializeEditorTextRange(doc, $from.pos, $to.pos))+ .join("\n");+};