Skip to content
This repository was archived by the owner on Aug 2, 2025. It is now read-only.

Commit 503db97

Browse files
committed
feat(themes): Transform and save CSS variables for themes
This commit introduces a transformation of the `vars` field in the `addTheme` function. It converts a JSON object of CSS variables into a string of CSS rules that target the root element. Additionally, it updates the themes table to set the name field as the primary key. This change is crucial for ensuring data integrity and efficient theme management.
1 parent 4ac7e14 commit 503db97

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

src/core/database/database.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export function init() {
9898
);
9999
100100
CREATE TABLE IF NOT EXISTS themes (
101-
name TEXT NOT NULL,
101+
name TEXT PRIMARY KEY,
102102
creator TEXT NOT NULL,
103103
vars TEXT NOT NULL,
104104
tags TEXT NOT NULL

src/handlers/themes.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,23 @@ class themeHandler {
77
}
88
addTheme(theme: Theme) {
99
try {
10-
return dbFunctions.addTheme({ ...theme });
10+
const rawVars =
11+
typeof theme.vars === "string" ? JSON.parse(theme.vars) : theme.vars;
12+
13+
const cssVars = Object.entries(rawVars)
14+
.map(([key, value]) => `--${key}: ${value};`)
15+
.join(" ");
16+
17+
const varsString = `.root, #root, #docs-root { ${cssVars} }`;
18+
19+
return dbFunctions.addTheme({
20+
...theme,
21+
vars: varsString,
22+
});
1123
} catch (error) {
12-
throw new Error(`Could not save theme ${theme}, error: ${error}`);
24+
throw new Error(
25+
`Could not save theme ${JSON.stringify(theme)}, error: ${error}`,
26+
);
1327
}
1428
}
1529
deleteTheme({ name }: Theme) {

0 commit comments

Comments
 (0)