New version of ShareJS
I”ve pushed a new version of sharejs tonight, now with type-specific API support.
Previously, you could write this:
sharejs.open('hello', 'text', function(doc, error) {
// Insert some text
doc.submitOp([{i:"Hi there!\n", p:0}]);
// Print the document's contents
console.log(doc.snapshot);
// Parse incoming operations
doc.on('remoteop', function(op) { ... });
});
… and now you can do this:
sharejs.open('hello', 'text', function(doc, error) {
// Insert some text
doc.insert("Hi there!\n", 0);
// Print the document's contents
console.log(doc.getText());
// Parse incoming operations
doc.on('insert', function(text, pos) { ... });
doc.on('delete', function(text, pos) { ... });
});
Voila!
That doesn’t look like much, but it means that you’re insulated from the specifics of how ops are constructed. … And so is the editor; so now swapping between text implementations is just a matter of asking for a different type. By magic, sharejs’s ace editor component now supports editing TP2 documents as well.
I’m planning on using the same system to make editing JSON documents less awful. (Jeremy’s idea.) At the moment, you’ve got to construct ops by hand, and the op format is uuugly. We should now be able to just add a few functions in the right place and JSON documents will be magically endowed with sane editing capacity.