cherry-markdown icon indicating copy to clipboard operation
cherry-markdown copied to clipboard

[Bug Report] 自定义菜单onClick为async函数时报错

Open joker585 opened this issue 1 year ago • 1 comments

Prerequisites

Describe the Bug

const onCancel = async () => {
    await onSaveDraft()
    setEdit(false)
  }
 const goBackMenu = Cherry.createMenuHook(t('返回'), {
      noIcon: true,
      onClick: onCancel,
    })

当我定义了一个类似的按钮时, 点击此按钮控制台会抛出如下错误, 如果我把onCancel改成普通的箭头函数之后这个错误就会消失

cherry-markdown.core.js:1 Uncaught TypeError: e.split is not a function at Ie (cherry-markdown.core.js:1:53865) ... ...

猜测是这里调用this.onClick没有进行await导致的

  /**
   * 处理菜单项点击事件
   * @param {MouseEvent | KeyboardEvent | undefined} [event] 点击事件
   * @returns {void}
   */
  fire(event, shortKey = '') {
    event?.stopPropagation();
    if (typeof this.onClick === 'function') {
      const selections = this.editor.editor.getSelections();
      // 判断是不是多选
      this.isSelections = selections.length > 1;
      // 当onClick返回null、undefined、false时,维持原样
      const ret = selections.map(
        (selection, index, srcArray) => this.onClick(selection, shortKey, event) || srcArray[index],
      );

      if (!this.bubbleMenu && this.updateMarkdown) {
        // 非下拉菜单按钮保留selection
        this.editor.editor.replaceSelections(ret, 'around');
        this.editor.editor.focus();
        this.$afterClick();
      }
    }
  }

Reproduction Steps

No response

System Information

No response

Contributing

None

joker585 avatar Feb 21 '24 13:02 joker585

📦 cherry-markdown dev preview published ⚠️ 注意: 此版本为开发预览版,禁止在生产环境使用! ⚠️ Note: This version is a developer preview and should not be used in production environments!

Install NPM with :

npm install @cherry-markdown/[email protected]

github-actions[bot] avatar Apr 09 '25 08:04 github-actions[bot]