-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfeature-manager.js
More file actions
148 lines (116 loc) · 4.39 KB
/
Copy pathfeature-manager.js
File metadata and controls
148 lines (116 loc) · 4.39 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
class FeatureManager {
constructor() {
this.features = {
lunrSearch: { enabled: true, loaded: false },
articleFilter: { enabled: true, loaded: false },
readingProgress: { enabled: true, loaded: false, articlePagesOnly: true },
themeManager: { enabled: true, loaded: false },
breadcrumbNav: { enabled: true, loaded: false },
aiEnhancer: { enabled: true, loaded: false, articlePagesOnly: true },
knowledgeGraph: { enabled: true, loaded: false, navPageOnly: true },
};
this.init();
}
init() {
this.waitForDependencies();
}
waitForDependencies() {
const checkDependencies = setInterval(() => {
const allLoaded = this.checkAllDependencies();
if (allLoaded) {
clearInterval(checkDependencies);
this.initializeFeatures();
}
}, 100);
setTimeout(() => {
clearInterval(checkDependencies);
console.warn('部分功能加载超时');
}, 5000);
}
checkAllDependencies() {
const dependencies = {
lunrSearch: typeof LunrSearch !== 'undefined',
articleFilter: typeof ArticleFilter !== 'undefined',
readingProgress: typeof ReadingProgress !== 'undefined',
themeManager: typeof ThemeManager !== 'undefined',
breadcrumbNav: typeof BreadcrumbNav !== 'undefined',
aiEnhancer: typeof AIContentEnhancer !== 'undefined',
knowledgeGraph: typeof KnowledgeGraphVisualizer !== 'undefined',
};
Object.keys(dependencies).forEach((key) => {
this.features[key].loaded = dependencies[key];
});
return Object.values(dependencies).every((dep) => dep);
}
initializeFeatures() {
const currentPage = this.getCurrentPageType();
Object.keys(this.features).forEach((featureName) => {
const feature = this.features[featureName];
if (!feature.enabled || !feature.loaded) {
return;
}
if (feature.articlePagesOnly && currentPage !== 'article') {
return;
}
if (feature.navPageOnly && currentPage !== 'nav') {
return;
}
console.log(`初始化功能: ${featureName}`);
});
this.addFeatureStatusIndicator();
}
getCurrentPageType() {
const path = window.location.pathname;
if (path.includes('/blog/') && path.endsWith('.html')) {
return 'article';
}
if (path.includes('nav.html')) {
return 'nav';
}
if (path.includes('index.html') || path === '/') {
return 'home';
}
return 'other';
}
addFeatureStatusIndicator() {
const enabledFeatures = Object.keys(this.features).filter(
(key) => this.features[key].enabled && this.features[key].loaded
).length;
const totalFeatures = Object.keys(this.features).length;
console.log(`功能加载完成: ${enabledFeatures}/${totalFeatures}`);
if (enabledFeatures < totalFeatures) {
const missing = Object.keys(this.features)
.filter((key) => !this.features[key].loaded)
.join(', ');
console.warn(`未加载的功能: ${missing}`);
}
}
enableFeature(featureName) {
if (this.features[featureName]) {
this.features[featureName].enabled = true;
console.log(`已启用功能: ${featureName}`);
}
}
disableFeature(featureName) {
if (this.features[featureName]) {
this.features[featureName].enabled = false;
console.log(`已禁用功能: ${featureName}`);
}
}
getFeatureStatus(featureName) {
return this.features[featureName] || null;
}
getAllFeaturesStatus() {
return this.features;
}
}
const featureManager = new FeatureManager();
window.FeatureManagerAPI = {
enable: featureManager.enableFeature.bind(featureManager),
disable: featureManager.disableFeature.bind(featureManager),
getStatus: featureManager.getFeatureStatus.bind(featureManager),
getAllStatus: featureManager.getAllFeaturesStatus.bind(featureManager),
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = FeatureManager;
}