Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 6 additions & 15 deletions examples/cm/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,15 @@
<script src="/share/share.uncompressed.js"></script>
<script src="/share/cm.js"></script>
<script>
var doc = null, editor = null;

function setDoc(docName) {
document.title = docName;

sharejs.open(docName, function(error, newDoc) {
if (doc !== null) {
doc.close();
doc.detach_ace();
}

doc = newDoc;

if (error) {
console.error(error);
return;
}
doc.attach_cm(editor);
sharejs.open(docName, 'text', function(error, newDoc) {
newDoc.attach_codemirror(editor);
// uncomment following lines if you want codemirror's onChange
// doc.onChange = function () {
// console.log('editor changed');
// };
});
};

Expand Down
158 changes: 100 additions & 58 deletions webclient/cm.js
Original file line number Diff line number Diff line change
@@ -1,88 +1,130 @@
// Generated by CoffeeScript 1.3.3
(function() {
(function () {
var applyToShareJS;
var preActionCodemirrorContent;

applyToShareJS = function(editorDoc, delta, doc) {
var delLen, i, startPos;
startPos = 0;
i = 0;
while (i < delta.from.line) {
startPos += editorDoc.lineInfo(i).text.length + 1;
i++;
}
startPos += delta.from.ch;
if (delta.to.line === delta.from.line && delta.to.ch === delta.from.ch) {
doc.insert(startPos, delta.text.join('\n'));
} else {
delLen = delta.to.ch - delta.from.ch;
while (i < delta.to.line) {
delLen += editorDoc.lineInfo(i).text.length + 1;
i++;
applyToShareJS = function (editorDoc, delta, doc) {
var pos, text;
change = delta;
while (1) {
pos = myIndexFromPos(change.from.line, change.from.ch, preActionCodemirrorContent);
end_pos = myIndexFromPos(change.to.line, change.to.ch, preActionCodemirrorContent);
action = '';
if (change.text[0] == "" && change.text.length == 1) {
if (change.from.line != change.to.line)
action = 'removeLines';
else
action = 'removeText';
}
doc.del(startPos, delLen);
if (delta.text) {
doc.insert(startPos, delta.text.join('\n'));
else {
if (change.text.length > 1)
action = 'insertLines';
else
action = 'insertText';
}
}
if (delta.next) {
return applyToShareJS(editorDoc, delta.next, doc);
switch (action) {
case 'insertText':
if (pos != end_pos)
doc.del(pos, end_pos - pos);
doc.insert(pos, change.text[0]);
break;
case 'removeText':
doc.del(pos, end_pos - pos);
break;
case 'insertLines':
if (pos != end_pos)
doc.del(pos, end_pos - pos);
text = change.text.join('\n');
doc.insert(pos, text);
break;
case 'removeLines':
doc.del(pos, end_pos - pos);
break;
default:
throw new Error("unknown action: " + delta.action);
}

preActionCodemirrorContent = doc.getText();
if (!change.next)
break;
change = change.next;
}
};

window.sharejs.extendDoc('attach_cm', function(editor, keepEditorContents) {
var check, editorListener, sharedoc, suppress;
if (!this.provides.text) {
throw new Error('Only text documents can be attached to CodeMirror2');
window.sharejs.Doc.prototype.attach_codemirror = function (editor, keepEditorContents) {
var check, doc, editorDoc, editorListener, suppress;
if (!this.provides['text']) {
throw new Error('Only text documents can be attached to CodeMirror');
}
sharedoc = this;
check = function() {
return window.setTimeout(function() {
doc = this;
editorDoc = editor;

check = function () {
return window.setTimeout(function () {
var editorText, otText;
editorText = editor.getValue();
otText = sharedoc.getText();
editorText = editorDoc.getValue();
otText = doc.getText();
if (editorText !== otText) {
console.error("Text does not match!");
console.error("editor: " + editorText);
console.error("ot: " + otText);
return editor.setValue(sharedoc.getText());
console.error("Texts are out of sync. Most likely this is caused by a bug in this code.");
}
}, 0);
};
if (keepEditorContents) {
this.del(0, sharedoc.getText().length);
this.insert(0, editor.getValue());
doc.del(0, doc.getText().length);
doc.insert(0, editorDoc.getValue());
} else {
editor.setValue(sharedoc.getText());
editorDoc.setValue(doc.getText());
}
preActionCodemirrorContent = editorDoc.getValue();
check();
suppress = false;
editorListener = function(ed, change) {
if (suppress) {
return;
}
applyToShareJS(editor, change, sharedoc);

doc.onChange = function () {
}

editorListener = function (change, tc) {
if (suppress) return;
applyToShareJS(editorDoc, tc, doc);
doc.onChange();
return check();
};
editor.setOption('onChange', editorListener);
this.on('insert', function(pos, text) {
editorDoc.setOption("onChange", editorListener);
myIndexFromPos = function (line, ch, value) {
myIndex = 0;
count = 0;
lines = value.split("\n");
for (i = 0; i < lines.length; i++) {
if (count < line)
myIndex += lines[i].length + 1
else {
myIndex += ch;
break;
}
count++;
}
return myIndex;
}
doc.on('insert', function (pos, text) {
suppress = true;
editor.replaceRange(text, editor.posFromIndex(pos));
start = editorDoc.posFromIndex(pos);
editorDoc.replaceRange(text, start);
suppress = false;
preActionCodemirrorContent = editorDoc.getValue();
return check();
});
this.on('delete', function(pos, text) {
var from, to;
doc.on('delete', function (pos, text) {
var range;
suppress = true;
from = editor.posFromIndex(pos);
to = editor.posFromIndex(pos + text.length);
editor.replaceRange('', from, to);
start = editorDoc.posFromIndex(pos);
end = editorDoc.posFromIndex(pos + text.length);
editorDoc.replaceRange("", start, end);
suppress = false;
preActionCodemirrorContent = editorDoc.getValue();
return check();
});
this.detach_cm = function() {
editor.setOption('onChange', null);
return delete this.detach_cm;
doc.detach_codemirror = function () {
editorDoc.removeListener('change', editorListener);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be editorDoc.setOption('onChange', null) for codemirror 2+

return delete doc.detach_codemirror;
};
});
};

}).call(this);
}).call(this);