feat: item color map, fix regex matching, fix sort order, fix resize handle

This commit is contained in:
Sebastian Seedorf
2026-06-04 11:04:58 +02:00
parent b9377daa04
commit 955b0a890d
7 changed files with 562 additions and 41 deletions

35
web/lib/colors.ts Normal file
View File

@@ -0,0 +1,35 @@
export type ColorMap = Map<string, string>;
declare global { var __colorMapCache: ColorMap | undefined; }
export function parseColorCsv(text: string): ColorMap {
const map: ColorMap = new Map();
const lines = text.split(/\r?\n/).slice(1);
for (const line of lines) {
if (!line.trim()) continue;
const [key, color] = line.split(',');
if (key && color) map.set(key.trim(), color.trim());
}
return map;
}
export async function getColorMap(): Promise<ColorMap> {
if (globalThis.__colorMapCache) return globalThis.__colorMapCache;
try {
const res = await fetch('/factorio_item_colors.csv');
globalThis.__colorMapCache = res.ok ? parseColorCsv(await res.text()) : new Map();
} catch {
globalThis.__colorMapCache = new Map();
}
return globalThis.__colorMapCache;
}
function djb2(s: string): number {
let h = 5381;
for (let i = 0; i < s.length; i++) h = (h * 33) ^ s.charCodeAt(i);
return h >>> 0;
}
export function getItemColor(key: string, colorMap: ColorMap): string {
return colorMap.get(key) ?? `hsl(${djb2(key) % 360},70%,60%)`;
}