无法使用Prosemirror进行协作编辑工作

时间:2017-06-05 09:35:18

标签: javascript node.js editor prose-mirror

我正在使用prosemirror来构建协作编辑器,其中多人可以编辑一个文档。我根据此处给出的示例编写了以下代码 - http://prosemirror.net/docs/guides/collab/

这是代码 -

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
}

当我运行此代码时(在安装所有依赖项并在nodejs中设置一个简单的服务器之后)我基本上能够编辑文本框但是我无法在chrome中打开两个选项卡并看到协作发生。我做错了什么?

会喜欢一些反馈。

1 个答案:

答案 0 :(得分:0)

这是简单的单页无外部通信设置的示例代码。因此,不,它不会与其他标签进行通信。为此,您必须将权限移动到其他位置并设置页面以通过HTTP或websockets实际与其进行通信。 (例如参见this demo。)