Skip to content

armgit / dashy

Public Imported from GitHub
0

πŸš€ A self-hostable personal dashboard built for you. Includes status-checking, widgets, themes, icon packs, a UI editor and tons more!

πŸ› Applies sub-page custom CSS, and re-applies styles on page change

Alicia Sykes368cdcb69713
diff --git a/src/App.vue b/src/App.vue
index eff1b9b9..a3f409a2 100644
--- a/src/App.vue
+++ b/src/App.vue
@@ -21,7 +21,7 @@ import { welcomeMsg } from '@/utils/logging/CoolConsole';
import ErrorHandler from '@/utils/logging/ErrorHandler';
import { syncPageMeta } from '@/utils/PageMeta';
import { viewFromPath } from '@/utils/config/ConfigHelpers';
-import { applyTheme, loadExternalStyleSheets } from '@/utils/Theming';
+import { applyTheme, applyPageStyles } from '@/utils/Theming';
import Keys from '@/utils/StoreMutations';
import { loadLocale } from '@/utils/languages';
import i18n from '@/utils/i18n';
@@ -61,9 +61,10 @@ export default {
immediate: true,
handler(t) { applyTheme(t, this.appConfig); },
},
- /* Sub-pages may set their own external stylesheets */
+ /* Apply custom CSS and stylesheets, and re-apply on page change for sub-pages */
'$store.state.currentConfigInfo.confId': {
- handler() { loadExternalStyleSheets(this.appConfig); },
+ immediate: true,
+ handler() { applyPageStyles(this.appConfig); },
},
/* Keep i18n in sync with the active page's language (incl. sub-page overrides) */
'appConfig.language': {
@@ -150,19 +151,6 @@ export default {
ErrorHandler(`Invalid contentMaxWidth value: '${usersCmw}'`);
return null;
},
- /* Injects the users custom CSS as a style tag */
- injectCustomStyles(usersCss) {
- const style = document.createElement('style');
- style.textContent = usersCss;
- document.head.insertBefore(style, document.querySelector('[data-external-source]'));
- },
- /* Applies user-defined custom CSS from appConfig */
- applyCustomStyles() {
- if (this.appConfig.customCss) {
- const cleanedCss = this.appConfig.customCss.replace(/<\/?[^>]+(>|$)/g, '');
- this.injectCustomStyles(cleanedCss);
- }
- },
/* Hide splash screen, either after 2 seconds, or immediately based on user preference */
hideSplash() {
if (this.shouldShowSplash) {
@@ -230,7 +218,6 @@ export default {
mounted() {
this.applyLanguage();
this.hideSplash();
- this.applyCustomStyles();
this.hideLoader();
welcomeMsg();
},
diff --git a/src/components/Configuration/CloudBackupRestore.vue b/src/components/Configuration/CloudBackupRestore.vue
index 84d6abe5..9f2fd98b 100644
--- a/src/components/Configuration/CloudBackupRestore.vue
+++ b/src/components/Configuration/CloudBackupRestore.vue
@@ -67,6 +67,7 @@ import Input from '@/components/FormElements/Input';
// Import utils and constants
import StoreKeys from '@/utils/StoreMutations';
import { backup, update, restore } from '@/utils/CloudBackup';
+import { applyPageStyles } from '@/utils/Theming';
import { localStorageKeys } from '@/utils/config/defaults';
import { configScope, stripRootOwnedFields } from '@/utils/config/ConfigHelpers';
import { InfoHandler, WarningInfoHandler, InfoKeys } from '@/utils/logging/ErrorHandler';
@@ -178,7 +179,8 @@ export default {
localStorage.setItem(localStorageKeys.CONF_PAGES, JSON.stringify(config.pages || []));
}
this.setBackupIdLocally(backupId, this.restorePassword);
- this.$store.dispatch(StoreKeys.APPLY_EDITED_CONFIG, clean);
+ this.$store.dispatch(StoreKeys.APPLY_EDITED_CONFIG, clean)
+ .then(() => applyPageStyles(this.$store.getters.appConfig));
this.showSuccessMsg(this.$t('cloud-sync.restore-success-msg'));
},
/* After backup/ update is made, then replace 'Make Backup' with 'Update Backup' */
diff --git a/src/components/Configuration/ConfigContainer.vue b/src/components/Configuration/ConfigContainer.vue
index beb57d1d..2b7289d7 100644
--- a/src/components/Configuration/ConfigContainer.vue
+++ b/src/components/Configuration/ConfigContainer.vue
@@ -99,6 +99,7 @@ import { localStorageKeys, modalNames } from '@/utils/config/defaults';
import { getUsersLanguage, clearScopedLocalConfig } from '@/utils/config/ConfigHelpers';
import ErrorHandler from '@/utils/logging/ErrorHandler';
import StoreKeys from '@/utils/StoreMutations';
+import { applyPageStyles } from '@/utils/Theming';
import CustomCssEditor from '@/components/Configuration/CustomCss';
import CloudBackupRestore from '@/components/Configuration/CloudBackupRestore';
import AppInfo from '@/components/Configuration/AppInfo';
@@ -222,7 +223,8 @@ export default {
confirmResetLocalSettings() {
clearScopedLocalConfig(this.$store.getters.pages);
this.$toast(this.$t('config.data-cleared-msg'));
- this.$store.dispatch(StoreKeys.INITIALIZE_CONFIG, this.$store.state.currentConfigInfo.confId);
+ this.$store.dispatch(StoreKeys.INITIALIZE_CONFIG, this.$store.state.currentConfigInfo.confId)
+ .then(() => applyPageStyles(this.$store.getters.appConfig));
},
resetConfirmMessage() {
return `${this.$t('config.reset-config-msg-l1')} `
diff --git a/src/components/Configuration/CustomCss.vue b/src/components/Configuration/CustomCss.vue
index e578efbd..4721d1a5 100644
--- a/src/components/Configuration/CustomCss.vue
+++ b/src/components/Configuration/CustomCss.vue
@@ -30,6 +30,7 @@ import Button from '@/components/FormElements/Button';
import StoreKeys from '@/utils/StoreMutations';
import { theme as defaultTheme } from '@/utils/config/defaults';
import { configScope } from '@/utils/config/ConfigHelpers';
+import { applyCustomCss } from '@/utils/Theming';
export default {
name: 'StyleEditor',
@@ -61,17 +62,10 @@ export default {
const css = this.customCss.replace(/<\/?[^>]+(>|$)/g, '');
this.$store.commit(StoreKeys.UPDATE_CUSTOM_CSS, css);
this.saveToBrowser(css);
- this.injectToPage(css);
+ applyCustomCss(css);
this.showSuccessMsg();
if (css === '') this.reloadPage();
},
- /* Formats CSS, and applies it to page */
- injectToPage(userStyles) {
- const cleanedCss = userStyles.replace(/<\/?[^>]+(>|$)/g, '');
- const style = document.createElement('style');
- style.textContent = cleanedCss;
- document.head.append(style);
- },
/* Saves custom CSS to localStorage, scoped to the active page */
saveToBrowser(css) {
const key = configScope(this.$store.state.currentConfigInfo.confId).APP_CONFIG;
diff --git a/src/utils/Theming.js b/src/utils/Theming.js
index 7c7c51a9..71dae912 100644
--- a/src/utils/Theming.js
+++ b/src/utils/Theming.js
@@ -6,6 +6,7 @@ import { localStorageKeys, mainCssVars, builtInThemes } from '@/utils/config/def
import ErrorHandler from '@/utils/logging/ErrorHandler';
const EXTERNAL_STYLE_ID = 'user-defined-stylesheet';
+const CUSTOM_CSS_ID = 'user-custom-css';
const EXTERNAL_SHEETS_TIMEOUT = 3000;
const html = () => document.documentElement;
@@ -46,6 +47,28 @@ export const loadExternalStyleSheets = (appConfig) => {
return Promise.race([Promise.all(loads), timeout]);
};
+/* Apply appConfig.customCss to the page, replacing whatever was applied before */
+export const applyCustomCss = (css) => {
+ const cleanCss = typeof css === 'string' ? css.replace(/<\/?[^>]+(>|$)/g, '') : '';
+ let style = document.getElementById(CUSTOM_CSS_ID);
+ if (!cleanCss) {
+ style?.remove();
+ return;
+ }
+ if (!style) {
+ style = document.createElement('style');
+ style.id = CUSTOM_CSS_ID;
+ document.head.insertBefore(style, document.querySelector('[data-external-source]'));
+ }
+ if (style.textContent !== cleanCss) style.textContent = cleanCss;
+};
+
+/* Apply a page's custom CSS and external stylesheets */
+export const applyPageStyles = (appConfig) => {
+ applyCustomCss(appConfig?.customCss);
+ return loadExternalStyleSheets(appConfig);
+};
+
/* Names of user-defined themes (appConfig.cssThemes). Always returns an array. */
export const getExtraThemeNames = (appConfig) => {
const t = appConfig?.cssThemes;