Skip to content

插件使用 addItem 方法之前执行异步操作会导致选项无法被添加 #15676

@TCOTC

Description

@TCOTC

我的插件需要 await fetchSyncPost 获取数据之后才能判断是否应该添加选项,但是只要使用了异步操作就无法正常 addItem

import {
    Plugin,
    Menu,
} from "siyuan";

export default class PluginSample extends Plugin {
    async onload() {
        this.eventBus.on('open-menu-doctree', this.openMenuDoctree);
    }

    openMenuDoctree = async (event: CustomEvent) => {
        const type = event.detail.type;
        const menu = event.detail.menu as Menu;

        if (type === "doc") {
            console.log("单个文档", menu);
            // 异步操作
            await new Promise(resolve => setTimeout(resolve, 1));

            menu.addItem({
                id: "test",
                label: "测试",
                click: () => {
                    console.log("点击");
                }
            });
        }
    };
}
video.webm

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions