Skip to content

Commit add965f

Browse files
committed
feat: do not include changes from remote
1 parent aec52e8 commit add965f

File tree

4 files changed

+55
-32
lines changed

4 files changed

+55
-32
lines changed

packages/core/src/editor/BlockNoteEditor.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1616,8 +1616,13 @@ export class BlockNoteEditor<
16161616
getChanges(): BlocksChanged<BSchema, ISchema, SSchema>;
16171617
},
16181618
) => void,
1619+
/**
1620+
* If true, the callback will be triggered when the changes are caused by a remote user
1621+
* @default true
1622+
*/
1623+
includeUpdatesFromRemote?: boolean,
16191624
) {
1620-
return this._eventManager.onChange(callback);
1625+
return this._eventManager.onChange(callback, includeUpdatesFromRemote);
16211626
}
16221627

16231628
/**

packages/core/src/editor/managers/EventManager.ts

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@ export type Unsubscribe = () => void;
1616
*/
1717
export class EventManager<Editor extends BlockNoteEditor> extends EventEmitter<{
1818
onChange: [
19-
editor: Editor,
2019
ctx: {
21-
getChanges(): BlocksChanged<
22-
Editor["schema"]["blockSchema"],
23-
Editor["schema"]["inlineContentSchema"],
24-
Editor["schema"]["styleSchema"]
25-
>;
20+
editor: Editor;
21+
transaction: Transaction;
22+
appendedTransactions: Transaction[];
2623
},
2724
];
2825
onSelectionChange: [ctx: { editor: Editor; transaction: Transaction }];
@@ -37,14 +34,7 @@ export class EventManager<Editor extends BlockNoteEditor> extends EventEmitter<{
3734
editor._tiptapEditor.on(
3835
"update",
3936
({ transaction, appendedTransactions }) => {
40-
this.emit("onChange", editor, {
41-
getChanges() {
42-
return getBlocksChangedByTransaction(
43-
transaction,
44-
appendedTransactions,
45-
);
46-
},
47-
});
37+
this.emit("onChange", { editor, transaction, appendedTransactions });
4838
},
4939
);
5040
editor._tiptapEditor.on("selectionUpdate", ({ transaction }) => {
@@ -73,11 +63,36 @@ export class EventManager<Editor extends BlockNoteEditor> extends EventEmitter<{
7363
>;
7464
},
7565
) => void,
66+
/**
67+
* If true, the callback will be triggered when the changes are caused by a remote user
68+
* @default true
69+
*/
70+
includeUpdatesFromRemote = true,
7671
): Unsubscribe {
77-
this.on("onChange", callback);
72+
const cb = ({
73+
transaction,
74+
appendedTransactions,
75+
}: {
76+
transaction: Transaction;
77+
appendedTransactions: Transaction[];
78+
}) => {
79+
if (!includeUpdatesFromRemote && isRemoteTransaction(transaction)) {
80+
// don't trigger the callback if the changes are caused by a remote user
81+
return;
82+
}
83+
callback(this.editor, {
84+
getChanges() {
85+
return getBlocksChangedByTransaction(
86+
transaction,
87+
appendedTransactions,
88+
);
89+
},
90+
});
91+
};
92+
this.on("onChange", cb);
7893

7994
return () => {
80-
this.off("onChange", callback);
95+
this.off("onChange", cb);
8196
};
8297
}
8398

@@ -93,11 +108,10 @@ export class EventManager<Editor extends BlockNoteEditor> extends EventEmitter<{
93108
): Unsubscribe {
94109
const cb = (e: { transaction: Transaction }) => {
95110
if (
96-
e.transaction.getMeta("$y-sync") &&
97-
!includeSelectionChangedByRemote
111+
!includeSelectionChangedByRemote &&
112+
isRemoteTransaction(e.transaction)
98113
) {
99-
// selection changed because of a yjs sync (i.e.: other user was typing)
100-
// we don't want to trigger the callback in this case
114+
// don't trigger the callback if the selection changed because of a remote user
101115
return;
102116
}
103117
callback(this.editor);
@@ -132,3 +146,7 @@ export class EventManager<Editor extends BlockNoteEditor> extends EventEmitter<{
132146
};
133147
}
134148
}
149+
150+
function isRemoteTransaction(transaction: Transaction): boolean {
151+
return !!transaction.getMeta("$y-sync");
152+
}

packages/core/src/editor/managers/extensions/types.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,11 @@ type InputRule = {
120120
}) => undefined | PartialBlockNoDefaults<any, any, any>;
121121
};
122122

123-
export type ExtensionFactory<Key extends string = string, State = any> = (
124-
editor: BlockNoteEditor,
125-
) => Extension<Key, State>;
123+
export type ExtensionFactory<
124+
Key extends string = string,
125+
State = any,
126+
Options = any,
127+
> = (editor: BlockNoteEditor, options: Options) => Extension<Key, State>;
126128

127129
/**
128130
* Helper function to create a BlockNote extension.

packages/core/src/extensions/FilePanel/Extension.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@ export const FilePanelExtension = createExtension((editor) => {
1818
});
1919
}
2020

21-
// reset the menu when the document changes (non-remote)
22-
editor.onChange((_e, { getChanges }) => {
23-
if (getChanges().some((change) => change.source.type === "yjs-remote")) {
24-
return;
25-
}
26-
// If the changes are not from remote, we should close the menu
27-
closeMenu();
28-
});
21+
// reset the menu when the document changes
22+
editor.onChange(
23+
closeMenu,
24+
// don't trigger the callback if the changes are caused by a remote user
25+
false,
26+
);
2927

3028
// reset the menu when the selection changes
3129
editor.onSelectionChange(closeMenu);

0 commit comments

Comments
 (0)