🎨 Updates data state for custom CSS

This commit is contained in:
Alicia Sykes 2021-10-28 00:11:46 +01:00
parent 618ab1c439
commit a19b8b8f88

View File

@ -4,7 +4,7 @@
<div class="css-wrapper"> <div class="css-wrapper">
<h2 class="css-input-title">Custom CSS</h2> <h2 class="css-input-title">Custom CSS</h2>
<textarea class="css-editor" v-model="customCss" /> <textarea class="css-editor" v-model="customCss" />
<Button class="save-button" :click="save">{{ $t('config.css-save-btn') }}</button> <Button class="save-button" :click="save">{{ $t('config.css-save-btn') }}</Button>
<p class="quick-note"> <p class="quick-note">
<b>{{ $t('config.css-note-label') }}:</b> <b>{{ $t('config.css-note-label') }}:</b>
{{ $t('config.css-note-l1') }} {{ $t('config.css-note-l2') }} {{ $t('config.css-note-l3') }} {{ $t('config.css-note-l1') }} {{ $t('config.css-note-l2') }} {{ $t('config.css-note-l3') }}
@ -20,49 +20,65 @@
import CustomThemeMaker from '@/components/Settings/CustomThemeMaker'; import CustomThemeMaker from '@/components/Settings/CustomThemeMaker';
import Button from '@/components/FormElements/Button'; import Button from '@/components/FormElements/Button';
import { getTheme } from '@/utils/ConfigHelpers'; import StoreKeys from '@/utils/StoreMutations';
import { localStorageKeys } from '@/utils/defaults';
import { InfoHandler } from '@/utils/ErrorHandler'; import { InfoHandler } from '@/utils/ErrorHandler';
import { localStorageKeys, theme as defaultTheme } from '@/utils/defaults';
export default { export default {
name: 'StyleEditor', name: 'StyleEditor',
props: {
config: Object,
},
components: { components: {
Button, Button,
CustomThemeMaker, CustomThemeMaker,
}, },
computed: {
appConfig() {
return this.$store.getters.appConfig;
},
currentTheme() {
return this.appConfig.theme || defaultTheme;
},
},
data() { data() {
return { return {
customCss: this.config.appConfig.customCss || '\n\n', customCss: '',
currentTheme: getTheme(),
}; };
}, },
mounted() {
// Get existing custom styles (if present) from appConfig
this.customCss = this.appConfig.customCss || '\n\n';
},
methods: { methods: {
/* Save custom CSS in browser, call inject, and show success message */ /* Sanitizes input, saves to browser and store, applies to page and shows message */
save() { save() {
// Get, and sanitize users CSS
const css = this.customCss.replace(/<\/?[^>]+(>|$)/g, ''); const css = this.customCss.replace(/<\/?[^>]+(>|$)/g, '');
// Update app config, and apply settings locally this.$store.commit(StoreKeys.UPDATE_CUSTOM_CSS, css);
const appConfig = { ...this.config.appConfig }; this.saveToBrowser(css);
appConfig.customCss = css; this.injectToPage(css);
localStorage.setItem(localStorageKeys.APP_CONFIG, JSON.stringify(appConfig)); this.showSuccessMsg();
// Immidiatley inject new CSS if (css === '') this.reloadPage();
this.inject(css);
// If reseting styles, then refresh the page
if (css === '') setTimeout(() => { location.reload(); }, 1500); // eslint-disable-line no-restricted-globals
// Show status message
InfoHandler('User syles has been saved', 'Custom CSS Update');
this.$toasted.show('Changes saved successfully');
}, },
/* Formats CSS, and applies it to page */ /* Formats CSS, and applies it to page */
inject(userStyles) { injectToPage(userStyles) {
const cleanedCss = userStyles.replace(/<\/?[^>]+(>|$)/g, ''); const cleanedCss = userStyles.replace(/<\/?[^>]+(>|$)/g, '');
const style = document.createElement('style'); const style = document.createElement('style');
style.textContent = cleanedCss; style.textContent = cleanedCss;
document.head.append(style); document.head.append(style);
}, },
/* Saves custom CSS local storage */
saveToBrowser(css) {
const localAppConfig = JSON.parse(localStorage.getItem(localStorageKeys.APP_CONFIG) || '{}');
localAppConfig.customCss = css;
localStorage.setItem(localStorageKeys.APP_CONFIG, JSON.stringify(localAppConfig));
},
/* Reload the page (only called if removing styles) */
reloadPage() {
setTimeout(() => { location.reload(); }, 1500); // eslint-disable-line no-restricted-globals
},
/* Show success toast and lot update */
showSuccessMsg() {
this.$toasted.show('Changes saved successfully');
InfoHandler('User syles has been saved', 'Custom CSS');
},
}, },
}; };
</script> </script>