This commit is contained in:
parent
8ae3b354ab
commit
7b2b3c22be
3 changed files with 57 additions and 2 deletions
|
@ -29,8 +29,11 @@ import {getLocalStorage} from "./protyle/util/compatibility";
|
|||
import {updateEditModeElement} from "./layout/topBar";
|
||||
import {getSearch} from "./util/functions";
|
||||
import {hideAllElements} from "./protyle/ui/hideElements";
|
||||
import {loadPlugins} from "./plugin/loader";
|
||||
|
||||
export class App {
|
||||
public plugins: import("./plugin").Plugin[] = [];
|
||||
|
||||
class App {
|
||||
constructor() {
|
||||
/// #if BROWSER
|
||||
registerServiceWorker(`${Constants.SERVICE_WORKER_PATH}?v=${Constants.SIYUAN_VERSION}`);
|
||||
|
@ -139,6 +142,7 @@ class App {
|
|||
}
|
||||
}),
|
||||
};
|
||||
|
||||
fetchPost("/api/system/getConf", {}, response => {
|
||||
window.siyuan.config = response.data.conf;
|
||||
// 历史数据兼容,202306后可删除
|
||||
|
@ -164,6 +168,7 @@ class App {
|
|||
fetchPost("/api/setting/getCloudUser", {}, userResponse => {
|
||||
window.siyuan.user = userResponse.data;
|
||||
onGetConfig(response.data.start);
|
||||
loadPlugins(siyuanApp);
|
||||
account.onSetaccount();
|
||||
resizeDrag();
|
||||
setTitle(window.siyuan.languages.siyuanNote);
|
||||
|
@ -178,7 +183,7 @@ class App {
|
|||
}
|
||||
}
|
||||
|
||||
new App();
|
||||
const siyuanApp = new App();
|
||||
|
||||
window.openFileByURL = (openURL) => {
|
||||
if (openURL && isSYProtocol(openURL)) {
|
||||
|
|
14
app/src/plugin/index.ts
Normal file
14
app/src/plugin/index.ts
Normal file
|
@ -0,0 +1,14 @@
|
|||
export class Plugin {
|
||||
|
||||
constructor() {
|
||||
|
||||
}
|
||||
|
||||
getData () {
|
||||
|
||||
}
|
||||
|
||||
public onload() {
|
||||
console.log("Hello, world!")
|
||||
}
|
||||
}
|
36
app/src/plugin/loader.ts
Normal file
36
app/src/plugin/loader.ts
Normal file
|
@ -0,0 +1,36 @@
|
|||
import {fetchPost} from "../util/fetch";
|
||||
import {App} from "../index";
|
||||
import {Plugin} from "./index";
|
||||
|
||||
const getObject = (key: string) => {
|
||||
const api = {
|
||||
siyuan: {
|
||||
Plugin: Plugin
|
||||
}
|
||||
};
|
||||
// @ts-ignore
|
||||
return api[key];
|
||||
}
|
||||
|
||||
const runCode = (code: string, sourceURL: string) => {
|
||||
return window.eval("(function anonymous(require, module){".concat(code, "\n})\n//# sourceURL=").concat(sourceURL, "\n"))
|
||||
}
|
||||
|
||||
export const loadPlugins = (app: App) => {
|
||||
fetchPost("/api/plugin/loadPlugins", {}, response => {
|
||||
let css = "";
|
||||
response.data.forEach((item: { id: string, name: string, jsCode: string, cssCode: string, lang: IObject }) => {
|
||||
const moduleObj = {}
|
||||
const execResult = runCode(item.jsCode, "plugin:" + encodeURIComponent(item.id))
|
||||
execResult(getObject, moduleObj);
|
||||
// @ts-ignore
|
||||
const plugin: Plugin = new moduleObj.exports.default({app, id: item.id, lang: item.lang})
|
||||
app.plugins.push(plugin);
|
||||
plugin.onload();
|
||||
css += item.cssCode + "\n";
|
||||
})
|
||||
const styleElement = document.createElement("style");
|
||||
styleElement.textContent = css;
|
||||
document.head.append(styleElement);
|
||||
})
|
||||
}
|
Loading…
Add table
Reference in a new issue