Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from ren85/master
modifications
- Loading branch information
Showing
1 changed file
with
94 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,144 +1,125 @@ | |||
(function() { | (function () { | ||
//var Range, | |||
var applyToShareJS; | |||
|
|
||
// Range = require("ace/range").Range; | var applyToShareJS; | ||
|
var preActionCodemirrorContent; | ||
applyToShareJS = function(editorDoc, delta, doc) { | applyToShareJS = function (editorDoc, delta, doc) { | ||
var getStartOffsetPosition, pos, text; | var pos, text; | ||
getStartOffsetPosition = function(range) { | change = delta; | ||
var i, line, lines, offset, _len; | while (1) { | ||
lines = editorDoc.getLines(0, range.start.row); | pos = myIndexFromPos(change.from.line, change.from.ch, preActionCodemirrorContent); | ||
offset = 0; | end_pos = myIndexFromPos(change.to.line, change.to.ch, preActionCodemirrorContent); | ||
for (i = 0, _len = lines.length; i < _len; i++) { | action = ''; | ||
line = lines[i]; | if (change.text[0] == "" && change.text.length == 1) { | ||
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) | |||
{ | |||
if (change.from.line != change.to.line) | if (change.from.line != change.to.line) | ||
action = 'removeLines'; | action = 'removeLines'; | ||
else | else | ||
action = 'removeText'; | action = 'removeText'; | ||
end_pos = editorDoc.indexFromPos(change.to); | } | ||
} | else { | ||
else | |||
{ | |||
if (change.text.length > 1) | if (change.text.length > 1) | ||
action = 'insertLines'; | action = 'insertLines'; | ||
else | else | ||
action = 'insertText'; | action = 'insertText'; | ||
} | } | ||
switch (action) { | switch (action) { | ||
case 'insertText': | case 'insertText': | ||
if (pos != end_pos) | |||
doc.del(pos, end_pos - pos); | |||
doc.insert(pos, change.text[0]); | doc.insert(pos, change.text[0]); | ||
break; | break; | ||
case 'removeText': | case 'removeText': | ||
doc.del(pos, end_pos - pos); | doc.del(pos, end_pos - pos); | ||
break; | break; | ||
case 'insertLines': | case 'insertLines': | ||
if (pos != end_pos) | |||
doc.del(pos, end_pos - pos); | |||
text = change.text.join('\n'); | text = change.text.join('\n'); | ||
doc.insert(pos, text); | doc.insert(pos, text); | ||
break; | break; | ||
case 'removeLines': | case 'removeLines': | ||
//text = change.text.join('\n') + '\n'; | doc.del(pos, end_pos - pos); | ||
doc.del(pos,end_pos - pos); | |||
break; | break; | ||
default: | default: | ||
throw new Error("unknown action: " + delta.action); | throw new Error("unknown action: " + delta.action); | ||
} | } | ||
|
|
||
if (!change.next) | preActionCodemirrorContent = doc.getText(); | ||
break; | if (!change.next) | ||
change = 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); | |||
} | } | ||
}, 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; | window.sharejs.Doc.prototype.attach_codemirror = function (editor, keepEditorContents) { | ||
start = editorDoc.posFromIndex(pos); | var check, doc, editorDoc, editorListener, suppress; | ||
editorDoc.replaceRange(text,start); | if (!this.provides['text']) { | ||
//editorDoc.insert(offsetToPos(pos), text); | throw new Error('Only text documents can be attached to CodeMirror'); | ||
suppress = false; | } | ||
return check(); | doc = this; | ||
}); | editorDoc = editor; | ||
doc.on('delete', function(pos, text) { |
|
||
var range; | check = function () { | ||
suppress = true; | return window.setTimeout(function () { | ||
start = editorDoc.posFromIndex(pos); | var editorText, otText; | ||
end = editorDoc.posFromIndex(pos + text.length); | editorText = editorDoc.getValue(); | ||
editorDoc.replaceRange("", start, end); | otText = doc.getText(); | ||
//range = Range.fromPoints(offsetToPos(pos), offsetToPos(pos + text.length)); | if (editorText !== otText) { | ||
//editorDoc.remove(range); | console.error("Texts are out of sync. Most likely this is caused by a bug in this code."); | ||
suppress = false; | } | ||
return check(); | }, 0); | ||
}); | }; | ||
doc.detach_codemirror = function() { | if (keepEditorContents) { | ||
doc.removeListener('remoteop', docListener); | doc.del(0, doc.getText().length); | ||
editorDoc.removeListener('change', editorListener); | doc.insert(0, editorDoc.getValue()); | ||
return delete doc.detach_codemirror; | } 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); |