update at 2025-10-08 19:45:28

This commit is contained in:
douboer
2025-10-08 19:45:28 +08:00
parent 5d32c0f5e7
commit 3460669602
20 changed files with 3325 additions and 101 deletions

View File

@@ -42,6 +42,7 @@ export default class AssetsManager {
wasmPath: string;
expertSettings: ExpertSettings;
isLoaded: boolean = false;
private loadingPromise: Promise<void> | null = null; // 防止重复并发加载
private static instance: AssetsManager;
@@ -75,18 +76,42 @@ export default class AssetsManager {
}
async loadAssets() {
await this.loadThemes();
await this.loadHighlights();
await this.loadCustomCSS();
await this.loadExpertSettings();
this.isLoaded = true;
if (this.isLoaded) return;
if (this.loadingPromise) {
// 已经在加载中,复用同一个 promise
return this.loadingPromise;
}
console.time('[Assets] loadAssets');
this.loadingPromise = (async () => {
try {
// 并行加载互不依赖的资源,加速启动
await Promise.all([
this.loadThemes().catch(e => console.error('[Assets] loadThemes 失败', e)),
this.loadHighlights().catch(e => console.error('[Assets] loadHighlights 失败', e)),
this.loadCustomCSS().catch(e => console.error('[Assets] loadCustomCSS 失败', e)),
this.loadExpertSettings().catch(e => console.error('[Assets] loadExpertSettings 失败', e)),
]);
this.isLoaded = true;
console.log('[Assets] 资源加载完成', {
themeCount: this.themes?.length ?? 0,
highlightCount: this.highlights?.length ?? 0,
customCSS: this.customCSS?.length ?? 0
});
} finally {
console.timeEnd('[Assets] loadAssets');
this.loadingPromise = null;
}
})();
return this.loadingPromise;
}
async loadThemes() {
try {
console.log('[Assets] loadThemes:start');
if (!await this.app.vault.adapter.exists(this.themeCfg)) {
new Notice('主题资源未下载,请前往设置下载!');
this.themes = [this.defaultTheme];
console.log('[Assets] loadThemes:themes.json missing -> default only');
return;
}
const data = await this.app.vault.adapter.read(this.themeCfg);
@@ -94,6 +119,7 @@ export default class AssetsManager {
const themes = JSON.parse(data);
await this.loadCSS(themes);
this.themes = [this.defaultTheme, ... themes];
console.log('[Assets] loadThemes:done', { count: this.themes.length });
}
} catch (error) {
console.error(error);
@@ -103,13 +129,19 @@ export default class AssetsManager {
async loadCSS(themes: Theme[]) {
try {
for (const theme of themes) {
const cssFile = this.themesPath + theme.className + '.css';
const cssContent = await this.app.vault.adapter.read(cssFile);
if (cssContent) {
theme.css = cssContent;
}
}
await Promise.all(
themes.map(async (theme) => {
try {
const cssFile = this.themesPath + theme.className + '.css';
const cssContent = await this.app.vault.adapter.read(cssFile);
if (cssContent) {
theme.css = cssContent;
}
} catch (e) {
console.warn('[Assets] 读取主题 CSS 失败', theme.className, e);
}
})
);
} catch (error) {
console.error(error);
new Notice('读取CSS失败');
@@ -118,6 +150,7 @@ export default class AssetsManager {
async loadCustomCSS() {
try {
console.log('[Assets] loadCustomCSS:start');
const customCSSNote = NMPSettings.getInstance().customCSSNote;
if (customCSSNote != '') {
const file = this.searchFile(customCSSNote);
@@ -141,6 +174,7 @@ export default class AssetsManager {
if (cssContent) {
this.customCSS = cssContent;
}
console.log('[Assets] loadCustomCSS:done', { hasContent: this.customCSS.length > 0 });
} catch (error) {
console.error(error);
new Notice('读取CSS失败');
@@ -149,6 +183,7 @@ export default class AssetsManager {
async loadExpertSettings() {
try {
console.log('[Assets] loadExpertSettings:start');
const note = NMPSettings.getInstance().expertSettingsNote;
if (note != '') {
const file = this.searchFile(note);
@@ -170,6 +205,7 @@ export default class AssetsManager {
else {
this.expertSettings = defaultExpertSettings;
}
console.log('[Assets] loadExpertSettings:done');
} catch (error) {
console.error(error);
new Notice('读取专家设置失败!');
@@ -178,10 +214,12 @@ export default class AssetsManager {
async loadHighlights() {
try {
console.log('[Assets] loadHighlights:start');
const defaultHighlight = {name: '默认', url: '', css: DefaultHighlight};
this.highlights = [defaultHighlight];
if (!await this.app.vault.adapter.exists(this.hilightCfg)) {
new Notice('高亮资源未下载,请前往设置下载!');
console.log('[Assets] loadHighlights:highlights.json missing -> default only');
return;
}
@@ -193,6 +231,7 @@ export default class AssetsManager {
const cssContent = await this.app.vault.adapter.read(cssFile);
this.highlights.push({name: item.name, url: item.url, css: cssContent});
}
console.log('[Assets] loadHighlights:done', { count: this.highlights.length });
}
}
catch (error) {
@@ -234,6 +273,10 @@ export default class AssetsManager {
return theme;
}
}
// 找不到主题时返回第一个主题(默认主题)
console.warn(`[Assets] 主题 "${themeName}" 未找到,使用默认主题`);
return this.themes[0];
}
getHighlight(highlightName: string) {
@@ -246,6 +289,10 @@ export default class AssetsManager {
return highlight;
}
}
// 找不到高亮时返回第一个高亮(默认高亮)
console.warn(`[Assets] 高亮 "${highlightName}" 未找到,使用默认高亮`);
return this.highlights[0];
}
getThemeURL() {