Skip to content

Commit f23482d

Browse files
authored
fix: put back onBeforeChange method #2221 (#2243)
1 parent 6a0ac84 commit f23482d

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

packages/core/src/editor/BlockNoteEditor.test.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
getNearestBlockPos,
77
} from "../api/getBlockInfoFromPos.js";
88
import { BlockNoteEditor } from "./BlockNoteEditor.js";
9+
import { BlocksChanged } from "../api/getBlocksChangedByTransaction.js";
910

1011
/**
1112
* @vitest-environment jsdom
@@ -190,3 +191,62 @@ it("sets an initial block id when using Y.js", async () => {
190191
`"<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>"`,
191192
);
192193
});
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+
});

packages/core/src/editor/BlockNoteEditor.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import {
5252
} from "./managers/index.js";
5353
import type { Selection } from "./selectionTypes.js";
5454
import { transformPasted } from "./transformPasted.js";
55+
import { BlockChangeExtension } from "../extensions/index.js";
5556

5657
export type BlockCache<
5758
BSchema extends BlockSchema = any,
@@ -904,6 +905,22 @@ export class BlockNoteEditor<
904905
this._tiptapEditor.on("selectionUpdate", callback);
905906
}
906907

908+
/**
909+
* Executes a callback before any change is applied to the editor, allowing you to cancel the change.
910+
* @param callback The callback to execute.
911+
* @returns A function to remove the callback.
912+
*/
913+
public onBeforeChange(
914+
callback: (context: {
915+
getChanges: () => BlocksChanged<any, any, any>;
916+
tr: Transaction;
917+
}) => boolean | void,
918+
) {
919+
return this._extensionManager
920+
.getExtension(BlockChangeExtension)
921+
?.subscribe(callback);
922+
}
923+
907924
/**
908925
* Gets a snapshot of the current text cursor position.
909926
* @returns A snapshot of the current text cursor position.

0 commit comments

Comments
 (0)