2

我正在使用 prosemiror 构建一个协作编辑器,多个人可以在其中编辑一个文档。我根据此处给出的示例编写了以下代码 - 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 中打开两个选项卡并看到协作发生。我究竟做错了什么?

会喜欢一些反馈。

4

1 回答 1

3

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

于 2017-07-04T20:17:40.323 回答