Skip to content

feat: change event allows getting a list of the changes made #1585

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 13 commits into from
Apr 17, 2025
Merged
58 changes: 55 additions & 3 deletions docs/pages/docs/editor-api/events.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,64 @@ editor.onCreate(() => {
The `onChange` callback is called when the editor content changes.

```typescript
editor.onChange(() => {
editor.onChange((editor, { getChanges }) => {
console.log("Editor updated");
const changes = getChanges();
console.log(changes);
});
```

You can see what specific changes occurred in the editor by calling `getChanges()` in the callback. This function returns an array of block changes which looks like:

```typescript
/**
* The changes that occurred in the editor.
*/
type BlocksChanged = Array<
| {
// The affected block
block: Block;
// The source of the change
source: BlockChangeSource;
type: "insert" | "delete";
// Insert and delete changes don't have a previous block
prevBlock: undefined;
}
| {
// The affected block
block: Block;
// The source of the change
source: BlockChangeSource;
type: "update";
// The block before the update
prevBlock: Block;
}
)>;

/**
* This attributes the changes to a specific source.
*/
type BlockChangeSource = {
/**
* The type of change source:
* - "local": Triggered by local user (default)
* - "paste": From paste operation
* - "drop": From drop operation
* - "undo"/"redo"/"undo-redo": From undo/redo operations
* - "yjs-remote": From remote user
*/
type:
| "local"
| "paste"
| "drop"
| "undo"
| "redo"
| "undo-redo"
| "yjs-remote";
};
```


## `onSelectionChange`

The `onSelectionChange` callback is called when the editor selection changes.
Expand All @@ -39,5 +92,4 @@ The `onSelectionChange` callback is called when the editor selection changes.
editor.onSelectionChange(() => {
console.log("Editor selection changed");
});
```

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"block": {
"children": [],
"content": [
{
"styles": {},
"text": "Double Nested Paragraph 0",
"type": "text",
},
],
"id": "double-nested-paragraph-0",
"props": {
"backgroundColor": "default",
"textAlignment": "left",
"textColor": "default",
},
"type": "paragraph",
},
"prevBlock": undefined,
"source": {
"type": "local",
},
"type": "delete",
},
]
68 changes: 68 additions & 0 deletions packages/core/src/api/__snapshots__/blocks-deleted-nested.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
[
{
"block": {
"children": [
{
"children": [],
"content": [
{
"styles": {},
"text": "Double Nested Paragraph 0",
"type": "text",
},
],
"id": "double-nested-paragraph-0",
"props": {
"backgroundColor": "default",
"textAlignment": "left",
"textColor": "default",
},
"type": "paragraph",
},
],
"content": [
{
"styles": {},
"text": "Nested Paragraph 0",
"type": "text",
},
],
"id": "nested-paragraph-0",
"props": {
"backgroundColor": "default",
"textAlignment": "left",
"textColor": "default",
},
"type": "paragraph",
},
"prevBlock": undefined,
"source": {
"type": "local",
},
"type": "delete",
},
{
"block": {
"children": [],
"content": [
{
"styles": {},
"text": "Double Nested Paragraph 0",
"type": "text",
},
],
"id": "double-nested-paragraph-0",
"props": {
"backgroundColor": "default",
"textAlignment": "left",
"textColor": "default",
},
"type": "paragraph",
},
"prevBlock": undefined,
"source": {
"type": "local",
},
"type": "delete",
},
]
26 changes: 26 additions & 0 deletions packages/core/src/api/__snapshots__/blocks-deleted.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"block": {
"children": [],
"content": [
{
"styles": {},
"text": "Paragraph 0",
"type": "text",
},
],
"id": "paragraph-0",
"props": {
"backgroundColor": "default",
"textAlignment": "left",
"textColor": "default",
},
"type": "paragraph",
},
"prevBlock": undefined,
"source": {
"type": "local",
},
"type": "delete",
},
]
62 changes: 62 additions & 0 deletions packages/core/src/api/__snapshots__/blocks-inserted-nested.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
[
{
"block": {
"children": [
{
"children": [],
"content": [
{
"styles": {},
"text": "Nested",
"type": "text",
},
],
"id": "1",
"props": {
"backgroundColor": "default",
"textAlignment": "left",
"textColor": "default",
},
"type": "paragraph",
},
],
"content": [],
"id": "0",
"props": {
"backgroundColor": "default",
"textAlignment": "left",
"textColor": "default",
},
"type": "paragraph",
},
"prevBlock": undefined,
"source": {
"type": "local",
},
"type": "insert",
},
{
"block": {
"children": [],
"content": [
{
"styles": {},
"text": "Nested",
"type": "text",
},
],
"id": "1",
"props": {
"backgroundColor": "default",
"textAlignment": "left",
"textColor": "default",
},
"type": "paragraph",
},
"prevBlock": undefined,
"source": {
"type": "local",
},
"type": "insert",
},
]
20 changes: 20 additions & 0 deletions packages/core/src/api/__snapshots__/blocks-inserted.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[
{
"block": {
"children": [],
"content": [],
"id": "0",
"props": {
"backgroundColor": "default",
"textAlignment": "left",
"textColor": "default",
},
"type": "paragraph",
},
"prevBlock": undefined,
"source": {
"type": "local",
},
"type": "insert",
},
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[
{
"block": {
"children": [],
"content": [
{
"styles": {},
"text": "HelloParagraph 2",
"type": "text",
},
],
"id": "paragraph-2",
"props": {
"backgroundColor": "default",
"textAlignment": "left",
"textColor": "default",
},
"type": "paragraph",
},
"prevBlock": {
"children": [],
"content": [
{
"styles": {},
"text": "Paragraph 2",
"type": "text",
},
],
"id": "paragraph-2",
"props": {
"backgroundColor": "default",
"textAlignment": "left",
"textColor": "default",
},
"type": "paragraph",
},
"source": {
"type": "local",
},
"type": "update",
},
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[
{
"block": {
"children": [],
"content": [
{
"styles": {},
"text": "ABC",
"type": "text",
},
],
"id": "0",
"props": {
"backgroundColor": "default",
"textAlignment": "left",
"textColor": "default",
},
"type": "paragraph",
},
"prevBlock": undefined,
"source": {
"type": "local",
},
"type": "insert",
},
{
"block": {
"children": [],
"content": [
{
"styles": {},
"text": "DEF",
"type": "text",
},
],
"id": "1",
"props": {
"backgroundColor": "default",
"textAlignment": "left",
"textColor": "default",
},
"type": "paragraph",
},
"prevBlock": undefined,
"source": {
"type": "local",
},
"type": "insert",
},
]
Loading
Loading