From 429dc2d5375abf8ca9c3861bdc4bdff52a31b0e4 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Sat, 10 Sep 2022 00:56:25 +0200 Subject: overlay rework + settings --- client-web/source/preferences/mod.ts | 87 ++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 client-web/source/preferences/mod.ts (limited to 'client-web/source/preferences/mod.ts') diff --git a/client-web/source/preferences/mod.ts b/client-web/source/preferences/mod.ts new file mode 100644 index 0000000..5b33924 --- /dev/null +++ b/client-web/source/preferences/mod.ts @@ -0,0 +1,87 @@ +import { PREF_DECLS } from "./decl.ts"; + + +export interface PrefDecl { + default?: T, + type: T, + description?: string, + possible_values?: T[] + optional?: boolean, +} + +type Type = "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"; +type TypeMapper = { "string": string, "number": number, "boolean": boolean } + +type PrefMap = { [Key in keyof T]: T[Key]["type"] } +type Optional = { [Key in keyof T]?: T[Key] } +export const { prefs: PREFS, explicit: PREFS_EXPLICIT } = register_prefs(PREF_DECLS) +export const on_pref_change_handlers: ((key: keyof typeof PREFS) => void)[] = [] + +export function register_prefs>>(ds: T): { prefs: PrefMap, explicit: Optional> } { + const prefs: PrefMap = {} as PrefMap + const explicit: Optional> = {} + for (const key in ds) { + const d = ds[key]; + const type = typeof d.type; + let value = get_param(type, key) + if (value !== undefined) explicit[key] = value + value ??= d.default; + if (d.possible_values) if (!d.possible_values.includes(value)) value = d.default + prefs[key] = value + } + return { prefs, explicit } +} + +export function change_pref(key: T, value: typeof PREFS[T]) { + PREFS[key] = value + if ((PREF_DECLS as Record>)[key].default != value) + PREFS_EXPLICIT[key] = value + else delete PREFS_EXPLICIT[key] + window.location.hash = "#" + generate_section() +} +export function generate_section(): string { + const section = [] + for (const key in PREFS_EXPLICIT) { + section.push(encodeURIComponent(key) + "=" + encodeURIComponent(param_to_string( + PREFS_EXPLICIT[key as unknown as keyof typeof PREFS_EXPLICIT] + ))) + } + return load_params().rname + "?" + section.join("&") +} + +export function load_params(): { raw_params: { [key: string]: string }, rname: string } { + const raw_params: Record = {} + const [rname, param_str] = window.location.hash.substring(1).split("?") + if (!param_str) return { rname, raw_params: {} } + for (const kv of param_str.split("&")) { + const [key, value] = kv.split("=") + if (key == "prototype") continue + raw_params[decodeURIComponent(key)] = decodeURIComponent(value) + } + return { raw_params, rname } +} + +function param_to_string(p: T): string { + if (typeof p == "string") return p + else if (typeof p == "boolean") return JSON.stringify(p) + else if (typeof p == "number") return JSON.stringify(p) + throw new Error("impossible"); +} + +function get_param(ty: string, key: string): T | undefined { + const v = load_params().raw_params[key] + if (v == undefined) return undefined + if (ty == "string") return v as unknown as T + else if (ty == "number") { + const n = parseInt(v) + if (!Number.isNaN(n)) return n as unknown as T + console.warn("invalid number parameter"); + } else if (ty == "boolean") { + if (v == "0" || v == "false" || v == "no") return false as unknown as T + if (v == "1" || v == "true" || v == "yes") return true as unknown as T + console.warn("invalid boolean parameter"); + } else { + throw new Error("invalid param type"); + } + return undefined +} -- cgit v1.2.3-70-g09d2