搜索定制相关接口
- 支持环境:web插件
注册自定义搜索分类(registerCategory)
- 注册自定义搜索分类,当前仅支持标准化的「头像+主标题+副标题」UI布局。
js
export function registerCategory(options: ISearchCategoryOptions): void搜索分类调整
- 插件自定义搜索分类可通过指定
order属性调整自定义分类的顺序,内置搜索分类暂不支持调整顺序。
js
// 搜索分类顺序,按数值升序排序,相同数值按注册先后顺序排序
// number - 搜索面板与综合搜索均采用该值
// separated - 搜索面板顺序值
// integrated - 综合搜索顺序值
type Order = number | Partial<Record<'separated' | 'integrated', number>>- 附:内置搜索分类综合搜索顺序值(搜索面板未开启“全部”)
| 搜索分类 | 联系人 | 群聊 | 机器人 | 云文档 | 聊天记录 | 部门 |
|---|---|---|---|---|---|---|
| 顺序值 | 0 | 1 | 2 | 3 | 5 | 6 |
- 附:内置搜索分类综合搜索顺序值(搜索面板已开启“全部”)
| 搜索分类 | 综合 | 聊天记录 | 聊天文件 | 云文档 | 机器人 | 联系人 | 群组聊天 | 部门 |
|---|---|---|---|---|---|---|---|---|
| 顺序值 | -Infinity | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
- 示例
js
const onClick = (data: ksxz.search.ISearchData) => console.log('点击了自定义搜索项:', data)
const doSearch = async (
keyword: string,
tag: string | number | undefined,
count: number,
offset: number,
onCancel: (cb: () => void) => void
) => {
let cancelled = false
onCancel(() => {
cancelled = true
console.log('搜索请求取消:', keyword, offset, count)
// cancelToken.cancel()
})
const all = new Array(100).fill(void 0).map((_, i) => ({
avatar: '头像图片资源链接',
title: `主标题支持高亮:<em>${keyword}</em>${i}`,
subtitle: '副标题不支持高亮',
time: Date.now() - 1000 * i
}))
// await wait(1000)
if (cancelled) {
throw new Error('cancelled')
}
return {
// 当前分页搜索结果数据
data: all.slice(offset, offset + count),
// 搜索结果总数,非当前分页结果数量
total: all.length,
// 是否还有更多数据
hasNext: offset + count < all.length
}
}
ksxz.search.registerCategory({
// 搜索分类唯一标识
category: 'custom1',
// 搜索分类名称
label: '自定义搜索1',
// 搜索器
search: doSearch,
// 搜索结果点击事件回调
onDidClickItem: onClick,
// 可选搜索分类图标,在综合搜索中显示
icon: '',
// 搜索分类排序
order: 1,
// 综合分类搜索模式
// inline - 在综合分类显示完整结果(仅主窗口综合搜索模式支持,独立窗口模式该值降级为inline-preview)
// inline-preview -(默认)在综合分类显示预览结果,点击查看更多后跳转到独立分类搜索
// flatten - 平铺展开,适用于数据量在10条以下的场景
// collapsed - 收起到列表末尾「更多」分类,显示为「在 xx分类 中搜索“关键字”」
// off - 不在综合搜索展示
integrateMode: 'collapsed',
})
ksxz.search.registerCategory({
// 搜索分类唯一标识
category: 'custom2',
// 搜索分类名称
label: '子分类搜索',
// 子分类
tags: [
{ id: 0, label: '待办' },
{ id: 1, label: '已办' }
],
// 搜索器
search: doSearch,
// 搜索结果点击事件回调
onDidClickItem: onClick
})