SettingsController.js 7.2 KB

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