|
5 | 5 | } from "../api/getBlockInfoFromPos.js"; |
6 | 6 | import { BlockNoteEditor } from "./BlockNoteEditor.js"; |
7 | 7 | import { BlockNoteExtension } from "./BlockNoteExtension.js"; |
| 8 | +import * as Y from "yjs"; |
8 | 9 |
|
9 | 10 | /** |
10 | 11 | * @vitest-environment jsdom |
@@ -146,3 +147,67 @@ it("onCreate event", () => { |
146 | 147 | }); |
147 | 148 | expect(created).toBe(true); |
148 | 149 | }); |
| 150 | + |
| 151 | +it("sets an initial block id when using Y.js", async () => { |
| 152 | + const doc = new Y.Doc(); |
| 153 | + const fragment = doc.getXmlFragment("doc"); |
| 154 | + let transactionCount = 0; |
| 155 | + const editor = BlockNoteEditor.create({ |
| 156 | + collaboration: { |
| 157 | + fragment, |
| 158 | + user: { name: "Hello", color: "#FFFFFF" }, |
| 159 | + provider: null, |
| 160 | + }, |
| 161 | + _tiptapOptions: { |
| 162 | + onTransaction: () => { |
| 163 | + transactionCount++; |
| 164 | + }, |
| 165 | + }, |
| 166 | + }); |
| 167 | + |
| 168 | + editor.mount(document.createElement("div")); |
| 169 | + |
| 170 | + expect(editor.prosemirrorState.doc.toJSON()).toMatchInlineSnapshot(` |
| 171 | + { |
| 172 | + "content": [ |
| 173 | + { |
| 174 | + "content": [ |
| 175 | + { |
| 176 | + "attrs": { |
| 177 | + "id": "initialBlockId", |
| 178 | + }, |
| 179 | + "content": [ |
| 180 | + { |
| 181 | + "attrs": { |
| 182 | + "backgroundColor": "default", |
| 183 | + "textAlignment": "left", |
| 184 | + "textColor": "default", |
| 185 | + }, |
| 186 | + "type": "paragraph", |
| 187 | + }, |
| 188 | + ], |
| 189 | + "type": "blockContainer", |
| 190 | + }, |
| 191 | + ], |
| 192 | + "type": "blockGroup", |
| 193 | + }, |
| 194 | + ], |
| 195 | + "type": "doc", |
| 196 | + } |
| 197 | + `); |
| 198 | + expect(transactionCount).toBe(1); |
| 199 | + // The fragment should not be modified yet, since the editor's content is only the initial content |
| 200 | + expect(fragment.toJSON()).toMatchInlineSnapshot(`""`); |
| 201 | + |
| 202 | + editor.replaceBlocks(editor.document, [ |
| 203 | + { |
| 204 | + type: "paragraph", |
| 205 | + content: [{ text: "Hello", styles: {}, type: "text" }], |
| 206 | + }, |
| 207 | + ]); |
| 208 | + expect(transactionCount).toBe(2); |
| 209 | + // Only after a real modification is made, will the fragment be updated |
| 210 | + expect(fragment.toJSON()).toMatchInlineSnapshot( |
| 211 | + `"<blockgroup><blockcontainer id="0"><paragraph backgroundColor="default" textAlignment="left" textColor="default">Hello</paragraph></blockcontainer><blockcontainer id="1"><paragraph backgroundColor="default" textAlignment="left" textColor="default"></paragraph></blockcontainer></blockgroup>"`, |
| 212 | + ); |
| 213 | +}); |
0 commit comments