From cb856fc943ec1f851b581cee87192cfe3695dce7 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 20 Nov 2026 11:38:13 +0800 Subject: [PATCH] fix: report contents use the sidebar's selected row; data chapters fold behind one row (BUG-2061) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The personal report's contents follow the prototype the owner approved on 2026-11-20: the nine reading sections are always listed at 40px rows, every other chapter sits behind one 「数据章节」 row with the real chapter count, and that row opens to the chapter families and then to chapters. The current row is the sidebar's selected row (neutral fill, radius 12, weight 600) with no coloured bar on the left. A folded row that holds the reader's place takes that fill, so the rail never unfolds on its own. Fold state is kept in localStorage behind try/catch. On phones the rail becomes a 「目录」 pill beside 「到底部」 that opens the same list as a bottom sheet. The scroll-spy now follows the node that holds a heading id after a lazy section redraws it; before, chapters drawn on scroll were never tracked. Also removes the left rail on the annual 「记住」 line and the rail and dots on the synastry period list. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01DbBXoyn7Hj3LCW3BUs5bZ8 --- docs/BUG_HISTORY.md | 18 ++ frontend/DESIGN.md | 4 +- frontend/src/app/annual-report.css | 2 +- frontend/src/app/globals.css | 155 +++++++------ .../personal-report-markdown-view.tsx | 207 +++++++++++++----- frontend/src/lib/report-toc-fold.ts | 83 +++++++ frontend/src/lib/report-toc-groups.ts | 10 + frontend/tests/personal-report-view.test.ts | 8 +- .../report-language-switch-20260930.test.tsx | 7 +- frontend/tests/report-reader-polish.test.tsx | 7 +- frontend/tests/report-toc-2061.test.tsx | 154 +++++++++++++ 11 files changed, 520 insertions(+), 135 deletions(-) create mode 100644 frontend/src/lib/report-toc-fold.ts create mode 100644 frontend/tests/report-toc-2061.test.tsx diff --git a/docs/BUG_HISTORY.md b/docs/BUG_HISTORY.md index 5bf1d90b6..a2be7e30a 100644 --- a/docs/BUG_HISTORY.md +++ b/docs/BUG_HISTORY.md @@ -27667,3 +27667,21 @@ - 验证与防复发:`frontend/tests/report-start-progress-2046.test.tsx` 断言卡片里的错误只在清空和点数不够时写入,其余走系统提示;`frontend/tests/results-as-toasts-2056.test.ts` 的名单加上开始弹窗和失败卡片各一处;`frontend/tests/annual-overlay-polish-2052.test.tsx` 断言说明句不再出现。没有在浏览器里看过。 - 关联记录:BUG-2046、BUG-2056、BUG-2052。 - 修复版本:`claude/ui-batch-1120c`。 + +## BUG-2061 | 个人报告的目录:当前项靠左边一条色条标出,几十个数据章节把九节解读挤在一起;手机上目录是一个折叠框;长报告滚到数据章节后目录不跟着走 + +- 状态:fixed-pending-verify(待测试环境真机截图确认)。 +- 首次发现 / 最近更新:2026-11-20 / 2026-11-20。 +- 触发与现象:产品负责人走查测试环境后提出目录要重做,并定下规则:选中状态不用左侧色条,用侧栏选中行的样子。原目录的当前项是左边一条 2 像素的强调色竖线;字号 13,行高不一;「解读 / 推运 / 本命数据」三组里,推运一组的十几个章节始终摊开。手机上目录是正文上方的一个折叠框,读到中间要滚回顶部才能用。另外两处也有左侧竖线:年运每个方面下的「记住」一行,合盘时间段列表(竖线加圆点)。 +- 触发条件:打开任何一份个人报告;年运、合盘报告的对应位置。 +- 根因:(1) 目录的当前项样式是 2026-09 定的,当时还没有「不用左侧色条」的规则。(2) 分组做的时候只把「本命数据」收起来,推运章节没有收。(3) 做核对时发现的旧缺陷:不分页的报告里,章节是滚到附近才画出来的,画出来时标题节点被换掉,目录只盯着最初的占位节点,所以滚进这些章节后当前项不再更新(分页的长报告按当前章节标,不受影响)。 +- 修复: + - 目录按产品 2026-11-20 确认的原型重做:「解读」九节始终全部列出,行高 40、字号 16;下面一条分隔线,然后是一行「数据章节」,右侧是真实的章节数(「N 章」),默认收起。点开后列出章节分组(推运、本命数据,各带自己的章节数;分组仍由章节标题判定,沿用 `groupReportToc`),分组再点开才是章节,当前章节下面列出它的小节。只有一个分组时章节直接挂在「数据章节」下面;没有解读的报告(英文版)默认展开。 + - 当前项是侧栏选中行的样子:中性底色 `--color-sidebar-selected`、圆角 12、字重 600,左边没有色条。读到收起的部分时,收起的那一行(「数据章节」或分组)带当前项的底色,目录不会在滚动时自己展开。 + - 展开收起的状态记在浏览器本地(读写都有保护,存不了时只在当前页面有效)。 + - 手机(860 以下):左下角一枚「目录」按钮,和右下角的「到底部」同一个样子、同一条线;点开是底部弹层(和「常用问题」同一套弹层),选一项后弹层收起并跳过去。 + - 滚动定位改为跟着当前持有该标题的节点走,章节重画后重新盯住。 + - 年运「记住」一行去掉左侧竖线和缩进;合盘时间段列表去掉竖线和圆点,成为普通列表。 +- 验证与防复发:`frontend/tests/report-toc-2061.test.tsx` 覆盖收起时的章节数、展开后的分组和章节、收起行的当前项、单一分组和无解读的情况、本地状态的容错、手机按钮和弹层,并钉住一条样式规则:目录、「记住」一行、合盘时间段列表的任何规则里都不能有左边框或前置圆点。钉住旧样式的三个测试按原值、新值、原因更新。版面核对:用产品样式表把真实组件放进无头浏览器(章节标题取自一份真实导出的报告,正文是示例文字),深浅两套、1280 和 390 两个宽度,看过收起、展开、分组展开、读到章节内、刷新后保持、收起时读到数据章节、手机按钮、弹层、弹层里选一项后的跳转,390 宽没有横向溢出。真实登录页面本机起不来,没有看过;分页的长报告(真实报告大多是这种)在本机只有测试覆盖,没有截图。 +- 关联记录:BUG-1631、BUG-2046。 +- 修复版本:`claude/report-toc-2061`。 diff --git a/frontend/DESIGN.md b/frontend/DESIGN.md index 97732458b..96d81f9b1 100644 --- a/frontend/DESIGN.md +++ b/frontend/DESIGN.md @@ -688,7 +688,7 @@ Action results and errors never take page layout. They are toasts from `src/lib/ - **判断不了的分盘提示 (2026-10-01, BUG-1139):** for a segment-v1 adoption whose card left D9/D10 undetermined, the longform body carries one plain paragraph directly under each such chart heading and its 数值位置表 heading — no new component, no blockquote, no colour. Under a chart heading it becomes the chart card's caption (`report-chart-grid-rehype.ts::parseCard` accepts exactly one paragraph between heading and fence), so the chart grid never splits. Added before the snapshot hash; legacy reports are untouched. - **到底部 / 回到顶部:** one floating control (`.report-edge-jump`, a component-library Button `variant="outline"` with class `report-edge-jump__button` and Lucide ArrowDown / ArrowUp, fixed bottom-right, same look as the chat's 跳到最新). It reads 「到底部」 until the reader is within 480px of the end, then 「回到顶部」 — never both. Sections below the fold mount lazily, so the click first mounts every section synchronously (`flushSync`) and then scrolls to `scrollHeight`, landing on the real end in one click. It is not a scroll follower and owns no anchor; it is hidden on paper. - **Long full-data reports (2026-10-08):** when the Markdown is longer than 200,000 characters and has more than one chapter, the reader shows one chapter at a time. The TOC and 「上一章 / 下一章」 (44px, screen only) change the chapter; the article carries `data-chapter-index` so a 中文 / English switch returns to the same chapter. 「到底部」 opens the last chapter. Shorter reports keep the lazy-section reader. There is no full-text search. Print still mounts every chapter. Markdown download stays on the existing 「导出」 sheet. -- **TOC rail:** a persistent right-hand column built from the outline's own `##`/`###` ids — no second slugger, and nothing that touches the chart-grid rehype pass. It is sticky under the report's own action bar, highlights the section in view via `IntersectionObserver`, and marks it with `aria-current="location"` plus a 2px `--color-action` bar. Placement is explicit (`grid-column: 2`) rather than DOM-ordered, so the narrow-screen drawer can stay first in the source. Below 860px the rail is gone and what remains is the collapsed 「目录」 drawer. +- **TOC rail:** a persistent right-hand column built from the outline's own `##`/`###` ids — no second slugger, and nothing that touches the chart-grid rehype pass. It is sticky under the report's own action bar, highlights the section in view via `IntersectionObserver`, and marks it with `aria-current="location"` plus the sidebar's selected-row look: `--color-sidebar-selected` fill, radius 12, weight 600, never a coloured bar on the left (2026-11-20, BUG-2061). 「解读」 is listed in full at 40px rows; every other chapter sits behind one 「数据章节」 row with the real chapter count, which opens to the chapter families and then to chapters. A folded row that holds the reader's place takes the current-row fill; the rail never unfolds by itself. Fold state is a per-viewer `localStorage` convenience. Placement is explicit (`grid-column: 2`) rather than DOM-ordered. Below 860px the rail is gone and what remains is a 「目录」 pill at the bottom-left (the twin of 「到底部」) that opens the same list as a bottom sheet. - **Paper stays paper:** the rail reads in the app palette because it is chrome. `--report-paper` / `--report-rule` / `--report-accent` are the document's own, deliberately out of step with the app accent, and nothing in this surface puts them on chrome or takes chrome colour onto the page. D11, continuing D3. - **Surface:** page floor `--color-canvas-soft`; the Markdown article is a `--color-canvas` sheet with a hairline and `--radius-lg`. Print flattens the sheet, hides chrome and the TOC, and pins the light palette. - **Print inside the shell:** printing takes the whole document, so the reader ships a second `media="print"` stylesheet that hides the sidebar and the 46px header **and unlocks them**: `.chat-app` / `.chat-panel` are `height: 100%; overflow: hidden`, which would otherwise clip a nine-section report to one page. It lives on the component rather than in `globals.css` so it exists only while the reader is mounted and no other surface has to know about it. The `@page` rule is unchanged. @@ -1083,7 +1083,7 @@ Three product rules for assistant markdown in `.message-markdown` (BUG-962 / 963 ## 14. 断点 -宽度切点只允许出现在 `globals.css` 顶部注释的白名单里。壳层切点以 `sidebarViewportForWidth` 为准:小于 768 移动,小于 1024 平板,否则桌面。组件级栅格(会员方案卡、校正候选)用 640/641。校正独立问题块(`.rectification-message-question.is-standalone`)与助手正文共用 `--assistant-content-inset`。报告目录在 860 塌成抽屉,因为那是第三栏的内容宽度,不是设备宽度。 +宽度切点只允许出现在 `globals.css` 顶部注释的白名单里。壳层切点以 `sidebarViewportForWidth` 为准:小于 768 移动,小于 1024 平板,否则桌面。组件级栅格(会员方案卡、校正候选)用 640/641。校正独立问题块(`.rectification-message-question.is-standalone`)与助手正文共用 `--assistant-content-inset`。报告目录在 860 收成左下角的「目录」按钮和底部弹层,因为那是第三栏的内容宽度,不是设备宽度。 禁止再引入 900 / 720 / 760 / 639 / 620 / 430 这类与壳层或彼此错位的切点。新切点必须先改白名单和 `viewport-breakpoint-contract.test.ts`。 diff --git a/frontend/src/app/annual-report.css b/frontend/src/app/annual-report.css index ec46fc3af..35373bf4f 100644 --- a/frontend/src/app/annual-report.css +++ b/frontend/src/app/annual-report.css @@ -103,7 +103,7 @@ @keyframes annual-pulse { 0%, 100% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--color-action) 45%, transparent); } 50% { box-shadow: 0 0 0 5px transparent; } } .annual-segment { display: flex; flex-direction: column; gap: var(--space-2); } /* 记住 line under each area (plain voice). */ -.annual-remember-line { padding-left: var(--space-3); border-left: 2px solid var(--color-border-strong); color: var(--color-ink-secondary); font-size: var(--type-body-sm); } +.annual-remember-line { color: var(--color-ink-secondary); font-size: var(--type-body-sm); } .annual-remember-line b { color: var(--color-ink); font-weight: 500; } .annual-evidence, .annual-timing { color: var(--color-ink-secondary); font-size: var(--type-body-sm); } .annual-evidence b, .annual-timing b { color: var(--color-ink); font-weight: 500; } diff --git a/frontend/src/app/globals.css b/frontend/src/app/globals.css index 28b6657ba..0b62274f7 100644 --- a/frontend/src/app/globals.css +++ b/frontend/src/app/globals.css @@ -4820,38 +4820,45 @@ input:not([type="radio"]):not([type="checkbox"]):not([class^="ant-"]):not([class overflow: auto; padding: var(--space-2) 0 var(--space-3); } -.personal-report-toc ol { +/* Contents rows (BUG-2061). Scoped to the groups box, not the rail, because + the phone sheet renders the same list in a portal outside the rail. + The current row is the sidebar's selected row: neutral fill, radius 12, + weight 600. No coloured edge on the left. */ +.personal-report-toc-groups ol { display: grid; gap: 2px; margin: 0; padding: 0; list-style: none; } -/* Two levels you can tell apart without the current-item pill (2026-09-29): - chapters are ink and semibold with air above each group; sections sit a - full step in and one tone lighter. */ -.personal-report-toc li[data-level="2"] + li[data-level="2"], -.personal-report-toc li[data-level="3"] + li[data-level="2"] { margin-top: var(--space-3); } -.personal-report-toc li[data-level="2"] > a { color: var(--color-ink); font-weight: 600; } -.personal-report-toc li[data-level="3"] { padding-left: var(--space-5); } -.personal-report-toc a { - display: block; - padding: 6px var(--space-2); - border-left: 2px solid transparent; - border-radius: var(--radius-sm); +.personal-report-toc-groups a { + display: flex; + align-items: center; + min-height: 40px; + padding: 6px var(--space-3); + border-radius: var(--radius-lg); color: var(--color-ink-secondary); - font-size: var(--type-caption); - line-height: 1.45; + font-size: var(--type-body-sm); + line-height: 1.4; text-decoration: none; } -/* The rail is app chrome, not paper, so it reads in the app palette. The three - --report-* tokens stay off it by design (D11/D3). */ -.personal-report-toc a.is-current { - border-left-color: var(--color-action); - background: var(--color-canvas-muted); +.personal-report-toc-groups ol[data-nested="true"] a { + min-height: 34px; + padding-left: var(--space-6); + font-size: var(--type-caption); +} +.personal-report-toc-groups ol[data-nested="true"] li[data-level="3"] a { padding-left: var(--space-8); } +.personal-report-toc-groups a:hover, +.personal-report-toc-groups .personal-report-toc-row:hover { + background: color-mix(in srgb, var(--color-sidebar-selected) 55%, transparent); color: var(--color-ink); } -.personal-report-toc a:hover { color: var(--color-ink); } +.personal-report-toc-groups a.is-current, +.personal-report-toc-groups .personal-report-toc-row.is-current { + background: var(--color-sidebar-selected); + color: var(--color-ink); + font-weight: 600; +} /* 「到底部」/「回到顶部」: one floating pill over the reader's bottom-right corner, same look as the chat's 跳到最新. Out of flow, so the paper never reflows when it flips direction. Hidden on paper via .personal-report-screen-only. */ @@ -4884,6 +4891,8 @@ input:not([type="radio"]):not([type="checkbox"]):not([class^="ant-"]):not([class } @media (max-width: 767px) { .report-edge-jump { right: var(--space-4); bottom: calc(var(--space-4) + env(safe-area-inset-bottom)); } + /* 「目录」 sits on the same line as its twin on the right. */ + .personal-report-md-layout > .personal-report-toc { bottom: calc(var(--space-4) + env(safe-area-inset-bottom)); } } .personal-report-chapter-pager { display: flex; @@ -4911,15 +4920,7 @@ input:not([type="radio"]):not([type="checkbox"]):not([class^="ant-"]):not([class color: var(--color-ink-secondary); font-size: var(--type-body-sm); } -.personal-report-toc-drawer { display: none; } -.personal-report-toc-desktop p { - margin: 0 0 var(--space-2); - padding: 0 var(--space-2); - color: var(--color-ink-tertiary); - font-size: var(--type-caption); - font-weight: 500; - letter-spacing: .08em; -} +.personal-report-toc .personal-report-toc-trigger { display: none; } .personal-report-md-article { min-width: 0; overflow-wrap: anywhere; @@ -5095,32 +5096,46 @@ input:not([type="radio"]):not([type="checkbox"]):not([class^="ant-"]):not([class text-decoration: none; } .personal-report-reading-chips a:hover { background: var(--color-canvas-strong); } -/* Contents in three groups: 解读 / 推运 / 本命数据 (folded). */ -.personal-report-toc-groups { display: grid; gap: var(--space-5); } -.personal-report-toc-group { display: grid; gap: var(--space-1); } +/* 解读 in full, then one 「数据章节」 row that opens to the chapter families. */ +.personal-report-toc-groups { display: grid; gap: var(--space-2); } +.personal-report-toc-group { display: grid; gap: 2px; } .personal-report-toc-group-label { margin: 0; - padding: 0 var(--space-2); + padding: var(--space-2) var(--space-3) var(--space-1); color: var(--color-ink-tertiary); - font-size: var(--type-overline); - letter-spacing: .06em; + font-size: var(--type-caption); + line-height: 1.5; } -.personal-report-toc .personal-report-toc-group-toggle { +.personal-report-toc-rule { height: 1px; margin: var(--space-2) var(--space-3); background: var(--color-border); } +.personal-report-toc-groups .personal-report-toc-row { display: flex; width: 100%; height: auto; min-height: 40px; - justify-content: flex-start; - gap: var(--space-2); - padding: 0 var(--space-2); - color: var(--color-ink-tertiary); - font-size: var(--type-overline); + align-items: center; + justify-content: space-between; + gap: var(--space-3); + padding: 0 var(--space-3); + border-radius: var(--radius-lg); + background: transparent; + color: var(--color-ink-secondary); + font-size: var(--type-body-sm); font-weight: 400; - letter-spacing: .06em; + text-align: left; + box-shadow: none; } -.personal-report-toc-group-count { margin-left: auto; letter-spacing: 0; } -.personal-report-toc-group-toggle svg { width: 14px; height: 14px; transition: transform 150ms ease-out; } -.personal-report-toc-group-toggle svg[data-open="true"] { transform: rotate(180deg); } +.personal-report-toc-groups .personal-report-toc-row-sub { + min-height: 34px; + padding-left: var(--space-6); + font-size: var(--type-caption); +} +.personal-report-toc-count { flex: none; color: var(--color-ink-tertiary); font-size: var(--type-caption); font-weight: 400; } +/* Phone: the same list in a bottom sheet (same sheet frame as 常用问题). */ +.personal-report-toc-sheet[data-slot="drawer-popup"] { padding: 10px 0 calc(16px + env(safe-area-inset-bottom)); border-color: var(--color-border); border-radius: 20px 20px 0 0; background: var(--color-canvas); } +.personal-report-toc-sheet [data-slot="drawer-swipe-handle"] { height: 4px; margin-bottom: 6px; } +.personal-report-toc-sheet [data-slot="drawer-swipe-handle"]::after { width: 36px; height: 4px; background: var(--color-canvas-strong); } +.personal-report-toc-sheet-title { margin: 0; padding: 6px 20px; color: var(--color-ink); font-size: 16px; font-weight: 600; line-height: 1.4; } +.personal-report-toc-sheet-body { max-height: min(68dvh, 560px); overflow-y: auto; overscroll-behavior: contain; padding: 0 8px; } .personal-report-md-article .personal-report-basis-label { margin: var(--space-8) 0 0; padding-top: var(--space-4); @@ -5129,13 +5144,6 @@ input:not([type="radio"]):not([type="checkbox"]):not([class^="ant-"]):not([class font-size: var(--type-overline); letter-spacing: .06em; } -.personal-report-toc-drawer summary { - min-height: 44px; - display: flex; - align-items: center; - cursor: pointer; - font-weight: 600; -} .personal-report-md-lead, .personal-report-md-section { min-width: 0; } .personal-report-md-article h4.personal-report-chart-heading { @@ -5161,20 +5169,36 @@ input:not([type="radio"]):not([type="checkbox"]):not([class^="ant-"]):not([class @media (max-width: 860px) { .personal-report-md-layout { grid-template-columns: 1fr; } .personal-report-md-article { grid-column: auto; grid-row: auto; } - /* The persistent rail is gone below this width; what is left is the collapsed - drawer, which needs the box the rail no longer draws. */ + /* The persistent rail is gone below this width; what is left is one 「目录」 + pill over the bottom-left corner (the twin of 到底部 on the right) that + opens the contents as a bottom sheet. */ .personal-report-toc { - grid-column: auto; - grid-row: auto; - position: static; + position: fixed; + top: auto; + left: var(--space-4); + bottom: var(--space-6); + z-index: 15; max-height: none; - padding: var(--space-3); - border: 1px solid var(--color-border); - border-radius: var(--radius-lg); - background: var(--color-canvas); + overflow: visible; + padding: 0; } .personal-report-toc-desktop { display: none; } - .personal-report-toc-drawer { display: block; } + .personal-report-toc .personal-report-toc-trigger { + min-height: 44px; + display: inline-flex; + align-items: center; + gap: var(--space-2); + padding: 0 var(--space-4); + border: 1px solid var(--color-border); + border-radius: 999px; + background: var(--color-canvas); + color: var(--color-ink); + box-shadow: var(--shadow-elevated); + font: inherit; + font-size: var(--type-body-sm); + cursor: pointer; + } + .personal-report-toc .personal-report-toc-trigger svg { width: 16px; height: 16px; flex: 0 0 auto; } } .personal-report-actions { position: sticky; @@ -5518,9 +5542,8 @@ button.synastry-dd-add { border-radius: 0 0 var(--radius-md) var(--radius-md); } .synastry-section p + p { margin-top: 10px; } .synastry-section .synastry-gist { font-weight: 600; } .synastry-section .lead { margin-top: 0; color: var(--color-ink-secondary); font-size: 16px; } -.synastry-tl { list-style: none; margin: 6px 0 0; padding: 0 0 0 16px; display: grid; gap: 16px; border-left: 2px solid var(--color-border); } -.synastry-tl li { position: relative; } -.synastry-tl li::before { content: ""; position: absolute; left: -22px; top: 6px; width: 10px; height: 10px; border-radius: 50%; background: var(--color-action); } +/* A plain list of periods: no rail and no dots down the left (BUG-2061). */ +.synastry-tl { list-style: none; margin: 6px 0 0; padding: 0; display: grid; gap: 16px; } .synastry-tl-h b { font-weight: 500; } .synastry-why { margin-top: 12px; padding: 8px 14px; border: 1px solid var(--color-border); border-radius: 10px; background: var(--color-canvas); } .synastry-why summary { color: var(--color-ink-secondary); font-size: 16px; cursor: pointer; } diff --git a/frontend/src/components/personal-report/personal-report-markdown-view.tsx b/frontend/src/components/personal-report/personal-report-markdown-view.tsx index 6cc5bd25c..82508809e 100644 --- a/frontend/src/components/personal-report/personal-report-markdown-view.tsx +++ b/frontend/src/components/personal-report/personal-report-markdown-view.tsx @@ -9,7 +9,17 @@ import { fencedLanguage, fencedSource } from "@/components/markdown-code-block"; import { ReportFocusCard } from "@/components/personal-report/report-focus-card"; import { ReportReadingSection } from "@/components/personal-report/report-reading-section"; import { REPORT_READING_ANCHOR as READING_ANCHOR, visibleReadingSections, type ReportReading } from "@/lib/personal-report-reading-contract"; -import { groupReportToc, isReadingAnchor, type ReportTocGroup } from "@/lib/report-toc-groups"; +import { groupReportToc, isReadingAnchor, REPORT_TOC_CHAPTERS_LABEL, reportTocChapterCountLabel, type ReportTocGroup } from "@/lib/report-toc-groups"; +import { + reportTocFoldServerSnapshot, + reportTocFoldSnapshot, + saveReportTocFold, + subscribeReportTocFold, + toggleReportTocData, + toggleReportTocGroup, + type ReportTocFold, +} from "@/lib/report-toc-fold"; +import { Drawer, DrawerContent, DrawerTitle, DrawerTrigger } from "@/components/ui/drawer"; import { chartAriaLabel, parseReportChartBlock, @@ -22,7 +32,7 @@ import { bindFactTablePrint } from "@/lib/report-fact-table-print"; import { remarkCjkStrong } from "@/lib/remark-cjk-strong"; import { NorthIndianChartSvg } from "@/components/personal-report/vedic-chart-svg"; import { ReportEdgeJump } from "@/components/personal-report/report-edge-jump"; -import { ChevronDown } from "lucide-react"; +import { List as ListIcon } from "lucide-react"; import { Button } from "@/components/ui/button"; import type { ReportLanguage } from "@/lib/report-language"; import { @@ -304,7 +314,9 @@ function ReportToc({ const label = language === "en" ? "Contents" : "目录"; const headings = useMemo(() => groups.flatMap((group) => group.items), [groups]); const [observedId, setObservedId] = useState(headings[0]?.id ?? ""); - const [dataOpen, setDataOpen] = useState(false); + // What the viewer opened stays open on this device (BUG-2061). + const fold = useSyncExternalStore(subscribeReportTocFold, reportTocFoldSnapshot, reportTocFoldServerSnapshot); + const [sheetOpen, setSheetOpen] = useState(false); // A reading section shown on the paged first chapter is still a scroll target. const activeId = (activeHeadingId && !isReadingAnchor(observedId) ? activeHeadingId : "") || observedId; @@ -317,87 +329,159 @@ function ReportToc({ const id = visible[0]?.target.id; if (id) setObservedId(id); }, { rootMargin: "-20% 0px -70% 0px", threshold: [0, 1] }); - for (const item of headings) { - const node = document.getElementById(item.id); - if (node) observer.observe(node); - } - return () => observer.disconnect(); + // A lazy section swaps its placeholder heading for the drawn chapter, and a + // paged report swaps the whole chapter: watch whichever node holds the id now. + const watched = new Map(); + const sync = () => { + for (const item of headings) { + const node = document.getElementById(item.id); + const before = watched.get(item.id); + if (node === before) continue; + if (before) observer.unobserve(before); + if (node) { + observer.observe(node); + watched.set(item.id, node); + } else { + watched.delete(item.id); + } + } + }; + sync(); + const article = document.querySelector(".personal-report-md-article"); + let frame = 0; + const redrawn = article && typeof MutationObserver !== "undefined" + ? new MutationObserver(() => { + if (frame) return; + frame = requestAnimationFrame(() => { + frame = 0; + sync(); + }); + }) + : null; + if (article) redrawn?.observe(article, { childList: true, subtree: true }); + return () => { + if (frame) cancelAnimationFrame(frame); + redrawn?.disconnect(); + observer.disconnect(); + }; }, [headings]); - const body = ( - setDataOpen((open) => !open)} - /> - ); + // In the phone sheet a pick closes the sheet first, then moves the page. + const pickFromSheet = useCallback((id: string) => { + setSheetOpen(false); + if (onSelect) onSelect(id); + else requestAnimationFrame(() => document.getElementById(id)?.scrollIntoView()); + }, [onSelect]); + + const shared = { + activeId, + fold, + groups, + language, + onToggleData: () => saveReportTocFold(toggleReportTocData(reportTocFoldSnapshot())), + onToggleGroup: (key: string) => saveReportTocFold(toggleReportTocGroup(reportTocFoldSnapshot(), key)), + }; return ( ); } /** - * 解读 / 推运 / 本命数据. The data group (twenty-odd engine tables) stays - * folded to one line until opened, or until the reader is inside it. + * 「解读」 is always listed in full. Every other chapter sits behind one + * 「数据章节」 row that shows the real chapter count; opened, it lists the + * chapter families (推运 / 本命数据) with their own counts, and a family opens + * to its chapters. A folded row that holds the reader's place takes the + * current-row fill, so the place is never lost and the rail never reflows on + * its own while scrolling. */ -function TocGroups({ +export function TocGroups({ groups, activeId, - dataOpen, + fold, language, onSelect, onToggleData, + onToggleGroup, }: { groups: readonly ReportTocGroup[]; activeId: string; - dataOpen: boolean; + fold: ReportTocFold; language: ReportLanguage; onSelect?: (id: string) => void; onToggleData: () => void; + onToggleGroup: (key: string) => void; }) { + const reading = groups.find((group) => group.key === "reading"); + const families = groups.filter((group) => group.key !== "reading"); + const chapterTotal = families.reduce((sum, group) => sum + group.chapterCount, 0); + const activeFamily = families.find((group) => group.items.some((item) => item.id === activeId)); + // A report with no reading has nothing else to show: its chapters start open. + const dataOpen = fold.dataOpen || !reading; return (
- {groups.map((group) => { - const holdsActive = group.items.some((item) => item.id === activeId); - if (group.key !== "data") { - return ( -
-

{group.label}

- -
- ); - } - const open = dataOpen || holdsActive; - const count = language === "en" ? `${group.chapterCount} tables` : `${group.chapterCount} 项`; - return ( -
- - {open ? : null} -
- ); - })} + {reading ? ( +
+

{reading.label}

+ +
+ ) : null} + {reading && families.length > 0 ? ); } @@ -576,15 +660,18 @@ export function tocOpenChapter(items: readonly LongformHeading[], activeId: stri function TocList({ items, activeId, + nested = false, onSelect, }: { items: readonly LongformHeading[]; activeId: string; + /** Chapters listed inside 「数据章节」: smaller rows, one step in. */ + nested?: boolean; onSelect?: (id: string) => void; }) { - const open = tocOpenChapter(items, activeId); + const open = activeId ? tocOpenChapter(items, activeId) : ""; return ( -
    +
      {items.filter((item, index) => item.level <= 2 || tocChapterOf(items, index) === open).map((item) => (
    1. typeof item === "string" && item.length <= 40).slice(0, 8) + : []; + return { dataOpen: record.dataOpen === true, openGroups }; + } catch { + return REPORT_TOC_FOLD_CLOSED; + } +} + +export function toggleReportTocData(fold: ReportTocFold): ReportTocFold { + return { ...fold, dataOpen: !fold.dataOpen }; +} + +export function toggleReportTocGroup(fold: ReportTocFold, key: string): ReportTocFold { + const openGroups = fold.openGroups.includes(key) + ? fold.openGroups.filter((item) => item !== key) + : [...fold.openGroups, key]; + return { ...fold, openGroups }; +} + +let cached: ReportTocFold | null = null; +const listeners = new Set<() => void>(); + +/** Stable snapshot for `useSyncExternalStore`; the first call reads storage. */ +export function reportTocFoldSnapshot(): ReportTocFold { + if (cached) return cached; + try { + cached = parseReportTocFold(globalThis.localStorage?.getItem(REPORT_TOC_FOLD_KEY)); + } catch { + cached = REPORT_TOC_FOLD_CLOSED; + } + return cached; +} + +/** The server and the first client render both show everything folded. */ +export function reportTocFoldServerSnapshot(): ReportTocFold { + return REPORT_TOC_FOLD_CLOSED; +} + +export function subscribeReportTocFold(listener: () => void): () => void { + listeners.add(listener); + return () => { + listeners.delete(listener); + }; +} + +export function saveReportTocFold(next: ReportTocFold): void { + cached = next; + try { + globalThis.localStorage?.setItem(REPORT_TOC_FOLD_KEY, JSON.stringify(next)); + } catch { + // Private windows and blocked site data: the choice lasts for this page only. + } + for (const listener of listeners) listener(); +} + +/** Tests only: forget the in-memory copy so the next snapshot reads storage again. */ +export function resetReportTocFoldForTest(): void { + cached = null; +} diff --git a/frontend/src/lib/report-toc-groups.ts b/frontend/src/lib/report-toc-groups.ts index a2b2ddb1a..47385883b 100644 --- a/frontend/src/lib/report-toc-groups.ts +++ b/frontend/src/lib/report-toc-groups.ts @@ -24,6 +24,16 @@ const GROUP_LABEL: Record> = { en: { reading: "Reading", timing: "Timing", data: "Natal data" }, }; +/** + * The folded entry that holds every chapter family except 解读 (BUG-2061): + * 「数据章节」 opens to 推运 and 本命数据, each with its own chapter count. + */ +export const REPORT_TOC_CHAPTERS_LABEL: Record = { zh: "数据章节", en: "Data chapters" }; + +export function reportTocChapterCountLabel(count: number, language: ReportLanguage): string { + return language === "en" ? `${count} chapters` : `${count} 章`; +} + /** Dasha, annual-chart and transit chapters; matched on the chapter title. */ const TIMING_TITLE = /大运|年度|流年|推运|行运|Sade\s*Sati|Dasha|Varshaphala|Mudda|Patyayini|Annual|Transit/i; diff --git a/frontend/tests/personal-report-view.test.ts b/frontend/tests/personal-report-view.test.ts index 021796642..e81d7c137 100644 --- a/frontend/tests/personal-report-view.test.ts +++ b/frontend/tests/personal-report-view.test.ts @@ -266,8 +266,12 @@ test("the report TOC is a persistent right-hand rail that leaves the paper palet const layout = cssDeclarations(".personal-report-md-layout", globals); assert.match(layout, /grid-template-columns: minmax\(0, 1fr\) minmax\(11rem, 15rem\)/); // The rail is app chrome; the three paper tokens are not on it (D11/D3). - const current = cssDeclarations(".personal-report-toc a.is-current", globals); - assert.match(current, /border-left-color: var\(--color-action\)/); + // 原值:`.personal-report-toc a.is-current` 用 `border-left-color: var(--color-action)` 标出当前项 + // 新值:当前项是侧栏选中行的中性底色,左边没有彩色竖线 + // 原因:BUG-2061,产品 2026-11-20 定的规则:选中状态不用左侧色条 + const current = globals.match(/\.personal-report-toc-groups a\.is-current,[^{]*\{[^}]*\}/)?.[0] ?? ""; + assert.match(current, /background: var\(--color-sidebar-selected\)/); + assert.doesNotMatch(current, /border-left/); assert.doesNotMatch(toc + current, /--report-(?:paper|rule|accent)/); assert.match(globals, /--report-accent: #85432f;/); }); diff --git a/frontend/tests/report-language-switch-20260930.test.tsx b/frontend/tests/report-language-switch-20260930.test.tsx index 1d4a31b00..3759faeda 100644 --- a/frontend/tests/report-language-switch-20260930.test.tsx +++ b/frontend/tests/report-language-switch-20260930.test.tsx @@ -50,14 +50,17 @@ async function openReader(initialLanguage: "zh" | "en") { for (let i = 0; i < 5 && !h.elements().some(node => hasClass(node, "personal-report-md-layout")); i += 1) await h.idle(); const read = () => { const layouts = h.elements().filter(node => hasClass(node, "personal-report-md-layout")); - const toc = h.elements().find(node => hasClass(node, "personal-report-toc-desktop")); + // 原值:读 `.personal-report-toc-desktop` 的第一个子节点(栏头「目录」) + // 新值:读手机上的「目录」按钮;桌面栏不再有栏头,第一行就是「解读」 + // 原因:BUG-2061 目录重做。要验证的仍是同一件事:目录的界面文字跟着报告语言走 + const toc = h.elements().find(node => hasClass(node, "personal-report-toc-trigger")); // 原值:选中的语言是 BUTTON 且 aria-pressed="true" // 新值:分段页签 role=tab 且 aria-selected="true" // 原因:K5 报告语言改用和星盘页盘型条一样的 Tabs。正文仍只能有一份 const pressed = h.elements().find(node => node.getAttribute("role") === "tab" && node.getAttribute("aria-selected") === "true"); // The article only: the 「到底部」 jump beside it is app chrome and stays Chinese (English edition decision 6). const articles = h.elements().filter(node => hasClass(node, "personal-report-md-article")); - return { layouts: layouts.length, reader: articles.map(node => node.text).join(""), toc: toc?.childNodes[0]?.text, pressed: pressed?.text, url: location.href }; + return { layouts: layouts.length, reader: articles.map(node => node.text).join(""), toc: toc?.text, pressed: pressed?.text, url: location.href }; }; const pick = async (label: string) => { const button = h.elements().find(node => node.getAttribute("role") === "tab" && node.text === label); diff --git a/frontend/tests/report-reader-polish.test.tsx b/frontend/tests/report-reader-polish.test.tsx index c37949e65..9679ed2a1 100644 --- a/frontend/tests/report-reader-polish.test.tsx +++ b/frontend/tests/report-reader-polish.test.tsx @@ -83,8 +83,11 @@ test("the edge jump stays off paper", () => { }); test("TOC chapters and sections read as two levels", () => { - assert.match(globals, /\.personal-report-toc li\[data-level="2"\] > a \{ color: var\(--color-ink\); font-weight: 600; \}/); - assert.match(globals, /\.personal-report-toc li\[data-level="3"\] \{ padding-left: var\(--space-5\); \}/); + // 原值:章节 `li[data-level="2"] > a` 墨色加粗、小节 `li[data-level="3"]` 缩进 --space-5 + // 新值:只有当前行加粗(侧栏选中行的样子);数据章节里的章节缩进 --space-6,章节下的小节再缩到 --space-8 + // 原因:BUG-2061 目录按产品 2026-11-20 确认的原型重做,层级靠缩进和字号区分,粗体只留给当前位置 + assert.match(globals, /\.personal-report-toc-groups ol\[data-nested="true"\] a \{[^}]*padding-left: var\(--space-6\);/); + assert.match(globals, /\.personal-report-toc-groups ol\[data-nested="true"\] li\[data-level="3"\] a \{ padding-left: var\(--space-8\); \}/); }); test("zebra bands live on report tables only, never on chat tables", () => { diff --git a/frontend/tests/report-toc-2061.test.tsx b/frontend/tests/report-toc-2061.test.tsx new file mode 100644 index 000000000..7cfe55885 --- /dev/null +++ b/frontend/tests/report-toc-2061.test.tsx @@ -0,0 +1,154 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { readFileSync } from "node:fs"; +import { renderToStaticMarkup } from "react-dom/server"; + +import { TocGroups } from "../src/components/personal-report/personal-report-markdown-view.tsx"; +import { groupReportToc } from "../src/lib/report-toc-groups.ts"; +import type { LongformHeading } from "../src/lib/personal-report-longform-outline.ts"; +import { + REPORT_TOC_FOLD_CLOSED, + REPORT_TOC_FOLD_KEY, + parseReportTocFold, + reportTocFoldSnapshot, + resetReportTocFoldForTest, + saveReportTocFold, + toggleReportTocData, + toggleReportTocGroup, +} from "../src/lib/report-toc-fold.ts"; + +const globals = readFileSync(new URL("../src/app/globals.css", import.meta.url), "utf8"); +const annualCss = readFileSync(new URL("../src/app/annual-report.css", import.meta.url), "utf8"); + +const HEADINGS: LongformHeading[] = [ + { id: "a", level: 2, title: "大运传统参考(通用)" }, + { id: "a1", level: 3, title: "金星大运" }, + { id: "b", level: 2, title: "本命盘原始位置" }, + { id: "b1", level: 3, title: "行星表" }, + { id: "c", level: 2, title: "年度 Mudda 大运行" }, + { id: "d", level: 2, title: "KP 行星征象星" }, + { id: "e", level: 2, title: "Shadbala 六分量" }, +]; +const READING = [{ id: "overview", title: "总评" }, { id: "love", title: "感情" }]; +const noop = () => {}; + +function render(fold = REPORT_TOC_FOLD_CLOSED, activeId = "report-reading-love", headings = HEADINGS, reading = READING) { + return renderToStaticMarkup( + , + ); +} + +test("BUG-2061 解读 is listed in full; 数据章节 is one folded row with the real chapter count", () => { + const markup = render(); + assert.match(markup, />总评感情]*>数据章节<\/span>5 章<\/span>/); + assert.doesNotMatch(markup, /大运传统参考|本命盘原始位置|推运/); +}); + +test("BUG-2061 opened, 数据章节 lists the chapter families with their own counts; a family opens to its chapters", () => { + const opened = render({ dataOpen: true, openGroups: [] }); + assert.match(opened, /数据章节<\/span>收起<\/span>/); + assert.match(opened, /data-group="timing"[^>]*>推运<\/span>2<\/span>/); + assert.match(opened, /data-group="data"[^>]*>本命数据<\/span>3<\/span>/); + assert.doesNotMatch(opened, /大运传统参考/); + + const family = render({ dataOpen: true, openGroups: ["timing"] }); + assert.match(family, /
        .*大运传统参考(通用).*年度 Mudda 大运行/); + assert.doesNotMatch(family, /本命盘原始位置/); + // A chapter's sections show only while the reader is inside that chapter. + assert.doesNotMatch(family, /金星大运/); + assert.match(render({ dataOpen: true, openGroups: ["timing"] }, "a"), /金星大运/); +}); + +test("BUG-2061 a folded row that holds the reader's place takes the current-row look", () => { + assert.match(render(REPORT_TOC_FOLD_CLOSED, "b1"), /class="[^"]*personal-report-toc-row is-current[^"]*"[^>]*>数据章节/); + const familyFolded = render({ dataOpen: true, openGroups: [] }, "b1"); + assert.match(familyFolded, /class="[^"]*personal-report-toc-row-sub is-current[^"]*"[^>]*data-group="data"|data-group="data"[^>]*class="[^"]*personal-report-toc-row-sub is-current/); + assert.doesNotMatch(familyFolded, /personal-report-toc-row is-current/); + // Reading a 解读 section: nothing folded is marked. + assert.doesNotMatch(render(), /personal-report-toc-row[^"]* is-current/); +}); + +test("BUG-2061 one chapter family hangs straight off 数据章节; a report without a reading starts open", () => { + const natalOnly = HEADINGS.filter((item) => item.id !== "a" && item.id !== "a1" && item.id !== "c"); + const single = render({ dataOpen: true, openGroups: [] }, "report-reading-love", natalOnly); + assert.match(single, /本命盘原始位置/); + assert.doesNotMatch(single, /本命数据<\/span>/); + const noReading = render(REPORT_TOC_FOLD_CLOSED, "a", HEADINGS, []); + assert.match(noReading, /aria-expanded="true"[^>]*>数据章节/); + assert.doesNotMatch(noReading, /personal-report-toc-rule/); +}); + +test("BUG-2061 fold state: junk reads as folded, toggles are pure, broken storage never throws", () => { + assert.deepEqual(parseReportTocFold(null), REPORT_TOC_FOLD_CLOSED); + assert.deepEqual(parseReportTocFold("{not json"), REPORT_TOC_FOLD_CLOSED); + assert.deepEqual(parseReportTocFold("[1]"), { dataOpen: false, openGroups: [] }); + assert.deepEqual(parseReportTocFold('{"dataOpen":"yes","openGroups":["timing",3]}'), { dataOpen: false, openGroups: ["timing"] }); + const opened = toggleReportTocData(REPORT_TOC_FOLD_CLOSED); + assert.equal(opened.dataOpen, true); + assert.deepEqual(toggleReportTocGroup(opened, "data").openGroups, ["data"]); + assert.deepEqual(toggleReportTocGroup(toggleReportTocGroup(opened, "data"), "data").openGroups, []); + + const original = Object.getOwnPropertyDescriptor(globalThis, "localStorage"); + try { + const store = new Map(); + Object.defineProperty(globalThis, "localStorage", { + configurable: true, + value: { getItem: (key: string) => store.get(key) ?? null, setItem: (key: string, value: string) => void store.set(key, value) }, + }); + resetReportTocFoldForTest(); + saveReportTocFold({ dataOpen: true, openGroups: ["timing"] }); + assert.equal(store.get(REPORT_TOC_FOLD_KEY), '{"dataOpen":true,"openGroups":["timing"]}'); + resetReportTocFoldForTest(); + assert.deepEqual(reportTocFoldSnapshot(), { dataOpen: true, openGroups: ["timing"] }); + + Object.defineProperty(globalThis, "localStorage", { + configurable: true, + get() { throw new Error("blocked"); }, + }); + resetReportTocFoldForTest(); + assert.deepEqual(reportTocFoldSnapshot(), REPORT_TOC_FOLD_CLOSED); + assert.doesNotThrow(() => saveReportTocFold({ dataOpen: true, openGroups: [] })); + assert.equal(reportTocFoldSnapshot().dataOpen, true, "the choice still holds for this page"); + } finally { + if (original) Object.defineProperty(globalThis, "localStorage", original); + else delete (globalThis as { localStorage?: unknown }).localStorage; + resetReportTocFoldForTest(); + } +}); + +/** Every rule whose selector mentions `needle`, as `selector { declarations }`. */ +function rulesFor(css: string, needle: string): string[] { + return [...css.matchAll(/([^{}]+)\{([^{}]*)\}/g)].filter((match) => match[1]!.includes(needle)).map((match) => match[0]); +} + +test("BUG-2061 no coloured bar on the left: contents rows, the annual 记住 line, the synastry period list", () => { + for (const [css, needle] of [[globals, "personal-report-toc"], [annualCss, "annual-remember-line"], [globals, "synastry-tl"]] as const) { + const rules = rulesFor(css, needle); + assert.ok(rules.length > 0, needle); + for (const rule of rules) assert.doesNotMatch(rule, /border-left|border-inline-start|::before/, rule.slice(0, 80)); + } + const current = rulesFor(globals, ".personal-report-toc-groups a.is-current").join("\n"); + assert.match(current, /background: var\(--color-sidebar-selected\)/); + assert.match(current, /font-weight: 600/); + assert.match(globals, /\.personal-report-toc-groups a \{[^}]*min-height: 40px;[^}]*border-radius: var\(--radius-lg\)/); +}); + +test("BUG-2061 phone: the rail becomes one 目录 button that opens a bottom sheet", () => { + const source = readFileSync(new URL("../src/components/personal-report/personal-report-markdown-view.tsx", import.meta.url), "utf8"); + assert.match(source, //); + assert.match(source, //); + const phone = globals.slice(globals.indexOf("@media (max-width: 860px) {\n .personal-report-md-layout")); + assert.match(phone.slice(0, 1600), /\.personal-report-toc-desktop \{ display: none; \}/); + assert.match(phone.slice(0, 1600), /\.personal-report-toc \.personal-report-toc-trigger \{\s*min-height: 44px;/); + assert.match(globals, /\.personal-report-toc \.personal-report-toc-trigger \{ display: none; \}/); +});