dsh-nav-fold 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.2.0 — 2026-11-15
4
+
5
+ 适配 DSH 0.2.2-rc.2 / rc.3 的侧栏结构变化(入口行外面多了一层 `div.panelEntry`,行内新增「取消固定」按钮)。
6
+
7
+ - 定位器改为结构判定:`button`(rc.1 及更早)与 `div > button.panelRow`(rc.2+)都认,不再依赖"子节点只能是 button/span"
8
+ - 窄屏/图标轨下外壳不渲染标题 span 时也能识别(用 aria-label 兜底)
9
+ - 保留行、计数徽标、紧凑网格、收起态全部按包裹层生效(标记同时打在包裹层与按钮上)
10
+ - rc.2+ 外壳已给每行配 Tooltip,不再叠加原生 `title`(避免双提示)
11
+ - 找不到导航时打印结构诊断(各 nav 的子节点标签与层数),便于宿主改版后定位
12
+ - 发布范围收紧为 `>=0.2.2-rc.1 <0.3.0`
13
+
3
14
  ## 0.1.0 — 2026-11-12
4
15
 
5
16
  首个公开版本。
@@ -12,4 +23,4 @@
12
23
  - 图标档 / 56px 图标轨下每行带原生 `title`,悬停显示插件名
13
24
  - 设置面板:行式布局,含自动档开关、保留行多选、恢复默认(二次确认)
14
25
  - 文案跟随 shell 语言(zh / en)
15
- - 不读写官方 `panelPins`,不修改官方行结构,卸载即完全复原
26
+ - 不读写官方 `panelPins`,不修改官方行结构,卸载即完全复原
package/README.md CHANGED
@@ -116,6 +116,10 @@ node --test test/*.test.mjs # 14 项,直接测上线产物 lib/client.js(v
116
116
 
117
117
  ## 版本契约(升级 DSH 后看这里)
118
118
 
119
- 定位器只依赖结构,不依赖 CSS Modules 的 hash 类名:`nav` 的子元素必须是 `button`/`span`(外加我们自己的控制条 `div`),至少一行是"图标 span + 文字 span"两段结构,且不含 `input/textarea/select/a/form`。图标轨判定优先读外壳的 `[data-sidebar-collapsed="true"]`(收起时内容是被平移进轨道的,布局宽度量不出来),读不到再退回宽度判断。
120
-
121
- 若控制台出现 `未找到「功能入口」导航`,说明外壳结构变了 —— 改 `looksLikePanelNav` 即可。
119
+ - 支持 **DSH >= 0.2.2-rc.1 < 0.3.0**(实测 rc.1 / rc.3)
120
+ - 侧栏入口行两种结构都适配:
121
+ - rc.1 及更早:`nav > button.panelRow`
122
+ - rc.2 起:`nav > div.panelEntry > button.panelRow`(行内可能还有「取消固定」按钮)
123
+ - 判据只看结构(标签层级 + aria-label),不认宿主 CSS Modules 的哈希类名
124
+ - 找不到入口导航时保持原生 UI,并在控制台打印一条结构诊断
125
+ - 升级 DSH 后若失效:先看那行诊断,再到仓库提 issue
package/lib/client.js CHANGED
@@ -29,7 +29,7 @@ const react = require("react");
29
29
  /** 插件名(cordis 插件标识) */
30
30
  const name = "nav-fold";
31
31
  /** 版本号:必须与 package.json.version 一致(有单测守着) */
32
- const PLUGIN_VERSION = "0.1.0";
32
+ const PLUGIN_VERSION = "0.2.0";
33
33
  /** 设置分区 / 控制条占用者 id */
34
34
  const ENTRY_ID = "nav-fold";
35
35
  /** 注入样式表的幂等标记 */
@@ -264,6 +264,12 @@ const CSS_TEXT = [
264
264
  'nav[data-navfold="compact"]>span>button{position:relative!important;justify-content:center!important;padding:0!important;min-height:32px!important;height:32px!important;width:100%!important}',
265
265
  'nav[data-navfold="compact"]>span>button>span+span{position:absolute!important;width:1px!important;height:1px!important;margin:-1px!important;padding:0!important;overflow:hidden!important;clip-path:inset(50%)!important;white-space:nowrap!important;border:0!important}',
266
266
  'nav[data-navfold="hidden"]>button:not([data-navfold-keep]){display:none!important}',
267
+ // rc.2 起每个入口多一层 div.panelEntry:同一套表现要按包裹层再写一遍
268
+ 'nav[data-navfold="compact"]>div:not([' + BAR_ATTR + ']){display:flex!important;min-width:0}',
269
+ 'nav[data-navfold="compact"]>div:not([' + BAR_ATTR + '])>button{flex:1!important;position:relative!important;justify-content:center!important;padding:0!important;min-height:32px!important;height:32px!important;border-radius:8px!important}',
270
+ 'nav[data-navfold="compact"]>div:not([' + BAR_ATTR + '])>button>span+span{position:absolute!important;width:1px!important;height:1px!important;margin:-1px!important;padding:0!important;overflow:hidden!important;clip:rect(0 0 0 0)!important;white-space:nowrap!important;border:0!important}',
271
+ 'nav[data-navfold="compact"]>div:not([' + BAR_ATTR + '])>button:not([class*="panelRow"]){display:none!important}',
272
+ 'nav[data-navfold="hidden"]>div:not([data-navfold-keep]):not([' + BAR_ATTR + ']){display:none!important}',
267
273
  "nav[data-navfold]>[" + BAR_ATTR + "] [data-navfold-count]{margin-right:2px;font-variant-numeric:tabular-nums;opacity:.8}",
268
274
  "nav[data-navfold]>[" + BAR_ATTR + "]{display:flex;align-items:center;justify-content:flex-end;gap:2px;height:22px;margin-top:2px;padding:0 2px;font-size:11px;line-height:1;color:var(--dsw-alias-label-tertiary,#8a8a8a)}",
269
275
  'nav[data-navfold="compact"]>[' + BAR_ATTR + "]{grid-column:1/-1}",
@@ -392,9 +398,59 @@ function autoWantsCompact(narrow, height) {
392
398
  return typeof height === "number" && height > 0 && height < AUTO_HEIGHT_PX;
393
399
  }
394
400
 
401
+ /** 是不是我们自己的控制条。 */
402
+ function isOwnBar(el) {
403
+ return !!el && el !== runtime.bar && typeof el.getAttribute === "function" && el.getAttribute(BAR_ATTR) !== null;
404
+ }
405
+
395
406
  /**
396
- * 判断一个元素是否是「功能入口」导航:子元素只能是 button / span(以及我们自己的
397
- * 控制条),至少一行是“图标 + 文字”两段结构,且不含输入控件或链接。
407
+ * 在包裹层里找行按钮(不依赖 querySelector,假 DOM 与真 DOM 都能跑)。
408
+ * rc.2 起结构是 div.panelEntry > button.panelRow,Tooltip 不额外包一层。
409
+ * @param host - 包裹层元素。
410
+ * @returns 行按钮或 null。
411
+ */
412
+ function findRowButton(host) {
413
+ if (!host || !host.children) return null;
414
+ const kids = host.children;
415
+ for (let i = 0; i < kids.length; i += 1) {
416
+ const kid = kids[i];
417
+ if (kid && kid.tagName === "BUTTON") return kid;
418
+ if (kid && kid.children) {
419
+ for (let j = 0; j < kid.children.length; j += 1) {
420
+ if (kid.children[j] && kid.children[j].tagName === "BUTTON") return kid.children[j];
421
+ }
422
+ }
423
+ }
424
+ return null;
425
+ }
426
+
427
+ /**
428
+ * 把 nav 的直接子节点归一成「入口行」。
429
+ * - rc.1 及更早:子节点本身就是 button.panelRow
430
+ * - rc.2 起:子节点是 div.panelEntry,按钮在它里面(行内可能还有 panelUnpin 等按钮)
431
+ * @param child - nav 的直接子节点。
432
+ * @returns { wrapper, button };不是入口行时返回 null。
433
+ */
434
+ function rowOf(child) {
435
+ if (!child || child === runtime.bar || isOwnBar(child)) return null;
436
+ if (child.tagName === "BUTTON") return { wrapper: child, button: child };
437
+ if (child.tagName !== "DIV") return null;
438
+ const btn = findRowButton(child);
439
+ return btn ? { wrapper: child, button: btn } : null;
440
+ }
441
+
442
+ /** 「更多」锚点里的按钮(rc.1 span.moreAnchor 与 rc.3 换了类名的 span 都按 tagName 认)。 */
443
+ function moreButtonOf(child) {
444
+ if (!child || child.tagName !== "SPAN") return null;
445
+ return findRowButton(child);
446
+ }
447
+
448
+ /**
449
+ * 判断一个元素是否是「功能入口」导航。
450
+ * 判据只看结构不认类名(宿主类名每版都变):
451
+ * - 子节点是入口行(button,或 rc.2+ 的 div 包裹 + 按钮)与「更多」span;
452
+ * - 至少 2 行,或 1 行且带 aria-label(窄屏时外壳不渲染标题 span,不能靠“两段结构”判定);
453
+ * - 不含输入控件 / 链接。
398
454
  * @param node - 待判定元素。
399
455
  * @returns 命中与否。
400
456
  */
@@ -402,23 +458,22 @@ function looksLikePanelNav(node) {
402
458
  if (!node || node.tagName !== "NAV") return false;
403
459
  const kids = node.children;
404
460
  if (!kids || kids.length === 0) return false;
405
- let buttons = 0;
406
- let twoPart = 0;
461
+ let rows = 0;
462
+ let labelled = 0;
407
463
  for (let i = 0; i < kids.length; i += 1) {
408
464
  const kid = kids[i];
409
- if (kid === runtime.bar) continue;
410
- const tag = kid.tagName;
411
- if (tag === "BUTTON") {
412
- buttons += 1;
413
- const inner = kid.children;
414
- if (inner && inner.length === 2) twoPart += 1;
465
+ if (kid === runtime.bar || isOwnBar(kid)) continue;
466
+ const row = rowOf(kid);
467
+ if (row) {
468
+ rows += 1;
469
+ if (typeof row.button.getAttribute === "function" && row.button.getAttribute("aria-label")) labelled += 1;
415
470
  continue;
416
471
  }
417
- if (tag === "SPAN") continue;
418
- if (tag === "DIV" && typeof kid.getAttribute === "function" && kid.getAttribute(BAR_ATTR) !== null) continue;
472
+ if (kid.tagName === "SPAN") continue;
419
473
  return false;
420
474
  }
421
- if (buttons === 0 || twoPart === 0) return false;
475
+ if (rows === 0) return false;
476
+ if (rows < 2 && labelled === 0) return false;
422
477
  if (typeof node.querySelector === "function" && node.querySelector("input, textarea, select, a, form")) return false;
423
478
  return true;
424
479
  }
@@ -431,11 +486,38 @@ function looksLikePanelNav(node) {
431
486
  function findNav(doc) {
432
487
  if (!doc || typeof doc.querySelectorAll !== "function") return null;
433
488
  const navs = doc.querySelectorAll("nav");
434
- if (!navs) return null;
489
+ if (!navs || navs.length === 0) return null;
490
+ let best = null;
491
+ let bestRows = 0;
435
492
  for (let i = 0; i < navs.length; i += 1) {
436
- if (looksLikePanelNav(navs[i])) return navs[i];
493
+ if (!looksLikePanelNav(navs[i])) continue;
494
+ let count = 0;
495
+ const kids = navs[i].children || [];
496
+ for (let j = 0; j < kids.length; j += 1) if (rowOf(kids[j])) count += 1;
497
+ if (count > bestRows) {
498
+ best = navs[i];
499
+ bestRows = count;
500
+ }
437
501
  }
438
- return null;
502
+ return best;
503
+ }
504
+
505
+ /** 找不到导航时打一条可复现的结构诊断(宿主改版时靠它定位)。 */
506
+ function describeNavs(doc) {
507
+ if (!doc || typeof doc.querySelectorAll !== "function") return "";
508
+ const navs = doc.querySelectorAll("nav");
509
+ const parts = [];
510
+ for (let i = 0; i < navs.length && i < 3; i += 1) {
511
+ const kids = navs[i].children || [];
512
+ const shape = [];
513
+ for (let j = 0; j < kids.length && j < 6; j += 1) {
514
+ const kid = kids[j];
515
+ const inner = kid && kid.children ? kid.children.length : 0;
516
+ shape.push(kid.tagName + (inner ? "(" + inner + ")" : ""));
517
+ }
518
+ parts.push("nav[" + i + "] " + kids.length + " kids: " + shape.join(","));
519
+ }
520
+ return parts.join(" | ");
439
521
  }
440
522
 
441
523
  /**
@@ -965,7 +1047,7 @@ function scan() {
965
1047
  if (!found) {
966
1048
  if (!runtime.warned && Date.now() - runtime.mountedAt > WARN_AFTER_MS) {
967
1049
  runtime.warned = true;
968
- warn("未找到「功能入口」导航(DSH 版本可能已变更),保持原生 UI");
1050
+ warn("未找到「功能入口」导航(DSH 版本可能已变更),保持原生 UI " + describeNavs(document));
969
1051
  }
970
1052
  return;
971
1053
  }
@@ -987,15 +1069,13 @@ function rowButtons(nav) {
987
1069
  for (let i = 0; i < nav.children.length; i += 1) {
988
1070
  const child = nav.children[i];
989
1071
  if (!child) continue;
990
- if (child.tagName === "BUTTON") {
991
- out.push(child);
1072
+ const row = rowOf(child);
1073
+ if (row) {
1074
+ out.push(row.button);
992
1075
  continue;
993
1076
  }
994
- if (child.tagName === "SPAN" && child.children) {
995
- for (let j = 0; j < child.children.length; j += 1) {
996
- if (child.children[j] && child.children[j].tagName === "BUTTON") out.push(child.children[j]);
997
- }
998
- }
1077
+ const more = moreButtonOf(child);
1078
+ if (more) out.push(more);
999
1079
  }
1000
1080
  return out;
1001
1081
  }
@@ -1007,7 +1087,10 @@ function rowButtons(nav) {
1007
1087
  */
1008
1088
  function syncTitles(nav) {
1009
1089
  const buttons = rowButtons(nav);
1010
- const wantTitles = runtime.iconOnly || runtime.mode === "compact";
1090
+ // rc.2 起外壳给每行配了 Tooltip:再叠原生 title 会双提示,交给外壳
1091
+ const rows = collectRows(nav);
1092
+ const shellHasTooltip = rows.some((row) => row.wrapper !== row.el);
1093
+ const wantTitles = (runtime.iconOnly || runtime.mode === "compact") && !shellHasTooltip;
1011
1094
  for (let i = 0; i < buttons.length; i += 1) {
1012
1095
  const el = buttons[i];
1013
1096
  if (!el || typeof el.setAttribute !== "function") continue;
@@ -1063,9 +1146,9 @@ function collectRows(nav) {
1063
1146
  const rows = [];
1064
1147
  if (!nav || !nav.children) return rows;
1065
1148
  for (let i = 0; i < nav.children.length; i += 1) {
1066
- const child = nav.children[i];
1067
- if (!child || child.tagName !== "BUTTON") continue;
1068
- rows.push({ label: rowLabel(child), el: child });
1149
+ const row = rowOf(nav.children[i]);
1150
+ if (!row) continue;
1151
+ rows.push({ label: rowLabel(row.button), el: row.button, wrapper: row.wrapper });
1069
1152
  }
1070
1153
  return rows;
1071
1154
  }
@@ -1092,9 +1175,12 @@ function syncKeeps(nav) {
1092
1175
  const row = rows[i];
1093
1176
  const keep = effective.indexOf(i) !== -1;
1094
1177
  if (keep) kept += 1;
1095
- if (typeof row.el.setAttribute === "function") {
1096
- if (keep) row.el.setAttribute("data-navfold-keep", "");
1097
- else if (typeof row.el.removeAttribute === "function") row.el.removeAttribute("data-navfold-keep");
1178
+ const targets = row.wrapper && row.wrapper !== row.el ? [row.wrapper, row.el] : [row.el];
1179
+ for (let t = 0; t < targets.length; t += 1) {
1180
+ const target = targets[t];
1181
+ if (!target) continue;
1182
+ if (typeof target.setAttribute === "function" && keep) target.setAttribute("data-navfold-keep", "");
1183
+ else if (typeof target.removeAttribute === "function") target.removeAttribute("data-navfold-keep");
1098
1184
  }
1099
1185
  }
1100
1186
  const changed = runtime.totals.kept !== kept || runtime.totals.total !== rows.length;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "dsh-nav-fold",
3
- "version": "0.1.0",
4
- "description": "nav-fold 导航折叠 · 把 DSH 左侧栏顶部的功能入口折叠成图标网格或收起(三态 + 三种控件模型,设置内可切换)",
3
+ "version": "0.2.0",
4
+ "description": "nav-fold 导航折叠 · 把 DSH 左侧栏顶部的功能入口折叠成图标网格或收起(三态 + 三种控件模型,兼容 rc.1 扁平行与 rc.2+ 包裹行)",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
7
7
  "exports": {
@@ -24,7 +24,7 @@
24
24
  ],
25
25
  "dsh": {
26
26
  "engines": {
27
- "dsh": ">=0.2.2-rc.1"
27
+ "dsh": ">=0.2.2-rc.1 <0.3.0"
28
28
  },
29
29
  "bundle": {
30
30
  "patch": "./cordis.patch.yml"
@@ -38,7 +38,7 @@
38
38
  }
39
39
  },
40
40
  "peerDependencies": {
41
- "@deepseek-ai/dsh": ">=0.2.2-rc.1",
41
+ "@deepseek-ai/dsh": ">=0.2.2-rc.1 <0.3.0",
42
42
  "react": "^18.2.0"
43
43
  },
44
44
  "scripts": {