refactor(frontend): consolidate knowledge tag filters and remove stale styles

main
wizardchen 2026-09-17 16:31:35 +08:00 committed by lyingbug
parent 820a14dafa
commit 559ad532ae
5 changed files with 675 additions and 1536 deletions

View File

@ -108,6 +108,17 @@
- 用重构前的三个实现对 30,000 组确定性生成输入执行差异校验,结果一致;输入包含中文、emoji、空白、非法字节和截断边界。临时对比代码未留在仓库,避免长期维护旧实现副本。
- 4 个受影响包测试通过:104 项顶层测试,包含既有同步、游标和错误处理测试。未修改前端,未运行浏览器检查或真实外部来源服务测试。
## 第七批已实施:知识库标签筛选与遗留样式
基于第六批合入后的 `820a14daf`,文档页与 FAQ 页复用 `KnowledgeTagFilter`,统一筛选浮层、多选、清空、搜索和管理入口。计入新增组件后,生产前端源码净减少 1,061 行。
- 页面继续负责标签 API、分页、搜索防抖、持久化选择和列表刷新;共享组件只负责展示与交互,保留文档数量/FAQ 分块数量、清空占位文案和管理权限差异。
- 删除两份无效的已选标签补齐逻辑:缺失项原本仍从同一个标签列表生成的映射中查找,因此不可能补齐;未加载的选中 ID 仍保留在选择状态中。
- 删除 `KnowledgeBase.vue` 中 682 行失效 scoped 样式,包括已移入子组件的卡片菜单、标签、悬浮详情和旧上传区域样式。保留仍在当前模板使用的骨架卡片、FAQ 容器和响应式布局。
- 对迁移前后编译后的 CSS 做临时声明对比,检查 2,196 项声明,确认保留的父组件规则与两种筛选界面的原有属性值一致;这不替代浏览器视觉验收。
- 新增 9 项真实 Vue 组件行为测试,覆盖两种计数、多选/清空、搜索、加载/空态、分页、管理权限及父级选择更新。
- `scripts/verify_frontend_pr.sh` 通过:957 项测试通过,1 项原有测试跳过,类型检查与生产构建通过。未做浏览器视觉验收;未修改后端。
## 后续优先级
| 优先级 | 证据 | 建议边界 | 验证重点 |

File diff suppressed because it is too large Load Diff

View File

@ -107,97 +107,11 @@
</template>
</t-input>
<div class="faq-filter-bar__filters">
<t-popup v-model:visible="tagFilterPanelVisible" trigger="click" placement="bottom-left"
overlay-class-name="tag-filter-popup" :overlay-inner-style="{ padding: 0 }">
<template #content>
<div class="tag-filter-panel" @click.stop>
<div class="tag-filter-panel__header">
<div class="tag-filter-panel__title">
<span>{{ $t('knowledgeBase.tagFilterTitle') }}</span>
<span class="tag-filter-panel__count">({{ sidebarCategoryCount }})</span>
</div>
</div>
<div class="tag-search-bar">
<t-input v-model.trim="tagSearchQuery" size="small"
:placeholder="$t('knowledgeBase.tagSearchPlaceholder')" clearable>
<template #prefix-icon>
<t-icon name="search" size="14px" />
</template>
</t-input>
</div>
<div class="tag-filter-panel__body">
<template v-if="tagLoading && !sidebarTags.length">
<div class="tag-filter-chips">
<div v-for="n in 8" :key="'skel-tag-' + n" class="tag-filter-chip-skeleton">
<t-skeleton animation="gradient"
:row-col="[{ width: '56px', height: '24px', type: 'rect' }]" />
</div>
</div>
</template>
<template v-else>
<div class="tag-filter-chips">
<button
v-for="tag in sidebarTags"
:key="tag.id"
type="button"
class="tag-filter-chip"
:class="{ active: isTagFilterActive(tag.id) }"
:title="`${tag.name} (${tag.chunk_count || 0})`"
@click="handleTagRowClick(tag.id)"
>
<span class="tag-filter-chip__label">{{ tag.name }}</span>
<span class="tag-filter-chip__count">{{ tag.chunk_count || 0 }}</span>
</button>
</div>
<div v-if="!sidebarTags.length" class="tag-empty-state">
{{ $t('knowledgeBase.tagEmptyResult') }}
</div>
<div v-if="tagHasMore" class="tag-load-more">
<t-button variant="text" size="small" :loading="tagLoadingMore" @click.stop="loadTags()">
{{ $t('tenant.loadMore') }}
</t-button>
</div>
</template>
</div>
<div v-if="canEdit" class="tag-filter-panel__footer">
<t-button variant="text" size="small" class="tag-manage-link" @click="openTagManageDrawer">
{{ $t('knowledgeBase.tagManageLink') }}
</t-button>
</div>
</div>
</template>
<div class="doc-filter-field">
<button type="button" class="doc-tag-filter-trigger doc-filter-field__control"
:class="{ open: tagFilterPanelVisible, 'is-placeholder': isTagFilterPlaceholder }"
:aria-label="$t('knowledgeBase.tagFilterTitle')"
:title="activeTagFilterTitle"
@mouseenter="tagFilterTriggerHover = true"
@mouseleave="tagFilterTriggerHover = false">
<span class="doc-tag-filter-trigger__prefix" aria-hidden="true">
<t-icon name="discount" size="16px" />
</span>
<span class="doc-tag-filter-trigger__label">{{ activeTagFilterLabel }}</span>
<span class="doc-tag-filter-trigger__suffix">
<span
v-if="showTagFilterClear"
class="t-input__suffix t-input__suffix-icon t-input__clear"
:aria-label="$t('common.clear')"
@click.stop="clearTagFilter"
@mousedown.stop
>
<t-icon name="close-circle-filled" class="t-input__suffix-clear" />
</span>
<t-icon
v-else
name="chevron-down"
size="16px"
class="doc-tag-filter-trigger__caret"
:class="{ open: tagFilterPanelVisible }"
/>
</span>
</button>
</div>
</t-popup>
<KnowledgeTagFilter v-model:search="tagSearchQuery" v-model:cleared="tagFilterCleared"
:tags="tagList" :selected-ids="selectedTagIds" :total="tagTotal"
:loading="tagLoading" :loading-more="tagLoadingMore" :has-more="tagHasMore"
:can-manage="canEdit" variant="faq" @change="handleTagFilterChange"
@load-more="loadTags()" @manage="openTagManageDrawer" />
</div>
<div class="faq-filter-bar__trailing">
<!-- 新建:新建条目 / 导入 -->
@ -863,6 +777,7 @@
<script setup lang="ts">
import { ref, reactive, watch, onMounted, computed, nextTick, onUnmounted, h } from 'vue'
import KnowledgeTagFilter from './KnowledgeTagFilter.vue'
import { MessagePlugin, DialogPlugin, Icon as TIcon } from 'tdesign-vue-next'
import type { FormRules, FormInstanceFunctions } from 'tdesign-vue-next'
import { useI18n } from 'vue-i18n'
@ -1063,8 +978,6 @@ let entrySearchDebounce: number | null = null
const tagList = ref<any[]>([])
const tagLoading = ref(false)
const selectedTagIds = ref<string[]>([])
const tagFilterPanelVisible = ref(false)
const tagFilterTriggerHover = ref(false)
const tagFilterCleared = ref(false)
const tagManageDrawerVisible = ref(false)
const overallFAQTotal = ref(0)
@ -1076,22 +989,6 @@ const tagLoadingMore = ref(false)
const tagTotal = ref(0)
let tagSearchDebounce: number | null = null
const showTagFilterClear = computed(
() => selectedTagIds.value.length > 0 && tagFilterTriggerHover.value,
)
const isTagFilterPlaceholder = computed(
() => selectedTagIds.value.length === 0 && tagFilterCleared.value,
)
const tagMap = computed<Record<string, any>>(() => {
const map: Record<string, any> = {}
tagList.value.forEach((tag) => {
map[tag.id] = tag
})
return map
})
// tagMapBySeqId uses seq_id as key for looking up by entry.tag_id
const tagMapBySeqId = computed<Record<number, any>>(() => {
const map: Record<number, any> = {}
@ -1109,48 +1006,6 @@ const tagSelectOptions = computed(() =>
regularTags.value.map((tag: any) => ({ label: tag.name, value: tag.seq_id })),
)
const sidebarCategoryCount = computed(() => tagTotal.value || tagList.value.length)
const sidebarTags = computed(() => {
const list = tagList.value
const selectedIds = selectedTagIds.value
if (!selectedIds.length) {
return list
}
const missingSelected = selectedIds
.filter((id) => !list.some((tag) => tag.id === id))
.map((id) => tagMap.value[id])
.filter(Boolean)
if (!missingSelected.length) {
return list
}
return [...missingSelected, ...list]
})
const activeTagFilterLabel = computed(() => {
if (selectedTagIds.value.length === 0) {
return tagFilterCleared.value
? t('knowledgeBase.tagFilterPlaceholder')
: t('knowledgeBase.allTags')
}
if (selectedTagIds.value.length === 1) {
const id = selectedTagIds.value[0]
return tagMap.value[id]?.name || t('knowledgeBase.allTags')
}
return t('knowledgeBase.tagFilterMulti', { count: selectedTagIds.value.length })
})
const activeTagFilterTitle = computed(() => {
if (selectedTagIds.value.length === 0) {
return t('knowledgeBase.tagFilterTitle')
}
const names = selectedTagIds.value
.map((id) => tagMap.value[id]?.name)
.filter(Boolean)
return names.length > 0 ? names.join('、') : t('knowledgeBase.tagFilterTitle')
})
const isTagFilterActive = (tagId: string) => selectedTagIds.value.includes(tagId)
const kbInfo = ref<any>(null)
const knowledgeList = ref<Array<{ id: string; name: string; type?: string }>>([])
@ -1349,26 +1204,7 @@ const handleTagFilterChange = (tagIds: string[]) => {
tagIds.forEach((id) => uiStore.toggleSelectedTagId(id))
}
const handleTagRowClick = (tagId: string) => {
const next = new Set(selectedTagIds.value)
if (next.has(tagId)) {
next.delete(tagId)
} else {
next.add(tagId)
}
if (next.size > 0) {
tagFilterCleared.value = false
}
handleTagFilterChange([...next])
}
const clearTagFilter = () => {
tagFilterCleared.value = true
handleTagFilterChange([])
}
const openTagManageDrawer = () => {
tagFilterPanelVisible.value = false
tagManageDrawerVisible.value = true
}
@ -2868,126 +2704,6 @@ watch(() => entries.value.map(e => ({
}, { deep: true })
</script>
<style lang="less">
/* 下拉菜单样式已统一至 @/assets/dropdown-menu.less */
.tag-filter-popup {
z-index: 5500 !important;
}
.tag-filter-popup .t-popup__content {
padding: 0 !important;
border-radius: 8px !important;
background: var(--td-bg-color-container) !important;
border: 0.5px solid var(--td-component-stroke) !important;
box-shadow:
0 0 0 0.5px rgba(0, 0, 0, 0.03),
0 2px 4px rgba(0, 0, 0, 0.04),
0 8px 24px rgba(0, 0, 0, 0.1) !important;
}
.tag-filter-panel {
width: 320px;
max-width: min(320px, calc(100vw - 32px));
max-height: min(70vh, 480px);
display: flex;
flex-direction: column;
padding: 12px 14px;
box-sizing: border-box;
font-size: 12px;
color: var(--td-text-color-primary);
}
.tag-filter-panel__header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 10px;
}
.tag-filter-panel__title {
display: flex;
align-items: baseline;
gap: 6px;
font-size: 14px;
font-weight: 600;
}
.tag-filter-panel__count {
font-size: 12px;
color: var(--td-text-color-placeholder);
font-weight: 400;
}
.tag-filter-panel .tag-search-bar {
margin-bottom: 10px;
}
.tag-filter-panel__body {
display: flex;
flex-direction: column;
gap: 8px;
flex: 1;
min-height: 0;
overflow-y: auto;
}
.tag-filter-chips {
display: flex;
flex-wrap: wrap;
gap: 6px;
}
.tag-filter-chip {
display: inline-flex;
align-items: center;
gap: 4px;
height: 24px;
padding: 0 8px;
border: 1px solid var(--td-component-stroke);
border-radius: 4px;
background: transparent;
color: var(--td-text-color-secondary);
font-size: 11px;
cursor: pointer;
}
.tag-filter-chip.active {
border-color: color-mix(in srgb, var(--td-brand-color) 35%, var(--td-component-stroke));
color: var(--td-brand-color);
background-color: color-mix(in srgb, var(--td-brand-color) 6%, transparent);
}
.tag-filter-chip__label {
max-width: 120px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.tag-filter-chip__count {
font-size: 10px;
color: var(--td-text-color-placeholder);
}
.tag-filter-panel__footer {
margin-top: 10px;
padding-top: 10px;
border-top: 1px solid var(--td-component-stroke);
}
.tag-empty-state {
text-align: center;
padding: 10px 6px;
color: var(--td-text-color-placeholder);
font-size: 11px;
}
.tag-load-more {
display: flex;
justify-content: center;
padding-top: 2px;
}
</style>
<style scoped lang="less">
.faq-manager {
display: flex;
@ -3096,78 +2812,6 @@ watch(() => entries.value.map(e => ({
}
}
.doc-filter-field {
width: 140px;
flex-shrink: 0;
&__control {
width: 100%;
}
}
.doc-tag-filter-trigger {
display: inline-flex;
align-items: center;
box-sizing: border-box;
width: 100%;
height: 32px;
padding: 0 8px;
border: 1px solid transparent;
border-radius: var(--td-radius-default);
background: var(--td-bg-color-secondarycontainer);
color: var(--td-text-color-primary);
font-family: var(--app-font-family);
font-size: 14px;
line-height: 1;
cursor: pointer;
transition: background 0.2s ease, border-color 0.2s ease;
&:hover,
&.open {
background: var(--td-bg-color-secondarycontainer);
border-color: transparent;
}
&.is-placeholder {
color: var(--td-text-color-placeholder);
}
&__prefix {
flex-shrink: 0;
display: inline-flex;
align-items: center;
margin-right: var(--td-comp-margin-s);
color: var(--td-text-color-placeholder);
}
&__label {
flex: 1;
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
text-align: left;
}
&__suffix {
flex-shrink: 0;
display: inline-flex;
align-items: center;
margin-left: var(--td-comp-margin-s);
}
&__caret {
flex-shrink: 0;
color: var(--td-text-color-placeholder);
transition: transform 0.2s ease, color 0.2s ease;
&.open {
color: var(--td-brand-color);
transform: rotate(180deg);
}
}
}
:deep(.t-input) {
font-size: 13px;
background-color: var(--td-bg-color-secondarycontainer);

View File

@ -0,0 +1,189 @@
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import { runInNewContext } from 'node:vm'
import test from 'node:test'
import ts from 'typescript'
import { compileScript, parse } from '@vue/compiler-sfc'
import * as vue from 'vue'
const source = readFileSync(new URL('./KnowledgeTagFilter.vue', import.meta.url), 'utf8')
const { descriptor } = parse(source)
const script = compileScript(descriptor, { id: 'tag-filter-test', inlineTemplate: true })
const compiled = ts.transpileModule(script.content, {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 },
}).outputText
const translate = (key: string, values?: { count: number }) => values ? `${key}:${values.count}` : key
const exports: { default?: vue.Component } = {}
runInNewContext(compiled, {
exports,
require(name: string) {
if (name === 'vue') return vue
if (name === 'vue-i18n') return { useI18n: () => ({ t: translate }) }
throw new Error(`Unexpected import: ${name}`)
},
})
type Host = { type: string; props: Record<string, any>; children: Host[]; text: string; parent?: Host }
const node = (type: string): Host => ({ type, props: {}, children: [], text: '' })
const renderer = vue.createRenderer<Host, Host>({
createElement: node,
createText: text => ({ ...node('#text'), text }),
createComment: text => ({ ...node('#comment'), text }),
patchProp: (el, key, _old, value) => { el.props[key] = value },
setText: (el, text) => { el.text = text },
setElementText: (el, text) => { el.children = []; el.text = text },
parentNode: el => el.parent || null,
nextSibling: el => el.parent?.children[el.parent.children.indexOf(el) + 1] || null,
insert(el, parent, anchor) {
if (el.parent) el.parent.children.splice(el.parent.children.indexOf(el), 1)
el.parent = parent
const index = anchor ? parent.children.indexOf(anchor) : -1
parent.children.splice(index < 0 ? parent.children.length : index, 0, el)
},
remove(el) {
el.parent?.children.splice(el.parent.children.indexOf(el), 1)
el.parent = undefined
},
})
const textOf = (el: Host): string => el.type === '#comment' ? '' : el.text + el.children.map(textOf).join('')
const all = (el: Host, predicate: (node: Host) => boolean): Host[] => [
...(predicate(el) ? [el] : []), ...el.children.flatMap(child => all(child, predicate)),
]
const hasClass = (el: Host, name: string) => String(el.props.class || '').split(' ').includes(name)
function fixture(variant: 'documents' | 'faq') {
const state = vue.reactive({
variant, tags: [
{ id: 'a', name: 'Alpha', knowledge_count: 12, chunk_count: 31 },
{ id: 'b', name: 'Beta', knowledge_count: 0, chunk_count: 2 },
],
selectedIds: [] as string[], total: 7, loading: false, loadingMore: false, hasMore: false,
canManage: true, search: '', cleared: false,
})
const events = { changes: [] as string[][], loadMore: 0, manage: 0 }
const root = node('root')
const app = renderer.createApp({ setup: () => () => vue.h(exports.default!, {
...state,
'onUpdate:search': (value: string) => { state.search = value },
'onUpdate:cleared': (value: boolean) => { state.cleared = value },
onChange: (ids: string[]) => { events.changes.push([...ids]); state.selectedIds = ids },
onLoadMore: () => { events.loadMore++ }, onManage: () => { events.manage++ },
}) })
app.config.globalProperties.$t = translate as typeof app.config.globalProperties.$t
app.component('t-popup', vue.defineComponent({
props: ['visible'], emits: ['update:visible'],
setup: (props, { emit, slots }) => () => vue.h('popup', {
visible: props.visible, onOpen: () => emit('update:visible', true),
}, [slots.default?.(), props.visible ? vue.h('overlay', {}, slots.content?.()) : null]),
}))
app.component('t-input', vue.defineComponent({
props: ['modelValue', 'modelModifiers'], emits: ['update:modelValue'],
setup: (props, { emit }) => () => vue.h('input', {
value: props.modelValue, onInput: (value: string) => emit('update:modelValue', value),
}),
}))
for (const [name, type] of [['t-button', 'button'], ['t-icon', 'icon'], ['t-skeleton', 'skeleton']]) {
app.component(name!, vue.defineComponent({ setup: (_props, { slots }) => () => vue.h(type!, {}, slots.default?.()) }))
}
app.mount(root)
const elements = (name: string) => all(root, el => hasClass(el, name))
const one = (name: string) => {
const el = elements(name)[0]; assert.ok(el, `missing .${name}`); return el
}
const fire = async (el: Host, event = 'onClick', value?: unknown) => {
let stopped = false
el.props[event](value ?? { stopPropagation() { stopped = true }, preventDefault() {} })
await vue.nextTick()
return stopped
}
return {
state, events, root, elements, one, fire,
open: () => fire(all(root, el => el.type === 'popup')[0]!, 'onOpen'),
close: () => app.unmount(),
}
}
for (const variant of ['documents', 'faq'] as const) {
test(`${variant}: renders counts, labels, selection and tooltips`, async t => {
const f = fixture(variant); t.after(f.close)
assert.equal(textOf(f.one('doc-tag-filter-trigger__label')), 'knowledgeBase.allTags')
await f.open()
assert.equal(textOf(f.one('tag-filter-panel__count')), '(7)')
assert.equal(textOf(f.elements('tag-filter-chip__count')[0]!), variant === 'documents' ? '12' : '31')
assert.equal(f.elements('tag-filter-chip')[0]!.props.title, `Alpha (${variant === 'documents' ? 12 : 31})`)
assert.equal(hasClass(f.one('tag-filter-panel'), 'tag-filter-panel--documents'), variant === 'documents')
await f.fire(f.elements('tag-filter-chip')[0]!)
assert.deepEqual(f.events.changes, [['a']])
assert.equal(textOf(f.one('doc-tag-filter-trigger__label')), 'Alpha')
assert.equal(f.one('doc-tag-filter-trigger').props.title, 'Alpha')
await f.fire(f.elements('tag-filter-chip')[1]!)
assert.equal(textOf(f.one('doc-tag-filter-trigger__label')), 'knowledgeBase.tagFilterMulti:2')
assert.equal(f.one('doc-tag-filter-trigger').props.title, 'Alpha、Beta')
await f.fire(f.elements('tag-filter-chip')[0]!)
assert.deepEqual(f.events.changes.at(-1), ['b'])
assert.equal(hasClass(f.elements('tag-filter-chip')[0]!, 'active'), false)
})
test(`${variant}: clearing stops propagation and parent resets restore the placeholder`, async t => {
const f = fixture(variant); t.after(f.close)
f.state.selectedIds = ['a']; await vue.nextTick()
assert.equal(f.elements('t-input__clear').length, 0)
await f.fire(f.one('doc-tag-filter-trigger'), 'onMouseenter')
assert.equal(await f.fire(f.one('t-input__clear')), true)
assert.equal(f.state.cleared, true)
assert.deepEqual([...f.state.selectedIds], [])
assert.equal(textOf(f.one('doc-tag-filter-trigger__label')), 'knowledgeBase.tagFilterPlaceholder')
assert.equal(hasClass(f.one('doc-tag-filter-trigger'), 'is-placeholder'), true)
await f.open(); await f.fire(f.elements('tag-filter-chip')[0]!)
assert.equal(f.state.cleared, false)
await f.fire(f.elements('tag-filter-chip')[0]!)
assert.equal(textOf(f.one('doc-tag-filter-trigger__label')), 'knowledgeBase.allTags')
f.state.cleared = true; await vue.nextTick()
f.state.cleared = false; await vue.nextTick()
assert.equal(hasClass(f.one('doc-tag-filter-trigger'), 'is-placeholder'), false)
})
test(`${variant}: search, skeletons, empty results and paging stay controlled by the parent`, async t => {
const f = fixture(variant); t.after(f.close)
f.state.tags = []; f.state.loading = true; f.state.total = 0
await f.open()
assert.equal(all(f.root, el => el.type === 'skeleton').length, 8)
assert.equal(f.elements('tag-empty-state').length, 0)
await f.fire(all(f.root, el => el.type === 'input')[0]!, 'onInput', ' title ')
assert.equal(f.state.search, 'title')
f.state.loading = false; await vue.nextTick()
assert.equal(textOf(f.one('tag-empty-state')), 'knowledgeBase.tagEmptyResult')
f.state.tags = [{ id: 'a', name: 'Result', knowledge_count: 0, chunk_count: 0 }]
f.state.hasMore = true; f.state.loadingMore = true; await vue.nextTick()
assert.equal(textOf(f.one('tag-filter-panel__count')), '(1)')
const button = all(f.one('tag-load-more'), el => el.type === 'button')[0]!
assert.equal(button.props.loading, true)
assert.equal(await f.fire(button), true)
assert.equal(f.events.loadMore, 1)
assert.equal(f.state.search, 'title')
})
test(`${variant}: viewers can filter and managers can open management`, async t => {
const f = fixture(variant); t.after(f.close)
f.state.canManage = false; await f.open()
assert.equal(f.elements('tag-manage-link').length, 0)
await f.fire(f.elements('tag-filter-chip')[0]!)
assert.deepEqual([...f.state.selectedIds], ['a'])
f.state.canManage = true; await vue.nextTick()
await f.fire(f.one('tag-manage-link'))
assert.equal(f.events.manage, 1)
assert.equal(all(f.root, el => el.type === 'overlay').length, 0)
})
}
test('external selection and tag pages update labels without dropping unloaded selections', async t => {
const f = fixture('documents'); t.after(f.close)
f.state.selectedIds = ['unloaded']; await vue.nextTick()
assert.equal(textOf(f.one('doc-tag-filter-trigger__label')), 'knowledgeBase.allTags')
assert.equal(f.one('doc-tag-filter-trigger').props.title, 'knowledgeBase.tagFilterTitle')
await f.open(); await f.fire(f.elements('tag-filter-chip')[0]!)
assert.deepEqual([...f.state.selectedIds], ['unloaded', 'a'])
assert.equal(f.one('doc-tag-filter-trigger').props.title, 'Alpha')
f.state.tags[0]!.name = 'Renamed'; await vue.nextTick()
assert.equal(f.one('doc-tag-filter-trigger').props.title, 'Renamed')
})

View File

@ -0,0 +1,463 @@
<script setup lang="ts">
import { computed, ref } from 'vue'
import { useI18n } from 'vue-i18n'
type FilterTag = { id: string; name: string; knowledge_count?: number; chunk_count?: number }
const props = defineProps<{
tags: FilterTag[]
selectedIds: string[]
total: number
loading: boolean
loadingMore: boolean
hasMore: boolean
canManage: boolean
variant: 'documents' | 'faq'
}>()
const search = defineModel<string>('search', { required: true })
const cleared = defineModel<boolean>('cleared', { required: true })
const emit = defineEmits<{
change: [ids: string[]]
'load-more': []
manage: []
}>()
const { t } = useI18n()
const visible = ref(false)
const hovered = ref(false)
const tagMap = computed(() => new Map(props.tags.map(tag => [tag.id, tag])))
const isPlaceholder = computed(() => props.selectedIds.length === 0 && cleared.value)
const label = computed(() => {
if (!props.selectedIds.length) return t(cleared.value ? 'knowledgeBase.tagFilterPlaceholder' : 'knowledgeBase.allTags')
if (props.selectedIds.length === 1) return tagMap.value.get(props.selectedIds[0]!)?.name || t('knowledgeBase.allTags')
return t('knowledgeBase.tagFilterMulti', { count: props.selectedIds.length })
})
const title = computed(() => {
const names = props.selectedIds.map(id => tagMap.value.get(id)?.name).filter(Boolean)
return names.length ? names.join('、') : t('knowledgeBase.tagFilterTitle')
})
const countOf = (tag: FilterTag) => (props.variant === 'documents' ? tag.knowledge_count : tag.chunk_count) || 0
const toggle = (id: string) => {
const next = new Set(props.selectedIds)
if (next.has(id)) next.delete(id)
else next.add(id)
if (next.size) cleared.value = false
emit('change', [...next])
}
const clear = () => {
cleared.value = true
emit('change', [])
}
const manage = () => {
visible.value = false
emit('manage')
}
</script>
<template>
<t-popup v-model:visible="visible" trigger="click" placement="bottom-left"
overlay-class-name="tag-filter-popup" :overlay-inner-style="{ padding: 0 }">
<template #content>
<div class="tag-filter-panel" :class="{ 'tag-filter-panel--documents': variant === 'documents' }" @click.stop>
<div class="tag-filter-panel__header">
<div class="tag-filter-panel__title">
<span>{{ $t('knowledgeBase.tagFilterTitle') }}</span>
<span class="tag-filter-panel__count">({{ total || tags.length }})</span>
</div>
</div>
<div class="tag-search-bar">
<t-input v-model.trim="search" size="small"
:placeholder="$t('knowledgeBase.tagSearchPlaceholder')" clearable>
<template #prefix-icon>
<t-icon name="search" size="14px" />
</template>
</t-input>
</div>
<div class="tag-filter-panel__body">
<template v-if="loading && !tags.length">
<div class="tag-filter-chips">
<div v-for="n in 8" :key="'skel-tag-' + n" class="tag-filter-chip-skeleton">
<t-skeleton animation="gradient"
:row-col="[{ width: '56px', height: '24px', type: 'rect' }]" />
</div>
</div>
</template>
<template v-else>
<div class="tag-filter-chips">
<button
v-for="tag in tags"
:key="tag.id"
type="button"
class="tag-filter-chip"
:class="{ active: selectedIds.includes(tag.id) }"
:title="`${tag.name} (${countOf(tag)})`"
@click="toggle(tag.id)"
>
<span class="tag-filter-chip__label">{{ tag.name }}</span>
<span class="tag-filter-chip__count">{{ countOf(tag) }}</span>
</button>
</div>
<div v-if="!tags.length" class="tag-empty-state">
{{ $t('knowledgeBase.tagEmptyResult') }}
</div>
<div v-if="hasMore" class="tag-load-more">
<t-button variant="text" size="small" :loading="loadingMore"
@click.stop="emit('load-more')">
{{ $t('tenant.loadMore') }}
</t-button>
</div>
</template>
</div>
<div v-if="canManage" class="tag-filter-panel__footer">
<t-button variant="text" size="small" class="tag-manage-link" @click="manage">
{{ $t('knowledgeBase.tagManageLink') }}
</t-button>
</div>
</div>
</template>
<div class="doc-filter-field" :class="{ 'doc-filter-field--documents': variant === 'documents' }">
<button type="button" class="doc-tag-filter-trigger doc-filter-field__control"
:class="{ open: visible, 'is-placeholder': isPlaceholder }"
:aria-label="$t('knowledgeBase.tagFilterTitle')"
:title="title"
@mouseenter="hovered = true"
@mouseleave="hovered = false">
<span class="doc-tag-filter-trigger__prefix" aria-hidden="true">
<t-icon name="discount" size="16px" />
</span>
<span class="doc-tag-filter-trigger__label">{{ label }}</span>
<span class="doc-tag-filter-trigger__suffix">
<span
v-if="selectedIds.length > 0 && hovered"
class="t-input__suffix t-input__suffix-icon t-input__clear"
:aria-label="$t('common.clear')"
@click.stop="clear"
@mousedown.stop
>
<t-icon name="close-circle-filled" class="t-input__suffix-clear" />
</span>
<t-icon
v-else
name="chevron-down"
size="16px"
class="doc-tag-filter-trigger__caret"
:class="{ open: visible }"
/>
</span>
</button>
</div>
</t-popup>
</template>
<style lang="less">
.tag-filter-popup {
z-index: 5500 !important;
}
.tag-filter-popup .t-popup__content {
padding: 0 !important;
border-radius: 8px !important;
background: var(--td-bg-color-container) !important;
border: 0.5px solid var(--td-component-stroke) !important;
box-shadow:
0 0 0 0.5px rgba(0, 0, 0, 0.03),
0 2px 4px rgba(0, 0, 0, 0.04),
0 8px 24px rgba(0, 0, 0, 0.1) !important;
}
</style>
<style scoped lang="less">
.doc-filter-field {
width: 140px;
flex-shrink: 0;
}
.doc-tag-filter-trigger {
display: inline-flex;
align-items: center;
box-sizing: border-box;
width: 100%;
height: 32px;
padding: 0 8px;
border: 1px solid transparent;
border-radius: var(--td-radius-default);
background: var(--td-bg-color-secondarycontainer);
color: var(--td-text-color-primary);
font-family: var(--app-font-family);
font-size: 14px;
line-height: 1;
cursor: pointer;
transition: background 0.2s ease, border-color 0.2s ease;
&:hover,
&.open {
background: var(--td-bg-color-secondarycontainer);
border-color: transparent;
}
&.is-placeholder {
color: var(--td-text-color-placeholder);
}
&__prefix {
flex-shrink: 0;
display: inline-flex;
align-items: center;
margin-right: var(--td-comp-margin-s);
color: var(--td-text-color-placeholder);
}
&__label {
flex: 1;
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
text-align: left;
}
&__suffix {
flex-shrink: 0;
display: inline-flex;
align-items: center;
margin-left: var(--td-comp-margin-s);
}
&__caret {
flex-shrink: 0;
color: var(--td-text-color-placeholder);
transition: transform 0.2s ease, color 0.2s ease;
&.open {
color: var(--td-brand-color);
transform: rotate(180deg);
}
}
}
.doc-filter-field--documents .doc-tag-filter-trigger__suffix {
:deep(.t-input__suffix) {
margin-left: 0;
}
:deep(.t-input__suffix-clear) {
font-size: 16px;
}
}
.tag-filter-panel {
width: 320px;
max-width: min(320px, calc(100vw - 32px));
max-height: min(70vh, 480px);
display: flex;
flex-direction: column;
padding: 12px 14px;
box-sizing: border-box;
font-size: 12px;
color: var(--td-text-color-primary);
}
.tag-filter-panel__header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 10px;
}
.tag-filter-panel__title {
display: flex;
align-items: baseline;
gap: 6px;
font-size: 14px;
font-weight: 600;
}
.tag-filter-panel__count {
font-size: 12px;
color: var(--td-text-color-placeholder);
font-weight: 400;
}
.tag-filter-panel .tag-search-bar {
margin-bottom: 10px;
}
.tag-filter-panel__body {
display: flex;
flex-direction: column;
gap: 8px;
flex: 1;
min-height: 0;
overflow-y: auto;
}
.tag-filter-chips {
display: flex;
flex-wrap: wrap;
gap: 6px;
}
.tag-filter-chip {
display: inline-flex;
align-items: center;
gap: 4px;
height: 24px;
padding: 0 8px;
border: 1px solid var(--td-component-stroke);
border-radius: 4px;
background: transparent;
color: var(--td-text-color-secondary);
font-size: 11px;
cursor: pointer;
}
.tag-filter-chip.active {
border-color: color-mix(in srgb, var(--td-brand-color) 35%, var(--td-component-stroke));
color: var(--td-brand-color);
background-color: color-mix(in srgb, var(--td-brand-color) 6%, transparent);
}
.tag-filter-chip__label {
max-width: 120px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.tag-filter-chip__count {
font-size: 10px;
color: var(--td-text-color-placeholder);
}
.tag-filter-panel__footer {
margin-top: 10px;
padding-top: 10px;
border-top: 1px solid var(--td-component-stroke);
}
.tag-empty-state {
text-align: center;
padding: 10px 6px;
color: var(--td-text-color-placeholder);
font-size: 11px;
}
.tag-load-more {
display: flex;
justify-content: center;
padding-top: 2px;
}
.tag-filter-panel--documents .tag-filter-panel__header {
padding: 0;
color: var(--td-text-color-primary);
}
.tag-filter-panel--documents .tag-filter-panel__title {
letter-spacing: 0.5px;
}
.tag-filter-panel--documents .tag-search-bar {
padding: 0;
}
.tag-filter-panel--documents .tag-search-bar :deep(.t-input) {
font-size: 13px;
background-color: var(--td-bg-color-secondarycontainer);
border-color: transparent;
border-radius: 6px;
box-shadow: none !important;
}
.tag-filter-panel--documents .tag-search-bar :deep(.t-input):hover,
.tag-filter-panel--documents .tag-search-bar :deep(.t-input):focus,
.tag-filter-panel--documents .tag-search-bar :deep(.t-input).t-is-focused {
border-color: var(--td-component-border);
background-color: var(--td-bg-color-container);
box-shadow: none !important;
}
.tag-filter-panel--documents .tag-search-bar :deep(.t-input__inner) {
font-size: 13px;
}
.tag-filter-panel--documents .tag-search-bar :deep(.t-input__prefix-icon) {
margin-right: 0;
}
.tag-filter-panel--documents .tag-filter-panel__body {
overflow-x: hidden;
scrollbar-width: thin;
}
.tag-filter-panel--documents .tag-filter-panel__body::-webkit-scrollbar {
width: 4px;
}
.tag-filter-panel--documents .tag-filter-panel__body::-webkit-scrollbar-thumb {
border-radius: 2px;
background: var(--td-scrollbar-color);
}
.tag-filter-panel--documents .tag-filter-chips {
align-items: flex-start;
}
.tag-filter-panel--documents .tag-filter-chip-skeleton {
flex-shrink: 0;
}
.tag-filter-panel--documents .tag-filter-chip {
box-sizing: border-box;
max-width: 100%;
font-family: var(--app-font-family);
font-weight: 400;
line-height: 24px;
outline: none;
transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
-webkit-font-smoothing: antialiased;
}
.tag-filter-panel--documents .tag-filter-chip:hover:not(.active) {
border-color: var(--td-component-border);
background: var(--td-bg-color-secondarycontainer);
color: var(--td-text-color-primary);
}
.tag-filter-panel--documents .tag-filter-chip:focus-visible {
box-shadow: 0 0 0 2px color-mix(in srgb, var(--td-component-stroke) 60%, transparent);
}
.tag-filter-panel--documents .tag-filter-chip.active {
font-weight: 500;
}
.tag-filter-panel--documents .tag-filter-chip.active .tag-filter-chip__count {
color: color-mix(in srgb, var(--td-brand-color) 72%, var(--td-text-color-secondary));
}
.tag-filter-panel--documents .tag-filter-chip.active:hover {
background-color: color-mix(in srgb, var(--td-brand-color) 10%, transparent);
}
.tag-filter-panel--documents .tag-filter-chip__label {
min-width: 0;
}
.tag-filter-panel--documents .tag-filter-chip__count {
flex-shrink: 0;
font-weight: 400;
font-variant-numeric: tabular-nums;
}
.tag-filter-panel--documents .tag-filter-chip__count::before {
content: '·';
margin-right: 2px;
opacity: 0.65;
}
.tag-filter-panel--documents .tag-filter-panel__footer {
display: flex;
justify-content: flex-start;
}
.tag-filter-panel--documents .tag-filter-panel__footer :deep(.tag-manage-link.t-button) {
padding: 0;
height: auto;
min-height: 0;
font-size: 13px;
color: var(--td-text-color-secondary);
border: none !important;
background: transparent !important;
box-shadow: none !important;
transition: color 0.15s ease;
}
.tag-filter-panel--documents .tag-filter-panel__footer :deep(.tag-manage-link.t-button):hover,
.tag-filter-panel--documents .tag-filter-panel__footer :deep(.tag-manage-link.t-button):focus-visible {
color: var(--td-brand-color) !important;
background: transparent !important;
border-color: transparent !important;
text-decoration: none;
}
.tag-filter-panel--documents .tag-load-more :deep(.t-button) {
padding: 0;
font-size: 12px;
color: var(--td-text-color-placeholder);
}
.tag-filter-panel--documents .tag-empty-state {
padding: 6px 0;
font-size: 12px;
}
</style>