EditorManager.coffee 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. openDoc: (doc, options = {}) ->
  39. sl_console.log "[openDoc] Opening #{doc.id}"
  40. @$scope.ui.view = "editor"
  41. done = () =>
  42. if options.gotoLine?
  43. # allow Ace to display document before moving, delay until next tick
  44. # added delay to make this happen later that gotoStoredPosition in
  45. # CursorPositionManager
  46. setTimeout () =>
  47. @$scope.$broadcast "editor:gotoLine", options.gotoLine, options.gotoColumn
  48. ,0
  49. if doc.id == @$scope.editor.open_doc_id and !options.forceReopen
  50. @$scope.$apply () =>
  51. done()
  52. return
  53. @$scope.editor.open_doc_id = doc.id
  54. @$scope.editor.open_doc_name = doc.name
  55. @ide.localStorage "doc.open_id.#{@$scope.project_id}", doc.id
  56. @ide.fileTreeManager.selectEntity(doc)
  57. @$scope.editor.opening = true
  58. @_openNewDocument doc, (error, sharejs_doc) =>
  59. if error?
  60. @ide.showGenericMessageModal(
  61. "Error opening document"
  62. "Sorry, something went wrong opening this document. Please try again."
  63. )
  64. return
  65. @$scope.$broadcast "doc:opened"
  66. @$scope.$apply () =>
  67. @$scope.editor.opening = false
  68. @$scope.editor.sharejs_doc = sharejs_doc
  69. done()
  70. _openNewDocument: (doc, callback = (error, sharejs_doc) ->) ->
  71. sl_console.log "[_openNewDocument] Opening..."
  72. current_sharejs_doc = @$scope.editor.sharejs_doc
  73. if current_sharejs_doc?
  74. sl_console.log "[_openNewDocument] Leaving existing open doc..."
  75. current_sharejs_doc.leaveAndCleanUp()
  76. @_unbindFromDocumentEvents(current_sharejs_doc)
  77. new_sharejs_doc = Document.getDocument @ide, doc.id
  78. new_sharejs_doc.join (error) =>
  79. return callback(error) if error?
  80. @_bindToDocumentEvents(doc, new_sharejs_doc)
  81. callback null, new_sharejs_doc
  82. _bindToDocumentEvents: (doc, sharejs_doc) ->
  83. sharejs_doc.on "error", (error, meta) =>
  84. if error?.message?.match "maxDocLength"
  85. @ide.showGenericMessageModal(
  86. "Document Too Long"
  87. "Sorry, this file is too long to be edited manually. Please upload it directly."
  88. )
  89. else
  90. @ide.socket.disconnect()
  91. @ide.reportError(error, meta)
  92. @ide.showGenericMessageModal(
  93. "Out of sync"
  94. "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>"
  95. )
  96. @openDoc(doc, forceReopen: true)
  97. sharejs_doc.on "externalUpdate", (update) =>
  98. return if @_ignoreExternalUpdates
  99. @ide.showGenericMessageModal(
  100. "Document Updated Externally"
  101. "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."
  102. )
  103. _unbindFromDocumentEvents: (document) ->
  104. document.off()
  105. getCurrentDocValue: () ->
  106. @$scope.editor.sharejs_doc?.getSnapshot()
  107. getCurrentDocId: () ->
  108. @$scope.editor.open_doc_id
  109. startIgnoringExternalUpdates: () ->
  110. @_ignoreExternalUpdates = true
  111. stopIgnoringExternalUpdates: () ->
  112. @_ignoreExternalUpdates = false