Private
Public Access
1
0

新增:收藏夹置顶功能

This commit is contained in:
2026-03-06 22:03:34 +08:00
parent 1eaf61cf41
commit 5f94ccf13b
4 changed files with 115 additions and 3 deletions

View File

@@ -18,6 +18,21 @@ export function useFavorites() {
pressedIndex: -1
})
/**
* 排序收藏列表:置顶项在前(按 pinnedAt 降序),非置顶项按添加时间降序
*/
const sortFavorites = () => {
favorites.value.sort((a, b) => {
// 置顶项优先
if (a.pinnedAt && !b.pinnedAt) return -1
if (!a.pinnedAt && b.pinnedAt) return 1
// 都是置顶项,按置顶时间降序
if (a.pinnedAt && b.pinnedAt) return b.pinnedAt - a.pinnedAt
// 都不是置顶项,按添加时间降序(最新在前)
return b.addedAt - a.addedAt
})
}
/**
* 从 localStorage 加载收藏列表
*/
@@ -32,6 +47,9 @@ export function useFavorites() {
...fav,
isDir: fav.isDir ?? (fav as any).is_dir ?? false
}))
// 排序
sortFavorites()
}
} catch (error) {
console.error('加载收藏列表失败:', error)
@@ -108,6 +126,34 @@ export function useFavorites() {
return favorites.value.some(fav => normalizePath(fav.path) === normalizedPath)
}
/**
* 切换置顶状态
*/
const togglePin = (path: string) => {
const normalizedPath = normalizePath(path)
const fav = favorites.value.find(f => normalizePath(f.path) === normalizedPath)
if (fav) {
if (fav.pinnedAt) {
// 取消置顶
fav.pinnedAt = undefined
} else {
// 设置置顶
fav.pinnedAt = Date.now()
}
sortFavorites()
saveFavorites()
}
}
/**
* 检查是否已置顶
*/
const isPinned = (path: string): boolean => {
const normalizedPath = normalizePath(path)
const fav = favorites.value.find(f => normalizePath(f.path) === normalizedPath)
return !!fav?.pinnedAt
}
/**
* 长按开始
*/
@@ -219,6 +265,8 @@ export function useFavorites() {
removeFavorite,
toggleFavorite,
isFavorite,
togglePin,
isPinned,
// 拖拽方法
onLongPressStart,