Vanessa 2025-03-20 18:57:40 +08:00
parent 96f17b46c4
commit b8bf4401bb
2 changed files with 13 additions and 6 deletions

View file

@ -29,19 +29,21 @@ const runCode = (code: string, sourceURL: string) => {
export const loadPlugins = async (app: App, names?: string[]) => {
const response = await fetchSyncPost("/api/petal/loadPetals", {frontend: getFrontend()});
let css = "";
let css = '<style id="pluginsStyle"></style>'; // 用于将内联样式插入到插件样式前的标识
// 为加快启动速度,不进行 await
response.data.forEach((item: IPluginData) => {
if (!names || (names && names.includes(item.name))) {
loadPluginJS(app, item);
}
css += item.css || "" + "\n";
if (item.css) {
css += `<style id="pluginsStyle${item.name}">${item.css}</style>`;
}
});
const pluginsStyle = document.getElementById("pluginsStyle");
if (pluginsStyle) {
pluginsStyle.innerHTML = css;
} else {
document.head.insertAdjacentHTML("beforeend", `<style id="pluginsStyle">${css}</style>`);
document.head.insertAdjacentHTML("beforeend", css);
}
};
@ -81,9 +83,12 @@ const loadPluginJS = async (app: App, item: IPluginData) => {
// 启用插件
export const loadPlugin = async (app: App, item: IPluginData) => {
const plugin = await loadPluginJS(app, item);
const styleElement = document.createElement("style");
styleElement.textContent = item.css;
document.head.append(styleElement);
if (item.css) {
const styleElement = document.createElement("style");
styleElement.id = "pluginsStyle" + item.name;
styleElement.textContent = item.css;
document.head.append(styleElement);
}
afterLoadPlugin(plugin);
saveLayout();
getAllEditor().forEach(editor => {

View file

@ -66,6 +66,8 @@ export const uninstall = (app: App, name: string, isUninstall = false) => {
getAllEditor().forEach(editor => {
editor.protyle.toolbar.update(editor.protyle);
});
// rm style
document.getElementById("pluginsStyle" + name)?.remove();
return true;
}
});