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 {clearOBG} from "../layout/dock/util";
import {Constants} from "../constants";
import {restartPlugin} from "./loader";
import {uninstall} from "./uninstall";
import {afterLoadPlugin, loadPlugins} from "./loader";
export class Plugin {
private app: App;
@ -117,7 +118,10 @@ export class Plugin {
public onDataChanged() {
// 存储数据变更
// 兼容 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) {

View file

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