From 359b16c9a41d9f22b7a952cb3f88fe3abb24b543 Mon Sep 17 00:00:00 2001 From: kicer Date: Sat, 5 Sep 2026 15:20:00 +0800 Subject: [PATCH] =?UTF-8?q?feat(files):=20=E4=BB=BB=E5=8A=A1=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E9=9D=A2=E6=9D=BF=E6=94=AF=E6=8C=81=E7=9B=AE=E5=BD=95?= =?UTF-8?q?=E5=88=86=E7=BB=84=E4=B8=8E=E6=8A=98=E5=8F=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 后端返回扁平文件列表(路径可含子目录),前端按路径组树渲染 - 每一级目录在上、文件在下;同级目录按名称排,同级文件按时间倒序 - 点击目录头切换折叠/展开,折叠状态在上传/删除重渲染后保持 - 嵌套目录按层级缩进(封顶防深层目录挤占屏幕) - 多选/全选/批量下载删除基于 data-path,嵌套行不受影响 --- app/server/public/css/style.css | 28 +++++++ app/server/public/index.html | 132 +++++++++++++++++++++++++------- 2 files changed, 134 insertions(+), 26 deletions(-) diff --git a/app/server/public/css/style.css b/app/server/public/css/style.css index f0e07de..c917f3d 100644 --- a/app/server/public/css/style.css +++ b/app/server/public/css/style.css @@ -461,6 +461,34 @@ footer { flex: none; padding: 8px 12px calc(10px + env(safe-area-inset-bottom, 0 .files-panel .file-row.clickable { cursor: pointer; } /* 默认状态:可点名称预览/下载 */ .files-panel .file-row.selected { background: var(--active-soft); } +/* 目录行:箭头 + 文件夹图标 + 名称 + 文件数;点整行切换折叠(状态由 .collapsed 控制) */ +.files-panel .file-dir-head { + display: flex; align-items: center; gap: 8px; + padding: 9px 8px; border-radius: 10px; font-size: 14px; cursor: pointer; + border-bottom: 1px dashed var(--border); +} +.files-panel .file-dir-head:active { background: var(--surface); } +.files-panel .file-dir-arrow { + width: 14px; height: 14px; flex: none; + fill: none; stroke: var(--gray); stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; + transform: rotate(90deg); /* 展开时箭头朝下,收起时朝右 */ + transition: transform .15s ease; +} +.files-panel .file-dir.collapsed > .file-dir-head .file-dir-arrow { transform: rotate(0deg); } +.files-panel .file-dir-ico { + width: 16px; height: 16px; flex: none; + fill: none; stroke: #f2b544; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; +} +.files-panel .file-dir-name { + flex: 1; min-width: 0; font-weight: 600; color: var(--text, #223); + overflow: hidden; text-overflow: ellipsis; white-space: nowrap; +} +.files-panel .file-dir-count { flex: none; font-size: 12px; color: var(--gray); } +.files-panel .file-dir.collapsed > .file-dir-body { display: none; } +/* 列表末尾收起的目录不再需要分隔线(常见两级,更深嵌套容忍多一条虚线) */ +.files-panel .files-list > .file-dir:last-child.collapsed > .file-dir-head, +.files-panel .file-dir-body > .file-dir:last-child.collapsed > .file-dir-head { border-bottom: none; } + /* 复选框:圆形,点击区域够大 */ .files-panel .file-check { flex: none; diff --git a/app/server/public/index.html b/app/server/public/index.html index cc90d0c..40bc790 100644 --- a/app/server/public/index.html +++ b/app/server/public/index.html @@ -1698,6 +1698,95 @@ updateFilesFooter(); } + // ---------- 目录分组:后端返回扁平列表(路径可含子目录),前端组树渲染 ---------- + // 规则:每一级都是目录在上、文件在下;同级目录按名称排序,同级文件按时间倒序(新的在前)。 + // filesCollapsedDirs 记住手动折叠过的目录(相对路径),上传/删除后重新渲染仍保持折叠。 + const filesCollapsedDirs = new Set(); + + function fileTimeOf(f) { return f.ctime || f.mtime || 0; } + function sortFilesByTime(arr) { + arr.sort(function (a, b) { + const d = fileTimeOf(b) - fileTimeOf(a); // 时间缺失的排最后,与原逻辑一致 + if (d) return d; + return a.filename < b.filename ? -1 : a.filename > b.filename ? 1 : 0; + }); + return arr; + } + function sortDirsByName(arr) { + arr.sort(function (a, b) { return a.name < b.name ? -1 : a.name > b.name ? 1 : 0; }); + return arr; + } + + // 扁平文件列表 → 树节点 { name, path, dirs: Map, files: [] } + function buildFilesTree(files) { + const root = { name: '', path: '', dirs: new Map(), files: [] }; + for (const f of files) { + const segs = String(f.filename).split('/'); + let node = root, rel = ''; + for (let i = 0; i < segs.length - 1; i++) { + const seg = segs[i]; + rel = rel ? rel + '/' + seg : seg; + if (!node.dirs.has(seg)) node.dirs.set(seg, { name: seg, path: rel, dirs: new Map(), files: [] }); + node = node.dirs.get(seg); + } + node.files.push(f); + } + return root; + } + + // 目录(含子目录)内文件总数,用于目录头计数 + function countDirFiles(node) { + let n = node.files.length; + for (const d of node.dirs.values()) n += countDirFiles(d); + return n; + } + + // 缩进:每层加深一点,封顶避免深层目录把文件名挤出屏幕 + function filesIndent(depth) { return 8 + Math.min(depth, 3) * 14; } + + function fileRowHtml(f, depth) { + // 预览策略:md/txt/html + 图片在 fs-viewer 预览(pdf 不预) + const low = f.filename.toLowerCase(); + const fsPv = /\.(md|txt|html?|png|jpe?g|gif|webp|svg)$/.test(low); + const ico = fsPv + ? '' + : ''; + const name = esc(String(f.filename).split('/').pop() || f.filename); + const ftime = fmtTime(f.ctime || f.mtime); + return '
' + + '' + + '
' + + '
' + name + '
' + + '
' + ico + '' + fmtSize(f.size) + '' + + (ftime ? '' + ftime + '' : '') + '
' + + '
' + + '
'; + } + + function dirNodeHtml(node, depth) { + const dirs = sortDirsByName(Array.from(node.dirs.values())); + sortFilesByTime(node.files); + const collapsed = filesCollapsedDirs.has(node.path); + return '
' + + '
' + + '' + + '' + + '' + esc(node.name) + '' + + '' + countDirFiles(node) + ' 个文件' + + '
' + + '
' + + dirs.map(function (d) { return dirNodeHtml(d, depth + 1); }).join('') + + node.files.map(function (f) { return fileRowHtml(f, depth + 1); }).join('') + + '
' + + '
'; + } + + // 顶层:目录在上、文件在下(无目录的文件直接平铺) + function filesTreeHtml(root) { + return sortDirsByName(Array.from(root.dirs.values())).map(function (d) { return dirNodeHtml(d, 0); }).join('') + + sortFilesByTime(root.files).map(function (f) { return fileRowHtml(f, 0); }).join(''); + } + async function loadFiles() { if (!state.sessionId) return; // 重新拉取前清空旧选择,避免脏数据遗留 @@ -1711,30 +1800,9 @@ el.innerHTML = '
还没有文件,在对话框上传或让 AI 生成
'; return; } - // 后端已按创建时间倒序,这里再排一次防旧排序缓存;时间缺失排最后 - files.sort(function (a, b) { - const ca = a.ctime || a.mtime || 0, cb = b.ctime || b.mtime || 0; - return cb - ca; - }); filesAllPaths = files.map(f => f.filename); - el.innerHTML = files.map(f => { - // 预览策略:md/txt/html + 图片在 fs-viewer 预览(pdf 不预) - const low = f.filename.toLowerCase(); - const fsPv = /\.(md|txt|html?|png|jpe?g|gif|webp|svg)$/.test(low); - const ico = fsPv - ? '' - : ''; - const name = esc(String(f.filename).split('/').pop() || f.filename); - const ftime = fmtTime(f.ctime || f.mtime); - return '
' + - '' + - '
' + - '
' + name + '
' + - '
' + ico + '' + fmtSize(f.size) + '' + - (ftime ? '' + ftime + '' : '') + '
' + - '
' + - '
'; - }).join(''); + // 按目录分组渲染(后端已按时间倒序,组内再排一次防旧排序缓存) + el.innerHTML = filesTreeHtml(buildFilesTree(files)); // 重新渲染后刷新底部计数与按钮状态 updateFilesFooter(); } catch (e) { toast('读取文件失败:' + e.message); } @@ -1826,10 +1894,22 @@ } catch (e) { toast('删除失败:' + e.message); } } // 列表事件委托: - // 1) 点击勾选框:仅选中状态,不触发预览/下载; - // 2) 一旦处于多选模式(Set 非空),整行点击仅切换选中状态,不再预览/下载; - // 3) 未进入多选模式时,点击文件名/图标根据 row.dataset.preview 决定预览或下载。 + // 1) 点击目录头:折叠/展开该目录(记住状态,刷新列表后保持); + // 2) 点击勾选框:仅选中状态,不触发预览/下载; + // 3) 一旦处于多选模式(Set 非空),整行点击仅切换选中状态,不再预览/下载; + // 4) 未进入多选模式时,点击文件名/图标根据 row.dataset.preview 决定预览或下载。 $('files-list').addEventListener('click', e => { + // 目录头:切换折叠/展开 + const head = e.target.closest('.file-dir-head'); + if (head) { + const dirEl = head.closest('.file-dir'); + if (!dirEl) return; + const collapsed = dirEl.classList.toggle('collapsed'); + head.setAttribute('aria-expanded', collapsed ? 'false' : 'true'); + const dp = dirEl.dataset.dir; + if (dp) { if (collapsed) filesCollapsedDirs.add(dp); else filesCollapsedDirs.delete(dp); } + return; + } const row = e.target.closest('.file-row'); if (!row || !state.sessionId) return; const path = row.dataset.path;