SettingsController.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* eslint-disable
  2. camelcase,
  3. max-len,
  4. no-return-assign,
  5. no-undef,
  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. define(['base'], App =>
  16. App.controller('SettingsController', function($scope, settings, ide, _) {
  17. $scope.overallThemesList = window.overallThemes
  18. $scope.ui = { loadingStyleSheet: false }
  19. const _updateCSSFile = function(theme) {
  20. $scope.ui.loadingStyleSheet = true
  21. const docHeadEl = document.querySelector('head')
  22. const oldStyleSheetEl = document.getElementById('main-stylesheet')
  23. const newStyleSheetEl = document.createElement('link')
  24. newStyleSheetEl.addEventListener('load', e => {
  25. return $scope.$applyAsync(() => {
  26. $scope.ui.loadingStyleSheet = false
  27. return docHeadEl.removeChild(oldStyleSheetEl)
  28. })
  29. })
  30. newStyleSheetEl.setAttribute('rel', 'stylesheet')
  31. newStyleSheetEl.setAttribute('id', 'main-stylesheet')
  32. newStyleSheetEl.setAttribute('href', theme.path)
  33. return docHeadEl.appendChild(newStyleSheetEl)
  34. }
  35. if (!['default', 'vim', 'emacs'].includes($scope.settings.mode)) {
  36. $scope.settings.mode = 'default'
  37. }
  38. if (!['pdfjs', 'native'].includes($scope.settings.pdfViewer)) {
  39. $scope.settings.pdfViewer = 'pdfjs'
  40. }
  41. if (
  42. $scope.settings.fontFamily != null &&
  43. !['monaco', 'lucida'].includes($scope.settings.fontFamily)
  44. ) {
  45. delete $scope.settings.fontFamily
  46. }
  47. if (
  48. $scope.settings.lineHeight != null &&
  49. !['compact', 'normal', 'wide'].includes($scope.settings.lineHeight)
  50. ) {
  51. delete $scope.settings.lineHeight
  52. }
  53. $scope.fontSizeAsStr = function(newVal) {
  54. if (newVal != null) {
  55. $scope.settings.fontSize = newVal
  56. }
  57. return $scope.settings.fontSize.toString()
  58. }
  59. $scope.$watch('settings.editorTheme', (editorTheme, oldEditorTheme) => {
  60. if (editorTheme !== oldEditorTheme) {
  61. return settings.saveSettings({ editorTheme })
  62. }
  63. })
  64. $scope.$watch('settings.overallTheme', (overallTheme, oldOverallTheme) => {
  65. if (overallTheme !== oldOverallTheme) {
  66. const chosenTheme = _.find(
  67. $scope.overallThemesList,
  68. theme => theme.val === overallTheme
  69. )
  70. if (chosenTheme != null) {
  71. _updateCSSFile(chosenTheme)
  72. return settings.saveSettings({ overallTheme })
  73. }
  74. }
  75. })
  76. $scope.$watch('settings.fontSize', (fontSize, oldFontSize) => {
  77. if (fontSize !== oldFontSize) {
  78. return settings.saveSettings({ fontSize: parseInt(fontSize, 10) })
  79. }
  80. })
  81. $scope.$watch('settings.mode', (mode, oldMode) => {
  82. if (mode !== oldMode) {
  83. return settings.saveSettings({ mode })
  84. }
  85. })
  86. $scope.$watch('settings.autoComplete', (autoComplete, oldAutoComplete) => {
  87. if (autoComplete !== oldAutoComplete) {
  88. return settings.saveSettings({ autoComplete })
  89. }
  90. })
  91. $scope.$watch(
  92. 'settings.autoPairDelimiters',
  93. (autoPairDelimiters, oldAutoPairDelimiters) => {
  94. if (autoPairDelimiters !== oldAutoPairDelimiters) {
  95. return settings.saveSettings({ autoPairDelimiters })
  96. }
  97. }
  98. )
  99. $scope.$watch('settings.pdfViewer', (pdfViewer, oldPdfViewer) => {
  100. if (pdfViewer !== oldPdfViewer) {
  101. return settings.saveSettings({ pdfViewer })
  102. }
  103. })
  104. $scope.$watch(
  105. 'settings.syntaxValidation',
  106. (syntaxValidation, oldSyntaxValidation) => {
  107. if (syntaxValidation !== oldSyntaxValidation) {
  108. return settings.saveSettings({ syntaxValidation })
  109. }
  110. }
  111. )
  112. $scope.$watch('settings.fontFamily', (fontFamily, oldFontFamily) => {
  113. if (fontFamily !== oldFontFamily) {
  114. return settings.saveSettings({ fontFamily })
  115. }
  116. })
  117. $scope.$watch('settings.lineHeight', (lineHeight, oldLineHeight) => {
  118. if (lineHeight !== oldLineHeight) {
  119. return settings.saveSettings({ lineHeight })
  120. }
  121. })
  122. $scope.$watch('project.spellCheckLanguage', (language, oldLanguage) => {
  123. if (this.ignoreUpdates) {
  124. return
  125. }
  126. if (oldLanguage != null && language !== oldLanguage) {
  127. settings.saveProjectSettings({ spellCheckLanguage: language })
  128. // Also set it as the default for the user
  129. return settings.saveSettings({ spellCheckLanguage: language })
  130. }
  131. })
  132. $scope.$watch('project.compiler', (compiler, oldCompiler) => {
  133. if (this.ignoreUpdates) {
  134. return
  135. }
  136. if (oldCompiler != null && compiler !== oldCompiler) {
  137. return settings.saveProjectSettings({ compiler })
  138. }
  139. })
  140. $scope.$watch('project.imageName', (imageName, oldImageName) => {
  141. if (this.ignoreUpdates) {
  142. return
  143. }
  144. if (oldImageName != null && imageName !== oldImageName) {
  145. return settings.saveProjectSettings({ imageName })
  146. }
  147. })
  148. $scope.$watch('project.rootDoc_id', (rootDoc_id, oldRootDoc_id) => {
  149. if (this.ignoreUpdates) {
  150. return
  151. }
  152. // don't save on initialisation, Angular passes oldRootDoc_id as
  153. // undefined in this case.
  154. if (typeof oldRootDoc_id === 'undefined') {
  155. return
  156. }
  157. // otherwise only save changes, null values are allowed
  158. if (rootDoc_id !== oldRootDoc_id) {
  159. return settings.saveProjectSettings({ rootDocId: rootDoc_id })
  160. }
  161. })
  162. ide.socket.on('compilerUpdated', compiler => {
  163. this.ignoreUpdates = true
  164. $scope.$apply(() => {
  165. return ($scope.project.compiler = compiler)
  166. })
  167. return delete this.ignoreUpdates
  168. })
  169. ide.socket.on('imageNameUpdated', imageName => {
  170. this.ignoreUpdates = true
  171. $scope.$apply(() => {
  172. return ($scope.project.imageName = imageName)
  173. })
  174. return delete this.ignoreUpdates
  175. })
  176. return ide.socket.on('spellCheckLanguageUpdated', languageCode => {
  177. this.ignoreUpdates = true
  178. $scope.$apply(() => {
  179. return ($scope.project.spellCheckLanguage = languageCode)
  180. })
  181. return delete this.ignoreUpdates
  182. })
  183. }))