简体   繁体   English

如何结合Quill Rich Text Editor和socket.io来交换Deltas

[英]How to combine Quill Rich Text Editor and socket.io to exchange Deltas

I am trying to combine Quill Rich Text Editor and socket.io. 我正在尝试将Quill Rich Text Editor和socket.io结合起来。 I would like to have an editor similar to Google docs, where people can edit simultaneously. 我希望有一个类似于Google文档的编辑器,人们可以同时编辑。

I am struggling to send and apply 'text-change' events across the wire, using code similar to this: 我正在努力发送并在线上发送“文本更改”事件,使用类似于此的代码:

fullEditor.on('text-change', function(delta, source) {
  if (source === 'user') {
    socket.emit('text change', {'who': my_id, 'delta': JSON.stringify(delta)});
  }
});


socket.on('text change', function(msg){
  if(msg.who != my_id) {
      var del = JSON.parse(msg.delta);
      var Delta = fullEditor.getContents().constructor;
      var delta = new Delta(del.startLength,del.endLength,del.ops);
      fullEditor.updateContents( delta );
    }
    });

This is failing with 这是失败的

Uncaught TypeError: undefined is not a function | 未捕获的TypeError:undefined不是函数| quill.js:8020 quill.js:8020

as on the other end I have a simple hash, and quill expects objects of specific type (InsertOp, http://quilljs.com/docs/editor/deltas/ etc.). 因为在另一端我有一个简单的哈希,并且quill期望特定类型的对象(InsertOp, http ://quilljs.com/docs/editor/deltas/等)。

Any ideas how to make it work? 任何想法如何使其工作?

The problem is that updateContents is expecting a Delta object and while you do create one, the Delta constructor is expected an array of Operations objects. 问题是updateContents期望一个Delta对象,当你创建一个Delta对象时,Delta构造函数应该是一个Operations对象数组。

The newest version (v0.14.0) updates updateContents to take a plain javascript object so you should be able to do: 最新版本(v0.14.0)更新updateContents以获取普通的javascript对象,因此您应该能够:

socket.on('text change', function(msg){
  if(msg.who != my_id) {
    var del = JSON.parse(msg.delta);
    fullEditor.updateContents( del );
  }
});

Note to implement realtime collaboration like Google Docs you will also need some sort of conflict resolution. 请注意,要实施Google Docs等实时协作,您还需要进行某种冲突解决。 The simplest is a platformized solution like GoInstant's OT API or you can roll your own with a library like ShareJS . 最简单的是像GoInstant的OT API这样的平台化解决方案,或者您可以使用像ShareJS这样的库来实现自己的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM