159 lines
6.4 KiB
Vue
159 lines
6.4 KiB
Vue
<script setup lang="ts">
|
||
/**
|
||
* 最近记录面板(P3-6):最近文件 + 最近文件夹两份列表。
|
||
*
|
||
* 「文件」与「文件夹」**分开存**(`settings.json` 的 `recentFiles` / `recentFolders`):
|
||
* 两者用途不同(打开文档 vs 打开目录),混在一起会让「最近文件夹」被文件挤掉。
|
||
* 记录与笔记库解耦 —— 库可以换 / 删,历史不该跟着丢。
|
||
*
|
||
* 失效项(路径已不存在)灰显但仍可点击清理由此产生的困惑:点击会给出明确失败提示,
|
||
* 而不是静默什么都不做。
|
||
*/
|
||
import { computed } from 'vue'
|
||
import { ExternalLink, FileText, Folder, History } from '@lucide/vue'
|
||
import { toast } from 'vue-sonner'
|
||
import { ScrollArea } from '@/components/ui/scroll-area'
|
||
import { StateBlock } from '@/components/common'
|
||
import { useEditorStore } from '@/stores/editorStore'
|
||
import type { RecentEntry } from '@/types/editor'
|
||
|
||
const store = useEditorStore()
|
||
|
||
const files = computed(() => store.recentFiles)
|
||
const folders = computed(() => store.recentFolders)
|
||
|
||
/** 取所在目录(同时兼容 `\` 与 `/`)。 */
|
||
function parentDir(fullPath: string): string {
|
||
const index = Math.max(fullPath.lastIndexOf('\\'), fullPath.lastIndexOf('/'))
|
||
return index > 0 ? fullPath.slice(0, index) : ''
|
||
}
|
||
|
||
/**
|
||
* 路径尾部摘要:只保留末尾若干字符并前置省略号(例:`...ways\Downloads`)。
|
||
*
|
||
* 做「一眼看出在哪儿」的提示,完整路径仍由行的 `title` 提供。短路径整段显示,
|
||
* 长路径保留末尾 14 个字符 —— 前面的盘符与中间层级对辨认位置帮助不大,省略它们
|
||
* 反而让列表更整齐。目录项与文件项都取**所在目录**,名字里已经有的末级不再重复。
|
||
*/
|
||
function pathTail(fullPath: string): string {
|
||
const dir = parentDir(fullPath).replace(/\//g, '\\')
|
||
if (!dir) return ''
|
||
return dir.length <= 18 ? dir : `...${dir.slice(-14)}`
|
||
}
|
||
|
||
async function openFile(entry: RecentEntry): Promise<void> {
|
||
try {
|
||
await store.openDoc(entry.path)
|
||
} catch (e) {
|
||
toast.error(`打开失败:${String(e)}`)
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 打开一个「最近文件夹」:读目录 + **切到侧栏的「目录」视图**。
|
||
*
|
||
* 只 `openFolder` 是不够的 —— 它只把 `dirListing` 读进 store,而侧栏还停在「最近」页,
|
||
* 于是用户点了一下什么都「没发生」(同一个列表还在那儿)。切视图才是点击应有的反馈:
|
||
* 跳到本地文件夹侧栏,直接看到这棵目录树。
|
||
*
|
||
* 先 await 再切:目录列举是异步的,提前切会先渲染一帧空态。
|
||
*/
|
||
async function openFolder(entry: RecentEntry): Promise<void> {
|
||
try {
|
||
await store.openFolder(entry.path)
|
||
store.setSideView('directory')
|
||
} catch (e) {
|
||
toast.error(`打开文件夹失败:${String(e)}`)
|
||
}
|
||
}
|
||
</script>
|
||
|
||
<template>
|
||
<div class="flex h-full min-h-0 flex-col">
|
||
<StateBlock
|
||
v-if="!files.length && !folders.length"
|
||
variant="empty"
|
||
title="还没有最近记录"
|
||
description="打开过的文件与文件夹会出现在这里"
|
||
:min-height="160"
|
||
/>
|
||
|
||
<ScrollArea v-else class="min-h-0 flex-1">
|
||
<div class="px-1.5 py-2">
|
||
<!-- 最近文件 -->
|
||
<div v-if="files.length" class="mb-2">
|
||
<div class="flex h-6 items-center gap-1.5 px-1.5 text-[11px] font-medium text-muted-foreground">
|
||
<FileText class="size-3" />
|
||
<span>最近文件</span>
|
||
<button
|
||
type="button"
|
||
class="ml-auto text-muted-foreground hover:text-foreground"
|
||
@click="store.clearRecentFiles()"
|
||
>
|
||
清空
|
||
</button>
|
||
</div>
|
||
<div
|
||
v-for="entry in files"
|
||
:key="entry.path"
|
||
class="group flex h-7 w-full cursor-pointer items-center gap-1.5 rounded-sm px-1.5 text-left text-xs transition-colors duration-150 hover:bg-row-hover"
|
||
:class="entry.exists ? 'text-fg-secondary hover:text-foreground' : 'text-fg-disabled'"
|
||
:title="entry.path"
|
||
@click="openFile(entry)"
|
||
>
|
||
<FileText class="size-3.5 shrink-0 opacity-70" />
|
||
<span class="truncate">
|
||
{{ entry.name }}<span v-if="pathTail(entry.path)" class="text-muted-foreground">({{ pathTail(entry.path) }})</span>
|
||
</span>
|
||
<span v-if="!entry.exists" class="shrink-0 text-[10px]">(已失效)</span>
|
||
<button
|
||
type="button"
|
||
class="ml-auto rounded-sm p-0.5 opacity-0 group-hover:opacity-100 hover:bg-active"
|
||
title="在资源管理器中显示"
|
||
@click.stop="store.revealPath(entry.path)"
|
||
>
|
||
<ExternalLink class="size-3.5" />
|
||
</button>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 最近文件夹 -->
|
||
<div v-if="folders.length">
|
||
<div class="flex h-6 items-center gap-1.5 px-1.5 text-[11px] font-medium text-muted-foreground">
|
||
<Folder class="size-3" />
|
||
<span>最近文件夹</span>
|
||
<button
|
||
type="button"
|
||
class="ml-auto text-muted-foreground hover:text-foreground"
|
||
@click="store.clearRecentFolders()"
|
||
>
|
||
清空
|
||
</button>
|
||
</div>
|
||
<div
|
||
v-for="entry in folders"
|
||
:key="entry.path"
|
||
class="group flex h-7 w-full cursor-pointer items-center gap-1.5 rounded-sm px-1.5 text-left text-xs transition-colors duration-150 hover:bg-row-hover"
|
||
:class="entry.exists ? 'text-fg-secondary hover:text-foreground' : 'text-fg-disabled'"
|
||
:title="entry.path"
|
||
@click="openFolder(entry)"
|
||
>
|
||
<History class="size-3.5 shrink-0 opacity-70" />
|
||
<span class="truncate">
|
||
{{ entry.name }}<span v-if="pathTail(entry.path)" class="text-muted-foreground">({{ pathTail(entry.path) }})</span>
|
||
</span>
|
||
<span v-if="!entry.exists" class="shrink-0 text-[10px]">(已失效)</span>
|
||
<button
|
||
type="button"
|
||
class="ml-auto rounded-sm p-0.5 opacity-0 group-hover:opacity-100 hover:bg-active"
|
||
title="在资源管理器中显示"
|
||
@click.stop="store.revealPath(entry.path)"
|
||
>
|
||
<ExternalLink class="size-3.5" />
|
||
</button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</ScrollArea>
|
||
</div>
|
||
</template> |