feat(jd-database): v1.7 卡片交互+CSV 导出恢复+已下载角标+类目树增强

This commit is contained in:
2026-11-21 16:53:05 +08:00
parent 08b1f3f7f8
commit b20f0bb44a
6 changed files with 406 additions and 22 deletions
@@ -8,7 +8,7 @@
* 计数聚合走 jdCategoryCountTree(v1.6 Phase 1):节点徽标=自身名+子树叶和(与
* 「点节点 → 子树展开 IN 过滤」的命中集合同源),树上对不上的历史行归「树外兜底」组
* (点兜底行=单名等值旧形状,path 空数组交给页面回落)。 */
import { computed, ref, watch } from 'vue'
import { computed, nextTick, ref, watch } from 'vue'
import {
jdCategoryCountTree,
type JdCategoryCountedNode,
@@ -70,12 +70,58 @@ const counted = computed(() => jdCategoryCountTree(props.categoryOptions ?? []))
const totalCount = computed(() =>
(props.categoryOptions ?? []).reduce((sum, row) => sum + (row.productCount || 0), 0))
/** 展开态:按路径串('/' 连接)记,含树外兜底组头。一级默认展开(二级可见)。 */
const expanded = ref<Set<string>>(new Set())
/** 展开态:按路径串('/' 连接)记,含树外兜底组头。出厂(无记忆)一级默认展开(二级可见);
* v1.7 树增强:展开态持久化 localStorage `jd_db_cat_expand`(切页/重进不丢),损坏/不存在
* =按出厂态(宁可重展一级也不冒充用户记忆)。 */
const EXPAND_STORAGE_KEY = 'jd_db_cat_expand'
/** 读展开记忆:合法字符串数组才采信;返回「键集+是否有记忆」两个值——空数组 `[]` 是
* 用户主动「全部收起」的合法记忆,不得与「没存过」混为一谈(混了=全收起被重置成
* 一级展开,用户意图被吞)。坏形状/不可读回无记忆。 */
function loadExpandedKeys(): { keys: Set<string>; stored: boolean } {
try {
const raw = localStorage.getItem(EXPAND_STORAGE_KEY)
if (raw === null) return { keys: new Set(), stored: false }
const parsed: unknown = JSON.parse(raw)
if (!Array.isArray(parsed)) return { keys: new Set(), stored: false }
return { keys: new Set(parsed.filter((value): value is string => typeof value === 'string')), stored: true }
} catch {
return { keys: new Set(), stored: false }
}
}
const initialExpand = loadExpandedKeys()
const expanded = ref<Set<string>>(initialExpand.keys)
const expandKey = (path: string[]): string => path.join('/')
// 一级默认展开:init 时把每个一级的 key 放进集合。
for (const root of counted.value.roots) expanded.value.add(expandKey([root.name]))
function persistExpanded(): void {
try {
localStorage.setItem(EXPAND_STORAGE_KEY, JSON.stringify([...expanded.value]))
} catch {
// 存储不可写(隐私模式/配额满)=展开态只活在本会话,树功能不受影响。
}
}
/** 是否有持久化记忆:有(哪怕空集=全收起)=尊重记忆(不再补一级展开);无=树根到达时
* 补一级默认展开。注意 setup 时树数据通常还没到(页面先 scope 后列表),「一级默认展开」
* 必须挂树根 watch 而不是 init 直接遍历(旧写法 roots 为空什么也没加=默认展开从未真正生效)。 */
let hasStoredExpand = initialExpand.stored
watch(
() => counted.value.roots,
(roots) => {
if (hasStoredExpand) return
const next = new Set(expanded.value)
for (const root of roots) next.add(expandKey([root.name]))
// 补过一次就置位:之后类目重取不再补默认(用户中途收起的一级行不被重取展开回去)。
hasStoredExpand = true
if (next.size !== expanded.value.size) {
expanded.value = next
persistExpanded()
}
},
{ immediate: true },
)
// ---------------- 商品目录树:扁平渲染(深度缩进 + 祖先全展开才可见) ----------------
@@ -106,6 +152,32 @@ function toggleExpand(path: string[]): void {
if (next.has(key)) next.delete(key)
else next.add(key)
expanded.value = next
persistExpanded()
}
/** 全部展开(v1.7 树增强):把每个有子级的节点 path 全收进展开集(深树一次到底)。 */
function expandAll(): void {
const next = new Set<string>()
const walk = (nodes: JdCategoryCountedNode[], prefix: string[]): void => {
for (const node of nodes) {
const path = [...prefix, node.name]
if (node.children.length) {
next.add(expandKey(path))
walk(node.children, path)
}
}
}
walk(counted.value.roots, [])
expanded.value = next
hasStoredExpand = true
persistExpanded()
}
/** 全部收起:清空展开集(一级也收,用户主动点了收起就是要全收)。 */
function collapseAll(): void {
expanded.value = new Set()
hasStoredExpand = true
persistExpanded()
}
/** 点树行=选中即筛;非末级行顺手展开(下钻感,与京麦列表筛选器同形;收起走箭头)。 */
@@ -116,7 +188,8 @@ function pickNode(node: FlatNode): void {
emit('select-category', { name: node.node.name, path: node.path })
}
/** 外部选中态(重置/清选/页面回显):展开选中节点的全部祖先,让它可见。
/** 外部选中态(重置/清选/页面回显):展开选中节点的全部祖先,让它可见,并把选中行
* 滚进视口(v1.7 树增强;jsdom 无 scrollIntoView 自动跳过——测试不需要真滚)。
* immediate=页面带着已激活的类目筛选挂载侧栏时(如切换回本页),选中路径直接展开到位。 */
watch(
() => props.selectedCategory,
@@ -126,7 +199,16 @@ watch(
for (let i = 1; i < selected.path.length; i++) {
next.add(expandKey(selected.path.slice(0, i)))
}
expanded.value = next
if (next.size !== expanded.value.size) {
expanded.value = next
persistExpanded()
}
void nextTick(() => {
const picked = document.querySelector('.jd-side-tree .jd-side-row.selected')
if (picked && typeof picked.scrollIntoView === 'function') {
picked.scrollIntoView({ block: 'nearest' })
}
})
},
{ immediate: true },
)
@@ -280,6 +362,11 @@ function cancelShopRename(): void {
<span class="jd-side-name">全部商品</span>
<span class="jd-side-count">{{ totalCount }}</span>
</button>
<!-- 全部展开/收起(v1.7 树增强,对齐原型树控件):深树一次到底/一键收干净。 -->
<div v-if="flatTree.length" class="jd-side-tree-ops">
<button type="button" aria-label="展开全部类目" @click="expandAll">全部展开</button>
<button type="button" aria-label="收起全部类目" @click="collapseAll">全部收起</button>
</div>
<div class="jd-side-tree">
<button
v-for="flat in flatTree"
@@ -589,6 +676,28 @@ function cancelShopRename(): void {
background: #d9ecff;
}
/* 树顶展开/收起操作行(v1.7 树增强):弱化小按钮,不与树行抢视觉。 */
.jd-side-tree-ops {
display: flex;
gap: 6px;
padding: 0 4px 4px;
}
.jd-side-tree-ops button {
padding: 2px 8px;
border: 1px solid #dcdfe6;
border-radius: 6px;
background: #fff;
color: #909399;
font-size: 12px;
cursor: pointer;
}
.jd-side-tree-ops button:hover {
color: #409eff;
border-color: #c6e2ff;
}
.jd-side-extra {
margin-top: 4px;
border-top: 1px dashed #e4e7ed;