SettingsController.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import _ from 'lodash'
  2. /* eslint-disable
  3. camelcase,
  4. max-len,
  5. no-return-assign,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  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 isValidTeXFile from '../../../main/is-valid-tex-file'
  17. export default App.controller(
  18. 'SettingsController',
  19. function ($scope, settings, ide) {
  20. $scope.overallThemesList = window.overallThemes
  21. $scope.ui = { loadingStyleSheet: false }
  22. const _updateCSSFile = function (theme) {
  23. $scope.ui.loadingStyleSheet = true
  24. const docHeadEl = document.querySelector('head')
  25. const oldStyleSheetEl = document.getElementById('main-stylesheet')
  26. const newStyleSheetEl = document.createElement('link')
  27. newStyleSheetEl.addEventListener('load', e => {
  28. return $scope.$applyAsync(() => {
  29. $scope.ui.loadingStyleSheet = false
  30. return docHeadEl.removeChild(oldStyleSheetEl)
  31. })
  32. })
  33. newStyleSheetEl.setAttribute('rel', 'stylesheet')
  34. newStyleSheetEl.setAttribute('id', 'main-stylesheet')
  35. newStyleSheetEl.setAttribute('href', theme.path)
  36. return docHeadEl.appendChild(newStyleSheetEl)
  37. }
  38. if (!['default', 'vim', 'emacs'].includes($scope.settings.mode)) {
  39. $scope.settings.mode = 'default'
  40. }
  41. if (!['pdfjs', 'native'].includes($scope.settings.pdfViewer)) {
  42. $scope.settings.pdfViewer = 'pdfjs'
  43. }
  44. if (
  45. $scope.settings.fontFamily != null &&
  46. !['monaco', 'lucida'].includes($scope.settings.fontFamily)
  47. ) {
  48. delete $scope.settings.fontFamily
  49. }
  50. if (
  51. $scope.settings.lineHeight != null &&
  52. !['compact', 'normal', 'wide'].includes($scope.settings.lineHeight)
  53. ) {
  54. delete $scope.settings.lineHeight
  55. }
  56. $scope.fontSizeAsStr = function (newVal) {
  57. if (newVal != null) {
  58. $scope.settings.fontSize = newVal
  59. }
  60. return $scope.settings.fontSize.toString()
  61. }
  62. $scope.getValidMainDocs = () => {
  63. let filteredDocs = []
  64. if ($scope.docs) {
  65. // Filter the existing docs (editable files) by accepted file extensions.
  66. // It's possible that an existing project has an invalid file selected as the main one.
  67. // To gracefully handle that case, make sure we also show the current main file (ignoring extension).
  68. filteredDocs = $scope.docs.filter(
  69. doc =>
  70. isValidTeXFile(doc.doc.name) ||
  71. $scope.project.rootDoc_id === doc.doc.id
  72. )
  73. }
  74. return filteredDocs
  75. }
  76. $scope.$watch('settings.editorTheme', (editorTheme, oldEditorTheme) => {
  77. if (editorTheme !== oldEditorTheme) {
  78. return settings.saveSettings({ editorTheme })
  79. }
  80. })
  81. $scope.$watch('settings.overallTheme', (overallTheme, oldOverallTheme) => {
  82. if (overallTheme !== oldOverallTheme) {
  83. const chosenTheme = _.find(
  84. $scope.overallThemesList,
  85. theme => theme.val === overallTheme
  86. )
  87. if (chosenTheme != null) {
  88. _updateCSSFile(chosenTheme)
  89. return settings.saveSettings({ overallTheme })
  90. }
  91. }
  92. })
  93. $scope.$watch('settings.fontSize', (fontSize, oldFontSize) => {
  94. if (fontSize !== oldFontSize) {
  95. return settings.saveSettings({ fontSize: parseInt(fontSize, 10) })
  96. }
  97. })
  98. $scope.$watch('settings.mode', (mode, oldMode) => {
  99. if (mode !== oldMode) {
  100. return settings.saveSettings({ mode })
  101. }
  102. })
  103. $scope.$watch('settings.autoComplete', (autoComplete, oldAutoComplete) => {
  104. if (autoComplete !== oldAutoComplete) {
  105. return settings.saveSettings({ autoComplete })
  106. }
  107. })
  108. $scope.$watch(
  109. 'settings.autoPairDelimiters',
  110. (autoPairDelimiters, oldAutoPairDelimiters) => {
  111. if (autoPairDelimiters !== oldAutoPairDelimiters) {
  112. return settings.saveSettings({ autoPairDelimiters })
  113. }
  114. }
  115. )
  116. $scope.$watch('settings.pdfViewer', (pdfViewer, oldPdfViewer) => {
  117. if (pdfViewer !== oldPdfViewer) {
  118. return settings.saveSettings({ pdfViewer })
  119. }
  120. })
  121. $scope.$watch(
  122. 'settings.syntaxValidation',
  123. (syntaxValidation, oldSyntaxValidation) => {
  124. if (syntaxValidation !== oldSyntaxValidation) {
  125. return settings.saveSettings({ syntaxValidation })
  126. }
  127. }
  128. )
  129. $scope.$watch('settings.fontFamily', (fontFamily, oldFontFamily) => {
  130. if (fontFamily !== oldFontFamily) {
  131. return settings.saveSettings({ fontFamily })
  132. }
  133. })
  134. $scope.$watch('settings.lineHeight', (lineHeight, oldLineHeight) => {
  135. if (lineHeight !== oldLineHeight) {
  136. return settings.saveSettings({ lineHeight })
  137. }
  138. })
  139. $scope.$watch('project.spellCheckLanguage', (language, oldLanguage) => {
  140. if (this.ignoreUpdates) {
  141. return
  142. }
  143. if (oldLanguage != null && language !== oldLanguage) {
  144. settings.saveProjectSettings({ spellCheckLanguage: language })
  145. // Also set it as the default for the user
  146. return settings.saveSettings({ spellCheckLanguage: language })
  147. }
  148. })
  149. $scope.$watch('project.compiler', (compiler, oldCompiler) => {
  150. if (this.ignoreUpdates) {
  151. return
  152. }
  153. if (oldCompiler != null && compiler !== oldCompiler) {
  154. return settings.saveProjectSettings({ compiler })
  155. }
  156. })
  157. $scope.$watch('project.imageName', (imageName, oldImageName) => {
  158. if (this.ignoreUpdates) {
  159. return
  160. }
  161. if (oldImageName != null && imageName !== oldImageName) {
  162. return settings.saveProjectSettings({ imageName })
  163. }
  164. })
  165. let rootDocUpdateFailed = 0
  166. $scope.$watch('project.rootDoc_id', (rootDoc_id, oldRootDoc_id) => {
  167. if (this.ignoreUpdates) {
  168. return
  169. }
  170. // don't save on initialisation, Angular passes oldRootDoc_id as
  171. // undefined in this case.
  172. if (typeof oldRootDoc_id === 'undefined') {
  173. return
  174. }
  175. if ($scope.permissionsLevel === 'readOnly') {
  176. // The user is unauthorized to persist rootDoc changes.
  177. // Use the new value for this very editor session only.
  178. return
  179. }
  180. // otherwise only save changes, null values are allowed
  181. if (rootDoc_id !== oldRootDoc_id) {
  182. settings
  183. .saveProjectSettings({ rootDocId: rootDoc_id })
  184. .then(() => {
  185. rootDocUpdateFailed = 0
  186. })
  187. .catch(() => {
  188. rootDocUpdateFailed++
  189. // Let the login redirect run (if any) and reset afterwards.
  190. setTimeout(() => {
  191. if (rootDocUpdateFailed > 10) {
  192. // We are in a loop of failing updates. Stop now.
  193. this.ignoreUpdates = true
  194. }
  195. $scope.$apply(() => {
  196. $scope.project.rootDoc_id = oldRootDoc_id
  197. })
  198. this.ignoreUpdates = false
  199. })
  200. })
  201. }
  202. })
  203. ide.socket.on('compilerUpdated', compiler => {
  204. this.ignoreUpdates = true
  205. $scope.$apply(() => {
  206. return ($scope.project.compiler = compiler)
  207. })
  208. return delete this.ignoreUpdates
  209. })
  210. ide.socket.on('imageNameUpdated', imageName => {
  211. this.ignoreUpdates = true
  212. $scope.$apply(() => {
  213. return ($scope.project.imageName = imageName)
  214. })
  215. return delete this.ignoreUpdates
  216. })
  217. return ide.socket.on('spellCheckLanguageUpdated', languageCode => {
  218. this.ignoreUpdates = true
  219. $scope.$apply(() => {
  220. return ($scope.project.spellCheckLanguage = languageCode)
  221. })
  222. return delete this.ignoreUpdates
  223. })
  224. }
  225. )