|
| 1 | +// @vitest-environment jsdom |
| 2 | + |
| 3 | +import { BlockNoteEditor, BlockNoteSchema } from "@blocknote/core"; |
| 4 | +import { withMultiColumn } from "@blocknote/xl-multi-column"; |
| 5 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 6 | + |
| 7 | +import { htmlBlockLLMFormat } from "./htmlBlocks.js"; |
| 8 | + |
| 9 | +const schema = withMultiColumn(BlockNoteSchema.create()); |
| 10 | + |
| 11 | +// Regression test for https://github.com/TypeCellOS/BlockNote/issues/2716 |
| 12 | +describe("htmlBlockLLMFormat.defaultDocumentStateBuilder with multi-column", () => { |
| 13 | + let editor: BlockNoteEditor<any, any, any>; |
| 14 | + const div = document.createElement("div"); |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + editor = BlockNoteEditor.create({ |
| 18 | + schema, |
| 19 | + initialContent: [ |
| 20 | + { |
| 21 | + id: "column-list-0", |
| 22 | + type: "columnList", |
| 23 | + children: [ |
| 24 | + { |
| 25 | + id: "column-0", |
| 26 | + type: "column", |
| 27 | + children: [ |
| 28 | + { |
| 29 | + id: "left-paragraph", |
| 30 | + type: "paragraph", |
| 31 | + content: "left column", |
| 32 | + }, |
| 33 | + ], |
| 34 | + }, |
| 35 | + { |
| 36 | + id: "column-1", |
| 37 | + type: "column", |
| 38 | + children: [ |
| 39 | + { |
| 40 | + id: "right-paragraph", |
| 41 | + type: "paragraph", |
| 42 | + content: "right column", |
| 43 | + }, |
| 44 | + ], |
| 45 | + }, |
| 46 | + ], |
| 47 | + }, |
| 48 | + ], |
| 49 | + }); |
| 50 | + editor.mount(div); |
| 51 | + }); |
| 52 | + |
| 53 | + afterEach(() => { |
| 54 | + editor._tiptapEditor.destroy(); |
| 55 | + editor = undefined as any; |
| 56 | + }); |
| 57 | + |
| 58 | + it("builds the document state for a doc containing a columnList without throwing", async () => { |
| 59 | + const result = await htmlBlockLLMFormat.defaultDocumentStateBuilder({ |
| 60 | + editor, |
| 61 | + streamTools: [], |
| 62 | + onStart: () => { |
| 63 | + // no-op |
| 64 | + }, |
| 65 | + }); |
| 66 | + |
| 67 | + if (result.selection !== false) { |
| 68 | + throw new Error("expected selection-less document state"); |
| 69 | + } |
| 70 | + |
| 71 | + const idEntries = result.blocks.filter( |
| 72 | + (b): b is { id: string; block: string } => "id" in b, |
| 73 | + ); |
| 74 | + |
| 75 | + const columnListEntry = idEntries.find((b) => |
| 76 | + b.id.includes("column-list-0"), |
| 77 | + ); |
| 78 | + expect(columnListEntry).toBeDefined(); |
| 79 | + expect(columnListEntry!.block).toContain('data-node-type="columnList"'); |
| 80 | + |
| 81 | + expect(idEntries.find((b) => b.id.includes("column-0"))).toBeDefined(); |
| 82 | + expect(idEntries.find((b) => b.id.includes("column-1"))).toBeDefined(); |
| 83 | + expect( |
| 84 | + idEntries.find((b) => b.id.includes("left-paragraph")), |
| 85 | + ).toBeDefined(); |
| 86 | + expect( |
| 87 | + idEntries.find((b) => b.id.includes("right-paragraph")), |
| 88 | + ).toBeDefined(); |
| 89 | + }); |
| 90 | + |
| 91 | + it("builds the document state when invoked with selectedBlocks containing a columnList", async () => { |
| 92 | + const result = await htmlBlockLLMFormat.defaultDocumentStateBuilder({ |
| 93 | + editor, |
| 94 | + selectedBlocks: editor.document, |
| 95 | + streamTools: [], |
| 96 | + onStart: () => { |
| 97 | + // no-op |
| 98 | + }, |
| 99 | + }); |
| 100 | + |
| 101 | + if (result.selection !== true) { |
| 102 | + throw new Error("expected selection-based document state"); |
| 103 | + } |
| 104 | + expect(result.selectedBlocks.length).toBeGreaterThan(0); |
| 105 | + }); |
| 106 | +}); |
0 commit comments