You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

实现点击Lexical的MentionNode时触发提及菜单并替换内容

基于Lexical提及插件的功能扩展需求与实现疑问

以Lexical文档中的提及插件为基础,我希望为其添加以下功能:

  • 点击已有的MentionNode时,渲染包含完整选项列表的提及菜单(和调用menuRenderFunction时的效果一致),不受queryString匹配限制;
  • 选择菜单中的选项时,用新选中的内容替换原提及内容。

请问能否在保留LexicalTypeaheadMenuPlugin对菜单控制权的前提下实现该功能?

已尝试的方案

我曾设想通过triggerFn返回正确的QueryMatch来实现需求,代码示例如下:

const x: FC = () => {
  const nodeAtSelection = useNodeAtSelection() // 返回选中位置的LexicalNode

  return (
    <LexicalTypeaheadMenuPlugin<VariableTypeaheadOption>
      triggerFn={(text, editor) => {
        if ($isMentionsNode(nodeAtSelection)) {
          // 不知道如何实现`getQueryMatchForMentionsNode`,也不确定是否可行
          return getQueryMatchForMentionsNode(nodeAtSelection, text, editor)
        }

        return checkForVariableBeforeCaret(text, editor)
      }}
    />
  )
}

我尝试了约半小时,但找不到triggerFn或QueryMatch的相关文档,因此未取得进展。此外我还想到一个可行但较为hack的方案,后续会将其作为答案发布。

内容的提问来源于stack exchange,提问作者Michal Kurz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 07:10:27