Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

modifications #1

Merged
merged 1 commit into from

2 participants

@ren85

changed a few things to make it work in all cases. Tested with codemirror 2.23. Example of usage at rextester.com

@tarr11 tarr11 merged commit ee047e3 into tarr11:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 5, 2012
  1. @ren85
This page is out of date. Refresh to see the latest.
Showing with 94 additions and 113 deletions.
  1. +94 −113 codemirror/codemirror_attach.js
View
207 codemirror/codemirror_attach.js
@@ -1,36 +1,21 @@
-(function() {
- //var Range,
- var applyToShareJS;
+(function () {
-// Range = require("ace/range").Range;
-
- applyToShareJS = function(editorDoc, delta, doc) {
- var getStartOffsetPosition, pos, text;
- getStartOffsetPosition = function(range) {
- var i, line, lines, offset, _len;
- lines = editorDoc.getLines(0, range.start.row);
- offset = 0;
- for (i = 0, _len = lines.length; i < _len; i++) {
- line = lines[i];
- offset += i < range.start.row ? line.length : range.start.column;
- }
- return offset + range.start.row;
- };
- change = delta;
- while (1)
- {
- pos = editorDoc.indexFromPos(change.from);
- action = 'insertText';
- if (change.text[0] == "" && change.text.length == 1)
- {
+ var applyToShareJS;
+ var preActionCodemirrorContent;
+ 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';
- end_pos = editorDoc.indexFromPos(change.to);
- }
- else
- {
+ }
+ else {
if (change.text.length > 1)
action = 'insertLines';
else
@@ -38,107 +23,103 @@
}
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':
- //text = change.text.join('\n') + '\n';
- doc.del(pos,end_pos - pos);
+ doc.del(pos, end_pos - pos);
break;
default:
throw new Error("unknown action: " + delta.action);
}
- if (!change.next)
- break;
- change = change.next;
- }
- };
-
- window.sharejs.Doc.prototype.attach_codemirror = function(editor, keepEditorContents) {
- var check, doc, docListener, editorDoc, editorListener, offsetToPos, suppress;
- if (!this.provides['text']) {
- throw new Error('Only text documents can be attached to CodeMirror');
- }
- doc = this;
- editorDoc = editor;//.getSession().getDocument();
- //editorDoc.setNewLineMode('unix');
- check = function() {
- return window.setTimeout(function() {
- var editorText, otText;
- editorText = editorDoc.getValue();
- otText = doc.getText();
- if (editorText !== otText) {
- console.error("Text does not match!");
- console.error("editor: " + editorText);
- return console.error("ot: " + otText);
+ preActionCodemirrorContent = doc.getText();
+ if (!change.next)
+ break;
+ change = change.next;
}
- }, 0);
- };
- if (keepEditorContents) {
- doc.del(0, doc.getText().length);
- doc.insert(0, editorDoc.getValue());
- } else {
- editorDoc.setValue(doc.getText());
- }
- check();
- suppress = false;
- editorListener = function(change, tc) {
- if (suppress) return;
- applyToShareJS(editorDoc, tc, doc);
- return check();
- };
- editorDoc.setOption("onChange", editorListener);
- docListener = function(op) {
- suppress = true;
- applyToDoc(editorDoc, op);
- suppress = false;
- return check();
- };
- offsetToPos = function(offset) {
- var line, lines, row, _len;
- lineCount = editorDoc.lineCount();
- row = 0;
- for (row = 0, _len = lineCount; row < _len; row++) {
- line = editorDoc.getLine(row);
- if (offset <= line.length) break;
- offset -= line.length + 1;
- }
- return {
- row: row,
- column: offset
- };
};
- doc.on('insert', function(pos, text) {
- suppress = true;
- start = editorDoc.posFromIndex(pos);
- editorDoc.replaceRange(text,start);
- //editorDoc.insert(offsetToPos(pos), text);
- suppress = false;
- return check();
- });
- doc.on('delete', function(pos, text) {
- var range;
- suppress = true;
- start = editorDoc.posFromIndex(pos);
- end = editorDoc.posFromIndex(pos + text.length);
- editorDoc.replaceRange("", start, end);
- //range = Range.fromPoints(offsetToPos(pos), offsetToPos(pos + text.length));
- //editorDoc.remove(range);
- suppress = false;
- return check();
- });
- doc.detach_codemirror = function() {
- doc.removeListener('remoteop', docListener);
- editorDoc.removeListener('change', editorListener);
- return delete doc.detach_codemirror;
+
+ 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');
+ }
+ doc = this;
+ editorDoc = editor;
+
+ check = function () {
+ return window.setTimeout(function () {
+ var editorText, otText;
+ editorText = editorDoc.getValue();
+ otText = doc.getText();
+ if (editorText !== otText) {
+ console.error("Texts are out of sync. Most likely this is caused by a bug in this code.");
+ }
+ }, 0);
+ };
+ if (keepEditorContents) {
+ doc.del(0, doc.getText().length);
+ doc.insert(0, editorDoc.getValue());
+ } else {
+ editorDoc.setValue(doc.getText());
+ }
+ preActionCodemirrorContent = editorDoc.getValue();
+ check();
+ suppress = false;
+ editorListener = function (change, tc) {
+ if (suppress) return;
+ applyToShareJS(editorDoc, tc, doc);
+ return check();
+ };
+ 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;
+ start = editorDoc.posFromIndex(pos);
+ editorDoc.replaceRange(text, start);
+ suppress = false;
+ preActionCodemirrorContent = editorDoc.getValue();
+ return check();
+ });
+ doc.on('delete', function (pos, text) {
+ var range;
+ suppress = true;
+ start = editorDoc.posFromIndex(pos);
+ end = editorDoc.posFromIndex(pos + text.length);
+ editorDoc.replaceRange("", start, end);
+ suppress = false;
+ preActionCodemirrorContent = editorDoc.getValue();
+ return check();
+ });
+ doc.detach_codemirror = function () {
+ editorDoc.removeListener('change', editorListener);
+ return delete doc.detach_codemirror;
+ };
};
- };
-}).call(this);
+}).call(this);
Something went wrong with that request. Please try again.