Files
Enterprise-Knowledge-Base/frontend/src/components/KnowledgeBaseSelector.vue
T
wizardchen 30332cca99 fix(frontend): correct floating UI positioning under root zoom
The user font-size preference applies CSS `zoom` on `<html>`
(see `composables/useFont.ts`). This breaks every popover that
anchors itself with `getBoundingClientRect()` + `position: fixed/absolute`,
because the rect is in visual pixels while CSS lengths are then
multiplied by zoom again — shifting popovers toward the lower-right
and miscomputing maxHeight / width / viewport-edge clamping.

PR #1459 patched only `AgentSelector.vue` (and left its width/maxHeight
unfixed). This PR introduces a shared helper `frontend/src/utils/zoom.ts`
(`getRootZoom`, `rectToCssPx`, `cssViewportSize`) and applies it to every
affected popover:

- `AgentSelector.vue` — refactored to use the helper; also normalizes
  `width` and `maxHeight` (previously still in visual px).
- `Input-field.vue` — model dropdown, mention popup (typed @ and button-
  triggered), and agent-mode dropdown.
- `KnowledgeBaseSelector.vue` — primary anchor path and fallback path.
- `UserMenu.vue` — tenant and IM floating submenus + viewport clamp.
- `FAQTagTooltip.vue` — fixed tooltip positioning + edge clamp.
- `AgentEditorModal.vue` — placeholder popups for system_prompt,
  context_template, and rewrite/fallback prompts.
- `AgentStreamDisplay.vue` — `kb-float-popup` (absolute under body, also
  under the zoom containing block).

Verified with `npm --prefix frontend run build`.
2026-05-24 23:03:26 +08:00

503 lines
16 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<div v-if="visible" class="kb-overlay" @click="close">
<div class="kb-dropdown" @click.stop @wheel.stop :style="dropdownStyle">
<!-- 搜索 -->
<div class="kb-search">
<input
ref="searchInput"
v-model="searchQuery"
type="text"
:placeholder="$t('knowledgeBase.searchPlaceholder')"
class="kb-search-input"
@keydown.down.prevent="moveSelection(1)"
@keydown.up.prevent="moveSelection(-1)"
@keydown.enter.prevent="toggleSelection"
@keydown.esc="close"
/>
</div>
<!-- 列表 -->
<div class="kb-list" ref="kbList" @wheel.stop>
<div
v-for="(kb, index) in filteredKnowledgeBases"
:key="kb.id"
:class="['kb-item', { selected: isSelected(kb.id), highlighted: highlightedIndex === index }]"
@click="toggleKb(kb.id)"
@mouseenter="highlightedIndex = index"
>
<div class="kb-item-left">
<div class="checkbox" :class="{ checked: isSelected(kb.id) }">
<svg v-if="isSelected(kb.id)" width="12" height="12" viewBox="0 0 12 12" fill="none">
<path d="M10 3L4.5 8.5L2 6" stroke="#fff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
</div>
<div class="kb-icon" :class="{ 'faq': kb.type === 'faq' }">
<svg v-if="kb.type === 'faq'" width="14" height="14" viewBox="0 0 24 24" fill="none">
<path d="M12 22C17.5228 22 22 17.5228 22 12C22 6.47715 17.5228 2 12 2C6.47715 2 2 6.47715 2 12C2 17.5228 6.47715 22 12 22Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
<path d="M9 9C9 7.89543 9.89543 7 11 7H13C14.1046 7 15 7.89543 15 9C15 10.1046 14.1046 11 13 11H12V14" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
<circle cx="12" cy="17" r="1" fill="currentColor"/>
</svg>
<svg v-else width="14" height="14" viewBox="0 0 24 24" fill="none">
<path d="M22 19C22 19.5304 21.7893 20.0391 21.4142 20.4142C21.0391 20.7893 20.5304 21 20 21H4C3.46957 21 2.96086 20.7893 2.58579 20.4142C2.21071 20.0391 2 19.5304 2 19V5C2 4.46957 2.21071 3.96086 2.58579 3.58579C2.96086 3.21071 3.46957 3 4 3H9L11 6H20C20.5304 6 21.0391 6.21071 21.4142 6.58579C21.7893 6.96086 22 7.46957 22 8V19Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
</div>
<div class="kb-name-wrap">
<span class="kb-name">{{ kb.name }}</span>
<span class="kb-docs">({{ kb.type === 'faq' ? (kb.chunk_count || 0) : (kb.knowledge_count || 0) }})</span>
</div>
</div>
</div>
<div v-if="filteredKnowledgeBases.length === 0" class="kb-empty">
{{ searchQuery ? $t('knowledgeBase.noMatch') : $t('knowledgeBase.noKnowledge') }}
</div>
</div>
<!-- 底部操作 -->
<div class="kb-actions">
<button @click="selectAll" class="kb-btn">{{ $t('common.selectAll') }}</button>
<button @click="clearAll" class="kb-btn">{{ $t('common.clear') }}</button>
</div>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, computed, watch, nextTick } from 'vue'
import { useSettingsStore } from '@/stores/settings'
import { listKnowledgeBases } from '@/api/knowledge-base'
import { useI18n } from 'vue-i18n'
import { getRootZoom, rectToCssPx, cssViewportSize } from '@/utils/zoom'
interface KnowledgeBase {
id: string
name: string
type?: 'document' | 'faq'
knowledge_count?: number
chunk_count?: number
embedding_model_id?: string
summary_model_id?: string
}
const { t } = useI18n()
const props = defineProps<{
visible: boolean
anchorEl?: any | null // 支持 DOM 节点、ref、组件实例
dropdownWidth?: number
offsetY?: number
}>()
const emit = defineEmits(['close', 'update:visible'])
const settingsStore = useSettingsStore()
// 本地状态
const searchQuery = ref('')
const highlightedIndex = ref(0)
const knowledgeBases = ref<KnowledgeBase[]>([])
const searchInput = ref<HTMLInputElement | null>(null)
const kbList = ref<HTMLElement | null>(null)
const dropdownStyle = ref<Record<string, string>>({})
// props 默认
const dropdownWidth = props.dropdownWidth ?? 300
const offsetY = props.offsetY ?? 8
// 过滤:只显示已初始化(有 embedding & summary)的
const filteredKnowledgeBases = computed(() => {
const valid = knowledgeBases.value.filter(
k => k.embedding_model_id && k.summary_model_id
)
if (!searchQuery.value) return valid
const q = searchQuery.value.toLowerCase()
return valid.filter(k => k.name.toLowerCase().includes(q))
})
const selectedKbIds = computed(() => settingsStore.settings.selectedKnowledgeBases || [])
// helper: 从 props.anchorEl 获取真实 DOM 元素(支持多种传入形式)
const resolveAnchorEl = () => {
const a = props.anchorEl
if (!a) return null
// 如果是 Vue ref:取 .value
if (typeof a === 'object' && 'value' in a) {
return a.value ?? null
}
// 如果是组件实例(可能有 $el)
if (typeof a === 'object' && '$el' in a) {
// @ts-ignore
return a.$el ?? null
}
// 直接 DOM 节点或 DOMRect
return a
}
const isSelected = (id: string) => selectedKbIds.value.includes(id)
const toggleKb = (id: string) => {
isSelected(id) ? settingsStore.removeKnowledgeBase(id) : settingsStore.addKnowledgeBase(id)
}
const toggleSelection = () => {
const kb = filteredKnowledgeBases.value[highlightedIndex.value]
if (kb) toggleKb(kb.id)
}
const moveSelection = (dir: number) => {
const max = filteredKnowledgeBases.value.length
if (max === 0) return
highlightedIndex.value = Math.max(0, Math.min(max - 1, highlightedIndex.value + dir))
nextTick(() => {
const items = kbList.value?.querySelectorAll('.kb-item')
items?.[highlightedIndex.value]?.scrollIntoView({ block: 'nearest', behavior: 'smooth' })
})
}
const selectAll = () => settingsStore.selectKnowledgeBases(filteredKnowledgeBases.value.map(k => k.id))
const clearAll = () => settingsStore.clearKnowledgeBases()
const close = () => {
emit('update:visible', false)
emit('close')
}
const loadKnowledgeBases = async () => {
try {
const res: any = await listKnowledgeBases()
if (res?.data && Array.isArray(res.data)) knowledgeBases.value = res.data
} catch (e) {
console.error(t('knowledgeBase.loadingFailed'), e)
}
}
// 计算下拉位置:水平居中对齐到按钮中点,处理视口边界
const updateDropdownPosition = () => {
const anchor = resolveAnchorEl()
// Cache root zoom for this update. Both fallback and rect-anchored paths
// need to convert visual measurements to CSS pixels (see utils/zoom.ts).
const zoom = getRootZoom()
const { width: vwFallback, height: vhFallback } = cssViewportSize(zoom)
// fallback 函数
const applyFallback = () => {
const topFallback = Math.max(80, vhFallback / 2 - 160);
dropdownStyle.value = {
position: 'fixed',
width: `${dropdownWidth}px`,
left: `${Math.round((vwFallback - dropdownWidth) / 2)}px`,
top: `${Math.round(topFallback)}px`,
transform: 'none',
margin: '0',
padding: '0',
};
};
if (!anchor) {
applyFallback()
return
}
// 获取 anchor 的 bounding rect(相对于视口)
let rawRect: { top: number; left: number; right: number; bottom: number; width: number; height: number } | null = null
try {
if (typeof anchor.getBoundingClientRect === 'function') {
const r = anchor.getBoundingClientRect()
rawRect = { top: r.top, left: r.left, right: r.right, bottom: r.bottom, width: r.width, height: r.height }
} else if (anchor.width !== undefined && anchor.left !== undefined) {
// Already a DOMRect-like
rawRect = anchor as DOMRect
}
} catch (e) {
console.error('[KnowledgeBaseSelector] Error getting bounding rect:', e)
}
if (!rawRect || rawRect.width === 0 || rawRect.height === 0) {
applyFallback()
return
}
// Convert to CSS pixels so subsequent comparisons against the dropdown's
// own width/height stay in one coordinate system.
const rect = rectToCssPx(rawRect, zoom)
console.log('[KB Selector] Button rect (css px):', rect)
const vw = vwFallback
const vh = vhFallback
// 左对齐到触发元素的左边缘
// 使用 Math.floor 而不是 Math.round,避免像素对齐问题
let left = Math.floor(rect.left)
// 边界处理:不超出视口左右(留 16px margin)
const minLeft = 16
const maxLeft = Math.max(16, vw - dropdownWidth - 16)
left = Math.max(minLeft, Math.min(maxLeft, left))
// 垂直定位:紧贴按钮,使用合理的高度避免空白
const preferredDropdownHeight = 280 // 优选高度(紧凑且够用)
const maxDropdownHeight = 360 // 最大高度
const minDropdownHeight = 200 // 最小高度
const topMargin = 20 // 顶部留白
const spaceBelow = vh - rect.bottom // 下方剩余空间
const spaceAbove = rect.top // 上方剩余空间
console.log('[KB Selector] Space check:', {
spaceBelow,
spaceAbove,
windowHeight: vh
})
let actualHeight: number
let shouldOpenBelow: boolean
// 优先考虑下方空间
if (spaceBelow >= minDropdownHeight + offsetY) {
// 下方有足够空间,向下弹出
actualHeight = Math.min(preferredDropdownHeight, spaceBelow - offsetY - 16)
shouldOpenBelow = true
console.log('[KB Selector] Position: below button', { actualHeight })
} else {
// 向上弹出,优先使用 preferredHeight,必要时才扩展到 maxHeight
const availableHeight = spaceAbove - offsetY - topMargin
if (availableHeight >= preferredDropdownHeight) {
// 有足够空间显示优选高度
actualHeight = preferredDropdownHeight
} else {
// 空间不够,使用可用空间(但不小于最小高度)
actualHeight = Math.max(minDropdownHeight, availableHeight)
}
shouldOpenBelow = false
console.log('[KB Selector] Position: above button', { actualHeight })
}
// 根据弹出方向使用不同的定位方式
if (shouldOpenBelow) {
// 向下弹出:使用 top 定位
const top = Math.floor(rect.bottom + offsetY)
console.log('[KB Selector] Opening below, top:', top)
dropdownStyle.value = {
position: 'fixed',
width: `${dropdownWidth}px`,
left: `${left}px`,
top: `${top}px`,
maxHeight: `${actualHeight}px`,
transform: 'none',
margin: '0',
padding: '0'
}
} else {
// 向上弹出:使用 bottom 定位
const bottom = vh - rect.top + offsetY
console.log('[KB Selector] Opening above, bottom:', bottom)
dropdownStyle.value = {
position: 'fixed',
width: `${dropdownWidth}px`,
left: `${left}px`,
bottom: `${bottom}px`,
maxHeight: `${actualHeight}px`,
transform: 'none',
margin: '0',
padding: '0'
}
}
}
// 事件监听器引用,用于清理
let resizeHandler: (() => void) | null = null
let scrollHandler: (() => void) | null = null
// 当 visible 变化时处理
watch(() => props.visible, async (v) => {
if (v) {
await loadKnowledgeBases();
// 等 DOM 渲染完再计算位置
await nextTick();
// 多次更新位置确保准确
requestAnimationFrame(() => {
updateDropdownPosition();
requestAnimationFrame(() => {
updateDropdownPosition();
setTimeout(() => {
updateDropdownPosition();
}, 50);
});
});
// 确保 focus
nextTick(() => searchInput.value?.focus());
// 监听 resize/scroll 做微调(使用 passive 提高性能)
resizeHandler = () => updateDropdownPosition();
scrollHandler = () => updateDropdownPosition();
window.addEventListener('resize', resizeHandler, { passive: true });
window.addEventListener('scroll', scrollHandler, { passive: true, capture: true });
} else {
searchQuery.value = '';
highlightedIndex.value = 0;
// 清理事件监听器
if (resizeHandler) {
window.removeEventListener('resize', resizeHandler);
resizeHandler = null;
}
if (scrollHandler) {
window.removeEventListener('scroll', scrollHandler, { capture: true });
scrollHandler = null;
}
}
});
</script>
<style scoped lang="less">
// 确保所有元素使用 border-box 盒模型
.kb-overlay,
.kb-overlay *,
.kb-overlay *::before,
.kb-overlay *::after {
box-sizing: border-box;
}
.kb-overlay {
position: fixed;
inset: 0;
z-index: 9999;
background: transparent;
/* 不阻止点击穿透,但防止触摸滚动 */
touch-action: none;
}
/* 下拉面板使用 fixed 定位,相对于视口 */
.kb-dropdown {
position: fixed !important;
background: var(--td-bg-color-container);
border: .5px solid var(--td-component-border);
border-radius: 10px;
box-shadow: var(--td-shadow-2);
overflow: hidden;
animation: fadeIn 0.15s ease-out;
z-index: 10000;
margin: 0;
/* 确保定位准确,动画使用 scale 而不是 translate */
transform-origin: top left;
display: flex;
flex-direction: column;
}
/* 宽度由 JS 控制(dropdownWidth),这里只做内部样式 */
.kb-search {
padding: 8px 10px;
border-bottom: .5px solid var(--td-component-stroke);
}
.kb-search-input {
width: 100%;
padding: 6px 10px;
font-size: 12px;
border: .5px solid var(--td-component-stroke);
border-radius: 6px;
background: var(--td-bg-color-secondarycontainer);
outline: none;
transition: border 0.12s;
}
.kb-search-input:focus {
border-color: var(--td-success-color);
background: var(--td-bg-color-container);
}
.kb-list {
flex: 1;
min-height: 0; /* 允许 flex 子元素缩小 */
max-height: 260px;
overflow-y: auto;
padding: 6px 8px;
/* 确保滚动限制在此容器内 */
overscroll-behavior: contain;
-webkit-overflow-scrolling: touch;
}
.kb-item {
display: flex;
align-items: center;
padding: 6px 8px;
border-radius: 6px;
cursor: pointer;
transition: background 0.12s;
margin-bottom: 4px;
}
.kb-item:last-child { margin-bottom: 0; }
.kb-item:hover,
.kb-item.highlighted { background: var(--td-bg-color-secondarycontainer); }
.kb-item.selected { background: var(--td-brand-color-light); }
.kb-item-left {
display: flex;
align-items: center;
gap: 8px;
width: 100%;
}
.checkbox {
width: 16px; height: 16px;
border-radius: 3px;
border: 1.5px solid var(--td-component-border);
display: flex;
align-items: center;
justify-content: center;
flex-shrink: 0;
}
.checkbox.checked {
background: var(--td-success-color);
border-color: var(--td-success-color);
}
.checkbox.checked svg {
width: 10px;
height: 10px;
}
.kb-icon {
width: 16px;
height: 16px;
display: flex;
align-items: center;
justify-content: center;
flex-shrink: 0;
color: var(--td-brand-color-active);
&.faq {
color: var(--td-brand-color);
}
}
.kb-name-wrap { display:flex; flex-direction: row; align-items: center; gap: 4px; min-width: 0; }
.kb-name { font-size: 12px; color: var(--td-text-color-primary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; line-height: 1.4; }
.kb-docs { font-size: 11px; color: var(--td-text-color-placeholder); flex-shrink: 0; }
.kb-empty { padding: 20px 8px; text-align: center; color: var(--td-text-color-placeholder); font-size: 12px; }
.kb-actions {
display: flex;
gap: 8px;
padding: 8px 10px;
border-top: 1px solid var(--td-component-stroke);
background: var(--td-bg-color-secondarycontainer);
}
.kb-btn {
flex: 1;
padding: 6px 10px;
border-radius: 6px;
border: 1px solid var(--td-component-stroke);
background: var(--td-bg-color-container);
font-size: 12px;
color: var(--td-text-color-secondary);
cursor: pointer;
transition: all 0.12s;
}
.kb-btn:hover {
border-color: var(--td-success-color);
color: var(--td-success-color);
background: var(--td-brand-color-light);
}
@keyframes fadeIn {
from { opacity: 0; transform: scale(0.98); }
to { opacity: 1; transform: scale(1); }
}
</style>