修复: 文件预览误报脏标记与编辑丢失等一批缺陷
- 脏标记行尾归一(含裸\r),CRLF 文件打开不再误报未保存 - 防抖回灌校验发起文件+flushEmit,杜绝串文件与丢尾 - F5/tab淘汰/读失败/草稿/重命名/保存刷新六处状态一致性修复 - GBK 文件按原编码回写(接口零改动),保存不再悄悄转 UTF-8 - .sh 系高亮根修(StreamLanguage 导入源错),面包屑长名截断 - CSV 大文件门豁免+表头贴顶;空标题滤除与 iframe 边距去除 - BGM 播放器增强收编(拖拽seek/音量/连播/快捷键/路径导航折叠) - 版本 0.5.3,CHANGELOG 同步
This commit is contained in:
1 parent
e5e8054916
commit
bce14a0480
26 files changed
+1010
-138
No files matched your search
@@ -1,6 +1,7 @@
|
||||
/**
|
||||
* 路径导航 Composable
|
||||
* 提供路径输入、历史记录、前进/后退等功能
|
||||
* 历史记录按服务器设备(profileId)分桶持久化:切换机器时下拉历史相应切换为该机器上次的访问记录
|
||||
*/
|
||||
|
||||
import { ref, watch, computed } from 'vue'
|
||||
@@ -8,15 +9,41 @@ import { STORAGE_KEYS } from '@/utils/constants'
|
||||
import { normalizePathSeparators } from '@/utils/fileUtils'
|
||||
import type { PathHistory } from '@/types/file-system'
|
||||
|
||||
export interface UsePathNavigationOptions {
|
||||
onListDirectory?: (path: string) => Promise<void>
|
||||
/** 获取当前 profileId,用于历史记录绑定 */
|
||||
getCurrentProfileId?: () => string | undefined
|
||||
initialPath?: string
|
||||
// 按 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 恢复上次的路径
|
||||
* 从 localStorage 恢复上次的路径(本地场景,远程机器的历史在分桶里)
|
||||
*/
|
||||
const restoreLastPath = (): string | null => {
|
||||
try {
|
||||
@@ -32,7 +59,7 @@ const restoreLastPath = (): string | null => {
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存路径到 localStorage
|
||||
* 保存路径到 localStorage(仅本地场景;远程历史以分桶为真值)
|
||||
*/
|
||||
const saveLastPath = (path: string) => {
|
||||
try {
|
||||
@@ -42,19 +69,103 @@ const saveLastPath = (path: string) => {
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
// 历史记录(每条路径绑定 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 }
|
||||
}
|
||||
|
||||
/**
|
||||
* 导航到指定路径
|
||||
*/
|
||||
@@ -81,18 +192,30 @@ export function usePathNavigation(options: UsePathNavigationOptions = {}) {
|
||||
|
||||
/** 添加到历史记录 */
|
||||
const addToHistory = (path: string, profileId?: string) => {
|
||||
const { paths, profileIds, currentIndex } = history.value
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -187,7 +310,8 @@ export function usePathNavigation(options: UsePathNavigationOptions = {}) {
|
||||
})
|
||||
|
||||
const getPathHistory = computed(() => {
|
||||
return history.value.paths.slice().reverse()
|
||||
// 下拉展示:钻取链按「父子关系×停留时长」折叠(A→A\B→A\B\C 秒过只显示 A\B\C)
|
||||
return collapseDrillDownPaths(history.value.paths, history.value.timestamps)
|
||||
})
|
||||
|
||||
watch(filePath, (newPath) => {
|
||||
@@ -210,10 +334,11 @@ export function usePathNavigation(options: UsePathNavigationOptions = {}) {
|
||||
normalizePath,
|
||||
canGoBack,
|
||||
canGoForward,
|
||||
getPathHistory,
|
||||
getHistoryProfileId,
|
||||
getProfileIdForPath
|
||||
}
|
||||
getPathHistory,
|
||||
getHistoryProfileId,
|
||||
getProfileIdForPath,
|
||||
setActiveProfile
|
||||
}
|
||||
}
|
||||
|
||||
export type { PathHistory }
|
||||
Reference in new issue
Block a user