EditorManager.coffee 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. define [
  2. "ide/editor/Document"
  3. "ide/editor/directives/aceEditor"
  4. "ide/editor/controllers/SavingNotificationController"
  5. ], (Document) ->
  6. class EditorManager
  7. constructor: (@ide, @$scope) ->
  8. @$scope.editor = {
  9. sharejs_doc: null
  10. last_updated: null
  11. open_doc_id: null
  12. opening: true
  13. cursorPosition: {}
  14. gotoLine: null
  15. }
  16. @$scope.$on "entity:selected", (event, entity) =>
  17. if (@$scope.ui.view != "track-changes" and entity.type == "doc")
  18. @openDoc(entity)
  19. initialized = false
  20. @$scope.$on "file-tree:initialized", () =>
  21. if !initialized
  22. initialized = true
  23. @autoOpenDoc()
  24. autoOpenDoc: () ->
  25. open_doc_id =
  26. $.localStorage("doc.open_id.#{@$scope.project_id}") or
  27. @$scope.project.rootDoc_id
  28. return if !open_doc_id?
  29. doc = @ide.fileTreeManager.findEntityById(open_doc_id)
  30. return if !doc?
  31. @openDoc(doc)
  32. openDoc: (doc, options = {}) ->
  33. @$scope.ui.view = "editor"
  34. done = () =>
  35. if options.gotoLine?
  36. @$scope.editor.gotoLine = options.gotoLine
  37. if doc.id == @$scope.editor.open_doc_id and !options.forceReopen
  38. @$scope.$apply () =>
  39. done()
  40. return
  41. @$scope.editor.open_doc_id = doc.id
  42. $.localStorage "doc.open_id.#{@$scope.project_id}", doc.id
  43. @ide.fileTreeManager.selectEntity(doc)
  44. @$scope.editor.opening = true
  45. @_openNewDocument doc, (error, sharejs_doc) =>
  46. if error?
  47. @ide.showGenericServerErrorMessage()
  48. return
  49. @$scope.$broadcast "doc:opened"
  50. @$scope.$apply () =>
  51. @$scope.editor.opening = false
  52. @$scope.editor.sharejs_doc = sharejs_doc
  53. done()
  54. _openNewDocument: (doc, callback = (error, sharejs_doc) ->) ->
  55. current_sharejs_doc = @$scope.editor.sharejs_doc
  56. if current_sharejs_doc?
  57. current_sharejs_doc.leaveAndCleanUp()
  58. @_unbindFromDocumentEvents(current_sharejs_doc)
  59. new_sharejs_doc = Document.getDocument @ide, doc.id
  60. new_sharejs_doc.join (error) =>
  61. return callback(error) if error?
  62. @_bindToDocumentEvents(doc, new_sharejs_doc)
  63. callback null, new_sharejs_doc
  64. _bindToDocumentEvents: (doc, sharejs_doc) ->
  65. sharejs_doc.on "error", (error) =>
  66. @openDoc(doc, forceReopen: true)
  67. @ide.showGenericMessageModal(
  68. "Out of sync"
  69. "Sorry, this file has gone out of sync and we need to do a full refresh. Please let us know if this happens frequently."
  70. )
  71. sharejs_doc.on "externalUpdate", () =>
  72. @ide.showGenericMessageModal(
  73. "Document Updated Externally"
  74. "This document was just updated externally. Any recent changes you have made may have been overwritten. To see previous versions please look in the history."
  75. )
  76. _unbindFromDocumentEvents: (document) ->
  77. document.off()
  78. lastUpdated: () ->
  79. @$scope.editor.last_updated
  80. getCurrentDocValue: () ->
  81. @$scope.editor.sharejs_doc?.getSnapshot()
  82. getCurrentDocId: () ->
  83. @$scope.editor.open_doc_id