不多说了,用的人都懂。
简洁版 (不推荐,有风险)
优点:代码简洁。
缺点:要求用户至少安装一个插件。
⚠️ 风险警告:当用户关闭第一个插件时,之前的绑定会失效。
function eventBusOn(eventName, callback) {
const plugin = window.siyuan.ws.app.plugins[0];
if(!plugin) {
console.log('绑定事件'+eventName+'失败,请至少安装一个插件');
return false;
}
plugin.eventBus.on(eventName, callback);
return true;
}
function eventBusOff(eventName, callback) {
const plugin = window.siyuan.ws.app.plugins[0];
if(!plugin) {
console.log('解绑事件'+eventName+'失败,请至少安装一个插件');
return false;
}
plugin.eventBus.off(eventName, callback);
return true;
}
改进版
优点:兼容性好。
缺点:代码量大。
// 参考 https://github.com/siyuan-note/siyuan/blob/f660b2ddfa98f93f778bbbbe5abc3bfaa5a932b6/app/src/plugin/index.ts
function eventBusOn(eventName, callback) {
const plugin = getMyPlugin();
plugin.eventBus.on(eventName, callback);
}
function eventBusOff(eventName, callback) {
const plugin = getMyPlugin();
plugin.eventBus.off(eventName, callback);
}
function getMyPlugin(pluginName = "my-custom-plugin") {
let myPlugin = window.siyuan.ws.app.plugins.find(item=>item.name === pluginName);
if(myPlugin) return myPlugin;
class EventBus {
constructor(name = "") {
this.eventTarget = document.createComment(name);
document.appendChild(this.eventTarget);
}
on(type, listener) {
this.eventTarget.addEventListener(type, listener);
}
once(type, listener) {
this.eventTarget.addEventListener(type, listener, { once: true });
}
off(type, listener) {
this.eventTarget.removeEventListener(type, listener);
}
emit(type, detail) {
return this.eventTarget.dispatchEvent(new CustomEvent(type, { detail, cancelable: true }));
}
}
class Plugin {
constructor(options) {
this.app = options.app||window.siyuan.ws.app.appId;
this.i18n = options.i18n;
this.displayName = options.displayName||options.name;
this.name = options.name;
this.eventBus = new EventBus(options.name);
this.protyleSlash = [];
this.customBlockRenders = {};
this.topBarIcons = [];
this.statusBarIcons = [];
this.commands = [];
this.models = {};
this.docks = {};
this.data = {};
this.protyleOptionsValue = null;
}
onload() {}
onunload() {}
uninstall() {}
async updateCards(options) { return options; } // 返回选项本身
onLayoutReady() {}
addCommand(command) {}
addIcons(svg) {}
addTopBar(options) { return null; } // 模拟返回null
addStatusBar(options) { return null; } // 模拟返回null
// 去掉设置,参考 https://github.com/siyuan-note/siyuan/blob/dae6158860cc704e353454565c96e874278c6f47/app/src/plugin/openTopBarMenu.ts#L25
//openSetting() {}
loadData(storageName) { return Promise.resolve(null); }
saveData(storageName, data) { return Promise.resolve(); }
removeData(storageName) { return Promise.resolve(); }
getOpenedTab() { return {}; } // 返回空对象
addTab(options) { return () => {}; } // 返回空函数模拟模型
addDock(options) { return {}; } // 返回空对象模拟 dock
addFloatLayer(options) {}
updateProtyleToolbar(toolbar) { return toolbar; } // 返回toolbar本身
set protyleOptions(options) {}
get protyleOptions() { return this.protyleOptionsValue; }
}
myPlugin = new Plugin({name:pluginName});
window.siyuan.ws.app.plugins.push(myPlugin);
return myPlugin;
}
已封装到 openAny 中。
[js] 连续点击 openAny,小代码,大作用,让一切触手可达
在 openAny 中调用方法示例:
const handler = (args)=>console.log(args);
openAny.on('open-menu-link', handler);
openAny.once('open-menu-link', handler);
openAny.off('open-menu-link', handler);
openAny.emit('your event name', data);
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于