feat(filelib-web): 刷新页面时保持树展开、选中节点、tab 与文件夹位置

This commit is contained in:
2026-07-27 21:25:05 +08:00
parent 8eda04f1d9
commit 87bbf0bd57
4 changed files with 108 additions and 12 deletions
+25 -4
View File
@@ -1,6 +1,6 @@
<script lang="ts">
import { api } from "./api.js";
import { currentNode, breadcrumb, bumpTree, clearSelectedFile } from "./browser.js";
import { currentNode, breadcrumb, bumpTree, clearSelectedFile, activeTab, restoreTab } from "./browser.js";
import { toastOk, toastErr } from "./stores.js";
import OverviewPanel from "./OverviewPanel.svelte";
import FilesPanel from "./FilesPanel.svelte";
@@ -10,7 +10,12 @@
import Icon from "./Icon.svelte";
type Tab = "detail" | "files" | "history" | "grants";
let tab = $state<Tab>("detail");
const validTabs: readonly Tab[] = ["detail", "files", "history", "grants"];
let tab = $state<Tab>((validTabs.includes(restoreTab as Tab) ? restoreTab as Tab : "detail"));
/** 跟踪上一次见到的 node id,用于判断是否真正切换了节点。 */
let prevNodeId: string | null = null;
/** 首次恢复时不重置 tab。 */
let restoredOnce = restoreTab !== null;
let showCreateChild = $state(false);
let newName = $state("");
let newKind = $state<"FOLDER" | "PROJECT">("FOLDER");
@@ -32,11 +37,27 @@
});
$effect(() => {
void node?.id;
tab = "detail";
const id = node?.id ?? null;
// node 还未加载或与上次相同时不做任何事。
if (id === null || id === prevNodeId) return;
prevNodeId = id;
if (restoredOnce) {
// 首次恢复(刷新后)保持 persisted tab,但要确保 tab 对当前节点有效。
restoredOnce = false;
const isProject = node?.kind === "PROJECT";
if ((tab === "files" || tab === "history") && !isProject) tab = "detail";
if (tab === "grants" && node?.role !== "MANAGE") tab = "detail";
} else {
tab = "detail";
}
clearSelectedFile();
});
// tab 变化时同步到持久化 store。
$effect(() => {
activeTab.set(tab);
});
async function createChild(): Promise<void> {
const name = newName.trim();
if (name === "" || node === null) return;