不用插件:绑定思源事件总线(eventBus)

简洁版(不推荐,有风险)

优点:代码简洁。

缺点:要求用户至少安装一个插件。

⚠️ 风险警告:当用户关闭第一个插件时,之前的绑定会失效。

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;
}

改进版

优点:兼容性好。

缺点:代码量大。

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;
            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 {}; } // 返回空对象或其他默认值
        onLayoutReady() {}
        addCommand(command) {}
        addIcons(svg) {}
        addTopBar(options) { return {}; } // 模拟返回一个空元素对象
        addStatusBar(options) { return {}; } // 模拟返回一个空元素对象
        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 []; } // 返回空数组
        set protyleOptions(options) {}
        get protyleOptions() { return this.protyleOptionsValue; }
    }
    myPlugin = new Plugin({name:pluginName});
    window.siyuan.ws.app.plugins.push(myPlugin);
    return myPlugin;
}

image.png

留下你的脚步
推荐阅读