dsh-plugin-zotero 0.6.0 → 0.6.1
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/client/client.js +493 -83
- package/client/snapshot/runtime.js +88 -2
- package/lib/host/router.js +33 -5
- package/lib/host/snapshot.js +2 -2
- package/lib/zotero/client.d.ts +13 -3
- package/lib/zotero/client.js +98 -22
- package/lib/zotero/prefs.d.ts +31 -0
- package/lib/zotero/prefs.js +84 -0
- package/lib/zotero/types.d.ts +10 -0
- package/package.json +1 -1
package/client/client.js
CHANGED
|
@@ -12,10 +12,13 @@ const WRITE_HEADER = "x-dsh-zotero-write";
|
|
|
12
12
|
/** Host 返回的错误,携带 §6 的错误码。 */
|
|
13
13
|
var ApiFailure = class extends Error {
|
|
14
14
|
code;
|
|
15
|
-
|
|
15
|
+
/** 有值时表示这个错误可以一键去修(页面上多一个按钮)。 */
|
|
16
|
+
fix;
|
|
17
|
+
constructor(code, message, fix) {
|
|
16
18
|
super(message);
|
|
17
19
|
this.name = "ApiFailure";
|
|
18
20
|
this.code = code;
|
|
21
|
+
this.fix = fix;
|
|
19
22
|
}
|
|
20
23
|
};
|
|
21
24
|
function buildUrl(action, query = {}) {
|
|
@@ -32,7 +35,7 @@ function buildUrl(action, query = {}) {
|
|
|
32
35
|
async function readEnvelope(res) {
|
|
33
36
|
const payload = await res.json().catch(() => null);
|
|
34
37
|
if (payload === null) throw new ApiFailure("internal_error", `Host 返回了非 JSON 响应(HTTP ${res.status})`);
|
|
35
|
-
if (!payload.ok) throw new ApiFailure(payload.error.code, payload.error.message);
|
|
38
|
+
if (!payload.ok) throw new ApiFailure(payload.error.code, payload.error.message, payload.error.fix);
|
|
36
39
|
return payload.data;
|
|
37
40
|
}
|
|
38
41
|
async function get(action, query = {}, signal) {
|
|
@@ -122,6 +125,15 @@ function snapshotUrl(key) {
|
|
|
122
125
|
async function openInZotero(input) {
|
|
123
126
|
await post("open-in-zotero", input);
|
|
124
127
|
}
|
|
128
|
+
/**
|
|
129
|
+
* 打开「去那儿修」的系统面板。
|
|
130
|
+
*
|
|
131
|
+
* 目前只支持 macOS 的「文件与文件夹」授权:插件读不到 Zotero 附件时,最需要
|
|
132
|
+
* 用户动手的那一步就在那里,让他在系统设置里翻半天不如给个按钮。
|
|
133
|
+
*/
|
|
134
|
+
async function openSystemSettings(target) {
|
|
135
|
+
await post("open-settings", { target });
|
|
136
|
+
}
|
|
125
137
|
/** 写通道当前状态(开关 + 内存里有没有 key)。 */
|
|
126
138
|
function fetchWriteStatus(signal) {
|
|
127
139
|
return get("auth-status", {}, signal);
|
|
@@ -389,9 +401,14 @@ const dicts = {
|
|
|
389
401
|
"detail.info": "文献信息与标注",
|
|
390
402
|
"outline.title": "目录",
|
|
391
403
|
"outline.loading": "正在读取目录…",
|
|
392
|
-
"outline.empty": "
|
|
404
|
+
"outline.empty": "这份文档没有目录",
|
|
393
405
|
"outline.count": "{n} 条",
|
|
394
406
|
"outline.jump": "跳到第 {page} 页",
|
|
407
|
+
"outline.jumpHeading": "跳到正文对应位置",
|
|
408
|
+
"outline.source.pdf": "PDF 书签 · 按页跳转",
|
|
409
|
+
"outline.source.snapshot": "网页快照标题 · 按标题定位",
|
|
410
|
+
"outline.source.note": "笔记标题 · 按标题定位",
|
|
411
|
+
"outline.source.text": "全文标题 · 按标题定位",
|
|
395
412
|
"detail.attachmentOpen": "在文档区打开",
|
|
396
413
|
"detail.annotationLocate": "定位",
|
|
397
414
|
"detail.attachments": "附件",
|
|
@@ -552,6 +569,8 @@ const dicts = {
|
|
|
552
569
|
"viewer.serverResponse": "Zotero 返回了 {status},但附件本身可以读取,请重试一次。",
|
|
553
570
|
"viewer.attachmentUnavailable": "附件打不开:{message}",
|
|
554
571
|
"viewer.unsupported": "暂不支持预览 {type},可用「在 Zotero 中打开」。",
|
|
572
|
+
"viewer.openPrivacySettings": "打开系统设置",
|
|
573
|
+
"viewer.openSettingsFailed": "打不开系统设置:{message}",
|
|
555
574
|
"viewer.page": "第 {n} 页",
|
|
556
575
|
"viewer.pageJump": "跳转到页码(回车确认,Esc 取消)",
|
|
557
576
|
"viewer.pageTotal": "/ {total}",
|
|
@@ -568,7 +587,7 @@ const dicts = {
|
|
|
568
587
|
"viewer.snapshotTimeout": "网页快照加载超时,内容可能不完整",
|
|
569
588
|
"viewer.snapshotRetry": "重新加载",
|
|
570
589
|
"viewer.snapshotNoHighlight": "这条快照没能加载高亮脚本,标注暂不可见",
|
|
571
|
-
"viewer.snapshotStaleBridge": "
|
|
590
|
+
"viewer.snapshotStaleBridge": "快照脚本是旧版本:编辑卡片可能居中、网页标题目录可能为空,重启 DSH 后恢复",
|
|
572
591
|
"viewer.highlightPartial": "有 {n} 条标注未能在网页上定位",
|
|
573
592
|
"detail.noSnapshot": "这条网页条目没有保存网页快照",
|
|
574
593
|
"detail.noSnapshotHint": "抓取时勾选「保存网页快照」才能离线还原网页;下面是 Zotero 索引的全文文本",
|
|
@@ -716,9 +735,14 @@ const dicts = {
|
|
|
716
735
|
"detail.info": "Metadata and annotations",
|
|
717
736
|
"outline.title": "Contents",
|
|
718
737
|
"outline.loading": "Reading the outline…",
|
|
719
|
-
"outline.empty": "This
|
|
738
|
+
"outline.empty": "This document has no outline",
|
|
720
739
|
"outline.count": "{n} entries",
|
|
721
740
|
"outline.jump": "Go to page {page}",
|
|
741
|
+
"outline.jumpHeading": "Scroll to this heading",
|
|
742
|
+
"outline.source.pdf": "PDF bookmarks · jump by page",
|
|
743
|
+
"outline.source.snapshot": "Web snapshot headings · jump to the heading",
|
|
744
|
+
"outline.source.note": "Note headings · jump to the heading",
|
|
745
|
+
"outline.source.text": "Full-text headings · jump to the heading",
|
|
722
746
|
"detail.attachmentOpen": "Open in the document pane",
|
|
723
747
|
"detail.annotationLocate": "Locate",
|
|
724
748
|
"detail.attachments": "Attachments",
|
|
@@ -879,6 +903,8 @@ const dicts = {
|
|
|
879
903
|
"viewer.serverResponse": "Zotero returned {status}, but the attachment itself is readable — please retry.",
|
|
880
904
|
"viewer.attachmentUnavailable": "Cannot open the attachment: {message}",
|
|
881
905
|
"viewer.unsupported": "Preview is not supported for {type} — use “Open in Zotero”.",
|
|
906
|
+
"viewer.openPrivacySettings": "Open System Settings",
|
|
907
|
+
"viewer.openSettingsFailed": "Cannot open System Settings: {message}",
|
|
882
908
|
"viewer.page": "Page {n}",
|
|
883
909
|
"viewer.pageJump": "Jump to page (Enter to confirm, Esc to cancel)",
|
|
884
910
|
"viewer.pageTotal": "/ {total}",
|
|
@@ -895,7 +921,7 @@ const dicts = {
|
|
|
895
921
|
"viewer.snapshotTimeout": "The web snapshot is taking too long; content may be incomplete",
|
|
896
922
|
"viewer.snapshotRetry": "Reload",
|
|
897
923
|
"viewer.snapshotNoHighlight": "The highlight script did not load; annotations are hidden",
|
|
898
|
-
"viewer.snapshotStaleBridge": "The snapshot bridge is outdated: the edit card
|
|
924
|
+
"viewer.snapshotStaleBridge": "The snapshot bridge is outdated: the edit card may be centred and the heading outline may be empty. Restart DSH to fix it",
|
|
899
925
|
"viewer.highlightPartial": "{n} annotation(s) could not be located on the page",
|
|
900
926
|
"detail.noSnapshot": "No web snapshot was saved for this item",
|
|
901
927
|
"detail.noSnapshotHint": "Enable “Save web page snapshot” when capturing to render the page offline; showing the indexed full text instead",
|
|
@@ -4771,20 +4797,21 @@ const attachmentMetaStyle = {
|
|
|
4771
4797
|
//#endregion
|
|
4772
4798
|
//#region src/client/OutlinePanel.tsx
|
|
4773
4799
|
/**
|
|
4774
|
-
*
|
|
4800
|
+
* 左上角的**统一**目录浮层:PDF 书签、网页快照标题、笔记 / 全文标题都在这里。
|
|
4775
4801
|
*
|
|
4776
4802
|
* 按钮与「文献信息」并排(目录在左),收起规则和其它浮层共用 {@link useDismiss}:
|
|
4777
4803
|
* 点浮层外、按 Esc、点进网页快照正文里都会收起。
|
|
4778
4804
|
*
|
|
4779
|
-
*
|
|
4780
|
-
*
|
|
4805
|
+
* 目录数据一律由渲染器上报(见 `viewer/types.ts` 的 `onOutlineChange`),这里只负责画:
|
|
4806
|
+
* 面板不解释跳转目标,点一条就把 `target` 原样交回 `ItemDetail` 转给渲染器——
|
|
4807
|
+
* 于是 PDF 跳页、HTML 滚到标题可以共用同一份 UI。
|
|
4781
4808
|
*
|
|
4782
4809
|
* 点条目跳转后立刻收起:浮层压在文档左侧,留着就把刚跳过去的内容挡了一半。
|
|
4783
4810
|
*/
|
|
4784
4811
|
/** 缩进宽度;层级再深也不再往外推,免得窄面板里标题全被挤成一点。 */
|
|
4785
4812
|
const INDENT$1 = 13;
|
|
4786
4813
|
const MAX_INDENT_DEPTH = 6;
|
|
4787
|
-
function OutlinePanel({ t, items, open, onToggle, onClose, onJump }) {
|
|
4814
|
+
function OutlinePanel({ t, items, sourceLabel, open, onToggle, onClose, onJump }) {
|
|
4788
4815
|
const rootRef = (0, react.useRef)(null);
|
|
4789
4816
|
/** 悬停行:内联 style 写不了 `:hover`,只能自己记一个下标。 */
|
|
4790
4817
|
const [hovered, setHovered] = (0, react.useState)(null);
|
|
@@ -4815,6 +4842,10 @@ function OutlinePanel({ t, items, open, onToggle, onClose, onJump }) {
|
|
|
4815
4842
|
children: tpl(t("outline.count"), { n: items.length })
|
|
4816
4843
|
})]
|
|
4817
4844
|
}),
|
|
4845
|
+
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
|
|
4846
|
+
style: sourceStyle,
|
|
4847
|
+
children: sourceLabel
|
|
4848
|
+
}),
|
|
4818
4849
|
items === null && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", {
|
|
4819
4850
|
style: emptyStyle$1,
|
|
4820
4851
|
children: t("outline.loading")
|
|
@@ -4826,16 +4857,15 @@ function OutlinePanel({ t, items, open, onToggle, onClose, onJump }) {
|
|
|
4826
4857
|
items !== null && items.length > 0 && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
|
|
4827
4858
|
style: listStyle,
|
|
4828
4859
|
children: items.map((item, index) => {
|
|
4829
|
-
const target = item.
|
|
4860
|
+
const target = item.target;
|
|
4861
|
+
const hint = target === null ? item.title : `${item.title}\n${target.kind === "page" ? tpl(t("outline.jump"), { page: target.page }) : t("outline.jumpHeading")}`;
|
|
4830
4862
|
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
|
|
4831
4863
|
type: "button",
|
|
4832
4864
|
disabled: target === null,
|
|
4833
|
-
onClick: () =>
|
|
4834
|
-
if (target !== null) onJump(target);
|
|
4835
|
-
},
|
|
4865
|
+
onClick: () => onJump(item),
|
|
4836
4866
|
onMouseEnter: () => setHovered(index),
|
|
4837
4867
|
onMouseLeave: () => setHovered((current) => current === index ? null : current),
|
|
4838
|
-
title:
|
|
4868
|
+
title: hint,
|
|
4839
4869
|
style: {
|
|
4840
4870
|
...rowStyle$2,
|
|
4841
4871
|
paddingLeft: 6 + Math.min(item.depth, MAX_INDENT_DEPTH) * INDENT$1,
|
|
@@ -4941,6 +4971,13 @@ const countStyle$1 = {
|
|
|
4941
4971
|
fontWeight: 400,
|
|
4942
4972
|
opacity: .65
|
|
4943
4973
|
};
|
|
4974
|
+
/** 来源说明:三种文档解析出来的目录长得不一样,给用户一句口径。 */
|
|
4975
|
+
const sourceStyle = {
|
|
4976
|
+
flexShrink: 0,
|
|
4977
|
+
padding: "0 4px 6px",
|
|
4978
|
+
fontSize: 11,
|
|
4979
|
+
opacity: .58
|
|
4980
|
+
};
|
|
4944
4981
|
const listStyle = {
|
|
4945
4982
|
flex: 1,
|
|
4946
4983
|
minHeight: 0,
|
|
@@ -29036,6 +29073,138 @@ function AnnotationOverlay({ annotations, viewport, flashKey }) {
|
|
|
29036
29073
|
})
|
|
29037
29074
|
});
|
|
29038
29075
|
}
|
|
29076
|
+
/**
|
|
29077
|
+
* 加到标题元素上的临时锚点属性。
|
|
29078
|
+
*
|
|
29079
|
+
* 只出现在插件自己生成的 HTML 里(笔记正文经 {@link parseHtmlHeadings} 加工),
|
|
29080
|
+
* 不会写回 Zotero;不用原生 `id`,免得撞上笔记里本来就有的锚点。
|
|
29081
|
+
*/
|
|
29082
|
+
const OUTLINE_ANCHOR_ATTR = "data-dsh-zp-outline";
|
|
29083
|
+
function pageTarget(page) {
|
|
29084
|
+
return {
|
|
29085
|
+
kind: "page",
|
|
29086
|
+
page
|
|
29087
|
+
};
|
|
29088
|
+
}
|
|
29089
|
+
function anchorTarget(id) {
|
|
29090
|
+
return {
|
|
29091
|
+
kind: "anchor",
|
|
29092
|
+
id: String(id)
|
|
29093
|
+
};
|
|
29094
|
+
}
|
|
29095
|
+
/**
|
|
29096
|
+
* 标题级别 → 缩进层级。
|
|
29097
|
+
*
|
|
29098
|
+
* 以整篇文档里**最小的标题级别**为 0:通篇只有 h3 的页面不该整体缩进两格,
|
|
29099
|
+
* 网页快照尤其常见(正文从 h2 起)。PDF 书签不用这个,它自带树形层级。
|
|
29100
|
+
*/
|
|
29101
|
+
function headingDepths(levels) {
|
|
29102
|
+
if (levels.length === 0) return [];
|
|
29103
|
+
const base = Math.min(...levels);
|
|
29104
|
+
return levels.map((level) => Math.max(0, Math.min(7, level - base)));
|
|
29105
|
+
}
|
|
29106
|
+
const HTML_HEADING_TAGS = {
|
|
29107
|
+
H1: 1,
|
|
29108
|
+
H2: 2,
|
|
29109
|
+
H3: 3,
|
|
29110
|
+
H4: 4,
|
|
29111
|
+
H5: 5,
|
|
29112
|
+
H6: 6
|
|
29113
|
+
};
|
|
29114
|
+
/** 元素纯文本或纯文本行里的 Markdown 标题(`# 标题`)。 */
|
|
29115
|
+
const MARKDOWN_HEADING = /^(#{1,6})[ \t]+(.+?)[ \t]*#*[ \t]*$/u;
|
|
29116
|
+
const MARKDOWN_LINE = /^[ \t]{0,3}(#{1,6})[ \t]+(.+?)[ \t]*#*[ \t]*$/u;
|
|
29117
|
+
function tidy(value) {
|
|
29118
|
+
return value.replace(/\s+/gu, " ").trim();
|
|
29119
|
+
}
|
|
29120
|
+
/**
|
|
29121
|
+
* 从 HTML 片段里提标题,并给每个标题打上锚点。
|
|
29122
|
+
*
|
|
29123
|
+
* 笔记是 Zotero 存的 HTML:标题通常是 `<h1>`–`<h6>`,但 Markdown 源码被原样贴进
|
|
29124
|
+
* 笔记时会变成 `<p># 标题</p>`,所以两种都认。解析用 `DOMParser` 的 inert 文档,
|
|
29125
|
+
* 只取节点与文本、不执行任何脚本;正文里危险的标签 Host 侧已经剥过。
|
|
29126
|
+
*
|
|
29127
|
+
* 返回加工后的 HTML 而不是只返回条目:标题节点要带锚点,跳转才有落点。
|
|
29128
|
+
*/
|
|
29129
|
+
function parseHtmlHeadings(html) {
|
|
29130
|
+
const doc = new DOMParser().parseFromString(`<body>${html}</body>`, "text/html");
|
|
29131
|
+
const candidates = Array.from(doc.body.querySelectorAll("h1,h2,h3,h4,h5,h6,p,li,blockquote"));
|
|
29132
|
+
const picked = [];
|
|
29133
|
+
const pickedElements = /* @__PURE__ */ new Set();
|
|
29134
|
+
for (const element of candidates) {
|
|
29135
|
+
if (picked.length >= 800) break;
|
|
29136
|
+
let parent = element.parentElement;
|
|
29137
|
+
let nested = false;
|
|
29138
|
+
while (parent !== null && parent !== doc.body) {
|
|
29139
|
+
if (pickedElements.has(parent)) {
|
|
29140
|
+
nested = true;
|
|
29141
|
+
break;
|
|
29142
|
+
}
|
|
29143
|
+
parent = parent.parentElement;
|
|
29144
|
+
}
|
|
29145
|
+
if (nested) continue;
|
|
29146
|
+
const raw = tidy(element.textContent ?? "");
|
|
29147
|
+
const tagLevel = HTML_HEADING_TAGS[element.tagName] ?? 0;
|
|
29148
|
+
const markdown = tagLevel === 0 ? MARKDOWN_HEADING.exec(raw) : null;
|
|
29149
|
+
const level = tagLevel > 0 ? tagLevel : markdown === null ? 0 : markdown[1].length;
|
|
29150
|
+
const title = tagLevel > 0 ? raw : tidy(markdown?.[2] ?? "");
|
|
29151
|
+
if (level === 0 || title === "") continue;
|
|
29152
|
+
picked.push({
|
|
29153
|
+
element,
|
|
29154
|
+
level,
|
|
29155
|
+
title
|
|
29156
|
+
});
|
|
29157
|
+
pickedElements.add(element);
|
|
29158
|
+
}
|
|
29159
|
+
const depths = headingDepths(picked.map((entry) => entry.level));
|
|
29160
|
+
const items = picked.map((entry, index) => {
|
|
29161
|
+
entry.element.setAttribute(OUTLINE_ANCHOR_ATTR, String(index));
|
|
29162
|
+
return {
|
|
29163
|
+
title: entry.title,
|
|
29164
|
+
depth: depths[index],
|
|
29165
|
+
target: anchorTarget(index)
|
|
29166
|
+
};
|
|
29167
|
+
});
|
|
29168
|
+
return {
|
|
29169
|
+
html: doc.body.innerHTML,
|
|
29170
|
+
items
|
|
29171
|
+
};
|
|
29172
|
+
}
|
|
29173
|
+
/**
|
|
29174
|
+
* 从纯文本里提 Markdown 标题。
|
|
29175
|
+
*
|
|
29176
|
+
* 用于 PDF 全文索引、`.md` 附件的全文文本这类「只有字符串」的兜底视图。跳转目标
|
|
29177
|
+
* 记的是**标题行在全文里的字符偏移**,渲染时不改正文 DOM(标注高亮按同样的偏移
|
|
29178
|
+
* 切分,插锚点会打乱它),跳的时候再按偏移找回文字节点。
|
|
29179
|
+
*/
|
|
29180
|
+
function parseMarkdownHeadings(text) {
|
|
29181
|
+
const levels = [];
|
|
29182
|
+
const titles = [];
|
|
29183
|
+
const offsets = [];
|
|
29184
|
+
let offset = 0;
|
|
29185
|
+
let fenced = false;
|
|
29186
|
+
for (const line of text.split("\n")) {
|
|
29187
|
+
if (/^\s*(?:```|~~~)/u.test(line)) fenced = !fenced;
|
|
29188
|
+
else if (!fenced && levels.length < 800) {
|
|
29189
|
+
const match = MARKDOWN_LINE.exec(line);
|
|
29190
|
+
if (match !== null) {
|
|
29191
|
+
const title = tidy(match[2]);
|
|
29192
|
+
if (title !== "") {
|
|
29193
|
+
levels.push(match[1].length);
|
|
29194
|
+
titles.push(title);
|
|
29195
|
+
offsets.push(offset);
|
|
29196
|
+
}
|
|
29197
|
+
}
|
|
29198
|
+
}
|
|
29199
|
+
offset += line.length + 1;
|
|
29200
|
+
}
|
|
29201
|
+
const depths = headingDepths(levels);
|
|
29202
|
+
return titles.map((title, index) => ({
|
|
29203
|
+
title,
|
|
29204
|
+
depth: depths[index],
|
|
29205
|
+
target: anchorTarget(offsets[index])
|
|
29206
|
+
}));
|
|
29207
|
+
}
|
|
29039
29208
|
//#endregion
|
|
29040
29209
|
//#region src/client/viewer/pdfjs.ts
|
|
29041
29210
|
/**
|
|
@@ -29067,10 +29236,14 @@ const PDF_CSS = `
|
|
|
29067
29236
|
.dsh-zotero-pdf-pages {
|
|
29068
29237
|
display: flex;
|
|
29069
29238
|
flex-direction: column;
|
|
29070
|
-
align-items:
|
|
29239
|
+
align-items: flex-start;
|
|
29071
29240
|
gap: 12px;
|
|
29072
29241
|
padding: 12px;
|
|
29073
|
-
|
|
29242
|
+
/* 宽度固定为可视区:横向页只向右溢出形成滚动条,不能让它把窄页一起推到
|
|
29243
|
+
容器中间(混排文档里会出现一侧几百像素的灰边)。每页靠 JS 算的
|
|
29244
|
+
margin-left 在自己的可视区里居中,宽页则贴着左内边距。 */
|
|
29245
|
+
box-sizing: border-box;
|
|
29246
|
+
width: 100%;
|
|
29074
29247
|
}
|
|
29075
29248
|
.dsh-zotero-pdf-page {
|
|
29076
29249
|
position: relative;
|
|
@@ -29576,6 +29749,10 @@ function useReadPosition(attachmentKey) {
|
|
|
29576
29749
|
* 分片节奏由 `renderQueue.paceChunk` 接管(滚动中远处的页等停手、近处的页隔帧),
|
|
29577
29750
|
* 文本层与选区框校正等停稳后才做,滚动期间文本层直接不上屏(字是透明的,撤掉
|
|
29578
29751
|
* 不影响画面);页码定位读缓存的页面 top 数组,不在滚动帧里读 `offsetTop`。
|
|
29752
|
+
*
|
|
29753
|
+
* 缩放按「页号 + 页内相对坐标」锚定视口中心那个文档点,倍率改完再把它放回中心,
|
|
29754
|
+
* 横竖两轴都不跳;之所以不用 scrollHeight 的比例,是因为同一份 PDF 里可能混着
|
|
29755
|
+
* 竖排和横排页,总高并不随倍率等比例变化。
|
|
29579
29756
|
*/
|
|
29580
29757
|
const MIN_SCALE = .5;
|
|
29581
29758
|
const MAX_SCALE = 3;
|
|
@@ -29589,6 +29766,8 @@ const ZOOM_STEPS = [
|
|
|
29589
29766
|
2,
|
|
29590
29767
|
3
|
|
29591
29768
|
];
|
|
29769
|
+
/** 页面容器左右内边距(与 PDF_CSS 的 padding 一致),适应宽度和水平居中都按它算。 */
|
|
29770
|
+
const GUTTER = 12;
|
|
29592
29771
|
const FLASH_MS$1 = 1600;
|
|
29593
29772
|
/** 停手这么久就把文本层放回屏幕(比绘制让路的判定短:字要尽早能选)。 */
|
|
29594
29773
|
const SCROLLING_FLAG_MS = 140;
|
|
@@ -29600,9 +29779,6 @@ const RETAIN_MARGIN = "300% 0px";
|
|
|
29600
29779
|
const MAX_CANVAS_PIXELS = 5242880;
|
|
29601
29780
|
const EMPTY_PAGES = /* @__PURE__ */ new Set();
|
|
29602
29781
|
const EMPTY_ANNOTATIONS = [];
|
|
29603
|
-
/** 目录条目数与层级的硬上限:个别 PDF 的书签能上千条,面板画不完也没人看得完。 */
|
|
29604
|
-
const OUTLINE_MAX_ITEMS = 800;
|
|
29605
|
-
const OUTLINE_MAX_DEPTH = 8;
|
|
29606
29782
|
function errorText$1(error) {
|
|
29607
29783
|
return error instanceof Error ? error.message : String(error);
|
|
29608
29784
|
}
|
|
@@ -29625,7 +29801,8 @@ function tidyLoadError(message) {
|
|
|
29625
29801
|
*
|
|
29626
29802
|
* `/api/file` 成功时发的是二进制流,失败时 pdf.js 只拿得到状态码,于是界面上
|
|
29627
29803
|
* 只剩一句看不懂的 404。这里补一次 `/api/attachment-info` 探测:
|
|
29628
|
-
* - 探测失败 → 那条错误消息就是真相(「附件文件不存在:/path
|
|
29804
|
+
* - 探测失败 → 那条错误消息就是真相(「附件文件不存在:/path」「没有权限读取」),
|
|
29805
|
+
* 能一键修的错误还会带上 `fix`;
|
|
29629
29806
|
* - 探测成功 → 文件本身读得到,这次 404 是瞬时的(Zotero 重启、正在重建索引),
|
|
29630
29807
|
* 提示重试比解释原因有用。
|
|
29631
29808
|
*
|
|
@@ -29636,10 +29813,14 @@ async function explainLoadFailure(error, key, t, signal) {
|
|
|
29636
29813
|
if (status === void 0 || key === "") return null;
|
|
29637
29814
|
try {
|
|
29638
29815
|
const info = await fetchAttachmentInfo(key, signal);
|
|
29639
|
-
if (!info.servable) return tpl(t("viewer.unsupported"), { type: info.contentType || info.filename });
|
|
29640
|
-
return tpl(t("viewer.serverResponse"), { status });
|
|
29816
|
+
if (!info.servable) return { text: tpl(t("viewer.unsupported"), { type: info.contentType || info.filename }) };
|
|
29817
|
+
return { text: tpl(t("viewer.serverResponse"), { status }) };
|
|
29641
29818
|
} catch (probeError) {
|
|
29642
|
-
|
|
29819
|
+
const fix = probeError instanceof ApiFailure ? probeError.fix : void 0;
|
|
29820
|
+
return {
|
|
29821
|
+
text: tpl(t("viewer.attachmentUnavailable"), { message: tidyLoadError(errorText$1(probeError)) }),
|
|
29822
|
+
...fix === void 0 ? {} : { fix }
|
|
29823
|
+
};
|
|
29643
29824
|
}
|
|
29644
29825
|
}
|
|
29645
29826
|
/**
|
|
@@ -29683,14 +29864,15 @@ async function readOutline(pdf) {
|
|
|
29683
29864
|
const out = [];
|
|
29684
29865
|
const walk = async (nodes, depth) => {
|
|
29685
29866
|
for (const node of nodes) {
|
|
29686
|
-
if (out.length >=
|
|
29867
|
+
if (out.length >= 800) return;
|
|
29868
|
+
const page = await outlinePageOf(pdf, node.dest);
|
|
29687
29869
|
out.push({
|
|
29688
29870
|
title: (node.title ?? "").trim() || "(无标题)",
|
|
29689
|
-
page:
|
|
29871
|
+
target: page === null ? null : pageTarget(page),
|
|
29690
29872
|
depth
|
|
29691
29873
|
});
|
|
29692
29874
|
const children = node.items;
|
|
29693
|
-
if (depth + 1 <
|
|
29875
|
+
if (depth + 1 < 8 && Array.isArray(children) && children.length > 0) await walk(children, depth + 1);
|
|
29694
29876
|
}
|
|
29695
29877
|
};
|
|
29696
29878
|
await walk(tree, 0);
|
|
@@ -29810,7 +29992,7 @@ function moveLineBreaksOffscreen(container) {
|
|
|
29810
29992
|
br.replaceWith(holder);
|
|
29811
29993
|
}
|
|
29812
29994
|
}
|
|
29813
|
-
const PdfPage = (0, react.memo)(function PdfPage({ label, pdf, pageNumber, scale, active, retained, estimateWidth, estimateHeight, annotations, flashKey, onAnnotationClick, priorityOf }) {
|
|
29995
|
+
const PdfPage = (0, react.memo)(function PdfPage({ label, pdf, pageNumber, scale, active, retained, estimateWidth, estimateHeight, containerWidth, annotations, flashKey, onAnnotationClick, priorityOf }) {
|
|
29814
29996
|
const wrapRef = (0, react.useRef)(null);
|
|
29815
29997
|
const hostRef = (0, react.useRef)(null);
|
|
29816
29998
|
const textRef = (0, react.useRef)(null);
|
|
@@ -29966,9 +30148,12 @@ const PdfPage = (0, react.memo)(function PdfPage({ label, pdf, pageNumber, scale
|
|
|
29966
30148
|
const annotation = hitTest(event.clientX, event.clientY);
|
|
29967
30149
|
if (annotation !== null) onAnnotationClick(annotation, event.clientX, event.clientY);
|
|
29968
30150
|
}
|
|
30151
|
+
const pageWidth = viewport?.width ?? estimateWidth;
|
|
30152
|
+
const leftInset = Math.max(0, (containerWidth - pageWidth) / 2 - GUTTER);
|
|
29969
30153
|
const pageStyle = {
|
|
29970
|
-
width:
|
|
30154
|
+
width: pageWidth,
|
|
29971
30155
|
height: viewport?.height ?? estimateHeight,
|
|
30156
|
+
marginLeft: leftInset,
|
|
29972
30157
|
"--total-scale-factor": scale,
|
|
29973
30158
|
"--scale-round-x": "1px",
|
|
29974
30159
|
"--scale-round-y": "1px"
|
|
@@ -30025,7 +30210,10 @@ function PdfViewer(props) {
|
|
|
30025
30210
|
const rootRef = (0, react.useRef)(null);
|
|
30026
30211
|
const scrollRef = (0, react.useRef)(null);
|
|
30027
30212
|
const cardRef = (0, react.useRef)(null);
|
|
30028
|
-
|
|
30213
|
+
/** 每次缩放前记下的锚点,由 useLayoutEffect 在新布局里消费一次。 */
|
|
30214
|
+
const zoomAnchorRef = (0, react.useRef)(null);
|
|
30215
|
+
/** 即使倍率没变(已到上限、适应宽度时本来就相等)也要消费掉锚点,所以用计数强制跑一次。 */
|
|
30216
|
+
const [zoomTick, setZoomTick] = (0, react.useState)(0);
|
|
30029
30217
|
const rafRef = (0, react.useRef)(null);
|
|
30030
30218
|
const flashTimerRef = (0, react.useRef)(null);
|
|
30031
30219
|
/** 「正在滚动」标记的定时器:停手一会儿后把文本层放回屏幕上。 */
|
|
@@ -30131,7 +30319,7 @@ function PdfViewer(props) {
|
|
|
30131
30319
|
});
|
|
30132
30320
|
}).catch((loadError) => {
|
|
30133
30321
|
if (disposed) return;
|
|
30134
|
-
setError(tpl(t("viewer.failed"), { message: tidyLoadError(errorText$1(loadError)) }));
|
|
30322
|
+
setError({ text: tpl(t("viewer.failed"), { message: tidyLoadError(errorText$1(loadError)) }) });
|
|
30135
30323
|
setLoading(false);
|
|
30136
30324
|
outlineRef.current?.([]);
|
|
30137
30325
|
explainLoadFailure(loadError, key, t, probe.signal).then((reason) => {
|
|
@@ -30253,10 +30441,51 @@ function PdfViewer(props) {
|
|
|
30253
30441
|
pageCount,
|
|
30254
30442
|
notePage
|
|
30255
30443
|
]);
|
|
30256
|
-
|
|
30444
|
+
/**
|
|
30445
|
+
* 改倍率前先固定屏幕上 (x, y) 这一点的文档位置。
|
|
30446
|
+
*
|
|
30447
|
+
* 只记「第几页 + 页内相对坐标」:混排 PDF 里各页高宽不同,按 scrollHeight
|
|
30448
|
+
* 比例还原会偏;按页记则不管页怎么变,同一个字仍落在同一个屏幕位置。
|
|
30449
|
+
* 点落在页间空隙或灰边上时就归到最近的一页,比例允许超出 0..1。
|
|
30450
|
+
*/
|
|
30451
|
+
const captureZoomAnchor = (0, react.useCallback)((x, y) => {
|
|
30452
|
+
const element = scrollRef.current;
|
|
30453
|
+
const pages = element?.firstElementChild;
|
|
30454
|
+
if (element === null || element === void 0 || pages === null || pages === void 0) return;
|
|
30455
|
+
const count = pages.children.length;
|
|
30456
|
+
if (count === 0) return;
|
|
30457
|
+
const docX = element.scrollLeft + x;
|
|
30458
|
+
const docY = element.scrollTop + y;
|
|
30459
|
+
let index = 0;
|
|
30460
|
+
for (let i = 0; i < count; i += 1) {
|
|
30461
|
+
const child = pages.children[i];
|
|
30462
|
+
if (docY <= child.offsetTop + child.offsetHeight) {
|
|
30463
|
+
index = i;
|
|
30464
|
+
break;
|
|
30465
|
+
}
|
|
30466
|
+
index = i;
|
|
30467
|
+
}
|
|
30468
|
+
const page = pages.children[index];
|
|
30469
|
+
const top = page.offsetTop;
|
|
30470
|
+
const width = page.offsetWidth;
|
|
30471
|
+
const height = page.offsetHeight;
|
|
30472
|
+
if (width <= 0 || height <= 0) return;
|
|
30473
|
+
zoomAnchorRef.current = {
|
|
30474
|
+
page: index + 1,
|
|
30475
|
+
fx: (docX - page.offsetLeft) / width,
|
|
30476
|
+
fy: (docY - top) / height,
|
|
30477
|
+
x,
|
|
30478
|
+
y
|
|
30479
|
+
};
|
|
30480
|
+
setZoomTick((value) => value + 1);
|
|
30481
|
+
}, []);
|
|
30482
|
+
/** 按钮 / 命令行缩放按视口中心锚定;Ctrl+滚轮按鼠标位置锚定。 */
|
|
30483
|
+
const applyZoom = (0, react.useCallback)((next, at) => {
|
|
30484
|
+
const element = scrollRef.current;
|
|
30485
|
+
if (element !== null) captureZoomAnchor(at?.x ?? element.clientWidth / 2, at?.y ?? element.clientHeight / 2);
|
|
30257
30486
|
setFitWidth(false);
|
|
30258
30487
|
setManualScale(clampScale(next));
|
|
30259
|
-
};
|
|
30488
|
+
}, [captureZoomAnchor]);
|
|
30260
30489
|
const scaleRef = (0, react.useRef)(scale);
|
|
30261
30490
|
scaleRef.current = scale;
|
|
30262
30491
|
(0, react.useEffect)(() => {
|
|
@@ -30266,25 +30495,31 @@ function PdfViewer(props) {
|
|
|
30266
30495
|
if (!(event.ctrlKey || event.metaKey)) return;
|
|
30267
30496
|
event.preventDefault();
|
|
30268
30497
|
const rect = element.getBoundingClientRect();
|
|
30269
|
-
const y = event.clientY - rect.top;
|
|
30270
|
-
anchorRef.current = {
|
|
30271
|
-
ratio: element.scrollHeight > 0 ? (element.scrollTop + y) / element.scrollHeight : 0,
|
|
30272
|
-
y
|
|
30273
|
-
};
|
|
30274
30498
|
const factor = event.deltaY < 0 ? 1.1 : 1 / 1.1;
|
|
30275
|
-
|
|
30276
|
-
|
|
30499
|
+
applyZoom(scaleRef.current * factor, {
|
|
30500
|
+
x: Math.min(Math.max(event.clientX - rect.left, 0), element.clientWidth),
|
|
30501
|
+
y: Math.min(Math.max(event.clientY - rect.top, 0), element.clientHeight)
|
|
30502
|
+
});
|
|
30277
30503
|
};
|
|
30278
30504
|
element.addEventListener("wheel", onWheel, { passive: false });
|
|
30279
30505
|
return () => element.removeEventListener("wheel", onWheel);
|
|
30280
|
-
}, []);
|
|
30506
|
+
}, [applyZoom]);
|
|
30507
|
+
/** 布局已按新倍率提交:把锚点那个文档点放回原来的屏幕位置,横竖两轴一起调。 */
|
|
30281
30508
|
(0, react.useLayoutEffect)(() => {
|
|
30282
|
-
const anchor =
|
|
30509
|
+
const anchor = zoomAnchorRef.current;
|
|
30283
30510
|
const element = scrollRef.current;
|
|
30284
30511
|
if (anchor === null || element === null) return;
|
|
30285
|
-
|
|
30286
|
-
|
|
30287
|
-
|
|
30512
|
+
zoomAnchorRef.current = null;
|
|
30513
|
+
topsRef.current = null;
|
|
30514
|
+
const target = element.querySelector(`[data-zp-page="${anchor.page}"]`);
|
|
30515
|
+
if (target === null) return;
|
|
30516
|
+
element.scrollLeft = Math.max(0, target.offsetLeft + anchor.fx * target.offsetWidth - anchor.x);
|
|
30517
|
+
element.scrollTop = Math.max(0, target.offsetTop + anchor.fy * target.offsetHeight - anchor.y);
|
|
30518
|
+
}, [
|
|
30519
|
+
zoomTick,
|
|
30520
|
+
scale,
|
|
30521
|
+
fitWidth
|
|
30522
|
+
]);
|
|
30288
30523
|
const annotationsByPage = (0, react.useMemo)(() => {
|
|
30289
30524
|
const map = /* @__PURE__ */ new Map();
|
|
30290
30525
|
for (const annotation of annotations) {
|
|
@@ -30523,10 +30758,17 @@ function PdfViewer(props) {
|
|
|
30523
30758
|
locate,
|
|
30524
30759
|
currentPage: () => currentPageRef.current,
|
|
30525
30760
|
goToPage,
|
|
30761
|
+
goToOutline: (target) => {
|
|
30762
|
+
if (target?.kind === "page") goToPage(target.page);
|
|
30763
|
+
},
|
|
30526
30764
|
zoomIn,
|
|
30527
30765
|
zoomOut,
|
|
30528
30766
|
setZoom: (percent) => applyZoom(percent / 100),
|
|
30529
|
-
fitToWidth: () =>
|
|
30767
|
+
fitToWidth: () => {
|
|
30768
|
+
const element = scrollRef.current;
|
|
30769
|
+
if (element !== null) captureZoomAnchor(element.clientWidth / 2, element.clientHeight / 2);
|
|
30770
|
+
setFitWidth(true);
|
|
30771
|
+
}
|
|
30530
30772
|
}));
|
|
30531
30773
|
(0, react.useEffect)(() => () => {
|
|
30532
30774
|
if (rafRef.current !== null) window.cancelAnimationFrame(rafRef.current);
|
|
@@ -30585,6 +30827,7 @@ function PdfViewer(props) {
|
|
|
30585
30827
|
retained: retainedPages.has(index + 1),
|
|
30586
30828
|
estimateWidth,
|
|
30587
30829
|
estimateHeight,
|
|
30830
|
+
containerWidth,
|
|
30588
30831
|
annotations: annotationsByPage.get(index) ?? EMPTY_ANNOTATIONS,
|
|
30589
30832
|
flashKey: index === flashPage ? flashKey : null,
|
|
30590
30833
|
onAnnotationClick,
|
|
@@ -30600,29 +30843,46 @@ function PdfViewer(props) {
|
|
|
30600
30843
|
style: hintOverlayStyle,
|
|
30601
30844
|
children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
|
|
30602
30845
|
style: {
|
|
30603
|
-
maxWidth:
|
|
30846
|
+
maxWidth: 300,
|
|
30604
30847
|
textAlign: "center"
|
|
30605
30848
|
},
|
|
30606
30849
|
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", {
|
|
30607
|
-
style: {
|
|
30608
|
-
|
|
30850
|
+
style: {
|
|
30851
|
+
margin: "0 0 8px",
|
|
30852
|
+
lineHeight: 1.5
|
|
30853
|
+
},
|
|
30854
|
+
children: error.text
|
|
30609
30855
|
}), /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
|
|
30610
30856
|
style: {
|
|
30611
30857
|
display: "flex",
|
|
30612
30858
|
gap: 6,
|
|
30613
|
-
justifyContent: "center"
|
|
30859
|
+
justifyContent: "center",
|
|
30860
|
+
flexWrap: "wrap"
|
|
30614
30861
|
},
|
|
30615
|
-
children: [
|
|
30616
|
-
|
|
30617
|
-
|
|
30618
|
-
|
|
30619
|
-
|
|
30620
|
-
|
|
30621
|
-
|
|
30622
|
-
|
|
30623
|
-
|
|
30624
|
-
|
|
30625
|
-
|
|
30862
|
+
children: [
|
|
30863
|
+
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
|
|
30864
|
+
type: "button",
|
|
30865
|
+
onClick: () => setRetry((value) => value + 1),
|
|
30866
|
+
style: smallButtonStyle,
|
|
30867
|
+
children: t("action.retry")
|
|
30868
|
+
}),
|
|
30869
|
+
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
|
|
30870
|
+
type: "button",
|
|
30871
|
+
onClick: () => onOpenInZotero(1),
|
|
30872
|
+
style: smallButtonStyle,
|
|
30873
|
+
children: t("viewer.openInZotero")
|
|
30874
|
+
}),
|
|
30875
|
+
error.fix !== void 0 && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
|
|
30876
|
+
type: "button",
|
|
30877
|
+
onClick: () => {
|
|
30878
|
+
openSystemSettings(error.fix).catch((reason) => {
|
|
30879
|
+
showToast(tpl(t("viewer.openSettingsFailed"), { message: errorText$1(reason) }));
|
|
30880
|
+
});
|
|
30881
|
+
},
|
|
30882
|
+
style: smallButtonStyle,
|
|
30883
|
+
children: t("viewer.openPrivacySettings")
|
|
30884
|
+
})
|
|
30885
|
+
]
|
|
30626
30886
|
})]
|
|
30627
30887
|
})
|
|
30628
30888
|
}),
|
|
@@ -30689,6 +30949,7 @@ const smallButtonStyle = {
|
|
|
30689
30949
|
const SNAPSHOT_CHANNEL = "dsh-zotero/snapshot";
|
|
30690
30950
|
const EVENT_TYPES = /* @__PURE__ */ new Set([
|
|
30691
30951
|
"ready",
|
|
30952
|
+
"outline",
|
|
30692
30953
|
"applied",
|
|
30693
30954
|
"selection",
|
|
30694
30955
|
"locate-result",
|
|
@@ -30726,6 +30987,9 @@ function commandEnvelope(command) {
|
|
|
30726
30987
|
*
|
|
30727
30988
|
* `annotations` 用一份轻量签名做依赖:`onChanged()` 之后详情会整份重取,
|
|
30728
30989
|
* 数组引用每次都变;按签名判断可以避免无关重渲染也往 iframe 里推一遍。
|
|
30990
|
+
*
|
|
30991
|
+
* 网页标题目录也走这座桥:iframe 里扫 h1–h6 打锚点后上报 `outline`,本组件转成
|
|
30992
|
+
* 统一目录项交给左上角浮层;点目录项发 `scroll-to-outline` 让 iframe 自己滚。
|
|
30729
30993
|
*/
|
|
30730
30994
|
/** 桥接脚本一般几百毫秒就绪;超过这个时间就认定它没跑起来。 */
|
|
30731
30995
|
const BRIDGE_TIMEOUT_MS = 8e3;
|
|
@@ -30746,6 +31010,28 @@ function itemsOf(annotations) {
|
|
|
30746
31010
|
}
|
|
30747
31011
|
return items;
|
|
30748
31012
|
}
|
|
31013
|
+
/**
|
|
31014
|
+
* iframe 报上来的标题目录 → 统一目录项。
|
|
31015
|
+
*
|
|
31016
|
+
* 消息来自沙箱里的页面脚本,虽然只能伪造文字也不能写库,还是逐字段收窄一遍:
|
|
31017
|
+
* 标题与锚点必须是字符串、层级夹到面板画得出来的范围、条数封顶。
|
|
31018
|
+
*/
|
|
31019
|
+
function outlineOf(raw) {
|
|
31020
|
+
const items = [];
|
|
31021
|
+
for (const entry of raw) {
|
|
31022
|
+
if (items.length >= 800) break;
|
|
31023
|
+
if (typeof entry?.title !== "string" || typeof entry?.id !== "string") continue;
|
|
31024
|
+
const title = entry.title.trim();
|
|
31025
|
+
if (title === "") continue;
|
|
31026
|
+
const depth = typeof entry.depth === "number" && Number.isFinite(entry.depth) ? Math.max(0, Math.min(7, Math.trunc(entry.depth))) : 0;
|
|
31027
|
+
items.push({
|
|
31028
|
+
title,
|
|
31029
|
+
depth,
|
|
31030
|
+
target: anchorTarget(entry.id)
|
|
31031
|
+
});
|
|
31032
|
+
}
|
|
31033
|
+
return items;
|
|
31034
|
+
}
|
|
30749
31035
|
/** 只把 http(s) 交给系统:快照里的 `javascript:` / `file:` 一律丢弃。 */
|
|
30750
31036
|
function openExternal(href) {
|
|
30751
31037
|
let url;
|
|
@@ -30757,7 +31043,7 @@ function openExternal(href) {
|
|
|
30757
31043
|
if (url.protocol !== "http:" && url.protocol !== "https:") return;
|
|
30758
31044
|
window.open(url.href, "_blank", "noopener,noreferrer");
|
|
30759
31045
|
}
|
|
30760
|
-
function SnapshotViewer({ t, detail, source, annotations, viewerRef, onReady, onInsertExcerpt, onInsertAnnotation, onOpenInZotero, onChanged }) {
|
|
31046
|
+
function SnapshotViewer({ t, detail, source, annotations, viewerRef, onReady, onInsertExcerpt, onInsertAnnotation, onOpenInZotero, onChanged, onOutlineChange }) {
|
|
30761
31047
|
const rootRef = (0, react.useRef)(null);
|
|
30762
31048
|
const frameRef = (0, react.useRef)(null);
|
|
30763
31049
|
const cardRef = (0, react.useRef)(null);
|
|
@@ -30766,6 +31052,9 @@ function SnapshotViewer({ t, detail, source, annotations, viewerRef, onReady, on
|
|
|
30766
31052
|
const selectionRef = (0, react.useRef)(null);
|
|
30767
31053
|
const annotationsRef = (0, react.useRef)(annotations);
|
|
30768
31054
|
annotationsRef.current = annotations;
|
|
31055
|
+
/** 目录回调走 ref:message 监听只该挂一次,不该因父级重渲染换了回调身份就重挂。 */
|
|
31056
|
+
const outlineRef = (0, react.useRef)(onOutlineChange);
|
|
31057
|
+
outlineRef.current = onOutlineChange;
|
|
30769
31058
|
const [reloadKey, setReloadKey] = (0, react.useState)(0);
|
|
30770
31059
|
const [bridgeReady, setBridgeReady] = (0, react.useState)(false);
|
|
30771
31060
|
const [bridgeMissing, setBridgeMissing] = (0, react.useState)(false);
|
|
@@ -30849,13 +31138,19 @@ function SnapshotViewer({ t, detail, source, annotations, viewerRef, onReady, on
|
|
|
30849
31138
|
if (data.type === "ready") {
|
|
30850
31139
|
readyRef.current = true;
|
|
30851
31140
|
setBridgeReady(true);
|
|
30852
|
-
|
|
31141
|
+
const bridge = data.bridge ?? 1;
|
|
31142
|
+
setStaleBridge(bridge < 3);
|
|
30853
31143
|
setBridgeMissing(false);
|
|
31144
|
+
if (bridge < 3) outlineRef.current?.([]);
|
|
30854
31145
|
pushAnnotations();
|
|
30855
31146
|
flushLocate();
|
|
30856
31147
|
onReady();
|
|
30857
31148
|
return;
|
|
30858
31149
|
}
|
|
31150
|
+
if (data.type === "outline") {
|
|
31151
|
+
outlineRef.current?.(outlineOf(Array.isArray(data.items) ? data.items : []));
|
|
31152
|
+
return;
|
|
31153
|
+
}
|
|
30859
31154
|
if (data.type === "applied") {
|
|
30860
31155
|
setMissed(typeof data.error === "string" ? data.total : Math.max(0, data.total - data.resolved));
|
|
30861
31156
|
flushLocate();
|
|
@@ -30888,6 +31183,7 @@ function SnapshotViewer({ t, detail, source, annotations, viewerRef, onReady, on
|
|
|
30888
31183
|
const timer = window.setTimeout(() => {
|
|
30889
31184
|
if (readyRef.current) return;
|
|
30890
31185
|
setBridgeMissing(true);
|
|
31186
|
+
outlineRef.current?.([]);
|
|
30891
31187
|
}, BRIDGE_TIMEOUT_MS);
|
|
30892
31188
|
return () => window.clearTimeout(timer);
|
|
30893
31189
|
}, [reloadKey]);
|
|
@@ -30908,6 +31204,12 @@ function SnapshotViewer({ t, detail, source, annotations, viewerRef, onReady, on
|
|
|
30908
31204
|
locate,
|
|
30909
31205
|
currentPage: () => 1,
|
|
30910
31206
|
goToPage: () => void 0,
|
|
31207
|
+
goToOutline: (target) => {
|
|
31208
|
+
if (target?.kind === "anchor") post({
|
|
31209
|
+
type: "scroll-to-outline",
|
|
31210
|
+
id: target.id
|
|
31211
|
+
});
|
|
31212
|
+
},
|
|
30911
31213
|
zoomIn: () => void 0,
|
|
30912
31214
|
zoomOut: () => void 0,
|
|
30913
31215
|
setZoom: () => void 0,
|
|
@@ -31102,15 +31404,50 @@ const retryStyle = {
|
|
|
31102
31404
|
//#endregion
|
|
31103
31405
|
//#region src/client/viewer/TextViewer.tsx
|
|
31104
31406
|
/**
|
|
31105
|
-
*
|
|
31407
|
+
* 文本渲染器:独立笔记 / Markdown 页面、PDF 全文或其它附件的纯文本兜底视图。
|
|
31106
31408
|
*
|
|
31107
31409
|
* PDF.js 不可用(非 PDF 附件、浏览器拒绝渲染、文件缺失)时由注册表回退到这里。
|
|
31108
31410
|
* 标注只有 `range`(全文区间)能落到文字上;点标注列表仍会滚动定位并闪烁。
|
|
31109
31411
|
* 该渲染器没有分页与缩放,工具条据 `capabilities` 自动隐藏对应控件。
|
|
31412
|
+
*
|
|
31413
|
+
* 目录:笔记按 HTML 标题(`<h1>`–`<h6>`,或写成 Markdown 的 `<p># 标题</p>`)解析,
|
|
31414
|
+
* 纯文本按 `#` 标题行解析,都上报给左上角那一份统一目录浮层。笔记用元素锚点定位,
|
|
31415
|
+
* 纯文本用字符偏移定位——后者不能往正文里插锚点,否则按同样偏移切分的标注会错位。
|
|
31110
31416
|
*/
|
|
31111
31417
|
const FLASH_CLASS = "dsh-zotero-ann-flash";
|
|
31112
31418
|
const FLASH_MS = 1200;
|
|
31419
|
+
const OUTLINE_FLASH_CLASS = "dsh-zp-outline-flash";
|
|
31113
31420
|
const TEXT_TYPES = /* @__PURE__ */ new Set(["highlight", "underline"]);
|
|
31421
|
+
/** 把视口里的某个 y 坐标滚到滚动容器顶部偏下一点的位置。 */
|
|
31422
|
+
function scrollBoxTo(box, viewportTop) {
|
|
31423
|
+
const boxTop = box.getBoundingClientRect().top;
|
|
31424
|
+
box.scrollTo({
|
|
31425
|
+
top: Math.max(0, box.scrollTop + viewportTop - boxTop - 8),
|
|
31426
|
+
behavior: "smooth"
|
|
31427
|
+
});
|
|
31428
|
+
}
|
|
31429
|
+
/**
|
|
31430
|
+
* 全文字符偏移 → 文字节点内偏移。
|
|
31431
|
+
*
|
|
31432
|
+
* 正文被标注切片包成 `<mark>` / `<span>`,但 `textContent` 与 `fulltext.content`
|
|
31433
|
+
* 完全一致,所以按文档顺序累加文字节点长度就能换回去。只遍历 `contentRef` 这一层,
|
|
31434
|
+
* 不含「全文索引兜底」等提示文字。
|
|
31435
|
+
*/
|
|
31436
|
+
function textNodeAtOffset(root, offset) {
|
|
31437
|
+
const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
|
|
31438
|
+
let consumed = 0;
|
|
31439
|
+
let current = walker.nextNode();
|
|
31440
|
+
while (current !== null) {
|
|
31441
|
+
const length = current.nodeValue?.length ?? 0;
|
|
31442
|
+
if (offset <= consumed + length) return {
|
|
31443
|
+
node: current,
|
|
31444
|
+
local: offset - consumed
|
|
31445
|
+
};
|
|
31446
|
+
consumed += length;
|
|
31447
|
+
current = walker.nextNode();
|
|
31448
|
+
}
|
|
31449
|
+
return null;
|
|
31450
|
+
}
|
|
31114
31451
|
const AnnotatedText = (0, react.memo)(function AnnotatedText({ segments }) {
|
|
31115
31452
|
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_jsx_runtime.Fragment, { children: segments.map((segment, index) => {
|
|
31116
31453
|
if (segment.kind === "text") return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react.Fragment, { children: segment.text }, index);
|
|
@@ -31136,14 +31473,26 @@ const AnnotatedText = (0, react.memo)(function AnnotatedText({ segments }) {
|
|
|
31136
31473
|
}, index);
|
|
31137
31474
|
}) });
|
|
31138
31475
|
});
|
|
31139
|
-
function TextViewer({ t, detail, annotations, viewerRef, onReady, onInsertExcerpt }) {
|
|
31476
|
+
function TextViewer({ t, detail, annotations, viewerRef, onReady, onInsertExcerpt, onOutlineChange }) {
|
|
31140
31477
|
const boxRef = (0, react.useRef)(null);
|
|
31478
|
+
/** 纯文本正文的内层:字符偏移只在这层里数,排除上下两条提示文字。 */
|
|
31479
|
+
const contentRef = (0, react.useRef)(null);
|
|
31141
31480
|
const flashTimer = (0, react.useRef)(null);
|
|
31481
|
+
const outlineTimer = (0, react.useRef)(null);
|
|
31142
31482
|
const [selection, setSelection] = (0, react.useState)(null);
|
|
31143
31483
|
const selfNote = detail.itemType === "note" ? detail.notes.find((note) => note.key === detail.key) ?? null : null;
|
|
31144
31484
|
const fulltext = detail.fulltext;
|
|
31145
31485
|
const marked = (0, react.useMemo)(() => selfNote === null ? annotations.filter((annotation) => TEXT_TYPES.has(annotation.type)) : [], [annotations, selfNote]);
|
|
31146
31486
|
const segments = (0, react.useMemo)(() => buildSegments(fulltext?.content ?? "", marked), [fulltext, marked]);
|
|
31487
|
+
/** 笔记 HTML 加好标题锚点后再交给 `dangerouslySetInnerHTML`。 */
|
|
31488
|
+
const noteParsed = (0, react.useMemo)(() => selfNote === null ? null : parseHtmlHeadings(selfNote.html), [selfNote]);
|
|
31489
|
+
const textOutline = (0, react.useMemo)(() => fulltext === null ? [] : parseMarkdownHeadings(fulltext.content), [fulltext]);
|
|
31490
|
+
/** 目录回调走 ref:上报 effect 不该因父级重渲染换了回调身份就重跑。 */
|
|
31491
|
+
const outlineRef = (0, react.useRef)(onOutlineChange);
|
|
31492
|
+
outlineRef.current = onOutlineChange;
|
|
31493
|
+
(0, react.useEffect)(() => {
|
|
31494
|
+
outlineRef.current?.(noteParsed?.items ?? textOutline);
|
|
31495
|
+
}, [noteParsed, textOutline]);
|
|
31147
31496
|
(0, react.useEffect)(() => {
|
|
31148
31497
|
const timer = window.setTimeout(onReady, 0);
|
|
31149
31498
|
return () => window.clearTimeout(timer);
|
|
@@ -31154,6 +31503,7 @@ function TextViewer({ t, detail, annotations, viewerRef, onReady, onInsertExcerp
|
|
|
31154
31503
|
]);
|
|
31155
31504
|
(0, react.useEffect)(() => () => {
|
|
31156
31505
|
if (flashTimer.current !== null) window.clearTimeout(flashTimer.current);
|
|
31506
|
+
if (outlineTimer.current !== null) window.clearTimeout(outlineTimer.current);
|
|
31157
31507
|
}, []);
|
|
31158
31508
|
function locate(annotationKey) {
|
|
31159
31509
|
const box = boxRef.current;
|
|
@@ -31176,10 +31526,41 @@ function TextViewer({ t, detail, annotations, viewerRef, onReady, onInsertExcerp
|
|
|
31176
31526
|
}, FLASH_MS);
|
|
31177
31527
|
return true;
|
|
31178
31528
|
}
|
|
31529
|
+
function flashHeading(element) {
|
|
31530
|
+
if (outlineTimer.current !== null) window.clearTimeout(outlineTimer.current);
|
|
31531
|
+
element.classList.remove(OUTLINE_FLASH_CLASS);
|
|
31532
|
+
element.offsetWidth;
|
|
31533
|
+
element.classList.add(OUTLINE_FLASH_CLASS);
|
|
31534
|
+
outlineTimer.current = window.setTimeout(() => {
|
|
31535
|
+
element.classList.remove(OUTLINE_FLASH_CLASS);
|
|
31536
|
+
outlineTimer.current = null;
|
|
31537
|
+
}, FLASH_MS);
|
|
31538
|
+
}
|
|
31539
|
+
function goToOutline(target) {
|
|
31540
|
+
const box = boxRef.current;
|
|
31541
|
+
if (target === null || target.kind !== "anchor" || box === null) return;
|
|
31542
|
+
if (noteParsed !== null) {
|
|
31543
|
+
if (!/^\d+$/u.test(target.id)) return;
|
|
31544
|
+
const element = box.querySelector(`[${OUTLINE_ANCHOR_ATTR}="${target.id}"]`);
|
|
31545
|
+
if (element === null) return;
|
|
31546
|
+
scrollBoxTo(box, element.getBoundingClientRect().top);
|
|
31547
|
+
flashHeading(element);
|
|
31548
|
+
return;
|
|
31549
|
+
}
|
|
31550
|
+
const offset = Number.parseInt(target.id, 10);
|
|
31551
|
+
if (!Number.isFinite(offset) || contentRef.current === null) return;
|
|
31552
|
+
const hit = textNodeAtOffset(contentRef.current, offset);
|
|
31553
|
+
if (hit === null) return;
|
|
31554
|
+
const range = document.createRange();
|
|
31555
|
+
range.setStart(hit.node, Math.min(Math.max(hit.local, 0), hit.node.nodeValue?.length ?? 0));
|
|
31556
|
+
range.collapse(true);
|
|
31557
|
+
scrollBoxTo(box, range.getBoundingClientRect().top);
|
|
31558
|
+
}
|
|
31179
31559
|
(0, react.useImperativeHandle)(viewerRef, () => ({
|
|
31180
31560
|
locate,
|
|
31181
31561
|
currentPage: () => 1,
|
|
31182
31562
|
goToPage: () => void 0,
|
|
31563
|
+
goToOutline,
|
|
31183
31564
|
zoomIn: () => void 0,
|
|
31184
31565
|
zoomOut: () => void 0,
|
|
31185
31566
|
setZoom: () => void 0,
|
|
@@ -31220,7 +31601,7 @@ function TextViewer({ t, detail, annotations, viewerRef, onReady, onInsertExcerp
|
|
|
31220
31601
|
style: noteStyle,
|
|
31221
31602
|
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
|
|
31222
31603
|
className: "dsh-zotero-note",
|
|
31223
|
-
dangerouslySetInnerHTML: { __html: selfNote.html }
|
|
31604
|
+
dangerouslySetInnerHTML: { __html: noteParsed?.html ?? selfNote.html }
|
|
31224
31605
|
}), selfNote.truncated && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
|
|
31225
31606
|
style: truncatedStyle,
|
|
31226
31607
|
children: t("detail.noteTruncated")
|
|
@@ -31232,7 +31613,10 @@ function TextViewer({ t, detail, annotations, viewerRef, onReady, onInsertExcerp
|
|
|
31232
31613
|
style: fallbackStyle,
|
|
31233
31614
|
children: t("detail.fulltextFallback")
|
|
31234
31615
|
}),
|
|
31235
|
-
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(
|
|
31616
|
+
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
|
|
31617
|
+
ref: contentRef,
|
|
31618
|
+
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(AnnotatedText, { segments })
|
|
31619
|
+
}),
|
|
31236
31620
|
fulltext?.truncated === true && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
|
|
31237
31621
|
style: truncatedStyle,
|
|
31238
31622
|
children: t("detail.fulltextTruncated")
|
|
@@ -31298,6 +31682,16 @@ const TEXT_CSS = `
|
|
|
31298
31682
|
0%, 100% { outline-color: transparent; }
|
|
31299
31683
|
20%, 80% { outline-color: rgba(80,140,255,0.95); }
|
|
31300
31684
|
}
|
|
31685
|
+
.dsh-zp-outline-flash {
|
|
31686
|
+
border-radius: 3px;
|
|
31687
|
+
outline: 2px solid transparent;
|
|
31688
|
+
outline-offset: 2px;
|
|
31689
|
+
animation: dsh-zp-outline-flash 1.1s ease-in-out 1;
|
|
31690
|
+
}
|
|
31691
|
+
@keyframes dsh-zp-outline-flash {
|
|
31692
|
+
0%, 100% { outline-color: transparent; }
|
|
31693
|
+
20%, 80% { outline-color: rgba(80,140,255,0.95); }
|
|
31694
|
+
}
|
|
31301
31695
|
`;
|
|
31302
31696
|
//#endregion
|
|
31303
31697
|
//#region src/client/viewer/WebpageViewer.tsx
|
|
@@ -31402,7 +31796,8 @@ const RENDERERS = [
|
|
|
31402
31796
|
id: "snapshot",
|
|
31403
31797
|
capabilities: {
|
|
31404
31798
|
zoom: false,
|
|
31405
|
-
pageNavigation: false
|
|
31799
|
+
pageNavigation: false,
|
|
31800
|
+
outline: true
|
|
31406
31801
|
},
|
|
31407
31802
|
match: (source) => source !== null && isHtml(source),
|
|
31408
31803
|
Component: SnapshotViewer
|
|
@@ -31411,7 +31806,8 @@ const RENDERERS = [
|
|
|
31411
31806
|
id: "pdf",
|
|
31412
31807
|
capabilities: {
|
|
31413
31808
|
zoom: true,
|
|
31414
|
-
pageNavigation: true
|
|
31809
|
+
pageNavigation: true,
|
|
31810
|
+
outline: true
|
|
31415
31811
|
},
|
|
31416
31812
|
match: (source) => source !== null && isPdf(source),
|
|
31417
31813
|
Component: PdfViewer
|
|
@@ -31424,7 +31820,8 @@ const RENDERERS = [
|
|
|
31424
31820
|
id: "webpage",
|
|
31425
31821
|
capabilities: {
|
|
31426
31822
|
zoom: false,
|
|
31427
|
-
pageNavigation: false
|
|
31823
|
+
pageNavigation: false,
|
|
31824
|
+
outline: true
|
|
31428
31825
|
},
|
|
31429
31826
|
match: (source, detail) => detail.itemType === "webpage" && (source === null || !isPdf(source) && !isHtml(source)),
|
|
31430
31827
|
Component: WebpageViewer
|
|
@@ -31433,7 +31830,8 @@ const RENDERERS = [
|
|
|
31433
31830
|
id: "text",
|
|
31434
31831
|
capabilities: {
|
|
31435
31832
|
zoom: false,
|
|
31436
|
-
pageNavigation: false
|
|
31833
|
+
pageNavigation: false,
|
|
31834
|
+
outline: true
|
|
31437
31835
|
},
|
|
31438
31836
|
match: () => true,
|
|
31439
31837
|
Component: TextViewer
|
|
@@ -31762,6 +32160,14 @@ const toolButtonStyle = {
|
|
|
31762
32160
|
/** 定位重试:渲染器刚就绪时页面占位可能还没提交,最多试几次。 */
|
|
31763
32161
|
const LOCATE_RETRIES = 6;
|
|
31764
32162
|
const LOCATE_RETRY_MS = 60;
|
|
32163
|
+
/** 目录来源说明:同一份浮层服务三类文档,给用户一句口径。 */
|
|
32164
|
+
const OUTLINE_SOURCE_LABEL = {
|
|
32165
|
+
pdf: "outline.source.pdf",
|
|
32166
|
+
snapshot: "outline.source.snapshot",
|
|
32167
|
+
note: "outline.source.note",
|
|
32168
|
+
text: "outline.source.text",
|
|
32169
|
+
webpage: "outline.source.text"
|
|
32170
|
+
};
|
|
31765
32171
|
function errorMessage$1(error) {
|
|
31766
32172
|
return error instanceof Error ? error.message : String(error);
|
|
31767
32173
|
}
|
|
@@ -31781,7 +32187,7 @@ function ItemDetail({ t, detail, onInsertItem, onInsertExcerpt, onInsertAnnotati
|
|
|
31781
32187
|
const [infoOpen, setInfoOpen] = (0, react.useState)(false);
|
|
31782
32188
|
const [outlineOpen, setOutlineOpen] = (0, react.useState)(false);
|
|
31783
32189
|
const [outline, setOutline] = (0, react.useState)({
|
|
31784
|
-
|
|
32190
|
+
documentKey: "",
|
|
31785
32191
|
items: null
|
|
31786
32192
|
});
|
|
31787
32193
|
const [selectedKey, setSelectedKey] = (0, react.useState)(null);
|
|
@@ -31822,17 +32228,19 @@ function ItemDetail({ t, detail, onInsertItem, onInsertExcerpt, onInsertAnnotati
|
|
|
31822
32228
|
} : null;
|
|
31823
32229
|
/** 收起信息浮层;`InfoPanel` 内部再用 `useDismiss` 接管「点浮层外 / Esc」。 */
|
|
31824
32230
|
const closeInfo = (0, react.useCallback)(() => setInfoOpen(false), []);
|
|
31825
|
-
const
|
|
32231
|
+
const documentKey = `${detail.key}:${source?.attachmentKey ?? "self"}`;
|
|
31826
32232
|
/** 归属对不上的目录一律当「还没读到」,绝不用上一条文献的书签跳页。 */
|
|
31827
|
-
const outlineItems = outline.
|
|
32233
|
+
const outlineItems = outline.documentKey === documentKey ? outline.items : null;
|
|
31828
32234
|
const handleOutlineChange = (0, react.useCallback)((items) => {
|
|
31829
32235
|
setOutline({
|
|
31830
|
-
|
|
32236
|
+
documentKey,
|
|
31831
32237
|
items
|
|
31832
32238
|
});
|
|
31833
|
-
}, [
|
|
31834
|
-
|
|
31835
|
-
|
|
32239
|
+
}, [documentKey]);
|
|
32240
|
+
const renderer = pickRenderer(source, detail);
|
|
32241
|
+
/** 目录按钮按渲染器能力出现:PDF / 快照 / 笔记 / 全文共用左上角那一个浮层。 */
|
|
32242
|
+
const outlineAvailable = renderer.capabilities.outline;
|
|
32243
|
+
const outlineSourceLabel = t(OUTLINE_SOURCE_LABEL[detail.itemType === "note" ? "note" : renderer.id] ?? "outline.source.text");
|
|
31836
32244
|
const flushPendingLocate = (0, react.useCallback)((key, attempt) => {
|
|
31837
32245
|
if (locateTimer.current !== null) {
|
|
31838
32246
|
window.clearTimeout(locateTimer.current);
|
|
@@ -31882,13 +32290,14 @@ function ItemDetail({ t, detail, onInsertItem, onInsertExcerpt, onInsertAnnotati
|
|
|
31882
32290
|
});
|
|
31883
32291
|
}
|
|
31884
32292
|
/**
|
|
31885
|
-
*
|
|
32293
|
+
* 跳到一条目录项。
|
|
31886
32294
|
*
|
|
31887
|
-
*
|
|
32295
|
+
* 目标原样交给渲染器:PDF 按页、网页快照 / 笔记按标题锚点,面板不解释。
|
|
32296
|
+
* 跳完就收起目录:浮层压在文档左边,留着会把刚跳过去的内容挡掉一半。
|
|
31888
32297
|
*/
|
|
31889
|
-
function
|
|
32298
|
+
function jumpToOutline(item) {
|
|
31890
32299
|
setOutlineOpen(false);
|
|
31891
|
-
viewerRef.current?.
|
|
32300
|
+
if (item.target !== null) viewerRef.current?.goToOutline(item.target);
|
|
31892
32301
|
}
|
|
31893
32302
|
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
|
|
31894
32303
|
style: rootStyle,
|
|
@@ -31909,13 +32318,14 @@ function ItemDetail({ t, detail, onInsertItem, onInsertExcerpt, onInsertAnnotati
|
|
|
31909
32318
|
outlineAvailable && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(OutlinePanel, {
|
|
31910
32319
|
t,
|
|
31911
32320
|
items: outlineItems,
|
|
32321
|
+
sourceLabel: outlineSourceLabel,
|
|
31912
32322
|
open: outlineOpen,
|
|
31913
32323
|
onToggle: () => {
|
|
31914
32324
|
if (!outlineOpen) setInfoOpen(false);
|
|
31915
32325
|
setOutlineOpen(!outlineOpen);
|
|
31916
32326
|
},
|
|
31917
32327
|
onClose: () => setOutlineOpen(false),
|
|
31918
|
-
onJump:
|
|
32328
|
+
onJump: jumpToOutline
|
|
31919
32329
|
}),
|
|
31920
32330
|
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(InfoPanel, {
|
|
31921
32331
|
t,
|