Sharedb-ace provides two-way bindings between sharedb and ace-editor.
https://www.npmjs.com/package/sharedb-ace
Option 1: Using npm:
npm install sharedb-ace
Option 2: Using pre-compiled js:
TODO: create distributable precompiled js file, put on CDN.
Creating the sharedb-ace
instance:
- wsURL: the websocket URL used for bi-directional communication between shareDB and the browser.
- namespace: namespace used in shareDB server.
- itemID: id of your shareDB item.
var ShareAce = new SharedbAce(wsURL, namespace, itemID);
Setup the ShareDB document as a string:
ShareAce.on('ready', function() {
ShareAce.add(editor);
});
Your setup may be more complex, and requires the use of multiple ace-instances synchronized over one connection. Setup the shareDB document to be a JSON object.
For example:
{
"foo": "",
"bar": ""
}
Next, connect the two ace-instances:
ShareAce.on('ready', function() {
ShareAce.add(editor1, ["foo"]);
ShareAce.add(editor2, ["bar"]);
});
ShareDB is used as the layer for real-time synchronization of text. This is achieved through operational transformation. ShareDB provides adapters for several databases, including MongoDB and postgresql.
Local changes are converted into shareDB ops, and are then sent to the server. These ops are transformed, and propogated to to client and then applied onto Ace text with deltas.
For example, inserting text where | is the cursor:
abc|
abcd|
Produces the following delta and its corresponding op:
delta: {'start':{'row':1,'column':3},'end':{'row':1,'column':4},'action':'insert','lines':['d']}
op: [{"p":["code",3],"si":"d"}]
[Illustration of real-time synchronization]
Here is what happens when Ace1
makes a local change:
onLocalChange
listener onAce1
triggered.delta
transformed to shareDB op, and is sent to server- server accepts
op
, applies OT. -
- Transformed op is propogated to all other listening ace-instances: [
Ace2
]. TODO: this needs factual-checking. is it propogated toAce1
too?
- Transformed op is propogated to all other listening ace-instances: [
Ace2
receives shareDB op from server, triggersonRemoteChange
.- shareDB op transformed to ace delta, applied to editor.
- Application of
delta
triggersonLocalChange
onAce2
, but source isself
, and the operation is suppressed. This prevents a feedback loop.
- Fork or clone this repo:
git clone https://github.com/jethrokuan/sharedb-ace.git
- Install the dependencies (we use yarn):
cd sharedb-ace && yarn install
- Write tests
- Allow for plugins