Skip to content

Commit 97c3dc7

Browse files
committed
docs: add docs for transact, exec, canExec and events
1 parent aa16b15 commit 97c3dc7

File tree

3 files changed

+149
-2
lines changed

3 files changed

+149
-2
lines changed

docs/pages/docs/editor-api/_meta.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
"server-processing": "",
77
"export-to-pdf": "",
88
"export-to-docx": "",
9-
"export-to-odt": ""
9+
"export-to-odt": "",
10+
"events": ""
1011
}

docs/pages/docs/editor-api/events.mdx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
title: Events
3+
description: BlockNote emits events when certain actions occur.
4+
imageTitle: Events
5+
path: /docs/events
6+
---
7+
8+
import { Example } from "@/components/example";
9+
10+
# Events
11+
12+
BlockNote emits events when certain actions occur.
13+
14+
## `onCreate`
15+
16+
The `onCreate` callback is called when the editor is initialized.
17+
18+
```typescript
19+
editor.onCreate(() => {
20+
console.log("Editor created");
21+
});
22+
```
23+
24+
## `onChange`
25+
26+
The `onChange` callback is called when the editor content changes.
27+
28+
```typescript
29+
editor.onChange(() => {
30+
console.log("Editor updated");
31+
});
32+
```
33+
34+
## `onSelectionChange`
35+
36+
The `onSelectionChange` callback is called when the editor selection changes.
37+
38+
```typescript
39+
editor.onSelectionChange(() => {
40+
console.log("Editor selection changed");
41+
});
42+
```
43+

docs/pages/docs/editor-api/manipulating-blocks.mdx

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ path: /docs/manipulating-blocks
99

1010
Below, we explain the methods on `editor` you can use to read Blocks from the editor, and how to create / remove / update Blocks:
1111

12-
- [`get document`](/docs/editor-api/manipulating-blocks#getting-the-document)
12+
- [`document`](/docs/editor-api/manipulating-blocks#getting-the-document)
1313
- [`getBlock`](/docs/editor-api/manipulating-blocks#single-specific-block)
1414
- [`getPrevBlock`](/docs/editor-api/manipulating-blocks#previous-block)
1515
- [`getNextBlock`](/docs/editor-api/manipulating-blocks#next-block)
@@ -312,3 +312,106 @@ unnestBlock(): void;
312312
// Usage
313313
editor.unnestBlock();
314314
```
315+
316+
## Low-level APIs
317+
318+
### BlockNote Transactions
319+
320+
The `transact` method is used to perform a series of changes to the editor. It can group multiple changes into a single undo/redo operation. And it also provides a low-level API to read state and perform changes to the editor.
321+
322+
```typescript
323+
transact<T>(callback: (tr: Transaction) => T): T;
324+
```
325+
326+
`callback:` A function that receives a `Transaction` object, and returns a value.
327+
328+
#### Multiple editor operations
329+
330+
The `transact` method can be used to group multiple editor operations into a single undo/redo operation.
331+
332+
```typescript
333+
editor.transact(() => {
334+
// Both of these changes are grouped into a single undo/redo operation
335+
editor.insertBlocks([{ type: "paragraph", content: "Hello World" }], "root");
336+
editor.updateBlock("root", { type: "heading" });
337+
});
338+
```
339+
340+
#### Reading state
341+
342+
The `transact` method can also be used to read the state of the editor.
343+
344+
```typescript
345+
const isSelectionEmpty = editor.transact((tr) => {
346+
// What you return here will be returned from the transact method
347+
return tr.selection.empty
348+
});
349+
```
350+
351+
#### Performing changes
352+
353+
The `transact` method can also be used to perform changes to the editor.
354+
355+
```typescript
356+
editor.transact((tr) => {
357+
// This tr is automatically applied to the editor when the transact method returns
358+
tr.insertText("Hello World");
359+
});
360+
```
361+
362+
### Executing ProseMirror Commands
363+
364+
The `exec` method can be used to execute a ProseMirror command. This is mostly for backwards compatibility with ProseMirror commands.
365+
366+
When possible, you should prefer the [`transact`](#transact) method, as it will automatically handle the dispatching of the transaction and work across blocknote transactions. Also, the `exec` method cannot be used within a `transact` call since they will conflict with each other.
367+
368+
```typescript
369+
exec(command: (state: EditorState, dispatch?: (tr: Transaction) => void, view?: EditorView) => boolean): boolean;
370+
```
371+
372+
`command:` A function that receives the current editor state, and a dispatch function to apply a transaction.
373+
374+
Returns `true` if the command was executed, `false` otherwise.
375+
376+
```typescript
377+
editor.exec((state, dispatch, view) => {
378+
const tr = state.tr;
379+
if (dispatch) {
380+
tr.insertText("Hello World");
381+
dispatch(tr);
382+
}
383+
return true;
384+
});
385+
```
386+
387+
This will insert the text "Hello World" into the editor at the current cursor position.
388+
389+
### Checking if a ProseMirror command can be executed
390+
391+
The `canExec` method can be used to check whether a ProseMirror command can be executed.
392+
393+
When possible, you should prefer the [`transact`](#transact) method, as it will automatically handle the dispatching of the transaction and work across blocknote transactions. Also, the `exec` method cannot be used within a `transact` call since they will conflict with each other.
394+
395+
```typescript
396+
canExec(command: (state: EditorState, dispatch?: (tr: Transaction) => void, view?: EditorView) => boolean): boolean;
397+
```
398+
399+
`command:` A function that receives the current editor state, and a dispatch function to apply a transaction.
400+
401+
Returns `true` if the command can be executed, `false` otherwise.
402+
403+
```typescript
404+
const canReplaceSelection = editor.canExec((state, dispatch, view) => {
405+
if (state.selection.from === state.selection.to){
406+
return false;
407+
}
408+
409+
if (dispatch) {
410+
dispatch(state.tr.insertText("Hello World"));
411+
}
412+
return true;
413+
});
414+
```
415+
416+
This will insert the text "Hello World" into the editor at the current cursor position only if the cursor is not empty.
417+

0 commit comments

Comments
 (0)