mirror of
https://github.com/siyuan-note/siyuan.git
synced 2026-02-10 17:24:21 +01:00
This commit is contained in:
parent
9bbfd2a3e4
commit
8fe520f4af
8 changed files with 67 additions and 8 deletions
9
app/src/plugin/API.ts
Normal file
9
app/src/plugin/API.ts
Normal 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
|
||||
};
|
||||
23
app/src/plugin/EventBus.ts
Normal file
23
app/src/plugin/EventBus.ts
Normal 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}));
|
||||
}
|
||||
}
|
||||
|
|
@ -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() {
|
||||
|
|
|
|||
|
|
@ -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";
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue