|
6 | 6 | getNearestBlockPos, |
7 | 7 | } from "../api/getBlockInfoFromPos.js"; |
8 | 8 | import { BlockNoteEditor } from "./BlockNoteEditor.js"; |
| 9 | +import { BlocksChanged } from "../api/getBlocksChangedByTransaction.js"; |
9 | 10 |
|
10 | 11 | /** |
11 | 12 | * @vitest-environment jsdom |
@@ -190,3 +191,62 @@ it("sets an initial block id when using Y.js", async () => { |
190 | 191 | `"<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>"`, |
191 | 192 | ); |
192 | 193 | }); |
| 194 | + |
| 195 | +it("onBeforeChange", () => { |
| 196 | + const editor = BlockNoteEditor.create(); |
| 197 | + let beforeChangeCalled = false; |
| 198 | + let changes: BlocksChanged<any, any, any> = []; |
| 199 | + editor.onBeforeChange(({ getChanges }) => { |
| 200 | + beforeChangeCalled = true; |
| 201 | + changes = getChanges(); |
| 202 | + return true; |
| 203 | + }); |
| 204 | + editor.mount(document.createElement("div")); |
| 205 | + editor.replaceBlocks(editor.document, [ |
| 206 | + { |
| 207 | + type: "paragraph", |
| 208 | + content: [{ text: "Hello", styles: {}, type: "text" }], |
| 209 | + }, |
| 210 | + ]); |
| 211 | + expect(beforeChangeCalled).toBe(true); |
| 212 | + expect(changes).toMatchInlineSnapshot(` |
| 213 | + [ |
| 214 | + { |
| 215 | + "block": { |
| 216 | + "children": [], |
| 217 | + "content": [], |
| 218 | + "id": "3", |
| 219 | + "props": { |
| 220 | + "backgroundColor": "default", |
| 221 | + "textAlignment": "left", |
| 222 | + "textColor": "default", |
| 223 | + }, |
| 224 | + "type": "paragraph", |
| 225 | + }, |
| 226 | + "prevBlock": undefined, |
| 227 | + "source": { |
| 228 | + "type": "local", |
| 229 | + }, |
| 230 | + "type": "insert", |
| 231 | + }, |
| 232 | + { |
| 233 | + "block": { |
| 234 | + "children": [], |
| 235 | + "content": [], |
| 236 | + "id": "2", |
| 237 | + "props": { |
| 238 | + "backgroundColor": "default", |
| 239 | + "textAlignment": "left", |
| 240 | + "textColor": "default", |
| 241 | + }, |
| 242 | + "type": "paragraph", |
| 243 | + }, |
| 244 | + "prevBlock": undefined, |
| 245 | + "source": { |
| 246 | + "type": "local", |
| 247 | + }, |
| 248 | + "type": "delete", |
| 249 | + }, |
| 250 | + ] |
| 251 | + `); |
| 252 | +}); |
0 commit comments