EditorManager.coffee 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. @$scope.$on "flush-changes", () =>
  28. Document.flushAll()
  29. autoOpenDoc: () ->
  30. open_doc_id =
  31. @ide.localStorage("doc.open_id.#{@$scope.project_id}") or
  32. @$scope.project.rootDoc_id
  33. return if !open_doc_id?
  34. doc = @ide.fileTreeManager.findEntityById(open_doc_id)
  35. return if !doc?
  36. @openDoc(doc)
  37. openDoc: (doc, options = {}) ->
  38. sl_console.log "[openDoc] Opening #{doc.id}"
  39. @$scope.ui.view = "editor"
  40. done = () =>
  41. if options.gotoLine?
  42. @$scope.$broadcast "editor:gotoLine", options.gotoLine
  43. if doc.id == @$scope.editor.open_doc_id and !options.forceReopen
  44. @$scope.$apply () =>
  45. done()
  46. return
  47. @$scope.editor.open_doc_id = doc.id
  48. @ide.localStorage "doc.open_id.#{@$scope.project_id}", doc.id
  49. @ide.fileTreeManager.selectEntity(doc)
  50. @$scope.editor.opening = true
  51. @_openNewDocument doc, (error, sharejs_doc) =>
  52. if error?
  53. @ide.showGenericMessageModal(
  54. "Error opening document"
  55. "Sorry, something went wrong opening this document. Please try again."
  56. )
  57. return
  58. @$scope.$broadcast "doc:opened"
  59. @$scope.$apply () =>
  60. @$scope.editor.opening = false
  61. @$scope.editor.sharejs_doc = sharejs_doc
  62. done()
  63. _openNewDocument: (doc, callback = (error, sharejs_doc) ->) ->
  64. sl_console.log "[_openNewDocument] Opening..."
  65. current_sharejs_doc = @$scope.editor.sharejs_doc
  66. if current_sharejs_doc?
  67. sl_console.log "[_openNewDocument] Leaving existing open doc..."
  68. current_sharejs_doc.leaveAndCleanUp()
  69. @_unbindFromDocumentEvents(current_sharejs_doc)
  70. new_sharejs_doc = Document.getDocument @ide, doc.id
  71. new_sharejs_doc.join (error) =>
  72. return callback(error) if error?
  73. @_bindToDocumentEvents(doc, new_sharejs_doc)
  74. callback null, new_sharejs_doc
  75. _bindToDocumentEvents: (doc, sharejs_doc) ->
  76. sharejs_doc.on "error", (error, meta) =>
  77. if error?.message?.match "maxDocLength"
  78. @ide.showGenericMessageModal(
  79. "Document Too Long"
  80. "Sorry, this file is too long to be edited manually. Please upload it directly."
  81. )
  82. else
  83. @ide.socket.disconnect()
  84. @ide.reportError(error, meta)
  85. @ide.showGenericMessageModal(
  86. "Out of sync"
  87. "Sorry, this file has gone out of sync and we need to do a full refresh. <br> <a href='http://sharelatex.tenderapp.com/help/kb/browsers/editor-out-of-sync-problems'>Please see this help guide for more information</a>"
  88. )
  89. @openDoc(doc, forceReopen: true)
  90. sharejs_doc.on "externalUpdate", (update) =>
  91. return if @_ignoreExternalUpdates
  92. @ide.showGenericMessageModal(
  93. "Document Updated Externally"
  94. "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."
  95. )
  96. _unbindFromDocumentEvents: (document) ->
  97. document.off()
  98. getCurrentDocValue: () ->
  99. @$scope.editor.sharejs_doc?.getSnapshot()
  100. getCurrentDocId: () ->
  101. @$scope.editor.open_doc_id
  102. startIgnoringExternalUpdates: () ->
  103. @_ignoreExternalUpdates = true
  104. stopIgnoringExternalUpdates: () ->
  105. @_ignoreExternalUpdates = false