EditorManager.coffee 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. open_doc_name: null
  12. opening: true
  13. }
  14. @$scope.$on "entity:selected", (event, entity) =>
  15. if (@$scope.ui.view != "history" and entity.type == "doc")
  16. @openDoc(entity)
  17. @$scope.$on "entity:deleted", (event, entity) =>
  18. if @$scope.editor.open_doc_id == entity.id
  19. return if !@$scope.project.rootDoc_id
  20. doc = @ide.fileTreeManager.findEntityById(@$scope.project.rootDoc_id)
  21. return if !doc?
  22. @openDoc(doc)
  23. initialized = false
  24. @$scope.$on "file-tree:initialized", () =>
  25. if !initialized
  26. initialized = true
  27. @autoOpenDoc()
  28. @$scope.$on "flush-changes", () =>
  29. Document.flushAll()
  30. autoOpenDoc: () ->
  31. open_doc_id =
  32. @ide.localStorage("doc.open_id.#{@$scope.project_id}") or
  33. @$scope.project.rootDoc_id
  34. return if !open_doc_id?
  35. doc = @ide.fileTreeManager.findEntityById(open_doc_id)
  36. return if !doc?
  37. @openDoc(doc)
  38. openDocId: (doc_id, options = {}) ->
  39. doc = @ide.fileTreeManager.findEntityById(doc_id)
  40. return if !doc?
  41. @openDoc(doc, options)
  42. openDoc: (doc, options = {}) ->
  43. sl_console.log "[openDoc] Opening #{doc.id}"
  44. @$scope.ui.view = "editor"
  45. done = () =>
  46. if options.gotoLine?
  47. # allow Ace to display document before moving, delay until next tick
  48. # added delay to make this happen later that gotoStoredPosition in
  49. # CursorPositionManager
  50. setTimeout () =>
  51. @$scope.$broadcast "editor:gotoLine", options.gotoLine, options.gotoColumn
  52. ,0
  53. if doc.id == @$scope.editor.open_doc_id and !options.forceReopen
  54. @$scope.$apply () =>
  55. done()
  56. return
  57. @$scope.editor.open_doc_id = doc.id
  58. @$scope.editor.open_doc_name = doc.name
  59. @ide.localStorage "doc.open_id.#{@$scope.project_id}", doc.id
  60. @ide.fileTreeManager.selectEntity(doc)
  61. @$scope.editor.opening = true
  62. @_openNewDocument doc, (error, sharejs_doc) =>
  63. if error?
  64. @ide.showGenericMessageModal(
  65. "Error opening document"
  66. "Sorry, something went wrong opening this document. Please try again."
  67. )
  68. return
  69. @$scope.$broadcast "doc:opened"
  70. @$scope.$apply () =>
  71. @$scope.editor.opening = false
  72. @$scope.editor.sharejs_doc = sharejs_doc
  73. done()
  74. _openNewDocument: (doc, callback = (error, sharejs_doc) ->) ->
  75. sl_console.log "[_openNewDocument] Opening..."
  76. current_sharejs_doc = @$scope.editor.sharejs_doc
  77. if current_sharejs_doc?
  78. sl_console.log "[_openNewDocument] Leaving existing open doc..."
  79. current_sharejs_doc.leaveAndCleanUp()
  80. @_unbindFromDocumentEvents(current_sharejs_doc)
  81. new_sharejs_doc = Document.getDocument @ide, doc.id
  82. new_sharejs_doc.join (error) =>
  83. return callback(error) if error?
  84. @_bindToDocumentEvents(doc, new_sharejs_doc)
  85. callback null, new_sharejs_doc
  86. _bindToDocumentEvents: (doc, sharejs_doc) ->
  87. sharejs_doc.on "error", (error, meta) =>
  88. if error?.message?.match "maxDocLength"
  89. @ide.showGenericMessageModal(
  90. "Document Too Long"
  91. "Sorry, this file is too long to be edited manually. Please upload it directly."
  92. )
  93. else
  94. @ide.socket.disconnect()
  95. @ide.reportError(error, meta)
  96. @ide.showGenericMessageModal(
  97. "Out of sync"
  98. "Sorry, this file has gone out of sync and we need to do a full refresh. <br> <a href='/learn/Kb/Editor_out_of_sync_problems'>Please see this help guide for more information</a>"
  99. )
  100. @openDoc(doc, forceReopen: true)
  101. sharejs_doc.on "externalUpdate", (update) =>
  102. return if @_ignoreExternalUpdates
  103. @ide.showGenericMessageModal(
  104. "Document Updated Externally"
  105. "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."
  106. )
  107. _unbindFromDocumentEvents: (document) ->
  108. document.off()
  109. getCurrentDocValue: () ->
  110. @$scope.editor.sharejs_doc?.getSnapshot()
  111. getCurrentDocId: () ->
  112. @$scope.editor.open_doc_id
  113. startIgnoringExternalUpdates: () ->
  114. @_ignoreExternalUpdates = true
  115. stopIgnoringExternalUpdates: () ->
  116. @_ignoreExternalUpdates = false