@li-canghai/dsh-paper 0.8.1 → 0.8.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.
package/lib/client.js CHANGED
@@ -1157,6 +1157,218 @@ function createLibraryModel() {
1157
1157
  * 相反,译文上划的线(offset = -1)照样能在 zhText 里精确定位。围栏内容整段跳过 ——
1158
1158
  * 高亮 Range 落进代码块没有视觉语义,还会把行号 / 高亮会话搅乱。
1159
1159
  */
1160
+ /** 空白字符集:行接缝的补空格、软连字符、零宽字符。与宿主 pdfQuoteInText 同口径。 */
1161
+ const spaceRun = () => /[\s\u00ad\u200b\u200c\u200d\ufeff]+/gu
1162
+ /** 单个字符是不是空白(含软连字符、零宽字符)。逐字符判断,不碰正则状态。 */
1163
+ const isSpaceChar = ch => ch === undefined || /[\s\u00ad\u200b\u200c\u200d\ufeff]/.test(ch)
1164
+
1165
+ /**
1166
+ * 忽略空白地在一段文本里定位原话,返回在**原文本**里的起止下标。
1167
+ *
1168
+ * 🔴 为什么绘制侧也需要它:PDF 档的文字层是逐词、逐字符的 span 拼出来的,行与行之间
1169
+ * **一个分隔符都没有**(每行是 display:block 的绝对定位元素),而 mark.quote 来自服务端
1170
+ * 的 group.text —— 那份在**行接缝补一个空格**。于是同一次拖选在两侧差着几个空格:
1171
+ * 实测 ResNet 第 2 页 `…problem by introducing…`(702 字)对 `…problem byintroducing…`
1172
+ * (693 字),`display.indexOf(quote)` 必然落空,划线画不出来。
1173
+ *
1174
+ * 做法:把两侧的空白都去掉再找,然后用一张「去空白后的下标 → 原文本下标」的表把结果
1175
+ * 换算回去。区间两端向外吸附到完整词元,避免高亮停在半个词上。
1176
+ *
1177
+ * @param {string} text 屏上显示文本(原样,含它自己的空白)
1178
+ * @param {string} quote 原话(可能带另一种空白)
1179
+ * @returns {{start: number, end: number}|null} 原文本里的区间;对不上返回 null
1180
+ */
1181
+ function locateQuoteIgnoringSpace(text, quote) {
1182
+ if (text === '' || quote === '') return null
1183
+ // squeezed 去掉空白,index 记录每个保留字符在原文本里的下标。
1184
+ let squeezed = ''
1185
+ const index = []
1186
+ for (let at = 0; at < text.length; at++) {
1187
+ if (isSpaceChar(text[at])) continue
1188
+ squeezed += text[at]
1189
+ index.push(at)
1190
+ }
1191
+ const target = quote.replace(spaceRun(), '')
1192
+ if (target === '' || squeezed === '') return null
1193
+ const hit = squeezed.indexOf(target)
1194
+ if (hit < 0) return null
1195
+ const start = index[hit]
1196
+ const end = index[hit + target.length - 1] + 1
1197
+ return { start, end }
1198
+ }
1199
+
1200
+ /** 行内公式的源码形态:`$…$`(不跨行)。两端锚定量词,避免跨段乱匹配。 */
1201
+ const formulaSource = () => /(\$[^$\n]+\$)/g
1202
+
1203
+ /**
1204
+ * 把原话按**行内公式**切成「文字段 / 公式段」,再把公式当**通配**去匹配显示文本。
1205
+ *
1206
+ * 🔴 为什么要这样:同一条划线在两侧的文字形态不同,公式是其中唯一的硬分界。
1207
+ * · 原文栏的显示文本里,公式是 KaTeX 渲染物被 {@link formulaSelectionText} 换回的
1208
+ * **LaTeX 源码**(`$\mathcal{H}(\mathbf{x})$`);
1209
+ * · PDF 档的显示文本里,公式是**渲染后的字形**(`H(x)`);
1210
+ * · 两边的**普通文字**逐字相同(只差空白)。
1211
+ *
1212
+ * 实测 ResNet 第 2 页 `p2-p2`:原话来自 PDF 字形,含 `H(x)`;原文栏那份是
1213
+ * `$\mathcal{H}(\mathbf{x})$`。整串逐字对不上,但公式**之间**的文字段四段全中
1214
+ * (0 / 254 / 316 / 353)。所以只要认定「这一段是公式」,两侧就能对齐 ——
1215
+ * 不必去猜公式该渲染成什么。
1216
+ *
1217
+ * 用户裁定:公式是**原子单位**——选区碰到公式的任何一部分,整个公式一起高亮。
1218
+ * 通配区间的两端就落在公式边界上,天然满足这条。
1219
+ *
1220
+ * @param {string} text 屏上显示文本(原样)
1221
+ * @param {string} quote 原话(公式是 `$…$` 源码形态)
1222
+ * @returns {{start: number, end: number}|null} 显示文本里的区间;对不上返回 null
1223
+ */
1224
+ /** 去掉空白,同时留一张「压缩下标 → 原文下标」的表。 */
1225
+ function squeezeWithIndex(text) {
1226
+ let squeezed = ''
1227
+ const index = []
1228
+ for (let at = 0; at < text.length; at++) {
1229
+ if (isSpaceChar(text[at])) continue
1230
+ squeezed += text[at]
1231
+ index.push(at)
1232
+ }
1233
+ return { squeezed, index }
1234
+ }
1235
+
1236
+ /**
1237
+ * 按 `$…$` 把一段文字切成「文字段 / 公式段」,返回**文字段**(公式段为 null)。
1238
+ * 奇数下标是公式。没有公式时返回 null —— 调用方据此判断这一侧能不能当模板。
1239
+ */
1240
+ function formulaSplit(text) {
1241
+ const pattern = formulaSource()
1242
+ if (!pattern.test(text)) return null
1243
+ // 🔴 正则**必须带捕获组**:String.split 在没有捕获组时会把分隔符整个丢掉,
1244
+ // 返回的只是文字段(['A ', ' B ', ' C']),公式位置根本没占位。
1245
+ // 旧实现用 map((p, i) => i % 2 === 0 ? p : null) 于是把下标 1 的**真文字**判成公式、
1246
+ // 三处公式全部丢失 —— 实测把 7 段切成 4 段,所有跨公式的划线定位失败。
1247
+ // 带捕获组后 split 返回 [文字, 公式, 文字, 公式, …],奇数位才是公式。
1248
+ return text.split(formulaSource()).map((piece, at) => at % 2 === 1 ? null : piece)
1249
+ }
1250
+
1251
+ /**
1252
+ * 一侧含行内公式、另一侧不含时,把**含公式的那侧**当模板切段,另一侧逐段定位。
1253
+ *
1254
+ * 🔴 方向是**双向**的,实测两种都真实存在:
1255
+ * · 划线取自 PDF 档 —— quote 是字形 `H(x)`(无 `$…$`),原文栏的 display 是
1256
+ * LaTeX 源码(3 处 `$…$`)。此时模板是 display。
1257
+ * · 划线取自原文栏 —— quote 带 `$…$` 源码,PDF 档的 display 是字形。此时模板是 quote。
1258
+ *
1259
+ * 先把模板的公式段换成通配,再用公式**之间**的文字段去另一侧依次定位;两侧都在
1260
+ * 压缩坐标(去空白)里走,最后换算回**目标文本**的原下标。
1261
+ *
1262
+ * 公式是原子单位(用户裁定):通配区间横跨整个公式,选区碰到哪一部分都整块高亮。
1263
+ *
1264
+ * @param {string} text 屏上显示文本(定位目标)
1265
+ * @param {string} quote 原话
1266
+ * @returns {{start: number, end: number}|null} 目标文本里的区间;对不上返回 null
1267
+ */
1268
+ function locateQuoteAcrossFormulas(text, quote) {
1269
+ if (text === '' || quote === '') return null
1270
+ const target = squeezeWithIndex(text)
1271
+ if (target.squeezed === '') return null
1272
+ // 两侧是否含 $…$。含公式的那一侧当**模板**切段,另一侧当**定位目标**。
1273
+ const quoteParts = formulaSplit(quote)
1274
+ const textParts = formulaSplit(text)
1275
+ if (quoteParts === null && textParts === null) return null
1276
+ const isText = piece => typeof piece === 'string' && piece !== ''
1277
+ const textPiecesOf = parts => parts.map(piece => piece === null ? null : piece.replace(spaceRun(), ''))
1278
+
1279
+ /**
1280
+ * 在 needle 里依次找 template 的各个文字段,返回覆盖到的**压缩坐标**区间。
1281
+ * 公式位不推进 —— 它只表示"这里跳过任意内容"。
1282
+ */
1283
+ const align = (needle, template) => {
1284
+ const wanted = textPiecesOf(template)
1285
+ const first = wanted.findIndex(isText)
1286
+ const last = wanted.length - 1 - [...wanted].reverse().findIndex(isText)
1287
+ if (first < 0) return null
1288
+ let cursor = needle.squeezed.indexOf(wanted[first])
1289
+ if (cursor < 0) return null
1290
+ const startAt = cursor
1291
+ cursor += wanted[first].length
1292
+ let endAt = cursor
1293
+ for (let at = first + 1; at <= last; at += 1) {
1294
+ const piece = wanted[at]
1295
+ if (piece === null) continue
1296
+ const found = needle.squeezed.indexOf(piece, cursor)
1297
+ if (found < 0) return null
1298
+ cursor = found + piece.length
1299
+ endAt = cursor
1300
+ }
1301
+ return { startAt, endAt }
1302
+ }
1303
+
1304
+ // 🔴 两种情形方向相反,必须分开写 —— 混用会让区间落在错误的坐标系上。
1305
+ let from
1306
+ let to
1307
+ if (textParts !== null && quoteParts === null) {
1308
+ // ① display 含公式、quote 不含(划线取自 PDF 档)。
1309
+ // quote 那侧的文字就是 display 文字段压缩后来的一份,先在 quote 里确认
1310
+ // 「这条划线覆盖到 display 的第几段」——对不上就说明不是同一段文字。
1311
+ const quoteTarget = squeezeWithIndex(quote)
1312
+ if (align(quoteTarget, textParts) === null) return null
1313
+ // 🔴 区间取 **display 自己**的范围:从第一段文字的开头到最后一段文字的结尾。
1314
+ // 中间那些公式段原样落在区间里,天然被整块包住 —— 这正是「公式是原子」
1315
+ // 要的效果,也不需要任何坐标换算(换算过一次,两次都算错)。
1316
+ // 带捕获组的 split 直接给出 [文字, 公式源码, 文字, …],按真实长度累加即可 ——
1317
+ // 不需要再猜公式段有多长。
1318
+ const shape = text.split(formulaSource())
1319
+ const isTextSeg = (piece, at) => at % 2 === 0 && piece !== ''
1320
+ const firstIdx = shape.findIndex(isTextSeg)
1321
+ const lastIdx = shape.length - 1 - [...shape].reverse().findIndex(isTextSeg)
1322
+ if (firstIdx < 0 || lastIdx < firstIdx) return null
1323
+ let cursorAt = 0
1324
+ let startPx = -1
1325
+ let endPx = -1
1326
+ for (let at = 0; at < shape.length; at += 1) {
1327
+ if (at === firstIdx) startPx = cursorAt
1328
+ cursorAt += shape[at].length
1329
+ if (at === lastIdx) { endPx = cursorAt; break }
1330
+ }
1331
+ if (startPx < 0 || endPx <= startPx) return null
1332
+ from = startPx
1333
+ to = endPx
1334
+ } else if (quoteParts !== null && textParts === null) {
1335
+ // ② quote 含公式、display 不含(划线取自原文栏)。
1336
+ // 用 quote 的文字段去 **display** 里定位,直接得到 display 区间。
1337
+ const hit = align(target, quoteParts)
1338
+ if (hit === null) return null
1339
+ from = target.index[hit.startAt]
1340
+ to = target.index[hit.endAt - 1] + 1
1341
+ // 🔴 划线**以公式开头/结尾**时,那个公式在 display 里是 KaTeX 渲染后的纯文本
1342
+ // (没有 `$…$`),所以 align 只能从公式**之后**的那段文字起算 —— 起点于是落在
1343
+ // 公式右边,公式本身永远进不了区间。实测 ResNet p2-p3:整段划线以
1344
+ // `$\mathcal{F}(\mathbf{x})+\mathbf{x}$` 开头,区间被算成 `[40,228)`,公式被排除在外。
1345
+ // 判据是模板的**首/尾段为空串**:空 = 划线是从那个公式开始的(结束的)。
1346
+ // 把区间扩到 display 的对应端点 —— 不猜长度,只是把「公式被当成光标位置」补回来。
1347
+ if (quoteParts[0] === '') from = 0
1348
+ if (quoteParts[quoteParts.length - 1] === '') to = text.length
1349
+ } else {
1350
+ return null
1351
+ }
1352
+ if (!(to > from)) return null
1353
+ // 🔴 **公式原子化**(用户裁定):选区碰到公式的任何一部分,整个公式一起高亮。
1354
+ for (let guard = 0; guard < 32; guard += 1) {
1355
+ let grown = false
1356
+ // 🔴 正则**必须在循环外建一次**:每次 new 一个带 g 的正则,lastIndex 永远从 0 起,
1357
+ // exec 每次都返回第一个匹配 —— 这是死循环(实测把测试跑成超时、日志 0 字节)。
1358
+ const pattern = formulaSource()
1359
+ let hit
1360
+ while ((hit = pattern.exec(text)) !== null) {
1361
+ const formulaFrom = hit.index
1362
+ const formulaTo = hit.index + hit[0].length
1363
+ if (!(formulaFrom < to && formulaTo > from)) continue
1364
+ if (formulaFrom < from) { from = formulaFrom; grown = true }
1365
+ if (formulaTo > to) { to = formulaTo; grown = true }
1366
+ }
1367
+ if (!grown) break
1368
+ }
1369
+ return { start: from, end: to }
1370
+ }
1371
+
1160
1372
  function quoteSpanPlan(mark, text) {
1161
1373
  if (mark === null || typeof mark !== 'object' || mark.anchor !== 'exact') return null
1162
1374
  const display = typeof text === 'string' ? text : ''
@@ -1169,7 +1381,22 @@ function createLibraryModel() {
1169
1381
  const at = locateQuoteInText(display, candidate, prefix, suffix)
1170
1382
  if (at >= 0) return { quote: candidate, start: at, end: at + candidate.length }
1171
1383
  }
1172
- // 🔴 逐字都对不上再试括号法:公式段的选区串是 KaTeX 两条渲染臂拼的,永远逐字对不上。
1384
+ // 🔴 逐字对不上先试**忽略空白**:PDF 文字层的行接缝没有分隔符,而原话那份有空格,
1385
+ // 同一次拖选两侧差着几个空格。这一步只放宽空白,别的字仍然要逐字对上。
1386
+ for (const candidate of quoteCandidates(quote)) {
1387
+ const span = locateQuoteIgnoringSpace(display, candidate)
1388
+ if (span !== null) return { quote: display.slice(span.start, span.end), start: span.start, end: span.end }
1389
+ }
1390
+ // 🔴 再试**公式通配**:同一条划线在两侧的文字形态不同 —— 原文栏把公式还原成 LaTeX
1391
+ // 源码($\mathcal{H}(\mathbf{x})$),PDF 档是渲染后的字形(H(x))。公式之间的普通文字
1392
+ // 逐字相同,所以只认「这一段是公式」、把它当通配,两侧就能对齐(见
1393
+ // {@link locateQuoteAcrossFormulas})。用户裁定公式是**原子单位**:选区碰到哪一部分,
1394
+ // 整个公式一起高亮。
1395
+ for (const candidate of quoteCandidates(quote)) {
1396
+ const span = locateQuoteAcrossFormulas(display, candidate)
1397
+ if (span !== null) return { quote: display.slice(span.start, span.end), start: span.start, end: span.end }
1398
+ }
1399
+ // 🔴 再试括号法:公式段的选区串是 KaTeX 两条渲染臂拼的,永远逐字对不上。
1173
1400
  // 只有**长原话**才走这一步 —— 短原话的首尾两截谁都能对上,夹出来的区间没有意义。
1174
1401
  for (const candidate of quoteCandidates(quote)) {
1175
1402
  const span = bracketSpan(display, candidate)
@@ -1575,13 +1802,46 @@ function createLibraryModel() {
1575
1802
  const range = new Range()
1576
1803
  if (!locate(row.node, target.start, false, range) || !locate(row.node, target.end, true, range)) continue
1577
1804
  const got = range.toString()
1578
- if (!got || (direct ? got !== direct.quote : got.trim() !== got)) continue
1805
+ // 🔴 终验:**普通文字逐字相等**;含公式时按 {@link formulaSelectionText} 的口径比。
1806
+ //
1807
+ // KaTeX 把公式摊成「TeX 源码 + 渲染字形」留在 textContent 里(实测
1808
+ // `$\mathcal{F}(\mathbf{x})+\mathbf{x}$` 是 34 字源码 + 5 字字形),与 `$…$` 形态的
1809
+ // quote 永远不可能逐字相等。用严格相等的话,凡覆盖公式的划线都被这道终验拒掉 ——
1810
+ // 实测区间已算对(`[0,228)` 覆盖公式)却 `painted = 0`,整条划线一个字都不画。
1811
+ // 折符号也**不行**(实测折完 190 vs 184:`\mathcal`/`\mathbf` 的命令名留在 DOM 里,
1812
+ // 而 quote 那侧只有符号)—— 唯一正确的口径是把公式还原成 `$…$` 再比。
1813
+ const matches = () => {
1814
+ if (!got) return false
1815
+ if (!direct) return got.trim() === got
1816
+ if (got === direct.quote) return true
1817
+ if (!formulaSource().test(direct.quote) && !formulaSource().test(got)) return false
1818
+ const contents = range.cloneContents?.()
1819
+ if (!contents) return false
1820
+ return formulaSelectionText(contents).trim() === direct.quote.trim()
1821
+ }
1822
+ if (!matches()) continue
1579
1823
  const name = 'dsh-paper-mark-' + color
1580
1824
  let entry = names.get(name)
1581
1825
  if (!entry) { entry = { ranges: [], hits: [] }; names.set(name, entry) }
1582
1826
  entry.ranges.push(range)
1583
1827
  entry.hits ??= []
1584
1828
  entry.hits.push({ range, markId: mark.id })
1829
+ // 🔴 **公式整块上色**(用户裁定:公式是原子单位,选区碰到哪一部分都整块亮)。
1830
+ //
1831
+ // KaTeX 把一个公式渲染成两条臂:`.katex-mathml`(`annotation` 里的 TeX 源码,
1832
+ // 被 clip 成 1×1 摆在屏幕外)与 `.katex-html`(真正看得见的字形)。实测
1833
+ // ResNet p2-p3 的 `$\mathcal{F}(\mathbf{x})+\mathbf{x}$`:mathml 臂占 34 个字符、
1834
+ // html 臂只有 5 个(`F(x)+x`)。划线的区间按 DOM 字符算,**整段都落在 mathml 臂上**,
1835
+ // 于是浏览器把颜色涂在那 1×1 的不可见区域 —— 用户看到的就是「公式没高亮」。
1836
+ //
1837
+ // 补法:凡区间碰到的 `.katex`,另给它的 `.katex-html` 加一条整块 Range。
1838
+ // 与 PDF 档 `pdfQuoteInText` 的口径一致,也不动定位逻辑一个字。
1839
+ for (const formula of katexArmsIn(row.node, range)) {
1840
+ const arm = new Range()
1841
+ arm.selectNodeContents(formula)
1842
+ entry.ranges.push(arm)
1843
+ entry.hits.push({ range: arm, markId: mark.id })
1844
+ }
1585
1845
  painted += 1
1586
1846
  completed.set(row.node, completed.get(row.node) + 1)
1587
1847
  }
@@ -1595,6 +1855,31 @@ function createLibraryModel() {
1595
1855
  return painted
1596
1856
  }
1597
1857
 
1858
+ /**
1859
+ * 一段 Range 碰到的所有 KaTeX 公式,返回各自**可见臂**(`.katex-html`)的根节点。
1860
+ *
1861
+ * 🔴 KaTeX 的 `.katex` 里有两条臂:`.katex-mathml`(无障碍树,TeX 源码,被 clip 成
1862
+ * 1×1 摆在屏幕外)与 `.katex-html`(可见字形)。划线的区间按 DOM 字符算,落在
1863
+ * mathml 臂上时浏览器会把颜色涂在那不可见的 1×1 区域 —— 用户看到「公式没高亮」。
1864
+ * 这里把碰到的公式找出来,交给调用方补一条覆盖可见臂的 Range(公式整块上色)。
1865
+ *
1866
+ * @param {unknown} node 块节点
1867
+ * @param {Range} range 已定位好的区间
1868
+ * @returns {Element[]} 可见臂节点(不可用的公式跳过)
1869
+ */
1870
+ function katexArmsIn(node, range) {
1871
+ const formulas = typeof node?.querySelectorAll === 'function' ? node.querySelectorAll('.katex') : []
1872
+ const arms = []
1873
+ for (const formula of formulas) {
1874
+ const arm = typeof formula?.querySelector === 'function' ? formula.querySelector('.katex-html') : null
1875
+ if (!arm) continue
1876
+ // 只要区间**碰到**这个公式就整块亮(用户裁定:公式是原子单位)。
1877
+ let touches = true
1878
+ try { touches = typeof range?.intersectsNode === 'function' ? range.intersectsNode(formula) : true } catch { touches = true }
1879
+ if (touches) arms.push(arm)
1880
+ }
1881
+ return arms
1882
+ }
1598
1883
  // KaTeX 同时包含无障碍树与可视树。克隆后用公式源码替换,避免把两份文字一起引用。
1599
1884
  function formulaSelectionText(node) {
1600
1885
  const clone = node.cloneNode(true)
@@ -4728,9 +5013,25 @@ h4.paper-reader-block{font-size:13px;font-weight:650}
4728
5013
  // 并排时把「我读到哪一段」广播给另一格(D183)。只在并排里发 —— 单格时没有听众,
4729
5014
  // 白算一次锚点。判据是本格是不是被分出来的那一格,或对面那格有没有登记过存在。
4730
5015
  if (!paneSyncLiveRef.current) return
4731
- // 回声屏蔽(D185 / D189):跟随途中或刚跟完的静默窗口里一律不广播。
4732
- // 🔴 只比「游标是否正好落在目标块上」不够了:跟随是**逐帧**靠近的,途中游标会扫过
4733
- // 一串中间块,每一个都会被当成"读者滚到了这里"推回去,两格就互相拖着走。
5016
+ // 🔴 **手动滚动优先于跟随**(2026-11-20 用户实测抓出)。
5017
+ //
5018
+ // 现象:并排里先在译文滚,原文跟着滚;接着去滚原文,**原文一动不动**。反过来一样。
5019
+ //
5020
+ // 根因不是渲染,是这里原来只 `return` 了事:
5021
+ //
5022
+ // ```text
5023
+ // A 滚 → 广播 → B 的 paneFollowRef 非空、排上 requestAnimationFrame
5024
+ // runFollow 每帧 container.scrollTop = next,并把 hold 推到 now+120ms
5025
+ // 用户去滚 B → onReaderScroll 被 hold 拦下 return(既不广播,也不取消 follow)
5026
+ // → 但 runFollow 还在跑,下一帧又把 B 拉回去 → 用户看到「滚不动」
5027
+ // ```
5028
+ //
5029
+ // 所以静默窗口只该压**广播**,不能顺手把用户的操作也压掉。用户真的滚动时,
5030
+ // 必须立刻终止本格的跟随,并把状态交还给用户。
5031
+ // 🔴 **不能靠 scroll 事件本身来区分「用户」和「回声」**:回声也是 scroll 事件,
5032
+ // 而且它在 paneFollowRef 还有效时就到了 —— 拿它取消跟随,跟随会被自己的尾巴打断,
5033
+ // 永远收敛不了。真正的用户信号只有**输入事件**(滚轮 / 触摸 / 指针 / 按键),
5034
+ // 所以那一路单独取消跟随(见 stopFollowForUser),这里仍然只压广播。
4734
5035
  if (Date.now() < paneSyncHoldRef.current.until) return
4735
5036
  publishScrollAnchor(readerRef.current, cursor.block, paneRole, anchor === null ? 0 : anchor.ratio)
4736
5037
  }
@@ -4786,11 +5087,54 @@ h4.paper-reader-block{font-size:13px;font-weight:650}
4786
5087
  }
4787
5088
  if (typeof requestAnimationFrame === 'function') paneFollowFrameRef.current = requestAnimationFrame(runFollow)
4788
5089
  }
5090
+ /**
5091
+ * 用户一旦真的动手,立刻交出控制权(2026-11-20 用户实测)。
5092
+ *
5093
+ * 🔴 判据必须用**输入事件**,不能用 scroll 事件:跟随自身每帧设 scrollTop,
5094
+ * 它产生的 scroll 事件与用户的混在一起,拿来判断会把跟随自己的尾巴当成人手,
5095
+ * 跟随永远收敛不了。滚轮、触摸、指针按下、方向键 —— 这些只可能来自用户。
5096
+ *
5097
+ * 不这么做的后果(实测):A 格滚完,B 格的 runFollow 还在跑;用户去滚 B,
5098
+ * 每帧被拉回 A 的位置,表现出来就是「这一格滚不动」,且要等 follow 自己收敛才恢复。
5099
+ */
5100
+ const stopFollowForUser = () => {
5101
+ if (paneFollowRef.current === null && paneFollowFrameRef.current === null)
5102
+ return
5103
+ paneFollowRef.current = null
5104
+ if (paneFollowFrameRef.current !== null && typeof cancelAnimationFrame === 'function')
5105
+ cancelAnimationFrame(paneFollowFrameRef.current)
5106
+ paneFollowFrameRef.current = null
5107
+ // 窗口一起撤掉:下一帧用户的真实滚动应当照常广播给对面。
5108
+ paneSyncHoldRef.current.until = 0
5109
+ }
5110
+ // 🔴 **监听挂在第一次真正收到锚点上,不在 effect 开头**(2026-11-20)。
5111
+ //
5112
+ // 这个 effect 的依赖是 [paneRole, paneView],它不能保证在 layoutRef 回调 ref
5113
+ // 赋值之后再跑一次。effect 开头直接读 `layoutElRef.current` 可能拿到 null,
5114
+ // 于是监听静默没挂上 —— 而用户看到的仍然是「滚不动」,且没有任何报错。
5115
+ // 收到锚点时容器一定已经挂好(锚点就是从滚动容器里发出来的),在那儿挂最稳。
5116
+ let settled = null
5117
+ const bindUserStop = () => {
5118
+ const node = layoutElRef.current
5119
+ if (node === null || node === settled || typeof node?.addEventListener !== 'function') return
5120
+ if (settled !== null && typeof settled.removeEventListener === 'function') {
5121
+ settled.removeEventListener('wheel', stopFollowForUser)
5122
+ settled.removeEventListener('touchmove', stopFollowForUser)
5123
+ settled.removeEventListener('pointerdown', stopFollowForUser)
5124
+ settled.removeEventListener('keydown', stopFollowForUser)
5125
+ }
5126
+ settled = node
5127
+ settled.addEventListener('wheel', stopFollowForUser, { passive: true })
5128
+ settled.addEventListener('touchmove', stopFollowForUser, { passive: true })
5129
+ settled.addEventListener('pointerdown', stopFollowForUser, { passive: true })
5130
+ settled.addEventListener('keydown', stopFollowForUser)
5131
+ }
4789
5132
  const stop = subscribeScrollAnchor(anchor => {
4790
5133
  if (!shouldFollowAnchor(anchor, { live: paneSyncLiveRef.current, role: paneRole, documentId: readerRef.current })) return
4791
5134
  const container = layoutElRef.current
4792
5135
  if (container === null || typeof container !== 'object') return
4793
5136
  if (blockNodeOf(container, anchor.blockId) === null) return
5137
+ bindUserStop()
4794
5138
  paneFollowRef.current = { blockId: anchor.blockId, ratio: anchor.ratio }
4795
5139
  // 已经有一帧在等就复用它:锚点每秒会来几十次,每次都排一帧等于自己跟自己打架。
4796
5140
  if (paneFollowFrameRef.current === null && typeof requestAnimationFrame === 'function') {
@@ -4799,11 +5143,17 @@ h4.paper-reader-block{font-size:13px;font-weight:650}
4799
5143
  })
4800
5144
  return () => {
4801
5145
  stop()
5146
+ if (settled !== null && typeof settled.removeEventListener === 'function') {
5147
+ settled.removeEventListener('wheel', stopFollowForUser)
5148
+ settled.removeEventListener('touchmove', stopFollowForUser)
5149
+ settled.removeEventListener('pointerdown', stopFollowForUser)
5150
+ settled.removeEventListener('keydown', stopFollowForUser)
5151
+ }
4802
5152
  if (paneFollowFrameRef.current !== null && typeof cancelAnimationFrame === 'function') cancelAnimationFrame(paneFollowFrameRef.current)
4803
5153
  paneFollowFrameRef.current = null
4804
5154
  paneFollowRef.current = null
4805
5155
  }
4806
- }, [paneRole, paneView])
5156
+ }, [paneRole, paneView, readerId])
4807
5157
 
4808
5158
  // ---- 阅读器(T16b) ----
4809
5159
  // 进入阅读器时拉一次 /reader。**只在进入时拉**,不挂轮询:/reader 会带一整篇的块,
@@ -10270,6 +10620,9 @@ h4.paper-reader-block{font-size:13px;font-weight:650}
10270
10620
  // 文本级高亮(D167)的纯函数:显示文本里的原话区间、每块的第一条计划、跨块夹回、
10271
10621
  // 挂高亮的核心过程(scope/names 进出,测试喂假 DOM)。
10272
10622
  exports.quoteSpanPlan = quoteSpanPlan
10623
+ exports.locateQuoteIgnoringSpace = locateQuoteIgnoringSpace
10624
+ exports.locateQuoteAcrossFormulas = locateQuoteAcrossFormulas
10625
+ exports.formulaSplit = formulaSplit
10273
10626
  // 中英匹配(D171):句级切分与跨列映射,测试直接喂中英两列断言。
10274
10627
  exports.sentenceSpans = sentenceSpans
10275
10628
  exports.crossSentenceSpan = crossSentenceSpan