EditorManager.coffee 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. initialized = false
  17. @$scope.$on "file-tree:initialized", () =>
  18. if !initialized
  19. initialized = true
  20. @autoOpenDoc()
  21. autoOpenDoc: () ->
  22. open_doc_id =
  23. $.localStorage("doc.open_id.#{@$scope.project_id}") or
  24. @$scope.project.rootDoc_id
  25. return if !open_doc_id?
  26. doc = @ide.fileTreeManager.findEntityById(open_doc_id)
  27. return if !doc?
  28. @openDoc(doc)
  29. openDoc: (doc, options = {}) ->
  30. @$scope.ui.view = "editor"
  31. done = () =>
  32. if options.gotoLine?
  33. @$scope.$broadcast "editor:gotoLine", options.gotoLine
  34. if doc.id == @$scope.editor.open_doc_id and !options.forceReopen
  35. @$scope.$apply () =>
  36. done()
  37. return
  38. @$scope.editor.open_doc_id = doc.id
  39. $.localStorage "doc.open_id.#{@$scope.project_id}", doc.id
  40. @ide.fileTreeManager.selectEntity(doc)
  41. @$scope.editor.opening = true
  42. @_openNewDocument doc, (error, sharejs_doc) =>
  43. if error?
  44. @ide.showGenericMessageModal(
  45. "Error opening document"
  46. "Sorry, something went wrong opening this document. Please try again."
  47. )
  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. getCurrentDocValue: () ->
  79. @$scope.editor.sharejs_doc?.getSnapshot()
  80. getCurrentDocId: () ->
  81. @$scope.editor.open_doc_id