ide.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /* eslint-disable
  2. camelcase,
  3. max-len,
  4. no-cond-assign,
  5. no-return-assign,
  6. no-unused-vars,
  7. no-useless-escape,
  8. */
  9. // TODO: This file was created by bulk-decaffeinate.
  10. // Fix any style issues and re-enable lint.
  11. /*
  12. * decaffeinate suggestions:
  13. * DS101: Remove unnecessary use of Array.from
  14. * DS102: Remove unnecessary code created because of implicit returns
  15. * DS103: Rewrite code to no longer use __guard__
  16. * DS207: Consider shorter variations of null checks
  17. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  18. */
  19. import App from './base'
  20. import FileTreeManager from './ide/file-tree/FileTreeManager'
  21. import LoadingManager from './ide/LoadingManager'
  22. import ConnectionManager from './ide/connection/ConnectionManager'
  23. import EditorManager from './ide/editor/EditorManager'
  24. import OnlineUsersManager from './ide/online-users/OnlineUsersManager'
  25. import PermissionsManager from './ide/permissions/PermissionsManager'
  26. import BinaryFilesManager from './ide/binary-files/BinaryFilesManager'
  27. import ReferencesManager from './ide/references/ReferencesManager'
  28. import MetadataManager from './ide/metadata/MetadataManager'
  29. import './ide/review-panel/ReviewPanelManager'
  30. import './ide/cobranding/CobrandingDataService'
  31. import './ide/chat/index'
  32. import './ide/file-view/index'
  33. import './ide/toolbar/index'
  34. import './ide/directives/layout'
  35. import './ide/directives/verticalResizablePanes'
  36. import './ide/services/ide'
  37. import './services/queued-http' // used in FileTreeManager
  38. import './main/event' // used in various controllers
  39. import './main/system-messages' // used in project/editor
  40. import '../../modules/modules-ide'
  41. import './features/source-editor/ide'
  42. import './shared/context/controllers/root-context-controller'
  43. import './features/editor-navigation-toolbar/controllers/editor-navigation-toolbar-controller'
  44. import './features/pdf-preview/controllers/pdf-preview-controller'
  45. import './features/share-project-modal/controllers/react-share-project-modal-controller'
  46. import './features/source-editor/controllers/grammarly-advert-controller'
  47. import './features/history/controllers/history-controller'
  48. import './features/editor-left-menu/controllers/editor-left-menu-controller'
  49. import { cleanupServiceWorker } from './utils/service-worker-cleanup'
  50. import { reportCM6Perf } from './infrastructure/cm6-performance'
  51. import { debugConsole } from '@/utils/debugging'
  52. App.controller('IdeController', [
  53. '$scope',
  54. '$timeout',
  55. 'ide',
  56. 'localStorage',
  57. 'eventTracking',
  58. 'metadata',
  59. 'CobrandingDataService',
  60. '$window',
  61. function (
  62. $scope,
  63. $timeout,
  64. ide,
  65. localStorage,
  66. eventTracking,
  67. metadata,
  68. CobrandingDataService,
  69. $window
  70. ) {
  71. // Don't freak out if we're already in an apply callback
  72. let err, pdfLayout, userAgent
  73. $scope.$originalApply = $scope.$apply
  74. $scope.$apply = function (fn) {
  75. if (fn == null) {
  76. fn = function () {}
  77. }
  78. const phase = this.$root.$$phase
  79. if (phase === '$apply' || phase === '$digest') {
  80. return fn()
  81. } else {
  82. return this.$originalApply(fn)
  83. }
  84. }
  85. $scope.state = {
  86. loading: true,
  87. load_progress: 40,
  88. error: null,
  89. }
  90. $scope.ui = {
  91. leftMenuShown: false,
  92. view: 'editor',
  93. chatOpen: false,
  94. pdfLayout: 'sideBySide',
  95. pdfHidden: false,
  96. pdfWidth: 0,
  97. reviewPanelOpen: localStorage(`ui.reviewPanelOpen.${window.project_id}`),
  98. miniReviewPanelVisible: false,
  99. chatResizerSizeOpen: 7,
  100. chatResizerSizeClosed: 0,
  101. }
  102. $scope.user = window.user
  103. $scope.settings = window.userSettings
  104. $scope.anonymous = window.anonymous
  105. $scope.isTokenMember = window.isTokenMember
  106. $scope.isRestrictedTokenMember = window.isRestrictedTokenMember
  107. $scope.cobranding = {
  108. isProjectCobranded: CobrandingDataService.isProjectCobranded(),
  109. logoImgUrl: CobrandingDataService.getLogoImgUrl(),
  110. submitBtnHtml: CobrandingDataService.getSubmitBtnHtml(),
  111. brandVariationName: CobrandingDataService.getBrandVariationName(),
  112. brandVariationHomeUrl: CobrandingDataService.getBrandVariationHomeUrl(),
  113. }
  114. $scope.chat = {}
  115. ide.toggleReviewPanel = $scope.toggleReviewPanel = function () {
  116. $scope.$applyAsync(() => {
  117. if (!$scope.project.features.trackChangesVisible) {
  118. return
  119. }
  120. $scope.ui.reviewPanelOpen = !$scope.ui.reviewPanelOpen
  121. eventTracking.sendMB('rp-toggle-panel', {
  122. value: $scope.ui.reviewPanelOpen,
  123. })
  124. })
  125. }
  126. $scope.$watch('ui.reviewPanelOpen', function (value) {
  127. if (value != null) {
  128. return localStorage(`ui.reviewPanelOpen.${window.project_id}`, value)
  129. }
  130. })
  131. $scope.$on('layout:pdf:resize', function (_, layoutState) {
  132. $scope.ui.pdfHidden = layoutState.east.initClosed
  133. return ($scope.ui.pdfWidth = layoutState.east.size)
  134. })
  135. $scope.$watch('ui.view', function (newView, oldView) {
  136. if (newView !== oldView) {
  137. $scope.$broadcast('layout:flat-screen:toggle')
  138. }
  139. if (newView != null && newView !== 'editor' && newView !== 'pdf') {
  140. eventTracking.sendMBOnce(`ide-open-view-${newView}-once`)
  141. }
  142. })
  143. $scope.$watch('ui.chatOpen', function (isOpen) {
  144. if (isOpen) {
  145. eventTracking.sendMBOnce('ide-open-chat-once')
  146. }
  147. })
  148. $scope.$watch('ui.leftMenuShown', function (isOpen) {
  149. if (isOpen) {
  150. eventTracking.sendMBOnce('ide-open-left-menu-once')
  151. }
  152. })
  153. $scope.trackHover = feature => {
  154. eventTracking.sendMBOnce(`ide-hover-${feature}-once`)
  155. }
  156. // End of tracking code.
  157. window._ide = ide
  158. ide.validFileRegex = '^[^*/]*$' // Don't allow * and /
  159. const useFallbackWebsocket =
  160. window.location &&
  161. window.location.search &&
  162. window.location.search.match(/ws=fallback/)
  163. // if we previously failed to load the websocket fall back to null (the siteUrl)
  164. ide.wsUrl = useFallbackWebsocket ? null : window.sharelatex.wsUrl || null // websocket url (if defined)
  165. ide.project_id = $scope.project_id = window.project_id
  166. ide.$scope = $scope
  167. ide.referencesSearchManager = new ReferencesManager(ide, $scope)
  168. ide.loadingManager = new LoadingManager($scope)
  169. ide.connectionManager = new ConnectionManager(ide, $scope)
  170. ide.fileTreeManager = new FileTreeManager(ide, $scope)
  171. ide.editorManager = new EditorManager(
  172. ide,
  173. $scope,
  174. localStorage,
  175. eventTracking
  176. )
  177. ide.onlineUsersManager = new OnlineUsersManager(ide, $scope)
  178. ide.permissionsManager = new PermissionsManager(ide, $scope)
  179. ide.binaryFilesManager = new BinaryFilesManager(ide, $scope)
  180. ide.metadataManager = new MetadataManager(ide, $scope, metadata)
  181. let inited = false
  182. $scope.$on('project:joined', function () {
  183. if (inited) {
  184. return
  185. }
  186. inited = true
  187. if (
  188. __guard__(
  189. $scope != null ? $scope.project : undefined,
  190. x => x.deletedByExternalDataSource
  191. )
  192. ) {
  193. ide.showGenericMessageModal(
  194. 'Project Renamed or Deleted',
  195. `\
  196. This project has either been renamed or deleted by an external data source such as Dropbox.
  197. We don't want to delete your data on Overleaf, so this project still contains your history and collaborators.
  198. If the project has been renamed please look in your project list for a new project under the new name.\
  199. `
  200. )
  201. }
  202. return $timeout(function () {
  203. if ($scope.permissions.write) {
  204. let _labelsInitialLoadDone
  205. ide.metadataManager.loadProjectMetaFromServer()
  206. return (_labelsInitialLoadDone = true)
  207. }
  208. }, 200)
  209. })
  210. // Count the first 'doc:opened' as a sign that the ide is loaded
  211. // and broadcast a message. This is a good event to listen for
  212. // if you want to wait until the ide is fully loaded and initialized
  213. let _loaded = false
  214. $scope.$on('doc:opened', function () {
  215. if (_loaded) {
  216. return
  217. }
  218. $scope.$broadcast('ide:loaded')
  219. return (_loaded = true)
  220. })
  221. ide.editingSessionHeartbeat = () => {
  222. eventTracking.editingSessionHeartbeat(() => {
  223. const editorType = ide.editorManager.getEditorType()
  224. const segmentation = {
  225. editorType,
  226. }
  227. if (editorType === 'cm6' || editorType === 'cm6-rich-text') {
  228. const cm6PerfData = reportCM6Perf()
  229. // Ignore if no typing has happened
  230. if (cm6PerfData.numberOfEntries > 0) {
  231. const perfProps = [
  232. 'Max',
  233. 'Mean',
  234. 'Median',
  235. 'NinetyFifthPercentile',
  236. 'DocLength',
  237. 'NumberOfEntries',
  238. 'MaxUserEventsBetweenDomUpdates',
  239. 'Grammarly',
  240. 'SessionLength',
  241. 'Memory',
  242. 'Lags',
  243. 'NonLags',
  244. 'LongestLag',
  245. 'MeanLagsPerMeasure',
  246. 'MeanKeypressesPerMeasure',
  247. 'MeanKeypressPaint',
  248. 'LongTasks',
  249. 'Release',
  250. ]
  251. for (const prop of perfProps) {
  252. const perfValue =
  253. cm6PerfData[prop.charAt(0).toLowerCase() + prop.slice(1)]
  254. if (perfValue !== null) {
  255. segmentation['cm6Perf' + prop] = perfValue
  256. }
  257. }
  258. }
  259. }
  260. return segmentation
  261. })
  262. }
  263. $scope.$on('cursor:editor:update', () => {
  264. ide.editingSessionHeartbeat()
  265. })
  266. $scope.$on('scroll:editor:update', () => {
  267. ide.editingSessionHeartbeat()
  268. })
  269. angular.element($window).on('click', ide.editingSessionHeartbeat)
  270. $scope.$on('$destroy', () =>
  271. angular.element($window).off('click', ide.editingSessionHeartbeat)
  272. )
  273. const DARK_THEMES = [
  274. 'ambiance',
  275. 'chaos',
  276. 'clouds_midnight',
  277. 'cobalt',
  278. 'idle_fingers',
  279. 'merbivore',
  280. 'merbivore_soft',
  281. 'mono_industrial',
  282. 'monokai',
  283. 'pastel_on_dark',
  284. 'solarized_dark',
  285. 'terminal',
  286. 'tomorrow_night',
  287. 'tomorrow_night_blue',
  288. 'tomorrow_night_bright',
  289. 'tomorrow_night_eighties',
  290. 'twilight',
  291. 'vibrant_ink',
  292. ]
  293. $scope.darkTheme = false
  294. // Listen for settings change from React
  295. window.addEventListener('settings:change', event => {
  296. $scope.darkTheme = DARK_THEMES.includes(event.detail.editorTheme)
  297. })
  298. ide.localStorage = localStorage
  299. $scope.switchToFlatLayout = function (view) {
  300. $scope.ui.pdfLayout = 'flat'
  301. $scope.ui.view = view
  302. return ide.localStorage('pdf.layout', 'flat')
  303. }
  304. $scope.switchToSideBySideLayout = function (view) {
  305. $scope.ui.pdfLayout = 'sideBySide'
  306. $scope.ui.view = view
  307. return localStorage('pdf.layout', 'split')
  308. }
  309. if ((pdfLayout = localStorage('pdf.layout'))) {
  310. if (pdfLayout === 'split') {
  311. $scope.switchToSideBySideLayout()
  312. }
  313. if (pdfLayout === 'flat') {
  314. $scope.switchToFlatLayout()
  315. }
  316. } else {
  317. $scope.switchToSideBySideLayout()
  318. }
  319. // Update ui.pdfOpen when the layout changes.
  320. // The east pane should open when the layout changes from "Editor only" or "PDF only" to "Editor & PDF".
  321. $scope.$watch('ui.pdfLayout', value => {
  322. $scope.ui.pdfOpen = value === 'sideBySide'
  323. })
  324. // Update ui.pdfLayout when the east pane is toggled.
  325. // The layout should be set to "Editor & PDF" (sideBySide) when the east pane is opened, and "Editor only" (flat) when the east pane is closed.
  326. $scope.$watch('ui.pdfOpen', value => {
  327. $scope.ui.pdfLayout = value ? 'sideBySide' : 'flat'
  328. if (value) {
  329. window.dispatchEvent(new CustomEvent('ui:pdf-open'))
  330. }
  331. })
  332. $scope.handleKeyDown = () => {
  333. // unused?
  334. }
  335. // User can append ?ft=somefeature to url to activate a feature toggle
  336. ide.featureToggle = __guard__(
  337. __guard__(
  338. typeof location !== 'undefined' && location !== null
  339. ? location.search
  340. : undefined,
  341. x1 => x1.match(/^\?ft=(\w+)$/)
  342. ),
  343. x => x[1]
  344. )
  345. // Listen for editor:lint event from CM6 linter
  346. window.addEventListener('editor:lint', event => {
  347. $scope.hasLintingError = event.detail.hasLintingError
  348. })
  349. ide.socket.on('project:access:revoked', () => {
  350. ide.showGenericMessageModal(
  351. 'Removed From Project',
  352. 'You have been removed from this project, and will no longer have access to it. You will be redirected to your project dashboard momentarily.'
  353. )
  354. })
  355. return ide.socket.on('project:publicAccessLevel:changed', data => {
  356. if (data.newAccessLevel != null) {
  357. ide.$scope.project.publicAccesLevel = data.newAccessLevel
  358. return $scope.$digest()
  359. }
  360. })
  361. },
  362. ])
  363. cleanupServiceWorker()
  364. angular.module('SharelatexApp').config([
  365. '$provide',
  366. function ($provide) {
  367. $provide.decorator('$browser', [
  368. '$delegate',
  369. function ($delegate) {
  370. $delegate.onUrlChange = function () {}
  371. $delegate.url = function () {
  372. return ''
  373. }
  374. return $delegate
  375. },
  376. ])
  377. },
  378. ])
  379. export default angular.bootstrap(document.body, ['SharelatexApp'])
  380. function __guard__(value, transform) {
  381. return typeof value !== 'undefined' && value !== null
  382. ? transform(value)
  383. : undefined
  384. }