forked from mxx1111/mdlook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththemeInjector.ts
More file actions
55 lines (49 loc) · 1.1 KB
/
Copy paththemeInjector.ts
File metadata and controls
55 lines (49 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* 主题样式注入器
* 负责管理动态注入的 <style> 标签
*/
/**
* 主题样式注入器类
*/
export class ThemeInjector {
private styleElement: HTMLStyleElement | null = null
private readonly styleId = `md-theme`
/**
* 注入或更新主题样式
* @param cssContent - CSS 内容
*/
inject(cssContent: string): void {
if (!this.styleElement) {
this.styleElement = document.createElement(`style`)
this.styleElement.id = this.styleId
document.head.appendChild(this.styleElement)
}
this.styleElement.textContent = cssContent
}
/**
* 移除主题样式
*/
remove(): void {
if (this.styleElement) {
this.styleElement.remove()
this.styleElement = null
}
}
/**
* 检查是否已注入
*/
isInjected(): boolean {
return this.styleElement !== null
}
}
// 单例模式
let injectorInstance: ThemeInjector | null = null
/**
* 获取主题注入器单例
*/
export function getThemeInjector(): ThemeInjector {
if (!injectorInstance) {
injectorInstance = new ThemeInjector()
}
return injectorInstance
}