188 lines
7.8 KiB
TypeScript
188 lines
7.8 KiB
TypeScript
// GoHR theme loader.
|
|
// Reads assets/gohr-theme/{theme-settings,palettes,fonts}.json and writes the chosen palette onto
|
|
// :root as --gh-* custom properties. Every themed rule (src/styles/theme/gohr-theme.scss) reads
|
|
// those variables, so changing theme-settings.json re-colours the whole application.
|
|
// The derived colours (hover, ring, menu shades, contrast text) follow GoHR Theme Studio exactly.
|
|
// It never throws: if a file is missing, the defaults compiled into gohr-theme.scss stay in place.
|
|
|
|
interface PaletteColors {
|
|
primaryDark: string; menuText: string; accent: string; white: string; titleText: string;
|
|
pageBackground: string; button: string; link: string; focus: string; tint: string;
|
|
selection: string; highlight: string; headingText: string; bodyText: string; border: string;
|
|
editIcon: string;
|
|
}
|
|
interface Palette { name: string; recommendedFont: string; colors: PaletteColors; }
|
|
interface StatusColors {
|
|
success: string; successHover: string; danger: string; dangerHover: string; warning: string;
|
|
info: string; muted: string; excel: string; pdf: string;
|
|
}
|
|
interface FontEntry { name: string; bangla: string | null; web: boolean; weights?: string; }
|
|
interface ThemeSettings {
|
|
palette: string; font: string; fontSize: number;
|
|
menu?: string; topbar?: string; background?: string;
|
|
}
|
|
|
|
const BASE = 'assets/gohr-theme/';
|
|
|
|
function url(file: string): string {
|
|
return new URL(BASE + file, document.baseURI).toString();
|
|
}
|
|
|
|
async function readJson<T>(file: string): Promise<T> {
|
|
const res = await fetch(url(file), {cache: 'no-store'});
|
|
if (!res.ok) {
|
|
throw new Error(file + ': HTTP ' + res.status);
|
|
}
|
|
return res.json();
|
|
}
|
|
|
|
/* ---------- colour helpers (same maths as the studio) ---------- */
|
|
function hex(h: string): string {
|
|
return (h || '').trim().toUpperCase();
|
|
}
|
|
|
|
function mix(a: string, b: string, t: number): string {
|
|
let o = '#';
|
|
for (let i = 1; i < 7; i += 2) {
|
|
const x = parseInt(a.substr(i, 2), 16), y = parseInt(b.substr(i, 2), 16);
|
|
o += ('0' + Math.round(x + (y - x) * t).toString(16)).slice(-2);
|
|
}
|
|
return o.toUpperCase();
|
|
}
|
|
|
|
function lum(h: string): number {
|
|
const r = [1, 3, 5].map(i => {
|
|
const c = parseInt(h.substr(i, 2), 16) / 255;
|
|
return c <= .03928 ? c / 12.92 : Math.pow((c + .055) / 1.055, 2.4);
|
|
});
|
|
return .2126 * r[0] + .7152 * r[1] + .0722 * r[2];
|
|
}
|
|
|
|
function ratio(a: string, b: string): number {
|
|
const x = lum(a), y = lum(b);
|
|
return (Math.max(x, y) + .05) / (Math.min(x, y) + .05);
|
|
}
|
|
|
|
function isDark(h: string): boolean {
|
|
return ratio(h, '#FFFFFF') >= ratio(h, '#111111');
|
|
}
|
|
|
|
function bestFg(bg: string, dark: string): string {
|
|
return ratio(bg, '#FFFFFF') >= ratio(bg, dark) ? '#FFFFFF' : dark;
|
|
}
|
|
|
|
function quoteFont(n: string): string {
|
|
return /^(ui-|system-)/.test(n) ? n : '\'' + n + '\'';
|
|
}
|
|
|
|
/** The fixed background choices of the studio's "Background" list. */
|
|
function backgroundHex(key: string | undefined, c: PaletteColors): string {
|
|
const named: { [k: string]: string } = {
|
|
'tint-primary': mix(c.primaryDark, '#FFFFFF', .93),
|
|
'tint-accent': mix(c.accent, '#FFFFFF', .93),
|
|
'tint-button': mix(c.button, '#FFFFFF', .95),
|
|
'tint-highlight': mix(c.highlight, '#FFFFFF', .88),
|
|
'cool-gray': '#F1F5F9', 'neutral-gray': '#F3F4F6', 'warm-gray': '#F5F5F4',
|
|
'soft-blue-gray': '#EEF2F6', 'darker-gray': '#E5E7EB'
|
|
};
|
|
if (!key || key === 'auto') {
|
|
return c.pageBackground;
|
|
}
|
|
return named[key] || (c as any)[key] || c.pageBackground;
|
|
}
|
|
|
|
function loadWebFont(f: FontEntry): void {
|
|
if (!f.web) {
|
|
return;
|
|
}
|
|
[f.name, f.bangla].filter(Boolean).forEach(n => {
|
|
const id = 'gh-font-' + n!.replace(/ /g, '-');
|
|
if (document.getElementById(id)) {
|
|
return;
|
|
}
|
|
const l = document.createElement('link');
|
|
l.id = id;
|
|
l.rel = 'stylesheet';
|
|
l.href = 'https://fonts.googleapis.com/css2?family=' + n!.replace(/ /g, '+') + ':wght@' +
|
|
(n === f.name && f.weights ? f.weights : '400;500;600;700') + '&display=swap';
|
|
document.head.appendChild(l);
|
|
});
|
|
}
|
|
|
|
/** Builds every --gh-* value from a palette + settings, as GoHR Theme Studio's apply() does. */
|
|
export function buildThemeVariables(p: Palette, fixed: StatusColors, fonts: FontEntry[],
|
|
s: ThemeSettings): { [k: string]: string } {
|
|
const c = {} as PaletteColors;
|
|
Object.keys(p.colors).forEach(k => (c as any)[k] = hex((p.colors as any)[k]));
|
|
const o: any = {
|
|
sb: c.primaryDark, st: c.menuText, am: c.accent, tb: c.white, tt: c.titleText, pb: c.pageBackground,
|
|
btn: c.button, lk: c.link, focus: c.focus, th: c.tint, sel: c.selection, bd: c.highlight,
|
|
ht: c.headingText, bt: c.bodyText, bo: c.border, edit: c.editIcon
|
|
};
|
|
o.btnh = mix(c.button, '#000000', .15);
|
|
o.ring = mix(c.focus, '#FFFFFF', .65);
|
|
o.bo2 = mix(c.border, c.bodyText, .2);
|
|
o.ok = fixed.success; o.okh = fixed.successHover; o.dn = fixed.danger; o.dnh = fixed.dangerHover;
|
|
o.warn = fixed.warning; o.info = fixed.info; o.muted = fixed.muted; o.xls = fixed.excel; o.pdf = fixed.pdf;
|
|
|
|
// Menu (sidebar)
|
|
const menuKey = s.menu && s.menu !== 'auto' ? s.menu : null;
|
|
const sb = menuKey ? ((c as any)[menuKey] || c.primaryDark) : c.primaryDark;
|
|
const sf = menuKey ? (isDark(sb) ? mix('#FFFFFF', sb, .14) : c.bodyText) : c.menuText;
|
|
const strong = isDark(sb) ? '#FFFFFF' : c.headingText;
|
|
let a = c.accent;
|
|
if (ratio(a, sb) < 1.8) {
|
|
a = isDark(sb) ? mix(c.accent, '#FFFFFF', .3) : mix(c.accent, '#000000', .25);
|
|
}
|
|
if (ratio(a, sb) < 1.5) {
|
|
a = mix(sb, strong, .22);
|
|
}
|
|
o.sideBg = sb; o.sideFg = sf; o.sideStrong = strong;
|
|
o.sideHover = mix(sb, strong, .1); o.sideLogo = mix(sb, strong, .07);
|
|
o.sideAct = a; o.sideActFg = bestFg(a, c.headingText);
|
|
|
|
// Topbar
|
|
const topKey = s.topbar && s.topbar !== 'auto' ? s.topbar : null;
|
|
const tb = topKey ? ((c as any)[topKey] || c.primaryDark) : c.primaryDark;
|
|
o.topBg = tb; o.topFg = bestFg(tb, c.headingText);
|
|
|
|
// Background, font, size
|
|
o.pb = backgroundHex(s.background, c);
|
|
const fontName = !s.font || s.font === 'auto' ? p.recommendedFont : s.font;
|
|
const f = fonts.find(x => x.name === fontName) || {name: fontName, bangla: null, web: false};
|
|
loadWebFont(f);
|
|
o.font = [quoteFont(f.name)].concat(f.bangla ? [quoteFont(f.bangla)] : [])
|
|
.concat(f.name.startsWith('ui-') ? ['system-ui'] : []).join(',') + ',\'Segoe UI\',Arial,sans-serif';
|
|
o.fz = (s.fontSize || 14) + 'px';
|
|
return o;
|
|
}
|
|
|
|
/**
|
|
* The current value of a palette variable, e.g. ghColor('--gh-btn'). For canvas and chart code,
|
|
* which cannot read CSS variables. gohr-theme.scss always declares a default, so it never returns ''.
|
|
*/
|
|
export function ghColor(name: string): string {
|
|
return getComputedStyle(document.documentElement).getPropertyValue(name).trim();
|
|
}
|
|
|
|
export async function applyGoHrTheme(): Promise<void> {
|
|
try {
|
|
const [settings, pal, fontFile] = await Promise.all([
|
|
readJson<ThemeSettings>('theme-settings.json'),
|
|
readJson<{ statusColors: StatusColors; palettes: Palette[] }>('palettes.json'),
|
|
readJson<{ fonts: FontEntry[] }>('fonts.json')
|
|
]);
|
|
const p = pal.palettes.find(x => x.name === settings.palette);
|
|
if (!p) {
|
|
console.warn('GoHR theme: palette "' + settings.palette + '" is not in palettes.json; keeping the default.');
|
|
return;
|
|
}
|
|
const vars = buildThemeVariables(p, pal.statusColors, fontFile.fonts, settings);
|
|
const root = document.documentElement.style;
|
|
Object.keys(vars).forEach(k => root.setProperty('--gh-' + k, vars[k]));
|
|
document.documentElement.setAttribute('data-gh-palette', p.name);
|
|
} catch (e) {
|
|
console.warn('GoHR theme: could not load theme files; keeping the default.', e);
|
|
}
|
|
}
|