siyuan/app/src/plugin/loader.ts

110 lines
3.6 KiB
TypeScript
Raw Normal View History

import {fetchPost} from "../util/fetch";
import {App} from "../index";
import {Plugin} from "./index";
2023-05-16 18:49:12 +08:00
/// #if !MOBILE
2023-05-16 10:31:01 +08:00
import {exportLayout} from "../layout/util";
2023-05-16 18:49:12 +08:00
/// #endif
import {API} from "./API";
const getObject = (key: string) => {
const api = {
siyuan: API
};
// @ts-ignore
return api[key];
2023-05-05 14:04:43 +08:00
};
const runCode = (code: string, sourceURL: string) => {
return window.eval("(function anonymous(require, module, exports){".concat(code, "\n})\n//# sourceURL=").concat(sourceURL, "\n"));
2023-05-05 14:04:43 +08:00
};
export const loadPlugins = (app: App) => {
fetchPost("/api/petal/loadPetals", {}, response => {
let css = "";
2023-05-16 10:31:01 +08:00
response.data.forEach((item: IPluginData) => {
loadPluginJS(app, item);
css += item.css || "" + "\n";
2023-05-05 14:04:43 +08:00
});
const styleElement = document.createElement("style");
styleElement.textContent = css;
document.head.append(styleElement);
2023-05-05 14:04:43 +08:00
});
};
2023-05-16 10:31:01 +08:00
const loadPluginJS = (app: App, item: IPluginData) => {
const exportsObj: { [key: string]: any } = {};
const moduleObj = {exports: exportsObj};
try {
runCode(item.js, "plugin:" + encodeURIComponent(item.name))(getObject, moduleObj, exportsObj);
} catch (e) {
console.error(`eval plugin ${item.name} error:`, e);
return;
}
const pluginClass = (moduleObj.exports || exportsObj).default || moduleObj.exports;
if (typeof pluginClass !== "function") {
console.error(`plugin ${item.name} has no export`);
return;
}
if (!(pluginClass.prototype instanceof Plugin)) {
console.error(`plugin ${item.name} does not extends Plugin`);
return;
}
const plugin = new pluginClass({
app,
name: item.name,
i18n: item.i18n
});
app.plugins.push(plugin);
try {
plugin.onload();
} catch (e) {
console.error(`plugin ${item.name} load error:`, e);
}
return plugin;
2023-05-16 18:51:13 +08:00
};
2023-05-16 10:31:01 +08:00
export const loadPlugin = (app: App, item: IPluginData) => {
const plugin = loadPluginJS(app, item);
Object.keys(plugin.docks).forEach(key => {
const dock = plugin.docks[key];
if (dock.config.position.startsWith("Left")) {
window.siyuan.layout.leftDock.genButton([{
type: key,
size: dock.config.size,
show: false,
icon: dock.config.icon,
title: dock.config.title,
hotkey: dock.config.hotkey
2023-05-16 18:51:13 +08:00
}], dock.config.position === "LeftBottom" ? 1 : 0, true);
2023-05-16 10:31:01 +08:00
} else if (dock.config.position.startsWith("Bottom")) {
window.siyuan.layout.bottomDock.genButton([{
type: key,
size: dock.config.size,
show: false,
icon: dock.config.icon,
title: dock.config.title,
hotkey: dock.config.hotkey
2023-05-16 18:51:13 +08:00
}], dock.config.position === "BottomRight" ? 1 : 0, true);
2023-05-16 10:31:01 +08:00
} else if (dock.config.position.startsWith("Right")) {
window.siyuan.layout.rightDock.genButton([{
type: key,
size: dock.config.size,
show: false,
icon: dock.config.icon,
title: dock.config.title,
hotkey: dock.config.hotkey
2023-05-16 18:51:13 +08:00
}], dock.config.position === "RightBottom" ? 1 : 0, true);
2023-05-16 10:31:01 +08:00
}
});
const styleElement = document.createElement("style");
styleElement.textContent = item.css;
document.head.append(styleElement);
2023-05-16 18:49:12 +08:00
/// #if !MOBILE
2023-05-16 10:31:01 +08:00
exportLayout({
reload: false,
onlyData: false,
errorExit: false
});
2023-05-16 18:49:12 +08:00
/// #endif
2023-05-16 10:31:01 +08:00
};