EditorManager.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. max-len,
  5. no-return-assign,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS206: Consider reworking classes to avoid initClass
  13. * DS207: Consider shorter variations of null checks
  14. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  15. */
  16. define([
  17. './Document',
  18. './components/spellMenu',
  19. './directives/aceEditor',
  20. './directives/toggleSwitch',
  21. './controllers/SavingNotificationController'
  22. ], function(Document) {
  23. let EditorManager
  24. return (EditorManager = (function() {
  25. EditorManager = class EditorManager {
  26. static initClass() {
  27. this.prototype._syncTimeout = null
  28. }
  29. constructor(ide, $scope, localStorage) {
  30. this.ide = ide
  31. this.$scope = $scope
  32. this.localStorage = localStorage
  33. this.$scope.editor = {
  34. sharejs_doc: null,
  35. open_doc_id: null,
  36. open_doc_name: null,
  37. opening: true,
  38. trackChanges: false,
  39. wantTrackChanges: false,
  40. showRichText: this.showRichText()
  41. }
  42. this.$scope.$on('entity:selected', (event, entity) => {
  43. if (this.$scope.ui.view !== 'history' && entity.type === 'doc') {
  44. return this.openDoc(entity)
  45. }
  46. })
  47. this.$scope.$on('entity:deleted', (event, entity) => {
  48. if (this.$scope.editor.open_doc_id === entity.id) {
  49. if (!this.$scope.project.rootDoc_id) {
  50. this.$scope.ui.view = null
  51. return
  52. }
  53. const doc = this.ide.fileTreeManager.findEntityById(
  54. this.$scope.project.rootDoc_id
  55. )
  56. if (doc == null) {
  57. this.$scope.ui.view = null
  58. return
  59. }
  60. return this.openDoc(doc)
  61. }
  62. })
  63. let initialized = false
  64. this.$scope.$on('file-tree:initialized', () => {
  65. if (!initialized) {
  66. initialized = true
  67. return this.autoOpenDoc()
  68. }
  69. })
  70. this.$scope.$on('flush-changes', () => {
  71. return Document.flushAll()
  72. })
  73. window.addEventListener('blur', () => {
  74. // The browser may put the tab into sleep as it looses focus.
  75. // Flushing the documents should help with keeping the documents in
  76. // sync: we can use any new version of the doc that the server may
  77. // present us. There should be no need to insert local changes into
  78. // the doc history as the user comes back.
  79. sl_console.log('[EditorManager] forcing flush onblur')
  80. Document.flushAll()
  81. })
  82. this.$scope.$watch('editor.wantTrackChanges', value => {
  83. if (value == null) {
  84. return
  85. }
  86. return this._syncTrackChangesState(this.$scope.editor.sharejs_doc)
  87. })
  88. }
  89. showRichText() {
  90. return (
  91. this.localStorage(`editor.mode.${this.$scope.project_id}`) ===
  92. 'rich-text'
  93. )
  94. }
  95. autoOpenDoc() {
  96. const open_doc_id =
  97. this.ide.localStorage(`doc.open_id.${this.$scope.project_id}`) ||
  98. this.$scope.project.rootDoc_id
  99. if (open_doc_id == null) {
  100. return
  101. }
  102. const doc = this.ide.fileTreeManager.findEntityById(open_doc_id)
  103. if (doc == null) {
  104. return
  105. }
  106. return this.openDoc(doc)
  107. }
  108. openDocId(doc_id, options) {
  109. if (options == null) {
  110. options = {}
  111. }
  112. const doc = this.ide.fileTreeManager.findEntityById(doc_id)
  113. if (doc == null) {
  114. return
  115. }
  116. return this.openDoc(doc, options)
  117. }
  118. openDoc(doc, options) {
  119. if (options == null) {
  120. options = {}
  121. }
  122. sl_console.log(`[openDoc] Opening ${doc.id}`)
  123. this.$scope.ui.view = 'editor'
  124. const done = () => {
  125. if (options.gotoLine != null) {
  126. // allow Ace to display document before moving, delay until next tick
  127. // added delay to make this happen later that gotoStoredPosition in
  128. // CursorPositionManager
  129. return setTimeout(() => {
  130. return this.$scope.$broadcast(
  131. 'editor:gotoLine',
  132. options.gotoLine,
  133. options.gotoColumn
  134. )
  135. }, 0)
  136. } else if (options.gotoOffset != null) {
  137. return setTimeout(() => {
  138. return this.$scope.$broadcast(
  139. 'editor:gotoOffset',
  140. options.gotoOffset
  141. )
  142. }, 0)
  143. }
  144. }
  145. // If we already have the document open we can return at this point.
  146. // Note: only use forceReopen:true to override this when the document is
  147. // is out of sync and needs to be reloaded from the server.
  148. if (doc.id === this.$scope.editor.open_doc_id && !options.forceReopen) {
  149. // automatically update the file tree whenever the file is opened
  150. this.ide.fileTreeManager.selectEntity(doc)
  151. this.$scope.$apply(() => {
  152. return done()
  153. })
  154. return
  155. }
  156. // We're now either opening a new document or reloading a broken one.
  157. this.$scope.editor.open_doc_id = doc.id
  158. this.$scope.editor.open_doc_name = doc.name
  159. this.ide.localStorage(`doc.open_id.${this.$scope.project_id}`, doc.id)
  160. this.ide.fileTreeManager.selectEntity(doc)
  161. this.$scope.editor.opening = true
  162. return this._openNewDocument(doc, (error, sharejs_doc) => {
  163. if (error != null) {
  164. this.ide.showGenericMessageModal(
  165. 'Error opening document',
  166. 'Sorry, something went wrong opening this document. Please try again.'
  167. )
  168. return
  169. }
  170. this._syncTrackChangesState(sharejs_doc)
  171. this.$scope.$broadcast('doc:opened')
  172. return this.$scope.$apply(() => {
  173. this.$scope.editor.opening = false
  174. this.$scope.editor.sharejs_doc = sharejs_doc
  175. return done()
  176. })
  177. })
  178. }
  179. _openNewDocument(doc, callback) {
  180. if (callback == null) {
  181. callback = function(error, sharejs_doc) {}
  182. }
  183. sl_console.log('[_openNewDocument] Opening...')
  184. const current_sharejs_doc = this.$scope.editor.sharejs_doc
  185. const new_sharejs_doc = Document.getDocument(this.ide, doc.id)
  186. // Leave the current document only when we are opening a different new
  187. // one, to avoid race conditions between leaving and joining the same
  188. // document.
  189. if (
  190. current_sharejs_doc != null &&
  191. current_sharejs_doc !== new_sharejs_doc
  192. ) {
  193. sl_console.log('[_openNewDocument] Leaving existing open doc...')
  194. current_sharejs_doc.leaveAndCleanUp()
  195. this._unbindFromDocumentEvents(current_sharejs_doc)
  196. }
  197. return new_sharejs_doc.join(error => {
  198. if (error != null) {
  199. return callback(error)
  200. }
  201. this._bindToDocumentEvents(doc, new_sharejs_doc)
  202. return callback(null, new_sharejs_doc)
  203. })
  204. }
  205. _bindToDocumentEvents(doc, sharejs_doc) {
  206. sharejs_doc.on('error', (error, meta) => {
  207. let message
  208. if ((error != null ? error.message : undefined) != null) {
  209. ;({ message } = error)
  210. } else if (typeof error === 'string') {
  211. message = error
  212. } else {
  213. message = ''
  214. }
  215. if (/maxDocLength/.test(message)) {
  216. this.ide.showGenericMessageModal(
  217. 'Document Too Long',
  218. 'Sorry, this file is too long to be edited manually. Please upload it directly.'
  219. )
  220. } else if (/too many comments or tracked changes/.test(message)) {
  221. this.ide.showGenericMessageModal(
  222. 'Too many comments or tracked changes',
  223. 'Sorry, this file has too many comments or tracked changes. Please try accepting or rejecting some existing changes, or resolving and deleting some comments.'
  224. )
  225. } else {
  226. this.ide.socket.disconnect()
  227. this.ide.reportError(error, meta)
  228. this.ide.showOutOfSyncModal(
  229. 'Out of sync',
  230. "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>",
  231. sharejs_doc.doc._doc.snapshot
  232. )
  233. }
  234. const removeHandler = this.$scope.$on('project:joined', () => {
  235. this.openDoc(doc, { forceReopen: true })
  236. removeHandler()
  237. })
  238. })
  239. return sharejs_doc.on('externalUpdate', update => {
  240. if (this._ignoreExternalUpdates) {
  241. return
  242. }
  243. if (
  244. _.property(['meta', 'type'])(update) === 'external' &&
  245. _.property(['meta', 'source'])(update) === 'git-bridge'
  246. ) {
  247. return
  248. }
  249. return this.ide.showGenericMessageModal(
  250. 'Document Updated Externally',
  251. '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.'
  252. )
  253. })
  254. }
  255. _unbindFromDocumentEvents(document) {
  256. return document.off()
  257. }
  258. getCurrentDocValue() {
  259. return this.$scope.editor.sharejs_doc != null
  260. ? this.$scope.editor.sharejs_doc.getSnapshot()
  261. : undefined
  262. }
  263. getCurrentDocId() {
  264. return this.$scope.editor.open_doc_id
  265. }
  266. startIgnoringExternalUpdates() {
  267. return (this._ignoreExternalUpdates = true)
  268. }
  269. stopIgnoringExternalUpdates() {
  270. return (this._ignoreExternalUpdates = false)
  271. }
  272. _syncTrackChangesState(doc) {
  273. let tryToggle
  274. if (doc == null) {
  275. return
  276. }
  277. if (this._syncTimeout != null) {
  278. clearTimeout(this._syncTimeout)
  279. this._syncTimeout = null
  280. }
  281. const want = this.$scope.editor.wantTrackChanges
  282. const have = doc.getTrackingChanges()
  283. if (want === have) {
  284. this.$scope.editor.trackChanges = want
  285. return
  286. }
  287. return (tryToggle = () => {
  288. const saved =
  289. doc.getInflightOp() == null && doc.getPendingOp() == null
  290. if (saved) {
  291. doc.setTrackingChanges(want)
  292. return this.$scope.$apply(() => {
  293. return (this.$scope.editor.trackChanges = want)
  294. })
  295. } else {
  296. return (this._syncTimeout = setTimeout(tryToggle, 100))
  297. }
  298. })()
  299. }
  300. }
  301. EditorManager.initClass()
  302. return EditorManager
  303. })())
  304. })