()
| 12 | } |
| 13 | // init themefield |
| 14 | function initTheme() { |
| 15 | Array.from(document.styleSheets) |
| 16 | .filter((sheet) => sheet.href?.startsWith(window.location.origin)) |
| 17 | .flatMap((sheet) => Array.from(sheet.cssRules)) |
| 18 | .forEach((rule) => { |
| 19 | var selector = rule.selectorText |
| 20 | if (selector && selector.startsWith(".theme-") && !selector.includes(" ")) { |
| 21 | if (DEFAULT_THEME) { |
| 22 | // re-add props that dont change (css needs this so they update correctly) |
| 23 | Array.from(DEFAULT_THEME.rule.style) |
| 24 | .filter((cssVariable) => !Array.from(rule.style).includes(cssVariable)) |
| 25 | .forEach((cssVariable) => { |
| 26 | rule.style.setProperty(cssVariable, DEFAULT_THEME.rule.style.getPropertyValue(cssVariable)) |
| 27 | }) |
| 28 | } |
| 29 | var theme_key = selector.substring(1) |
| 30 | THEMES.push({ |
| 31 | key: theme_key, |
| 32 | name: getThemeName(theme_key), |
| 33 | rule: rule, |
| 34 | }) |
| 35 | } |
| 36 | if (selector && selector == ":root") { |
| 37 | DEFAULT_THEME = { |
| 38 | key: "theme-default", |
| 39 | name: "Default", |
| 40 | rule: rule, |
| 41 | } |
| 42 | } |
| 43 | }) |
| 44 | |
| 45 | THEMES.forEach((theme) => { |
| 46 | var new_option = document.createElement("option") |
| 47 | new_option.setAttribute("value", theme.key) |
| 48 | new_option.innerText = theme.name |
| 49 | themeField.appendChild(new_option) |
| 50 | }) |
| 51 | |
| 52 | // setup the style transitions a second after app initializes, so initial style is instant |
| 53 | setTimeout(() => { |
| 54 | var body = document.querySelector("body") |
| 55 | var style = document.createElement("style") |
| 56 | style.innerHTML = "* { transition: background 0.5s, color 0.5s, background-color 0.5s; }" |
| 57 | body.appendChild(style) |
| 58 | }, 1000) |
| 59 | } |
| 60 | initTheme() |
| 61 | |
| 62 | function themeFieldChanged() { |
no test coverage detected