EditorManager.coffee 4.1 KB

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