-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy paththemes.ts
More file actions
74 lines (64 loc) · 2.79 KB
/
themes.ts
File metadata and controls
74 lines (64 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { Theme, BadgeTheme } from '../types.js';
import { baseThemes } from './themes/base.js';
import { graphThemes } from './themes/graph.js';
import { badgeThemes } from './themes/badge.js';
const defaultFontName = 'Orbitron';
const defaultFontFamily = `'${defaultFontName}', 'Ubuntu', 'sans-serif'`;
const defaultFontUrl = '/fonts/orbitron.woff2';
export const themes: { [key: string]: Theme } = { ...baseThemes, ...graphThemes };
export { badgeThemes };
/**
* Normalises a theme name for fuzzy lookup:
* lower-case + collapse spaces / underscores / hyphens to a single hyphen.
* e.g. "Tokyo Night", "tokyoNight", "tokyo_night" all → "tokyo-night"
*/
function normalizeKey(name: string): string {
return name.toLowerCase().replace(/[\s_]+/g, '-');
}
/** Pre-built map: normalised key → original themes key */
const themeIndex: Map<string, string> = new Map(
Object.keys(themes).map(k => [normalizeKey(k), k])
);
/** Pre-built map: normalised key → original badgeThemes key */
const badgeThemeIndex: Map<string, string> = new Map(
Object.keys(badgeThemes).map(k => [normalizeKey(k), k])
);
/** Resolve a user-supplied theme name to the actual themes key, or 'default'. */
function resolveThemeName(name: string): string {
// Exact match first (fast path)
if (themes[name]) return name;
// Normalised match (case / separator insensitive)
const resolved = themeIndex.get(normalizeKey(name));
return resolved ?? 'default';
}
/** Resolve a user-supplied badge theme name to the actual badgeThemes key, or 'default'. */
function resolveBadgeThemeName(name: string): string {
// Exact match first (fast path)
if (badgeThemes[name]) return name;
// Normalised match (case / separator insensitive)
const resolved = badgeThemeIndex.get(normalizeKey(name));
return resolved ?? 'default';
}
export function getTheme(themeName: string = 'default', customColors?: {
bgColor?: string;
borderColor?: string;
textColor?: string;
titleColor?: string;
}): Theme {
const theme = themes[resolveThemeName(themeName)];
return {
...themes.default,
...theme,
fontName: theme.fontName ?? defaultFontName,
fontFamily: theme.fontFamily ?? defaultFontFamily,
fontUrl: theme.fontUrl ?? defaultFontUrl,
// Override with custom colors if provided
...(customColors?.bgColor && { bgColor: customColors.bgColor }),
...(customColors?.borderColor && { borderColor: customColors.borderColor }),
...(customColors?.textColor && { textColor: customColors.textColor }),
...(customColors?.titleColor && { titleColor: customColors.titleColor, iconColor: customColors.titleColor }),
};
}
export function getBadgeTheme(themeName: string = 'default'): BadgeTheme {
return badgeThemes[resolveBadgeThemeName(themeName)];
}