Skip to content

Instantly share code, notes, and snippets.

@thebookworm101
Created December 12, 2013 14:03
Show Gist options
  • Save thebookworm101/7928391 to your computer and use it in GitHub Desktop.
Save thebookworm101/7928391 to your computer and use it in GitHub Desktop.
This is what i did to get the ace editor working with sharejs @0.7
/*
Most stuff up here seemed to need no change the orginal unmodified may be found here:
https://github.com/share/ShareJS/blob/0.6/webclient/ace.js
ideally you should be able to use it thus:
sjs = new window.sharejs.Connection(sock);
var doc = sjs.get('users', 'blah ' + Math.random());
doc.attach_ace(editor); // editor here here is some ace editor object i.e: var editor = ace.edit("editor");
###############
// i'v deleted content of stuff i did not change to keep this short, so do not delete!
###############
.
.
.
*/
sharejs.Doc.prototype['attach_ace'] = function(editor, keepEditorContents) {
var check, deleteListener, doc, docListener, editorDoc, editorListener, insertListener, offsetToPos, refreshListener, replaceTokenizer, suppress;
if (!this.provides['text']) {}
var doc = this.createContext() ; // @ changed
var editorDoc = editor.getSession().getDocument();
editorDoc.setNewLineMode('unix');
check = function() {
return window.setTimeout(function() {
var editorText, otText;
editorText = editorDoc.getValue();
otText = doc.get(); // @ changed
if (editorText.length !=0 && typeof otText != 'undefined' && editorText !==otText ) { //@ changed
console.error("Text does not match!");
console.error("editor: " + editorText);
return console.error("ot: " + otText);
}
}, 0);
};
if (keepEditorContents) {
doc.del(0, doc.get().length); // @ changed
doc.insert(0, editorDoc.getValue());
} else {
editorDoc.setValue(doc.get()); // @ changed
}
check();
suppress = false;
editorListener = function(change) {}; // i'v deleted content of stuff i did not change to keep this short, so do not delete!
replaceTokenizer = function() {}; // i'v deleted content of stuff i did not change to keep this short, so do not delete!
if (doc.getAttributes != null) {
replaceTokenizer();
}
editorDoc.on('change', editorListener);
docListener = function(op) {}; // i'v deleted content of functions i did not change to keep this short, so do not delete!
offsetToPos = function(offset) {};// i'v deleted content of functions i did not change to keep this short, so do not delete!
doc.onInsert = function(pos, text) {};
doc.onRemove = function(pos, text_len) {};
doc.onRefresh = function(startoffset, length) {};
doc.detach_ace = function() {
editorDoc.removeListener('change', editorListener);
}; // @ you may need to fix this funtion, i ignored it (coomented out all the removelisteners except the one left)
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment