EditorManager.js 9.7 KB

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