ide.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS101: Remove unnecessary use of Array.from
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS103: Rewrite code to no longer use __guard__
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. import App from '../../base'
  16. import EditorWatchdogManager from '../connection/EditorWatchdogManager'
  17. import { debugConsole } from '@/utils/debugging'
  18. // We create and provide this as service so that we can access the global ide
  19. // from within other parts of the angular app.
  20. App.factory(
  21. 'ide',
  22. function ($http, queuedHttp, $modal, $q, $filter, $timeout, eventTracking) {
  23. const ide = {}
  24. ide.$http = $http
  25. ide.queuedHttp = queuedHttp
  26. ide.$q = $q
  27. ide.$filter = $filter
  28. ide.$timeout = $timeout
  29. ide.globalEditorWatchdogManager = new EditorWatchdogManager({
  30. onTimeoutHandler: meta => {
  31. eventTracking.sendMB('losing-edits', meta)
  32. // clone the meta object, reportError adds additional fields into it
  33. ide.reportError('losing-edits', Object.assign({}, meta))
  34. },
  35. })
  36. this.recentEvents = []
  37. ide.pushEvent = (type, meta) => {
  38. if (meta == null) {
  39. meta = {}
  40. }
  41. debugConsole.log('event', type, meta)
  42. this.recentEvents.push({ type, meta, date: new Date() })
  43. if (this.recentEvents.length > 100) {
  44. return this.recentEvents.shift()
  45. }
  46. }
  47. ide.reportError = (error, meta) => {
  48. if (meta == null) {
  49. meta = {}
  50. }
  51. meta.user_id = window.user_id
  52. meta.project_id = window.project_id
  53. meta.client_id = __guard__(
  54. ide.socket != null ? ide.socket.socket : undefined,
  55. x => x.sessionid
  56. )
  57. meta.transport = __guard__(
  58. __guard__(
  59. ide.socket != null ? ide.socket.socket : undefined,
  60. x2 => x2.transport
  61. ),
  62. x1 => x1.name
  63. )
  64. meta.client_now = new Date()
  65. const errorObj = {}
  66. if (typeof error === 'object') {
  67. for (const key of Array.from(Object.getOwnPropertyNames(error))) {
  68. errorObj[key] = error[key]
  69. }
  70. } else if (typeof error === 'string') {
  71. errorObj.message = error
  72. }
  73. return $http.post('/error/client', {
  74. error: errorObj,
  75. meta,
  76. _csrf: window.csrfToken,
  77. })
  78. }
  79. ide.showGenericMessageModal = (title, message) =>
  80. $modal.open({
  81. templateUrl: 'genericMessageModalTemplate',
  82. controller: 'GenericMessageModalController',
  83. resolve: {
  84. title() {
  85. return title
  86. },
  87. message() {
  88. return message
  89. },
  90. },
  91. })
  92. ide.showOutOfSyncModal = (title, message, editorContent) =>
  93. $modal.open({
  94. templateUrl: 'outOfSyncModalTemplate',
  95. controller: 'OutOfSyncModalController',
  96. backdrop: false, // not dismissable by clicking background
  97. keyboard: false, // prevent dismiss via keyboard
  98. resolve: {
  99. title() {
  100. return title
  101. },
  102. message() {
  103. return message
  104. },
  105. editorContent() {
  106. return editorContent
  107. },
  108. },
  109. windowClass: 'out-of-sync-modal',
  110. })
  111. ide.showLockEditorMessageModal = (title, message) =>
  112. // modal to block the editor when connection is down
  113. $modal.open({
  114. templateUrl: 'lockEditorModalTemplate',
  115. controller: 'GenericMessageModalController',
  116. backdrop: false, // not dismissable by clicking background
  117. keyboard: false, // prevent dismiss via keyboard
  118. resolve: {
  119. title() {
  120. return title
  121. },
  122. message() {
  123. return message
  124. },
  125. },
  126. windowClass: 'lock-editor-modal',
  127. })
  128. return ide
  129. }
  130. )
  131. App.controller(
  132. 'GenericMessageModalController',
  133. function ($scope, $modalInstance, title, message) {
  134. $scope.title = title
  135. $scope.message = message
  136. return ($scope.done = () => $modalInstance.close())
  137. }
  138. )
  139. App.controller(
  140. 'OutOfSyncModalController',
  141. function ($scope, $window, title, message, editorContent) {
  142. $scope.title = title
  143. $scope.message = message
  144. $scope.editorContent = editorContent
  145. $scope.editorContentRows = editorContent.split('\n').length
  146. $scope.done = () => {
  147. // Reload the page to avoid staying in an inconsistent state.
  148. // https://github.com/overleaf/issues/issues/3694
  149. $window.location.reload()
  150. }
  151. }
  152. )
  153. function __guard__(value, transform) {
  154. return typeof value !== 'undefined' && value !== null
  155. ? transform(value)
  156. : undefined
  157. }