@@ -16,13 +16,10 @@ export type Unsubscribe = () => void;
1616 */
1717export 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+ }
0 commit comments