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

View file

@ -54,6 +54,9 @@ export class App {
id: genUUID(), id: genUUID(),
type: "main", type: "main",
msgCallback: (data) => { msgCallback: (data) => {
this.plugins.forEach((plugin) => {
plugin.eventBus.emit("ws-main", data);
});
if (data) { if (data) {
switch (data.cmd) { switch (data.cmd) {
case "syncMergeResult": case "syncMergeResult":
@ -167,8 +170,8 @@ export class App {
bootSync(); bootSync();
fetchPost("/api/setting/getCloudUser", {}, userResponse => { fetchPost("/api/setting/getCloudUser", {}, userResponse => {
window.siyuan.user = userResponse.data; window.siyuan.user = userResponse.data;
onGetConfig(response.data.start);
loadPlugins(siyuanApp); loadPlugins(siyuanApp);
onGetConfig(response.data.start);
account.onSetaccount(); account.onSetaccount();
resizeDrag(); resizeDrag();
setTitle(window.siyuan.languages.siyuanNote); setTitle(window.siyuan.languages.siyuanNote);

View file

@ -22,8 +22,11 @@ import {getSearch} from "../util/functions";
import {initRightMenu} from "./menu"; import {initRightMenu} from "./menu";
import {openChangelog} from "../boot/openChangelog"; import {openChangelog} from "../boot/openChangelog";
import {registerServiceWorker} from "../util/serviceWorker"; import {registerServiceWorker} from "../util/serviceWorker";
import {loadPlugins} from "../plugin/loader";
class App { class App {
public plugins: import("../plugin").Plugin[] = [];
constructor() { constructor() {
if (!window.webkit?.messageHandlers && !window.JSAndroid) { if (!window.webkit?.messageHandlers && !window.JSAndroid) {
registerServiceWorker(`${Constants.SERVICE_WORKER_PATH}?v=${Constants.SIYUAN_VERSION}`); registerServiceWorker(`${Constants.SERVICE_WORKER_PATH}?v=${Constants.SIYUAN_VERSION}`);
@ -41,7 +44,10 @@ class App {
ws: new Model({ ws: new Model({
id: genUUID(), id: genUUID(),
type: "main", type: "main",
msgCallback(data) { msgCallback: (data)=> {
this.plugins.forEach((plugin) => {
plugin.eventBus.emit("ws-main", data);
});
onMessage(data); onMessage(data);
} }
}) })
@ -66,6 +72,7 @@ class App {
initAssets(); initAssets();
fetchPost("/api/setting/getCloudUser", {}, userResponse => { fetchPost("/api/setting/getCloudUser", {}, userResponse => {
window.siyuan.user = userResponse.data; window.siyuan.user = userResponse.data;
loadPlugins(siyuanApp);
fetchPost("/api/system/getEmojiConf", {}, emojiResponse => { fetchPost("/api/system/getEmojiConf", {}, emojiResponse => {
window.siyuan.emojis = emojiResponse.data as IEmoji[]; window.siyuan.emojis = emojiResponse.data as IEmoji[];
initFramework(); initFramework();
@ -85,7 +92,7 @@ class App {
} }
} }
new App(); const siyuanApp = new App();
window.goBack = goBack; window.goBack = goBack;
window.showKeyboardToolbar = (height) => { window.showKeyboardToolbar = (height) => {

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 {App} from "../index";
import {EventBus} from "./EventBus";
export class Plugin { export class Plugin {
public i18n: IObject; public i18n: IObject;
public eventBus: EventBus;
constructor(options: { constructor(options: {
app: App, app: App,
id: string, id: string,
name: string,
i18n: IObject i18n: IObject
}) { }) {
this.i18n = options.i18n; this.i18n = options.i18n;
this.eventBus = new EventBus(options.name);
} }
public getData() { public getData() {

View file

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

View file

@ -31,6 +31,8 @@ type TOperation =
| "removeFlashcards" | "removeFlashcards"
type TBazaarType = "templates" | "icons" | "widgets" | "themes" | "plugins" type TBazaarType = "templates" | "icons" | "widgets" | "themes" | "plugins"
type TCardType = "doc" | "notebook" | "all" type TCardType = "doc" | "notebook" | "all"
type TEventBus = "ws-main"
declare module "blueimp-md5" declare module "blueimp-md5"
interface Window { interface Window {

View file

@ -21,8 +21,11 @@ import {getAllTabs} from "../layout/getAll";
import {getLocalStorage} from "../protyle/util/compatibility"; import {getLocalStorage} from "../protyle/util/compatibility";
import {init} from "../window/init"; import {init} from "../window/init";
import {positionPDF, switchTabById} from "./global/function"; import {positionPDF, switchTabById} from "./global/function";
import {loadPlugins} from "../plugin/loader";
class App { class App {
public plugins: import("../plugin").Plugin[] = [];
constructor() { constructor() {
addScriptSync(`${Constants.PROTYLE_CDN}/js/lute/lute.min.js?v=${Constants.SIYUAN_VERSION}`, "protyleLuteScript"); addScriptSync(`${Constants.PROTYLE_CDN}/js/lute/lute.min.js?v=${Constants.SIYUAN_VERSION}`, "protyleLuteScript");
addScript(`${Constants.PROTYLE_CDN}/js/protyle-html.js?v=${Constants.SIYUAN_VERSION}`, "protyleWcHtmlScript"); addScript(`${Constants.PROTYLE_CDN}/js/protyle-html.js?v=${Constants.SIYUAN_VERSION}`, "protyleWcHtmlScript");
@ -40,6 +43,9 @@ class App {
id: genUUID(), id: genUUID(),
type: "main", type: "main",
msgCallback: (data) => { msgCallback: (data) => {
this.plugins.forEach((plugin) => {
plugin.eventBus.emit("ws-main", data);
});
if (data) { if (data) {
switch (data.cmd) { switch (data.cmd) {
case "syncMergeResult": case "syncMergeResult":
@ -128,6 +134,7 @@ class App {
window.siyuan.menus = new Menus(); window.siyuan.menus = new Menus();
fetchPost("/api/setting/getCloudUser", {}, userResponse => { fetchPost("/api/setting/getCloudUser", {}, userResponse => {
window.siyuan.user = userResponse.data; window.siyuan.user = userResponse.data;
loadPlugins(siyuanApp);
init(); init();
setTitle(window.siyuan.languages.siyuanNote); setTitle(window.siyuan.languages.siyuanNote);
initMessage(); initMessage();
@ -141,7 +148,7 @@ class App {
} }
} }
new App(); const siyuanApp = new App();
// 再次点击新窗口已打开的 PDF 时,需进行定位 // 再次点击新窗口已打开的 PDF 时,需进行定位
window.newWindow = { window.newWindow = {