mirror of
https://github.com/sean2077/obsidian-dynamic-theme-background.git
synced 2026-07-22 06:44:57 +00:00
Extract 6 core services from the monolithic plugin.ts (819→284 lines): - EventBus: unified typed event system replacing 3 ad-hoc notification mechanisms - TimeRuleScheduler: pure time-rule matching and scheduling logic - StyleManager: CSS variable injection, image analysis with size caching - BackgroundManager: lifecycle, timer management, and background selection - BackgroundPersistence: immutable vault file I/O (no longer mutates BackgroundItem) - Logger: centralized logging with consistent prefix Decompose settings-tab.ts (1417→147 lines) into 4 focused sections: - BasicSettingsSection, ModeSettingsSection, BgManagementSection, ApiSettingsSection Additional improvements: - Enable TypeScript strict mode - Pin all devDependency versions for reproducible builds - Template method pattern for provider updateImageCache/deinit/fetchPage - Move safeString() to BaseWallpaperApi, consolidate ID generation - Fix 26 bugs: deleteApi missing deinit, getImages infinite loop, settings-view memory leak, manual mode bounds check, statusBar DOM leak, test API ID collision, rule lookup by .name→.id, DragSort cleanup, perPage ordering, and more
49 lines
1.4 KiB
TypeScript
49 lines
1.4 KiB
TypeScript
/**
|
|
* @file src/utils.ts
|
|
* @description Utility functions for the Obsidian Dynamic Theme Background plugin.
|
|
*/
|
|
|
|
import { logger } from "../core/logger";
|
|
|
|
/**
|
|
* Converts a hexadecimal color string to an RGBA color string with the specified opacity.
|
|
*
|
|
* Supports both 3-digit and 6-digit hex color formats. If the input is invalid,
|
|
* returns a default RGBA color (`rgba(31, 30, 30, opacity)`).
|
|
*
|
|
* @param hex - The hexadecimal color string (e.g., "#fff" or "#ffffff").
|
|
* @param opacity - The opacity value for the RGBA color (between 0 and 1).
|
|
* @returns The RGBA color string representation.
|
|
*/
|
|
export function hexToRgba(hex: string, opacity: number): string {
|
|
// 移除 # 符号
|
|
hex = hex.replace("#", "");
|
|
|
|
// 处理3位和6位十六进制颜色
|
|
if (hex.length === 3) {
|
|
hex = hex
|
|
.split("")
|
|
.map((char) => char + char)
|
|
.join("");
|
|
}
|
|
|
|
if (hex.length !== 6) {
|
|
logger.warn("Invalid hex color format:", hex);
|
|
return `rgba(31, 30, 30, ${opacity})`;
|
|
}
|
|
|
|
const r = parseInt(hex.substring(0, 2), 16);
|
|
const g = parseInt(hex.substring(2, 4), 16);
|
|
const b = parseInt(hex.substring(4, 6), 16);
|
|
|
|
return `rgba(${r}, ${g}, ${b}, ${opacity})`;
|
|
}
|
|
|
|
/**
|
|
* 生成唯一 ID
|
|
* @param prefix 可选前缀
|
|
*/
|
|
export function generateId(prefix?: string): string {
|
|
const uuid = crypto.randomUUID();
|
|
return prefix ? `${prefix}-${uuid}` : uuid;
|
|
}
|