source: Klonkt/src/services/ThemeService.js@ 3d7312a

main
Last change on this file since 3d7312a was 834bcc3, checked in by Robin Genis <roboburr@…>, 3 months ago

i18n: translate Dutch code comments to English across src/

Comments in routes/services/views/config/middleware/assets translated to
English for the public repo. A few dev-facing throw/console message strings
were Englished too. No user-facing UI strings or i18n dictionary values changed
(src/services/i18n.js untouched). Logic unchanged.

Co-Authored-By: Claude <noreply@…>

  • Property mode set to 100644
File size: 7.5 KB
RevLine 
[7bc636b]1/**
2 * ThemeService — Palette and theme management
3 *
4 * 8 Built-in Palettes (from v9):
5 * - Sage (default, warm crème)
6 * - Paper (minimalist white)
7 * - Ocean (cool blues)
8 * - Forest (greens)
9 * - Stone (grays)
10 * - Midnight (dark blue)
11 * - Sunset (warm oranges)
12 * - Cream (light beige)
13 *
14 * Dark/Light mode toggle stored per user
15 */
16
17class ThemeService {
[834bcc3]18 // Paper/ink values map 1-to-1 from the [data-palette] CSS in style.css (= what
19 // is ACTUALLY applied); the accent dot is a representative color per palette.
[7bc636b]20 static PALETTES = {
[834bcc3]21 // Brand default — matches klonkt.com (dark blue + gold).
[dd7e2a2]22 klonkt: {
23 name: 'Klonkt',
24 light: { paper: '#f3f1ea', ink: '#11141c', accent: '#c98a2a' },
25 dark: { paper: '#0b0d12', ink: '#f3f1ea', accent: '#e8b04b' }
26 },
[7bc636b]27 sage: {
28 name: 'Sage',
29 light: { paper: '#faf8f3', ink: '#1a1a1a', accent: '#c2410c' },
30 dark: { paper: '#1c1a17', ink: '#f4ede0', accent: '#c2410c' }
31 },
32 paper: {
33 name: 'Paper',
34 light: { paper: '#ffffff', ink: '#09090b', accent: '#000000' },
[999aad2]35 dark: { paper: '#0a0a0a', ink: '#fafafa', accent: '#ffffff' }
[7bc636b]36 },
37 forest: {
38 name: 'Forest',
[999aad2]39 light: { paper: '#f2f6ed', ink: '#1a2e15', accent: '#4d7c2a' },
40 dark: { paper: '#0d1f12', ink: '#dcf2d0', accent: '#6fae3f' }
[7bc636b]41 },
42 stone: {
43 name: 'Stone',
[999aad2]44 light: { paper: '#f5f0e8', ink: '#2b2218', accent: '#8a6a45' },
45 dark: { paper: '#1a130b', ink: '#f5e9d5', accent: '#b89366' }
[7bc636b]46 },
47 midnight: {
48 name: 'Midnight',
[999aad2]49 light: { paper: '#f3f1f8', ink: '#1e1a2e', accent: '#7c5cbf' },
50 dark: { paper: '#0f0a1f', ink: '#e9def7', accent: '#9d88e0' }
[7bc636b]51 },
52 sunset: {
53 name: 'Sunset',
[999aad2]54 light: { paper: '#fdf4f3', ink: '#2e1618', accent: '#d6477f' },
55 dark: { paper: '#1f0a14', ink: '#fce7f3', accent: '#f06fa3' }
[7bc636b]56 },
57 cream: {
58 name: 'Cream',
[999aad2]59 light: { paper: '#fefaf0', ink: '#2a1f0f', accent: '#d97706' },
60 dark: { paper: '#1a1208', ink: '#fef3d6', accent: '#f0a93a' }
61 },
62 rose: {
63 name: 'Rose',
64 light: { paper: '#fdf2f4', ink: '#2e1419', accent: '#e11d6b' },
65 dark: { paper: '#1f0a0f', ink: '#fce4ea', accent: '#f06b9a' }
66 },
[834bcc3]67 // key stays 'mint' (DB-safe), but recolored to warm Terracotta — less green.
[999aad2]68 mint: {
[0251bae]69 name: 'Terracotta',
70 light: { paper: '#faf2ee', ink: '#2e1a12', accent: '#c2410c' },
71 dark: { paper: '#1f120c', ink: '#f7e6da', accent: '#e8783f' }
[999aad2]72 },
73 lilac: {
74 name: 'Lilac',
75 light: { paper: '#faf4fb', ink: '#2a1830', accent: '#a855f7' },
76 dark: { paper: '#170a1c', ink: '#f3e2f7', accent: '#c084fc' }
[7bc636b]77 }
78 };
79
80 /**
81 * Curated accent palette — admins pick one of these instead of a free-form
82 * hex picker. Keeps the brand consistent and avoids unreadable combinations.
83 * Each color works against both light and dark themes.
84 */
[834bcc3]85 // Balanced across the color wheel — fewer greens/blues (4 of 12),
86 // more warm + purple/pink variation. All readable on both light and dark.
[7bc636b]87 static ACCENTS = [
[dd7e2a2]88 { key: 'klonkt', name: 'Klonkt-geel', color: '#e8b04b' },
[0251bae]89 { key: 'red', name: 'Rood', color: '#dc2626' },
90 { key: 'orange', name: 'Oranje', color: '#ea580c' },
91 { key: 'amber', name: 'Amber', color: '#d97706' },
92 { key: 'gold', name: 'Goud', color: '#ca8a04' },
93 { key: 'forest', name: 'Groen', color: '#16a34a' },
[999aad2]94 { key: 'teal', name: 'Turquoise', color: '#0d9488' },
[0251bae]95 { key: 'ocean', name: 'Blauw', color: '#2563eb' },
96 { key: 'indigo', name: 'Indigo', color: '#4f46e5' },
97 { key: 'violet', name: 'Violet', color: '#7c3aed' },
98 { key: 'plum', name: 'Magenta', color: '#c026d3' },
99 { key: 'pink', name: 'Roze', color: '#db2777' },
100 { key: 'brown', name: 'Bruin', color: '#9a3412' },
[7bc636b]101 ];
102
103 static listAccents() {
104 return this.ACCENTS;
105 }
106
107 /**
108 * Validate an accent color. Returns the canonical hex if it's in the curated
109 * set (case-insensitive match), or null if it's not. Server-side validation
110 * uses this so we never persist arbitrary hex from the form.
111 */
112 static validateAccent(hex) {
113 if (!hex || typeof hex !== 'string') return null;
114 const target = hex.trim().toLowerCase();
115 const found = this.ACCENTS.find(a => a.color.toLowerCase() === target);
116 return found ? found.color : null;
117 }
118
119 /**
120 * Get palette data
121 */
122 static getPalette(paletteKey) {
[dd7e2a2]123 return this.PALETTES[paletteKey] || this.PALETTES.klonkt;
[7bc636b]124 }
125
126 /**
127 * Get all available palettes (with full color data so a picker UI can
128 * render true previews of paper/ink/accent for both light and dark).
129 */
130 static listPalettes() {
131 return Object.entries(this.PALETTES).map(([key, data]) => ({
132 key,
133 name: data.name,
134 light: data.light,
135 dark: data.dark,
136 }));
137 }
138
139 /**
140 * Generate CSS variables for palette + theme
141 */
142 static generateCSSVars(paletteKey, theme = 'dark', accentColor = null) {
143 const palette = this.getPalette(paletteKey);
144 const colors = theme === 'dark' ? palette.dark : palette.light;
145 const accent = accentColor || colors.accent;
146
147 return `
148 :root {
149 --palette: ${paletteKey};
150 --theme: ${theme};
151 --paper: ${colors.paper};
152 --ink: ${colors.ink};
153 --accent: ${accent};
154 }
155 `.trim();
156 }
157
158 /**
159 * Update user's theme preference
160 */
161 static updateUserTheme(db, userId, theme, palette) {
162 if (!['dark', 'light'].includes(theme)) {
163 throw new Error('Invalid theme. Must be "dark" or "light"');
164 }
165 if (!this.PALETTES[palette]) {
166 throw new Error('Invalid palette');
167 }
168
169 db.prepare(`
170 UPDATE users SET theme = ?, palette = ? WHERE id = ?
171 `).run(theme, palette, userId);
172 }
173
174 /**
175 * Update site's palette + accent
176 */
177 static updateSitePalette(db, siteId, paletteKey, accentColor) {
178 if (!this.PALETTES[paletteKey]) {
179 throw new Error('Invalid palette');
180 }
181 if (!/^#[0-9a-f]{6}$/i.test(accentColor)) {
182 throw new Error('Invalid accent color. Must be hex #RRGGBB');
183 }
184
185 db.prepare(`
186 UPDATE sites SET palette = ?, accent = ? WHERE id = ?
187 `).run(paletteKey, accentColor, siteId);
188 }
189
190 /**
191 * Generate full HTML theme meta tags
192 */
193 static generateThemeMeta(userTheme, userPalette, siteTheme, siteAccent) {
194 const theme = userTheme || siteTheme || 'dark';
[dd7e2a2]195 const palette = userPalette || 'klonkt';
196 const accent = siteAccent || '#e8b04b';
[7bc636b]197 const paletteData = this.getPalette(palette);
198 const colors = theme === 'dark' ? paletteData.dark : paletteData.light;
199
200 return {
201 colorScheme: 'dark light',
202 themeColor: accent,
203 appleMobileWebAppStatusBarStyle: 'black-translucent',
204 cssVars: this.generateCSSVars(palette, theme, accent),
205 inline: `
206 <style>
207 html {
208 color-scheme: ${theme === 'dark' ? 'dark light' : 'light dark'};
209 }
210 :root {
211 --paper: ${colors.paper};
212 --ink: ${colors.ink};
213 --accent: ${accent};
214 }
215 </style>
216 <script>
217 // Apply theme ASAP (before paint) to avoid flash
218 (function() {
219 try {
220 const t = localStorage.getItem('pcms-theme') || '${theme}';
221 const p = localStorage.getItem('pcms-palette') || '${palette}';
222 document.documentElement.setAttribute('data-theme', t);
223 document.documentElement.setAttribute('data-palette', p);
224 } catch(e) {}
225 })();
226 </script>
227 `.trim()
228 };
229 }
230}
231
232export default ThemeService;
Note: See TracBrowser for help on using the repository browser.