|
| 1 | +/** |
| 2 | + * Regression test for https://github.com/TypeCellOS/BlockNote/issues/2947 |
| 3 | + * |
| 4 | + * Comment marks are `blocknoteIgnore` marks: they're not part of the BlockNote |
| 5 | + * document model and don't survive an HTML round-trip. The HTML rebase tool |
| 6 | + * used to treat that as a fatal "html diff", which made every AI operation on a |
| 7 | + * block containing a comment fail. |
| 8 | + * |
| 9 | + * Runs fully offline (no LLM call): tool calls are fed straight into the |
| 10 | + * executor. |
| 11 | + */ |
| 12 | +import { BlockNoteEditor, createExtension } from "@blocknote/core"; |
| 13 | +import { CommentMark } from "@blocknote/core/comments"; |
| 14 | +import { describe, expect, it } from "vite-plus/test"; |
| 15 | + |
| 16 | +import { AIExtension } from "../../../AIExtension.js"; |
| 17 | +import { StreamToolExecutor } from "../../../streamTool/StreamToolExecutor.js"; |
| 18 | +import { StreamTool } from "../../../streamTool/streamTool.js"; |
| 19 | +import { tools } from "./tools/index.js"; |
| 20 | +import { createHTMLRebaseTool } from "./tools/rebaseTool.js"; |
| 21 | + |
| 22 | +/** |
| 23 | + * Registers just the comment mark. The full `CommentsExtension` needs a thread |
| 24 | + * store and user resolver, neither of which affects the document model. |
| 25 | + */ |
| 26 | +const CommentMarkExtension = createExtension(() => ({ |
| 27 | + key: "commentMarkOnly", |
| 28 | + tiptapExtensions: [CommentMark], |
| 29 | +})); |
| 30 | + |
| 31 | +function createEditorWithComment() { |
| 32 | + const editor = BlockNoteEditor.create({ |
| 33 | + initialContent: [ |
| 34 | + { id: "ref1", type: "paragraph", content: "Hello, world!" }, |
| 35 | + { id: "ref2", type: "paragraph", content: "How are you?" }, |
| 36 | + ], |
| 37 | + trailingBlock: false, |
| 38 | + extensions: [AIExtension(), CommentMarkExtension()], |
| 39 | + }); |
| 40 | + editor.mount(document.createElement("div")); |
| 41 | + |
| 42 | + // comment on "Hello" in the first block |
| 43 | + editor.transact((tr) => |
| 44 | + tr.addMark( |
| 45 | + 3, |
| 46 | + 8, |
| 47 | + editor.pmSchema.marks.comment.create({ |
| 48 | + threadId: "thread-1", |
| 49 | + orphan: false, |
| 50 | + }), |
| 51 | + ), |
| 52 | + ); |
| 53 | + |
| 54 | + return editor; |
| 55 | +} |
| 56 | + |
| 57 | +function commentedRanges(editor: BlockNoteEditor<any, any, any>) { |
| 58 | + const ranges: { text: string; threadId: string }[] = []; |
| 59 | + editor.prosemirrorState.doc.descendants((node) => { |
| 60 | + const mark = node.marks.find((m) => m.type.name === "comment"); |
| 61 | + if (mark) { |
| 62 | + ranges.push({ text: node.text!, threadId: mark.attrs.threadId }); |
| 63 | + } |
| 64 | + }); |
| 65 | + return ranges; |
| 66 | +} |
| 67 | + |
| 68 | +async function runUpdate( |
| 69 | + editor: BlockNoteEditor<any, any, any>, |
| 70 | + id: string, |
| 71 | + html: string, |
| 72 | +) { |
| 73 | + const streamTools = [ |
| 74 | + tools.update(editor, { idsSuffixed: false, withDelays: false }), |
| 75 | + ] as StreamTool<any>[]; |
| 76 | + |
| 77 | + await new StreamToolExecutor(streamTools).execute( |
| 78 | + (async function* () { |
| 79 | + yield { |
| 80 | + operation: { type: "update" as const, id, block: html }, |
| 81 | + isUpdateToPreviousOperation: false, |
| 82 | + isPossiblyPartial: false, |
| 83 | + metadata: undefined, |
| 84 | + }; |
| 85 | + })(), |
| 86 | + ); |
| 87 | +} |
| 88 | + |
| 89 | +describe("blocks containing comments", () => { |
| 90 | + it("can build a rebase tool for a commented block", () => { |
| 91 | + const editor = createEditorWithComment(); |
| 92 | + |
| 93 | + expect(() => createHTMLRebaseTool("ref1", editor)).not.toThrow(); |
| 94 | + }); |
| 95 | + |
| 96 | + it("updates a block that contains a comment", async () => { |
| 97 | + const editor = createEditorWithComment(); |
| 98 | + |
| 99 | + await runUpdate(editor, "ref1", "<p>Hello, universe!</p>"); |
| 100 | + |
| 101 | + editor.getExtension(AIExtension)?.acceptChanges(); |
| 102 | + expect((editor.document[0] as any).content[0].text).toBe( |
| 103 | + "Hello, universe!", |
| 104 | + ); |
| 105 | + // the untouched part of the comment is still anchored |
| 106 | + expect(commentedRanges(editor)).toEqual([ |
| 107 | + { text: "Hello", threadId: "thread-1" }, |
| 108 | + ]); |
| 109 | + }); |
| 110 | + |
| 111 | + it("updates a sibling block while another block has a comment", async () => { |
| 112 | + const editor = createEditorWithComment(); |
| 113 | + |
| 114 | + await runUpdate(editor, "ref2", "<p>How do you do?</p>"); |
| 115 | + |
| 116 | + editor.getExtension(AIExtension)?.acceptChanges(); |
| 117 | + expect((editor.document[1] as any).content[0].text).toBe("How do you do?"); |
| 118 | + expect(commentedRanges(editor)).toEqual([ |
| 119 | + { text: "Hello", threadId: "thread-1" }, |
| 120 | + ]); |
| 121 | + }); |
| 122 | +}); |
0 commit comments