2024-11-30 14:03:32 +08:00
|
|
|
import { Configuration, PathDescription } from "commons/serializableType";
|
|
|
|
import { HttpClient } from "core/http";
|
2024-11-27 01:02:05 +08:00
|
|
|
|
|
|
|
export interface ThemeConfig {
|
|
|
|
name: string;
|
|
|
|
displayName: string;
|
|
|
|
icon?: string;
|
|
|
|
version: string;
|
|
|
|
description?: string;
|
|
|
|
author?: string;
|
|
|
|
templates: Map<string, PathDescription>;
|
|
|
|
globalSettings?: {
|
|
|
|
layout?: string;
|
|
|
|
css?: string;
|
|
|
|
};
|
|
|
|
configuration: Configuration;
|
|
|
|
routes: {
|
|
|
|
index: string;
|
|
|
|
post: string;
|
|
|
|
tag: string;
|
|
|
|
category: string;
|
|
|
|
error: string;
|
|
|
|
loading: string;
|
|
|
|
page: Map<string, string>;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ThemeService {
|
|
|
|
private static instance: ThemeService;
|
|
|
|
private currentTheme?: ThemeConfig;
|
2024-11-30 14:03:32 +08:00
|
|
|
private http: HttpClient;
|
2024-11-27 01:02:05 +08:00
|
|
|
|
2024-11-30 14:03:32 +08:00
|
|
|
private constructor(api: HttpClient) {
|
|
|
|
this.http = api;
|
2024-11-27 01:02:05 +08:00
|
|
|
}
|
|
|
|
|
2024-11-30 14:03:32 +08:00
|
|
|
public static getInstance(api?: HttpClient): ThemeService {
|
2024-11-27 01:02:05 +08:00
|
|
|
if (!ThemeService.instance && api) {
|
|
|
|
ThemeService.instance = new ThemeService(api);
|
|
|
|
}
|
|
|
|
return ThemeService.instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async getCurrentTheme(): Promise<void> {
|
|
|
|
try {
|
2024-11-30 14:03:32 +08:00
|
|
|
const themeConfig = await this.http.api<ThemeConfig>("/theme", {
|
2024-11-30 02:15:46 +08:00
|
|
|
method: "GET",
|
|
|
|
});
|
2024-11-27 01:02:05 +08:00
|
|
|
this.currentTheme = themeConfig;
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Failed to initialize theme:", error);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public getThemeConfig(): ThemeConfig | undefined {
|
|
|
|
return this.currentTheme;
|
|
|
|
}
|
|
|
|
|
2024-11-30 02:15:46 +08:00
|
|
|
public async updateThemeConfig(
|
|
|
|
config: Partial<ThemeConfig>,
|
|
|
|
name: string,
|
|
|
|
): Promise<void> {
|
2024-11-27 01:02:05 +08:00
|
|
|
try {
|
2024-11-30 14:03:32 +08:00
|
|
|
const updatedConfig = await this.http.api<ThemeConfig>(`/theme/`, {
|
2024-11-30 02:15:46 +08:00
|
|
|
method: "PUT",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
2024-11-27 01:02:05 +08:00
|
|
|
},
|
2024-11-30 02:15:46 +08:00
|
|
|
body: JSON.stringify(config),
|
|
|
|
});
|
2024-11-27 01:02:05 +08:00
|
|
|
|
|
|
|
await this.loadTheme(updatedConfig);
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Failed to update theme configuration:", error);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|