EditorManager.coffee 3.3 KB

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