|
| 1 | +import { describe, expect, it } from "vite-plus/test"; |
| 2 | + |
| 3 | +import { BlockNoteEditor } from "../../editor/BlockNoteEditor.js"; |
| 4 | +import { nodeToBlock } from "./nodeToBlock.js"; |
| 5 | +import { blockToNode } from "./blockToNode.js"; |
| 6 | + |
| 7 | +/** |
| 8 | + * @vitest-environment jsdom |
| 9 | + */ |
| 10 | + |
| 11 | +// Backwards compatibility for the "plain" content migration on the NON-Yjs |
| 12 | +// (BlockNote JSON) path. A block whose content type changed from "inline" |
| 13 | +// (formatting marks allowed) to "plain" (only the "annotation" group allowed) |
| 14 | +// may appear in stored BlockNote JSON with formatting styles on its text. The |
| 15 | +// write path must drop those disallowed marks instead of throwing when the node |
| 16 | +// is assembled (`createChecked`), and the change must be scoped to plain blocks |
| 17 | +// so inline blocks still keep their formatting. |
| 18 | + |
| 19 | +// Old BlockNote JSON: a (now-plain) code block carrying a bold style. |
| 20 | +const legacyBoldCodeBlock = { |
| 21 | + type: "codeBlock" as const, |
| 22 | + props: { language: "javascript" }, |
| 23 | + content: [ |
| 24 | + { type: "text" as const, text: "const x = 1;", styles: { bold: true } }, |
| 25 | + ], |
| 26 | +}; |
| 27 | + |
| 28 | +describe("blockToNode: plain-block backwards compatibility (write path)", () => { |
| 29 | + it("does not throw and drops formatting when converting a plain block with styles", () => { |
| 30 | + const editor = BlockNoteEditor.create(); |
| 31 | + |
| 32 | + expect(() => |
| 33 | + blockToNode(legacyBoldCodeBlock as any, editor.pmSchema), |
| 34 | + ).not.toThrow(); |
| 35 | + |
| 36 | + const node = blockToNode(legacyBoldCodeBlock as any, editor.pmSchema); |
| 37 | + // The node is valid (would otherwise fail this check). |
| 38 | + expect(() => node.check()).not.toThrow(); |
| 39 | + |
| 40 | + // Round-trips back to a plain string with no materialized formatting. |
| 41 | + const block = nodeToBlock(node, editor.pmSchema); |
| 42 | + expect(block.type).toBe("codeBlock"); |
| 43 | + expect(block.content).toBe("const x = 1;"); |
| 44 | + |
| 45 | + editor._tiptapEditor.destroy(); |
| 46 | + }); |
| 47 | + |
| 48 | + it("keeps formatting on an inline block (scoped to plain blocks only)", () => { |
| 49 | + const editor = BlockNoteEditor.create(); |
| 50 | + |
| 51 | + const node = blockToNode( |
| 52 | + { |
| 53 | + type: "paragraph", |
| 54 | + content: [{ type: "text", text: "hello", styles: { bold: true } }], |
| 55 | + } as any, |
| 56 | + editor.pmSchema, |
| 57 | + ); |
| 58 | + |
| 59 | + const block = nodeToBlock(node, editor.pmSchema); |
| 60 | + expect(block.type).toBe("paragraph"); |
| 61 | + // Paragraph is "inline" content, so bold survives. |
| 62 | + expect(Array.isArray(block.content)).toBe(true); |
| 63 | + expect((block.content as any[])[0].styles.bold).toBe(true); |
| 64 | + |
| 65 | + editor._tiptapEditor.destroy(); |
| 66 | + }); |
| 67 | + |
| 68 | + it("mounts an editor with legacy initialContent without throwing", () => { |
| 69 | + const editor = BlockNoteEditor.create({ |
| 70 | + initialContent: [legacyBoldCodeBlock as any], |
| 71 | + }); |
| 72 | + editor.mount(document.createElement("div")); |
| 73 | + |
| 74 | + expect(editor.document.map((b) => b.type)).toEqual(["codeBlock"]); |
| 75 | + expect(editor.document[0].content).toBe("const x = 1;"); |
| 76 | + |
| 77 | + editor._tiptapEditor.destroy(); |
| 78 | + }); |
| 79 | + |
| 80 | + it("read path already strips marks: nodeToBlock yields a bare string", () => { |
| 81 | + const editor = BlockNoteEditor.create(); |
| 82 | + |
| 83 | + // Build the node leniently and confirm the read path emits no styles even if |
| 84 | + // the block content is plain text. |
| 85 | + const node = blockToNode(legacyBoldCodeBlock as any, editor.pmSchema); |
| 86 | + const block = nodeToBlock(node, editor.pmSchema); |
| 87 | + expect(typeof block.content).toBe("string"); |
| 88 | + |
| 89 | + editor._tiptapEditor.destroy(); |
| 90 | + }); |
| 91 | +}); |
0 commit comments