From 36bd59ef02cff73e90ba793d423f1b1fd6a7cce5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodolphe=20Br=C3=A9ard?= Date: Tue, 26 Sep 2023 10:31:09 +0200 Subject: [PATCH] Automatically correct invalid values for preferences --- CHANGELOG.md | 3 +++ src/const.js | 8 ++++++++ src/main.js | 10 +++++++--- 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 src/const.js diff --git a/CHANGELOG.md b/CHANGELOG.md index fc2e3d0..d2864dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - The style has been entirely reworked using Bootstrap instead of Bulma - It is now impossible to include the separator in the dedicated name +## Fixed +- Invalid preferences are now automatically corrected + ## [0.3.0] - 2023-08-25 diff --git a/src/const.js b/src/const.js new file mode 100644 index 0000000..f8ed9a5 --- /dev/null +++ b/src/const.js @@ -0,0 +1,8 @@ +export const allowedColorModes = [ + 'light', + 'dark', +]; +export const allowedLocales = [ + 'en', + 'fr', +]; diff --git a/src/main.js b/src/main.js index 04f0db4..77afc05 100644 --- a/src/main.js +++ b/src/main.js @@ -1,5 +1,6 @@ import './assets/main.sass'; +import { allowedColorModes, allowedLocales } from './const'; import { createApp } from 'vue'; import { createI18n } from 'vue-i18n'; import { useStorage } from '@vueuse/core'; @@ -7,19 +8,22 @@ import App from './App.vue'; import router from './router'; import messages from '@intlify/unplugin-vue-i18n/messages'; -const setGlobalAttribute = (attrName, storageName, defaultValue) => { +const setGlobalAttribute = (attrName, storageName, defaultValue, allowedValues) => { const stored_value = useStorage(storageName, ''); if (!stored_value.value) { stored_value.value = defaultValue; } document.documentElement.setAttribute(attrName, stored_value.value); + if (!allowedValues.includes(stored_value.value)) { + stored_value.value = defaultValue; + } return { 'stored': stored_value, 'defaultValue': defaultValue, }; }; -const locale = setGlobalAttribute('lang', 'sake-locale', 'en'); -const colorMode = setGlobalAttribute('data-bs-theme', 'sake-color-mode', 'light'); +const locale = setGlobalAttribute('lang', 'sake-locale', 'en', allowedLocales); +const colorMode = setGlobalAttribute('data-bs-theme', 'sake-color-mode', 'light', allowedColorModes); const i18n = createI18n({ legacy: false,