Tencent / Tencent/cherry-markdown
[Bug Report] 自定义菜单onClick为async函数时报错
Open
Nobody has claimed this yet.
🐞bug
- Dominant language
- JavaScript
- Stars
- 4.9k
- Forks
- 572
- Avg merge
- 17h 5m
- Merged PRs (30d)
- 14
Description
Prerequisites
- There isn't an existing issue that reports the same bug to avoid duplicates.
- The provided information offers a minimal reproducible example of the bug.
- For Q&A, please open a GitHub Discussion instead.
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the fire method shown in the issue and the createMenuHook call; trace how the callback result is consumed by replaceSelections. Reproduce the async onClick case in the editor, then verify that custom menu callbacks no longer produce the reported e.split error while ordinary callbacks retain their behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- frontend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100