Monday, 5 June 2017

Unable to make collaborative editing work using Prosemirror

I am using prosemirror to build a collaborative editor, where multiple people can edit one document. I wrote the following code, based on the example given here - http://prosemirror.net/docs/guides/collab/

Here is the code-

const { EditorState } = require('prosemirror-state'); 
const { EditorView } = require('prosemirror-view');
const { DOMParser } = require("prosemirror-model");
const {schema} = require("./schema");
var collab = require("prosemirror-collab");


function Authority(doc) {
  this.doc = doc
  this.steps = []
  this.stepClientIDs = []
  this.onNewSteps = []
}

Authority.prototype.receiveSteps = function(version, steps, clientID) {
  if (version != this.steps.length) return

  var self = this
  // Apply and accumulate new steps
  steps.forEach(function(step) {
    self.doc = step.apply(self.doc).doc
    self.steps.push(step)
    self.stepClientIDs.push(clientID)
  })
  // Signal listeners
  this.onNewSteps.forEach(function(f) { f() })
}

Authority.prototype.stepsSince = function(version) {
  return {
    steps: this.steps.slice(version),
    clientIDs: this.stepClientIDs.slice(version)
  }
}

var auth = new Authority('');
collabEditor(auth)

function collabEditor(authority) {
  var view = new EditorView(document.querySelector("#editor"), {
    state: EditorState.create({schema: schema, plugins: [collab.collab()]}),
    dispatchTransaction: function(transaction) {
      var newState = view.state.apply(transaction)
      view.updateState(newState)
      var sendable = collab.sendableSteps(newState)
      if (sendable)
        authority.receiveSteps(sendable.version, sendable.steps,
                               sendable.clientID)
    }
  })
   authority.onNewSteps.push(function() {
    var newData = authority.stepsSince(collab.getVersion(view.state))
    view.dispatch(
      collab.receiveTransaction(view.state, newData.steps, newData.clientIDs))
  })

  return view
}

When i run this code (after installing all the dependencies and setting up a simple server in nodejs) I am basically able to edit a text box but I am not able to open two tabs in chrome and see the collaboration happen. What am i doing wrong?

Will love some feedback.



via Adam

No comments:

Post a Comment