ace.coffee 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # This is some utility code to connect an ace editor to a sharejs document.
  2. Range = require("ace/range").Range
  3. # Convert an ace delta into an op understood by share.js
  4. applyToShareJS = (editorDoc, delta, doc) ->
  5. # Get the start position of the range, in no. of characters
  6. getStartOffsetPosition = (range) ->
  7. # This is quite inefficient - getLines makes a copy of the entire
  8. # lines array in the document. It would be nice if we could just
  9. # access them directly.
  10. lines = editorDoc.getLines 0, range.start.row
  11. offset = 0
  12. for line, i in lines
  13. offset += if i < range.start.row
  14. line.length
  15. else
  16. range.start.column
  17. # Add the row number to include newlines.
  18. offset + range.start.row
  19. pos = getStartOffsetPosition(delta.range)
  20. switch delta.action
  21. when 'insertText' then doc.insert pos, delta.text
  22. when 'removeText' then doc.del pos, delta.text.length
  23. when 'insertLines'
  24. text = delta.lines.join('\n') + '\n'
  25. doc.insert pos, text
  26. when 'removeLines'
  27. text = delta.lines.join('\n') + '\n'
  28. doc.del pos, text.length
  29. else throw new Error "unknown action: #{delta.action}"
  30. return
  31. # Attach an ace editor to the document. The editor's contents are replaced
  32. # with the document's contents unless keepEditorContents is true. (In which case the document's
  33. # contents are nuked and replaced with the editor's).
  34. window.sharejs.extendDoc 'attach_ace', (editor, keepEditorContents) ->
  35. throw new Error 'Only text documents can be attached to ace' unless @provides['text']
  36. doc = this
  37. editorDoc = editor.getSession().getDocument()
  38. editorDoc.setNewLineMode 'unix'
  39. check = ->
  40. window.setTimeout ->
  41. editorText = editorDoc.getValue()
  42. otText = doc.getText()
  43. if editorText != otText
  44. console.error "Text does not match!"
  45. console.error "editor: #{editorText}"
  46. console.error "ot: #{otText}"
  47. # Should probably also replace the editor text with the doc snapshot.
  48. , 0
  49. if keepEditorContents
  50. doc.del 0, doc.getText().length
  51. doc.insert 0, editorDoc.getValue()
  52. else
  53. editorDoc.setValue doc.getText()
  54. check()
  55. # When we apply ops from sharejs, ace emits edit events. We need to ignore those
  56. # to prevent an infinite typing loop.
  57. suppress = false
  58. # Listen for edits in ace
  59. editorListener = (change) ->
  60. return if suppress
  61. applyToShareJS editorDoc, change.data, doc
  62. check()
  63. editorDoc.on 'change', editorListener
  64. # Listen for remote ops on the sharejs document
  65. docListener = (op) ->
  66. suppress = true
  67. applyToDoc editorDoc, op
  68. suppress = false
  69. check()
  70. # Horribly inefficient.
  71. offsetToPos = (offset) ->
  72. # Again, very inefficient.
  73. lines = editorDoc.getAllLines()
  74. row = 0
  75. for line, row in lines
  76. break if offset <= line.length
  77. # +1 for the newline.
  78. offset -= lines[row].length + 1
  79. row:row, column:offset
  80. doc.on 'insert', (pos, text) ->
  81. suppress = true
  82. editorDoc.insert offsetToPos(pos), text
  83. suppress = false
  84. check()
  85. doc.on 'delete', (pos, text) ->
  86. suppress = true
  87. range = Range.fromPoints offsetToPos(pos), offsetToPos(pos + text.length)
  88. editorDoc.remove range
  89. suppress = false
  90. check()
  91. doc.detach_ace = ->
  92. doc.removeListener 'remoteop', docListener
  93. editorDoc.removeListener 'change', editorListener
  94. delete doc.detach_ace
  95. return