Skip to content
File

Blob: tests/client/components/editor/lib/clipboard.test.ts

typescript55 lines
1import { describe, expect, it } from "vitest";
2import { Editor, getTextBetween, getTextSerializersFromSchema } from "@tiptap/core";
3import { StarterKit } from "@tiptap/starter-kit";
4import { EDITOR_CORE_EXTENSION_OPTIONS } from "@/client/components/editor/lib/clipboard";
5 
6function serializePlainText(content: Record<string, unknown>) {
7 const editor = new Editor({
8 extensions: [StarterKit],
9 content,
10 coreExtensionOptions: EDITOR_CORE_EXTENSION_OPTIONS,
11 });
12 
13 try {
14 return getTextBetween(
15 editor.state.doc,
16 { from: 0, to: editor.state.doc.content.size },
17 {
18 blockSeparator: EDITOR_CORE_EXTENSION_OPTIONS.clipboardTextSerializer.blockSeparator,
19 textSerializers: getTextSerializersFromSchema(editor.schema),
20 },
21 );
22 } finally {
23 editor.destroy();
24 }
25}
26 
27describe("plain-text clipboard serialization", () => {
28 it("uses single newlines between paragraphs", () => {
29 expect(
30 serializePlainText({
31 type: "doc",
32 content: [
33 { type: "paragraph", content: [{ type: "text", text: "one" }] },
34 { type: "paragraph", content: [{ type: "text", text: "two" }] },
35 ],
36 }),
37 ).toBe("one\ntwo");
38 });
39 
40 it("preserves hard breaks as single newlines", () => {
41 expect(
42 serializePlainText({
43 type: "doc",
44 content: [
45 {
46 type: "paragraph",
47 content: [{ type: "text", text: "one" }, { type: "hardBreak" }, { type: "text", text: "two" }],
48 },
49 { type: "paragraph", content: [{ type: "text", text: "three" }] },
50 ],
51 }),
52 ).toBe("one\ntwo\nthree");
53 });
54});