@ggboy123/dsh-paper-reader 1.6.1 → 1.7.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 -4
- package/dist/host.js +50 -1
- package/dist/library-manage.js +9 -3
- package/dist/notes.js +184 -0
- package/dist/opencode.js +123 -0
- package/dist/translate-config.js +8 -1
- package/dist/translate-selection.js +6 -0
- package/dist/translate.js +7 -1
- package/lib/client.js +2 -2
- package/package.json +1 -1
- package/reader/index.html +662 -6
- package/runtime/babeldoc-driver.py +5 -0
package/reader/index.html
CHANGED
|
@@ -190,6 +190,55 @@
|
|
|
190
190
|
.textLayer span.find-hit { background: rgba(255,214,0,.45); border-radius: 2px; }
|
|
191
191
|
.textLayer span.find-hit.cur { background: rgba(255,146,0,.55); }
|
|
192
192
|
|
|
193
|
+
/* ---------- 笔记(划词标注 + 可移动笔记窗口) ----------
|
|
194
|
+
标注用「百分比盒子 + 底部波浪底图」而不是给 span 加 text-decoration:一个选区常跨多个 span,
|
|
195
|
+
加类会把整行都画上波浪线(与查找高亮同一条教训)。盒子层 pointer-events:none,
|
|
196
|
+
悬停/点击靠坐标命中测试,不会挡住划词选字。 */
|
|
197
|
+
.noteLayer { position: absolute; inset: 0; pointer-events: none; z-index: 4; }
|
|
198
|
+
.note-under { position: absolute; border-radius: 2px; pointer-events: none;
|
|
199
|
+
background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='9' height='4'%3E%3Cpath d='M0 3 Q2.25 .6 4.5 3 T9 3' fill='none' stroke='%232f6bff' stroke-width='1.1'/%3E%3C/svg%3E");
|
|
200
|
+
background-repeat: repeat-x; background-position: 0 100%; background-size: 9px 4px; }
|
|
201
|
+
.note-under.flash { background-color: rgba(47,107,255,.20); }
|
|
202
|
+
#note-tip { position: fixed; z-index: 58; display: none; max-width: 320px; background: var(--panel);
|
|
203
|
+
border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 6px 22px rgba(0,0,0,.18);
|
|
204
|
+
padding: 8px 10px; font-size: 12px; line-height: 1.55; white-space: pre-wrap; word-break: break-word; pointer-events: none; }
|
|
205
|
+
#note-tip .tip-hint { display: block; margin-top: 4px; color: var(--text-2); font-size: 11px; }
|
|
206
|
+
/* 气泡里的「笔记 / 全局搜索」:与「译」并列的小动作按钮,不改变原提问链路 */
|
|
207
|
+
#ask-note, #ask-find { background: none; border: 1px solid var(--border); border-radius: 6px; color: var(--text-2); cursor: pointer; }
|
|
208
|
+
/* 笔记窗口:与查找窗同一套(可拖动 / 拖边框缩放 / 位置记忆)。列表与编辑器切换显示。 */
|
|
209
|
+
#note-window { position: fixed; z-index: 46; display: none; flex-direction: column; width: 330px; height: 360px;
|
|
210
|
+
min-width: 260px; min-height: 200px; background: var(--panel); border: 1px solid var(--border);
|
|
211
|
+
border-radius: 10px; box-shadow: 0 10px 30px rgba(0,0,0,.18); overflow: hidden; }
|
|
212
|
+
#note-window.on { display: flex; }
|
|
213
|
+
#note-head { display: flex; align-items: center; gap: 6px; padding: 6px 8px; cursor: move; user-select: none;
|
|
214
|
+
border-bottom: 1px solid var(--border); font-size: 12.5px; font-weight: 600; flex: none; }
|
|
215
|
+
#note-head .spacer, #note-actions .spacer { flex: 1; }
|
|
216
|
+
#note-headcount { font-weight: 400; color: var(--text-2); font-variant-numeric: tabular-nums; }
|
|
217
|
+
#note-list { flex: 1; overflow: auto; padding: 4px 8px 10px; }
|
|
218
|
+
.note-item { display: block; width: 100%; text-align: left; border: 1px solid var(--border); border-radius: 6px;
|
|
219
|
+
background: var(--bg); color: var(--text); padding: 6px 8px; margin: 5px 0; cursor: pointer; font: inherit; font-size: 12px; line-height: 1.5; }
|
|
220
|
+
.note-item:hover { border-color: var(--accent); }
|
|
221
|
+
.note-item .ni-meta { display: block; color: var(--accent); font-size: 11px; margin-bottom: 2px; }
|
|
222
|
+
.note-item .ni-meta .ni-time { float: right; color: var(--text-2); font-weight: 400; }
|
|
223
|
+
.note-item .ni-body { display: block; white-space: pre-wrap; word-break: break-word; }
|
|
224
|
+
.note-item .ni-quote { display: block; color: var(--text-2); font-size: 11px; margin-top: 3px;
|
|
225
|
+
overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
226
|
+
#note-empty { padding: 14px 6px; color: var(--text-2); font-size: 12px; line-height: 1.6; }
|
|
227
|
+
#note-editor { display: none; flex-direction: column; flex: 1; min-height: 0; padding: 6px 8px 8px; }
|
|
228
|
+
#note-window.editing #note-list, #note-window.editing #note-empty { display: none; }
|
|
229
|
+
#note-window.editing #note-editor { display: flex; }
|
|
230
|
+
#note-quote { flex: none; max-height: 72px; overflow: auto; font-size: 11.5px; color: var(--text-2);
|
|
231
|
+
border-left: 3px solid var(--accent-soft); padding-left: 8px; margin-bottom: 6px; }
|
|
232
|
+
#note-body { flex: 1; min-height: 80px; width: 100%; resize: none; border: 1px solid var(--border); border-radius: 6px;
|
|
233
|
+
padding: 6px 8px; font: inherit; font-size: 12.5px; line-height: 1.6; background: var(--panel); color: var(--text); }
|
|
234
|
+
#note-actions { display: flex; align-items: center; gap: 6px; margin-top: 6px; flex: none; }
|
|
235
|
+
.tbtn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
|
|
236
|
+
#note-grip { position: absolute; right: 0; bottom: 0; width: 16px; height: 16px; cursor: nwse-resize; z-index: 2; }
|
|
237
|
+
#note-grip::after { content: ''; position: absolute; right: 3px; bottom: 3px; width: 8px; height: 8px;
|
|
238
|
+
border-right: 2px solid var(--text-2); border-bottom: 2px solid var(--text-2); border-bottom-right-radius: 3px; opacity: .55; }
|
|
239
|
+
#note-grip-r { position: absolute; top: 34px; right: 0; bottom: 16px; width: 6px; cursor: ew-resize; z-index: 2; }
|
|
240
|
+
#note-grip-b { position: absolute; left: 0; right: 16px; bottom: 0; height: 6px; cursor: ns-resize; z-index: 2; }
|
|
241
|
+
|
|
193
242
|
/* ---------- 选中即问浮层 ---------- */
|
|
194
243
|
#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; }
|
|
195
244
|
#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; }
|
|
@@ -254,6 +303,8 @@
|
|
|
254
303
|
<button id="dl-btn" class="tbtn" title="下载 PDF" style="display:none" data-icon="download"></button>
|
|
255
304
|
<!-- 在文中查找(Ctrl+F):入口紧挨下载,实际输入条是浮层,不占顶栏宽度 -->
|
|
256
305
|
<button id="find-open" class="tbtn" title="在文中查找(Ctrl+F)" style="display:none" data-icon="search"></button>
|
|
306
|
+
<!-- 笔记:入口紧挨搜索;点开是「笔记窗口」(列全部笔记 / 新建 / 编辑) -->
|
|
307
|
+
<button id="note-open" class="tbtn" title="笔记" style="display:none" data-icon="note"></button>
|
|
257
308
|
<span class="spacer"></span>
|
|
258
309
|
<button id="mode-toggle" class="tbtn" title="切换 滚动/翻页模式" style="display:none" data-icon="paged"></button>
|
|
259
310
|
<span id="pager" style="display:none">
|
|
@@ -310,6 +361,32 @@
|
|
|
310
361
|
<div id="find-grip-b" title="拖动改变高度"></div>
|
|
311
362
|
<div id="find-grip" title="拖动调整大小"></div>
|
|
312
363
|
</div>
|
|
364
|
+
|
|
365
|
+
<!-- 笔记窗口:与查找窗同一套交互(标题栏拖动 / 三边缩放 / 位置尺寸记忆)。
|
|
366
|
+
列表态显示全部笔记,编辑态换成「引用 + 正文 + 保存/删除」。 -->
|
|
367
|
+
<div id="note-window" role="dialog" aria-label="笔记">
|
|
368
|
+
<div id="note-head">
|
|
369
|
+
<span>笔记</span>
|
|
370
|
+
<span id="note-headcount"></span>
|
|
371
|
+
<span class="spacer"></span>
|
|
372
|
+
<button id="note-new" class="tbtn mini" title="把当前选中的文字做成新笔记">+</button>
|
|
373
|
+
<button id="note-window-close" class="tbtn mini" title="关闭(Esc)">✕</button>
|
|
374
|
+
</div>
|
|
375
|
+
<div id="note-list"></div>
|
|
376
|
+
<div id="note-editor">
|
|
377
|
+
<div id="note-quote"></div>
|
|
378
|
+
<textarea id="note-body" placeholder="写下你的理解、疑问、和别处的联系…(⌘/Ctrl+Enter 保存)"></textarea>
|
|
379
|
+
<div id="note-actions">
|
|
380
|
+
<button id="note-delete" class="tbtn mini" title="删除这条笔记">删除</button>
|
|
381
|
+
<span class="spacer"></span>
|
|
382
|
+
<button id="note-cancel" class="tbtn mini">取消</button>
|
|
383
|
+
<button id="note-save" class="tbtn mini primary">保存</button>
|
|
384
|
+
</div>
|
|
385
|
+
</div>
|
|
386
|
+
<div id="note-grip-r" title="拖动改变宽度"></div>
|
|
387
|
+
<div id="note-grip-b" title="拖动改变高度"></div>
|
|
388
|
+
<div id="note-grip" title="拖动调整大小"></div>
|
|
389
|
+
</div>
|
|
313
390
|
</div>
|
|
314
391
|
|
|
315
392
|
<div id="ask-pop">
|
|
@@ -318,6 +395,8 @@
|
|
|
318
395
|
<div id="ask-actions">
|
|
319
396
|
<button id="ask-copy" style="display:none">复制 LaTeX</button>
|
|
320
397
|
<button id="ask-translate" style="display:none" title="翻译选中内容(含公式时用 MinerU 的 LaTeX 还原)">译</button>
|
|
398
|
+
<button id="ask-note" style="display:none" title="给这段文字加一条笔记">笔记</button>
|
|
399
|
+
<button id="ask-find" style="display:none" title="在全文里查找这段文字">全局搜索</button>
|
|
321
400
|
<button id="ask-cancel">取消</button>
|
|
322
401
|
<button id="ask-send">提问</button>
|
|
323
402
|
</div>
|
|
@@ -332,7 +411,7 @@
|
|
|
332
411
|
|
|
333
412
|
<div id="cfg-pop">
|
|
334
413
|
<h3>翻译端点</h3>
|
|
335
|
-
<div id="cfg-hint">选预设供应商,或选「自定义」填任意 OpenAI 兼容端点(通义 / 本地 vLLM
|
|
414
|
+
<div id="cfg-hint">选预设供应商,或选「自定义」填任意 OpenAI 兼容端点(通义 / 本地 vLLM 等)。OpenCode Go 需先在 OpenCode Console 订阅并复制 API Key。保存前会先测试连接。</div>
|
|
336
415
|
<div id="cfg-source"></div>
|
|
337
416
|
<label for="cfg-provider">供应商</label>
|
|
338
417
|
<select id="cfg-provider"></select>
|
|
@@ -350,6 +429,9 @@
|
|
|
350
429
|
</div>
|
|
351
430
|
</div>
|
|
352
431
|
|
|
432
|
+
<!-- 笔记悬停提示:鼠标停在被标注文字上显示笔记内容 -->
|
|
433
|
+
<div id="note-tip"></div>
|
|
434
|
+
|
|
353
435
|
<div id="toast"></div>
|
|
354
436
|
|
|
355
437
|
<script>
|
|
@@ -377,10 +459,12 @@ const ICONS = {
|
|
|
377
459
|
spin: '<path d="M21 12a9 9 0 1 1-6.219-8.56"/>',
|
|
378
460
|
search: '<circle cx="11" cy="11" r="7"/><path d="m20.5 20.5-3.9-3.9"/>',
|
|
379
461
|
close: '<path d="M18 6 6 18"/><path d="m6 6 12 12"/>',
|
|
462
|
+
note: '<path d="M12 20h9"/><path d="M16.5 3.5a2.12 2.12 0 0 1 3 3L7 19l-4 1 1-4Z"/>',
|
|
463
|
+
ask: '<path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/>',
|
|
380
464
|
};
|
|
381
465
|
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>`;
|
|
382
466
|
// 静态按钮按 data-icon 注入一次;动态按钮(翻译/重翻/模式切换)在状态变化时用同一函数换图标
|
|
383
|
-
document.querySelectorAll('#bar [data-icon], #find-window [data-icon]').forEach((b) => b.insertAdjacentHTML('afterbegin', iconHtml(b.dataset.icon)));
|
|
467
|
+
document.querySelectorAll('#bar [data-icon], #find-window [data-icon], #note-window [data-icon]').forEach((b) => b.insertAdjacentHTML('afterbegin', iconHtml(b.dataset.icon)));
|
|
384
468
|
const api = (p, opts) => fetch('/paper-reader/api' + p, opts).then(async (r) => {
|
|
385
469
|
if (!r.ok) {
|
|
386
470
|
// 服务端错误体有两种:{error}(参数/权限)与 {reason}(zh/generate 的未启动原因,带完整说明)
|
|
@@ -461,6 +545,9 @@ async function openPaper(topic, name, opts = {}) {
|
|
|
461
545
|
$('zh-toggle').classList.toggle('active', zhVariant !== null);
|
|
462
546
|
// 译文版页宽与原文不同(对照双栏≈2×),分屏每列只有一半宽 ⇒ 都先进适宽
|
|
463
547
|
if (zhVariant || viewMode === 'split') { fitMode = true; fitBtn.classList.add('active'); }
|
|
548
|
+
// 笔记与公式索引都必须在首次 paintPage 之前到货:paintPage 直接按 notes.list 铺波浪线。
|
|
549
|
+
// 两者互不依赖,并行拉取;笔记读失败不阻塞 PDF(loadNotes 内部吞错并保留空列表)。
|
|
550
|
+
const notesReady = loadNotes().catch(() => { /* 读失败:本页只是没有波浪线,不影响读论文 */ });
|
|
464
551
|
// 公式索引(MinerU content_list):**必须在 loadPdf 之前到货**——热区是在 paintPage 里
|
|
465
552
|
// 按 eqIndex 铺的,先渲染会漏画/残留上一篇的矩形;又必须排在 zhVariant 定稿之后——
|
|
466
553
|
// eqHotEnabled()/renderEqPanel() 都按 zhVariant 决定「译文视图关热区」。失败不影响打开文献。
|
|
@@ -468,12 +555,13 @@ async function openPaper(topic, name, opts = {}) {
|
|
|
468
555
|
updateDlBtn();
|
|
469
556
|
updateViewButtons();
|
|
470
557
|
if (viewMode === 'split') {
|
|
558
|
+
await notesReady; // 分屏两列由 openSplit 安排渲染,这里只需保证笔记在首帧前就绪
|
|
471
559
|
await openSplit(); // 内部自己拉原文 + -zh.pdf 两个文档
|
|
472
560
|
} else {
|
|
473
561
|
// R-02:原地换文献时**无条件**退掉分屏(幂等)——否则新论文有 -zh.pdf、而持久化视图又不是
|
|
474
562
|
// split 时,refreshZhState 的回落守卫不触发,中文列会留着**上一篇**的 PDF(pdfDocZh 也不释放)。
|
|
475
563
|
teardownSplit();
|
|
476
|
-
await loadPdf(pdfUrl(zhVariant));
|
|
564
|
+
await loadPdf(pdfUrl(zhVariant), { beforePaint: notesReady });
|
|
477
565
|
}
|
|
478
566
|
if (opts.page) jumpTo(opts.page, opts.q); // 不 await:跳转内部自带「等页面渲出来」轮询
|
|
479
567
|
}
|
|
@@ -483,13 +571,16 @@ async function openPaper(topic, name, opts = {}) {
|
|
|
483
571
|
function updateDlBtn() {
|
|
484
572
|
const btn = $('dl-btn');
|
|
485
573
|
const findBtn = $('find-open');
|
|
574
|
+
const noteBtn = $('note-open');
|
|
486
575
|
if (!current) {
|
|
487
576
|
btn.style.display = 'none';
|
|
488
577
|
if (findBtn) findBtn.style.display = 'none';
|
|
578
|
+
if (noteBtn) noteBtn.style.display = 'none';
|
|
489
579
|
return;
|
|
490
580
|
}
|
|
491
581
|
btn.style.display = '';
|
|
492
582
|
if (findBtn) findBtn.style.display = '';
|
|
583
|
+
if (noteBtn) noteBtn.style.display = '';
|
|
493
584
|
btn.title = zhVariant === 'zh' ? '下载:纯中文版 PDF'
|
|
494
585
|
: zhVariant === 'dual' ? '下载:中英对照版 PDF' : '下载:原文 PDF';
|
|
495
586
|
}
|
|
@@ -1091,6 +1182,548 @@ window.dprFindDebug = () => {
|
|
|
1091
1182
|
};
|
|
1092
1183
|
};
|
|
1093
1184
|
|
|
1185
|
+
/* ================= 笔记(划词标注 + 可移动笔记窗口)=================
|
|
1186
|
+
存储:服务端 `<论文>.notes.json`(与 `.study.json` 同目录 ⇒ 跟文献走、换浏览器不丢、将来也能被伴读读到)。
|
|
1187
|
+
锚点:页号 + 「该页 DOM 文本层拼接串」里的字符区间。文本层的内容只由 PDF 决定(与缩放/模式无关),
|
|
1188
|
+
所以偏移在重渲染后仍然有效 → 每次渲染完成按偏移重建 Range → 百分比盒子(蓝色波浪线)。
|
|
1189
|
+
为什么不用给 span 加 text-decoration:一个选区常跨多个 span,加类会把整行都画上波浪线。
|
|
1190
|
+
盒子层 pointer-events:none,悬停/点击靠坐标命中测试 —— 绝不挡住划词选字。 */
|
|
1191
|
+
const NOTE_WIN_KEY = 'dpr.noteWin';
|
|
1192
|
+
const NOTE_BODY_MAX = 20000;
|
|
1193
|
+
const notes = { list: [], shown: false, editing: null, isNew: true, armed: false, loading: false };
|
|
1194
|
+
let noteWin = { x: null, y: null, w: 330, h: 360 };
|
|
1195
|
+
const noteWinEl = $('note-window');
|
|
1196
|
+
const noteTipEl = $('note-tip');
|
|
1197
|
+
let noteArmTimer = 0;
|
|
1198
|
+
let noteHoverRaf = 0;
|
|
1199
|
+
let noteHoverId = null;
|
|
1200
|
+
|
|
1201
|
+
/* ---------- 窗口几何:与查找窗同一套(位置尺寸记在 localStorage) ---------- */
|
|
1202
|
+
function noteWinSave() {
|
|
1203
|
+
try {
|
|
1204
|
+
const r = noteWinEl.getBoundingClientRect();
|
|
1205
|
+
localStorage.setItem(NOTE_WIN_KEY, JSON.stringify({
|
|
1206
|
+
x: Math.round(r.left), y: Math.round(r.top), w: Math.round(r.width), h: Math.round(r.height),
|
|
1207
|
+
}));
|
|
1208
|
+
} catch { /* 隐私模式记不住:下次回到默认位置 */ }
|
|
1209
|
+
}
|
|
1210
|
+
function noteWinLoad() {
|
|
1211
|
+
if (!noteWinEl) return;
|
|
1212
|
+
try {
|
|
1213
|
+
const raw = JSON.parse(localStorage.getItem(NOTE_WIN_KEY) || 'null');
|
|
1214
|
+
if (raw) {
|
|
1215
|
+
if (Number.isFinite(raw.w)) noteWin.w = Math.max(260, Math.min(720, raw.w));
|
|
1216
|
+
if (Number.isFinite(raw.h)) noteWin.h = Math.max(200, raw.h);
|
|
1217
|
+
if (Number.isFinite(raw.x) && Number.isFinite(raw.y)) { noteWin.x = raw.x; noteWin.y = raw.y; }
|
|
1218
|
+
}
|
|
1219
|
+
} catch { /* 坏 JSON:用默认值 */ }
|
|
1220
|
+
}
|
|
1221
|
+
function noteWinPlace() {
|
|
1222
|
+
if (!noteWinEl) return;
|
|
1223
|
+
const w = noteWin.w;
|
|
1224
|
+
const h = Math.min(noteWin.h, Math.max(200, window.innerHeight - 40));
|
|
1225
|
+
let x = noteWin.x;
|
|
1226
|
+
let y = noteWin.y;
|
|
1227
|
+
if (x === null || y === null) {
|
|
1228
|
+
const btn = $('note-open');
|
|
1229
|
+
const r = btn ? btn.getBoundingClientRect() : null;
|
|
1230
|
+
if (r && r.width) { x = r.right - w; y = r.bottom + 6; }
|
|
1231
|
+
else { x = window.innerWidth - w - 26; y = 64; }
|
|
1232
|
+
}
|
|
1233
|
+
x = Math.max(8, Math.min(x, window.innerWidth - w - 8));
|
|
1234
|
+
y = Math.max(8, Math.min(y, window.innerHeight - h - 8));
|
|
1235
|
+
noteWinEl.style.width = w + 'px';
|
|
1236
|
+
noteWinEl.style.height = h + 'px';
|
|
1237
|
+
noteWinEl.style.left = Math.round(x) + 'px';
|
|
1238
|
+
noteWinEl.style.top = Math.round(y) + 'px';
|
|
1239
|
+
noteWin.x = Math.round(x);
|
|
1240
|
+
noteWin.y = Math.round(y);
|
|
1241
|
+
}
|
|
1242
|
+
function noteWinDrag(e) {
|
|
1243
|
+
if (e.button !== 0 || e.target.closest('button')) return;
|
|
1244
|
+
e.preventDefault();
|
|
1245
|
+
const start = { mx: e.clientX, my: e.clientY, x: noteWinEl.offsetLeft, y: noteWinEl.offsetTop };
|
|
1246
|
+
const move = (ev) => {
|
|
1247
|
+
noteWin.x = start.x + (ev.clientX - start.mx);
|
|
1248
|
+
noteWin.y = start.y + (ev.clientY - start.my);
|
|
1249
|
+
noteWinPlace();
|
|
1250
|
+
};
|
|
1251
|
+
const up = () => {
|
|
1252
|
+
window.removeEventListener('pointermove', move);
|
|
1253
|
+
window.removeEventListener('pointerup', up);
|
|
1254
|
+
window.removeEventListener('pointercancel', up);
|
|
1255
|
+
noteWinSave();
|
|
1256
|
+
};
|
|
1257
|
+
window.addEventListener('pointermove', move);
|
|
1258
|
+
window.addEventListener('pointerup', up);
|
|
1259
|
+
window.addEventListener('pointercancel', up);
|
|
1260
|
+
}
|
|
1261
|
+
/** 拖边缘/右下角改大小:dir = 'se'(角)| 'e'(右边缘)| 's'(下边缘) */
|
|
1262
|
+
function noteWinResize(e, dir) {
|
|
1263
|
+
if (e.button !== 0) return;
|
|
1264
|
+
e.preventDefault();
|
|
1265
|
+
e.stopPropagation();
|
|
1266
|
+
const start = { mx: e.clientX, my: e.clientY, w: noteWinEl.offsetWidth, h: noteWinEl.offsetHeight, x: noteWinEl.offsetLeft, y: noteWinEl.offsetTop };
|
|
1267
|
+
const move = (ev) => {
|
|
1268
|
+
if (dir !== 's') noteWin.w = Math.max(260, Math.min(window.innerWidth - 16, start.w + (ev.clientX - start.mx)));
|
|
1269
|
+
if (dir !== 'e') noteWin.h = Math.max(200, Math.min(window.innerHeight - 24, start.h + (ev.clientY - start.my)));
|
|
1270
|
+
noteWin.x = start.x;
|
|
1271
|
+
noteWin.y = start.y;
|
|
1272
|
+
noteWinPlace();
|
|
1273
|
+
};
|
|
1274
|
+
const up = () => {
|
|
1275
|
+
window.removeEventListener('pointermove', move);
|
|
1276
|
+
window.removeEventListener('pointerup', up);
|
|
1277
|
+
window.removeEventListener('pointercancel', up);
|
|
1278
|
+
noteWinSave();
|
|
1279
|
+
};
|
|
1280
|
+
window.addEventListener('pointermove', move);
|
|
1281
|
+
window.addEventListener('pointerup', up);
|
|
1282
|
+
window.addEventListener('pointercancel', up);
|
|
1283
|
+
}
|
|
1284
|
+
|
|
1285
|
+
/* ---------- 读写(服务端 <论文>.notes.json) ---------- */
|
|
1286
|
+
function notesQs() {
|
|
1287
|
+
return '?topic=' + encodeURIComponent(current.topic) + '&name=' + encodeURIComponent(current.name);
|
|
1288
|
+
}
|
|
1289
|
+
async function loadNotes() {
|
|
1290
|
+
notes.list = [];
|
|
1291
|
+
notes.editing = null;
|
|
1292
|
+
notes.loading = true;
|
|
1293
|
+
if (noteWinEl) noteWinEl.classList.remove('editing');
|
|
1294
|
+
noteClearLayers();
|
|
1295
|
+
renderNoteList();
|
|
1296
|
+
if (!current) { notes.loading = false; return; }
|
|
1297
|
+
const want = current.topic + '/' + current.name;
|
|
1298
|
+
try {
|
|
1299
|
+
const r = await api('/notes' + notesQs());
|
|
1300
|
+
if (!current || current.topic + '/' + current.name !== want) return; // 期间换了文献:丢弃
|
|
1301
|
+
notes.list = Array.isArray(r.notes) ? r.notes : [];
|
|
1302
|
+
} catch { /* 笔记读不出来不影响读论文 */ }
|
|
1303
|
+
notes.loading = false;
|
|
1304
|
+
renderNoteList();
|
|
1305
|
+
notesReapply();
|
|
1306
|
+
}
|
|
1307
|
+
/** 写一条(save)或删一条(delete);服务端返回整份笔记列表,客户端整体替换 */
|
|
1308
|
+
async function notesPost(action, note) {
|
|
1309
|
+
const body = { topic: current.topic, name: current.name, action };
|
|
1310
|
+
if (action === 'delete') body.id = note.id;
|
|
1311
|
+
else body.note = note;
|
|
1312
|
+
const r = await api('/notes', {
|
|
1313
|
+
method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body),
|
|
1314
|
+
});
|
|
1315
|
+
notes.list = Array.isArray(r.notes) ? r.notes : notes.list;
|
|
1316
|
+
}
|
|
1317
|
+
|
|
1318
|
+
/* ---------- 锚点:选区 ⇄ 字符偏移 ---------- */
|
|
1319
|
+
/** 一页文本层的「拼接串 + 文本节点表」;at(off) 给 (文本节点, 节点内偏移),offsetOf(node,off) 反向换算。
|
|
1320
|
+
* 必须用文本节点:Range.setStart(元素, n) 里的 n 是子节点序号(查找高亮踩过这个坑)。 */
|
|
1321
|
+
function noteTextHay(pageDiv) {
|
|
1322
|
+
const tl = pageDiv && pageDiv.querySelector('.textLayer');
|
|
1323
|
+
if (!tl) return null;
|
|
1324
|
+
const nodes = [];
|
|
1325
|
+
const startOf = [];
|
|
1326
|
+
let hay = '';
|
|
1327
|
+
const walker = document.createTreeWalker(tl, NodeFilter.SHOW_TEXT);
|
|
1328
|
+
for (let n = walker.nextNode(); n; n = walker.nextNode()) {
|
|
1329
|
+
const t = n.nodeValue || '';
|
|
1330
|
+
if (!t) continue;
|
|
1331
|
+
nodes.push(n);
|
|
1332
|
+
startOf.push(hay.length);
|
|
1333
|
+
hay += t;
|
|
1334
|
+
}
|
|
1335
|
+
if (!hay) return null;
|
|
1336
|
+
const at = (off) => {
|
|
1337
|
+
let lo = 0;
|
|
1338
|
+
let hi = startOf.length - 1;
|
|
1339
|
+
let idx = 0;
|
|
1340
|
+
while (lo <= hi) {
|
|
1341
|
+
const mid = (lo + hi) >> 1;
|
|
1342
|
+
if (startOf[mid] <= off) { idx = mid; lo = mid + 1; } else { hi = mid - 1; }
|
|
1343
|
+
}
|
|
1344
|
+
const node = nodes[idx];
|
|
1345
|
+
return [node, Math.max(0, Math.min(off - startOf[idx], node.nodeValue.length))];
|
|
1346
|
+
};
|
|
1347
|
+
const offsetOf = (node, off) => {
|
|
1348
|
+
const i = nodes.indexOf(node);
|
|
1349
|
+
return i < 0 ? null : startOf[i] + off;
|
|
1350
|
+
};
|
|
1351
|
+
return { hay, at, offsetOf, length: hay.length };
|
|
1352
|
+
}
|
|
1353
|
+
/** 把当前选区换算成 `{page,start,end,doc}`;换算不出来(扫描件/跨页/结构异常)返回 null */
|
|
1354
|
+
function noteAnchorFromSelection() {
|
|
1355
|
+
const sel = askSelection;
|
|
1356
|
+
if (!sel || !sel.range || !current) return null;
|
|
1357
|
+
const node = sel.range.startContainer;
|
|
1358
|
+
const el = node && (node.nodeType === 3 ? node.parentElement : node);
|
|
1359
|
+
const pageEl = el && el.closest ? el.closest('.page') : null;
|
|
1360
|
+
if (!pageEl) return null;
|
|
1361
|
+
const page = +(pageEl.dataset.page || 0) || 0;
|
|
1362
|
+
if (!page) return null;
|
|
1363
|
+
const hay = noteTextHay(pageEl);
|
|
1364
|
+
if (!hay) return null;
|
|
1365
|
+
const start = hay.offsetOf(sel.range.startContainer, sel.range.startOffset);
|
|
1366
|
+
const end = hay.offsetOf(sel.range.endContainer, sel.range.endOffset);
|
|
1367
|
+
if (start === null || end === null) return null;
|
|
1368
|
+
const a = Math.min(start, end);
|
|
1369
|
+
const b = Math.max(start, end);
|
|
1370
|
+
if (b - a < 1) return null;
|
|
1371
|
+
// 分屏时在中文列划的词,锚点算的是译文文本层的偏移 → 记下是哪一列,重画时只画回同一列
|
|
1372
|
+
const doc = pageEl.closest('#pdf-pages-zh') ? 'zh' : 'orig';
|
|
1373
|
+
return { page, start: a, end: b, doc, quote: sel.text.slice(0, 400) };
|
|
1374
|
+
}
|
|
1375
|
+
|
|
1376
|
+
/* ---------- 画波浪线(蓝色,选中文字下方) ---------- */
|
|
1377
|
+
function noteClearLayers() {
|
|
1378
|
+
document.querySelectorAll('.noteLayer').forEach((l) => l.remove());
|
|
1379
|
+
noteHideTip();
|
|
1380
|
+
}
|
|
1381
|
+
function notePaintPageDiv(div) {
|
|
1382
|
+
const tl = div && div.querySelector('.textLayer');
|
|
1383
|
+
const page = +(div && div.dataset && div.dataset.page) || 0;
|
|
1384
|
+
if (!tl || !page) return;
|
|
1385
|
+
const doc = div.closest && div.closest('#pdf-pages-zh') ? 'zh' : 'orig';
|
|
1386
|
+
const list = notes.list.filter((n) => n.anchor && n.anchor.page === page
|
|
1387
|
+
&& n.anchor.end > n.anchor.start && (n.anchor.doc || 'orig') === doc);
|
|
1388
|
+
if (!list.length) return;
|
|
1389
|
+
const hay = noteTextHay(div);
|
|
1390
|
+
const rect = tl.getBoundingClientRect();
|
|
1391
|
+
if (!hay || !rect.width || !rect.height) return;
|
|
1392
|
+
// 同一页可能被重画多次(逐页渲染 + 收尾重放):先清掉这一页旧的装饰层,避免叠出两层波浪线
|
|
1393
|
+
const stale = div.querySelector(':scope > .noteLayer');
|
|
1394
|
+
if (stale) stale.remove();
|
|
1395
|
+
const pct = (v, base) => (v / base * 100).toFixed(3) + '%';
|
|
1396
|
+
const layer = document.createElement('div');
|
|
1397
|
+
layer.className = 'noteLayer';
|
|
1398
|
+
// 与 findLayer 同理:必须插在 textLayer **之前**,否则 paintPage 的 firstChild/lastChild 会取错元素
|
|
1399
|
+
div.insertBefore(layer, tl);
|
|
1400
|
+
for (const n of list) {
|
|
1401
|
+
const a = Math.max(0, Math.min(n.anchor.start, hay.length - 1));
|
|
1402
|
+
const b = Math.max(a + 1, Math.min(n.anchor.end, hay.length));
|
|
1403
|
+
const [na, oa] = hay.at(a);
|
|
1404
|
+
const [nb, ob] = hay.at(b);
|
|
1405
|
+
try {
|
|
1406
|
+
const rng = document.createRange();
|
|
1407
|
+
rng.setStart(na, oa);
|
|
1408
|
+
rng.setEnd(nb, ob);
|
|
1409
|
+
for (const r of rng.getClientRects()) {
|
|
1410
|
+
if (r.width < 0.6 || r.height < 0.6) continue; // 换行点产生的退化矩形
|
|
1411
|
+
const box = document.createElement('span');
|
|
1412
|
+
box.className = 'note-under';
|
|
1413
|
+
box.dataset.note = n.id;
|
|
1414
|
+
box.style.left = pct(r.left - rect.left, rect.width);
|
|
1415
|
+
box.style.top = pct(r.top - rect.top, rect.height);
|
|
1416
|
+
box.style.width = pct(r.width, rect.width);
|
|
1417
|
+
box.style.height = pct(r.height, rect.height);
|
|
1418
|
+
layer.appendChild(box);
|
|
1419
|
+
}
|
|
1420
|
+
} catch { /* 单条锚点算不出几何:跳过这条,列表里仍能看到 */ }
|
|
1421
|
+
}
|
|
1422
|
+
if (!layer.childElementCount) layer.remove();
|
|
1423
|
+
}
|
|
1424
|
+
/** 缩放/切模式/换文档会重建整个 DOM:重画全部波浪线 */
|
|
1425
|
+
function notesReapply() {
|
|
1426
|
+
noteClearLayers();
|
|
1427
|
+
if (!pdfDoc || !notes.list.length) return;
|
|
1428
|
+
document.querySelectorAll('#pdf-pages .page, #pdf-pages-zh .page').forEach((div) => notePaintPageDiv(div));
|
|
1429
|
+
}
|
|
1430
|
+
|
|
1431
|
+
/* ---------- 悬停提示 + 点击命中 ---------- */
|
|
1432
|
+
function noteById(id) {
|
|
1433
|
+
return notes.list.find((n) => n.id === id) || null;
|
|
1434
|
+
}
|
|
1435
|
+
function noteFindAt(x, y) {
|
|
1436
|
+
for (const box of document.querySelectorAll('.note-under')) {
|
|
1437
|
+
const r = box.getBoundingClientRect();
|
|
1438
|
+
if (x >= r.left - 1 && x <= r.right + 1 && y >= r.top - 1 && y <= r.bottom + 2) return box.dataset.note || null;
|
|
1439
|
+
}
|
|
1440
|
+
return null;
|
|
1441
|
+
}
|
|
1442
|
+
function noteHideTip() {
|
|
1443
|
+
if (noteTipEl) noteTipEl.style.display = 'none';
|
|
1444
|
+
}
|
|
1445
|
+
function noteShowTip(n, x, y) {
|
|
1446
|
+
if (!n || !noteTipEl) { noteHideTip(); return; }
|
|
1447
|
+
const body = String(n.body || '').trim();
|
|
1448
|
+
noteTipEl.textContent = '';
|
|
1449
|
+
const t = document.createElement('span');
|
|
1450
|
+
t.textContent = body.length > 400 ? body.slice(0, 400) + '…' : body;
|
|
1451
|
+
noteTipEl.appendChild(t);
|
|
1452
|
+
const hint = document.createElement('span');
|
|
1453
|
+
hint.className = 'tip-hint';
|
|
1454
|
+
hint.textContent = '点击编辑这条笔记';
|
|
1455
|
+
noteTipEl.appendChild(hint);
|
|
1456
|
+
noteTipEl.style.display = 'block';
|
|
1457
|
+
const r = noteTipEl.getBoundingClientRect();
|
|
1458
|
+
noteTipEl.style.left = Math.max(8, Math.min(x + 12, window.innerWidth - r.width - 8)) + 'px';
|
|
1459
|
+
noteTipEl.style.top = Math.max(8, Math.min(y + 16, window.innerHeight - r.height - 8)) + 'px';
|
|
1460
|
+
}
|
|
1461
|
+
$('main').addEventListener('mousemove', (e) => {
|
|
1462
|
+
if ((noteWinEl && noteWinEl.contains(e.target)) || (askPop && askPop.contains(e.target))) {
|
|
1463
|
+
noteHideTip();
|
|
1464
|
+
return;
|
|
1465
|
+
}
|
|
1466
|
+
if (noteHoverRaf) return;
|
|
1467
|
+
const x = e.clientX;
|
|
1468
|
+
const y = e.clientY;
|
|
1469
|
+
noteHoverRaf = requestAnimationFrame(() => {
|
|
1470
|
+
noteHoverRaf = 0;
|
|
1471
|
+
const id = noteFindAt(x, y);
|
|
1472
|
+
if (id === noteHoverId && id) { noteShowTip(noteById(id), x, y); return; }
|
|
1473
|
+
noteHoverId = id;
|
|
1474
|
+
if (id) noteShowTip(noteById(id), x, y);
|
|
1475
|
+
else noteHideTip();
|
|
1476
|
+
});
|
|
1477
|
+
});
|
|
1478
|
+
|
|
1479
|
+
/* ---------- 笔记窗口:列表态 / 编辑态 ---------- */
|
|
1480
|
+
function noteWinShow(on) {
|
|
1481
|
+
if (!noteWinEl) return;
|
|
1482
|
+
if (!on) {
|
|
1483
|
+
noteWinEl.classList.remove('on');
|
|
1484
|
+
notes.shown = false;
|
|
1485
|
+
noteHideTip();
|
|
1486
|
+
return;
|
|
1487
|
+
}
|
|
1488
|
+
noteWinEl.classList.add('on');
|
|
1489
|
+
notes.shown = true;
|
|
1490
|
+
noteWinPlace();
|
|
1491
|
+
renderNoteList();
|
|
1492
|
+
}
|
|
1493
|
+
function noteFmtTime(iso) {
|
|
1494
|
+
const d = iso ? new Date(iso) : null;
|
|
1495
|
+
if (!d || Number.isNaN(d.getTime())) return '';
|
|
1496
|
+
const p = (v) => String(v).padStart(2, '0');
|
|
1497
|
+
return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())} ${p(d.getHours())}:${p(d.getMinutes())}`;
|
|
1498
|
+
}
|
|
1499
|
+
function renderNoteList() {
|
|
1500
|
+
const list = $('note-list');
|
|
1501
|
+
const head = $('note-headcount');
|
|
1502
|
+
if (!list || !head) return;
|
|
1503
|
+
head.textContent = notes.list.length ? `${notes.list.length} 条` : '';
|
|
1504
|
+
list.textContent = '';
|
|
1505
|
+
if (!notes.list.length) {
|
|
1506
|
+
const empty = document.createElement('div');
|
|
1507
|
+
empty.id = 'note-empty';
|
|
1508
|
+
empty.textContent = notes.loading
|
|
1509
|
+
? '正在读取笔记…'
|
|
1510
|
+
: '还没有笔记。选中正文里的文字 → 点「笔记」,或点标题栏的 + 新建。笔记保存在这篇文献旁边(<论文>.notes.json),换浏览器也不会丢。';
|
|
1511
|
+
list.appendChild(empty);
|
|
1512
|
+
return;
|
|
1513
|
+
}
|
|
1514
|
+
// 按原文顺序列(没锚定的排最后)
|
|
1515
|
+
const sorted = notes.list.slice().sort((a, b) => {
|
|
1516
|
+
const pa = a.anchor && a.anchor.page ? a.anchor.page : 1e9;
|
|
1517
|
+
const pb = b.anchor && b.anchor.page ? b.anchor.page : 1e9;
|
|
1518
|
+
if (pa !== pb) return pa - pb;
|
|
1519
|
+
return ((a.anchor && a.anchor.start) || 0) - ((b.anchor && b.anchor.start) || 0);
|
|
1520
|
+
});
|
|
1521
|
+
const frag = document.createDocumentFragment();
|
|
1522
|
+
for (const n of sorted) {
|
|
1523
|
+
const btn = document.createElement('button');
|
|
1524
|
+
btn.type = 'button';
|
|
1525
|
+
btn.className = 'note-item';
|
|
1526
|
+
btn.dataset.id = n.id;
|
|
1527
|
+
btn.title = n.anchor && n.anchor.page ? `跳到第 ${n.anchor.page} 页` : '这条笔记没有可定位的原文位置';
|
|
1528
|
+
const meta = document.createElement('span');
|
|
1529
|
+
meta.className = 'ni-meta';
|
|
1530
|
+
meta.textContent = n.anchor && n.anchor.page ? `第 ${n.anchor.page} 页` : '未定位';
|
|
1531
|
+
const time = document.createElement('span');
|
|
1532
|
+
time.className = 'ni-time';
|
|
1533
|
+
time.textContent = noteFmtTime(n.updated || n.created);
|
|
1534
|
+
meta.appendChild(time);
|
|
1535
|
+
const bodyEl = document.createElement('span');
|
|
1536
|
+
bodyEl.className = 'ni-body';
|
|
1537
|
+
const body = String(n.body || '').trim();
|
|
1538
|
+
bodyEl.textContent = body.length > 220 ? body.slice(0, 220) + '…' : body;
|
|
1539
|
+
btn.appendChild(meta);
|
|
1540
|
+
btn.appendChild(bodyEl);
|
|
1541
|
+
if (n.quote) {
|
|
1542
|
+
const q = document.createElement('span');
|
|
1543
|
+
q.className = 'ni-quote';
|
|
1544
|
+
q.textContent = '「' + String(n.quote).replace(/\s+/g, ' ') + '」';
|
|
1545
|
+
btn.appendChild(q);
|
|
1546
|
+
}
|
|
1547
|
+
frag.appendChild(btn);
|
|
1548
|
+
}
|
|
1549
|
+
list.appendChild(frag);
|
|
1550
|
+
}
|
|
1551
|
+
function noteSetEditing(on) {
|
|
1552
|
+
notes.armed = false;
|
|
1553
|
+
const del = $('note-delete');
|
|
1554
|
+
if (del) { del.textContent = '删除'; del.classList.remove('primary'); }
|
|
1555
|
+
noteWinEl.classList.toggle('editing', !!on);
|
|
1556
|
+
if (!on) { notes.editing = null; notes.isNew = true; }
|
|
1557
|
+
}
|
|
1558
|
+
function noteStartEdit(note) {
|
|
1559
|
+
const isNew = !notes.list.some((n) => n.id === note.id);
|
|
1560
|
+
notes.editing = note;
|
|
1561
|
+
notes.isNew = isNew;
|
|
1562
|
+
$('note-quote').textContent = note.quote ? '「' + String(note.quote).replace(/\s+/g, ' ').slice(0, 300) + '」' : '(没有引用原文,只留正文)';
|
|
1563
|
+
$('note-body').value = note.body || '';
|
|
1564
|
+
$('note-delete').style.display = isNew ? 'none' : '';
|
|
1565
|
+
noteWinEl.classList.add('editing');
|
|
1566
|
+
noteWinShow(true);
|
|
1567
|
+
setTimeout(() => {
|
|
1568
|
+
const t = $('note-body');
|
|
1569
|
+
if (t) { t.focus(); t.setSelectionRange(t.value.length, t.value.length); }
|
|
1570
|
+
}, 30);
|
|
1571
|
+
}
|
|
1572
|
+
/** 划词「笔记」:带着引文和锚点直接进编辑态(保存前不落盘) */
|
|
1573
|
+
function noteCreateFromSelection() {
|
|
1574
|
+
const sel = askSelection;
|
|
1575
|
+
if (!sel || !current) { toast('先在正文里选中一段文字'); return; }
|
|
1576
|
+
const anchor = noteAnchorFromSelection();
|
|
1577
|
+
noteStartEdit({ quote: sel.text, anchor: anchor || undefined, body: '' });
|
|
1578
|
+
if (!anchor) toast('这段选区没能精确锚定:笔记会留在列表里,但不会画波浪线', 5000);
|
|
1579
|
+
}
|
|
1580
|
+
function noteCreateBlank() {
|
|
1581
|
+
const sel = askSelection;
|
|
1582
|
+
if (sel && sel.text) { noteCreateFromSelection(); return; }
|
|
1583
|
+
noteStartEdit({ quote: '', body: '' });
|
|
1584
|
+
}
|
|
1585
|
+
async function noteSave() {
|
|
1586
|
+
const body = $('note-body').value.trim();
|
|
1587
|
+
if (!body) { toast('笔记内容不能为空'); return; }
|
|
1588
|
+
if (!current) return;
|
|
1589
|
+
const old = notes.editing || {};
|
|
1590
|
+
const note = {
|
|
1591
|
+
id: old.id,
|
|
1592
|
+
quote: old.quote || '',
|
|
1593
|
+
anchor: old.anchor || undefined,
|
|
1594
|
+
body: body.slice(0, NOTE_BODY_MAX),
|
|
1595
|
+
created: old.created,
|
|
1596
|
+
updated: old.updated,
|
|
1597
|
+
};
|
|
1598
|
+
$('note-save').disabled = true;
|
|
1599
|
+
try {
|
|
1600
|
+
await notesPost('save', note);
|
|
1601
|
+
noteSetEditing(false);
|
|
1602
|
+
renderNoteList();
|
|
1603
|
+
notesReapply();
|
|
1604
|
+
toast('笔记已保存');
|
|
1605
|
+
} catch (e) { toast('保存失败:' + e.message, 6000); }
|
|
1606
|
+
$('note-save').disabled = false;
|
|
1607
|
+
}
|
|
1608
|
+
function noteArmDelete() {
|
|
1609
|
+
const btn = $('note-delete');
|
|
1610
|
+
if (!notes.editing || notes.isNew) return;
|
|
1611
|
+
if (!notes.armed) {
|
|
1612
|
+
notes.armed = true;
|
|
1613
|
+
btn.classList.add('primary');
|
|
1614
|
+
btn.textContent = '确认删除';
|
|
1615
|
+
btn.title = '再点一次确认删除这条笔记';
|
|
1616
|
+
clearTimeout(noteArmTimer);
|
|
1617
|
+
noteArmTimer = setTimeout(() => {
|
|
1618
|
+
notes.armed = false;
|
|
1619
|
+
btn.textContent = '删除';
|
|
1620
|
+
btn.classList.remove('primary');
|
|
1621
|
+
btn.title = '删除这条笔记';
|
|
1622
|
+
}, 3000);
|
|
1623
|
+
return;
|
|
1624
|
+
}
|
|
1625
|
+
clearTimeout(noteArmTimer);
|
|
1626
|
+
notes.armed = false;
|
|
1627
|
+
btn.textContent = '删除';
|
|
1628
|
+
btn.classList.remove('primary');
|
|
1629
|
+
void noteDoDelete(notes.editing);
|
|
1630
|
+
}
|
|
1631
|
+
async function noteDoDelete(note) {
|
|
1632
|
+
if (!note || !note.id) return;
|
|
1633
|
+
try {
|
|
1634
|
+
await notesPost('delete', note);
|
|
1635
|
+
noteSetEditing(false);
|
|
1636
|
+
renderNoteList();
|
|
1637
|
+
notesReapply();
|
|
1638
|
+
toast('笔记已删除');
|
|
1639
|
+
} catch (e) { toast('删除失败:' + e.message, 6000); }
|
|
1640
|
+
}
|
|
1641
|
+
async function noteGoto(note) {
|
|
1642
|
+
if (!note) return;
|
|
1643
|
+
const a = note.anchor;
|
|
1644
|
+
if (!a || !a.page) { toast('这条笔记没有可定位的原文位置'); return; }
|
|
1645
|
+
await findReadyPage(a.page);
|
|
1646
|
+
notesReapply();
|
|
1647
|
+
// 不把 id 拼进 CSS 选择器:服务端只截长度、不限字符集,id 含引号/括号会抛 DOMException。
|
|
1648
|
+
let box = null;
|
|
1649
|
+
for (const b of document.querySelectorAll('.note-under')) {
|
|
1650
|
+
if (b.dataset.note === note.id) { box = b; break; }
|
|
1651
|
+
}
|
|
1652
|
+
const target = box || origPageNode(a.page);
|
|
1653
|
+
if (target) target.scrollIntoView({ behavior: 'smooth', block: 'center' });
|
|
1654
|
+
if (box) {
|
|
1655
|
+
box.classList.add('flash');
|
|
1656
|
+
setTimeout(() => box.classList.remove('flash'), 900);
|
|
1657
|
+
}
|
|
1658
|
+
}
|
|
1659
|
+
|
|
1660
|
+
/* ---------- 气泡里的附加动作:笔记 / 全局搜索 ---------- */
|
|
1661
|
+
/** 「全局搜索」= 把选中文字塞进查找窗口并跳到第一处(与手打关键词同一条链路,不另做一套搜索) */
|
|
1662
|
+
function findSearchText(text) {
|
|
1663
|
+
const q = String(text || '').replace(/\s+/g, ' ').trim();
|
|
1664
|
+
if (!q) { toast('没有可搜索的文字'); return; }
|
|
1665
|
+
const use = q.length > 120 ? q.slice(0, 120) : q;
|
|
1666
|
+
findInputEl.value = use;
|
|
1667
|
+
findWinShow(true);
|
|
1668
|
+
void findRun(use).then(() => findGoto(0)).catch(() => { /* 索引失败:列表里会有说明 */ });
|
|
1669
|
+
}
|
|
1670
|
+
$('ask-find').onclick = () => {
|
|
1671
|
+
askPop.style.display = 'none';
|
|
1672
|
+
if (askSelection) findSearchText(askSelection.text);
|
|
1673
|
+
};
|
|
1674
|
+
$('ask-note').onclick = () => {
|
|
1675
|
+
askPop.style.display = 'none';
|
|
1676
|
+
noteCreateFromSelection();
|
|
1677
|
+
};
|
|
1678
|
+
|
|
1679
|
+
/* ---------- 接线 ---------- */
|
|
1680
|
+
$('note-open').onclick = () => {
|
|
1681
|
+
if (!current) return;
|
|
1682
|
+
if (noteWinEl.classList.contains('on') && !noteWinEl.classList.contains('editing')) { noteWinShow(false); return; }
|
|
1683
|
+
noteSetEditing(false);
|
|
1684
|
+
noteWinShow(true);
|
|
1685
|
+
};
|
|
1686
|
+
$('note-window-close').onclick = () => { noteSetEditing(false); noteWinShow(false); };
|
|
1687
|
+
$('note-new').onclick = () => noteCreateBlank();
|
|
1688
|
+
$('note-save').onclick = () => void noteSave();
|
|
1689
|
+
$('note-cancel').onclick = () => noteSetEditing(false);
|
|
1690
|
+
$('note-delete').onclick = () => noteArmDelete();
|
|
1691
|
+
$('note-list').addEventListener('click', (e) => {
|
|
1692
|
+
const btn = e.target.closest ? e.target.closest('.note-item') : null;
|
|
1693
|
+
if (!btn) return;
|
|
1694
|
+
void noteGoto(noteById(btn.dataset.id));
|
|
1695
|
+
});
|
|
1696
|
+
$('note-body').addEventListener('keydown', (e) => {
|
|
1697
|
+
if ((e.metaKey || e.ctrlKey) && e.key === 'Enter') { e.preventDefault(); void noteSave(); }
|
|
1698
|
+
else if (e.key === 'Escape') { e.preventDefault(); noteSetEditing(false); }
|
|
1699
|
+
});
|
|
1700
|
+
// 全局 Esc:标题栏写着「关闭(Esc)」就必须真的能关。编辑态里先退出编辑(上面的 textarea
|
|
1701
|
+
// 处理器已 preventDefault,这里靠 defaultPrevented 避免同一次按键把窗口也一起关掉)。
|
|
1702
|
+
document.addEventListener('keydown', (e) => {
|
|
1703
|
+
if (e.key !== 'Escape' || e.defaultPrevented) return;
|
|
1704
|
+
if (!noteWinEl || !noteWinEl.classList.contains('on')) return;
|
|
1705
|
+
if (noteWinEl.classList.contains('editing')) noteSetEditing(false);
|
|
1706
|
+
else noteWinShow(false);
|
|
1707
|
+
});
|
|
1708
|
+
$('note-head').addEventListener('pointerdown', noteWinDrag);
|
|
1709
|
+
$('note-grip').addEventListener('pointerdown', (e) => noteWinResize(e, 'se'));
|
|
1710
|
+
$('note-grip-r').addEventListener('pointerdown', (e) => noteWinResize(e, 'e'));
|
|
1711
|
+
$('note-grip-b').addEventListener('pointerdown', (e) => noteWinResize(e, 's'));
|
|
1712
|
+
window.addEventListener('resize', () => { if (noteWinEl.classList.contains('on')) noteWinPlace(); });
|
|
1713
|
+
$('pdf-pane').addEventListener('scroll', () => { noteHideTip(); }, { passive: true });
|
|
1714
|
+
noteWinLoad();
|
|
1715
|
+
|
|
1716
|
+
/** 排查用:控制台执行 dprNotesDebug() 看笔记状态(波浪线不显示时先看 boxes) */
|
|
1717
|
+
window.dprNotesDebug = () => ({
|
|
1718
|
+
paper: current ? current.topic + '/' + current.name : null,
|
|
1719
|
+
notes: notes.list.length,
|
|
1720
|
+
anchored: notes.list.filter((n) => n.anchor).length,
|
|
1721
|
+
boxes: document.querySelectorAll('.note-under').length,
|
|
1722
|
+
layers: document.querySelectorAll('.noteLayer').length,
|
|
1723
|
+
editing: notes.editing ? notes.editing.id || '(new)' : null,
|
|
1724
|
+
windowOpen: !!(noteWinEl && noteWinEl.classList.contains('on')),
|
|
1725
|
+
});
|
|
1726
|
+
|
|
1094
1727
|
/* ================= 中英切换 ================= */
|
|
1095
1728
|
/** 翻译进度条(只在 busy 时出现):宽度跟着后端 progress.overall,文字给阶段+百分比。 */
|
|
1096
1729
|
function updateZhProgress() { const box = $('zh-prog');
|
|
@@ -1313,6 +1946,9 @@ const PROVIDERS = [
|
|
|
1313
1946
|
{ id: 'deepseek', name: 'DeepSeek', baseUrl: 'https://api.deepseek.com', models: ['deepseek-v4-pro', 'deepseek-flash'] },
|
|
1314
1947
|
{ id: 'glm', name: '智谱 GLM', baseUrl: 'https://open.bigmodel.cn/api/coding/paas/v4', models: ['glm-5.3-flash', 'glm-5.3', 'glm-4.6'] },
|
|
1315
1948
|
{ id: 'kimi', name: 'Kimi', baseUrl: 'https://api.kimi.com/coding/v1', models: ['k3'] },
|
|
1949
|
+
// OpenCode Go:订阅后到 OpenCode Console 复制 API Key;模型列表以控制台为准(此处只做输入建议)。
|
|
1950
|
+
// 服务端会对 opencode.ai 的请求自带 x-opencode-session(见 src/opencode.ts)。
|
|
1951
|
+
{ id: 'opencode', name: 'OpenCode Go', baseUrl: 'https://opencode.ai/inference/go/openai/v1', models: ['deepseek-v4.1-flash', 'glm-5.3-flash', 'kimi-k3.1-pro', 'qwen3.8-flash', 'minimax-m3.1-flash'] },
|
|
1316
1952
|
];
|
|
1317
1953
|
const normUrl = (u) => (u || '').trim().replace(/\/+$/, '');
|
|
1318
1954
|
// 按 baseUrl 反查预设(打开表单时回显选中项);查不到就是 custom
|
|
@@ -1449,6 +2085,8 @@ async function loadPdf(url, opts = {}) {
|
|
|
1449
2085
|
// 翻页控件与模式切换按钮:文档就绪后才出现
|
|
1450
2086
|
$('pager').style.display = '';
|
|
1451
2087
|
$('mode-toggle').style.display = '';
|
|
2088
|
+
// 笔记请求与 PDF 文档加载并行;首次渲染前才等它(见 openPaper 的 beforePaint)
|
|
2089
|
+
if (opts.beforePaint) await opts.beforePaint;
|
|
1452
2090
|
// 分屏(模式 B)自己安排两列的渲染与适宽:这里只把原文文档装上,不触发单列渲染
|
|
1453
2091
|
if (opts.deferRender) return;
|
|
1454
2092
|
if (fitMode) { fitToWidth(); return; } // fitToWidth 内部 setZoom → rerender
|
|
@@ -1505,13 +2143,18 @@ async function paintPage(page, div, viewport, token) {
|
|
|
1505
2143
|
// 少了这一行,被抢占的收尾会把**上一档缩放**的几何写回 EQ_BY_PAGE → 点击热区整体偏移。
|
|
1506
2144
|
if (token !== renderToken) return;
|
|
1507
2145
|
paintEqLayer(div, viewport, +div.dataset.page);
|
|
2146
|
+
// 笔记波浪线:滚动模式懒加载、翻页模式单页渲染时也要画,不能只靠 rerender 收尾那一次
|
|
2147
|
+
notePaintPageDiv(div);
|
|
1508
2148
|
}
|
|
1509
2149
|
|
|
1510
2150
|
/** 当前模式的全量重渲入口:缩放/DPR/换文档/换变体统一走这里。
|
|
1511
2151
|
* 分屏(模式 B)是第三套布局,转到 renderSplit();renderPaged/renderAllPages 的既有语义**不变**。 */
|
|
1512
2152
|
function rerender() {
|
|
1513
|
-
//
|
|
1514
|
-
const done = () => {
|
|
2153
|
+
// 渲染完成后再补画查找高亮与笔记波浪线:缩放/切模式把 DOM 整个重建,旧的装饰层随之消失。
|
|
2154
|
+
const done = () => {
|
|
2155
|
+
try { findReapply(); } catch { /* 高亮是装饰,失败不影响阅读 */ }
|
|
2156
|
+
try { notesReapply(); } catch { /* 同上 */ }
|
|
2157
|
+
};
|
|
1515
2158
|
if (viewMode === 'split') return renderSplit().then(done);
|
|
1516
2159
|
return (pageMode === 'paged' ? renderPaged() : renderAllPages()).then(done);
|
|
1517
2160
|
}
|
|
@@ -2108,6 +2751,10 @@ function showAskPop(o) {
|
|
|
2108
2751
|
&& (askSelection.doc ?? 'orig') === 'orig'
|
|
2109
2752
|
&& askSelection.text.length <= MAX_SELECTION_CHARS
|
|
2110
2753
|
? 'inline-block' : 'none';
|
|
2754
|
+
// 笔记 / 全局搜索:只对文字选区出现(公式引用没有可标注/可搜索的正文)
|
|
2755
|
+
const textSel = !!(askSelection && askSelection.kind === 'text');
|
|
2756
|
+
$('ask-note').style.display = textSel ? 'inline-block' : 'none';
|
|
2757
|
+
$('ask-find').style.display = textSel ? 'inline-block' : 'none';
|
|
2111
2758
|
hideTrPop(); // 新选区/新引用出现即收起上一个译文气泡(同一锚点必然重叠)
|
|
2112
2759
|
invalidateTranslate(); // R-01:新选区/新引用必须让在飞请求失效,否则旧译文会盖到新锚点上
|
|
2113
2760
|
askPop.style.display = 'block';
|
|
@@ -2122,6 +2769,7 @@ document.addEventListener('mousedown', (e) => { downPt = { x: e.clientX, y: e.cl
|
|
|
2122
2769
|
|
|
2123
2770
|
document.addEventListener('mouseup', (e) => {
|
|
2124
2771
|
if (askPop.contains(e.target)) return;
|
|
2772
|
+
if (noteWinEl && noteWinEl.contains(e.target)) return;
|
|
2125
2773
|
setTimeout(() => {
|
|
2126
2774
|
const moved = downPt ? Math.abs(e.clientX - downPt.x) + Math.abs(e.clientY - downPt.y) > 4 : false;
|
|
2127
2775
|
downPt = null;
|
|
@@ -2130,6 +2778,12 @@ document.addEventListener('mouseup', (e) => {
|
|
|
2130
2778
|
if (!text || !current) {
|
|
2131
2779
|
// 无选区(或没有当前文献)时才尝试公式点击 —— 有选区时「划词提问」优先,绝不抢。
|
|
2132
2780
|
if (!text && !moved && current) {
|
|
2781
|
+
// 点在被标注的文字上 → 直接编辑那条笔记(笔记优先于公式热区)
|
|
2782
|
+
const nid = noteFindAt(e.clientX, e.clientY);
|
|
2783
|
+
if (nid) {
|
|
2784
|
+
const n = noteById(nid);
|
|
2785
|
+
if (n) { askPop.style.display = 'none'; noteStartEdit(n); return; }
|
|
2786
|
+
}
|
|
2133
2787
|
const hit = hitTestFormula(e);
|
|
2134
2788
|
if (hit) { citeFormula(hit.eq, e.clientX, e.clientY + 6); return; }
|
|
2135
2789
|
}
|
|
@@ -2155,7 +2809,9 @@ document.addEventListener('mouseup', (e) => {
|
|
|
2155
2809
|
const r0 = sel.getRangeAt(0);
|
|
2156
2810
|
if (r0 && typeof r0.cloneRange === 'function') askSelection.range = r0.cloneRange();
|
|
2157
2811
|
} catch { /* 选区已失效:保持无 range */ }
|
|
2158
|
-
|
|
2812
|
+
// 划词后**直接**弹回 v1.6.0 的「选中即问」气泡(E11/R-01/R-07 与 docs/features.md 的契约);
|
|
2813
|
+
// 笔记 / 全局搜索作为气泡里的小按钮,不再多一层菜单。
|
|
2814
|
+
showAskPop({ quote: text, x: e.clientX, y: Math.min(range.bottom + 8, window.innerHeight - 60) });
|
|
2159
2815
|
}, 10);
|
|
2160
2816
|
});
|
|
2161
2817
|
document.addEventListener('mousedown', (e) => { if (!askPop.contains(e.target)) askPop.style.display = 'none'; });
|