Vanessa 2025-12-02 11:00:13 +08:00
parent bcdef64d0f
commit 4433372bee
3 changed files with 19 additions and 34 deletions

View file

@ -16,7 +16,8 @@ import {BlockPanel} from "../block/Panel";
import {Setting} from "./Setting"; import {Setting} from "./Setting";
import {clearOBG} from "../layout/dock/util"; import {clearOBG} from "../layout/dock/util";
import {Constants} from "../constants"; import {Constants} from "../constants";
import {restartPlugin} from "./loader"; import {uninstall} from "./uninstall";
import {afterLoadPlugin, loadPlugins} from "./loader";
export class Plugin { export class Plugin {
private app: App; private app: App;
@ -117,7 +118,10 @@ export class Plugin {
public onDataChanged() { public onDataChanged() {
// 存储数据变更 // 存储数据变更
// 兼容 3.4.1 以前同步数据使用重载插件的问题 // 兼容 3.4.1 以前同步数据使用重载插件的问题
restartPlugin(this.app, this); uninstall(this.app, this.name, false);
loadPlugins(this.app, [this.name]).then(() => {
afterLoadPlugin(this);
});
} }
public async updateCards(options: ICardData) { public async updateCards(options: ICardData) {

View file

@ -30,12 +30,10 @@ const runCode = (code: string, sourceURL: string) => {
export const loadPlugins = async (app: App, names?: string[]) => { export const loadPlugins = async (app: App, names?: string[]) => {
const response = await fetchSyncPost("/api/petal/loadPetals", {frontend: getFrontend()}); const response = await fetchSyncPost("/api/petal/loadPetals", {frontend: getFrontend()});
const pluginsStyle = getPluginsStyle(); const pluginsStyle = getPluginsStyle();
const pluginsToLoad = !names response.data.forEach((item: IPluginData) => {
? response.data if (!names || (names && names.includes(item.name))) {
: response.data.filter((item: IPluginData) => names.includes(item.name)); loadPluginJS(app, item);
pluginsToLoad.forEach((item: IPluginData) => { }
// 为加快启动速度,不进行 await
loadPluginJS(app, item);
insertPluginCSS(item, pluginsStyle); insertPluginCSS(item, pluginsStyle);
}); });
}; };
@ -237,31 +235,16 @@ export const reloadPlugin = async (app: App, data: {
} }
}); });
}); });
// 先收集需要处理的插件,避免在遍历过程中修改数组导致重复执行 app.plugins.forEach(item => {
const dataChangedPlugins = app.plugins.filter(item => upsertDataPlugins.includes(item.name)); if (upsertDataPlugins.includes(item.name)) {
dataChangedPlugins.forEach(item => { try {
try { item.onDataChanged();
item.onDataChanged(); } catch (e) {
} catch (e) { console.error(`plugin ${item.name} onDataChanged error:`, e);
console.error(`plugin ${item.name} onDataChanged error:`, e); }
} }
}); });
/// #if !MOBILE /// #if !MOBILE
saveLayout(); saveLayout();
/// #endif /// #endif
}; };
// 重启插件
export const restartPlugin = async (app: App, plugin: Plugin) => {
uninstall(app, plugin.name, false, true);
app.plugins.push(plugin);
try {
await plugin.onload();
} catch (e) {
console.error(`plugin ${plugin.name} onload error:`, e);
}
afterLoadPlugin(plugin);
getAllEditor().forEach(editor => {
editor.protyle.toolbar.update(editor.protyle);
});
};

View file

@ -8,7 +8,7 @@ import {Constants} from "../constants";
import {setStorageVal} from "../protyle/util/compatibility"; import {setStorageVal} from "../protyle/util/compatibility";
import {getAllEditor} from "../layout/getAll"; import {getAllEditor} from "../layout/getAll";
export const uninstall = (app: App, name: string, isUninstall: boolean, keepCSS: boolean = false) => { export const uninstall = (app: App, name: string, isUninstall: boolean) => {
app.plugins.find((plugin: Plugin, index) => { app.plugins.find((plugin: Plugin, index) => {
if (plugin.name === name) { if (plugin.name === name) {
try { try {
@ -75,9 +75,7 @@ export const uninstall = (app: App, name: string, isUninstall: boolean, keepCSS:
editor.protyle.toolbar.update(editor.protyle); editor.protyle.toolbar.update(editor.protyle);
}); });
// rm style // rm style
if (!keepCSS) { document.getElementById("pluginsStyle" + name)?.remove();
document.getElementById("pluginsStyle" + name)?.remove();
}
return true; return true;
} }
}); });