cm.coffee 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # This is some utility code to connect a CodeMirror editor
  2. # to a sharejs document.
  3. # It is heavily inspired from the Ace editor hook.
  4. # Convert a CodeMirror delta into an op understood by share.js
  5. applyToShareJS = (editorDoc, delta, doc) ->
  6. # CodeMirror deltas give a text replacement.
  7. # I tuned this operation a little bit, for speed.
  8. startPos = 0 # Get character position from # of chars in each line.
  9. i = 0 # i goes through all lines.
  10. while i < delta.from.line
  11. startPos += editorDoc.lineInfo(i).text.length + 1 # Add 1 for '\n'
  12. i++
  13. startPos += delta.from.ch
  14. if delta.to.line == delta.from.line &&
  15. delta.to.ch == delta.from.ch # Then nothing was removed.
  16. doc.insert startPos, delta.text.join '\n'
  17. else
  18. delLen = delta.to.ch - delta.from.ch
  19. while i < delta.to.line
  20. delLen += editorDoc.lineInfo(i).text.length + 1 # Add 1 for '\n'
  21. i++
  22. doc.del startPos, delLen
  23. doc.insert startPos, delta.text.join '\n' if delta.text
  24. applyToShareJS editorDoc, delta.next, doc if delta.next
  25. # Attach a CodeMirror editor to the document. The editor's contents are replaced
  26. # with the document's contents unless keepEditorContents is true. (In which case
  27. # the document's contents are nuked and replaced with the editor's).
  28. window.sharejs.extendDoc 'attach_cm', (editor, keepEditorContents) ->
  29. unless @provides.text
  30. throw new Error 'Only text documents can be attached to CodeMirror2'
  31. sharedoc = @
  32. check = ->
  33. window.setTimeout ->
  34. editorText = editor.getValue()
  35. otText = sharedoc.getValue()
  36. if editorText != otText
  37. console.error "Text does not match!"
  38. console.error "editor: #{editorText}"
  39. console.error "ot: #{otText}"
  40. # Replace the editor text with the doc snapshot.
  41. editor.setValue sharedoc.getValue()
  42. , 0
  43. if keepEditorContents
  44. @del 0, sharedoc.getText().length
  45. @insert 0, editor.getValue()
  46. else
  47. editor.setValue sharedoc.getText()
  48. check()
  49. # When we apply ops from sharejs, CodeMirror emits edit events.
  50. # We need to ignore those to prevent an infinite typing loop.
  51. suppress = false
  52. # Listen for edits in CodeMirror.
  53. editorListener = (ed, change) ->
  54. return if suppress
  55. applyToShareJS editor, change, sharedoc
  56. check()
  57. editor.setOption 'onChange', editorListener
  58. @on 'insert', (pos, text) ->
  59. suppress = true
  60. # All the primitives we need are already in CM's API.
  61. editor.replaceRange text, editor.posFromIndex(pos)
  62. suppress = false
  63. check()
  64. @on 'delete', (pos, text) ->
  65. suppress = true
  66. from = editor.posFromIndex pos
  67. to = editor.posFromIndex (pos + text.length)
  68. editor.replaceRange '', from, to
  69. suppress = false
  70. check()
  71. @detach_cm = ->
  72. # TODO: can we remove the insert and delete event callbacks?
  73. editor.setOption 'onChange', null
  74. delete @detach_cm
  75. return