ace.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Generated by CoffeeScript 1.4.0
  2. (function() {
  3. var Range, applyToShareJS;
  4. Range = require("ace/range").Range;
  5. applyToShareJS = function(editorDoc, delta, doc) {
  6. var getStartOffsetPosition, pos, text;
  7. getStartOffsetPosition = function(range) {
  8. var i, line, lines, offset, _i, _len;
  9. lines = editorDoc.getLines(0, range.start.row);
  10. offset = 0;
  11. for (i = _i = 0, _len = lines.length; _i < _len; i = ++_i) {
  12. line = lines[i];
  13. offset += i < range.start.row ? line.length : range.start.column;
  14. }
  15. return offset + range.start.row;
  16. };
  17. pos = getStartOffsetPosition(delta.range);
  18. switch (delta.action) {
  19. case 'insertText':
  20. doc.insert(pos, delta.text);
  21. break;
  22. case 'removeText':
  23. doc.del(pos, delta.text.length);
  24. break;
  25. case 'insertLines':
  26. text = delta.lines.join('\n') + '\n';
  27. doc.insert(pos, text);
  28. break;
  29. case 'removeLines':
  30. text = delta.lines.join('\n') + '\n';
  31. doc.del(pos, text.length);
  32. break;
  33. default:
  34. throw new Error("unknown action: " + delta.action);
  35. }
  36. };
  37. window.sharejs.extendDoc('attach_ace', function(editor, keepEditorContents) {
  38. var check, doc, docListener, editorDoc, editorListener, offsetToPos, suppress;
  39. if (!this.provides['text']) {
  40. throw new Error('Only text documents can be attached to ace');
  41. }
  42. doc = this;
  43. editorDoc = editor.getSession().getDocument();
  44. editorDoc.setNewLineMode('unix');
  45. check = function() {
  46. return window.setTimeout(function() {
  47. var editorText, otText;
  48. editorText = editorDoc.getValue();
  49. otText = doc.getText();
  50. if (editorText !== otText) {
  51. console.error("Text does not match!");
  52. console.error("editor: " + editorText);
  53. return console.error("ot: " + otText);
  54. }
  55. }, 0);
  56. };
  57. if (keepEditorContents) {
  58. doc.del(0, doc.getText().length);
  59. doc.insert(0, editorDoc.getValue());
  60. } else {
  61. editorDoc.setValue(doc.getText());
  62. }
  63. check();
  64. suppress = false;
  65. editorListener = function(change) {
  66. if (suppress) {
  67. return;
  68. }
  69. applyToShareJS(editorDoc, change.data, doc);
  70. return check();
  71. };
  72. editorDoc.on('change', editorListener);
  73. docListener = function(op) {
  74. suppress = true;
  75. applyToDoc(editorDoc, op);
  76. suppress = false;
  77. return check();
  78. };
  79. offsetToPos = function(offset) {
  80. var line, lines, row, _i, _len;
  81. lines = editorDoc.getAllLines();
  82. row = 0;
  83. for (row = _i = 0, _len = lines.length; _i < _len; row = ++_i) {
  84. line = lines[row];
  85. if (offset <= line.length) {
  86. break;
  87. }
  88. offset -= lines[row].length + 1;
  89. }
  90. return {
  91. row: row,
  92. column: offset
  93. };
  94. };
  95. doc.on('insert', function(pos, text) {
  96. suppress = true;
  97. editorDoc.insert(offsetToPos(pos), text);
  98. suppress = false;
  99. return check();
  100. });
  101. doc.on('delete', function(pos, text) {
  102. var range;
  103. suppress = true;
  104. range = Range.fromPoints(offsetToPos(pos), offsetToPos(pos + text.length));
  105. editorDoc.remove(range);
  106. suppress = false;
  107. return check();
  108. });
  109. doc.detach_ace = function() {
  110. doc.removeListener('remoteop', docListener);
  111. editorDoc.removeListener('change', editorListener);
  112. return delete doc.detach_ace;
  113. };
  114. });
  115. }).call(this);