2
2
*@module bindings/quill
3
3
*/
4
4
5
- import { createMutex } from 'lib0/mutex.js'
6
5
import * as Y from 'yjs' // eslint-disable-line
7
6
import { Awareness } from 'y-protocols/awareness.js' // eslint-disable-line
8
7
@@ -59,9 +58,7 @@ export class QuillBinding {
59
58
*@param {Awareness } [awareness]
60
59
*/
61
60
constructor ( type , quill , awareness ) {
62
- const mux = createMutex ( )
63
61
const doc = /**@type {Y.Doc } */ ( type . doc )
64
- this . mux = mux
65
62
this . type = type
66
63
this . doc = doc
67
64
this . quill = quill
@@ -82,8 +79,11 @@ export class QuillBinding {
82
79
quillCursors . removeCursor ( id . toString ( ) )
83
80
} )
84
81
}
82
+ /**
83
+ *@param {Y.YTextEvent } event
84
+ */
85
85
this . _typeObserver = event => {
86
- mux ( ( ) => {
86
+ if ( event . transaction . origin !== this ) {
87
87
const eventDelta = event . delta
88
88
// We always explicitly set attributes, otherwise concurrent edits may
89
89
// result in quill assuming that a text insertion shall inherit existing
@@ -97,11 +97,11 @@ export class QuillBinding {
97
97
delta . push ( d )
98
98
}
99
99
}
100
- quill . updateContents ( delta , 'yjs' )
101
- } )
100
+ quill . updateContents ( delta , this )
101
+ }
102
102
}
103
103
type . observe ( this . _typeObserver )
104
- this . _quillObserver = ( eventType , delta ) => {
104
+ this . _quillObserver = ( eventType , delta , state , origin ) => {
105
105
if ( delta && delta . ops ) {
106
106
// update content
107
107
const ops = delta . ops
@@ -114,9 +114,11 @@ export class QuillBinding {
114
114
}
115
115
}
116
116
} )
117
- mux ( ( ) => {
118
- type . applyDelta ( ops )
119
- } )
117
+ if ( origin !== this ) {
118
+ doc . transact ( ( ) => {
119
+ type . applyDelta ( ops )
120
+ } , this )
121
+ }
120
122
}
121
123
// always check selection
122
124
if ( awareness && quillCursors ) {
@@ -143,11 +145,9 @@ export class QuillBinding {
143
145
}
144
146
}
145
147
quill . on ( 'editor-change' , this . _quillObserver )
146
- mux ( ( ) => {
147
- // This indirectly initializes _negatedUsedFormats.
148
- // Make sure that this call this after the _quillObserver is set.
149
- quill . setContents ( type . toDelta ( ) )
150
- } )
148
+ // This indirectly initializes _negatedUsedFormats.
149
+ // Make sure that this call this after the _quillObserver is set.
150
+ quill . setContents ( type . toDelta ( ) , this )
151
151
// init remote cursors
152
152
if ( quillCursors !== null && awareness ) {
153
153
awareness . getStates ( ) . forEach ( ( aw , clientId ) => {