Skip to content

Build your personal knowledge base with Trilium Notes

feat(presentation): highlight a code slide in the code note's colors

Elian Doran727d441e9e04
diff --git a/apps/client/src/services/code_note_preview.spec.ts b/apps/client/src/services/code_note_preview.spec.ts
index 01b45502ce..e1fd0c3b91 100644
--- a/apps/client/src/services/code_note_preview.spec.ts
+++ b/apps/client/src/services/code_note_preview.spec.ts
@@ -48,6 +48,17 @@ describe("renderCodeNotePreview", () => {
.toMatch(/^\.code-preview\[data-code-theme-t="monokai"\]/);
});
+ it("gives the colors of the code theme for a light or a dark background, unscoped, for a slide", async () => {
+ const matching = await freshModule({ codeNoteThemeMatchesApp: "true" });
+ expect(await matching.getCodePreviewThemeCss(false))
+ .toMatch(/^\.code-preview\[data-code-theme-l="vs-code-light"\]/);
+ expect(await matching.getCodePreviewThemeCss(true))
+ .toMatch(/^\.code-preview\[data-code-theme-d="vs-code-dark"\]/);
+
+ const single = await freshModule({ codeNoteThemeMatchesApp: "false" });
+ expect(await single.getCodePreviewThemeCss(true)).toMatch(/^\.code-preview\[data-code-theme-t="monokai"\]/);
+ });
+
it("falls back to the default style for a theme that is not a built-in one", async () => {
const { renderCodeNotePreview } = await freshModule({
codeNoteThemeMatchesApp: "false", codeNoteTheme: "custom"
diff --git a/apps/client/src/services/code_note_preview.ts b/apps/client/src/services/code_note_preview.ts
index 51b6229285..bed8ded01f 100644
--- a/apps/client/src/services/code_note_preview.ts
+++ b/apps/client/src/services/code_note_preview.ts
@@ -69,6 +69,17 @@ export async function renderCodeNotePreview(
return template.content.firstElementChild as HTMLElement;
}
+/**
+ * Returns the CSS that colors the previews for a background that `isDark` tells, unscoped, for
+ * previews in a shadow root, where the CSS of the page and its scopes do not reach: a presentation
+ * slide, whose theme rather than the app sets how dark it is.
+ */
+export async function getCodePreviewThemeCss(isDark: boolean) {
+ const choices = getThemeChoices();
+ const choice = choices.find(({ key }) => key === (isDark ? "d" : "l")) ?? choices[0];
+ return buildCodePreviewCss(await loadSlot(choice));
+}
+
/** The light and dark themes when they follow the app, else the one theme. Print is light. */
function getThemeChoices(): ThemeChoice[] {
if (glob.device === "print") {
diff --git a/apps/client/src/widgets/collections/presentation/themes.spec.ts b/apps/client/src/widgets/collections/presentation/themes.spec.ts
index a82030c847..b4d365cc58 100644
--- a/apps/client/src/widgets/collections/presentation/themes.spec.ts
+++ b/apps/client/src/widgets/collections/presentation/themes.spec.ts
@@ -23,8 +23,9 @@ describe("Presentation themes", () => {
it("sets the color scheme that matches the background of each theme", async () => {
const darkThemes = [ "black", "black-contrast", "blood", "dracula", "league", "moon", "night" ];
- for (const { id } of getPresentationThemes()) {
+ for (const { id, isDark } of getPresentationThemes()) {
const scheme = darkThemes.includes(id) ? "dark" : "light";
+ expect(isDark, id).toBe(scheme === "dark");
expect(await loadPresentationTheme(id), id).toMatch(
new RegExp(String.raw`\.reveal\s*\{[^}]*color-scheme:\s*${scheme};`));
}
diff --git a/apps/client/src/widgets/collections/presentation/themes.ts b/apps/client/src/widgets/collections/presentation/themes.ts
index 37368d59f5..94f1b63a0e 100644
--- a/apps/client/src/widgets/collections/presentation/themes.ts
+++ b/apps/client/src/widgets/collections/presentation/themes.ts
@@ -3,69 +3,90 @@ export const DEFAULT_THEME = "white";
const themes = {
black: {
name: "Black",
+ isDark: true,
loadTheme: () => import("./reveal-themes/black.scss?inline")
},
"black-contrast": {
name: "Black Contrast",
+ isDark: true,
loadTheme: () => import("./reveal-themes/black-contrast.scss?inline")
},
white: {
name: "White",
+ isDark: false,
loadTheme: () => import("./reveal-themes/white.scss?inline")
},
"white-contrast": {
name: "White Contrast",
+ isDark: false,
loadTheme: () => import("./reveal-themes/white-contrast.scss?inline")
},
beige: {
name: "Beige",
+ isDark: false,
loadTheme: () => import("./reveal-themes/beige.scss?inline")
},
serif: {
name: "Serif",
+ isDark: false,
loadTheme: () => import("./reveal-themes/serif.scss?inline")
},
simple: {
name: "Simple",
+ isDark: false,
loadTheme: () => import("./reveal-themes/simple.scss?inline")
},
solarized: {
name: "Solarized",
+ isDark: false,
loadTheme: () => import("./reveal-themes/solarized.scss?inline")
},
moon: {
name: "Moon",
+ isDark: true,
loadTheme: () => import("./reveal-themes/moon.scss?inline")
},
dracula: {
name: "Dracula",
+ isDark: true,
loadTheme: () => import("./reveal-themes/dracula.scss?inline")
},
sky: {
name: "Sky",
+ isDark: false,
loadTheme: () => import("./reveal-themes/sky.scss?inline")
},
blood: {
name: "Blood",
+ isDark: true,
loadTheme: () => import("./reveal-themes/blood.scss?inline")
},
league: {
name: "League",
+ isDark: true,
loadTheme: () => import("./reveal-themes/league.scss?inline")
},
night: {
name: "Night",
+ isDark: true,
loadTheme: () => import("./reveal-themes/night.scss?inline")
}
} as const;
+/** The themes, each with whether its background is dark, as the `color-scheme` that it sets says. */
export function getPresentationThemes() {
return Object.entries(themes).map(([ id, theme ]) => ({
id,
- name: theme.name
+ name: theme.name,
+ isDark: theme.isDark
}));
}
+/** Whether the background of the theme `name`, or of the default theme for an unknown one, is dark. */
+export function isPresentationThemeDark(name: string) {
+ return (themes[name as keyof typeof themes] ?? themes[DEFAULT_THEME]).isDark;
+}
+
export async function loadPresentationTheme(name: keyof typeof themes | string) {
let theme = themes[name];
if (!theme) theme = themes[DEFAULT_THEME];