SettingsController.js 6.9 KB

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