Skip to content

getActiveMentionQuery

光标前解析活跃 @ 提及查询(纯文本,与 TipTap getTextBetween 对齐)。

函数签名

typescript
function getActiveMentionQuery(
  text: string,
  cursorPos: number,
): { atIndex: number; filter: string } | null

参数

参数名类型必填说明
textstring编辑器纯文本全文
cursorPosnumber光标位置(0-based,含当前字符左侧)

返回值

类型说明
{ atIndex, filter }找到活跃 @atIndex@ 下标,filter@ 后已输入的过滤串
null光标前无活跃 @(无 @、已有空白/换行、或 @ 前非空白边界)

工作原理

  1. text.slice(0, cursorPos) 作为光标前文本。
  2. 自右向左查找 @;若 @ 前为非空白字符则继续向左(避免邮箱等误判)。
  3. @ 后若含换行或空白,视为提及已结束,继续查找更早的 @
  4. 找到合法 @ 后返回其下标与 @ 后的过滤字符串(不含 @ 本身)。