EditorManager.coffee 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. open_doc_id: null
  11. opening: true
  12. }
  13. @$scope.$on "entity:selected", (event, entity) =>
  14. if (@$scope.ui.view != "track-changes" and entity.type == "doc")
  15. @openDoc(entity)
  16. @$scope.$on "entity:deleted", (event, entity) =>
  17. if @$scope.editor.open_doc_id == entity.id
  18. return if !@$scope.project.rootDoc_id
  19. doc = @ide.fileTreeManager.findEntityById(@$scope.project.rootDoc_id)
  20. return if !doc?
  21. @openDoc(doc)
  22. initialized = false
  23. @$scope.$on "file-tree:initialized", () =>
  24. if !initialized
  25. initialized = true
  26. @autoOpenDoc()
  27. @$scope.$on "flush-changes", () =>
  28. Document.flushAll()
  29. autoOpenDoc: () ->
  30. open_doc_id =
  31. @ide.localStorage("doc.open_id.#{@$scope.project_id}") or
  32. @$scope.project.rootDoc_id
  33. return if !open_doc_id?
  34. doc = @ide.fileTreeManager.findEntityById(open_doc_id)
  35. return if !doc?
  36. @openDoc(doc)
  37. openDoc: (doc, options = {}) ->
  38. @$scope.ui.view = "editor"
  39. done = () =>
  40. if options.gotoLine?
  41. @$scope.$broadcast "editor:gotoLine", options.gotoLine
  42. if doc.id == @$scope.editor.open_doc_id and !options.forceReopen
  43. @$scope.$apply () =>
  44. done()
  45. return
  46. @$scope.editor.open_doc_id = doc.id
  47. @ide.localStorage "doc.open_id.#{@$scope.project_id}", doc.id
  48. @ide.fileTreeManager.selectEntity(doc)
  49. @$scope.editor.opening = true
  50. @_openNewDocument doc, (error, sharejs_doc) =>
  51. if error?
  52. @ide.showGenericMessageModal(
  53. "Error opening document"
  54. "Sorry, something went wrong opening this document. Please try again."
  55. )
  56. return
  57. @$scope.$broadcast "doc:opened"
  58. @$scope.$apply () =>
  59. @$scope.editor.opening = false
  60. @$scope.editor.sharejs_doc = sharejs_doc
  61. done()
  62. _openNewDocument: (doc, callback = (error, sharejs_doc) ->) ->
  63. current_sharejs_doc = @$scope.editor.sharejs_doc
  64. if current_sharejs_doc?
  65. current_sharejs_doc.leaveAndCleanUp()
  66. @_unbindFromDocumentEvents(current_sharejs_doc)
  67. new_sharejs_doc = Document.getDocument @ide, doc.id
  68. new_sharejs_doc.join (error) =>
  69. return callback(error) if error?
  70. @_bindToDocumentEvents(doc, new_sharejs_doc)
  71. callback null, new_sharejs_doc
  72. _bindToDocumentEvents: (doc, sharejs_doc) ->
  73. sharejs_doc.on "error", (error) =>
  74. @openDoc(doc, forceReopen: true)
  75. @ide.showGenericMessageModal(
  76. "Out of sync"
  77. "Sorry, this file has gone out of sync and we need to do a full refresh. Please let us know if this happens frequently."
  78. )
  79. sharejs_doc.on "externalUpdate", (update) =>
  80. return if @_ignoreExternalUpdates
  81. @ide.showGenericMessageModal(
  82. "Document Updated Externally"
  83. "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."
  84. )
  85. _unbindFromDocumentEvents: (document) ->
  86. document.off()
  87. getCurrentDocValue: () ->
  88. @$scope.editor.sharejs_doc?.getSnapshot()
  89. getCurrentDocId: () ->
  90. @$scope.editor.open_doc_id
  91. startIgnoringExternalUpdates: () ->
  92. @_ignoreExternalUpdates = true
  93. stopIgnoringExternalUpdates: () ->
  94. @_ignoreExternalUpdates = false