|
| 1 | +--- |
| 2 | +title: Methods |
| 3 | +description: BlockNote provides a number of methods to interact with the editor. |
| 4 | +imageTitle: Methods |
| 5 | +path: /docs/methods |
| 6 | +--- |
| 7 | + |
| 8 | +import { Example } from "@/components/example"; |
| 9 | + |
| 10 | +# Methods |
| 11 | + |
| 12 | +BlockNote provides a number of methods to interact with the editor. |
| 13 | + |
| 14 | +## `undo` |
| 15 | + |
| 16 | +The `undo` method is used to undo the last action. |
| 17 | + |
| 18 | +```typescript |
| 19 | +editor.undo(); |
| 20 | +``` |
| 21 | + |
| 22 | +## `redo` |
| 23 | + |
| 24 | +The `redo` method is used to redo the last action. |
| 25 | + |
| 26 | +```typescript |
| 27 | +editor.redo(); |
| 28 | +``` |
| 29 | + |
| 30 | +## `exec` |
| 31 | + |
| 32 | +The `exec` method executes a prosemirror command. This is mostly for backwards compatibility with older code. |
| 33 | + |
| 34 | +You should prefer the `transact` method when possible, as it will automatically handle the dispatching of the transaction and work across blocknote transactions. |
| 35 | + |
| 36 | +```typescript |
| 37 | +// Example of a custom command |
| 38 | +function insertTextCommand(state: EditorState, dispatch: EditorDispatch, view: EditorView) { |
| 39 | + if (dispatch) { |
| 40 | + dispatch(state.tr.insertText("Hello, world!")); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +editor.exec(insertTextCommand); |
| 45 | +``` |
| 46 | + |
| 47 | +## `canExec` |
| 48 | + |
| 49 | +The `canExec` method checks if a prosemirror command can be executed. |
| 50 | + |
| 51 | +```typescript |
| 52 | +const canExecute = editor.canExec(insertTextCommand); |
| 53 | +``` |
| 54 | + |
| 55 | +## `transact` |
| 56 | + |
| 57 | +The `transact` method executes a prosemirror transaction. See the [low-level APIs](/docs/editor-api/manipulating-blocks#blocknote-transactions) section for more information. |
| 58 | + |
| 59 | +```typescript |
| 60 | +editor.transact((tr) => { |
| 61 | + tr.insertText("Hello, world!"); |
| 62 | +}); |
| 63 | +``` |
| 64 | + |
| 65 | +## `pasteHTML` |
| 66 | + |
| 67 | +The `pasteHTML` method pastes HTML into the editor. |
| 68 | + |
| 69 | +```typescript |
| 70 | +editor.pasteHTML("<p>Hello, world!</p>"); |
| 71 | +``` |
| 72 | + |
| 73 | +## `pasteText` |
| 74 | + |
| 75 | +The `pasteText` method pastes text into the editor. |
| 76 | + |
| 77 | +```typescript |
| 78 | +editor.pasteText("Hello, world!"); |
| 79 | +``` |
| 80 | + |
| 81 | +## `pasteMarkdown` |
| 82 | + |
| 83 | +The `pasteMarkdown` method pastes markdown into the editor. |
| 84 | + |
| 85 | +```typescript |
| 86 | +editor.pasteMarkdown("**Hello, world!**"); |
| 87 | +``` |
0 commit comments