细节调整及优化(26.8.3)

This commit is contained in:
zhongluofeng committed 2026-08-21 17:25:47 +08:00
1 parent 0a19b4b38a
commit 4dd60f42a1
72 files changed
+4779 -1395

No files matched your search

+231 -145
View File
@@ -12,6 +12,10 @@ import {
} from '@lucide/vue'
import { ScrollArea } from '@/components/ui/scroll-area'
import { Input } from '@/components/ui/input'
import { Button } from '@/components/ui/button'
import {
Select, SelectContent, SelectItem, SelectTrigger, SelectValue,
} from '@/components/ui/select'
import {
Pagination, PaginationContent, PaginationItem, PaginationEllipsis,
} from '@/components/ui/pagination'
@@ -27,6 +31,8 @@ const total = ref(0)
const currentPage = ref(1)
const PAGE_SIZE = 50
const searchQuery = ref('')
/** 类型筛选:全部/文本/图片/文件 */
const kindFilter = ref<'all' | 'text' | 'image' | 'files'>('all')
const selectedIndex = ref(0)
const loading = ref(false)
const searchInputRef = ref<HTMLInputElement | null>(null)
@@ -38,21 +44,37 @@ const totalPages = computed(() => Math.max(1, Math.ceil(total.value / PAGE_SIZE)
// ===== 数据加载 =====
/** 加载请求序号:翻页/搜索快速操作时丢弃过期请求结果,避免旧请求覆盖新结果 */
let loadSeq = 0
/** 列表视图:history = 全部历史(后端分页);pinned = 仅钉住条目(前端筛选+分页) */
const viewMode = ref<'history' | 'pinned'>('history')
async function loadData() {
const seq = ++loadSeq
loading.value = true
try {
const q = searchQuery.value.trim()
const offset = (currentPage.value - 1) * PAGE_SIZE
let res: HistoryPage
if (q) {
res = await commands.clipboardSearch(q, PAGE_SIZE, offset)
if (viewMode.value === 'pinned') {
// 钉住视图:一次性取回,前端按类型/搜索筛选并分页
const all = await commands.clipboardGetPinned()
if (seq !== loadSeq) return
const q = searchQuery.value.trim().toLowerCase()
const kind = kindFilter.value
let filtered = all
if (kind !== 'all') filtered = filtered.filter(i => i.kind === kind)
if (q) filtered = filtered.filter(i => i.preview.toLowerCase().includes(q))
total.value = filtered.length
const start = (currentPage.value - 1) * PAGE_SIZE
items.value = filtered.slice(start, start + PAGE_SIZE)
} else {
res = await commands.clipboardGetHistory(PAGE_SIZE, offset, 'all')
const q = searchQuery.value.trim()
const offset = (currentPage.value - 1) * PAGE_SIZE
let res: HistoryPage
if (q) {
res = await commands.clipboardSearch(q, PAGE_SIZE, offset)
} else {
res = await commands.clipboardGetHistory(PAGE_SIZE, offset, kindFilter.value)
}
if (seq !== loadSeq) return // 过期请求丢弃
items.value = res.items
total.value = res.total
}
if (seq !== loadSeq) return // 过期请求丢弃
items.value = res.items
total.value = res.total
selectedIndex.value = 0
} catch (e) {
if (seq !== loadSeq) return
@@ -67,13 +89,20 @@ async function loadData() {
}
}
/** 切换历史/钉住视图 */
async function toggleViewMode() {
viewMode.value = viewMode.value === 'history' ? 'pinned' : 'history'
currentPage.value = 1
await loadData()
}
async function gotoPage(p: number) {
currentPage.value = Math.min(Math.max(1, p), totalPages.value)
await loadData()
}
// 防抖搜索
watch(searchQuery, () => {
// 防抖搜索/筛选
watch([searchQuery, kindFilter], () => {
currentPage.value = 1
if (searchTimer) clearTimeout(searchTimer)
searchTimer = setTimeout(loadData, 200)
@@ -107,7 +136,14 @@ async function deleteItem(item: ClipboardItem, ev: Event) {
ev.stopPropagation()
try {
await commands.clipboardDelete(item.id)
// 本地先移除保持即时反馈,再整页刷新(total/分页数与后端保持一致;
// 钉住视图下数据源也需重建)
items.value = items.value.filter((i) => i.id !== item.id)
// 当前页删空且不在第一页:回退一页,避免停留在空页
if (items.value.length === 0 && currentPage.value > 1) {
currentPage.value -= 1
}
await loadData()
} catch (e) {
console.error('[clipboard-popup] 删除失败:', e)
}
@@ -121,19 +157,109 @@ async function hideWindow() {
}
}
// ===== 独立预览窗口 =====
async function showPreview(item: ClipboardItem) {
try {
await commands.clipboardShowPreview(item.id)
} catch {
/* 忽略 */
}
}
async function hidePreview() {
try {
await commands.clipboardHidePreview()
} catch {
/* 忽略 */
}
}
/** 存储条目预览文本元素,用于检测是否被截断(仅截断的文本才显示预览) */
const previewEls = new Map<number, HTMLElement>()
function setPreviewEl(id: number, el: unknown) {
if (el instanceof HTMLElement) previewEls.set(id, el)
else previewEls.delete(id)
}
/** line-clamp-1 截断检测:内容高度超过单行即视为截断 */
function isTextTruncated(id: number): boolean {
const el = previewEls.get(id)
if (!el) return true // 无法测量时保守显示
return el.scrollHeight > el.clientHeight
}
/** 当前条目是否需要预览:图片始终预览;文本仅被截断时预览 */
function needsPreview(item: ClipboardItem): boolean {
if (item.kind === 'image') return true
if (item.kind === 'text') return isTextTruncated(item.id)
return false
}
let hoverTimer: ReturnType<typeof setTimeout> | null = null
/** 鼠标是否位于独立预览窗内:为 true 时离开条目不隐藏预览,便于点击复制/放大 */
const mouseInPreview = ref(false)
/** 键盘选中:立即显示/隐藏预览(不等待悬停延迟) */
function showSelectedPreview() {
cancelHoverTimer()
mouseInPreview.value = false
const item = items.value[selectedIndex.value]
if (item && needsPreview(item)) {
void showPreview(item)
} else {
void hidePreview()
}
}
function onItemHover(idx: number, item: ClipboardItem) {
selectedIndex.value = idx
// 先取消之前的定时器
cancelHoverTimer()
mouseInPreview.value = false
// 不需要预览的条目:立即隐藏预览(避免前一条目的预览残留)
if (!needsPreview(item)) {
void hidePreview()
return
}
// 100ms 后显示(快速响应悬停意图,同时避免鼠标快速划过时频繁开关)
hoverTimer = setTimeout(() => {
void showPreview(item)
}, 100)
}
function onItemLeave() {
// 延迟隐藏:避免鼠标在条目间快速移动时预览闪烁。
// 若鼠标已移入预览窗(点击复制/放大),由 preview-enter 事件取消该定时器。
cancelHoverTimer()
if (mouseInPreview.value) return
hoverTimer = setTimeout(() => {
void hidePreview()
}, 250)
}
function cancelHoverTimer() {
if (hoverTimer) {
clearTimeout(hoverTimer)
hoverTimer = null
}
}
// ===== 键盘导航 =====
// 类型筛选 Select 的展开状态:展开时 ↑↓/Enter/Esc 由 Select 自行处理,
// 不触发列表导航/粘贴/关闭弹窗
const selectOpen = ref(false)
function onKeydown(e: KeyboardEvent) {
if (selectOpen.value) return
if (e.key === 'ArrowDown') {
e.preventDefault()
selectedIndex.value = Math.min(selectedIndex.value + 1, items.value.length - 1)
cancelHoverTimer()
previewVisible.value = false
showSelectedPreview()
scrollSelectedIntoView()
} else if (e.key === 'ArrowUp') {
e.preventDefault()
selectedIndex.value = Math.max(selectedIndex.value - 1, 0)
cancelHoverTimer()
previewVisible.value = false
showSelectedPreview()
scrollSelectedIntoView()
} else if (e.key === 'Enter') {
e.preventDefault()
@@ -141,6 +267,8 @@ function onKeydown(e: KeyboardEvent) {
if (item) selectAndPaste(item)
} else if (e.key === 'Escape') {
e.preventDefault()
cancelHoverTimer()
void hidePreview()
hideWindow()
}
}
@@ -176,62 +304,6 @@ const formatTime = (ms: number) => {
const hasItems = computed(() => items.value.length > 0)
// ===== 图片悬停预览(悬停 100ms 后显示缩略图) =====
const previewSrc = ref('')
const previewVisible = ref(false)
let hoverTimer: ReturnType<typeof setTimeout> | null = null
// 缓存已加载的图片 id → dataUrl,避免重复请求
const imageCache = new Map<number, string>()
/** 根据 base64 前缀判断 MIME 类型 */
function buildImageDataUrl(b64: string): string {
const mime = b64.startsWith('/9j/') ? 'image/jpeg' : 'image/png'
return `data:${mime};base64,${b64}`
}
async function onItemHover(idx: number, item: ClipboardItem) {
selectedIndex.value = idx
// 仅图片类型触发预览
if (item.kind !== 'image') {
cancelHoverTimer()
previewVisible.value = false
return
}
// 先取消之前的定时器和预览
cancelHoverTimer()
// 100ms 后加载并显示(快速响应悬停意图)
hoverTimer = setTimeout(async () => {
try {
let src = imageCache.get(item.id)
if (!src) {
const detail = await commands.clipboardGetItem(item.id)
if (detail?.imageBase64) {
src = buildImageDataUrl(detail.imageBase64)
imageCache.set(item.id, src)
}
}
if (src) {
previewSrc.value = src
previewVisible.value = true
}
} catch {
/* 忽略加载失败 */
}
}, 100)
}
function cancelHoverTimer() {
if (hoverTimer) {
clearTimeout(hoverTimer)
hoverTimer = null
}
}
function onItemLeave() {
cancelHoverTimer()
previewVisible.value = false
}
// ===== 主题应用(与主应用同步) =====
/** 从 localStorage 读取主应用的主题设置 */
function readMainTheme(): { theme: string; effect: string } {
@@ -334,15 +406,37 @@ onMounted(async () => {
await applyTheme()
searchQuery.value = ''
currentPage.value = 1
// 重置预览状态,清空缓存避免历史图片占用内存
// 隐藏独立预览窗口(弹窗重新显示时清除上次预览)
cancelHoverTimer()
previewVisible.value = false
imageCache.clear()
mouseInPreview.value = false
void hidePreview()
await loadData()
await nextTick()
searchInputRef.value?.focus()
}))
// 监听弹窗隐藏事件:取消悬停定时器并隐藏预览,避免弹窗隐藏后残留定时器重新弹出预览窗
unlistenFns.push(await listen(EVENTS.clipboardPopupHide, () => {
cancelHoverTimer()
mouseInPreview.value = false
void hidePreview()
}))
// 鼠标进入预览窗:取消条目离开触发的隐藏定时器,允许在预览窗内停留并点击复制/放大
unlistenFns.push(await listen(EVENTS.clipboardPreviewEnter, () => {
mouseInPreview.value = true
cancelHoverTimer()
}))
// 鼠标离开预览窗:恢复可隐藏状态并延迟隐藏;鼠标移回条目时由 onItemHover 重新显示
unlistenFns.push(await listen(EVENTS.clipboardPreviewLeave, () => {
mouseInPreview.value = false
cancelHoverTimer()
hoverTimer = setTimeout(() => {
void hidePreview()
}, 250)
}))
// 加载初始数据
await loadData()
await nextTick()
@@ -364,9 +458,9 @@ onUnmounted(() => {
<template>
<div class="popup-root flex flex-col h-screen w-screen" @keydown="onKeydown">
<!-- 搜索栏(与剪切板主页统一样式) -->
<!-- 搜索栏(与剪切板主页统一样式)+ 类型筛选 -->
<div class="flex items-center gap-2 px-3 py-2 border-b border-border shrink-0">
<div class="relative flex-1 max-w-sm">
<div class="relative flex-1 min-w-0">
<Search class="absolute left-2.5 top-1/2 -translate-y-1/2 size-4 text-muted-foreground" />
<Input
ref="searchInputRef"
@@ -376,16 +470,20 @@ onUnmounted(() => {
spellcheck="false"
/>
</div>
<span class="text-xs text-muted-foreground whitespace-nowrap">{{ total }} 条</span>
<Select v-model="kindFilter" :open="selectOpen" @update:open="selectOpen = $event">
<SelectTrigger size="sm" class="w-[76px] shrink-0 text-xs" title="按类型筛选">
<SelectValue />
</SelectTrigger>
<!-- 内容宽度与 trigger 等宽:覆盖默认 min-w-[8rem](128px),紧凑弹窗内不显过宽 -->
<SelectContent class="w-(--reka-select-trigger-width) min-w-0">
<SelectItem value="all">全部</SelectItem>
<SelectItem value="text">文本</SelectItem>
<SelectItem value="image">图片</SelectItem>
<SelectItem value="files">文件</SelectItem>
</SelectContent>
</Select>
</div>
<!-- 图片悬停预览浮层 -->
<Transition name="popup-preview">
<div v-if="previewVisible && previewSrc" class="popup-preview">
<img :src="previewSrc" alt="预览" />
</div>
</Transition>
<!-- 列表 -->
<ScrollArea class="popup-list flex-1 min-h-0">
<div class="space-y-1.5 p-2">
@@ -419,7 +517,10 @@ onUnmounted(() => {
>
<component :is="kindIcon(item.kind)" class="size-4 text-muted-foreground shrink-0 mt-0.5" />
<div class="flex-1 min-w-0">
<p class="text-sm break-all line-clamp-1">{{ item.preview }}</p>
<p
class="text-sm break-all line-clamp-1"
:ref="el => setPreviewEl(item.id, el)"
>{{ item.preview }}</p>
<div class="flex items-center gap-2 mt-0.5 text-xs text-muted-foreground flex-wrap">
<span class="popup-item-kind px-1.5 py-0 text-[10px] border rounded-sm" :class="kindBadgeClass(item.kind)">{{ kindLabel(item.kind) }}</span>
<span>{{ formatTime(item.createdAt) }}</span>
@@ -437,38 +538,56 @@ onUnmounted(() => {
</div>
</ScrollArea>
<!-- 分页(与剪切板历史统一样式) -->
<div v-if="totalPages > 1" class="flex items-center justify-center gap-1 px-2 py-1 border-t border-border">
<Pagination
v-slot="{ page }"
:page="currentPage"
:total="total"
:items-per-page="PAGE_SIZE"
:sibling-count="1"
show-edges
@update:page="gotoPage"
>
<PaginationContent v-slot="{ items: pageItems }" class="gap-1">
<template v-for="(item, index) in pageItems" :key="index">
<PaginationItem
v-if="item.type === 'page'"
:value="item.value"
:is-active="item.value === page"
size="icon"
class="size-7 text-xs"
>
{{ item.value }}
</PaginationItem>
<PaginationEllipsis v-else class="size-7" />
</template>
</PaginationContent>
</Pagination>
<!-- 底部:总数 + 分页 + 视图切换(grid 两侧 1fr 等宽,分页严格居中) -->
<div class="grid grid-cols-[1fr_auto_1fr] items-center gap-2 px-3 py-1 border-t border-border shrink-0">
<span class="text-xs text-muted-foreground whitespace-nowrap justify-self-start">{{ total }} 条</span>
<div v-if="totalPages > 1" class="justify-self-center">
<Pagination
v-slot="{ page }"
:page="currentPage"
:total="total"
:items-per-page="PAGE_SIZE"
:sibling-count="1"
show-edges
@update:page="gotoPage"
>
<PaginationContent v-slot="{ items: pageItems }" class="gap-1">
<template v-for="(item, index) in pageItems" :key="index">
<PaginationItem
v-if="item.type === 'page'"
:value="item.value"
:is-active="item.value === page"
size="icon"
class="size-7 text-xs"
>
{{ item.value }}
</PaginationItem>
<PaginationEllipsis v-else class="size-7" />
</template>
</PaginationContent>
</Pagination>
</div>
<!-- col-start-3:无分页时中间轨道宽为 0,若不固定列位置按钮会被自动放置
到第 2 轨道(视觉居中),显式固定第 3 轨道保证始终贴右 -->
<div class="justify-self-end col-start-3">
<Button
:variant="viewMode === 'pinned' ? 'default' : 'outline'"
size="sm"
class="h-7 px-2 text-xs gap-1"
:title="viewMode === 'pinned' ? '当前:仅钉住条目,点击查看全部历史' : '当前:全部历史,点击仅查看钉住条目'"
@click="toggleViewMode"
>
<!-- 图标与文字同义(Pin=钉住视图 / ClipboardList=历史视图),高亮表示当前视图 -->
<component :is="viewMode === 'pinned' ? Pin : ClipboardList" class="size-3.5" />
{{ viewMode === 'pinned' ? '钉住' : '历史' }}
</Button>
</div>
</div>
<!-- 底部提示 -->
<div class="popup-footer shrink-0">
<span><kbd>↑</kbd><kbd>↓</kbd> 导航</span>
<span><kbd>Enter</kbd> 粘贴</span>
<span><kbd>Enter</kbd>/<kbd>左键</kbd> 粘贴</span>
<span><kbd>Esc</kbd> 关闭</span>
</div>
</div>
@@ -588,39 +707,6 @@ onUnmounted(() => {
color: var(--muted-foreground);
}
/* 图片悬停预览浮层:固定在弹窗右上角,不遮挡列表操作 */
.popup-preview {
position: absolute;
top: 50px;
right: 10px;
z-index: 100;
max-width: 180px;
max-height: 180px;
border-radius: 6px;
overflow: hidden;
border: 1px solid var(--border);
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25);
background: var(--popover, var(--background));
pointer-events: none;
}
.popup-preview img {
display: block;
max-width: 100%;
max-height: 180px;
object-fit: contain;
}
/* 预览浮层淡入淡出 */
.popup-preview-enter-active,
.popup-preview-leave-active {
transition: opacity 0.15s ease;
}
.popup-preview-enter-from,
.popup-preview-leave-to {
opacity: 0;
}
.popup-footer kbd {
background: var(--muted);
color: var(--foreground);