2019-10-01 13:21:22 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2019-10-01 13:21:22 +00:00
|
|
|
Copyright 2019 Michael Telatynski <7t3chguy@gmail.com>
|
2019-10-02 09:05:57 +00:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2019-10-01 13:21:22 +00:00
|
|
|
|
2024-09-09 13:57:16 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2019-10-01 13:21:22 +00:00
|
|
|
*/
|
|
|
|
|
2023-06-28 14:39:19 +00:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2022-11-15 10:20:36 +00:00
|
|
|
|
2021-06-29 12:11:58 +00:00
|
|
|
import { _t } from "./languageHandler";
|
2020-05-28 12:55:07 +00:00
|
|
|
import SettingsStore from "./settings/SettingsStore";
|
|
|
|
import ThemeWatcher from "./settings/watchers/ThemeWatcher";
|
2019-11-20 13:41:06 +00:00
|
|
|
|
2021-09-27 12:13:30 +00:00
|
|
|
export const DEFAULT_THEME = "light";
|
2023-02-13 11:39:16 +00:00
|
|
|
const HIGH_CONTRAST_THEMES: Record<string, string> = {
|
2022-12-12 11:24:14 +00:00
|
|
|
light: "light-high-contrast",
|
2021-10-27 13:31:54 +00:00
|
|
|
};
|
2021-09-27 12:13:30 +00:00
|
|
|
|
2023-03-06 17:21:09 +00:00
|
|
|
interface IFontFaces extends Omit<Record<(typeof allowedFontFaceProps)[number], string>, "src"> {
|
2021-09-27 12:13:30 +00:00
|
|
|
src: {
|
|
|
|
format: string;
|
|
|
|
url: string;
|
|
|
|
local: string;
|
|
|
|
}[];
|
|
|
|
}
|
|
|
|
|
2024-02-13 14:07:58 +00:00
|
|
|
interface CompoundTheme {
|
|
|
|
[token: string]: string;
|
|
|
|
}
|
|
|
|
|
2023-06-28 14:39:19 +00:00
|
|
|
export type CustomTheme = {
|
|
|
|
name: string;
|
2024-02-13 14:07:58 +00:00
|
|
|
is_dark?: boolean; // eslint-disable-line camelcase
|
|
|
|
colors?: {
|
2021-09-27 12:13:30 +00:00
|
|
|
[key: string]: string;
|
|
|
|
};
|
2024-02-13 14:07:58 +00:00
|
|
|
fonts?: {
|
2021-09-27 12:13:30 +00:00
|
|
|
faces: IFontFaces[];
|
|
|
|
general: string;
|
|
|
|
monospace: string;
|
|
|
|
};
|
2024-02-13 14:07:58 +00:00
|
|
|
compound?: CompoundTheme;
|
2023-06-28 14:39:19 +00:00
|
|
|
};
|
2021-09-27 12:13:30 +00:00
|
|
|
|
2021-10-27 13:31:54 +00:00
|
|
|
/**
|
|
|
|
* Given a non-high-contrast theme, find the corresponding high-contrast one
|
|
|
|
* if it exists, or return undefined if not.
|
|
|
|
*/
|
2023-01-12 13:25:14 +00:00
|
|
|
export function findHighContrastTheme(theme: string): string | undefined {
|
2021-10-27 13:31:54 +00:00
|
|
|
return HIGH_CONTRAST_THEMES[theme];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a high-contrast theme, find the corresponding non-high-contrast one
|
|
|
|
* if it exists, or return undefined if not.
|
|
|
|
*/
|
2023-01-12 13:25:14 +00:00
|
|
|
export function findNonHighContrastTheme(hcTheme: string): string | undefined {
|
2021-10-27 13:31:54 +00:00
|
|
|
for (const theme in HIGH_CONTRAST_THEMES) {
|
|
|
|
if (HIGH_CONTRAST_THEMES[theme] === hcTheme) {
|
|
|
|
return theme;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decide whether the supplied theme is high contrast.
|
|
|
|
*/
|
2023-01-12 13:25:14 +00:00
|
|
|
export function isHighContrastTheme(theme: string): boolean {
|
2021-10-27 13:31:54 +00:00
|
|
|
return Object.values(HIGH_CONTRAST_THEMES).includes(theme);
|
|
|
|
}
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
export function enumerateThemes(): { [key: string]: string } {
|
2019-10-01 13:21:22 +00:00
|
|
|
const BUILTIN_THEMES = {
|
2023-08-23 09:25:33 +00:00
|
|
|
"light": _t("common|light"),
|
2023-09-19 16:16:38 +00:00
|
|
|
"light-high-contrast": _t("theme|light_high_contrast"),
|
2023-08-23 09:25:33 +00:00
|
|
|
"dark": _t("common|dark"),
|
2019-10-01 13:21:22 +00:00
|
|
|
};
|
2023-06-28 14:39:19 +00:00
|
|
|
const customThemes = SettingsStore.getValue("custom_themes") || [];
|
2023-02-13 11:39:16 +00:00
|
|
|
const customThemeNames: Record<string, string> = {};
|
2023-06-28 14:39:19 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
for (const { name } of customThemes) {
|
|
|
|
customThemeNames[`custom-${name}`] = name;
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
logger.warn("Error loading custom themes", {
|
|
|
|
err,
|
|
|
|
customThemes,
|
|
|
|
});
|
2019-10-01 13:21:22 +00:00
|
|
|
}
|
2023-06-28 14:39:19 +00:00
|
|
|
|
2019-10-01 13:21:22 +00:00
|
|
|
return Object.assign({}, customThemeNames, BUILTIN_THEMES);
|
|
|
|
}
|
|
|
|
|
2024-06-26 15:47:01 +00:00
|
|
|
export interface ITheme {
|
2021-11-26 15:41:04 +00:00
|
|
|
id: string;
|
|
|
|
name: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getOrderedThemes(): ITheme[] {
|
|
|
|
const themes = Object.entries(enumerateThemes())
|
2022-12-12 11:24:14 +00:00
|
|
|
.map((p) => ({ id: p[0], name: p[1] })) // convert pairs to objects for code readability
|
|
|
|
.filter((p) => !isHighContrastTheme(p.id));
|
|
|
|
const builtInThemes = themes.filter((p) => !p.id.startsWith("custom-"));
|
2024-07-17 13:51:42 +00:00
|
|
|
const collator = new Intl.Collator();
|
|
|
|
const customThemes = themes
|
|
|
|
.filter((p) => !builtInThemes.includes(p))
|
|
|
|
.sort((a, b) => collator.compare(a.name, b.name));
|
2021-11-26 15:41:04 +00:00
|
|
|
return [...builtInThemes, ...customThemes];
|
|
|
|
}
|
|
|
|
|
2021-09-27 12:13:30 +00:00
|
|
|
function clearCustomTheme(): void {
|
2020-06-23 15:54:17 +00:00
|
|
|
// remove all css variables, we assume these are there because of the custom theme
|
|
|
|
const inlineStyleProps = Object.values(document.body.style);
|
|
|
|
for (const prop of inlineStyleProps) {
|
2024-03-28 12:04:14 +00:00
|
|
|
if (prop.startsWith("--")) {
|
2020-06-23 15:54:17 +00:00
|
|
|
document.body.style.removeProperty(prop);
|
|
|
|
}
|
|
|
|
}
|
2024-02-13 14:07:58 +00:00
|
|
|
|
|
|
|
// remove the custom style sheets
|
|
|
|
document.querySelector("head > style[title='custom-theme-font-faces']")?.remove();
|
|
|
|
document.querySelector("head > style[title='custom-theme-compound']")?.remove();
|
2020-06-23 15:54:17 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 12:54:14 +00:00
|
|
|
const allowedFontFaceProps = [
|
|
|
|
"font-display",
|
|
|
|
"font-family",
|
|
|
|
"font-stretch",
|
|
|
|
"font-style",
|
|
|
|
"font-weight",
|
|
|
|
"font-variant",
|
|
|
|
"font-feature-settings",
|
|
|
|
"font-variation-settings",
|
|
|
|
"src",
|
2020-06-24 12:58:41 +00:00
|
|
|
"unicode-range",
|
2023-02-13 11:39:16 +00:00
|
|
|
] as const;
|
2020-06-24 12:54:14 +00:00
|
|
|
|
2021-09-27 12:13:30 +00:00
|
|
|
function generateCustomFontFaceCSS(faces: IFontFaces[]): string {
|
2022-12-12 11:24:14 +00:00
|
|
|
return faces
|
|
|
|
.map((face) => {
|
2023-02-13 11:39:16 +00:00
|
|
|
const src = face.src
|
|
|
|
?.map((srcElement) => {
|
2023-02-15 13:36:22 +00:00
|
|
|
let format = "";
|
2023-02-13 11:39:16 +00:00
|
|
|
if (srcElement.format) {
|
|
|
|
format = `format("${srcElement.format}")`;
|
|
|
|
}
|
|
|
|
if (srcElement.url) {
|
|
|
|
return `url("${srcElement.url}") ${format}`;
|
|
|
|
} else if (srcElement.local) {
|
|
|
|
return `local("${srcElement.local}") ${format}`;
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
})
|
|
|
|
.join(", ");
|
2023-05-16 13:25:43 +00:00
|
|
|
const props = Object.keys(face).filter((prop) =>
|
|
|
|
allowedFontFaceProps.includes(prop as (typeof allowedFontFaceProps)[number]),
|
2023-03-06 17:21:09 +00:00
|
|
|
) as Array<(typeof allowedFontFaceProps)[number]>;
|
2022-12-12 11:24:14 +00:00
|
|
|
const body = props
|
|
|
|
.map((prop) => {
|
2023-02-13 11:39:16 +00:00
|
|
|
let value: string;
|
2022-12-12 11:24:14 +00:00
|
|
|
if (prop === "src") {
|
|
|
|
value = src;
|
|
|
|
} else if (prop === "font-family") {
|
|
|
|
value = `"${face[prop]}"`;
|
|
|
|
} else {
|
|
|
|
value = face[prop];
|
|
|
|
}
|
|
|
|
return `${prop}: ${value}`;
|
|
|
|
})
|
|
|
|
.join(";");
|
|
|
|
return `@font-face {${body}}`;
|
|
|
|
})
|
|
|
|
.join("\n");
|
2020-06-23 15:54:38 +00:00
|
|
|
}
|
2020-04-27 15:31:22 +00:00
|
|
|
|
2024-02-13 14:07:58 +00:00
|
|
|
const COMPOUND_TOKEN = /^--cpd-[a-z0-9-]+$/;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a style sheet to override Compound design tokens as specified in
|
|
|
|
* the given theme.
|
|
|
|
*/
|
|
|
|
function generateCustomCompoundCSS(theme: CompoundTheme): string {
|
|
|
|
const properties: string[] = [];
|
|
|
|
for (const [token, value] of Object.entries(theme))
|
|
|
|
if (COMPOUND_TOKEN.test(token)) properties.push(`${token}: ${value};`);
|
|
|
|
else logger.warn(`'${token}' is not a valid Compound token`);
|
|
|
|
// Insert the design token overrides into the 'custom' cascade layer as
|
|
|
|
// documented at https://compound.element.io/?path=/docs/develop-theming--docs
|
|
|
|
return `@layer compound.custom { :root, [class*="cpd-theme-"] { ${properties.join(" ")} } }`;
|
|
|
|
}
|
|
|
|
|
2023-06-28 14:39:19 +00:00
|
|
|
function setCustomThemeVars(customTheme: CustomTheme): void {
|
2021-06-29 12:11:58 +00:00
|
|
|
const { style } = document.body;
|
2020-04-27 15:31:22 +00:00
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
function setCSSColorVariable(name: string, hexColor: string, doPct = true): void {
|
2020-04-27 15:31:22 +00:00
|
|
|
style.setProperty(`--${name}`, hexColor);
|
|
|
|
if (doPct) {
|
2020-04-27 17:39:07 +00:00
|
|
|
// uses #rrggbbaa to define the color with alpha values at 0%, 15% and 50%
|
2019-10-01 15:00:29 +00:00
|
|
|
style.setProperty(`--${name}-0pct`, hexColor + "00");
|
2020-04-27 15:30:28 +00:00
|
|
|
style.setProperty(`--${name}-15pct`, hexColor + "26");
|
2019-10-01 15:00:29 +00:00
|
|
|
style.setProperty(`--${name}-50pct`, hexColor + "7F");
|
|
|
|
}
|
|
|
|
}
|
2020-04-27 15:31:22 +00:00
|
|
|
|
|
|
|
if (customTheme.colors) {
|
|
|
|
for (const [name, value] of Object.entries(customTheme.colors)) {
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
for (let i = 0; i < value.length; i += 1) {
|
2020-06-23 15:52:54 +00:00
|
|
|
setCSSColorVariable(`${name}_${i}`, value[i], false);
|
2020-04-27 15:31:22 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-06-23 15:52:54 +00:00
|
|
|
setCSSColorVariable(name, value);
|
2020-04-27 15:31:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-23 15:53:16 +00:00
|
|
|
if (customTheme.fonts) {
|
2021-06-29 12:11:58 +00:00
|
|
|
const { fonts } = customTheme;
|
2020-06-23 15:54:38 +00:00
|
|
|
if (fonts.faces) {
|
|
|
|
const css = generateCustomFontFaceCSS(fonts.faces);
|
|
|
|
const style = document.createElement("style");
|
|
|
|
style.setAttribute("title", "custom-theme-font-faces");
|
|
|
|
style.setAttribute("type", "text/css");
|
|
|
|
style.appendChild(document.createTextNode(css));
|
|
|
|
document.head.appendChild(style);
|
|
|
|
}
|
2020-06-23 15:53:16 +00:00
|
|
|
if (fonts.general) {
|
|
|
|
style.setProperty("--font-family", fonts.general);
|
|
|
|
}
|
|
|
|
if (fonts.monospace) {
|
|
|
|
style.setProperty("--font-family-monospace", fonts.monospace);
|
|
|
|
}
|
|
|
|
}
|
2024-02-13 14:07:58 +00:00
|
|
|
if (customTheme.compound) {
|
|
|
|
const css = generateCustomCompoundCSS(customTheme.compound);
|
|
|
|
const style = document.createElement("style");
|
|
|
|
style.setAttribute("title", "custom-theme-compound");
|
|
|
|
style.setAttribute("type", "text/css");
|
|
|
|
style.appendChild(document.createTextNode(css));
|
|
|
|
document.head.appendChild(style);
|
|
|
|
}
|
2019-10-01 15:00:29 +00:00
|
|
|
}
|
|
|
|
|
2023-06-28 14:39:19 +00:00
|
|
|
export function getCustomTheme(themeName: string): CustomTheme {
|
2019-10-01 13:21:22 +00:00
|
|
|
// set css variables
|
|
|
|
const customThemes = SettingsStore.getValue("custom_themes");
|
|
|
|
if (!customThemes) {
|
|
|
|
throw new Error(`No custom themes set, can't set custom theme "${themeName}"`);
|
|
|
|
}
|
2023-02-13 11:39:16 +00:00
|
|
|
const customTheme = customThemes.find((t: ITheme) => t.name === themeName);
|
2019-10-01 13:21:22 +00:00
|
|
|
if (!customTheme) {
|
2023-02-13 11:39:16 +00:00
|
|
|
const knownNames = customThemes.map((t: ITheme) => t.name).join(", ");
|
2019-10-01 13:21:22 +00:00
|
|
|
throw new Error(`Can't find custom theme "${themeName}", only know ${knownNames}`);
|
|
|
|
}
|
2019-10-01 15:00:29 +00:00
|
|
|
return customTheme;
|
2019-10-01 13:21:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called whenever someone changes the theme
|
2019-11-20 15:45:32 +00:00
|
|
|
* Async function that returns once the theme has been set
|
|
|
|
* (ie. the CSS has been loaded)
|
2019-10-01 13:21:22 +00:00
|
|
|
*
|
|
|
|
* @param {string} theme new theme
|
|
|
|
*/
|
2021-10-01 14:40:04 +00:00
|
|
|
export async function setTheme(theme?: string): Promise<void> {
|
2019-10-01 13:21:22 +00:00
|
|
|
if (!theme) {
|
2019-11-20 13:41:06 +00:00
|
|
|
const themeWatcher = new ThemeWatcher();
|
|
|
|
theme = themeWatcher.getEffectiveTheme();
|
2019-10-01 13:21:22 +00:00
|
|
|
}
|
2020-06-23 15:54:17 +00:00
|
|
|
clearCustomTheme();
|
2019-10-01 13:21:22 +00:00
|
|
|
let stylesheetName = theme;
|
|
|
|
if (theme.startsWith("custom-")) {
|
2022-04-14 07:52:42 +00:00
|
|
|
const customTheme = getCustomTheme(theme.slice(7));
|
2019-10-01 15:00:29 +00:00
|
|
|
stylesheetName = customTheme.is_dark ? "dark-custom" : "light-custom";
|
|
|
|
setCustomThemeVars(customTheme);
|
2019-10-01 13:21:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// look for the stylesheet elements.
|
|
|
|
// styleElements is a map from style name to HTMLLinkElement.
|
2022-09-28 08:44:32 +00:00
|
|
|
const styleElements = new Map<string, HTMLLinkElement>();
|
2022-12-12 11:24:14 +00:00
|
|
|
const themes = Array.from(document.querySelectorAll<HTMLLinkElement>("[data-mx-theme]"));
|
|
|
|
themes.forEach((theme) => {
|
2023-02-13 17:01:43 +00:00
|
|
|
styleElements.set(theme.dataset.mxTheme!.toLowerCase(), theme);
|
2021-08-01 14:18:06 +00:00
|
|
|
});
|
2019-10-01 13:21:22 +00:00
|
|
|
|
2022-09-28 08:44:32 +00:00
|
|
|
if (!styleElements.has(stylesheetName)) {
|
2019-10-01 13:21:22 +00:00
|
|
|
throw new Error("Unknown theme " + stylesheetName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// disable all of them first, then enable the one we want. Chrome only
|
|
|
|
// bothers to do an update on a true->false transition, so this ensures
|
|
|
|
// that we get exactly one update, at the right time.
|
|
|
|
//
|
|
|
|
// ^ This comment was true when we used to use alternative stylesheets
|
|
|
|
// for the CSS. Nowadays we just set them all as disabled in index.html
|
|
|
|
// and enable them as needed. It might be cleaner to disable them all
|
|
|
|
// at the same time to prevent loading two themes simultaneously and
|
|
|
|
// having them interact badly... but this causes a flash of unstyled app
|
|
|
|
// which is even uglier. So we don't.
|
|
|
|
|
2023-02-13 17:01:43 +00:00
|
|
|
const styleSheet = styleElements.get(stylesheetName)!;
|
2022-09-28 08:44:32 +00:00
|
|
|
styleSheet.disabled = false;
|
2019-10-01 13:21:22 +00:00
|
|
|
|
2023-06-14 15:49:14 +00:00
|
|
|
/**
|
|
|
|
* Adds the Compound theme class to the top-most element in the document
|
|
|
|
* This will automatically refresh the colour scales based on the OS or user
|
|
|
|
* preferences
|
|
|
|
*/
|
|
|
|
document.body.classList.remove("cpd-theme-light", "cpd-theme-dark", "cpd-theme-light-hc", "cpd-theme-dark-hc");
|
|
|
|
|
|
|
|
let compoundThemeClassName = `cpd-theme-` + (stylesheetName.includes("light") ? "light" : "dark");
|
|
|
|
// Always respect user OS preference!
|
|
|
|
if (isHighContrastTheme(theme) || window.matchMedia("(prefers-contrast: more)").matches) {
|
|
|
|
compoundThemeClassName += "-hc";
|
|
|
|
}
|
|
|
|
|
|
|
|
document.body.classList.add(compoundThemeClassName);
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2023-01-12 13:25:14 +00:00
|
|
|
const switchTheme = function (): void {
|
2019-11-20 15:45:32 +00:00
|
|
|
// we re-enable our theme here just in case we raced with another
|
2020-08-03 15:02:26 +00:00
|
|
|
// theme set request as per https://github.com/vector-im/element-web/issues/5601.
|
2019-11-20 15:45:32 +00:00
|
|
|
// We could alternatively lock or similar to stop the race, but
|
|
|
|
// this is probably good enough for now.
|
2022-09-28 08:44:32 +00:00
|
|
|
styleSheet.disabled = false;
|
2022-12-12 11:24:14 +00:00
|
|
|
styleElements.forEach((a) => {
|
2022-09-28 08:44:32 +00:00
|
|
|
if (a == styleSheet) return;
|
2019-11-20 15:45:32 +00:00
|
|
|
a.disabled = true;
|
|
|
|
});
|
2020-06-23 15:54:57 +00:00
|
|
|
const bodyStyles = global.getComputedStyle(document.body);
|
2020-05-06 15:23:06 +00:00
|
|
|
if (bodyStyles.backgroundColor) {
|
2023-02-13 17:01:43 +00:00
|
|
|
const metaElement = document.querySelector<HTMLMetaElement>('meta[name="theme-color"]')!;
|
2021-09-27 12:13:30 +00:00
|
|
|
metaElement.content = bodyStyles.backgroundColor;
|
2020-05-06 15:23:06 +00:00
|
|
|
}
|
2019-11-20 15:45:32 +00:00
|
|
|
resolve();
|
|
|
|
};
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
const isStyleSheetLoaded = (): boolean =>
|
2022-12-12 11:24:14 +00:00
|
|
|
Boolean([...document.styleSheets].find((_styleSheet) => _styleSheet?.href === styleSheet.href));
|
2019-11-20 15:45:32 +00:00
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
function waitForStyleSheetLoading(): void {
|
2022-09-27 09:02:57 +00:00
|
|
|
// turns out that Firefox preloads the CSS for link elements with
|
|
|
|
// the disabled attribute, but Chrome doesn't.
|
|
|
|
if (isStyleSheetLoaded()) {
|
|
|
|
switchTheme();
|
|
|
|
return;
|
|
|
|
}
|
2019-11-20 15:45:32 +00:00
|
|
|
|
2022-09-27 09:02:57 +00:00
|
|
|
let counter = 0;
|
2019-11-20 15:45:32 +00:00
|
|
|
|
2022-09-27 09:02:57 +00:00
|
|
|
// In case of theme toggling (white => black => white)
|
|
|
|
// Chrome doesn't fire the `load` event when the white theme is selected the second times
|
2022-11-30 11:32:56 +00:00
|
|
|
const intervalId = window.setInterval(() => {
|
2022-09-27 09:02:57 +00:00
|
|
|
if (isStyleSheetLoaded()) {
|
|
|
|
clearInterval(intervalId);
|
2023-02-13 17:01:43 +00:00
|
|
|
styleSheet.onload = null;
|
|
|
|
styleSheet.onerror = null;
|
2022-09-27 09:02:57 +00:00
|
|
|
switchTheme();
|
|
|
|
}
|
2019-10-01 13:21:22 +00:00
|
|
|
|
2022-09-27 09:02:57 +00:00
|
|
|
// Avoid to be stuck in an endless loop if there is an issue in the stylesheet loading
|
|
|
|
counter++;
|
2022-09-28 10:42:40 +00:00
|
|
|
if (counter === 10) {
|
2022-09-27 09:02:57 +00:00
|
|
|
clearInterval(intervalId);
|
2022-09-28 10:42:40 +00:00
|
|
|
reject();
|
2022-09-27 09:02:57 +00:00
|
|
|
}
|
2022-09-28 10:42:40 +00:00
|
|
|
}, 200);
|
2022-09-27 09:02:57 +00:00
|
|
|
|
2022-09-28 08:44:32 +00:00
|
|
|
styleSheet.onload = () => {
|
2022-09-27 09:02:57 +00:00
|
|
|
clearInterval(intervalId);
|
|
|
|
switchTheme();
|
|
|
|
};
|
|
|
|
|
2022-09-28 10:42:40 +00:00
|
|
|
styleSheet.onerror = (e) => {
|
2022-09-27 09:02:57 +00:00
|
|
|
clearInterval(intervalId);
|
2022-09-28 10:42:40 +00:00
|
|
|
reject(e);
|
2022-09-27 09:02:57 +00:00
|
|
|
};
|
2019-11-20 15:45:32 +00:00
|
|
|
}
|
2022-09-27 09:02:57 +00:00
|
|
|
|
|
|
|
waitForStyleSheetLoading();
|
2022-12-12 11:24:14 +00:00
|
|
|
});
|
2019-10-01 13:21:22 +00:00
|
|
|
}
|