ide.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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('ide', [
  21. '$http',
  22. 'queuedHttp',
  23. '$modal',
  24. '$q',
  25. '$filter',
  26. '$timeout',
  27. 'eventTracking',
  28. function ($http, queuedHttp, $modal, $q, $filter, $timeout, eventTracking) {
  29. const ide = {}
  30. ide.$http = $http
  31. ide.queuedHttp = queuedHttp
  32. ide.$q = $q
  33. ide.$filter = $filter
  34. ide.$timeout = $timeout
  35. ide.globalEditorWatchdogManager = new EditorWatchdogManager({
  36. onTimeoutHandler: meta => {
  37. eventTracking.sendMB('losing-edits', meta)
  38. // clone the meta object, reportError adds additional fields into it
  39. ide.reportError('losing-edits', Object.assign({}, meta))
  40. },
  41. })
  42. this.recentEvents = []
  43. ide.pushEvent = (type, meta) => {
  44. if (meta == null) {
  45. meta = {}
  46. }
  47. debugConsole.log('event', type, meta)
  48. this.recentEvents.push({ type, meta, date: new Date() })
  49. if (this.recentEvents.length > 100) {
  50. return this.recentEvents.shift()
  51. }
  52. }
  53. ide.reportError = (error, meta) => {
  54. if (meta == null) {
  55. meta = {}
  56. }
  57. meta.user_id = window.user_id
  58. meta.project_id = window.project_id
  59. meta.client_id = __guard__(
  60. ide.socket != null ? ide.socket.socket : undefined,
  61. x => x.sessionid
  62. )
  63. meta.transport = __guard__(
  64. __guard__(
  65. ide.socket != null ? ide.socket.socket : undefined,
  66. x2 => x2.transport
  67. ),
  68. x1 => x1.name
  69. )
  70. meta.client_now = new Date()
  71. const errorObj = {}
  72. if (typeof error === 'object') {
  73. for (const key of Array.from(Object.getOwnPropertyNames(error))) {
  74. errorObj[key] = error[key]
  75. }
  76. } else if (typeof error === 'string') {
  77. errorObj.message = error
  78. }
  79. return $http.post('/error/client', {
  80. error: errorObj,
  81. meta,
  82. _csrf: window.csrfToken,
  83. })
  84. }
  85. ide.showGenericMessageModal = (title, message) =>
  86. $modal.open({
  87. templateUrl: 'genericMessageModalTemplate',
  88. controller: 'GenericMessageModalController',
  89. resolve: {
  90. title() {
  91. return title
  92. },
  93. message() {
  94. return message
  95. },
  96. },
  97. })
  98. ide.showOutOfSyncModal = (title, message, editorContent) =>
  99. $modal.open({
  100. templateUrl: 'outOfSyncModalTemplate',
  101. controller: 'OutOfSyncModalController',
  102. backdrop: false, // not dismissable by clicking background
  103. keyboard: false, // prevent dismiss via keyboard
  104. resolve: {
  105. title() {
  106. return title
  107. },
  108. message() {
  109. return message
  110. },
  111. editorContent() {
  112. return editorContent
  113. },
  114. },
  115. windowClass: 'out-of-sync-modal',
  116. })
  117. ide.showLockEditorMessageModal = (title, message) =>
  118. // modal to block the editor when connection is down
  119. $modal.open({
  120. templateUrl: 'lockEditorModalTemplate',
  121. controller: 'GenericMessageModalController',
  122. backdrop: false, // not dismissable by clicking background
  123. keyboard: false, // prevent dismiss via keyboard
  124. resolve: {
  125. title() {
  126. return title
  127. },
  128. message() {
  129. return message
  130. },
  131. },
  132. windowClass: 'lock-editor-modal',
  133. })
  134. return ide
  135. },
  136. ])
  137. App.controller('GenericMessageModalController', [
  138. '$scope',
  139. '$modalInstance',
  140. 'title',
  141. 'message',
  142. function ($scope, $modalInstance, title, message) {
  143. $scope.title = title
  144. $scope.message = message
  145. return ($scope.done = () => $modalInstance.close())
  146. },
  147. ])
  148. App.controller('OutOfSyncModalController', [
  149. '$scope',
  150. '$window',
  151. 'title',
  152. 'message',
  153. 'editorContent',
  154. function ($scope, $window, title, message, editorContent) {
  155. $scope.title = title
  156. $scope.message = message
  157. $scope.editorContent = editorContent
  158. $scope.editorContentRows = editorContent.split('\n').length
  159. $scope.done = () => {
  160. // Reload the page to avoid staying in an inconsistent state.
  161. // https://github.com/overleaf/issues/issues/3694
  162. $window.location.reload()
  163. }
  164. },
  165. ])
  166. function __guard__(value, transform) {
  167. return typeof value !== 'undefined' && value !== null
  168. ? transform(value)
  169. : undefined
  170. }