EditorManager.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. import _ from 'lodash'
  2. /* eslint-disable
  3. camelcase,
  4. handle-callback-err,
  5. max-len,
  6. no-return-assign,
  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. import Document from './Document'
  18. import './components/spellMenu'
  19. import './directives/aceEditor'
  20. import './directives/toggleSwitch'
  21. import './controllers/SavingNotificationController'
  22. let EditorManager
  23. export default (EditorManager = (function() {
  24. EditorManager = class EditorManager {
  25. static initClass() {
  26. this.prototype._syncTimeout = null
  27. }
  28. constructor(ide, $scope, localStorage) {
  29. this.ide = ide
  30. this.$scope = $scope
  31. this.localStorage = localStorage
  32. this.$scope.editor = {
  33. sharejs_doc: null,
  34. open_doc_id: null,
  35. open_doc_name: null,
  36. opening: true,
  37. trackChanges: false,
  38. wantTrackChanges: false,
  39. showRichText: this.showRichText()
  40. }
  41. this.$scope.$on('entity:selected', (event, entity) => {
  42. if (this.$scope.ui.view !== 'history' && entity.type === 'doc') {
  43. return this.openDoc(entity)
  44. }
  45. })
  46. this.$scope.$on('entity:deleted', (event, entity) => {
  47. if (this.$scope.editor.open_doc_id === entity.id) {
  48. if (!this.$scope.project.rootDoc_id) {
  49. this.$scope.ui.view = null
  50. return
  51. }
  52. const doc = this.ide.fileTreeManager.findEntityById(
  53. this.$scope.project.rootDoc_id
  54. )
  55. if (doc == null) {
  56. this.$scope.ui.view = null
  57. return
  58. }
  59. return this.openDoc(doc)
  60. }
  61. })
  62. let initialized = false
  63. this.$scope.$on('file-tree:initialized', () => {
  64. if (!initialized) {
  65. initialized = true
  66. return this.autoOpenDoc()
  67. }
  68. })
  69. this.$scope.$on('flush-changes', () => {
  70. return Document.flushAll()
  71. })
  72. window.addEventListener('blur', () => {
  73. // The browser may put the tab into sleep as it looses focus.
  74. // Flushing the documents should help with keeping the documents in
  75. // sync: we can use any new version of the doc that the server may
  76. // present us. There should be no need to insert local changes into
  77. // the doc history as the user comes back.
  78. sl_console.log('[EditorManager] forcing flush onblur')
  79. Document.flushAll()
  80. })
  81. this.$scope.$watch('editor.wantTrackChanges', value => {
  82. if (value == null) {
  83. return
  84. }
  85. return this._syncTrackChangesState(this.$scope.editor.sharejs_doc)
  86. })
  87. }
  88. showRichText() {
  89. return (
  90. this.localStorage(`editor.mode.${this.$scope.project_id}`) ===
  91. 'rich-text'
  92. )
  93. }
  94. autoOpenDoc() {
  95. const open_doc_id =
  96. this.ide.localStorage(`doc.open_id.${this.$scope.project_id}`) ||
  97. this.$scope.project.rootDoc_id
  98. if (open_doc_id == null) {
  99. return
  100. }
  101. const doc = this.ide.fileTreeManager.findEntityById(open_doc_id)
  102. if (doc == null) {
  103. return
  104. }
  105. return this.openDoc(doc)
  106. }
  107. openDocId(doc_id, options) {
  108. if (options == null) {
  109. options = {}
  110. }
  111. const doc = this.ide.fileTreeManager.findEntityById(doc_id)
  112. if (doc == null) {
  113. return
  114. }
  115. return this.openDoc(doc, options)
  116. }
  117. openDoc(doc, options) {
  118. if (options == null) {
  119. options = {}
  120. }
  121. sl_console.log(`[openDoc] Opening ${doc.id}`)
  122. this.$scope.ui.view = 'editor'
  123. const done = () => {
  124. this.$scope.$broadcast('doc:after-opened')
  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 = doc.getInflightOp() == null && doc.getPendingOp() == null
  289. if (saved) {
  290. doc.setTrackingChanges(want)
  291. return this.$scope.$apply(() => {
  292. return (this.$scope.editor.trackChanges = want)
  293. })
  294. } else {
  295. return (this._syncTimeout = setTimeout(tryToggle, 100))
  296. }
  297. })()
  298. }
  299. }
  300. EditorManager.initClass()
  301. return EditorManager
  302. })())