Skip to content

feat: add undo and redo methods to editor API #1592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/pages/docs/editor-api/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"export-to-pdf": "",
"export-to-docx": "",
"export-to-odt": "",
"events": ""
"events": "",
"methods": ""
}
87 changes: 87 additions & 0 deletions docs/pages/docs/editor-api/methods.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
title: Methods
description: BlockNote provides a number of methods to interact with the editor.
imageTitle: Methods
path: /docs/methods
---

import { Example } from "@/components/example";

# Methods

BlockNote provides a number of methods to interact with the editor.

## `undo`

The `undo` method is used to undo the last action.

```typescript
editor.undo();
```

## `redo`

The `redo` method is used to redo the last action.

```typescript
editor.redo();
```

## `exec`

The `exec` method executes a prosemirror command. This is mostly for backwards compatibility with older code.

You should prefer the `transact` method when possible, as it will automatically handle the dispatching of the transaction and work across blocknote transactions.

```typescript
// Example of a custom command
function insertTextCommand(state: EditorState, dispatch: EditorDispatch, view: EditorView) {
if (dispatch) {
dispatch(state.tr.insertText("Hello, world!"));
}
}

editor.exec(insertTextCommand);
```

## `canExec`

The `canExec` method checks if a prosemirror command can be executed.

```typescript
const canExecute = editor.canExec(insertTextCommand);
```

## `transact`

The `transact` method executes a prosemirror transaction. See the [low-level APIs](/docs/editor-api/manipulating-blocks#blocknote-transactions) section for more information.

```typescript
editor.transact((tr) => {
tr.insertText("Hello, world!");
});
```

## `pasteHTML`

The `pasteHTML` method pastes HTML into the editor.

```typescript
editor.pasteHTML("<p>Hello, world!</p>");
```

## `pasteText`

The `pasteText` method pastes text into the editor.

```typescript
editor.pasteText("Hello, world!");
```

## `pasteMarkdown`

The `pasteMarkdown` method pastes markdown into the editor.

```typescript
editor.pasteMarkdown("**Hello, world!**");
```
16 changes: 6 additions & 10 deletions examples/07-collaboration/02-liveblocks/.bnexample.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@
"playground": true,
"docs": true,
"author": "yousefed",
"tags": [
"Advanced",
"Saving/Loading",
"Collaboration"
],
"tags": ["Advanced", "Saving/Loading", "Collaboration"],
"dependencies": {
"@liveblocks/client": "^2.22.3",
"@liveblocks/react": "^2.22.3",
"@liveblocks/react-blocknote": "^2.22.3",
"@liveblocks/react-tiptap": "^2.22.3",
"@liveblocks/react-ui": "^2.22.3",
"@liveblocks/client": "^2.23.1",
"@liveblocks/react": "^2.23.1",
"@liveblocks/react-blocknote": "^2.23.1",
"@liveblocks/react-tiptap": "^2.23.1",
"@liveblocks/react-ui": "^2.23.1",
"yjs": "^13.6.15"
}
}
10 changes: 5 additions & 5 deletions examples/07-collaboration/02-liveblocks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
"@blocknote/shadcn": "latest",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"@liveblocks/client": "^2.22.3",
"@liveblocks/react": "^2.22.3",
"@liveblocks/react-blocknote": "^2.22.3",
"@liveblocks/react-tiptap": "^2.22.3",
"@liveblocks/react-ui": "^2.22.3",
"@liveblocks/client": "^2.23.1",
"@liveblocks/react": "^2.23.1",
"@liveblocks/react-blocknote": "^2.23.1",
"@liveblocks/react-tiptap": "^2.23.1",
"@liveblocks/react-ui": "^2.23.1",
"yjs": "^13.6.15"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
"remark-stringify": "^11.0.0",
"unified": "^11.0.5",
"uuid": "^8.3.2",
"y-prosemirror": "^1.3.3",
"y-prosemirror": "^1.3.4",
"y-protocols": "^1.0.6",
"yjs": "^13.6.15"
},
Expand Down
24 changes: 23 additions & 1 deletion packages/core/src/editor/BlockNoteEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ import {
} from "@tiptap/pm/state";
import { dropCursor } from "prosemirror-dropcursor";
import { EditorView } from "prosemirror-view";
import { ySyncPluginKey } from "y-prosemirror";
import { undoCommand, redoCommand, ySyncPluginKey } from "y-prosemirror";
import { undo, redo } from "@tiptap/pm/history";
import { createInternalHTMLSerializer } from "../api/exporters/html/internalHTMLSerializer.js";
import { inlineContentToNodes } from "../api/nodeConversions/blockToNode.js";
import { nodeToBlock } from "../api/nodeConversions/nodeToBlock.js";
Expand Down Expand Up @@ -1199,6 +1200,27 @@ export class BlockNoteEditor<
);
}

/**
* Undo the last action.
*/
public undo() {
if (this.options.collaboration) {
return this.exec(undoCommand);
}

return this.exec(undo);
}

/**
* Redo the last action.
*/
public redo() {
if (this.options.collaboration) {
return this.exec(redoCommand);
}
return this.exec(redo);
}

/**
* Insert a piece of content at the current cursor position.
*
Expand Down
2 changes: 1 addition & 1 deletion packages/server-util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"@tiptap/core": "^2.7.1",
"@tiptap/pm": "^2.7.1",
"jsdom": "^25.0.1",
"y-prosemirror": "^1.3.3",
"y-prosemirror": "^1.3.4",
"y-protocols": "^1.0.6",
"yjs": "^13.6.15"
},
Expand Down
10 changes: 5 additions & 5 deletions playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
"@blocknote/xl-pdf-exporter": "workspace:^",
"@emotion/react": "^11.11.4",
"@emotion/styled": "^11.11.5",
"@liveblocks/core": "^2.22.3",
"@liveblocks/react": "^2.22.3",
"@liveblocks/react-blocknote": "^2.22.3",
"@liveblocks/react-tiptap": "^2.22.3",
"@liveblocks/react-ui": "^2.22.3",
"@liveblocks/core": "^2.23.1",
"@liveblocks/react": "^2.23.1",
"@liveblocks/react-blocknote": "^2.23.1",
"@liveblocks/react-tiptap": "^2.23.1",
"@liveblocks/react-ui": "^2.23.1",
"@mantine/core": "^7.10.1",
"@mui/icons-material": "^5.16.1",
"@mui/material": "^5.16.1",
Expand Down
10 changes: 5 additions & 5 deletions playground/src/examples.gen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1241,11 +1241,11 @@
"Collaboration"
],
"dependencies": {
"@liveblocks/client": "^2.22.3",
"@liveblocks/react": "^2.22.3",
"@liveblocks/react-blocknote": "^2.22.3",
"@liveblocks/react-tiptap": "^2.22.3",
"@liveblocks/react-ui": "^2.22.3",
"@liveblocks/client": "^2.23.1",
"@liveblocks/react": "^2.23.1",
"@liveblocks/react-blocknote": "^2.23.1",
"@liveblocks/react-tiptap": "^2.23.1",
"@liveblocks/react-ui": "^2.23.1",
"yjs": "^13.6.15"
} as any
},
Expand Down
Loading
Loading