Bugfix: loading theme options
This commit is contained in:
parent
8d372d30fb
commit
e5b08e772a
1 changed files with 39 additions and 17 deletions
|
@ -64,15 +64,16 @@ import TextBox from "./TextBox.astro";
|
|||
<script>
|
||||
var lessLoaded = false
|
||||
|
||||
class ThemingOption {
|
||||
class ThemingOption<T> {
|
||||
input: HTMLInputElement | undefined
|
||||
value: T;
|
||||
onchange:
|
||||
(opt: ThemingOption, upd: boolean) => any =
|
||||
(opt: ThemingOption<T>, upd: boolean) => any =
|
||||
(_opt, _upd) => {}
|
||||
|
||||
constructor (
|
||||
inputId: string,
|
||||
onchange: (opt: ThemingOption, upd: boolean) => any
|
||||
onchange: (opt: ThemingOption<T>, upd: boolean) => any
|
||||
) {
|
||||
this.input = document.getElementById(inputId) as HTMLInputElement
|
||||
this.onchange = onchange
|
||||
|
@ -103,6 +104,15 @@ import TextBox from "./TextBox.astro";
|
|||
}
|
||||
}
|
||||
|
||||
/** Converts any object to number.
|
||||
* Returns the default value passed to `def` parameter
|
||||
* if `Number(val)` returns `NaN`.
|
||||
*/
|
||||
const toNum = (val: any, def: number) => {
|
||||
const res = Number(val)
|
||||
return isNaN(res) ? def : res
|
||||
}
|
||||
|
||||
const colorRegex = /#?([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})/
|
||||
/** Parses color-like string and
|
||||
* returns the color in the general format.
|
||||
|
@ -114,11 +124,12 @@ import TextBox from "./TextBox.astro";
|
|||
return (val.match(colorRegex) || [])[1]
|
||||
}
|
||||
|
||||
new ThemingOption(
|
||||
new ThemingOption<boolean>(
|
||||
'dark',
|
||||
(opt, upd) => {
|
||||
if (upd) {
|
||||
const state = opt.input.checked
|
||||
opt.value = state
|
||||
localStorage.setItem('dc09_dark', state ? '1' : '0')
|
||||
return opt.onchange(opt, false)
|
||||
}
|
||||
|
@ -132,70 +143,81 @@ import TextBox from "./TextBox.astro";
|
|||
media.matches
|
||||
)
|
||||
)
|
||||
opt.value = state
|
||||
opt.input.checked = state
|
||||
document.body.dataset.dark = state ? '1' : '0'
|
||||
}
|
||||
)
|
||||
|
||||
const purebgOpt = new ThemingOption(
|
||||
const purebgOpt = new ThemingOption<boolean>(
|
||||
'purebg', // pure background
|
||||
(opt, upd) => {
|
||||
if (upd) {
|
||||
const state = opt.input.checked
|
||||
opt.value = state
|
||||
localStorage.setItem('dc09_purebg', state ? '1' : '0')
|
||||
reloadTheme()
|
||||
return opt.onchange(opt, false)
|
||||
}
|
||||
|
||||
opt.input.checked = toBool(localStorage.getItem('dc09_purebg'))
|
||||
const value = toBool(localStorage.getItem('dc09_purebg'))
|
||||
opt.value = value
|
||||
opt.input.checked = value
|
||||
}
|
||||
)
|
||||
|
||||
const accentOpt = new ThemingOption(
|
||||
const accentOpt = new ThemingOption<string>(
|
||||
'accent',
|
||||
(opt, upd) => {
|
||||
if (upd) {
|
||||
if (!opt.input.validity.valid) return
|
||||
const color = toColor(opt.input.value)
|
||||
if (!color) return
|
||||
opt.value = color
|
||||
localStorage.setItem('dc09_accent', color)
|
||||
reloadTheme()
|
||||
return
|
||||
}
|
||||
|
||||
const color = localStorage.getItem('dc09_accent') || '6570d6'
|
||||
const color = toColor(localStorage.getItem('dc09_accent')) || '6570d6'
|
||||
opt.value = color
|
||||
opt.input.value = color
|
||||
}
|
||||
)
|
||||
|
||||
const bdrsOpt = new ThemingOption(
|
||||
const bdrsOpt = new ThemingOption<number>(
|
||||
'bdrs', // border-radius, as in Emmet
|
||||
(opt, upd) => {
|
||||
if (upd) {
|
||||
const value = opt.input.value
|
||||
localStorage.setItem('dc09_bdrs', value)
|
||||
const valueStr = opt.input.value
|
||||
const value = Number(opt.input.value)
|
||||
if (isNaN(value)) return;
|
||||
opt.value = value
|
||||
localStorage.setItem('dc09_bdrs', valueStr)
|
||||
reloadTheme()
|
||||
return
|
||||
}
|
||||
|
||||
let value = Number(localStorage.getItem('dc09_bdrs'))
|
||||
value = isNaN(value) ? 2 : value
|
||||
const value = toNum(localStorage.getItem('dc09_bdrs'), 2)
|
||||
opt.value = value
|
||||
opt.input.value = String(value)
|
||||
opt.input.dispatchEvent(new Event('updatedByJavascript'))
|
||||
}
|
||||
)
|
||||
|
||||
const colorsSection = document.getElementById('custom-colors')
|
||||
new ThemingOption(
|
||||
new ThemingOption<boolean>(
|
||||
'custom-theme',
|
||||
(opt, upd) => {
|
||||
if (upd) {
|
||||
const state = opt.input.checked
|
||||
opt.value = state
|
||||
localStorage.setItem('dc09_custom', state ? '1' : '0')
|
||||
return opt.onchange(opt, false)
|
||||
}
|
||||
|
||||
const state = toBool(localStorage.getItem('dc09_custom'))
|
||||
opt.value = state
|
||||
opt.input.checked = state
|
||||
colorsSection.dataset.show = state ? '1' : '0'
|
||||
|
||||
|
@ -225,9 +247,9 @@ import TextBox from "./TextBox.astro";
|
|||
}
|
||||
|
||||
function reloadTheme() {
|
||||
const accent = '#' + toColor(accentOpt.input.value)
|
||||
const bdrs = Number(bdrsOpt.input.value)
|
||||
const purebg = purebgOpt.input.checked
|
||||
const accent = '#' + accentOpt.value
|
||||
const bdrs = bdrsOpt.value
|
||||
const purebg = purebgOpt.value
|
||||
eval('less').modifyVars({
|
||||
accent: accent,
|
||||
bdrs: `${bdrs}rem`,
|
||||
|
|
Loading…
Add table
Reference in a new issue