@luziyang2026/dsh-question-nav 0.7.0 → 0.7.2

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.
@@ -12,8 +12,13 @@
12
12
  * vertical cascade of question cards opens — the selected (center) card is the
13
13
  * focus (brand accent, elevated, full question text), the four neighbors are
14
14
  * narrower context cards clamped to fewer lines. Every card is clickable and
15
- * jumps to its question, exactly like clicking the dot; the rail auto-centers
16
- * the selected dot.
15
+ * jumps to its question, exactly like clicking the dot; the rail scrolls the
16
+ * selected dot into view only when it is clipped by the band's edges.
17
+ *
18
+ * Overflow is paged, not auto-scrolled: two small triangle buttons in the dot
19
+ * style sit above and below the dot queue (▲ / ▼), each click revealing five
20
+ * hidden dots. The native scrollbar stays hidden and the column fades at its
21
+ * edges as a pure visual cue — no hover auto-scroll.
17
22
  *
18
23
  * Data source: the host-folded `questionIndex` session projection (whole
19
24
  * history, persisted host-side, pushed live through session/projection
@@ -37,7 +42,7 @@ import type { QuestionEntry } from '../core/question-entry.ts'
37
42
  import { groupQuestionsByTurn, mergeLiveQuestions, type TurnDot } from '../core/turn-dots.ts'
38
43
  import type { AlignPreference } from '../core/align.ts'
39
44
  import type { JumpFailureCode } from '../core/jump.ts'
40
- import { EDGE_SCROLL_ZONE, FOCUS_RADIUS, edgeScrollSpeed, focusCardMetrics, focusScale, focusTier } from '../core/focus.ts'
45
+ import { FOCUS_RADIUS, clampScrollTop, focusCardMetrics, focusScale, focusTier, minimalScrollIntoView, pageStep } from '../core/focus.ts'
41
46
  import { formatQuestionTime } from '../core/time.ts'
42
47
  import type { QuestionNavKey } from './locales.ts'
43
48
  import styles from './question-nav.module.css'
@@ -152,62 +157,31 @@ export function QuestionNavStrip(props: ComponentProps): React.JSX.Element | nul
152
157
  // Last focused dot key, so re-hovering the same dot after a gap re-centers it.
153
158
  const lastFocusedKeyRef = useRef<string | null>(null)
154
159
  // Whether the dot band overflows its 60% clamp (drives the edge-fade mask
155
- // and the hover auto-scroll).
160
+ // and the ▲/▼ paging buttons).
156
161
  const [scrollable, setScrollable] = useState(false)
157
- // Edge auto-scroll state: the fade zones at the band's top/bottom edges are
158
- // scroll affordances — hovering them scrolls the band so the faded dots
159
- // flow into the clear area. Speed follows how deep the pointer is inside
160
- // the zone; a rAF loop applies it smoothly while it is non-zero.
161
- const edgeScrollRef = useRef<{ speed: number; raf: number; last: number }>({ speed: 0, raf: 0, last: 0 })
162
-
163
- const stopEdgeScroll = (): void => {
164
- edgeScrollRef.current.speed = 0
165
- if (edgeScrollRef.current.raf !== 0) {
166
- cancelAnimationFrame(edgeScrollRef.current.raf)
167
- edgeScrollRef.current.raf = 0
168
- }
169
- }
162
+ // Scroll offset of the band + its max offset: drives the ▲/▼ visibility
163
+ // (each direction hides once there is nothing more to reveal).
164
+ const [scrollPos, setScrollPos] = useState<{ top: number; max: number }>({ top: 0, max: 0 })
170
165
 
171
- const edgeTick = (now: number): void => {
166
+ const syncScroll = (): void => {
172
167
  const list = listRef.current
173
- const state = edgeScrollRef.current
174
- state.raf = 0
175
- if (list === null || state.speed === 0) return
176
- const dt = Math.min(64, now - state.last) / 1000
177
- state.last = now
178
- const before = list.scrollTop
179
- list.scrollTop = before + state.speed * dt
180
- // Stop at the scroll bounds — there is nothing more to reveal.
181
- const atTop = list.scrollTop <= 0 && state.speed < 0
182
- const atBottom = list.scrollTop >= list.scrollHeight - list.clientHeight - 1 && state.speed > 0
183
- if (atTop || atBottom) {
184
- state.speed = 0
185
- return
186
- }
187
- state.raf = requestAnimationFrame(edgeTick)
168
+ if (list === null) return
169
+ const max = Math.max(0, list.scrollHeight - list.clientHeight)
170
+ setScrollPos({ top: list.scrollTop, max })
188
171
  }
189
172
 
190
- const onListMouseMove = (e: React.MouseEvent<HTMLDivElement>): void => {
173
+ // Page the band by one DOT_PAGE_ROWS click in the given direction.
174
+ const pageBy = (dir: 1 | -1): void => {
191
175
  const list = listRef.current
192
176
  if (list === null) return
193
- const rect = list.getBoundingClientRect()
194
- const y = e.clientY - rect.top
195
- const depthTop = EDGE_SCROLL_ZONE - y
196
- const depthBottom = y - (rect.height - EDGE_SCROLL_ZONE)
197
- let speed = 0
198
- if (depthTop > 0 && list.scrollTop > 0) {
199
- speed = -edgeScrollSpeed(depthTop / EDGE_SCROLL_ZONE)
200
- } else if (depthBottom > 0 && list.scrollTop < list.scrollHeight - list.clientHeight - 1) {
201
- speed = edgeScrollSpeed(depthBottom / EDGE_SCROLL_ZONE)
202
- }
203
- const state = edgeScrollRef.current
204
- state.speed = speed
205
- if (speed !== 0 && state.raf === 0) {
206
- state.last = performance.now()
207
- state.raf = requestAnimationFrame(edgeTick)
208
- }
177
+ const max = Math.max(0, list.scrollHeight - list.clientHeight)
178
+ list.scrollTop = clampScrollTop(list.scrollTop + dir * pageStep(), max)
179
+ syncScroll()
209
180
  }
210
181
 
182
+ const canPageUp = scrollable && scrollPos.top > 0
183
+ const canPageDown = scrollable && scrollPos.top < scrollPos.max
184
+
211
185
  // Focus persists briefly after leaving the rail, so the mouse can reach the
212
186
  // clickable cascade cards; entering a card cancels the clear, leaving
213
187
  // everything re-arms it.
@@ -383,9 +357,11 @@ export function QuestionNavStrip(props: ComponentProps): React.JSX.Element | nul
383
357
  }
384
358
  }, [visible, align])
385
359
 
386
- // Keep the focused dot centered in the (≤60% tall, scrollable) band so its
387
- // two neighbors on each side stay visible. Re-centers only when the focused
388
- // key changes; scrolls instantly to avoid chasing a fast-moving hover.
360
+ // Keep the focused dot visible inside the (≤60% tall, scrollable) band:
361
+ // scroll only when the dot (plus room for its two magnified neighbors) is
362
+ // clipped by the band edges, and then only by the minimal amount — never
363
+ // re-center an already-visible dot, so browsing dot-by-dot doesn't shift
364
+ // the band under the pointer.
389
365
  useLayoutEffect(() => {
390
366
  const key = focus?.key ?? null
391
367
  if (lastFocusedKeyRef.current === key) return
@@ -395,18 +371,27 @@ export function QuestionNavStrip(props: ComponentProps): React.JSX.Element | nul
395
371
  if (list === null) return
396
372
  const target = list.querySelector<HTMLElement>('[data-question-nav-focused="true"]')
397
373
  if (target === null) return
398
- const delta = target.offsetTop - list.offsetTop
399
- const center = delta - (list.clientHeight - target.offsetHeight) / 2
400
- list.scrollTop = Math.max(0, Math.min(center, list.scrollHeight - list.clientHeight))
374
+ const next = minimalScrollIntoView(
375
+ list.scrollTop,
376
+ list.clientHeight,
377
+ list.scrollHeight,
378
+ target.offsetTop - list.offsetTop,
379
+ target.offsetHeight,
380
+ )
381
+ if (next !== null) list.scrollTop = next
382
+ syncScroll()
401
383
  }, [focus])
402
384
 
403
- // Detect band overflow: drives the edge-fade mask + hover auto-scroll.
385
+ // Detect band overflow: drives the edge-fade mask + the ▲/▼ paging buttons.
404
386
  // Re-checked when the dots change and whenever the band itself resizes
405
387
  // (the layout loop above clamps it to the conversation height).
406
388
  useLayoutEffect(() => {
407
389
  const list = listRef.current
408
390
  if (list === null) return
409
- const check = (): void => setScrollable(list.scrollHeight > list.clientHeight + 1)
391
+ const check = (): void => {
392
+ setScrollable(list.scrollHeight > list.clientHeight + 1)
393
+ syncScroll()
394
+ }
410
395
  check()
411
396
  if (typeof ResizeObserver === 'undefined') return
412
397
  const observer = new ResizeObserver(check)
@@ -414,11 +399,10 @@ export function QuestionNavStrip(props: ComponentProps): React.JSX.Element | nul
414
399
  return () => observer.disconnect()
415
400
  }, [dots, align])
416
401
 
417
- // Clear any pending timers and the edge auto-scroll on unmount.
402
+ // Clear any pending timers on unmount.
418
403
  useEffect(() => () => {
419
404
  if (hintTimerRef.current !== null) window.clearTimeout(hintTimerRef.current)
420
405
  if (clearFocusTimerRef.current !== null) window.clearTimeout(clearFocusTimerRef.current)
421
- stopEdgeScroll()
422
406
  // eslint-disable-next-line react-hooks/exhaustive-deps
423
407
  }, [])
424
408
 
@@ -460,43 +444,66 @@ export function QuestionNavStrip(props: ComponentProps): React.JSX.Element | nul
460
444
  className={align === 'right' ? `${styles.rail} ${styles.railRight}` : styles.rail}
461
445
  data-question-nav="rail"
462
446
  >
463
- <div
464
- ref={listRef}
465
- className={scrollable ? `${styles.list} ${styles.listScrollable}` : styles.list}
466
- onMouseMove={scrollable ? onListMouseMove : undefined}
467
- onMouseLeave={() => {
468
- stopEdgeScroll()
469
- scheduleClearFocus()
470
- }}
471
- >
447
+ <div className={styles.queue}>
472
448
  {dots.length === 0 ? (
473
449
  <div className={styles.empty}>{t('strip.empty')}</div>
474
450
  ) : (
475
- <div className={styles.dots}>
451
+ <>
476
452
  <span className={styles.count}>{dots.length}</span>
477
- {dots.map((dot, index) => {
478
- const isFocused = focus !== null && focus.key === dot.key
479
- // Progressive magnification: selected largest, ±1 smaller, ±2
480
- // smaller still, the rest base scale.
481
- const tier = selectedIndex < 0 ? null : focusTier(index - selectedIndex)
482
- const scale = jumpingKey === dot.key ? 1.6 : tier !== null ? focusScale(tier) : 1
483
- const cls = [styles.dot]
484
- if (isFocused) cls.push(styles.focused)
485
- if (jumpingKey === dot.key) cls.push(styles.active)
486
- return (
487
- <button
488
- key={dot.key}
489
- className={cls.join(' ')}
490
- style={{ transform: `scale(${scale})` }}
491
- data-question-nav-focused={isFocused ? 'true' : undefined}
492
- data-question-nav-index={index}
493
- aria-label={dot.texts[0] ?? ''}
494
- onMouseEnter={(e) => openFocus(dot, e.currentTarget)}
495
- onClick={() => onJump(dot)}
496
- />
497
- )
498
- })}
499
- </div>
453
+ {scrollable ? (
454
+ <button
455
+ type="button"
456
+ className={`${styles.navBtn} ${styles.navUp}`}
457
+ aria-label={t('strip.up')}
458
+ style={{ visibility: canPageUp ? 'visible' : 'hidden' }}
459
+ onMouseEnter={cancelClearFocus}
460
+ onMouseLeave={scheduleClearFocus}
461
+ onClick={() => pageBy(-1)}
462
+ />
463
+ ) : null}
464
+ <div
465
+ ref={listRef}
466
+ className={scrollable ? `${styles.list} ${styles.listScrollable}` : styles.list}
467
+ onScroll={syncScroll}
468
+ onMouseLeave={scheduleClearFocus}
469
+ >
470
+ <div className={styles.dots}>
471
+ {dots.map((dot, index) => {
472
+ const isFocused = focus !== null && focus.key === dot.key
473
+ // Progressive magnification: selected largest, ±1 smaller, ±2
474
+ // smaller still, the rest base scale.
475
+ const tier = selectedIndex < 0 ? null : focusTier(index - selectedIndex)
476
+ const scale = jumpingKey === dot.key ? 1.6 : tier !== null ? focusScale(tier) : 1
477
+ const cls = [styles.dot]
478
+ if (isFocused) cls.push(styles.focused)
479
+ if (jumpingKey === dot.key) cls.push(styles.active)
480
+ return (
481
+ <button
482
+ key={dot.key}
483
+ className={cls.join(' ')}
484
+ style={{ transform: `scale(${scale})` }}
485
+ data-question-nav-focused={isFocused ? 'true' : undefined}
486
+ data-question-nav-index={index}
487
+ aria-label={dot.texts[0] ?? ''}
488
+ onMouseEnter={(e) => openFocus(dot, e.currentTarget)}
489
+ onClick={() => onJump(dot)}
490
+ />
491
+ )
492
+ })}
493
+ </div>
494
+ </div>
495
+ {scrollable ? (
496
+ <button
497
+ type="button"
498
+ className={`${styles.navBtn} ${styles.navDown}`}
499
+ aria-label={t('strip.down')}
500
+ style={{ visibility: canPageDown ? 'visible' : 'hidden' }}
501
+ onMouseEnter={cancelClearFocus}
502
+ onMouseLeave={scheduleClearFocus}
503
+ onClick={() => pageBy(1)}
504
+ />
505
+ ) : null}
506
+ </>
500
507
  )}
501
508
  </div>
502
509
  {hint !== null
@@ -4,6 +4,8 @@
4
4
  */
5
5
  export const zh = {
6
6
  'strip.empty': '本会话还没有提问',
7
+ 'strip.up': '向上翻出 5 个提问圆点',
8
+ 'strip.down': '向下翻出 5 个提问圆点',
7
9
  'jump.inactive': '聊天视图未激活',
8
10
  'jump.hidden': '目标无独立气泡,已定位到邻近内容',
9
11
  'jump.notfound': '目标未加载或不存在(可能已压缩)',
@@ -17,6 +19,8 @@ export const zh = {
17
19
 
18
20
  export const en = {
19
21
  'strip.empty': 'No questions in this session yet',
22
+ 'strip.up': 'Reveal 5 question dots above',
23
+ 'strip.down': 'Reveal 5 question dots below',
20
24
  'jump.inactive': 'Chat view is not active',
21
25
  'jump.hidden': 'No dedicated bubble; landed on nearby content',
22
26
  'jump.notfound': 'Target not loaded or missing (maybe compacted)',
@@ -21,19 +21,34 @@
21
21
  left: auto;
22
22
  }
23
23
 
24
- /* Vertically center the dot column when it is short; when it is tall it is
25
- clamped to at most 60% of the conversation height (auto margins center the
26
- band) and scrolls from the top (auto margins collapse to 0 on overflow).
27
- The band itself is interactive so wheel-scrolling works over the gaps too
28
- (the rail above is pointer-events:none). The native scrollbar is always
29
- hidden: this is a minimap scrolled by wheel/trackpad and by hovering the
30
- fade zones, and overflow is signalled by the edge fade below. */
31
- .list {
24
+ /* The paging queue: the fixed column (count + ▲, then the scrollable dot band,
25
+ then ▼) that replaces the edge-fade auto-scroll. Clamped to 60% of the
26
+ conversation height and vertically centered (auto margins); when the queue
27
+ overflows, only the middle dot band scrolls — the count and triangles stay
28
+ pinned. The wrapper stays pass-through; the band and triangles re-enable
29
+ pointer events. */
30
+ .queue {
32
31
  flex: 0 1 auto;
33
32
  min-height: 0;
34
33
  max-height: 60%;
35
34
  margin: auto 0;
36
35
  width: 100%;
36
+ display: flex;
37
+ flex-direction: column;
38
+ align-items: center;
39
+ gap: 6px;
40
+ box-sizing: border-box;
41
+ pointer-events: none;
42
+ }
43
+
44
+ /* The middle dot band: the only scrollable part of the queue. Scrolls from the
45
+ top (overflow collapses the auto margins to 0). The native scrollbar is
46
+ always hidden: this is a minimap paged by the ▲/▼ buttons and by
47
+ wheel/trackpad, and overflow is signalled by the edge fade below. */
48
+ .list {
49
+ flex: 1 1 auto;
50
+ min-height: 0;
51
+ width: 100%;
37
52
  overflow-y: auto;
38
53
  overflow-x: hidden;
39
54
  display: flex;
@@ -50,18 +65,12 @@
50
65
  display: none;
51
66
  }
52
67
  /* Overflowing band (applied from JS when scrollHeight > clientHeight): dots
53
- fade out towards both edges, signalling "more beyond" without a scrollbar.
54
- The fade zones double as hover auto-scroll areas (see QuestionNavStrip). */
68
+ fade out towards both edges — a pure visual cue for "more beyond" (the
69
+ paging triangles do the actual scrolling). */
55
70
  .listScrollable {
56
71
  -webkit-mask-image: linear-gradient(to bottom, transparent, black 22px, black calc(100% - 22px), transparent);
57
72
  mask-image: linear-gradient(to bottom, transparent, black 22px, black calc(100% - 22px), transparent);
58
73
  }
59
- .list > *:first-child {
60
- margin-top: auto;
61
- }
62
- .list > *:last-child {
63
- margin-bottom: auto;
64
- }
65
74
 
66
75
  .dot {
67
76
  flex: none;
@@ -97,7 +106,7 @@
97
106
  user-select: none;
98
107
  }
99
108
 
100
- /* The centered group: count + dot column, centered together (the list's auto
109
+ /* The centered group: dot column, centered together (the list's auto
101
110
  margins center it when short; it scrolls as a unit when tall). */
102
111
  .dots {
103
112
  display: flex;
@@ -106,6 +115,46 @@
106
115
  gap: 6px;
107
116
  }
108
117
 
118
+ /* Paging triangles (▲ above the dot queue, ▼ below), styled like the dots:
119
+ a plain triangle in the dot's quiet border color that lights up brand on
120
+ hover. Clicking pages the band by DOT_PAGE_ROWS dots. The triangle is a
121
+ pure CSS border triangle so it stays crisp at any DPI; the button box is a
122
+ slightly larger transparent hit target. */
123
+ .navBtn {
124
+ flex: none;
125
+ pointer-events: auto;
126
+ width: 14px;
127
+ height: 12px;
128
+ padding: 0;
129
+ border: none;
130
+ background: transparent;
131
+ cursor: pointer;
132
+ display: flex;
133
+ align-items: center;
134
+ justify-content: center;
135
+ }
136
+ .navBtn::before {
137
+ content: '';
138
+ display: block;
139
+ width: 0;
140
+ height: 0;
141
+ border-left: 5px solid transparent;
142
+ border-right: 5px solid transparent;
143
+ transition: border-color 120ms ease;
144
+ }
145
+ .navUp::before {
146
+ border-bottom: 7px solid var(--dsw-alias-border-l3);
147
+ }
148
+ .navDown::before {
149
+ border-top: 7px solid var(--dsw-alias-border-l3);
150
+ }
151
+ .navUp:hover::before {
152
+ border-bottom-color: var(--dsw-alias-brand-primary);
153
+ }
154
+ .navDown:hover::before {
155
+ border-top-color: var(--dsw-alias-brand-primary);
156
+ }
157
+
109
158
  .empty {
110
159
  padding: 10px 4px;
111
160
  font-size: 11px;
package/src/core/focus.ts CHANGED
@@ -75,22 +75,68 @@ export function magnificationWindow(total: number, selected: number, radius: num
75
75
  return out
76
76
  }
77
77
 
78
- /** Height (px) of the dot band's top/bottom fade zones, which double as hover
79
- * auto-scroll areas (slightly larger than the 22px CSS mask fade). */
80
- export const EDGE_SCROLL_ZONE = 26
78
+ /** Clearance (px) kept around the focused dot when scrolling it into view, so
79
+ * its two magnified neighbors on each side stay inside the band's clear area
80
+ * (2 dot rows ≈ 28px + the 22px fade zone). */
81
+ export const FOCUS_NEIGHBOR_CLEARANCE = 50
81
82
 
82
- /** Edge auto-scroll speed range (px/s): MIN at the zone boundary, MAX at the
83
- * very edge of the band. */
84
- export const EDGE_SCROLL_MIN_SPEED = 90
85
- export const EDGE_SCROLL_MAX_SPEED = 420
83
+ /**
84
+ * Minimal "scroll into view" for the focused dot, in the spirit of
85
+ * scrollIntoView({ block: 'nearest' }): returns the scrollTop that brings the
86
+ * dot — plus `clearance` room for its magnified neighbors — inside the
87
+ * visible band with the smallest possible movement, or null when the dot is
88
+ * already fully visible. Unlike unconditional centering this never shifts the
89
+ * band while the user browses dot-by-dot: only a clipped dot is scrolled.
90
+ */
91
+ export function minimalScrollIntoView(
92
+ scrollTop: number,
93
+ clientHeight: number,
94
+ scrollHeight: number,
95
+ dotTop: number,
96
+ dotHeight: number,
97
+ clearance: number = FOCUS_NEIGHBOR_CLEARANCE,
98
+ ): number | null {
99
+ const margin = Math.min(clearance, clientHeight / 4)
100
+ const viewTop = scrollTop + margin
101
+ const viewBottom = scrollTop + clientHeight - margin
102
+ if (dotTop >= viewTop && dotTop + dotHeight <= viewBottom) return null
103
+ const next = dotTop < viewTop
104
+ ? dotTop - margin
105
+ : dotTop + dotHeight + margin - clientHeight
106
+ return Math.max(0, Math.min(next, Math.max(0, scrollHeight - clientHeight)))
107
+ }
86
108
 
87
109
  /**
88
- * Auto-scroll speed (px/s) for a pointer at `ratio` depth into an edge fade
89
- * zone (0 = at the zone boundary, 1 = at the band's very edge): eases
90
- * linearly from MIN to MAX so a shallow probe scrolls gently and pushing
91
- * into the edge moves fast. The sign (direction) is applied by the caller.
110
+ * Paging model for the dot band. Overflowing dots are not auto-scrolled by
111
+ * hovering the edges: instead the user pages them with two triangle buttons
112
+ * (▲ above the dot queue, ▼ below it), each click revealing `DOT_PAGE_ROWS`
113
+ * hidden dots. Pure arithmetic — no React, no DOM.
92
114
  */
93
- export function edgeScrollSpeed(ratio: number): number {
94
- const r = Math.max(0, Math.min(1, ratio))
95
- return EDGE_SCROLL_MIN_SPEED + (EDGE_SCROLL_MAX_SPEED - EDGE_SCROLL_MIN_SPEED) * r
115
+
116
+ /** How many hidden dots one click of a paging triangle reveals. */
117
+ export const DOT_PAGE_ROWS = 5
118
+
119
+ /** Base dot diameter (px) and gap (px), matching the rail CSS — the paging
120
+ * step is expressed in whole dot rows, so it tracks the visible geometry. */
121
+ export const DOT_SIZE = 8
122
+ export const DOT_GAP = 6
123
+
124
+ /** Scroll step (px) for one paging click: `rows` whole dot rows. */
125
+ export function pageStep(dotSize: number = DOT_SIZE, gap: number = DOT_GAP, rows: number = DOT_PAGE_ROWS): number {
126
+ return rows * (dotSize + gap)
127
+ }
128
+
129
+ /** Clamp a target scrollTop into the band's valid range (0..maxScroll). */
130
+ export function clampScrollTop(target: number, maxScroll: number): number {
131
+ return Math.max(0, Math.min(target, Math.max(0, maxScroll)))
132
+ }
133
+
134
+ /** Whether hidden dots remain above the band (show ▲). */
135
+ export function canScrollAbove(scrollTop: number): boolean {
136
+ return scrollTop > 0
137
+ }
138
+
139
+ /** Whether hidden dots remain below the band (show ▼). */
140
+ export function canScrollBelow(scrollTop: number, maxScroll: number): boolean {
141
+ return scrollTop < maxScroll
96
142
  }