- 脏标记行尾归一(含裸\r),CRLF 文件打开不再误报未保存 - 防抖回灌校验发起文件+flushEmit,杜绝串文件与丢尾 - F5/tab淘汰/读失败/草稿/重命名/保存刷新六处状态一致性修复 - GBK 文件按原编码回写(接口零改动),保存不再悄悄转 UTF-8 - .sh 系高亮根修(StreamLanguage 导入源错),面包屑长名截断 - CSV 大文件门豁免+表头贴顶;空标题滤除与 iframe 边距去除 - BGM 播放器增强收编(拖拽seek/音量/连播/快捷键/路径导航折叠) - 版本 0.5.3,CHANGELOG 同步
345 lines
10 KiB
TypeScript
345 lines
10 KiB
TypeScript
/**
|
|
* 路径导航 Composable
|
|
* 提供路径输入、历史记录、前进/后退等功能
|
|
* 历史记录按服务器设备(profileId)分桶持久化:切换机器时下拉历史相应切换为该机器上次的访问记录
|
|
*/
|
|
|
|
import { ref, watch, computed } from 'vue'
|
|
import { STORAGE_KEYS } from '@/utils/constants'
|
|
import { normalizePathSeparators } from '@/utils/fileUtils'
|
|
import type { PathHistory } from '@/types/file-system'
|
|
|
|
// 按 profile 分桶的历史持久化 key(localStorage)
|
|
const HISTORY_BY_PROFILE_KEY = 'desk:pathHistoryByProfile'
|
|
// 每台机器保留的历史条数上限(原始栈条数)
|
|
const HISTORY_PER_PROFILE_MAX = 50
|
|
// 历史下拉折叠门槛:在目录停留不足该秒数视为“路过”,与下一跳是父子关系时折叠进终点
|
|
const DWELL_THRESHOLD_MS = 5000
|
|
|
|
interface ProfileHistoryBucket {
|
|
paths: string[]
|
|
profileIds: (string | undefined)[]
|
|
timestamps: number[]
|
|
}
|
|
|
|
/** 读取全部分桶历史 */
|
|
const loadHistoryBuckets = (): Record<string, ProfileHistoryBucket> => {
|
|
try {
|
|
const raw = localStorage.getItem(HISTORY_BY_PROFILE_KEY)
|
|
return raw ? JSON.parse(raw) : {}
|
|
} catch (error) {
|
|
console.error('读取分桶历史失败:', error)
|
|
return {}
|
|
}
|
|
}
|
|
|
|
/** 写回全部分桶历史 */
|
|
const persistHistoryBuckets = (buckets: Record<string, ProfileHistoryBucket>) => {
|
|
try {
|
|
localStorage.setItem(HISTORY_BY_PROFILE_KEY, JSON.stringify(buckets))
|
|
} catch (error) {
|
|
console.error('保存分桶历史失败:', error)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 从 localStorage 恢复上次的路径(本地场景,远程机器的历史在分桶里)
|
|
*/
|
|
const restoreLastPath = (): string | null => {
|
|
try {
|
|
const lastPath = localStorage.getItem(STORAGE_KEYS.FILESYSTEM.FILE_PATH)
|
|
if (lastPath) {
|
|
return normalizePathSeparators(lastPath)
|
|
}
|
|
return lastPath
|
|
} catch (error) {
|
|
console.error('恢复路径失败:', error)
|
|
return null
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 保存路径到 localStorage(仅本地场景;远程历史以分桶为真值)
|
|
*/
|
|
const saveLastPath = (path: string) => {
|
|
try {
|
|
localStorage.setItem(STORAGE_KEYS.FILESYSTEM.FILE_PATH, path)
|
|
} catch (error) {
|
|
console.error('保存路径失败:', error)
|
|
}
|
|
}
|
|
|
|
export interface UsePathNavigationOptions {
|
|
onListDirectory?: (path: string) => Promise<void>
|
|
/** 获取当前 profileId,用于历史记录绑定 */
|
|
getCurrentProfileId?: () => string | undefined
|
|
initialPath?: string
|
|
}
|
|
|
|
/**
|
|
* 父子路径判定:parent 是 child 的直接前缀目录(A 是 A\B 的父)
|
|
*/
|
|
const isAncestorPath = (parent: string, child: string): boolean => {
|
|
if (child.length <= parent.length) return false
|
|
if (!child.startsWith(parent)) return false
|
|
const sep = child[parent.length]
|
|
return sep === '/' || sep === '\\'
|
|
}
|
|
|
|
/**
|
|
* 展示层折叠:相邻两条是父子关系且上一条停留不足门槛(「路过」)时,上一条折叠进终点。
|
|
* 钻取链 A→A\B→A\B\C(每步秒过)折叠为单条 A\B\C;在中间层停留≥门槛则保留该层。
|
|
* 只作用于历史下拉展示,导航栈(后退/前进)不受影响。
|
|
*/
|
|
const collapseDrillDownPaths = (paths: string[], timestamps: number[]): string[] => {
|
|
const keep: string[] = []
|
|
for (let i = 0; i < paths.length; i++) {
|
|
const prev = keep[keep.length - 1]
|
|
const dwell = timestamps[i - 1] !== undefined && timestamps[i] !== undefined
|
|
? timestamps[i] - timestamps[i - 1]
|
|
: Infinity // 旧数据无时间戳:不折叠,保守保留
|
|
if (prev !== undefined && isAncestorPath(prev, paths[i]) && dwell < DWELL_THRESHOLD_MS) {
|
|
keep[keep.length - 1] = paths[i]
|
|
} else {
|
|
keep.push(paths[i])
|
|
}
|
|
}
|
|
return keep.reverse() // 下拉最新在上
|
|
}
|
|
|
|
export function usePathNavigation(options: UsePathNavigationOptions = {}) {
|
|
const { onListDirectory, getCurrentProfileId, initialPath = '' } = options
|
|
|
|
const savedPath = restoreLastPath()
|
|
const filePath = ref(savedPath || initialPath)
|
|
|
|
// 历史记录(每条路径绑定 profileId + 进入时刻;会话内在当前机器的桶上导航)
|
|
const history = ref<PathHistory>({
|
|
paths: [],
|
|
profileIds: [],
|
|
timestamps: [],
|
|
currentIndex: -1
|
|
})
|
|
|
|
// ===== 按 profile 分桶的历史(持久化 + 内存缓存) =====
|
|
const _buckets = loadHistoryBuckets()
|
|
let _activeProfileId = getCurrentProfileId?.() || 'local-default'
|
|
|
|
// 启动时载入当前机器的历史桶(filePath 保持单一 lastPath 行为不变)
|
|
const _initBucket = _buckets[_activeProfileId]
|
|
if (_initBucket?.paths.length) {
|
|
history.value = {
|
|
paths: [..._initBucket.paths],
|
|
profileIds: [..._initBucket.profileIds],
|
|
// 旧版持久化数据无 timestamps:补零长数组,折叠层按无时间戳保守处理
|
|
timestamps: [...(_initBucket.timestamps ?? [])],
|
|
currentIndex: _initBucket.paths.length - 1
|
|
}
|
|
}
|
|
|
|
/** 当前会话历史写回对应机器的桶并持久化 */
|
|
const syncCurrentBucket = () => {
|
|
_buckets[_activeProfileId] = {
|
|
paths: [...history.value.paths],
|
|
profileIds: [...history.value.profileIds],
|
|
timestamps: [...history.value.timestamps]
|
|
}
|
|
persistHistoryBuckets(_buckets)
|
|
}
|
|
|
|
/**
|
|
* 连接切换时调用:历史下拉/后退前进切换为目标机器上次的访问记录
|
|
*/
|
|
const setActiveProfile = (profileId?: string) => {
|
|
const id = profileId || 'local-default'
|
|
if (id === _activeProfileId) return
|
|
syncCurrentBucket()
|
|
_activeProfileId = id
|
|
const bucket = _buckets[id]
|
|
history.value = bucket?.paths.length
|
|
? {
|
|
paths: [...bucket.paths],
|
|
profileIds: [...bucket.profileIds],
|
|
timestamps: [...(bucket.timestamps ?? [])],
|
|
currentIndex: bucket.paths.length - 1
|
|
}
|
|
: { paths: [], profileIds: [], timestamps: [], currentIndex: -1 }
|
|
}
|
|
|
|
/**
|
|
* 导航到指定路径
|
|
*/
|
|
const navigate = async (path: string) => {
|
|
if (!path || path === filePath.value) {
|
|
return
|
|
}
|
|
|
|
try {
|
|
const normalizedPath = normalizePathSeparators(path)
|
|
filePath.value = normalizedPath
|
|
|
|
const profileId = getCurrentProfileId?.()
|
|
addToHistory(normalizedPath, profileId)
|
|
|
|
if (onListDirectory) {
|
|
await onListDirectory(normalizedPath)
|
|
}
|
|
} catch (error) {
|
|
console.error('导航失败:', error)
|
|
throw error
|
|
}
|
|
}
|
|
|
|
/** 添加到历史记录 */
|
|
const addToHistory = (path: string, profileId?: string) => {
|
|
const { paths, profileIds, timestamps, currentIndex } = history.value
|
|
|
|
// 分支跳转:丢弃当前位置之后的失效前进栈(三数组同步裁剪)
|
|
if (currentIndex < paths.length - 1) {
|
|
history.value.paths = paths.slice(0, currentIndex + 1)
|
|
history.value.profileIds = profileIds.slice(0, currentIndex + 1)
|
|
history.value.timestamps = timestamps.slice(0, currentIndex + 1)
|
|
}
|
|
|
|
const lastPath = history.value.paths[history.value.paths.length - 1]
|
|
if (lastPath !== path) {
|
|
history.value.paths.push(path)
|
|
history.value.profileIds.push(profileId)
|
|
history.value.timestamps.push(Date.now())
|
|
history.value.currentIndex = history.value.paths.length - 1
|
|
// 单机历史上限裁剪(裁最旧的,三数组同步)
|
|
if (history.value.paths.length > HISTORY_PER_PROFILE_MAX) {
|
|
const overflow = history.value.paths.length - HISTORY_PER_PROFILE_MAX
|
|
history.value.paths.splice(0, overflow)
|
|
history.value.profileIds.splice(0, overflow)
|
|
history.value.timestamps.splice(0, overflow)
|
|
history.value.currentIndex = history.value.paths.length - 1
|
|
}
|
|
syncCurrentBucket()
|
|
}
|
|
}
|
|
|
|
/** 后退,返回目标 profileId */
|
|
const back = async (): Promise<string | undefined> => {
|
|
const { paths, currentIndex } = history.value
|
|
|
|
if (currentIndex <= 0) return undefined
|
|
|
|
try {
|
|
const newIndex = currentIndex - 1
|
|
history.value.currentIndex = newIndex
|
|
filePath.value = paths[newIndex]
|
|
|
|
if (onListDirectory) {
|
|
await onListDirectory(paths[newIndex])
|
|
}
|
|
return history.value.profileIds[newIndex]
|
|
} catch (error) {
|
|
console.error('后退失败:', error)
|
|
throw error
|
|
}
|
|
}
|
|
|
|
/** 前进,返回目标 profileId */
|
|
const forward = async (): Promise<string | undefined> => {
|
|
const { paths, currentIndex } = history.value
|
|
|
|
if (currentIndex >= paths.length - 1) return undefined
|
|
|
|
try {
|
|
const newIndex = currentIndex + 1
|
|
history.value.currentIndex = newIndex
|
|
filePath.value = paths[newIndex]
|
|
|
|
if (onListDirectory) {
|
|
await onListDirectory(paths[newIndex])
|
|
}
|
|
return history.value.profileIds[newIndex]
|
|
} catch (error) {
|
|
console.error('前进失败:', error)
|
|
throw error
|
|
}
|
|
}
|
|
|
|
/** 获取指定历史索引的 profileId */
|
|
const getHistoryProfileId = (index: number): string | undefined => {
|
|
return history.value.profileIds[index]
|
|
}
|
|
|
|
/** 通过路径查历史 profileId */
|
|
const getProfileIdForPath = (path: string): string | undefined => {
|
|
const idx = history.value.paths.indexOf(path)
|
|
return idx !== -1 ? history.value.profileIds[idx] : undefined
|
|
}
|
|
|
|
const onPathSelect = (value: string) => {
|
|
navigate(value)
|
|
}
|
|
|
|
const onPathEnter = (value: string) => {
|
|
navigate(value)
|
|
}
|
|
|
|
const browseDirectory = async (path: string) => {
|
|
await navigate(path)
|
|
}
|
|
|
|
const getParentPath = (path: string): string => {
|
|
const separator = path.includes('\\') ? '\\' : '/'
|
|
const lastSeparator = path.lastIndexOf(separator)
|
|
return lastSeparator > 0 ? path.substring(0, lastSeparator) : path
|
|
}
|
|
|
|
const goUp = async () => {
|
|
const parentPath = getParentPath(filePath.value)
|
|
if (parentPath !== filePath.value) {
|
|
await navigate(parentPath)
|
|
}
|
|
}
|
|
|
|
const normalizePath = (path: string): string => {
|
|
return normalizePathSeparators(path)
|
|
}
|
|
|
|
const canGoBack = computed(() => {
|
|
return history.value.currentIndex > 0
|
|
})
|
|
|
|
const canGoForward = computed(() => {
|
|
return history.value.currentIndex < history.value.paths.length - 1
|
|
})
|
|
|
|
const getPathHistory = computed(() => {
|
|
// 下拉展示:钻取链按「父子关系×停留时长」折叠(A→A\B→A\B\C 秒过只显示 A\B\C)
|
|
return collapseDrillDownPaths(history.value.paths, history.value.timestamps)
|
|
})
|
|
|
|
watch(filePath, (newPath) => {
|
|
if (newPath) {
|
|
saveLastPath(newPath)
|
|
}
|
|
})
|
|
|
|
return {
|
|
filePath,
|
|
history,
|
|
navigate,
|
|
back,
|
|
forward,
|
|
goUp,
|
|
browseDirectory,
|
|
onPathSelect,
|
|
onPathEnter,
|
|
getParentPath,
|
|
normalizePath,
|
|
canGoBack,
|
|
canGoForward,
|
|
getPathHistory,
|
|
getHistoryProfileId,
|
|
getProfileIdForPath,
|
|
setActiveProfile
|
|
}
|
|
}
|
|
|
|
export type { PathHistory }
|