SettingsController.js 6.9 KB

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