{"version":3,"sources":["../../../templates/js/src/theme_handler.ts"],"sourcesContent":["import { Themeable, ThemeCallback } from \"./types\";\n\nvar isDark = false;\nvar key: number = 0;\nconst callbacks: { [key: number]: ThemeCallback } = {};\n\n// const ts = document.getElementById(\"theme-switch\");\n\nfunction SetTheme(dark: boolean) {\n isDark = dark;\n if (dark) {\n document.documentElement.classList.add(\"dark\");\n localStorage.theme = \"dark\";\n } else {\n document.documentElement.classList.remove(\"dark\");\n localStorage.theme = \"light\";\n }\n for (const callback of Object.values(callbacks)) {\n console.info(\"SetTheme callback \", callback);\n callback(dark);\n }\n}\nfunction SetDarkTheme() {\n SetTheme(true);\n}\n\nfunction SetLightTheme() {\n SetTheme(false);\n}\n\nfunction ToggleTheme() {\n if (localStorage.theme === \"dark\") {\n SetLightTheme();\n } else {\n SetDarkTheme();\n }\n}\n\nfunction RegisterCallback(callback: ThemeCallback): number {\n callback(isDark);\n const k = key++;\n callbacks[k] = callback;\n return k;\n}\nfunction UnRegisterCallback(k: number): void {\n delete callbacks[k];\n}\n\nfunction RegisterThemeable(t: Themeable): number {\n const callback: ThemeCallback = (dark) => t.SetTheme(dark);\n return RegisterCallback(callback);\n}\nfunction UnRegisterThemeable(k: number) {\n UnRegisterCallback(k);\n}\n\nfunction InitTheme() {\n // On page load or when changing themes, best to add inline in `head` to avoid FOUC\n if (\n localStorage.theme === \"dark\" ||\n (!(\"theme\" in localStorage) &&\n window.matchMedia(\"(prefers-color-scheme: dark)\").matches)\n ) {\n SetDarkTheme();\n } else {\n SetLightTheme();\n }\n}\n\ndeclare global {\n interface Window {\n theme: {\n Init: () => void;\n SetTheme: (dark: boolean) => void;\n SetDarkTheme: () => void;\n SetLightTheme: () => void;\n ToggleTheme: () => void;\n RegisterThemeable: (t: Themeable) => number;\n RegisterCallback: (callback: ThemeCallback) => number;\n UnRegisterThemeable: (k: number) => void;\n UnRegisterCallback: (k: number) => void;\n };\n }\n}\n\nwindow.theme = {\n Init: InitTheme,\n SetTheme: SetTheme,\n SetDarkTheme: SetDarkTheme,\n SetLightTheme: SetLightTheme,\n ToggleTheme: ToggleTheme,\n RegisterThemeable: RegisterThemeable,\n UnRegisterThemeable: UnRegisterThemeable,\n RegisterCallback: RegisterCallback,\n UnRegisterCallback: UnRegisterCallback,\n};\n"],"mappings":"mBAEA,IAAIA,EAAS,GACTC,EAAc,EACZC,EAA8C,CAAC,EAIrD,SAASC,EAASC,EAAe,CAC7BJ,EAASI,EACLA,GACA,SAAS,gBAAgB,UAAU,IAAI,MAAM,EAC7C,aAAa,MAAQ,SAErB,SAAS,gBAAgB,UAAU,OAAO,MAAM,EAChD,aAAa,MAAQ,SAEzB,QAAWC,KAAY,OAAO,OAAOH,CAAS,EAC1C,QAAQ,KAAK,qBAAsBG,CAAQ,EAC3CA,EAASD,CAAI,CAErB,CACA,SAASE,GAAe,CACpBH,EAAS,EAAI,CACjB,CAEA,SAASI,GAAgB,CACrBJ,EAAS,EAAK,CAClB,CAEA,SAASK,GAAc,CACf,aAAa,QAAU,OACvBD,EAAc,EAEdD,EAAa,CAErB,CAEA,SAASG,EAAiBJ,EAAiC,CACvDA,EAASL,CAAM,EACf,IAAMU,EAAIT,IACV,OAAAC,EAAUQ,CAAC,EAAIL,EACRK,CACX,CACA,SAASC,EAAmBD,EAAiB,CACzC,OAAOR,EAAUQ,CAAC,CACtB,CAEA,SAASE,EAAkBC,EAAsB,CAE7C,OAAOJ,EAD0BL,GAASS,EAAE,SAAST,CAAI,CACzB,CACpC,CACA,SAASU,EAAoBJ,EAAW,CACpCC,EAAmBD,CAAC,CACxB,CAEA,SAASK,GAAY,CAGb,aAAa,QAAU,QACtB,EAAE,UAAW,eACV,OAAO,WAAW,8BAA8B,EAAE,QAEtDT,EAAa,EAEbC,EAAc,CAEtB,CAkBA,OAAO,MAAQ,CACX,KAAMQ,EACN,SAAUZ,EACV,aAAcG,EACd,cAAeC,EACf,YAAaC,EACb,kBAAmBI,EACnB,oBAAqBE,EACrB,iBAAkBL,EAClB,mBAAoBE,CACxB","names":["isDark","key","callbacks","SetTheme","dark","callback","SetDarkTheme","SetLightTheme","ToggleTheme","RegisterCallback","k","UnRegisterCallback","RegisterThemeable","t","UnRegisterThemeable","InitTheme"]}