Vanessa 2023-05-07 17:31:50 +08:00
parent 9bbfd2a3e4
commit 8fe520f4af
8 changed files with 67 additions and 8 deletions

9
app/src/plugin/API.ts Normal file
View file

@ -0,0 +1,9 @@
import {confirmDialog} from "../dialog/confirmDialog";
import {Plugin} from "./index";
import {showMessage} from "../dialog/message";
export const API = {
Plugin: Plugin,
Confirm: confirmDialog,
Message: showMessage
};

View file

@ -0,0 +1,23 @@
export class EventBus<DetailType = any> {
private eventTarget: EventTarget;
constructor(name = "") {
this.eventTarget = document.appendChild(document.createComment(name));
}
on(type: TEventBus, listener: (event: CustomEvent<DetailType>) => void) {
this.eventTarget.addEventListener(type, listener);
}
once(type: TEventBus, listener: (event: CustomEvent<DetailType>) => void) {
this.eventTarget.addEventListener(type, listener, {once: true});
}
off(type: TEventBus, listener: (event: CustomEvent<DetailType>) => void) {
this.eventTarget.removeEventListener(type, listener);
}
emit(type: TEventBus, detail?: DetailType) {
return this.eventTarget.dispatchEvent(new CustomEvent(type, {detail}));
}
}

View file

@ -1,14 +1,18 @@
import {App} from "../index";
import {EventBus} from "./EventBus";
export class Plugin {
public i18n: IObject;
public eventBus: EventBus;
constructor(options: {
app: App,
id: string,
name: string,
i18n: IObject
}) {
this.i18n = options.i18n;
this.eventBus = new EventBus(options.name);
}
public getData() {

View file

@ -1,12 +1,11 @@
import {fetchPost} from "../util/fetch";
import {App} from "../index";
import {Plugin} from "./index";
import {API} from "./API";
const getObject = (key: string) => {
const api = {
siyuan: {
Plugin: Plugin
}
siyuan: API
};
// @ts-ignore
return api[key];
@ -37,7 +36,12 @@ export const loadPlugins = (app: App) => {
console.error(`plugin ${item.name} does not extends Plugin`);
return;
}
const plugin = new pluginClass({app, id: item.id, i18n: item.i18n});
const plugin = new pluginClass({
app,
name: item.name,
id: item.id,
i18n: item.i18n
});
app.plugins.push(plugin);
plugin.onload();
css += item.css || "" + "\n";