@ggboy123/dsh-paper-reader 1.5.0 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -5
- package/cordis.patch.yml +73 -0
- package/dist/host.js +2 -13
- package/dist/mineru.js +5 -2
- package/dist/search.js +11 -3
- package/dist/tools.js +1 -1
- package/dist/translate.js +291 -45
- package/lib/client.js +14 -4
- package/package.json +8 -6
- package/reader/index.html +726 -6
- package/runtime/babeldoc-driver.py +379 -0
- package/dist/preset.js +0 -31
- package/presets/paper-reader/agent.cordis.yml +0 -66
- package/presets/paper-reader/preset.yml +0 -3
package/reader/index.html
CHANGED
|
@@ -44,6 +44,13 @@
|
|
|
44
44
|
/* min-width:0 —— flex 项默认 min-width:auto 会拒绝收缩,空间不够时转而去挤按钮;
|
|
45
45
|
让标题自己截断(已有 ellipsis),按钮才能保持原样 */
|
|
46
46
|
#status { flex: 0 1 auto; min-width: 0; color: var(--text-2); font-size: 12.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
|
|
47
|
+
/* 翻译进度条(只在生成中文版时出现):细长条 + 阶段文字,宽度跟着 babeldoc 的 overall 走 */
|
|
48
|
+
#zh-prog { flex: 0 1 auto; min-width: 0; max-width: 46%; display: inline-flex; align-items: center; gap: 7px; }
|
|
49
|
+
#zh-prog[hidden] { display: none; }
|
|
50
|
+
#zh-prog .track { position: relative; flex: 1 1 auto; min-width: 48px; height: 6px; border-radius: 3px; background: var(--border); overflow: hidden; }
|
|
51
|
+
#zh-prog .fill { position: absolute; top: 0; bottom: 0; left: 0; width: 0; border-radius: 3px; background: var(--accent); transition: width .35s ease; }
|
|
52
|
+
#zh-prog .txt { flex: none; font-size: 12px; color: var(--text-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
|
|
53
|
+
@media (prefers-reduced-motion: reduce) { #zh-prog .fill { transition: none; } }
|
|
47
54
|
/* PDF 文件路径:可点的只读文本(点击复制)。⚠️ 必须可收缩(flex:0 1 auto + min-width:0)——
|
|
48
55
|
它是最长的内容项,窄列时先让位给按钮、自己截断(title 悬停始终有完整路径),
|
|
49
56
|
曾经 max-width:34% + flex:none 在窄列把 +/适宽 挤出可视区(用户实测) */
|
|
@@ -134,6 +141,55 @@
|
|
|
134
141
|
#eq-toggle.muted { opacity: .55; }
|
|
135
142
|
#ask-copy { background: none; border: 1px solid var(--border); border-radius: 6px; color: var(--text-2); cursor: pointer; margin-right: auto; }
|
|
136
143
|
|
|
144
|
+
/* ---------- 在文中查找(顶栏 🔍 / Ctrl+F)----------
|
|
145
|
+
输入框与结果列表在**同一个浮窗**里:窗口可拖动、可拖边框改大小,开关只有窗口一件事,
|
|
146
|
+
不再有「输入条」和「结果面板」两套入口。页内高亮是 .findLayer 里的百分比盒子
|
|
147
|
+
(Range.getClientRects 算出来的词级矩形):不给 span 加背景类——span 常常是一整行,
|
|
148
|
+
而且加类会动 textLayer 的换行与划词。 */
|
|
149
|
+
#find-tools { display: flex; align-items: center; gap: 6px; padding: 6px 8px; border-bottom: 1px solid var(--border); flex: none; }
|
|
150
|
+
#find-input { flex: 1; min-width: 0; border: 1px solid var(--border); border-radius: 6px; padding: 4px 8px; font: inherit;
|
|
151
|
+
font-size: 12.5px; line-height: 18px; background: var(--panel); color: var(--text); }
|
|
152
|
+
#find-input:focus { outline: none; border-color: var(--accent); }
|
|
153
|
+
#find-count { flex: none; min-width: 44px; max-width: 96px; text-align: center; font-size: 12px; color: var(--text-2);
|
|
154
|
+
font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
|
|
155
|
+
#find-count.hit { color: var(--accent); }
|
|
156
|
+
#find-count:empty { display: none; }
|
|
157
|
+
/* 查找浮窗:输入行 + 结果列表装在一起,可拖动、可拖边框改大小,默认 330×340,不占满阅读区。
|
|
158
|
+
旧版用右侧滑出面板占满整列高度,会遮住半页纸,所以改成浮窗。 */
|
|
159
|
+
#find-window { position: fixed; z-index: 46; display: none; flex-direction: column; width: 330px; height: 340px;
|
|
160
|
+
min-width: 240px; min-height: 190px; background: var(--panel); border: 1px solid var(--border);
|
|
161
|
+
border-radius: 10px; box-shadow: 0 10px 30px rgba(0,0,0,.18); overflow: hidden; }
|
|
162
|
+
#find-window.on { display: flex; }
|
|
163
|
+
#find-head { display: flex; align-items: center; gap: 6px; padding: 6px 8px; cursor: move; user-select: none;
|
|
164
|
+
border-bottom: 1px solid var(--border); font-size: 12.5px; font-weight: 600; flex: none; }
|
|
165
|
+
#find-head .spacer { flex: 1; }
|
|
166
|
+
#find-headcount { font-weight: 400; color: var(--text-2); font-variant-numeric: tabular-nums; }
|
|
167
|
+
#find-meta { padding: 6px 10px 2px; font-size: 11.5px; color: var(--text-2); line-height: 1.5; flex: none; }
|
|
168
|
+
/* 列表自己滚:头部与说明固定在窗口上沿,关按钮永远在最上面看得见 */
|
|
169
|
+
#find-list { flex: 1; overflow: auto; padding: 2px 8px 10px; }
|
|
170
|
+
#find-grip { position: absolute; right: 0; bottom: 0; width: 16px; height: 16px; cursor: nwse-resize; z-index: 2; }
|
|
171
|
+
#find-grip::after { content: ''; position: absolute; right: 3px; bottom: 3px; width: 8px; height: 8px;
|
|
172
|
+
border-right: 2px solid var(--text-2); border-bottom: 2px solid var(--text-2); border-bottom-right-radius: 3px; opacity: .55; }
|
|
173
|
+
/* 右边缘 / 下边缘也能拖:整条边都是热区,不用非得瞄准右下角那 16px */
|
|
174
|
+
#find-grip-r { position: absolute; top: 68px; right: 0; bottom: 16px; width: 6px; cursor: ew-resize; z-index: 2; }
|
|
175
|
+
#find-grip-b { position: absolute; left: 0; right: 16px; bottom: 0; height: 6px; cursor: ns-resize; z-index: 2; }
|
|
176
|
+
.tbtn.mini { height: 24px; padding: 0 6px; border-radius: 5px; }
|
|
177
|
+
.tbtn.mini svg.ic { width: 14px; height: 14px; }
|
|
178
|
+
#find-window .tbtn.mini.on { border-color: var(--accent); color: var(--accent); }
|
|
179
|
+
.find-page { margin: 10px 0 2px; font-size: 12px; font-weight: 600; color: var(--text-2); }
|
|
180
|
+
.find-item { display: block; width: 100%; text-align: left; border: 1px solid var(--border); border-radius: 6px;
|
|
181
|
+
background: var(--bg); color: var(--text); padding: 5px 8px; margin: 3px 0; cursor: pointer;
|
|
182
|
+
font: inherit; font-size: 12px; line-height: 1.5; }
|
|
183
|
+
.find-item:hover { border-color: var(--accent); }
|
|
184
|
+
.find-item.cur { border-color: var(--accent); background: var(--accent-soft); }
|
|
185
|
+
.find-item mark { background: rgba(255,196,0,.55); color: inherit; border-radius: 2px; padding: 0 1px; }
|
|
186
|
+
.findLayer { position: absolute; inset: 0; pointer-events: none; z-index: 3; }
|
|
187
|
+
.find-box { position: absolute; border-radius: 2px; background: rgba(255,214,0,.45); }
|
|
188
|
+
.find-box.cur { background: rgba(255,146,0,.55); box-shadow: 0 0 0 1.5px rgba(230,110,0,.85); }
|
|
189
|
+
/* 兜底高亮:Range 几何算不出来时给 span 加类(宁可糙一点,也不能「搜到了却看不见」) */
|
|
190
|
+
.textLayer span.find-hit { background: rgba(255,214,0,.45); border-radius: 2px; }
|
|
191
|
+
.textLayer span.find-hit.cur { background: rgba(255,146,0,.55); }
|
|
192
|
+
|
|
137
193
|
/* ---------- 选中即问浮层 ---------- */
|
|
138
194
|
#ask-pop { position: fixed; z-index: 50; display: none; background: var(--panel); border: 1px solid var(--border); border-radius: 10px; box-shadow: 0 6px 24px rgba(0,0,0,.16); padding: 10px; width: 320px; }
|
|
139
195
|
#ask-quote { font-size: 12px; color: var(--text-2); max-height: 64px; overflow: hidden; margin-bottom: 8px; border-left: 3px solid var(--accent-soft); padding-left: 8px; }
|
|
@@ -183,6 +239,9 @@
|
|
|
183
239
|
|
|
184
240
|
<div id="bar">
|
|
185
241
|
<span id="status">论文伴读</span>
|
|
242
|
+
<!-- 翻译进度条:只在 babeldoc 跑着时出现(宽度=overall,文字=阶段+百分比),
|
|
243
|
+
窄列下先让位给按钮(flex:0 1 auto + min-width:0),别把控件挤出去 -->
|
|
244
|
+
<span id="zh-prog" hidden><span class="track"><span class="fill"></span></span><span class="txt"></span></span>
|
|
186
245
|
<button id="pdf-path" style="display:none" title="点击复制文件路径"></button>
|
|
187
246
|
<button id="zh-toggle" class="tbtn" title="切换中文译文(没有译文时可生成)" style="display:none" data-icon="translate"></button>
|
|
188
247
|
<button id="dual-toggle" class="tbtn" title="用 babeldoc 的中英对照 PDF(模式 A)" style="display:none" data-icon="columns">对照</button>
|
|
@@ -193,6 +252,8 @@
|
|
|
193
252
|
<button id="sync-toggle" class="tbtn" title="同步滚动 / Sync scroll:开(两列一起滚,点击关闭)" style="display:none" data-icon="sync"></button>
|
|
194
253
|
<button id="zh-regen" class="tbtn" title="重新翻译:删除现有译文,用当前端点重跑(换模型后用)" style="display:none" data-icon="regen"></button>
|
|
195
254
|
<button id="dl-btn" class="tbtn" title="下载 PDF" style="display:none" data-icon="download"></button>
|
|
255
|
+
<!-- 在文中查找(Ctrl+F):入口紧挨下载,实际输入条是浮层,不占顶栏宽度 -->
|
|
256
|
+
<button id="find-open" class="tbtn" title="在文中查找(Ctrl+F)" style="display:none" data-icon="search"></button>
|
|
196
257
|
<span class="spacer"></span>
|
|
197
258
|
<button id="mode-toggle" class="tbtn" title="切换 滚动/翻页模式" style="display:none" data-icon="paged"></button>
|
|
198
259
|
<span id="pager" style="display:none">
|
|
@@ -227,6 +288,28 @@
|
|
|
227
288
|
<div id="eq-meta"></div>
|
|
228
289
|
<div id="eq-list"></div>
|
|
229
290
|
</div>
|
|
291
|
+
<!-- 查找:输入框与结果列表在同一个浮窗里(可拖动 / 拖边框改大小 / 精简↔全部,
|
|
292
|
+
位置尺寸记在 localStorage)。头部固定,只有 #find-list 自己滚,所以关按钮永远可见。 -->
|
|
293
|
+
<div id="find-window" role="dialog" aria-label="查找">
|
|
294
|
+
<div id="find-head">
|
|
295
|
+
<span>查找</span>
|
|
296
|
+
<span id="find-headcount"></span>
|
|
297
|
+
<span class="spacer"></span>
|
|
298
|
+
<button id="find-expand" class="tbtn mini" title="在「精简」与「全部」之间切换">精简</button>
|
|
299
|
+
<button id="find-window-close" class="tbtn mini" title="关闭(Esc)">✕</button>
|
|
300
|
+
</div>
|
|
301
|
+
<div id="find-tools">
|
|
302
|
+
<input id="find-input" type="text" placeholder="在文中查找…" autocomplete="off" spellcheck="false">
|
|
303
|
+
<span id="find-count"></span>
|
|
304
|
+
<button id="find-prev" class="tbtn mini" title="上一处(Shift+Enter)" data-icon="prev"></button>
|
|
305
|
+
<button id="find-next" class="tbtn mini" title="下一处(Enter)" data-icon="next"></button>
|
|
306
|
+
</div>
|
|
307
|
+
<div id="find-meta"></div>
|
|
308
|
+
<div id="find-list"></div>
|
|
309
|
+
<div id="find-grip-r" title="拖动改变宽度"></div>
|
|
310
|
+
<div id="find-grip-b" title="拖动改变高度"></div>
|
|
311
|
+
<div id="find-grip" title="拖动调整大小"></div>
|
|
312
|
+
</div>
|
|
230
313
|
</div>
|
|
231
314
|
|
|
232
315
|
<div id="ask-pop">
|
|
@@ -292,10 +375,12 @@ const ICONS = {
|
|
|
292
375
|
sync: '<path d="m17 2 4 4-4 4"/><path d="M3 11v-1a4 4 0 0 1 4-4h14"/><path d="m7 22-4-4 4-4"/><path d="M21 13v1a4 4 0 0 1-4 4H3"/>',
|
|
293
376
|
syncOff: '<path d="m18.84 12.25 1.72-1.71h-.02a5.004 5.004 0 0 0-.12-7.07 5.006 5.006 0 0 0-6.95 0l-1.72 1.71"/><path d="m5.17 11.75-1.71 1.71a5.004 5.004 0 0 0 .12 7.07 5.006 5.006 0 0 0 6.95 0l1.71-1.71"/><line x1="8" x2="8" y1="2" y2="5"/><line x1="2" x2="5" y1="8" y2="8"/><line x1="16" x2="16" y1="19" y2="22"/><line x1="19" x2="22" y1="16" y2="16"/>',
|
|
294
377
|
spin: '<path d="M21 12a9 9 0 1 1-6.219-8.56"/>',
|
|
378
|
+
search: '<circle cx="11" cy="11" r="7"/><path d="m20.5 20.5-3.9-3.9"/>',
|
|
379
|
+
close: '<path d="M18 6 6 18"/><path d="m6 6 12 12"/>',
|
|
295
380
|
};
|
|
296
381
|
const iconHtml = (name) => `<svg class="ic${name === 'spin' ? ' spin' : ''}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">${ICONS[name]}</svg>`;
|
|
297
382
|
// 静态按钮按 data-icon 注入一次;动态按钮(翻译/重翻/模式切换)在状态变化时用同一函数换图标
|
|
298
|
-
document.querySelectorAll('#bar [data-icon]').forEach((b) => b.insertAdjacentHTML('afterbegin', iconHtml(b.dataset.icon)));
|
|
383
|
+
document.querySelectorAll('#bar [data-icon], #find-window [data-icon]').forEach((b) => b.insertAdjacentHTML('afterbegin', iconHtml(b.dataset.icon)));
|
|
299
384
|
const api = (p, opts) => fetch('/paper-reader/api' + p, opts).then(async (r) => {
|
|
300
385
|
if (!r.ok) {
|
|
301
386
|
// 服务端错误体有两种:{error}(参数/权限)与 {reason}(zh/generate 的未启动原因,带完整说明)
|
|
@@ -397,8 +482,14 @@ async function openPaper(topic, name, opts = {}) {
|
|
|
397
482
|
// 下载当前正在看的那个版本(原文/纯中文/中英对照)——导出译文的主要入口
|
|
398
483
|
function updateDlBtn() {
|
|
399
484
|
const btn = $('dl-btn');
|
|
400
|
-
|
|
485
|
+
const findBtn = $('find-open');
|
|
486
|
+
if (!current) {
|
|
487
|
+
btn.style.display = 'none';
|
|
488
|
+
if (findBtn) findBtn.style.display = 'none';
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
401
491
|
btn.style.display = '';
|
|
492
|
+
if (findBtn) findBtn.style.display = '';
|
|
402
493
|
btn.title = zhVariant === 'zh' ? '下载:纯中文版 PDF'
|
|
403
494
|
: zhVariant === 'dual' ? '下载:中英对照版 PDF' : '下载:原文 PDF';
|
|
404
495
|
}
|
|
@@ -414,7 +505,615 @@ function downloadCurrent() {
|
|
|
414
505
|
}
|
|
415
506
|
$('dl-btn').onclick = downloadCurrent;
|
|
416
507
|
|
|
508
|
+
/* ================= 在文中查找(顶栏 🔍 / Ctrl+F)=================
|
|
509
|
+
索引来自 pdf.js 的 getTextContent 而**不是** DOM:翻页/分屏模式 DOM 里只有当前页,
|
|
510
|
+
只查 DOM 会漏掉其它页的命中。归一化口径与 highlightSnippet 一致(忽略空白、连字符、
|
|
511
|
+
软连字符与大小写),论文跨行断词(dis-tributed)也能命中。
|
|
512
|
+
页内高亮不给 span 加背景类(span 常常是一整行,加类还会动 textLayer 的换行与划词),
|
|
513
|
+
改用 Range.getClientRects() 在 .findLayer 里画百分比盒子:精确到词、缩放后自动对齐、
|
|
514
|
+
不吃鼠标事件(pointer-events:none,划词提问照旧)。 */
|
|
515
|
+
const FIND_MAX_HITS = 2000; // 防御:极端查询(单个字母)不至于把面板撑爆
|
|
516
|
+
const FIND_WIN_KEY = 'dpr.findWin'; // 结果窗口的位置与尺寸(localStorage)
|
|
517
|
+
const FIND_EXPAND_KEY = 'dpr.findExpand'; // 精简/全部(1 = 全部)
|
|
518
|
+
const FIND_COMPACT_HITS = 30; // 精简模式列多少处
|
|
519
|
+
const findInputEl = $('find-input');
|
|
520
|
+
const findCountEl = $('find-count');
|
|
521
|
+
let find = { q: '', needle: '', hits: [], idx: -1, index: null, doc: null, building: false, shown: false, listStale: true, expand: false };
|
|
522
|
+
let findTimer = null;
|
|
523
|
+
let findWin = { x: null, y: null, w: 330, h: 340 }; // x/y 为 null = 还没定位过(贴 🔍 下方)
|
|
524
|
+
const findWinEl = $('find-window');
|
|
525
|
+
try { find.expand = localStorage.getItem(FIND_EXPAND_KEY) === '1'; } catch { /* 隐私模式:默认精简 */ }
|
|
526
|
+
|
|
527
|
+
/** 归一化 + 回原串索引:丢掉空白/连字符/软连字符并转小写(map[i] = 归一化串第 i 字符在原文的位置) */
|
|
528
|
+
function findNorm(str) {
|
|
529
|
+
let nh = '';
|
|
530
|
+
const map = [];
|
|
531
|
+
for (let i = 0; i < str.length; i++) {
|
|
532
|
+
const c = str[i].toLowerCase();
|
|
533
|
+
if (/\s/.test(c) || c === '-' || c === '') continue;
|
|
534
|
+
nh += c;
|
|
535
|
+
map.push(i);
|
|
536
|
+
}
|
|
537
|
+
return { nh, map };
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
/** 一页的文本索引:text 保留空格(给结果列表做上下文片段),nh/map 用于匹配 */
|
|
541
|
+
async function findPageIndex(n) {
|
|
542
|
+
const page = await pdfDoc.getPage(n);
|
|
543
|
+
const tc = await page.getTextContent();
|
|
544
|
+
// item 之间补空格:pdf.js 把同一行的单词切成多个 item,直接拼会粘连成 "theenclave"
|
|
545
|
+
const text = tc.items.map((it) => it.str || '').join(' ').replace(/\s+/g, ' ').trim();
|
|
546
|
+
const { nh, map } = findNorm(text);
|
|
547
|
+
return { text, nh, map };
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
async function findBuildIndex() {
|
|
551
|
+
if (!pdfDoc) return;
|
|
552
|
+
if (find.index && find.doc === pdfDoc) return;
|
|
553
|
+
const doc = pdfDoc;
|
|
554
|
+
find.doc = doc;
|
|
555
|
+
find.index = new Map();
|
|
556
|
+
find.building = true;
|
|
557
|
+
const total = doc.numPages;
|
|
558
|
+
for (let n = 1; n <= total; n++) {
|
|
559
|
+
if (find.doc !== doc) return; // 索引期间换了文档/变体:这份索引作废
|
|
560
|
+
try { find.index.set(n, await findPageIndex(n)); } catch { find.index.set(n, null); }
|
|
561
|
+
updateFindCount(`索引 ${n}/${total}`);
|
|
562
|
+
if (n % 8 === 0) await new Promise((r) => setTimeout(r, 0)); // 让出主线程,别卡住滚动
|
|
563
|
+
}
|
|
564
|
+
find.building = false;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
/** 上下文片段:命中前后各留一段,去掉首尾空白并把偏移跟着挪 */
|
|
568
|
+
function findSnippet(text, start, end) {
|
|
569
|
+
const pad = 42;
|
|
570
|
+
let a = Math.max(0, start - pad);
|
|
571
|
+
let b = Math.min(text.length, end + pad);
|
|
572
|
+
while (a < start && /\s/.test(text[a])) a++;
|
|
573
|
+
while (b > end && /\s/.test(text[b - 1])) b--;
|
|
574
|
+
return { pre: a > 0, post: b < text.length, text: text.slice(a, b), hitStart: start - a, hitEnd: end - a };
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
/** 扫全篇收集命中(按页、按出现顺序),每处带「该页内第几个」序号(高亮时要对号入座) */
|
|
578
|
+
function findCollect() {
|
|
579
|
+
find.hits = [];
|
|
580
|
+
find.idx = -1;
|
|
581
|
+
const needle = find.needle;
|
|
582
|
+
if (!needle || !find.index) return;
|
|
583
|
+
for (const [page, e] of find.index) {
|
|
584
|
+
if (!e || !e.nh) continue;
|
|
585
|
+
let from = 0;
|
|
586
|
+
let ord = 0;
|
|
587
|
+
for (;;) {
|
|
588
|
+
if (find.hits.length >= FIND_MAX_HITS) return;
|
|
589
|
+
const at = e.nh.indexOf(needle, from);
|
|
590
|
+
if (at < 0) break;
|
|
591
|
+
const start = e.map[at];
|
|
592
|
+
const end = e.map[at + needle.length - 1] + 1;
|
|
593
|
+
find.hits.push({ page, ord, snippet: findSnippet(e.text, start, end) });
|
|
594
|
+
ord += 1;
|
|
595
|
+
from = at + Math.max(1, needle.length);
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
function updateFindCount(note) {
|
|
601
|
+
if (!findCountEl) return;
|
|
602
|
+
findCountEl.classList.remove('hit');
|
|
603
|
+
if (note) { findCountEl.textContent = note; findCountEl.title = ''; return; }
|
|
604
|
+
if (!find.q || !find.needle) { findCountEl.textContent = ''; findCountEl.title = ''; return; }
|
|
605
|
+
if (find.building) { findCountEl.textContent = '索引…'; return; }
|
|
606
|
+
if (!find.hits.length) {
|
|
607
|
+
findCountEl.textContent = '无匹配';
|
|
608
|
+
findCountEl.title = '没有找到这段文字。扫描件没有文本层时也搜不到';
|
|
609
|
+
return;
|
|
610
|
+
}
|
|
611
|
+
// idx < 0 = 还没跳过(边打边搜阶段):显示总处数,别让用户看到 "0/12"
|
|
612
|
+
findCountEl.textContent = find.idx < 0 ? `${find.hits.length} 处` : `${find.idx + 1}/${find.hits.length}`;
|
|
613
|
+
findCountEl.classList.add('hit');
|
|
614
|
+
findCountEl.title = `共 ${find.hits.length} 处(Enter 下一处,Shift+Enter 上一处)`;
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
function findMetaText() {
|
|
618
|
+
if (!find.q) return '输入关键词:Enter 跳到第一处,Shift+Enter 上一处,Esc 关闭。';
|
|
619
|
+
if (!find.needle) return '';
|
|
620
|
+
if (find.building && !find.hits.length) return '正在建立索引…';
|
|
621
|
+
if (!find.hits.length) {
|
|
622
|
+
return `没有找到「${find.q}」。如果这篇是扫描件(没有文本层)就会搜不到,可先用转录或 MinerU 解析再试。`;
|
|
623
|
+
}
|
|
624
|
+
const pages = new Set(find.hits.map((h) => h.page)).size;
|
|
625
|
+
const capped = find.hits.length >= FIND_MAX_HITS ? `(已截断到前 ${FIND_MAX_HITS} 处)` : '';
|
|
626
|
+
return `「${find.q}」共 ${find.hits.length} 处,分布在 ${pages} 页${capped}。点条目直接跳转。`;
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
/* ---------- 结果窗口:可拖动、可拖边框缩放、可精简/全部 ---------- */
|
|
630
|
+
function findWinSave() {
|
|
631
|
+
if (!findWinEl) return;
|
|
632
|
+
try {
|
|
633
|
+
const r = findWinEl.getBoundingClientRect();
|
|
634
|
+
localStorage.setItem(FIND_WIN_KEY, JSON.stringify({
|
|
635
|
+
x: Math.round(r.left), y: Math.round(r.top), w: Math.round(r.width), h: Math.round(r.height),
|
|
636
|
+
}));
|
|
637
|
+
} catch { /* 隐私模式记不住:下次回到默认位置,不影响本次使用 */ }
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
function findWinLoad() {
|
|
641
|
+
if (!findWinEl) return;
|
|
642
|
+
try {
|
|
643
|
+
const raw = JSON.parse(localStorage.getItem(FIND_WIN_KEY) || 'null');
|
|
644
|
+
if (raw) {
|
|
645
|
+
if (Number.isFinite(raw.w)) findWin.w = Math.max(240, Math.min(720, raw.w));
|
|
646
|
+
if (Number.isFinite(raw.h)) findWin.h = Math.max(190, raw.h);
|
|
647
|
+
if (Number.isFinite(raw.x) && Number.isFinite(raw.y)) { findWin.x = raw.x; findWin.y = raw.y; }
|
|
648
|
+
}
|
|
649
|
+
} catch { /* 坏 JSON:用默认值 */ }
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
/** 落位并夹在视口内(窗口拖动/缩放/视口变化后都走这里,保证不会拖到屏幕外找不回来) */
|
|
653
|
+
function findWinPlace() {
|
|
654
|
+
if (!findWinEl) return;
|
|
655
|
+
const w = findWin.w;
|
|
656
|
+
const h = Math.min(findWin.h, Math.max(190, window.innerHeight - 40));
|
|
657
|
+
let x = findWin.x;
|
|
658
|
+
let y = findWin.y;
|
|
659
|
+
if (x === null || y === null) {
|
|
660
|
+
// 首次打开:贴在 🔍 按钮下方(用户点的那颗按钮旁边最符合直觉);按钮没显出来就贴右上角
|
|
661
|
+
const btn = $('find-open');
|
|
662
|
+
const r = btn ? btn.getBoundingClientRect() : null;
|
|
663
|
+
if (r && r.width) {
|
|
664
|
+
x = r.right - w;
|
|
665
|
+
y = r.bottom + 6;
|
|
666
|
+
} else {
|
|
667
|
+
x = window.innerWidth - w - 26;
|
|
668
|
+
y = 64;
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
x = Math.max(8, Math.min(x, window.innerWidth - w - 8));
|
|
672
|
+
y = Math.max(8, Math.min(y, window.innerHeight - h - 8));
|
|
673
|
+
findWinEl.style.width = w + 'px';
|
|
674
|
+
findWinEl.style.height = h + 'px';
|
|
675
|
+
findWinEl.style.left = Math.round(x) + 'px';
|
|
676
|
+
findWinEl.style.top = Math.round(y) + 'px';
|
|
677
|
+
findWin.x = Math.round(x);
|
|
678
|
+
findWin.y = Math.round(y);
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
function findWinShow(on) {
|
|
682
|
+
if (!findWinEl) return;
|
|
683
|
+
if (!on) {
|
|
684
|
+
findWinEl.classList.remove('on');
|
|
685
|
+
find.shown = false;
|
|
686
|
+
return;
|
|
687
|
+
}
|
|
688
|
+
findWinEl.classList.add('on');
|
|
689
|
+
find.shown = true;
|
|
690
|
+
findWinPlace();
|
|
691
|
+
renderFindList();
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
/** 精简模式只列当前命中附近的一段,避免几百处命中把窗口刷成冗长清单 */
|
|
695
|
+
function findVisibleIdx() {
|
|
696
|
+
const n = find.hits.length;
|
|
697
|
+
const out = [];
|
|
698
|
+
if (find.expand || n <= FIND_COMPACT_HITS) {
|
|
699
|
+
for (let i = 0; i < n; i++) out.push(i);
|
|
700
|
+
return out;
|
|
701
|
+
}
|
|
702
|
+
const center = find.idx < 0 ? 0 : find.idx;
|
|
703
|
+
let start = Math.max(0, center - Math.floor(FIND_COMPACT_HITS / 2));
|
|
704
|
+
if (start + FIND_COMPACT_HITS > n) start = Math.max(0, n - FIND_COMPACT_HITS);
|
|
705
|
+
for (let i = start; i < start + FIND_COMPACT_HITS; i++) out.push(i);
|
|
706
|
+
return out;
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
function findSetExpand(on) {
|
|
710
|
+
find.expand = on;
|
|
711
|
+
try { localStorage.setItem(FIND_EXPAND_KEY, on ? '1' : '0'); } catch { /* 忽略 */ }
|
|
712
|
+
find.listStale = true;
|
|
713
|
+
renderFindList();
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
/** 重建结果列表(只在查询/模式变化时做;跳转只切 .cur 类,不重建 DOM) */
|
|
717
|
+
function renderFindList() {
|
|
718
|
+
const list = $('find-list');
|
|
719
|
+
const meta = $('find-meta');
|
|
720
|
+
const head = $('find-headcount');
|
|
721
|
+
if (!list || !meta || !head) return;
|
|
722
|
+
meta.textContent = findMetaText();
|
|
723
|
+
head.textContent = !find.q || !find.needle ? ''
|
|
724
|
+
: !find.hits.length ? '无匹配'
|
|
725
|
+
: find.idx < 0 ? `${find.hits.length} 处` : `${find.idx + 1}/${find.hits.length}`;
|
|
726
|
+
const expBtn = $('find-expand');
|
|
727
|
+
if (expBtn) {
|
|
728
|
+
expBtn.style.display = find.hits.length > FIND_COMPACT_HITS ? '' : 'none';
|
|
729
|
+
// 按钮写的是「点了会变成什么」:精简时显示「全部」,全部时显示「精简」
|
|
730
|
+
expBtn.textContent = find.expand ? '精简' : '全部';
|
|
731
|
+
expBtn.classList.toggle('on', find.expand);
|
|
732
|
+
expBtn.title = find.expand ? `当前列出全部 ${find.hits.length} 处,点击收成精简` : `当前只列 ${FIND_COMPACT_HITS} 处,点击展开全部 ${find.hits.length} 处`;
|
|
733
|
+
}
|
|
734
|
+
if (!find.listStale && list.childElementCount) { syncFindCurrent(); return; }
|
|
735
|
+
find.listStale = false;
|
|
736
|
+
list.innerHTML = '';
|
|
737
|
+
if (!find.hits.length) return;
|
|
738
|
+
const frag = document.createDocumentFragment();
|
|
739
|
+
const shown = findVisibleIdx();
|
|
740
|
+
let lastPage = 0;
|
|
741
|
+
for (const i of shown) {
|
|
742
|
+
const hit = find.hits[i];
|
|
743
|
+
if (hit.page !== lastPage) {
|
|
744
|
+
const ph = document.createElement('div');
|
|
745
|
+
ph.className = 'find-page';
|
|
746
|
+
ph.textContent = `第 ${hit.page} 页`;
|
|
747
|
+
frag.appendChild(ph);
|
|
748
|
+
lastPage = hit.page;
|
|
749
|
+
}
|
|
750
|
+
const s = hit.snippet;
|
|
751
|
+
const btn = document.createElement('button');
|
|
752
|
+
btn.type = 'button';
|
|
753
|
+
btn.className = 'find-item';
|
|
754
|
+
btn.dataset.i = String(i);
|
|
755
|
+
if (s.pre) btn.appendChild(document.createTextNode('…'));
|
|
756
|
+
btn.appendChild(document.createTextNode(s.text.slice(0, s.hitStart)));
|
|
757
|
+
const mk = document.createElement('mark');
|
|
758
|
+
mk.textContent = s.text.slice(s.hitStart, s.hitEnd);
|
|
759
|
+
btn.appendChild(mk);
|
|
760
|
+
btn.appendChild(document.createTextNode(s.text.slice(s.hitEnd)));
|
|
761
|
+
if (s.post) btn.appendChild(document.createTextNode('…'));
|
|
762
|
+
btn.addEventListener('click', () => { void findGoto(i); });
|
|
763
|
+
frag.appendChild(btn);
|
|
764
|
+
}
|
|
765
|
+
if (shown.length < find.hits.length) {
|
|
766
|
+
const more = document.createElement('button');
|
|
767
|
+
more.type = 'button';
|
|
768
|
+
more.className = 'find-item';
|
|
769
|
+
more.style.textAlign = 'center';
|
|
770
|
+
more.style.color = 'var(--text-2)';
|
|
771
|
+
more.textContent = `还有 ${find.hits.length - shown.length} 处没有列出 · 点这里展开全部`;
|
|
772
|
+
more.addEventListener('click', () => findSetExpand(true));
|
|
773
|
+
frag.appendChild(more);
|
|
774
|
+
}
|
|
775
|
+
list.appendChild(frag);
|
|
776
|
+
syncFindCurrent();
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
function syncFindCurrent() {
|
|
780
|
+
const list = $('find-list');
|
|
781
|
+
if (!list) return;
|
|
782
|
+
let cur = null;
|
|
783
|
+
list.querySelectorAll('.find-item').forEach((el) => {
|
|
784
|
+
const on = Number(el.dataset.i) === find.idx;
|
|
785
|
+
el.classList.toggle('cur', on);
|
|
786
|
+
if (on) cur = el;
|
|
787
|
+
});
|
|
788
|
+
if (cur) cur.scrollIntoView({ block: 'nearest' });
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
function findClearBoxes() {
|
|
792
|
+
document.querySelectorAll('.findLayer').forEach((l) => l.remove());
|
|
793
|
+
document.querySelectorAll('.textLayer span.find-hit').forEach((sp) => sp.classList.remove('find-hit', 'cur'));
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
/** 一页 DOM 的「拼接串 + 归一化映射 + 偏移→(文本节点, offset) 定位器」 */
|
|
797
|
+
function findPageHay(pageDiv) {
|
|
798
|
+
const tl = pageDiv.querySelector('.textLayer');
|
|
799
|
+
if (!tl) return null;
|
|
800
|
+
// 用**文本节点**而不是 span:Range.setStart(node, n) 里的 n 对元素节点是「子节点序号」,
|
|
801
|
+
// 对文本节点才是字符偏移。之前把字符偏移直接喂给 span,Range 要么抛错、要么落在行首,
|
|
802
|
+
// getClientRects 拿不到有效矩形 → 这就是「搜到了却没有标黄」的根因。
|
|
803
|
+
const nodes = [];
|
|
804
|
+
const startOf = [];
|
|
805
|
+
let hay = '';
|
|
806
|
+
const walker = document.createTreeWalker(tl, NodeFilter.SHOW_TEXT);
|
|
807
|
+
for (let n = walker.nextNode(); n; n = walker.nextNode()) {
|
|
808
|
+
const t = n.nodeValue || '';
|
|
809
|
+
if (!t) continue;
|
|
810
|
+
nodes.push(n);
|
|
811
|
+
startOf.push(hay.length);
|
|
812
|
+
hay += t;
|
|
813
|
+
}
|
|
814
|
+
if (!hay) return null;
|
|
815
|
+
const { nh, map } = findNorm(hay);
|
|
816
|
+
if (!nh) return null;
|
|
817
|
+
const at = (off) => {
|
|
818
|
+
let lo = 0;
|
|
819
|
+
let hi = startOf.length - 1;
|
|
820
|
+
let idx = 0;
|
|
821
|
+
while (lo <= hi) {
|
|
822
|
+
const mid = (lo + hi) >> 1;
|
|
823
|
+
if (startOf[mid] <= off) { idx = mid; lo = mid + 1; } else { hi = mid - 1; }
|
|
824
|
+
}
|
|
825
|
+
return [nodes[idx], Math.min(off - startOf[idx], nodes[idx].nodeValue.length)];
|
|
826
|
+
};
|
|
827
|
+
return { nh, map, at };
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
/** 兜底高亮:Range 几何算不出来(跨节点怪例、TextLayer 结构变化)时给 span 加类。
|
|
831
|
+
* 宁可粗一点,也不能出现「搜到了却看不见」。 */
|
|
832
|
+
function findMarkSpansFallback(tl, ord) {
|
|
833
|
+
const spans = [...tl.querySelectorAll('span:not(.markedContent)')];
|
|
834
|
+
let seen = 0;
|
|
835
|
+
for (const sp of spans) {
|
|
836
|
+
const t = sp.textContent || '';
|
|
837
|
+
const { nh } = findNorm(t);
|
|
838
|
+
if (!nh || !nh.includes(find.needle)) continue;
|
|
839
|
+
sp.classList.add('find-hit');
|
|
840
|
+
if (seen === ord) sp.classList.add('cur');
|
|
841
|
+
seen += 1;
|
|
842
|
+
}
|
|
843
|
+
return seen > 0;
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
/** 在指定页画出该页全部命中(第 ord 处标为当前),返回当前盒子供滚动 */
|
|
847
|
+
function findMarkPage(page, ord) {
|
|
848
|
+
findClearBoxes();
|
|
849
|
+
const div = origPageNode(page);
|
|
850
|
+
const tl = div && div.querySelector('.textLayer');
|
|
851
|
+
if (!div || !tl || !find.needle) return null;
|
|
852
|
+
const hay = findPageHay(div);
|
|
853
|
+
if (!hay) return null;
|
|
854
|
+
const rect = tl.getBoundingClientRect();
|
|
855
|
+
if (!rect.width || !rect.height) return null;
|
|
856
|
+
const layer = document.createElement('div');
|
|
857
|
+
layer.className = 'findLayer';
|
|
858
|
+
// 必须插在 textLayer **之前**:paintPage 靠 div.firstChild/lastChild 取 canvas/textLayer,
|
|
859
|
+
// 追加在后面会让 lastChild 变成高亮层 → TextLayer 挂错容器(同 paintEqLayer 的坑)
|
|
860
|
+
div.insertBefore(layer, tl);
|
|
861
|
+
const percent = (v, base) => (v / base * 100).toFixed(3) + '%';
|
|
862
|
+
let cur = null;
|
|
863
|
+
let found = 0;
|
|
864
|
+
let from = 0;
|
|
865
|
+
for (;;) {
|
|
866
|
+
const at = hay.nh.indexOf(find.needle, from);
|
|
867
|
+
if (at < 0) break;
|
|
868
|
+
const start = hay.map[at];
|
|
869
|
+
const end = hay.map[at + find.needle.length - 1] + 1;
|
|
870
|
+
const [nodeA, offA] = hay.at(start);
|
|
871
|
+
const [nodeB, offB] = hay.at(end);
|
|
872
|
+
try {
|
|
873
|
+
const rng = document.createRange();
|
|
874
|
+
rng.setStart(nodeA, offA);
|
|
875
|
+
rng.setEnd(nodeB, offB);
|
|
876
|
+
for (const r of rng.getClientRects()) {
|
|
877
|
+
if (r.width < 0.6 || r.height < 0.6) continue; // 换行点产生的退化矩形跳过
|
|
878
|
+
const isCur = found === ord;
|
|
879
|
+
const box = document.createElement('span');
|
|
880
|
+
box.className = 'find-box' + (isCur ? ' cur' : '');
|
|
881
|
+
box.style.left = percent(r.left - rect.left, rect.width);
|
|
882
|
+
box.style.top = percent(r.top - rect.top, rect.height);
|
|
883
|
+
box.style.width = percent(r.width, rect.width);
|
|
884
|
+
box.style.height = percent(r.height, rect.height);
|
|
885
|
+
layer.appendChild(box);
|
|
886
|
+
if (isCur && !cur) cur = box;
|
|
887
|
+
}
|
|
888
|
+
} catch { /* 这一处几何算不出来:循环结束后统一兜底 */ }
|
|
889
|
+
found += 1;
|
|
890
|
+
from = at + Math.max(1, find.needle.length);
|
|
891
|
+
if (found > 600) break; // 单页防御
|
|
892
|
+
}
|
|
893
|
+
if (!layer.childElementCount) {
|
|
894
|
+
layer.remove(); // 空层留着也没用
|
|
895
|
+
findMarkSpansFallback(tl, ord); // 兜底:给命中的 span 上底色
|
|
896
|
+
return null;
|
|
897
|
+
}
|
|
898
|
+
return cur;
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
/** 等目标页真的出现(翻页/分屏模式要先切页再等 TextLayer 铺好) */
|
|
902
|
+
async function findReadyPage(page) {
|
|
903
|
+
if (!pdfDoc) return;
|
|
904
|
+
const p = Math.max(1, Math.min(page, pdfDoc.numPages));
|
|
905
|
+
if ((pageMode === 'paged' || viewMode === 'split') && curPage !== p) {
|
|
906
|
+
curPage = p;
|
|
907
|
+
await rerender();
|
|
908
|
+
updatePager();
|
|
909
|
+
}
|
|
910
|
+
for (let i = 0; i < 120; i++) {
|
|
911
|
+
const div = origPageNode(p);
|
|
912
|
+
if (div && div.querySelector('.textLayer span')) return;
|
|
913
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
async function findGoto(i) {
|
|
918
|
+
if (!find.hits.length) return;
|
|
919
|
+
const n = find.hits.length;
|
|
920
|
+
find.idx = ((i % n) + n) % n; // 首尾循环
|
|
921
|
+
const hit = find.hits[find.idx];
|
|
922
|
+
await findReadyPage(hit.page);
|
|
923
|
+
if (!find.hits.length) return;
|
|
924
|
+
const cur = findMarkPage(hit.page, hit.ord);
|
|
925
|
+
updateFindCount();
|
|
926
|
+
if (!find.listStale) syncFindCurrent();
|
|
927
|
+
const target = cur || origPageNode(hit.page);
|
|
928
|
+
if (target) {
|
|
929
|
+
await new Promise((r) => requestAnimationFrame(r));
|
|
930
|
+
target.scrollIntoView({ behavior: 'smooth', block: 'center' });
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
/** 新的查询:清空命中 → 建索引(有缓存则秒回)→ 收集 → 渲染列表 */
|
|
935
|
+
async function findRun(q) {
|
|
936
|
+
find.q = q;
|
|
937
|
+
find.needle = findNorm(q).nh;
|
|
938
|
+
find.hits = [];
|
|
939
|
+
find.idx = -1;
|
|
940
|
+
find.listStale = true;
|
|
941
|
+
findClearBoxes();
|
|
942
|
+
renderFindList();
|
|
943
|
+
if (!find.needle) { updateFindCount(); return; }
|
|
944
|
+
updateFindCount('索引…');
|
|
945
|
+
await findBuildIndex();
|
|
946
|
+
if (find.q !== q) return; // 期间输入又变了
|
|
947
|
+
findCollect();
|
|
948
|
+
renderFindList();
|
|
949
|
+
updateFindCount();
|
|
950
|
+
// 输入框就在窗口里,所以窗口一打开就一直开着;有结果时顺手把列表滚到当前命中
|
|
951
|
+
findWinShow(true);
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
/** 打开查找窗口(focus=true 时选中输入框内容,方便直接改写关键词) */
|
|
955
|
+
function findPanelOpen(focus) {
|
|
956
|
+
if (!findWinEl) return;
|
|
957
|
+
findWinShow(true);
|
|
958
|
+
if (find.idx >= 0) findReapply(); // 重新打开时把上次的当前命中重新标出来,不用再跳一次
|
|
959
|
+
if (focus) { findInputEl.focus(); findInputEl.select(); }
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
/** 关闭查找窗口:连页内高亮一起收掉(关键词留着,下次按 🔍 还能直接用) */
|
|
963
|
+
function findClose() {
|
|
964
|
+
findWinShow(false);
|
|
965
|
+
findClearBoxes();
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
/** 拖动窗口标题栏(指针事件:鼠标/触控板/触屏同一套) */
|
|
969
|
+
function findWinDrag(e) {
|
|
970
|
+
if (e.button !== 0 || e.target.closest('button')) return; // 头部按钮不触发拖动
|
|
971
|
+
e.preventDefault();
|
|
972
|
+
const start = { mx: e.clientX, my: e.clientY, x: findWinEl.offsetLeft, y: findWinEl.offsetTop };
|
|
973
|
+
const move = (ev) => {
|
|
974
|
+
findWin.x = start.x + (ev.clientX - start.mx);
|
|
975
|
+
findWin.y = start.y + (ev.clientY - start.my);
|
|
976
|
+
findWinPlace();
|
|
977
|
+
};
|
|
978
|
+
const up = () => {
|
|
979
|
+
window.removeEventListener('pointermove', move);
|
|
980
|
+
window.removeEventListener('pointerup', up);
|
|
981
|
+
window.removeEventListener('pointercancel', up);
|
|
982
|
+
findWinSave();
|
|
983
|
+
};
|
|
984
|
+
window.addEventListener('pointermove', move);
|
|
985
|
+
window.addEventListener('pointerup', up);
|
|
986
|
+
window.addEventListener('pointercancel', up);
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
/** 拖边缘/右下角改大小:dir = 'se'(角)| 'e'(右边缘)| 's'(下边缘) */
|
|
990
|
+
function findWinResize(e, dir) {
|
|
991
|
+
if (e.button !== 0) return;
|
|
992
|
+
e.preventDefault();
|
|
993
|
+
e.stopPropagation();
|
|
994
|
+
const start = { mx: e.clientX, my: e.clientY, w: findWinEl.offsetWidth, h: findWinEl.offsetHeight, x: findWinEl.offsetLeft, y: findWinEl.offsetTop };
|
|
995
|
+
const move = (ev) => {
|
|
996
|
+
if (dir !== 's') findWin.w = Math.max(220, Math.min(window.innerWidth - 16, start.w + (ev.clientX - start.mx)));
|
|
997
|
+
if (dir !== 'e') findWin.h = Math.max(150, Math.min(window.innerHeight - 24, start.h + (ev.clientY - start.my)));
|
|
998
|
+
findWin.x = start.x;
|
|
999
|
+
findWin.y = start.y;
|
|
1000
|
+
findWinPlace();
|
|
1001
|
+
};
|
|
1002
|
+
const up = () => {
|
|
1003
|
+
window.removeEventListener('pointermove', move);
|
|
1004
|
+
window.removeEventListener('pointerup', up);
|
|
1005
|
+
window.removeEventListener('pointercancel', up);
|
|
1006
|
+
findWinSave();
|
|
1007
|
+
};
|
|
1008
|
+
window.addEventListener('pointermove', move);
|
|
1009
|
+
window.addEventListener('pointerup', up);
|
|
1010
|
+
window.addEventListener('pointercancel', up);
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
/** 缩放/换模式/开关分屏后 DOM 被重建:把当前命中重新画上去 */
|
|
1014
|
+
function findReapply() {
|
|
1015
|
+
if (!find.shown || !find.hits.length) return;
|
|
1016
|
+
const hit = find.hits[find.idx];
|
|
1017
|
+
if (hit) findMarkPage(hit.page, hit.ord);
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
/** 换了文档(换论文/原文↔译文):索引与命中全部作废,输入框里的词留着 */
|
|
1021
|
+
function findReset() {
|
|
1022
|
+
find.index = null;
|
|
1023
|
+
find.doc = null;
|
|
1024
|
+
find.hits = [];
|
|
1025
|
+
find.idx = -1;
|
|
1026
|
+
find.needle = '';
|
|
1027
|
+
find.q = '';
|
|
1028
|
+
find.listStale = true;
|
|
1029
|
+
findClearBoxes();
|
|
1030
|
+
updateFindCount();
|
|
1031
|
+
renderFindList();
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1034
|
+
findInputEl.addEventListener('input', () => {
|
|
1035
|
+
clearTimeout(findTimer);
|
|
1036
|
+
const q = findInputEl.value.trim();
|
|
1037
|
+
// 边打边算(220ms 防抖)但不自动跳页:跳页留给 Enter/上下箭头,避免打字时页面乱跳
|
|
1038
|
+
findTimer = setTimeout(() => { void findRun(q); }, 220);
|
|
1039
|
+
});
|
|
1040
|
+
findInputEl.addEventListener('keydown', (e) => {
|
|
1041
|
+
if (e.key === 'Enter') {
|
|
1042
|
+
e.preventDefault();
|
|
1043
|
+
clearTimeout(findTimer);
|
|
1044
|
+
const q = findInputEl.value.trim();
|
|
1045
|
+
if (q !== find.q) { void findRun(q).then(() => findGoto(0)); return; }
|
|
1046
|
+
void findGoto(find.idx + (e.shiftKey ? -1 : 1));
|
|
1047
|
+
} else if (e.key === 'Escape') {
|
|
1048
|
+
e.preventDefault();
|
|
1049
|
+
findClose();
|
|
1050
|
+
}
|
|
1051
|
+
});
|
|
1052
|
+
// 🔍 只是「打开查找窗口」:窗口关着就开、开着就把焦点给输入框(关键词和结果都还在)
|
|
1053
|
+
$('find-open').onclick = () => {
|
|
1054
|
+
if (!pdfDoc) return;
|
|
1055
|
+
findPanelOpen(true);
|
|
1056
|
+
};
|
|
1057
|
+
$('find-prev').onclick = () => { void findGoto(find.idx - 1); };
|
|
1058
|
+
$('find-next').onclick = () => { void findGoto(find.idx + 1); };
|
|
1059
|
+
$('find-window-close').onclick = findClose;
|
|
1060
|
+
$('find-expand').onclick = () => findSetExpand(!find.expand);
|
|
1061
|
+
$('find-head').addEventListener('pointerdown', findWinDrag);
|
|
1062
|
+
$('find-grip').addEventListener('pointerdown', (e) => findWinResize(e, 'se'));
|
|
1063
|
+
$('find-grip-r').addEventListener('pointerdown', (e) => findWinResize(e, 'e'));
|
|
1064
|
+
$('find-grip-b').addEventListener('pointerdown', (e) => findWinResize(e, 's'));
|
|
1065
|
+
document.addEventListener('keydown', (e) => {
|
|
1066
|
+
if ((e.metaKey || e.ctrlKey) && !e.altKey && (e.key === 'f' || e.key === 'F') && pdfDoc) {
|
|
1067
|
+
e.preventDefault();
|
|
1068
|
+
findPanelOpen(true);
|
|
1069
|
+
}
|
|
1070
|
+
});
|
|
1071
|
+
window.addEventListener('resize', () => {
|
|
1072
|
+
if (findWinEl.classList.contains('on')) findWinPlace(); // 视口变小:把窗口夹回可见范围
|
|
1073
|
+
});
|
|
1074
|
+
findWinLoad();
|
|
1075
|
+
|
|
1076
|
+
/** 排查用:控制台执行 dprFindDebug() 能看到当前查找的内部状态(高亮不显示时先看 boxes 是否为 0) */
|
|
1077
|
+
window.dprFindDebug = () => {
|
|
1078
|
+
const cur = find.hits[find.idx];
|
|
1079
|
+
return {
|
|
1080
|
+
q: find.q,
|
|
1081
|
+
needle: find.needle,
|
|
1082
|
+
hits: find.hits.length,
|
|
1083
|
+
idx: find.idx,
|
|
1084
|
+
indexPages: find.index ? find.index.size : 0,
|
|
1085
|
+
currentHit: cur ? { page: cur.page, ord: cur.ord } : null,
|
|
1086
|
+
spansInDom: document.querySelectorAll('.textLayer span').length,
|
|
1087
|
+
boxes: document.querySelectorAll('.find-box').length,
|
|
1088
|
+
fallbackMarks: document.querySelectorAll('.textLayer span.find-hit').length,
|
|
1089
|
+
windowOpen: !!(findWinEl && findWinEl.classList.contains('on')),
|
|
1090
|
+
winPos: findWinEl ? { x: findWinEl.offsetLeft, y: findWinEl.offsetTop, w: findWinEl.offsetWidth, h: findWinEl.offsetHeight } : null,
|
|
1091
|
+
};
|
|
1092
|
+
};
|
|
1093
|
+
|
|
417
1094
|
/* ================= 中英切换 ================= */
|
|
1095
|
+
/** 翻译进度条(只在 busy 时出现):宽度跟着后端 progress.overall,文字给阶段+百分比。 */
|
|
1096
|
+
function updateZhProgress() { const box = $('zh-prog');
|
|
1097
|
+
const p = zhState && zhState.busy ? zhState.progress : null;
|
|
1098
|
+
if (!p) { box.hidden = true; return; }
|
|
1099
|
+
const pct = typeof p.overall === 'number' ? Math.max(0, Math.min(100, p.overall)) : null;
|
|
1100
|
+
box.hidden = false;
|
|
1101
|
+
box.querySelector('.fill').style.width = (pct === null ? 8 : Math.max(2, pct)).toFixed(1) + '%';
|
|
1102
|
+
box.querySelector('.txt').textContent = pct === null
|
|
1103
|
+
? `${p.label || '翻译'}…`
|
|
1104
|
+
: `${p.label || '翻译'} ${Math.round(pct)}%`;
|
|
1105
|
+
box.title = p.text || '';
|
|
1106
|
+
}
|
|
1107
|
+
// 别人启动的翻译(另开会话/刷新页面后进来)也要能看到进度:busy 就自动挂一个低成本轮询
|
|
1108
|
+
let zhWatch = null;
|
|
1109
|
+
let zhGenerating = false;
|
|
1110
|
+
function ensureZhWatch() {
|
|
1111
|
+
if (zhState.busy && !zhWatch && !zhGenerating) {
|
|
1112
|
+
zhWatch = setInterval(() => { void refreshZhState(); }, 1200);
|
|
1113
|
+
} else if (!zhState.busy && zhWatch) {
|
|
1114
|
+
clearInterval(zhWatch); zhWatch = null;
|
|
1115
|
+
}
|
|
1116
|
+
}
|
|
418
1117
|
async function refreshZhState() {
|
|
419
1118
|
zhState = await api(`/zh?topic=${encodeURIComponent(current.topic)}&name=${encodeURIComponent(current.name)}`);
|
|
420
1119
|
const btn = $('zh-toggle');
|
|
@@ -433,6 +1132,8 @@ async function refreshZhState() {
|
|
|
433
1132
|
delete statusEl.dataset.phaseShown;
|
|
434
1133
|
if (statusMeta) statusEl.textContent = statusMeta;
|
|
435
1134
|
}
|
|
1135
|
+
updateZhProgress();
|
|
1136
|
+
ensureZhWatch();
|
|
436
1137
|
// 重翻按钮只在「已有译文且空闲」时出现;状态变化时顺手拆掉两击确认的待确认态
|
|
437
1138
|
const regen = $('zh-regen');
|
|
438
1139
|
const canRegen = (zhState.zh || zhState.dual) && !zhState.busy;
|
|
@@ -557,10 +1258,17 @@ async function startGenerate(force) {
|
|
|
557
1258
|
}
|
|
558
1259
|
$('zh-toggle').disabled = true; $('zh-toggle').innerHTML = iconHtml('spin');
|
|
559
1260
|
$('zh-regen').style.display = 'none';
|
|
1261
|
+
// 先本地亮一个「准备中」的进度条,别等第一轮轮询才出现(首轮可能要几秒)
|
|
1262
|
+
zhState = { ...zhState, busy: true, progress: null };
|
|
1263
|
+
updateZhProgress();
|
|
1264
|
+
zhGenerating = true;
|
|
1265
|
+
// 翻译期间 1s 一轮(进度条要跟手);结束后自动停
|
|
560
1266
|
const timer = setInterval(async () => {
|
|
561
1267
|
await refreshZhState();
|
|
562
1268
|
if (!zhState.busy) {
|
|
563
1269
|
clearInterval(timer);
|
|
1270
|
+
zhGenerating = false;
|
|
1271
|
+
ensureZhWatch();
|
|
564
1272
|
if (zhState.dual || zhState.zh) {
|
|
565
1273
|
toast('中文版已生成');
|
|
566
1274
|
// 重翻完成时用户可能正停在译文视图/分屏:原地刷新,而不是来回切换
|
|
@@ -570,7 +1278,7 @@ async function startGenerate(force) {
|
|
|
570
1278
|
}
|
|
571
1279
|
else toast('生成失败:' + (zhState.error || '未知原因'), 8000);
|
|
572
1280
|
}
|
|
573
|
-
},
|
|
1281
|
+
}, 1000);
|
|
574
1282
|
}
|
|
575
1283
|
|
|
576
1284
|
/* ================= 重新翻译(重翻按钮,两击确认) ================= */
|
|
@@ -734,6 +1442,9 @@ let renderToken = 0;
|
|
|
734
1442
|
async function loadPdf(url, opts = {}) {
|
|
735
1443
|
await ensurePdfjs(); // 就绪前禁止触 pdfjsLib(defer 竞态,见文件头注释)
|
|
736
1444
|
pdfDoc = await pdfjsLib.getDocument({ url }).promise; // v6 起不接受字符串参数,必须传对象
|
|
1445
|
+
// 换了文档(换论文 / 原文↔译文):查找索引按文档对象缓存,这里必须作废,
|
|
1446
|
+
// 否则会拿旧文档的页码去定位新文档(页数不同时直接跳错页)。
|
|
1447
|
+
if (find.doc !== pdfDoc) findReset();
|
|
737
1448
|
$('empty').style.display = 'none';
|
|
738
1449
|
// 翻页控件与模式切换按钮:文档就绪后才出现
|
|
739
1450
|
$('pager').style.display = '';
|
|
@@ -799,8 +1510,10 @@ async function paintPage(page, div, viewport, token) {
|
|
|
799
1510
|
/** 当前模式的全量重渲入口:缩放/DPR/换文档/换变体统一走这里。
|
|
800
1511
|
* 分屏(模式 B)是第三套布局,转到 renderSplit();renderPaged/renderAllPages 的既有语义**不变**。 */
|
|
801
1512
|
function rerender() {
|
|
802
|
-
|
|
803
|
-
|
|
1513
|
+
// 渲染完成后再补画查找高亮:缩放/切模式把 DOM 整个重建,旧的高亮层随之消失。
|
|
1514
|
+
const done = () => { try { findReapply(); } catch { /* 高亮是装饰,失败不影响阅读 */ } };
|
|
1515
|
+
if (viewMode === 'split') return renderSplit().then(done);
|
|
1516
|
+
return (pageMode === 'paged' ? renderPaged() : renderAllPages()).then(done);
|
|
804
1517
|
}
|
|
805
1518
|
|
|
806
1519
|
// 隐藏期间被跳过的重渲请求:尺寸恢复(resize)后补跑。
|
|
@@ -1757,7 +2470,14 @@ function buildChunks(chars, itemsByPage) {
|
|
|
1757
2470
|
if (room <= 0) break;
|
|
1758
2471
|
let s = payload;
|
|
1759
2472
|
const cap = c.kind === 'text' ? Math.min(MAX_SELECTION_CHARS, room) : room;
|
|
1760
|
-
if (s.length > cap)
|
|
2473
|
+
if (s.length > cap) {
|
|
2474
|
+
// 截断回退到码点边界:slice 按 UTF-16 码元计数,切开代理对(数学斜体 𝑋 等)会让
|
|
2475
|
+
// 翻译请求带孤立代理项、上游整轮 400(issue #9 同类)。宁可短 1 个码元。
|
|
2476
|
+
let cut = cap;
|
|
2477
|
+
const before = s.charCodeAt(cut - 1);
|
|
2478
|
+
if (before >= 0xd800 && before <= 0xdbff && cut > 1) cut -= 1;
|
|
2479
|
+
s = s.slice(0, cut);
|
|
2480
|
+
}
|
|
1761
2481
|
if (!s) continue;
|
|
1762
2482
|
total += s.length;
|
|
1763
2483
|
capped.push(c.kind === 'text' ? { page: c.page, kind: 'text', text: s } : { page: c.page, kind: 'formula', latex: s });
|