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

Commit 15a6d01

Browse files
committed
✨ Feat: Add theme options
This commit introduces the ability to store and retrieve theme options in the database. The `themes` table in the database has been updated to include an `options` column, which stores the theme options as a JSON string. The `addTheme` function now accepts an `options` parameter and stores it in the database. A new function `getThemeOptions` has been added to retrieve the theme options for a given theme. This change allows for more flexible and customizable themes.
1 parent 830c36d commit 15a6d01

File tree

3 files changed

+93
-52
lines changed

3 files changed

+93
-52
lines changed

src/core/database/database.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ export function init() {
105105
name TEXT PRIMARY KEY,
106106
creator TEXT NOT NULL,
107107
vars TEXT NOT NULL,
108+
options TEXT NOT NULL,
108109
tags TEXT NOT NULL
109110
)
110111
`);
@@ -135,10 +136,23 @@ export function init() {
135136
}
136137
`;
137138

139+
const defaultThemeOptions = {
140+
backgroundAnimation: {
141+
enabled: true,
142+
from: ["#c084fc", "#818cf9", "#60a5fa"],
143+
},
144+
};
145+
138146
if (themeRows.count === 0) {
139147
db.prepare(
140-
"INSERT INTO themes (name, creator, vars, tags) VALUES (?,?,?,?)",
141-
).run("default", "Its4Nik", defaultCss, "default, dark");
148+
"INSERT INTO themes (name, creator, vars, options, tags) VALUES (?,?,?,?,?)",
149+
).run(
150+
"default",
151+
"Its4Nik",
152+
defaultCss,
153+
JSON.stringify(defaultThemeOptions),
154+
"default, dark",
155+
);
142156
}
143157

144158
const configRow = db

src/core/database/themes.ts

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,55 @@
1-
import type { Theme } from "~/typings/database";
1+
import type { Theme, ThemeOptions } from "~/typings/database";
22
import { logger } from "../utils/logger";
33
import { db } from "./database";
44
import { executeDbOperation } from "./helper";
55

66
const stmt = {
7-
insert: db.prepare(`
8-
INSERT INTO themes (name, creator, vars, tags) VALUES (?, ?, ?, ?)
7+
insert: db.prepare(`
8+
INSERT INTO themes (name, creator, vars, options, tags) VALUES (?, ?, ?, ?, ?)
99
`),
10-
remove: db.prepare("DELETE FROM themes WHERE name = ?"),
11-
read: db.prepare("SELECT * FROM themes WHERE name = ?"),
12-
readAll: db.prepare("SELECT * FROM themes"),
10+
remove: db.prepare("DELETE FROM themes WHERE name = ?"),
11+
read: db.prepare(
12+
"SELECT name, creator, vars, tags FROM themes WHERE name = ?",
13+
),
14+
readOptions: db.prepare("SELECT options FROM themes WHERE name = ?"),
15+
readAll: db.prepare("SELECT * FROM themes"),
1316
};
1417

1518
export function getThemes() {
16-
return executeDbOperation("Get Themes", () => stmt.readAll.all()) as Theme[];
19+
return executeDbOperation("Get Themes", () => stmt.readAll.all()) as Theme[];
1720
}
1821

19-
export function addTheme({ name, creator, vars, tags }: Theme) {
20-
return executeDbOperation("Save Theme", () =>
21-
stmt.insert.run(name, creator, vars, tags.toString()),
22-
);
22+
export function addTheme({ name, creator, options, vars, tags }: Theme) {
23+
return executeDbOperation("Save Theme", () =>
24+
stmt.insert.run(
25+
name,
26+
creator,
27+
vars,
28+
JSON.stringify(options),
29+
tags.toString(),
30+
),
31+
);
2332
}
33+
2434
export function getSpecificTheme(name: string): Theme {
25-
return executeDbOperation(
26-
"Getting specific Theme",
27-
() => stmt.read.get(name) as Theme,
28-
);
35+
return executeDbOperation(
36+
"Getting specific Theme",
37+
() => stmt.read.get(name) as Theme,
38+
);
39+
}
40+
41+
export function getThemeOptions(name: string): ThemeOptions {
42+
const data = executeDbOperation(
43+
"Getting Theme Options",
44+
() => (stmt.readOptions.get(name) as { options: string }).options,
45+
);
46+
47+
logger.debug(`RAW DB: ${JSON.stringify(stmt.readOptions.get(name))}`);
48+
49+
return JSON.parse(data);
2950
}
3051

3152
export function deleteTheme(name: string) {
32-
logger.debug(`Removing ${name} from themes `);
33-
return executeDbOperation("Remove Theme", () => stmt.remove.run(name));
53+
logger.debug(`Removing ${name} from themes `);
54+
return executeDbOperation("Remove Theme", () => stmt.remove.run(name));
3455
}

src/handlers/themes.ts

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,48 @@
11
import { dbFunctions } from "~/core/database";
2-
import type { Theme } from "~/typings/database";
2+
import { logger } from "~/core/utils/logger";
3+
import type { Theme, ThemeOptions } from "~/typings/database";
34

45
class themeHandler {
5-
getThemes(): Theme[] {
6-
return dbFunctions.getThemes();
7-
}
8-
addTheme(theme: Theme) {
9-
try {
10-
const rawVars =
11-
typeof theme.vars === "string" ? JSON.parse(theme.vars) : theme.vars;
6+
getThemes(): Theme[] {
7+
return dbFunctions.getThemes();
8+
}
9+
addTheme(theme: Theme) {
10+
try {
11+
const rawVars =
12+
typeof theme.vars === "string" ? JSON.parse(theme.vars) : theme.vars;
1213

13-
const cssVars = Object.entries(rawVars)
14-
.map(([key, value]) => `--${key}: ${value};`)
15-
.join(" ");
14+
const cssVars = Object.entries(rawVars)
15+
.map(([key, value]) => `--${key}: ${value};`)
16+
.join(" ");
1617

17-
const varsString = `.root, #root, #docs-root { ${cssVars} }`;
18+
const varsString = `.root, #root, #docs-root { ${cssVars} }`;
1819

19-
return dbFunctions.addTheme({
20-
...theme,
21-
vars: varsString,
22-
});
23-
} catch (error) {
24-
throw new Error(
25-
`Could not save theme ${JSON.stringify(theme)}, error: ${error}`,
26-
);
27-
}
28-
}
29-
deleteTheme(name: string) {
30-
try {
31-
dbFunctions.deleteTheme(name);
32-
return "Deleted theme";
33-
} catch (error) {
34-
throw new Error(`Could not save theme ${name}, error: ${error}`);
35-
}
36-
}
37-
getTheme(name: string): Theme {
38-
return dbFunctions.getSpecificTheme(name);
39-
}
20+
return dbFunctions.addTheme({
21+
...theme,
22+
vars: varsString,
23+
});
24+
} catch (error) {
25+
throw new Error(
26+
`Could not save theme ${JSON.stringify(theme)}, error: ${error}`,
27+
);
28+
}
29+
}
30+
deleteTheme(name: string) {
31+
try {
32+
dbFunctions.deleteTheme(name);
33+
return "Deleted theme";
34+
} catch (error) {
35+
throw new Error(`Could not save theme ${name}, error: ${error}`);
36+
}
37+
}
38+
getTheme(name: string): Theme {
39+
return dbFunctions.getSpecificTheme(name);
40+
}
41+
getThemeOptions(name: string): ThemeOptions {
42+
const data = dbFunctions.getThemeOptions(name);
43+
logger.debug(`Received ${JSON.stringify(data)}`);
44+
return data;
45+
}
4046
}
4147

4248
export const ThemeHandler = new themeHandler();

0 commit comments

Comments
 (0)