iter(g132): 新增 @提及输入 mentions

This commit is contained in:
HIsheR
2026-10-26 15:22:59 +08:00
parent eac02f0c80
commit 9d0c195beb
12 changed files with 382 additions and 10 deletions
+1
View File
@@ -63,3 +63,4 @@ export { calendar } from './calendar.js';
export { typography } from './typography.js';
export { grid } from './grid.js';
export { floatButton } from './floatButton.js';
export { mentions } from './mentions.js';
+114
View File
@@ -0,0 +1,114 @@
import { effect, h, signal } from '@ui-arch/frame';
import type { MentionItem, MentionsProps } from './types.js';
// 每个实例的 listbox id 前缀,保证 aria-activedescendant 唯一
let seq = 0;
/*** 取光标前的 @ 触发词(不含 @ 本身):没有未闭合的 @ 片段时返回 null ***/
function tokenBefore(text: string, caret: number): string | null {
const match = /@([^\s@]*)$/.exec(text.slice(0, caret));
return match ? match[1] : null;
}
/*** 用选中的名字替换光标前的 @片段,返回替换后的全文与新光标位置 ***/
function applyPick(text: string, caret: number, name: string): { text: string; caret: number } {
const head = text.slice(0, caret).replace(/@[^\s@]*$/, `@${name} `);
return { text: head + text.slice(caret), caret: head.length };
}
/*** @提及输入:textarea 内键入 @ 弹候选,↑↓ 环形移动、Enter/Tab 选中、Esc 只收本层;弹层贴输入框下沿,选中按光标替换 @片段 ***/
export function mentions(props: MentionsProps): HTMLElement {
const uid = `ua-mentions-${++seq}`;
const items = props.items;
const value = props.value ?? signal('');
const open = signal(false); // 弹层开合
const query = signal(''); // 当前 @ 后的过滤词
const active = signal(0); // 高亮项下标
const input = h('textarea', {
class: 'ua-mentions-input',
rows: String(props.rows ?? 3),
placeholder: props.placeholder ?? '',
'aria-autocomplete': 'list',
'aria-controls': uid,
'aria-expanded': 'false',
}) as HTMLTextAreaElement;
input.value = value();
const list = h('div', { class: 'ua-mentions-pop', role: 'listbox', id: uid });
const wrap = h('div', { class: 'ua-mentions' }, input, list);
// 候选:空过滤词给全量,否则按名字 / 关键词做包含匹配
const filtered = (): MentionItem[] => (query()
? items.filter((it) => it.name.includes(query()) || (it.keywords ?? []).some((k) => k.includes(query())))
: items);
/*** 重绘弹层:同步显隐、高亮项与 aria-activedescendant(无候选时不开层) ***/
const paint = (): void => {
const shown = filtered();
const visible = open() && shown.length > 0;
wrap.classList.toggle('is-open', visible);
input.setAttribute('aria-expanded', String(visible));
list.replaceChildren(...shown.map((item, i) => h('div', {
class: `ua-mentions-item${i === active() ? ' is-active' : ''}`,
role: 'option',
id: `${uid}-${i}`,
'aria-selected': i === active() ? 'true' : 'false',
onmousedown: (e: Event) => { e.preventDefault(); pick(item); }, // 阻止默认让 textarea 不失焦
}, h('span', { class: 'ua-mentions-name' }, item.label),
item.description ? h('span', { class: 'ua-mentions-desc' }, item.description) : null)));
if (visible) input.setAttribute('aria-activedescendant', `${uid}-${active()}`);
else input.removeAttribute('aria-activedescendant');
};
/*** 选中候选:替换光标前的 @片段、写回信号、光标落到插入文本之后 ***/
const pick = (item: MentionItem): void => {
const caret = input.selectionStart ?? input.value.length;
const next = applyPick(input.value, caret, item.name);
input.value = next.text;
value(next.text);
input.setSelectionRange(next.caret, next.caret);
input.focus();
open(false);
query('');
paint();
props.onSelect?.(item);
};
input.addEventListener('input', () => {
value(input.value);
props.onInput?.(input.value);
const token = tokenBefore(input.value, input.selectionStart ?? input.value.length);
query(token ?? '');
active(0); // 每次输入重新从第一项开始高亮
open(token !== null);
paint();
});
input.addEventListener('keydown', (e) => {
if (!open()) return; // 未开层时所有按键交给浏览器
const shown = filtered();
if (e.key === 'Escape') {
e.preventDefault();
e.stopPropagation(); // 只收本层,不连带外层弹层
open(false);
paint();
return;
}
if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
e.preventDefault();
active((active() + (e.key === 'ArrowDown' ? 1 : -1) + shown.length) % shown.length); // 环形移动
paint();
return;
}
if ((e.key === 'Enter' || e.key === 'Tab') && shown.length) {
e.preventDefault();
pick(shown[Math.min(active(), shown.length - 1)]);
}
});
input.addEventListener('blur', () => { open(false); paint(); }); // 失焦收起
effect(() => {
const outer = value();
if (outer !== input.value) input.value = outer; // 外部改信号时同步输入框
});
return wrap;
}
+18
View File
@@ -777,4 +777,22 @@ export interface FloatButtonProps {
onClick?: () => void; // 点击回调
}
/*** @提及候选项 ***/
export interface MentionItem {
name: string; // 插入正文的名字(@ 之后的部分,如 zhangsan)
label: string; // 候选行显示名称(如 张三)
description?: string; // 候选行右侧说明(部门 / 邮箱)
keywords?: string[]; // 额外匹配词(拼音、工号),不显示只参与过滤
}
/*** @提及输入入参 ***/
export interface MentionsProps {
items: MentionItem[]; // 候选池:键入 @ 后按名字 / 关键词过滤
value?: Signal<string>; // 受控文本信号;不传则内部维护
rows?: number; // 输入框行数;缺省 3
placeholder?: string; // 占位文案
onSelect?: (item: MentionItem) => void; // 选中某人后的回调(弹层已收起)
onInput?: (text: string) => void; // 每次输入的回调,参数为当前全文
}
export type { ColumnDef, RowKey }; // 再导出常用内核类型,方便调用方