Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
chore(client): address requested changes
Elian Doran97b05f97836a
diff --git a/apps/client/src/services/theme.spec.ts b/apps/client/src/services/theme.spec.tsindex ec43683f4f..b895760309 100644--- a/apps/client/src/services/theme.spec.ts+++ b/apps/client/src/services/theme.spec.ts@@ -231,6 +231,37 @@ describe("applyTheme", () => {expect(win.glob?.customThemeCssUrl).toBe("api/notes/download/abc123");expect(win.glob?.themeBase).toBe("next-dark");});++ it("keeps the previous theme when a new stylesheet fails to load", async () => {+ applyTheme("dark");+ fireLoadOnPendingThemeLinks();+ await Promise.resolve();++ // A failing single-stylesheet theme rolls back the swap, including the glob metadata.+ applyTheme("my-theme", "api/notes/download/missing");+ const failedLink = document.head.querySelector<HTMLLinkElement>(`link[href="api/notes/download/missing"]`);+ expect(failedLink).not.toBeNull();+ failedLink?.dispatchEvent(new Event("error"));+ await Promise.resolve();++ expect(themeStylesheetHrefs()).toEqual([`${STYLESHEETS_PATH}/theme-dark.css`]);+ expect(document.body.getAttribute("data-theme-id")).toBe("dark");+ expect(win.glob?.theme).toBe("dark");+ expect(win.glob?.customThemeCssUrl).toBeUndefined();++ // A partial failure of a multi-stylesheet theme also rolls back fully, removing the+ // stylesheets that did load.+ applyTheme("next");+ const loadedLink = document.head.querySelector<HTMLLinkElement>(`link[href="${STYLESHEETS_PATH}/theme-next-light.css"]`);+ const erroredLink = document.head.querySelector<HTMLLinkElement>(`link[href="${STYLESHEETS_PATH}/theme-next-dark.css"]`);+ loadedLink?.dispatchEvent(new Event("load"));+ erroredLink?.dispatchEvent(new Event("error"));+ await Promise.resolve();++ expect(themeStylesheetHrefs()).toEqual([`${STYLESHEETS_PATH}/theme-dark.css`]);+ expect(document.body.getAttribute("data-theme-id")).toBe("dark");+ expect(win.glob?.theme).toBe("dark");+ });});function themeStylesheetHrefs() {diff --git a/apps/client/src/services/theme.ts b/apps/client/src/services/theme.tsindex 9e803dc527..2cea976ac7 100644--- a/apps/client/src/services/theme.ts+++ b/apps/client/src/services/theme.ts@@ -177,8 +177,14 @@ interface CustomThemeInfo {/*** Swaps the active theme stylesheets without reloading the page. The new stylesheets are appended first and the* previous ones removed only once the new ones have loaded, so the swap stays free of a flash of unstyled content.+ * If any of the new stylesheets fails to load, the swap is rolled back and the previous theme stays applied.*/export function applyTheme(theme: string, customThemeCssUrl?: string, themeBase?: string) {+ const previousGlob = {+ theme: window.glob.theme,+ customThemeCssUrl: window.glob.customThemeCssUrl,+ themeBase: window.glob.themeBase+ };window.glob.theme = theme;window.glob.customThemeCssUrl = customThemeCssUrl;window.glob.themeBase = themeBase as ThemeBase | undefined;@@ -199,7 +205,20 @@ export function applyTheme(theme: string, customThemeCssUrl?: string, themeBase?return link;});- void waitForStylesheets(newLinks).then(() => {+ void waitForStylesheets(newLinks).then((allLoaded) => {+ // If any new stylesheet failed to load (e.g. a deleted custom theme note or a network+ // error), keep the previous theme rather than leaving the page unstyled. The metadata is+ // restored to keep getThemeStyle() consistent with what is actually rendered.+ if (!allLoaded) {+ for (const newLink of newLinks) {+ newLink.remove();+ }+ window.glob.theme = previousGlob.theme;+ window.glob.customThemeCssUrl = previousGlob.customThemeCssUrl;+ window.glob.themeBase = previousGlob.themeBase;+ return;+ }+for (const oldLink of oldLinks) {oldLink.remove();}@@ -216,17 +235,22 @@ export function applyTheme(theme: string, customThemeCssUrl?: string, themeBase?});}-function waitForStylesheets(links: HTMLLinkElement[]): Promise<void> {+/** Resolves once every link has settled, with `true` only if all of them loaded successfully. */+function waitForStylesheets(links: HTMLLinkElement[]): Promise<boolean> {if (links.length === 0) {- return Promise.resolve();+ return Promise.resolve(true);}return new Promise((resolve) => {let remaining = links.length;- const onSettled = () => {+ let allLoaded = true;+ const onSettled = (e: Event) => {+ if (e.type === "error") {+ allLoaded = false;+ }remaining--;if (remaining <= 0) {- resolve();+ resolve(allLoaded);}};for (const link of links) {