I’m trying to build a collaborative wiki-like functionality for a web-app using CodeMirror 6. However, I have a bit of a hard time understanding how to use the collab extension.
I’ve got the editor set up on the client side, and passed in a collab extension created with collabExt = collab({startVersion: wikiPageVersionFromServer, clientId: userName})
. I listen to editor events and try to retrieve changes to send to the server by adding an extension along the lines of:
listenerExtension = EditorView.updateListener.of(editorUpdate => {
const updates = sendableUpdates(this.editorState)
this.sendUpdatesToServer(updates)
})
And I pass in the wiki page text from the server and the extensions when creating the editor state: EditorState.create({doc: wikiPageText, extensions: [collabExt, listenerExtension, otherExtensions]})
.
However, the sendableUpdates function seems to always return an empty array in the listener code above, even when editorUpdate
contains the characters typed in the editor (verified by logging them).
In addition, there were some other parts of the collab API that I didn’t quite understand, despite trying to glance at the source. My questions are:
- How and when is
sendableUpdates
supposed to be used? - The receiveUpdates
(state: EditorState, updates: readonly Update[], ownUpdateCount: number) → Transaction
function from the collab package takes anownUpdateCount
parameter, what should be passed in here? It’s not the document version number, is it? From looking at the code, it seems to be used to skip entries in the update array? - Is the following general understanding of using the collab API correct?:
- The clients should listen to changes in their editors and use
sendableUpdates
to get a list of updates, - then send the updates to the server along with the version number retrieved with getSyncedVersion,
- the server checks that the version number matches with its latest one, applies the updates to a Text object it keeps, and increases its version number by the number of updates,
- then the server sends the updates to other clients (but not the client where they originated?), who use
receiveUpdates
to apply the list of updates to their editor states. - (In case the server had a higher version number, it rejects the update and tells the client, which applies the latest changes from the server with
receiveUpdates
and sends the old updates plus any new updates retrieved withsendableUpdates
to the server along with it’s updatedgetSyncedVersion
)
- The clients should listen to changes in their editors and use
In general I found the CodeMirror 6 architecture very elegant and nice to work with, despite a bit of a learning curve. It would be very nice if using the collab extension was documented in more detail, either in the guide or in an example, as I believe it’s a great strength of the library (that’s why I ended up using CodeMirror 6 and not trying to implement OT on my own).