2020-06-23 04:45:38 -04:00
|
|
|
import _ from 'lodash'
|
2018-11-05 05:06:39 -05:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
max-len,
|
|
|
|
no-return-assign,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-05-19 05:02:56 -04:00
|
|
|
import App from '../../../base'
|
|
|
|
|
|
|
|
export default App.controller('SettingsController', function(
|
|
|
|
$scope,
|
|
|
|
ExposedSettings,
|
|
|
|
settings,
|
2020-06-23 04:45:38 -04:00
|
|
|
ide
|
2020-05-19 05:02:56 -04:00
|
|
|
) {
|
|
|
|
const validRootDocExtensions = ExposedSettings.validRootDocExtensions
|
|
|
|
const validRootDocRegExp = new RegExp(
|
|
|
|
`\\.(${validRootDocExtensions.join('|')})$`,
|
|
|
|
'i'
|
|
|
|
)
|
|
|
|
|
|
|
|
$scope.overallThemesList = window.overallThemes
|
|
|
|
$scope.ui = { loadingStyleSheet: false }
|
|
|
|
|
|
|
|
const _updateCSSFile = function(theme) {
|
|
|
|
$scope.ui.loadingStyleSheet = true
|
|
|
|
const docHeadEl = document.querySelector('head')
|
|
|
|
const oldStyleSheetEl = document.getElementById('main-stylesheet')
|
|
|
|
const newStyleSheetEl = document.createElement('link')
|
|
|
|
newStyleSheetEl.addEventListener('load', e => {
|
|
|
|
return $scope.$applyAsync(() => {
|
|
|
|
$scope.ui.loadingStyleSheet = false
|
|
|
|
return docHeadEl.removeChild(oldStyleSheetEl)
|
2019-07-16 05:13:18 -04:00
|
|
|
})
|
2020-05-19 05:02:56 -04:00
|
|
|
})
|
|
|
|
newStyleSheetEl.setAttribute('rel', 'stylesheet')
|
|
|
|
newStyleSheetEl.setAttribute('id', 'main-stylesheet')
|
|
|
|
newStyleSheetEl.setAttribute('href', theme.path)
|
|
|
|
return docHeadEl.appendChild(newStyleSheetEl)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!['default', 'vim', 'emacs'].includes($scope.settings.mode)) {
|
|
|
|
$scope.settings.mode = 'default'
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!['pdfjs', 'native'].includes($scope.settings.pdfViewer)) {
|
|
|
|
$scope.settings.pdfViewer = 'pdfjs'
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
$scope.settings.fontFamily != null &&
|
|
|
|
!['monaco', 'lucida'].includes($scope.settings.fontFamily)
|
|
|
|
) {
|
|
|
|
delete $scope.settings.fontFamily
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
if (
|
|
|
|
$scope.settings.lineHeight != null &&
|
|
|
|
!['compact', 'normal', 'wide'].includes($scope.settings.lineHeight)
|
|
|
|
) {
|
|
|
|
delete $scope.settings.lineHeight
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.fontSizeAsStr = function(newVal) {
|
|
|
|
if (newVal != null) {
|
|
|
|
$scope.settings.fontSize = newVal
|
2019-07-16 05:13:18 -04:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
return $scope.settings.fontSize.toString()
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.getValidMainDocs = () => {
|
|
|
|
let filteredDocs = []
|
|
|
|
if ($scope.docs) {
|
|
|
|
// Filter the existing docs (editable files) by accepted file extensions.
|
|
|
|
// It's possible that an existing project has an invalid file selected as the main one.
|
|
|
|
// To gracefully handle that case, make sure we also show the current main file (ignoring extension).
|
|
|
|
filteredDocs = $scope.docs.filter(
|
|
|
|
doc =>
|
|
|
|
validRootDocRegExp.test(doc.doc.name) ||
|
|
|
|
$scope.project.rootDoc_id === doc.doc.id
|
|
|
|
)
|
2019-07-16 05:13:18 -04:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
return filteredDocs
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.$watch('settings.editorTheme', (editorTheme, oldEditorTheme) => {
|
|
|
|
if (editorTheme !== oldEditorTheme) {
|
|
|
|
return settings.saveSettings({ editorTheme })
|
2019-07-16 05:13:18 -04:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
$scope.$watch('settings.overallTheme', (overallTheme, oldOverallTheme) => {
|
|
|
|
if (overallTheme !== oldOverallTheme) {
|
|
|
|
const chosenTheme = _.find(
|
|
|
|
$scope.overallThemesList,
|
|
|
|
theme => theme.val === overallTheme
|
|
|
|
)
|
|
|
|
if (chosenTheme != null) {
|
|
|
|
_updateCSSFile(chosenTheme)
|
|
|
|
return settings.saveSettings({ overallTheme })
|
2019-10-03 10:10:00 -04:00
|
|
|
}
|
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
})
|
2019-10-03 10:10:00 -04:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.$watch('settings.fontSize', (fontSize, oldFontSize) => {
|
|
|
|
if (fontSize !== oldFontSize) {
|
|
|
|
return settings.saveSettings({ fontSize: parseInt(fontSize, 10) })
|
|
|
|
}
|
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.$watch('settings.mode', (mode, oldMode) => {
|
|
|
|
if (mode !== oldMode) {
|
|
|
|
return settings.saveSettings({ mode })
|
|
|
|
}
|
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.$watch('settings.autoComplete', (autoComplete, oldAutoComplete) => {
|
|
|
|
if (autoComplete !== oldAutoComplete) {
|
|
|
|
return settings.saveSettings({ autoComplete })
|
|
|
|
}
|
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.$watch(
|
|
|
|
'settings.autoPairDelimiters',
|
|
|
|
(autoPairDelimiters, oldAutoPairDelimiters) => {
|
|
|
|
if (autoPairDelimiters !== oldAutoPairDelimiters) {
|
|
|
|
return settings.saveSettings({ autoPairDelimiters })
|
2019-07-16 05:13:18 -04:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
|
|
|
)
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.$watch('settings.pdfViewer', (pdfViewer, oldPdfViewer) => {
|
|
|
|
if (pdfViewer !== oldPdfViewer) {
|
|
|
|
return settings.saveSettings({ pdfViewer })
|
|
|
|
}
|
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.$watch(
|
|
|
|
'settings.syntaxValidation',
|
|
|
|
(syntaxValidation, oldSyntaxValidation) => {
|
|
|
|
if (syntaxValidation !== oldSyntaxValidation) {
|
|
|
|
return settings.saveSettings({ syntaxValidation })
|
2019-07-16 05:13:18 -04:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
|
|
|
)
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.$watch('settings.fontFamily', (fontFamily, oldFontFamily) => {
|
|
|
|
if (fontFamily !== oldFontFamily) {
|
|
|
|
return settings.saveSettings({ fontFamily })
|
|
|
|
}
|
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.$watch('settings.lineHeight', (lineHeight, oldLineHeight) => {
|
|
|
|
if (lineHeight !== oldLineHeight) {
|
|
|
|
return settings.saveSettings({ lineHeight })
|
|
|
|
}
|
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.$watch('project.spellCheckLanguage', (language, oldLanguage) => {
|
|
|
|
if (this.ignoreUpdates) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (oldLanguage != null && language !== oldLanguage) {
|
|
|
|
settings.saveProjectSettings({ spellCheckLanguage: language })
|
|
|
|
// Also set it as the default for the user
|
|
|
|
return settings.saveSettings({ spellCheckLanguage: language })
|
|
|
|
}
|
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.$watch('project.compiler', (compiler, oldCompiler) => {
|
|
|
|
if (this.ignoreUpdates) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (oldCompiler != null && compiler !== oldCompiler) {
|
|
|
|
return settings.saveProjectSettings({ compiler })
|
|
|
|
}
|
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.$watch('project.imageName', (imageName, oldImageName) => {
|
|
|
|
if (this.ignoreUpdates) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (oldImageName != null && imageName !== oldImageName) {
|
|
|
|
return settings.saveProjectSettings({ imageName })
|
|
|
|
}
|
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.$watch('project.rootDoc_id', (rootDoc_id, oldRootDoc_id) => {
|
|
|
|
if (this.ignoreUpdates) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// don't save on initialisation, Angular passes oldRootDoc_id as
|
|
|
|
// undefined in this case.
|
|
|
|
if (
|
|
|
|
typeof rootDoc_id === 'undefined' &&
|
|
|
|
typeof oldRootDoc_id === 'undefined'
|
|
|
|
) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// otherwise only save changes, null values are allowed
|
|
|
|
if (rootDoc_id !== oldRootDoc_id) {
|
|
|
|
settings.saveProjectSettings({ rootDocId: rootDoc_id }).catch(() => {
|
|
|
|
$scope.project.rootDoc_id = oldRootDoc_id
|
2018-11-05 05:06:39 -05:00
|
|
|
})
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
ide.socket.on('compilerUpdated', compiler => {
|
|
|
|
this.ignoreUpdates = true
|
|
|
|
$scope.$apply(() => {
|
|
|
|
return ($scope.project.compiler = compiler)
|
2019-07-16 05:13:18 -04:00
|
|
|
})
|
2020-05-19 05:02:56 -04:00
|
|
|
return delete this.ignoreUpdates
|
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
ide.socket.on('imageNameUpdated', imageName => {
|
|
|
|
this.ignoreUpdates = true
|
|
|
|
$scope.$apply(() => {
|
|
|
|
return ($scope.project.imageName = imageName)
|
2019-07-16 05:13:18 -04:00
|
|
|
})
|
2020-05-19 05:02:56 -04:00
|
|
|
return delete this.ignoreUpdates
|
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
return ide.socket.on('spellCheckLanguageUpdated', languageCode => {
|
|
|
|
this.ignoreUpdates = true
|
|
|
|
$scope.$apply(() => {
|
|
|
|
return ($scope.project.spellCheckLanguage = languageCode)
|
2019-07-16 05:13:18 -04:00
|
|
|
})
|
2020-05-19 05:02:56 -04:00
|
|
|
return delete this.ignoreUpdates
|
|
|
|
})
|
|
|
|
})
|