Skip to content

Commit f96847d

Browse files
committed
Update theme-palette.js
1 parent 9d5041e commit f96847d

1 file changed

Lines changed: 16 additions & 49 deletions

File tree

docs/assets/javascripts/theme-palette.js

Lines changed: 16 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
(() => {
2-
const STORAGE_KEY = "qrcraft_palette_v1";
32
const STYLE_TAG_ID = "qrcraft-theme-palette-style";
43

54
const presets = [
@@ -23,25 +22,6 @@
2322
},
2423
];
2524

26-
function safeParse(json) {
27-
try {
28-
return JSON.parse(json);
29-
} catch {
30-
return null;
31-
}
32-
}
33-
34-
function loadConfig() {
35-
const raw = localStorage.getItem(STORAGE_KEY);
36-
const data = raw ? safeParse(raw) : null;
37-
if (!data || typeof data !== "object") return null;
38-
return data;
39-
}
40-
41-
function saveConfig(cfg) {
42-
localStorage.setItem(STORAGE_KEY, JSON.stringify(cfg));
43-
}
44-
4525
function isHexColor(s) {
4626
return typeof s === "string" && /^#[0-9a-fA-F]{6}$/.test(s);
4727
}
@@ -134,12 +114,6 @@
134114
style.textContent = lines.join("\n");
135115
}
136116

137-
function applyFromStorage() {
138-
const cfg = loadConfig();
139-
if (!cfg) return;
140-
applyColors(cfg.primary, cfg.accent);
141-
}
142-
143117
function createHeaderButton() {
144118
const btn = document.createElement("button");
145119
btn.type = "button";
@@ -182,6 +156,15 @@
182156
}
183157

184158
function createDialog() {
159+
function syncInputsToComputed(primaryInput, accentInput) {
160+
const p = getCssVar("--md-primary-fg-color");
161+
const a = getCssVar("--md-accent-fg-color");
162+
const pHex = rgbStringToHex(p) || (isHexColor(p) ? p : null);
163+
const aHex = rgbStringToHex(a) || (isHexColor(a) ? a : null);
164+
if (pHex) primaryInput.value = pHex;
165+
if (aHex) accentInput.value = aHex;
166+
}
167+
185168
const overlay = document.createElement("div");
186169
overlay.className = "theme-palette-overlay";
187170
overlay.hidden = true;
@@ -280,16 +263,7 @@
280263
overlay.appendChild(panel);
281264

282265
function open() {
283-
const cfg = loadConfig();
284-
if (cfg?.primary && isHexColor(cfg.primary)) primaryInput.value = cfg.primary;
285-
if (cfg?.accent && isHexColor(cfg.accent)) accentInput.value = cfg.accent;
286-
if (!cfg) {
287-
const p = getCssVar("--md-primary-fg-color");
288-
const pHex = rgbStringToHex(p) || (isHexColor(p) ? p : null);
289-
if (pHex) primaryInput.value = pHex;
290-
const aHex = pHex ? lightenHex(pHex, 0.35) : null;
291-
if (aHex) accentInput.value = aHex;
292-
}
266+
syncInputsToComputed(primaryInput, accentInput);
293267
overlay.hidden = false;
294268
overlay.style.removeProperty("display");
295269
document.documentElement.classList.add("theme-palette-open");
@@ -301,9 +275,8 @@
301275
document.documentElement.classList.remove("theme-palette-open");
302276
}
303277

304-
function applyAndSave(primary, accent) {
278+
function applyNow(primary, accent) {
305279
applyColors(primary, accent);
306-
saveConfig({ primary, accent });
307280
}
308281

309282
panel.addEventListener("click", (e) => {
@@ -330,30 +303,24 @@
330303
const primary = t.dataset.primary;
331304
const accent = t.dataset.accent;
332305
if (!isHexColor(primary) || !isHexColor(accent)) return;
333-
applyAndSave(primary, accent);
306+
applyNow(primary, accent);
334307
});
335308

336309
applyBtn.addEventListener("click", () => {
337-
applyAndSave(primaryInput.value, accentInput.value);
310+
applyNow(primaryInput.value, accentInput.value);
338311
});
339312

340313
resetBtn.addEventListener("click", () => {
341-
localStorage.removeItem(STORAGE_KEY);
342314
clearStyleTag();
343-
const p = getCssVar("--md-primary-fg-color");
344-
const a = getCssVar("--md-accent-fg-color");
345-
const pHex = rgbStringToHex(p) || (isHexColor(p) ? p : null);
346-
const aHex = rgbStringToHex(a) || (isHexColor(a) ? a : null);
347-
if (pHex) primaryInput.value = pHex;
348-
if (aHex) accentInput.value = aHex;
315+
requestAnimationFrame(() => {
316+
syncInputsToComputed(primaryInput, accentInput);
317+
});
349318
});
350319

351320
return { overlay, open, close: closePanel };
352321
}
353322

354323
function mount() {
355-
applyFromStorage();
356-
357324
if (document.querySelector(".theme-palette-overlay")) return;
358325
const { overlay, open } = createDialog();
359326
document.body.appendChild(overlay);

0 commit comments

Comments
 (0)