mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-22 09:30:14 +01:00
✨ enable plugin
This commit is contained in:
parent
f3d1acd166
commit
85496345ef
4 changed files with 105 additions and 41 deletions
|
|
@ -2,6 +2,7 @@ import {fetchPost} from "../util/fetch";
|
|||
import {App} from "../index";
|
||||
import {Plugin} from "./index";
|
||||
import {API} from "./API";
|
||||
import {exportLayout} from "../layout/util";
|
||||
|
||||
const getObject = (key: string) => {
|
||||
const api = {
|
||||
|
|
@ -18,35 +19,8 @@ const runCode = (code: string, sourceURL: string) => {
|
|||
export const loadPlugins = (app: App) => {
|
||||
fetchPost("/api/petal/loadPetals", {}, response => {
|
||||
let css = "";
|
||||
response.data.forEach((item: { name: string, js: string, css: string, i18n: IObject }) => {
|
||||
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);
|
||||
}
|
||||
response.data.forEach((item: IPluginData) => {
|
||||
loadPluginJS(app, item);
|
||||
css += item.css || "" + "\n";
|
||||
});
|
||||
const styleElement = document.createElement("style");
|
||||
|
|
@ -54,3 +28,78 @@ export const loadPlugins = (app: App) => {
|
|||
document.head.append(styleElement);
|
||||
});
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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
|
||||
}], dock.config.position === "LeftBottom" ? 1 : 0, true)
|
||||
} 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
|
||||
}], dock.config.position === "BottomRight" ? 1 : 0, true)
|
||||
} 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
|
||||
}], dock.config.position === "RightBottom" ? 1 : 0, true)
|
||||
}
|
||||
});
|
||||
const styleElement = document.createElement("style");
|
||||
styleElement.textContent = item.css;
|
||||
document.head.append(styleElement);
|
||||
exportLayout({
|
||||
reload: false,
|
||||
onlyData: false,
|
||||
errorExit: false
|
||||
});
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue