EditorManager.coffee 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. else if options.gotoOffset?
  54. setTimeout () =>
  55. @$scope.$broadcast "editor:gotoOffset", options.gotoOffset
  56. , 0
  57. if doc.id == @$scope.editor.open_doc_id and !options.forceReopen
  58. @$scope.$apply () =>
  59. done()
  60. return
  61. @$scope.editor.open_doc_id = doc.id
  62. @$scope.editor.open_doc_name = doc.name
  63. @ide.localStorage "doc.open_id.#{@$scope.project_id}", doc.id
  64. @ide.fileTreeManager.selectEntity(doc)
  65. @$scope.editor.opening = true
  66. @_openNewDocument doc, (error, sharejs_doc) =>
  67. if error?
  68. @ide.showGenericMessageModal(
  69. "Error opening document"
  70. "Sorry, something went wrong opening this document. Please try again."
  71. )
  72. return
  73. @$scope.$broadcast "doc:opened"
  74. @$scope.$apply () =>
  75. @$scope.editor.opening = false
  76. @$scope.editor.sharejs_doc = sharejs_doc
  77. done()
  78. _openNewDocument: (doc, callback = (error, sharejs_doc) ->) ->
  79. sl_console.log "[_openNewDocument] Opening..."
  80. current_sharejs_doc = @$scope.editor.sharejs_doc
  81. if current_sharejs_doc?
  82. sl_console.log "[_openNewDocument] Leaving existing open doc..."
  83. current_sharejs_doc.leaveAndCleanUp()
  84. @_unbindFromDocumentEvents(current_sharejs_doc)
  85. new_sharejs_doc = Document.getDocument @ide, doc.id
  86. new_sharejs_doc.join (error) =>
  87. return callback(error) if error?
  88. @_bindToDocumentEvents(doc, new_sharejs_doc)
  89. callback null, new_sharejs_doc
  90. _bindToDocumentEvents: (doc, sharejs_doc) ->
  91. sharejs_doc.on "error", (error, meta) =>
  92. if error?.message?.match "maxDocLength"
  93. @ide.showGenericMessageModal(
  94. "Document Too Long"
  95. "Sorry, this file is too long to be edited manually. Please upload it directly."
  96. )
  97. else
  98. @ide.socket.disconnect()
  99. @ide.reportError(error, meta)
  100. @ide.showGenericMessageModal(
  101. "Out of sync"
  102. "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>"
  103. )
  104. @openDoc(doc, forceReopen: true)
  105. sharejs_doc.on "externalUpdate", (update) =>
  106. return if @_ignoreExternalUpdates
  107. @ide.showGenericMessageModal(
  108. "Document Updated Externally"
  109. "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."
  110. )
  111. _unbindFromDocumentEvents: (document) ->
  112. document.off()
  113. getCurrentDocValue: () ->
  114. @$scope.editor.sharejs_doc?.getSnapshot()
  115. getCurrentDocId: () ->
  116. @$scope.editor.open_doc_id
  117. startIgnoringExternalUpdates: () ->
  118. @_ignoreExternalUpdates = true
  119. stopIgnoringExternalUpdates: () ->
  120. @_ignoreExternalUpdates = false