You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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
+
returntr.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.
`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
+
returntrue;
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.
0 commit comments