项目作者: LevelFourAB

项目描述 :
Collaborative Realtime Editing via Operational Transformation
高级语言: JavaScript
项目地址: git://github.com/LevelFourAB/otter-js.git
创建时间: 2016-08-02T12:29:38Z
项目社区:https://github.com/LevelFourAB/otter-js

开源协议:Apache License 2.0

下载


Otter

Otter is a library to support collaborative realtime editing using
Operational Transformation.
This repository contains the JavaScript-implementation for use both in a browser
and in a Node-based server.

A Java-implementation is also available.

Using Otter

Install via NPM:

npm install --save otter-js

Otter consists of three parts, the operations library, the editing engine and
a high level model. The high level model is what you usually want to use
unless you are implementing something special.

Operations

The lowest level of Otter is the operational transformation algorithms. Otter
supports transformations on maps, lists and strings. There is also a combined
type that can be used to combine several other types based on unique
identifiers. All of these transformations are used together to create the
higher level model.

Engine

The engine contains editing control. It provides support for creating
editors on top of any supported operational transformation.

  1. const string = require('otter-js/operations/string');
  2. const Editor = require('otter-js/engine/editor');
  3. const sync = new YourOperationSync(string.newType(), ...);
  4. const editor = new Editor(sync);
  5. // Connect and do something with the current version
  6. editor.connect()
  7. .then(() => {
  8. editor.current.apply(...);
  9. editor.on('change', function(e) {
  10. // This will receive all operations that occur on the editor
  11. });
  12. });
  13. // Perform an operation
  14. editor.apply(string.delta()
  15. .retain(currentStringLength)
  16. .insert('abc')
  17. .done()
  18. );

Editors require a synchronization helper for sending and receiving operations
from a server. There is intentionally no default implementation of such a sync
as different applications will have different requirements here.

In the end all operations performed by an editor will end up being handled by
an instance of EditorControl.

  1. const EditorControl = require('otter-js/engine/editor-control');
  2. const control = new EditorControl(historyStorage);
  3. control.latest()
  4. .then(latestVersion => {
  5. // Do something with the latest version
  6. });
  7. // When an operation is received from a client it needs to be stored and
  8. // the result needs to be sent back to all clients
  9. control.store(taggedOperation)
  10. .then(op => {
  11. // Op should be sent back to all clients
  12. });

Model

This is the high level API that makes it easier to work with shared editing.
The model provides shared objects of different types that are synchronized
between all editors of the model.

Here is a tiny example of working with the model:

  1. const combined = require('otter-js/operations/combined');
  2. const Editor = require('otter-js/engine/editor');
  3. const Model = require('otter-js/model');
  4. const sync = new YourOperationSync(combined.newType(), ...);
  5. const editor = new Editor(sync);
  6. const model = new Model(editor);
  7. model.open()
  8. .then(function() {
  9. // Create a new string and store it in the root map
  10. const title = model.newString();
  11. title.set('Cookies are tasty');
  12. model.set('title', title);
  13. // Set a primitive value in the map
  14. model.set('priority', 10);
  15. });