EditorManager.js 10 KB

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