dsh-scroll-piano 0.3.2 → 0.3.3

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.
Files changed (3) hide show
  1. package/README.md +5 -4
  2. package/lib/client.js +54 -51
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -6,13 +6,14 @@
6
6
 
7
7
  - 聊天区右边缘一条细彩色竖条(开启时淡淡发光),鼠标进入该区间即触发。
8
8
  - 鼠标纵向移动 = 连续刮奏;竖条上有个白点跟随鼠标位置。
9
- - 竖条上有**「我的消息」刻度**:你每发一条消息,对应高度就有一根蓝色小横杠——一眼看出自己发的话都在哪。
10
- - **气泡只在鼠标贴住「你发的某条消息」时出现**(±24px 容差),内容就是那条消息的正文(最多 3 行)+「点击跳转」提示;**其余位置整个不显示**(不显示音名、不显示 MIDI 数字)。
9
+ - 竖条上有**「我的消息」小地图刻度**:你每发一条消息,对应位置一根蓝色小横杠——**按「在整段对话里的位置」定位,与滚动无关**,所以不管你滚到对话多深,自己的消息始终标在那、点得到。
10
+ - **气泡只在鼠标贴近某条刻度(±16px)时出现**,内容就是那条消息的正文(最多 3 行)+「点击跳转」提示;**其余位置整个不显示**(不显示音名、不显示 MIDI 数字)。
11
11
  - **点击气泡** → 平滑滚动,把那条消息滚到视口中央。
12
12
  - 左下角一枚 `🎵 滚动钢琴 ON/OFF` 胶囊:点开关切换、右侧滑块调音量(设置存 `localStorage`)。
13
13
  - 默认音域 C3–C6(3 个八度),只按半音触发(同音不重复触发)。
14
14
 
15
- > 预览**只显示用户自己发的消息**,且**不做「取全局最近一条」兜底**:鼠标离你所有消息都超过 ±24px 时气泡整个收起——否则在一个只有一条用户消息的视口里,滑到哪都显示同一条(就是「重复显示」)。位置感交给竖条刻度。声音与预览**彼此独立**——任何位置滑动都照常出声,只有预览挑消息。
15
+ > 预览**只显示用户自己发的消息**,**不做「取全局最近一条」兜底**(否则同一条会在整片区域反复出现 = 重复显示),位置感交给刻度。**声音与预览彼此独立**——任何位置滑动都照常出声,只有预览挑消息。
16
+ > 拿不到聊天区时(欢迎页 / 新会话还没有消息),触发区**退回窗口右边缘**,声音照出——否则那类页面连声音都没有(踩过)。
16
17
 
17
18
  ## 消息预览怎么定位
18
19
 
@@ -24,7 +25,7 @@
24
25
  | `14:assistant-stepN:M` | AI 消息 → 跳过 |
25
26
  | `12:turn-processN` / `["process",…]` / `9:turn-tailN` | 过程 / 工具调用 / 轮次尾 → 跳过 |
26
27
 
27
- 滑动时按鼠标纵坐标 y 找**纵坐标落在其 `getBoundingClientRect()` 内**的那条用户消息,`innerText` 取正文(去掉末尾 `HH:MM` 时间戳);点击时对该元素 `scrollIntoView({ block: "center" })`。**只认这些 `data-*` 属性**——容器类名(`icaHSq_flowItem`、`ST7X_W_scrollBody`)都带 hash,不碰。
28
+ 刻度 y = **消息在滚动内容里的偏移 ÷ 内容总长 × 视口高**(`r.top − 视口top + scrollTop` 得到内容坐标,再归一化),**与 scrollTop 无关**——滚多远都不变,消息不会"滚出屏幕"而看不见。鼠标贴近某条刻度(±16px)就取它 `innerText` 的正文(去掉末尾 `HH:MM` 时间戳)显示,点击时对该元素 `scrollIntoView({ block: "center" })`。**只认这些 `data-*` 属性**——容器类名(`icaHSq_flowItem`、`ST7X_W_scrollBody`)都带 hash,不碰。
28
29
 
29
30
  ## 触发区:聊天区右边缘,不是窗口右边缘
30
31
 
package/lib/client.js CHANGED
@@ -167,53 +167,35 @@ window.__ModuleLoader__.load({
167
167
  }
168
168
 
169
169
  /**
170
- * 聊天区滚动容器的视口矩形——把触发区钉在**聊天区右边缘**,
171
- * 而不是窗口右边缘:右侧栏(文件预览等)打开时,窗口右边是面板,
172
- * 不该在面板上触发、更不该让气泡盖住面板。
170
+ * 聊天区滚动容器的度量。`right` 用来把触发区钉在聊天区右边缘(右侧栏打开时不盖面板);
171
+ * `top/scrollTop/scrollHeight` 用来把**整段对话**压进竖条做小地图。
172
+ *
173
+ * 拿不到聊天区(欢迎页 / 新会话还没消息)时返回 null——调用方**退回窗口右边缘**,
174
+ * 否则那类页面连钢琴声都不出(踩过)。
173
175
  */
174
176
  function chatViewport() {
175
177
  const el = findChatScroller();
176
178
  if (!el) return null;
177
179
  const r = el.getBoundingClientRect();
178
- return { right: Math.round(r.right), top: Math.round(r.top), bottom: Math.round(r.bottom) };
180
+ return {
181
+ right: Math.round(r.right),
182
+ top: Math.round(r.top),
183
+ scrollTop: el.scrollTop,
184
+ scrollHeight: Math.max(1, el.scrollHeight),
185
+ clientHeight: Math.max(1, el.clientHeight)
186
+ };
179
187
  }
180
188
 
181
189
  /**
182
- * 取鼠标纵坐标附近的那条**用户消息**——只认「我输入的」,AI 回复一律不参与。
183
- * 稳定锚点是 `[data-chat-flow]` 的直接子元素,key 形如
184
- * `13:input-message<uuid>`(用户)/ `14:assistant-stepN:M`(AI)/
185
- * `12:turn-processN`(过程块),类名带 hash 所以只认 data 属性。
190
+ * 把**整段对话**映射成竖条上的小地图刻度:每条用户消息一个 y(0..视口高)。
186
191
  *
187
- * **必须贴住消息本体**(超出 ±TOL 就算没有):不搞「取全局最近一条」,
188
- * 否则在只有一个用户消息的视口里,鼠标滑到哪都显示同一条 = 重复显示。
189
- * 位置感交给竖条上的刻度(`userMessageTicks`)。
192
+ * y 取的是「消息在**滚动内容**里的偏移 / 内容总长」,**与 scrollTop 无关**——
193
+ * 所以不管滚到哪、滚多远,自己的消息刻度**始终在那**,看得见也点得到。
194
+ * (上一版按视口坐标定位:滚到对话底部时你的消息 y 是 -4339,全跑到屏幕外了。)
190
195
  */
191
- function userMessageAtY(y) {
192
- const TOL = 24; // 允许鼠标略微超出消息边界,滑动更宽容
193
- const flow = document.querySelector("[data-chat-flow]");
194
- if (!flow) return null;
195
- let best = null;
196
- let bestDist = Infinity;
197
- for (const el of flow.children) {
198
- const key = String(el.getAttribute("data-chat-flow-key") || el.getAttribute("data-chat-node-key") || "");
199
- if (!/input-message/.test(key)) continue;
200
- const r = el.getBoundingClientRect();
201
- if (r.height <= 0) continue;
202
- const dist = y < r.top ? r.top - y : (y > r.bottom ? y - r.bottom : 0);
203
- if (dist > TOL || dist >= bestDist) continue;
204
- // innerText 抓渲染后的正文;末尾的 HH:MM 是消息时间戳,去掉
205
- const text = (el.innerText || "").replace(/\s+/g, " ").trim().replace(/\s*\d{1,2}:\d{2}$/, "").trim();
206
- if (!text) continue;
207
- bestDist = dist;
208
- best = { text, el, dist };
209
- }
210
- return best;
211
- }
212
-
213
- /** 列出当前所有用户消息在视口里的纵向中心(给竖条上的刻度用)。 */
214
- function userMessageTicks() {
196
+ function minimapTicks(view) {
215
197
  const flow = document.querySelector("[data-chat-flow]");
216
- if (!flow) return [];
198
+ if (!flow || !view) return [];
217
199
  const out = [];
218
200
  for (const el of flow.children) {
219
201
  const key = String(el.getAttribute("data-chat-flow-key") || el.getAttribute("data-chat-node-key") || "");
@@ -221,11 +203,26 @@ window.__ModuleLoader__.load({
221
203
  const r = el.getBoundingClientRect();
222
204
  if (r.height <= 0) continue;
223
205
  if (!(el.innerText || "").trim()) continue;
224
- out.push(Math.round(r.top + r.height / 2));
206
+ // 视口坐标 + 已滚动量 = 在滚动内容里的坐标
207
+ const contentCenter = r.top - view.top + view.scrollTop + r.height / 2;
208
+ const y = Math.round(contentCenter / view.scrollHeight * window.innerHeight);
209
+ out.push({ y, el });
225
210
  }
226
211
  return out;
227
212
  }
228
213
 
214
+ /** 取小地图纵坐标 y 附近(±TOL)的那条用户消息;没有就 null → 气泡收起。 */
215
+ function messageNearTick(y, ticks) {
216
+ const TOL = 16;
217
+ let best = null;
218
+ let bestD = Infinity;
219
+ for (const t of ticks) {
220
+ const d = Math.abs(t.y - y);
221
+ if (d <= TOL && d < bestD) { bestD = d; best = t; }
222
+ }
223
+ return best;
224
+ }
225
+
229
226
  //#region settings
230
227
  const LS_KEY = "dsh-scroll-piano:settings";
231
228
  const DEFAULTS = { on: true, volume: 0.25, stripWidth: 30, lowNote: 48, highNote: 84, inset: 56 };
@@ -253,6 +250,7 @@ window.__ModuleLoader__.load({
253
250
  const roleRef = useRef(null);
254
251
  const textRef = useRef(null);
255
252
  const ticksRef = useRef(null);
253
+ const ticksDataRef = useRef([]); // [{y, el}] — 小地图刻度数据(showPreview 靠它找消息)
256
254
  const activeMsgRef = useRef(null);
257
255
 
258
256
  const setOn = useCallback((on) => {
@@ -277,20 +275,22 @@ window.__ModuleLoader__.load({
277
275
  let mo = null;
278
276
  let moTimer = 0;
279
277
  let tickTimer = 0;
280
- /** 竖条上给「我的消息」打刻度:一眼看出自己发的话都在哪。 */
278
+ /** 竖条上给「我的消息」打小地图刻度:不管滚到哪,自己的消息始终标在那。 */
281
279
  const refreshTicks = () => {
282
280
  const box = ticksRef.current;
281
+ const view = chatViewport();
282
+ const data = minimapTicks(view);
283
+ ticksDataRef.current = data;
283
284
  if (!box) return;
284
- const ys = userMessageTicks();
285
- while (box.children.length > ys.length) box.removeChild(box.lastChild);
286
- while (box.children.length < ys.length) {
285
+ while (box.children.length > data.length) box.removeChild(box.lastChild);
286
+ while (box.children.length < data.length) {
287
287
  const i = document.createElement("i");
288
288
  i.className = "dshscp_tick";
289
289
  box.appendChild(i);
290
290
  }
291
- for (let i = 0; i < ys.length; i++) box.children[i].style.top = Math.round(ys[i]) + "px";
291
+ for (let i = 0; i < data.length; i++) box.children[i].style.top = data[i].y + "px";
292
292
  };
293
- // 滚动 / 流式更新会让刻度移位;节流到 150ms,避免 pointermove 上做布局读取
293
+ // 对话增长 / 面板开合会改内容总长 → 节流到 150ms 重算(刻度与 scrollTop 无关,滚动不用重算)
294
294
  const scheduleTicks = () => {
295
295
  if (tickTimer) return;
296
296
  tickTimer = window.setTimeout(() => { tickTimer = 0; refreshTicks(); }, 150);
@@ -341,7 +341,11 @@ window.__ModuleLoader__.load({
341
341
  return;
342
342
  }
343
343
  activeMsgRef.current = msg;
344
- if (textRef.current) textRef.current.textContent = msg.text;
344
+ // 内容现取现算(消息可能被重新渲染);去掉末尾的 HH:MM 时间戳
345
+ if (textRef.current) {
346
+ textRef.current.textContent = (msg.el.innerText || "")
347
+ .replace(/\s+/g, " ").trim().replace(/\s*\d{1,2}:\d{2}$/, "").trim();
348
+ }
345
349
  el.style.pointerEvents = "auto";
346
350
  el.style.cursor = "pointer";
347
351
  // 竖直居中于鼠标,并夹在视口内
@@ -372,9 +376,9 @@ window.__ModuleLoader__.load({
372
376
  if (!s.on) return;
373
377
  if (overBubble(e.clientX, e.clientY)) return; // 鼠标停在气泡上(正要点击)
374
378
  const view = placeAtChatEdge();
375
- if (!view) return;
376
- // 触发区 = 聊天区右边缘那一条;落在右侧面板上一律不响应
377
- if (e.clientX < view.right - s.stripWidth || e.clientX > view.right + 2) {
379
+ // 没有聊天区(欢迎页 / 新会话还没消息)→ 退回窗口右边缘,声音照出(踩过:这里 return 会让声音都没)
380
+ const baseRight = view ? view.right : window.innerWidth;
381
+ if (e.clientX < baseRight - s.stripWidth || e.clientX > baseRight + 2) {
378
382
  lastMidiRef.current = null;
379
383
  hidePreview();
380
384
  return;
@@ -384,8 +388,8 @@ window.__ModuleLoader__.load({
384
388
  // t=0(顶部)→ highNote;t=1(底部)→ lowNote
385
389
  const midi = Math.round(s.highNote - t * (s.highNote - s.lowNote));
386
390
  showMarker(e.clientY);
387
- // 预览只挑「我发的消息」里离鼠标最近的那条(不显示音名/MIDI)
388
- showPreview(e.clientY, userMessageAtY(e.clientY));
391
+ // 预览只挑「我发的消息」里离该刻度最近的那条(不显示音名/MIDI)
392
+ showPreview(e.clientY, messageNearTick(e.clientY, ticksDataRef.current));
389
393
  if (markerRef.current) markerRef.current.classList.add("dshscp_markerOn");
390
394
  if (midi !== lastMidiRef.current) {
391
395
  lastMidiRef.current = midi;
@@ -403,14 +407,13 @@ window.__ModuleLoader__.load({
403
407
  };
404
408
  document.addEventListener("pointermove", onMove, { passive: true });
405
409
  document.addEventListener("pointerleave", onLeave);
406
- document.addEventListener("scroll", scheduleTicks, { passive: true, capture: true });
410
+ // 不需要监听 scroll:刻度是按「内容偏移/内容总长」算的,与 scrollTop 无关
407
411
  window.addEventListener("blur", onLeave);
408
412
  window.addEventListener("resize", placeAtChatEdge);
409
413
  placeAtChatEdge();
410
414
  return () => {
411
415
  document.removeEventListener("pointermove", onMove);
412
416
  document.removeEventListener("pointerleave", onLeave);
413
- document.removeEventListener("scroll", scheduleTicks, { capture: true });
414
417
  window.removeEventListener("blur", onLeave);
415
418
  window.removeEventListener("resize", placeAtChatEdge);
416
419
  if (ro) ro.disconnect();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-scroll-piano",
3
- "version": "0.3.2",
3
+ "version": "0.3.3",
4
4
  "description": "滚动钢琴:鼠标滑过 DSH 右侧边缘的滚动条竖条时,按纵向位置发出钢琴音(越高音越高,特雷门琴式)。",
5
5
  "type": "module",
6
6
  "license": "MIT",