feat: save and restore config from cookies
This commit is contained in:
@@ -11,6 +11,6 @@ TODO docs
|
|||||||
- [x] Configuration
|
- [x] Configuration
|
||||||
- [ ] Generate table
|
- [ ] Generate table
|
||||||
- [ ] Copy table option
|
- [ ] Copy table option
|
||||||
- [ ] Save last state in cookies
|
- [x] Save last state in cookies
|
||||||
- [ ] Other format: raw text
|
- [ ] Other format: raw text
|
||||||
- [ ] Other format: SVG
|
- [ ] Other format: SVG
|
||||||
|
|||||||
@@ -44,6 +44,27 @@ const utils = {
|
|||||||
}
|
}
|
||||||
return output;
|
return output;
|
||||||
},
|
},
|
||||||
|
setCookie(cname, cvalue, exdays) {
|
||||||
|
const date = new Date();
|
||||||
|
date.setTime(date.getTime() + exdays * 24 * 60 * 60 * 1000);
|
||||||
|
const expires = `expires=${date.toUTCString()}`;
|
||||||
|
document.cookie = `${cname}=${cvalue}; path=/; ${expires}`;
|
||||||
|
},
|
||||||
|
getCookie(cname, defaultValue) {
|
||||||
|
const name = `${cname}=`;
|
||||||
|
const decodedCookie = decodeURIComponent(document.cookie);
|
||||||
|
const ca = decodedCookie.split(";");
|
||||||
|
for (let index = 0; index < ca.length; index += 1) {
|
||||||
|
let cookie = ca[index];
|
||||||
|
while (cookie.charAt(0) === " ") {
|
||||||
|
cookie = cookie.substring(1);
|
||||||
|
}
|
||||||
|
if (cookie.indexOf(name) === 0) {
|
||||||
|
return cookie.substring(name.length, cookie.length);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return defaultValue;
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const VEGETABLES = {
|
const VEGETABLES = {
|
||||||
@@ -105,6 +126,12 @@ const app = createApp({
|
|||||||
vegetable() {
|
vegetable() {
|
||||||
document.title = `${this.vegetable} Légume`;
|
document.title = `${this.vegetable} Légume`;
|
||||||
},
|
},
|
||||||
|
config: {
|
||||||
|
handler() {
|
||||||
|
this.saveConfig();
|
||||||
|
},
|
||||||
|
deep: true,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
updated() {
|
updated() {
|
||||||
utils.updateIcons();
|
utils.updateIcons();
|
||||||
@@ -118,6 +145,7 @@ const app = createApp({
|
|||||||
.map((key) => `${key} ${VEGETABLES[key]}`)
|
.map((key) => `${key} ${VEGETABLES[key]}`)
|
||||||
.slice(0, 6)
|
.slice(0, 6)
|
||||||
.join("\n");
|
.join("\n");
|
||||||
|
this.loadConfig();
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
getTime(minutes) {
|
getTime(minutes) {
|
||||||
@@ -131,6 +159,24 @@ const app = createApp({
|
|||||||
newSeed() {
|
newSeed() {
|
||||||
this.config.seed = utils.randomSeed();
|
this.config.seed = utils.randomSeed();
|
||||||
},
|
},
|
||||||
|
saveConfig() {
|
||||||
|
utils.setCookie("legume-config", JSON.stringify(this.config));
|
||||||
|
},
|
||||||
|
loadConfig() {
|
||||||
|
const rawCookie = utils.getCookie("legume-config", null);
|
||||||
|
if (rawCookie) {
|
||||||
|
try {
|
||||||
|
const parsedConfig = JSON.parse(rawCookie);
|
||||||
|
Object.keys(parsedConfig).forEach((key) => {
|
||||||
|
if (Object.hasOwn(this.config, key)) {
|
||||||
|
this.config[key] = parsedConfig[key];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch {
|
||||||
|
/* Empty */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user