EditorManager.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. jumpToLine(options) {
  118. return this.$scope.$broadcast(
  119. 'editor:gotoLine',
  120. options.gotoLine,
  121. options.gotoColumn,
  122. options.syncToPdf
  123. )
  124. }
  125. openDoc(doc, options) {
  126. if (options == null) {
  127. options = {}
  128. }
  129. sl_console.log(`[openDoc] Opening ${doc.id}`)
  130. this.$scope.ui.view = 'editor'
  131. const done = isNewDoc => {
  132. this.$scope.$broadcast('doc:after-opened', { isNewDoc })
  133. if (options.gotoLine != null) {
  134. // allow Ace to display document before moving, delay until next tick
  135. // added delay to make this happen later that gotoStoredPosition in
  136. // CursorPositionManager
  137. return setTimeout(() => this.jumpToLine(options), 0)
  138. } else if (options.gotoOffset != null) {
  139. return setTimeout(() => {
  140. return this.$scope.$broadcast(
  141. 'editor:gotoOffset',
  142. options.gotoOffset
  143. )
  144. }, 0)
  145. }
  146. }
  147. // If we already have the document open we can return at this point.
  148. // Note: only use forceReopen:true to override this when the document is
  149. // is out of sync and needs to be reloaded from the server.
  150. if (doc.id === this.$scope.editor.open_doc_id && !options.forceReopen) {
  151. // automatically update the file tree whenever the file is opened
  152. this.ide.fileTreeManager.selectEntity(doc)
  153. this.$scope.$apply(() => {
  154. return done(false)
  155. })
  156. return
  157. }
  158. // We're now either opening a new document or reloading a broken one.
  159. this.$scope.editor.open_doc_id = doc.id
  160. this.$scope.editor.open_doc_name = doc.name
  161. this.ide.localStorage(`doc.open_id.${this.$scope.project_id}`, doc.id)
  162. this.ide.fileTreeManager.selectEntity(doc)
  163. this.$scope.editor.opening = true
  164. return this._openNewDocument(doc, (error, sharejs_doc) => {
  165. if (error != null) {
  166. this.ide.showGenericMessageModal(
  167. 'Error opening document',
  168. 'Sorry, something went wrong opening this document. Please try again.'
  169. )
  170. return
  171. }
  172. this._syncTrackChangesState(sharejs_doc)
  173. this.$scope.$broadcast('doc:opened')
  174. return this.$scope.$apply(() => {
  175. this.$scope.editor.opening = false
  176. this.$scope.editor.sharejs_doc = sharejs_doc
  177. return done(true)
  178. })
  179. })
  180. }
  181. _openNewDocument(doc, callback) {
  182. if (callback == null) {
  183. callback = function(error, sharejs_doc) {}
  184. }
  185. sl_console.log('[_openNewDocument] Opening...')
  186. const current_sharejs_doc = this.$scope.editor.sharejs_doc
  187. const new_sharejs_doc = Document.getDocument(this.ide, doc.id)
  188. // Leave the current document only when we are opening a different new
  189. // one, to avoid race conditions between leaving and joining the same
  190. // document.
  191. if (
  192. current_sharejs_doc != null &&
  193. current_sharejs_doc !== new_sharejs_doc
  194. ) {
  195. sl_console.log('[_openNewDocument] Leaving existing open doc...')
  196. current_sharejs_doc.leaveAndCleanUp()
  197. this._unbindFromDocumentEvents(current_sharejs_doc)
  198. }
  199. return new_sharejs_doc.join(error => {
  200. if (error != null) {
  201. return callback(error)
  202. }
  203. this._bindToDocumentEvents(doc, new_sharejs_doc)
  204. return callback(null, new_sharejs_doc)
  205. })
  206. }
  207. _bindToDocumentEvents(doc, sharejs_doc) {
  208. sharejs_doc.on('error', (error, meta) => {
  209. let message
  210. if ((error != null ? error.message : undefined) != null) {
  211. ;({ message } = error)
  212. } else if (typeof error === 'string') {
  213. message = error
  214. } else {
  215. message = ''
  216. }
  217. if (/maxDocLength/.test(message)) {
  218. this.ide.showGenericMessageModal(
  219. 'Document Too Long',
  220. 'Sorry, this file is too long to be edited manually. Please upload it directly.'
  221. )
  222. } else if (/too many comments or tracked changes/.test(message)) {
  223. this.ide.showGenericMessageModal(
  224. 'Too many comments or tracked changes',
  225. 'Sorry, this file has too many comments or tracked changes. Please try accepting or rejecting some existing changes, or resolving and deleting some comments.'
  226. )
  227. } else {
  228. this.ide.socket.disconnect()
  229. this.ide.reportError(error, meta)
  230. this.ide.showOutOfSyncModal(
  231. 'Out of sync',
  232. "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>",
  233. sharejs_doc.doc._doc.snapshot
  234. )
  235. }
  236. const removeHandler = this.$scope.$on('project:joined', () => {
  237. this.openDoc(doc, { forceReopen: true })
  238. removeHandler()
  239. })
  240. })
  241. return sharejs_doc.on('externalUpdate', update => {
  242. if (this._ignoreExternalUpdates) {
  243. return
  244. }
  245. if (
  246. _.property(['meta', 'type'])(update) === 'external' &&
  247. _.property(['meta', 'source'])(update) === 'git-bridge'
  248. ) {
  249. return
  250. }
  251. return this.ide.showGenericMessageModal(
  252. 'Document Updated Externally',
  253. '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.'
  254. )
  255. })
  256. }
  257. _unbindFromDocumentEvents(document) {
  258. return document.off()
  259. }
  260. getCurrentDocValue() {
  261. return this.$scope.editor.sharejs_doc != null
  262. ? this.$scope.editor.sharejs_doc.getSnapshot()
  263. : undefined
  264. }
  265. getCurrentDocId() {
  266. return this.$scope.editor.open_doc_id
  267. }
  268. startIgnoringExternalUpdates() {
  269. return (this._ignoreExternalUpdates = true)
  270. }
  271. stopIgnoringExternalUpdates() {
  272. return (this._ignoreExternalUpdates = false)
  273. }
  274. _syncTrackChangesState(doc) {
  275. let tryToggle
  276. if (doc == null) {
  277. return
  278. }
  279. if (this._syncTimeout != null) {
  280. clearTimeout(this._syncTimeout)
  281. this._syncTimeout = null
  282. }
  283. const want = this.$scope.editor.wantTrackChanges
  284. const have = doc.getTrackingChanges()
  285. if (want === have) {
  286. this.$scope.editor.trackChanges = want
  287. return
  288. }
  289. return (tryToggle = () => {
  290. const saved = doc.getInflightOp() == null && doc.getPendingOp() == null
  291. if (saved) {
  292. doc.setTrackingChanges(want)
  293. return this.$scope.$apply(() => {
  294. return (this.$scope.editor.trackChanges = want)
  295. })
  296. } else {
  297. return (this._syncTimeout = setTimeout(tryToggle, 100))
  298. }
  299. })()
  300. }
  301. }
  302. EditorManager.initClass()
  303. return EditorManager
  304. })())