dsh-plugin-zotero 0.1.8 → 0.1.9
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 +3 -2
- package/client/client.js +399 -246
- package/lib/zotero/client.d.ts +2 -0
- package/lib/zotero/client.js +58 -5
- package/lib/zotero/types.d.ts +5 -0
- package/package.json +6 -7
package/README.md
CHANGED
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
- **读文献**:读摘要、PDF 全文、笔记与标注,写综述、做对比、提取数据都行;需要精确引用时会保留原文措辞。
|
|
21
21
|
- **整理文库**:新建、重命名、移动、删除分类,批量把条目加入或移出分类。你说一句「把这几篇归到『待读』里」,它就照做。
|
|
22
22
|
- **帮你入库**:把本地下载好的论文、PDF 交给 DSH 导入 Zotero;也可以让它读完外文文献后,把译文作为附件挂回条目。
|
|
23
|
-
-
|
|
23
|
+
- **带进对话**:右侧面板单条目引用、全文划词摘录或整分类引用,一键写进当前会话草稿(不自动发送),你再接着说要求。条目和整分类以「引用胶囊」的形式出现,草稿里一眼看清引了哪几篇,单独删掉某一条也不影响其他内容。
|
|
24
24
|
- **安全可控**:浏览面板严格只读;模型能用的每一项工具都能单独开关,想彻底只读就把写操作关掉;写操作优先走集成 MCP,它在 Zotero 进程内直接执行,不触发本地 API 的授权弹窗。
|
|
25
25
|
|
|
26
26
|
面板分三块:顶部状态栏显示连接状态并提供刷新;左侧分类树按分类浏览与搜索;右侧详情区展示正文与「参考文献 / 笔记 / 标注 / 附件」标签页。
|
|
@@ -80,7 +80,8 @@ curl http://127.0.0.1:23119/api/ # 期望输出:Zotero is running
|
|
|
80
80
|
- **摘要默认折叠**:标题行右侧的箭头按钮展开 / 收起摘要,默认把空间留给正文与全文。
|
|
81
81
|
- **笔记可读**:独立笔记(文库里的全局笔记)打开后正文直接占据上方正文区,跟普通条目一样阅读;条目下的子笔记仍进「笔记」tab,以折叠列表呈现(默认收起,点击标题展开),正文保留 Zotero 的引用块、标注高亮、表格与外链图片。Host 侧已清洗脚本与事件属性,避免注入。「引用」把该条笔记转成纯文本写进会话草稿。
|
|
82
82
|
- **标注可读**:PDF 上的高亮与划线汇总进「标注」tab(带颜色、页码、原文与评论,可按类型 / 颜色筛选);全文块里原位标出底色与下划线,点列表条目可滚动定位并闪烁,「引用」把该条标注写进会话草稿。
|
|
83
|
-
-
|
|
83
|
+
- **五种插入方式**:单条目引用与整目录引用以「引用胶囊」写进草稿——胶囊只显示标题,发送那一刻才展开成完整引用文本,模型拿到的内容与以前一致;全文划词摘录、单条标注、单条笔记仍按纯文本插入。所有方式都只写进会话草稿(不自动发送、不抢焦点),文本携带 `itemKey` / `collectionKey` 供模型接续操作。
|
|
84
|
+
- **胶囊依赖本插件展开**:草稿里留着引用胶囊时禁用或卸载本插件,发送会失败并提示找不到引用来源,把胶囊删掉即可继续正常发送。
|
|
84
85
|
- **面板严格只读**:不提供任何新建 / 改名 / 删除分类的按钮。
|
|
85
86
|
- **模型侧工具可逐个开关**:插件自持连接集成 MCP,把工具以 `mcp__zotero__*` 注册进 dsh,可在插件页按工具启用 / 禁用。
|
|
86
87
|
|
package/client/client.js
CHANGED
|
@@ -37,6 +37,7 @@ const dicts = {
|
|
|
37
37
|
"status.mcpOn": "模型工具可用",
|
|
38
38
|
"status.mcpOff": "模型工具不可用",
|
|
39
39
|
"status.loading": "加载中…",
|
|
40
|
+
"item.lastRead": "最后阅读:{date}",
|
|
40
41
|
"tree.library": "我的文库",
|
|
41
42
|
"tree.publications": "我的出版物",
|
|
42
43
|
"tree.unfiled": "未分类条目",
|
|
@@ -143,7 +144,9 @@ const dicts = {
|
|
|
143
144
|
"cmp.excerpt.quote": "“{value}”",
|
|
144
145
|
"cmp.collection.header": "【Zotero 目录|{name}|{key}】共 {n} 条",
|
|
145
146
|
"cmp.collection.line": "- {title}({meta})",
|
|
146
|
-
"cmp.collection.more": "……(仅列出前 {n} 条;完整清单可用 mcp__zotero__get_collection_items 获取)"
|
|
147
|
+
"cmp.collection.more": "……(仅列出前 {n} 条;完整清单可用 mcp__zotero__get_collection_items 获取)",
|
|
148
|
+
"cmp.missing.item": "【Zotero 条目|{key}】该条目已不在文库中,引用文本未能生成。",
|
|
149
|
+
"cmp.missing.collection": "【Zotero 目录|{key}】该目录已不存在,条目清单未能生成。"
|
|
147
150
|
},
|
|
148
151
|
en: {
|
|
149
152
|
"tab.title": "Zotero Library",
|
|
@@ -161,6 +164,7 @@ const dicts = {
|
|
|
161
164
|
"status.mcpOn": "Model tools available",
|
|
162
165
|
"status.mcpOff": "Model tools unavailable",
|
|
163
166
|
"status.loading": "Loading…",
|
|
167
|
+
"item.lastRead": "Last read: {date}",
|
|
164
168
|
"tree.library": "My Library",
|
|
165
169
|
"tree.publications": "My Publications",
|
|
166
170
|
"tree.unfiled": "Unfiled Items",
|
|
@@ -267,7 +271,9 @@ const dicts = {
|
|
|
267
271
|
"cmp.excerpt.quote": "“{value}”",
|
|
268
272
|
"cmp.collection.header": "[Zotero collection | {name} | {key}] {n} items",
|
|
269
273
|
"cmp.collection.line": "- {title} ({meta})",
|
|
270
|
-
"cmp.collection.more": "… (first {n} only; use mcp__zotero__get_collection_items for the full list)"
|
|
274
|
+
"cmp.collection.more": "… (first {n} only; use mcp__zotero__get_collection_items for the full list)",
|
|
275
|
+
"cmp.missing.item": "[Zotero item | {key}] this item is no longer in the library; citation text unavailable.",
|
|
276
|
+
"cmp.missing.collection": "[Zotero collection | {key}] this collection no longer exists; item list unavailable."
|
|
271
277
|
}
|
|
272
278
|
};
|
|
273
279
|
//#endregion
|
|
@@ -288,10 +294,10 @@ function buildUrl(action, query = {}) {
|
|
|
288
294
|
const search = params.toString();
|
|
289
295
|
return `${BASE}/${action}${search === "" ? "" : `?${search}`}`;
|
|
290
296
|
}
|
|
291
|
-
async function get(action, query = {}) {
|
|
297
|
+
async function get(action, query = {}, signal) {
|
|
292
298
|
let res;
|
|
293
299
|
try {
|
|
294
|
-
res = await fetch(buildUrl(action, query));
|
|
300
|
+
res = await fetch(buildUrl(action, query), { signal });
|
|
295
301
|
} catch (error) {
|
|
296
302
|
throw new ApiFailure("network_error", `无法访问 Host 端点:${error instanceof Error ? error.message : String(error)}`);
|
|
297
303
|
}
|
|
@@ -303,17 +309,17 @@ async function get(action, query = {}) {
|
|
|
303
309
|
function fetchHealth() {
|
|
304
310
|
return get("health");
|
|
305
311
|
}
|
|
306
|
-
function fetchTree() {
|
|
307
|
-
return get("tree");
|
|
312
|
+
function fetchTree(signal) {
|
|
313
|
+
return get("tree", {}, signal);
|
|
308
314
|
}
|
|
309
|
-
function fetchItems(query = {}) {
|
|
310
|
-
return get("items", query);
|
|
315
|
+
function fetchItems(query = {}, signal) {
|
|
316
|
+
return get("items", query, signal);
|
|
311
317
|
}
|
|
312
|
-
function fetchItem(key, style = "apa") {
|
|
318
|
+
function fetchItem(key, style = "apa", signal) {
|
|
313
319
|
return get("item", {
|
|
314
320
|
key,
|
|
315
321
|
style
|
|
316
|
-
});
|
|
322
|
+
}, signal);
|
|
317
323
|
}
|
|
318
324
|
function fetchCollectionBib(key, style = "apa") {
|
|
319
325
|
return get("collection-bib", {
|
|
@@ -571,6 +577,258 @@ function resetPaneStore() {
|
|
|
571
577
|
for (const listener of listeners) listener();
|
|
572
578
|
}
|
|
573
579
|
//#endregion
|
|
580
|
+
//#region src/client/annotationComment.ts
|
|
581
|
+
/** 只有这些标签有意义;属性不会出现,仍容忍以防手改。 */
|
|
582
|
+
const TAG_SOURCE = "<\\/?(?:b|i|sub|sup|br|div)\\b[^>]*>";
|
|
583
|
+
function tagRegex() {
|
|
584
|
+
return new RegExp(TAG_SOURCE, "giu");
|
|
585
|
+
}
|
|
586
|
+
/** 评论 → 渲染 token;不平衡的闭合标签直接忽略。 */
|
|
587
|
+
function parseComment(comment) {
|
|
588
|
+
const tokens = [];
|
|
589
|
+
let bold = 0;
|
|
590
|
+
let italic = 0;
|
|
591
|
+
let sub = 0;
|
|
592
|
+
let sup = 0;
|
|
593
|
+
let last = 0;
|
|
594
|
+
const pushText = (text) => {
|
|
595
|
+
if (text === "") return;
|
|
596
|
+
tokens.push({
|
|
597
|
+
kind: "text",
|
|
598
|
+
text,
|
|
599
|
+
bold: bold > 0,
|
|
600
|
+
italic: italic > 0,
|
|
601
|
+
sub: sub > 0,
|
|
602
|
+
sup: sup > 0
|
|
603
|
+
});
|
|
604
|
+
};
|
|
605
|
+
for (const match of comment.matchAll(tagRegex())) {
|
|
606
|
+
const index = match.index ?? 0;
|
|
607
|
+
pushText(comment.slice(last, index));
|
|
608
|
+
last = index + match[0].length;
|
|
609
|
+
const tag = match[0].toLowerCase();
|
|
610
|
+
const closing = tag.startsWith("</");
|
|
611
|
+
const name = tag.replace(/[</>\s]/gu, "");
|
|
612
|
+
if (name === "br" || name === "div") {
|
|
613
|
+
if (!closing) tokens.push({
|
|
614
|
+
kind: "break",
|
|
615
|
+
text: "",
|
|
616
|
+
bold: false,
|
|
617
|
+
italic: false,
|
|
618
|
+
sub: false,
|
|
619
|
+
sup: false
|
|
620
|
+
});
|
|
621
|
+
continue;
|
|
622
|
+
}
|
|
623
|
+
const delta = closing ? -1 : 1;
|
|
624
|
+
if (name === "b") bold = Math.max(0, bold + delta);
|
|
625
|
+
else if (name === "i") italic = Math.max(0, italic + delta);
|
|
626
|
+
else if (name === "sub") sub = Math.max(0, sub + delta);
|
|
627
|
+
else if (name === "sup") sup = Math.max(0, sup + delta);
|
|
628
|
+
}
|
|
629
|
+
pushText(comment.slice(last));
|
|
630
|
+
while (tokens[0]?.kind === "break") tokens.shift();
|
|
631
|
+
while (tokens[tokens.length - 1]?.kind === "break") tokens.pop();
|
|
632
|
+
return tokens;
|
|
633
|
+
}
|
|
634
|
+
/** 去标签与换行,供插入草稿使用。 */
|
|
635
|
+
function commentPlain(comment) {
|
|
636
|
+
return comment.replace(tagRegex(), " ").replace(/\s+/gu, " ").trim();
|
|
637
|
+
}
|
|
638
|
+
/** 是否有可显示的内容(纯空白不算)。 */
|
|
639
|
+
function hasComment(comment) {
|
|
640
|
+
return commentPlain(comment) !== "";
|
|
641
|
+
}
|
|
642
|
+
function line(t, key, value) {
|
|
643
|
+
return tpl(t(key), { value });
|
|
644
|
+
}
|
|
645
|
+
/** 单条目的引用文本。 */
|
|
646
|
+
function itemText(t, detail) {
|
|
647
|
+
const lines = [tpl(t("cmp.item.header"), {
|
|
648
|
+
key: detail.key,
|
|
649
|
+
title: detail.title
|
|
650
|
+
})];
|
|
651
|
+
if (detail.creators.length > 0) lines.push(line(t, "cmp.item.creators", detail.creators.join("、")));
|
|
652
|
+
if (detail.year !== "") lines.push(line(t, "cmp.item.year", detail.year));
|
|
653
|
+
if (detail.publicationTitle !== "") lines.push(line(t, "cmp.item.source", detail.publicationTitle));
|
|
654
|
+
if (detail.doi !== "") lines.push(line(t, "cmp.item.doi", detail.doi));
|
|
655
|
+
if (detail.url !== "") lines.push(line(t, "cmp.item.url", detail.url));
|
|
656
|
+
if (detail.tags.length > 0) lines.push(line(t, "cmp.item.tags", detail.tags.join("、")));
|
|
657
|
+
if (detail.abstract !== "") lines.push(line(t, "cmp.item.abstract", detail.abstract));
|
|
658
|
+
return lines.join("\n");
|
|
659
|
+
}
|
|
660
|
+
/** 选中片段的摘录文本。 */
|
|
661
|
+
function excerptText(t, detail, quote) {
|
|
662
|
+
return `${tpl(t("cmp.excerpt.header"), {
|
|
663
|
+
title: detail.title,
|
|
664
|
+
key: detail.key
|
|
665
|
+
})}\n${tpl(t("cmp.excerpt.quote"), { value: quote })}`;
|
|
666
|
+
}
|
|
667
|
+
/**
|
|
668
|
+
* PDF 抽取会在汉字之间插空格(`硫 化 氢`),展示与插入时去掉;
|
|
669
|
+
* 只处理汉字与汉字之间的空格,避免动到西文词距。
|
|
670
|
+
*/
|
|
671
|
+
function tidyAnnotation(text) {
|
|
672
|
+
return text.replace(/\s+/gu, " ").replace(/(?<=[\u3400-\u9fff])\s+(?=[\u3400-\u9fff])/gu, "").trim();
|
|
673
|
+
}
|
|
674
|
+
/** 一条标注的引用文本:原文 + 类型 + 页码 + 评论 + 标注 key。 */
|
|
675
|
+
function annotationText(t, detail, annotation) {
|
|
676
|
+
const comment = commentPlain(annotation.comment);
|
|
677
|
+
const quote = tidyAnnotation(annotation.text) || comment;
|
|
678
|
+
const type = annotation.type === "underline" ? t("annotation.type.underline") : t("annotation.type.highlight");
|
|
679
|
+
const lines = [excerptText(t, detail, quote), tpl(t("cmp.annotation.type"), { value: type })];
|
|
680
|
+
if (annotation.pageLabel !== "") lines.push(tpl(t("cmp.annotation.page"), { value: annotation.pageLabel }));
|
|
681
|
+
if (comment !== "" && quote !== comment) lines.push(tpl(t("cmp.annotation.comment"), { value: comment }));
|
|
682
|
+
lines.push(tpl(t("cmp.annotation.key"), { value: annotation.key }));
|
|
683
|
+
return lines.join("\n");
|
|
684
|
+
}
|
|
685
|
+
/** 命名实体的最小集合(与 Host 清洗后的笔记 HTML 对齐)。 */
|
|
686
|
+
const NAMED_ENTITIES = {
|
|
687
|
+
nbsp: " ",
|
|
688
|
+
amp: String.fromCharCode(38),
|
|
689
|
+
lt: String.fromCharCode(60),
|
|
690
|
+
gt: String.fromCharCode(62),
|
|
691
|
+
quot: String.fromCharCode(34),
|
|
692
|
+
apos: String.fromCharCode(39)
|
|
693
|
+
};
|
|
694
|
+
/** 解码 HTML 实体;未识别的实体原样保留。 */
|
|
695
|
+
function decodeEntities(text) {
|
|
696
|
+
return text.replace(/&(#x[0-9a-f]+|#[0-9]+|[a-z]+);/giu, (match, body) => {
|
|
697
|
+
if (body.startsWith("#x") || body.startsWith("#X")) {
|
|
698
|
+
const code = Number.parseInt(body.slice(2), 16);
|
|
699
|
+
return Number.isFinite(code) ? String.fromCodePoint(code) : match;
|
|
700
|
+
}
|
|
701
|
+
if (body.startsWith("#")) {
|
|
702
|
+
const code = Number.parseInt(body.slice(1), 10);
|
|
703
|
+
return Number.isFinite(code) ? String.fromCodePoint(code) : match;
|
|
704
|
+
}
|
|
705
|
+
return NAMED_ENTITIES[body.toLowerCase()] ?? match;
|
|
706
|
+
});
|
|
707
|
+
}
|
|
708
|
+
/**
|
|
709
|
+
* 笔记 HTML → 纯文本:块级标签转换行、列表项加 `- `,剥标签后解码实体。
|
|
710
|
+
*
|
|
711
|
+
* 只保留段落与列表结构,图片等没有文本的可选内容自然消失。
|
|
712
|
+
*/
|
|
713
|
+
function noteHtmlToText(html) {
|
|
714
|
+
return decodeEntities(html.replace(/<br\s*\/?>/giu, "\n").replace(/<\/(?:p|div|h[1-6]|blockquote|li|tr|ul|ol|table|figure|figcaption|pre)>/giu, "\n").replace(/<li\b[^>]*>/giu, "- ").replace(/<[^>]*>/gu, "")).replace(/\u00a0/gu, " ").replace(/[ \t]+/gu, " ").replace(/ *\n */gu, "\n").replace(/(^|\n)- *\n/gu, "$1- ").replace(/\n{2,}(?=- )/gu, "\n").replace(/\n{3,}/gu, "\n\n").trim();
|
|
715
|
+
}
|
|
716
|
+
/** 单条笔记的引用文本:所属条目 + 正文纯文本 + 截断提示 + 更新日期 + 笔记 key。 */
|
|
717
|
+
function noteText(t, detail, note) {
|
|
718
|
+
const head = tpl(t("cmp.note.header"), {
|
|
719
|
+
title: detail.title,
|
|
720
|
+
key: detail.key
|
|
721
|
+
});
|
|
722
|
+
const body = noteHtmlToText(note.html);
|
|
723
|
+
const lines = [head];
|
|
724
|
+
if (body !== "") lines.push(body);
|
|
725
|
+
if (note.truncated) lines.push(t("cmp.note.truncated"));
|
|
726
|
+
const date = note.dateModified.slice(0, 10);
|
|
727
|
+
if (date !== "") lines.push(tpl(t("cmp.note.date"), { value: date }));
|
|
728
|
+
lines.push(tpl(t("cmp.note.key"), { value: note.key }));
|
|
729
|
+
return lines.join("\n");
|
|
730
|
+
}
|
|
731
|
+
/** 整目录的引用文本(标题清单 + 稳定标识)。 */
|
|
732
|
+
function collectionText(t, collection, items, total) {
|
|
733
|
+
const head = tpl(t("cmp.collection.header"), {
|
|
734
|
+
name: collection.name,
|
|
735
|
+
key: collection.key,
|
|
736
|
+
n: total
|
|
737
|
+
});
|
|
738
|
+
const preview = items.slice(0, 50);
|
|
739
|
+
const lines = preview.map((item) => {
|
|
740
|
+
const meta = [item.creators, item.year].filter((part) => part !== "").join(", ");
|
|
741
|
+
return tpl(t("cmp.collection.line"), {
|
|
742
|
+
title: item.title,
|
|
743
|
+
meta
|
|
744
|
+
});
|
|
745
|
+
});
|
|
746
|
+
if (total > preview.length) lines.push(tpl(t("cmp.collection.more"), { n: preview.length }));
|
|
747
|
+
return [head, ...lines].join("\n");
|
|
748
|
+
}
|
|
749
|
+
//#endregion
|
|
750
|
+
//#region src/client/cite.ts
|
|
751
|
+
/** 文库里已经没有这个对象(与「连不上 Zotero」区分开)。 */
|
|
752
|
+
function isMissing(error) {
|
|
753
|
+
return error instanceof ApiFailure && error.code === "zotero_not_found";
|
|
754
|
+
}
|
|
755
|
+
async function itemCitation(t, key, signal) {
|
|
756
|
+
try {
|
|
757
|
+
return itemText(t, await fetchItem(key, "apa", signal));
|
|
758
|
+
} catch (error) {
|
|
759
|
+
if (isMissing(error)) return tpl(t("cmp.missing.item"), { key });
|
|
760
|
+
throw error;
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
async function collectionCitation(t, key, signal) {
|
|
764
|
+
const collection = (await fetchTree(signal)).collections.find((node) => node.key === key);
|
|
765
|
+
if (collection === void 0) return tpl(t("cmp.missing.collection"), { key });
|
|
766
|
+
const data = await fetchItems({
|
|
767
|
+
scope: collection.scope,
|
|
768
|
+
collectionKey: collection.scope === void 0 ? collection.key : void 0,
|
|
769
|
+
limit: 50
|
|
770
|
+
}, signal);
|
|
771
|
+
return collectionText(t, collection, data.items, data.total);
|
|
772
|
+
}
|
|
773
|
+
//#endregion
|
|
774
|
+
//#region src/client/reference.ts
|
|
775
|
+
/**
|
|
776
|
+
* 写进 chip 的持久标识:一旦发布就不能改名,老草稿里的 chip 靠它找回 codec。
|
|
777
|
+
*/
|
|
778
|
+
const REF_SOURCE = "zotero";
|
|
779
|
+
const ITEM_PREFIX = "item:";
|
|
780
|
+
const COLLECTION_PREFIX = "col:";
|
|
781
|
+
/** 按 ref 前缀分派到对应的引用文本构建。 */
|
|
782
|
+
function citationForRef(t, ref, signal) {
|
|
783
|
+
if (ref.startsWith(ITEM_PREFIX)) return itemCitation(t, ref.slice(5), signal);
|
|
784
|
+
if (ref.startsWith(COLLECTION_PREFIX)) return collectionCitation(t, ref.slice(4), signal);
|
|
785
|
+
return Promise.reject(/* @__PURE__ */ new Error(`无法识别的 Zotero 引用:${ref}`));
|
|
786
|
+
}
|
|
787
|
+
function createZoteroSource(t) {
|
|
788
|
+
return {
|
|
789
|
+
trigger: "@",
|
|
790
|
+
name: REF_SOURCE,
|
|
791
|
+
candidates: () => Promise.resolve([]),
|
|
792
|
+
onPick: () => void 0,
|
|
793
|
+
codec: {
|
|
794
|
+
clipboardText: (ref) => `@${ref}`,
|
|
795
|
+
serialize: (ref, signal) => citationForRef(t, ref, signal)
|
|
796
|
+
}
|
|
797
|
+
};
|
|
798
|
+
}
|
|
799
|
+
function reference(prefix, key, title, appearance) {
|
|
800
|
+
const ref = `${prefix}${key}`;
|
|
801
|
+
return {
|
|
802
|
+
source: REF_SOURCE,
|
|
803
|
+
ref,
|
|
804
|
+
label: title === "" ? key : title,
|
|
805
|
+
appearance,
|
|
806
|
+
clipboardText: `@${ref}`
|
|
807
|
+
};
|
|
808
|
+
}
|
|
809
|
+
/** 一条文献的 chip。 */
|
|
810
|
+
function itemReference(key, title) {
|
|
811
|
+
return reference(ITEM_PREFIX, key, title, "file");
|
|
812
|
+
}
|
|
813
|
+
/** 一个目录的 chip。 */
|
|
814
|
+
function collectionReference(key, name) {
|
|
815
|
+
return reference(COLLECTION_PREFIX, key, name, "folder");
|
|
816
|
+
}
|
|
817
|
+
/**
|
|
818
|
+
* 把 chip 插进该会话的输入框。
|
|
819
|
+
*
|
|
820
|
+
* `thisArg` 必须显式传会话 ctx:cordis 的事件表挂在根 ctx 上,不带过滤参数会把
|
|
821
|
+
* **所有**会话的监听器都跑一遍,第一个返回 true 的赢——多开几个会话就会插错地方。
|
|
822
|
+
*/
|
|
823
|
+
function insertChip(sessions, sessionId, reference, span) {
|
|
824
|
+
const actx = sessionId === void 0 ? void 0 : sessions.scope(sessionId);
|
|
825
|
+
if (!actx) return false;
|
|
826
|
+
return actx.bail(actx, "slash/input-insert-reference", {
|
|
827
|
+
reference,
|
|
828
|
+
span
|
|
829
|
+
}) === true;
|
|
830
|
+
}
|
|
831
|
+
//#endregion
|
|
574
832
|
//#region src/client/ZoteroCard.tsx
|
|
575
833
|
/**
|
|
576
834
|
* 插件页配置卡(浏览器半)。
|
|
@@ -885,6 +1143,45 @@ function ZoteroCard(props) {
|
|
|
885
1143
|
});
|
|
886
1144
|
}
|
|
887
1145
|
//#endregion
|
|
1146
|
+
//#region src/client/readState.tsx
|
|
1147
|
+
/**
|
|
1148
|
+
* 已读圆点颜色:柔和的粉色。
|
|
1149
|
+
*
|
|
1150
|
+
* 不用主题蓝:蓝点在列表里很像「未读消息」徽标,粉色更接近 Zotero Style 的
|
|
1151
|
+
* 阅读进度条配色,读起来是「标记」而不是「待办」。
|
|
1152
|
+
*/
|
|
1153
|
+
const READ_DOT_COLOR = "#e8798d";
|
|
1154
|
+
/** 已读条目标题的淡化程度:留一点提示,又不至于看成禁用态。 */
|
|
1155
|
+
const READ_TITLE_OPACITY = .75;
|
|
1156
|
+
function isRead(item) {
|
|
1157
|
+
return item.lastRead !== null;
|
|
1158
|
+
}
|
|
1159
|
+
/** 圆点槽位:未读时透明但仍占宽,避免已读/未读行的标题起点错位。 */
|
|
1160
|
+
function ReadDot({ read }) {
|
|
1161
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
|
|
1162
|
+
"aria-hidden": true,
|
|
1163
|
+
style: {
|
|
1164
|
+
width: 4,
|
|
1165
|
+
height: 4,
|
|
1166
|
+
borderRadius: "50%",
|
|
1167
|
+
flexShrink: 0,
|
|
1168
|
+
alignSelf: "center",
|
|
1169
|
+
background: read ? READ_DOT_COLOR : "transparent",
|
|
1170
|
+
opacity: read ? .9 : 0
|
|
1171
|
+
}
|
|
1172
|
+
});
|
|
1173
|
+
}
|
|
1174
|
+
/** 已读条目的标题淡化;未读返回空对象,直接展开不影响原有样式。 */
|
|
1175
|
+
function readTitleStyle(read) {
|
|
1176
|
+
return read ? { opacity: READ_TITLE_OPACITY } : {};
|
|
1177
|
+
}
|
|
1178
|
+
/** 行内 hover 提示:读过的在标题后补一行最后阅读时间。 */
|
|
1179
|
+
function itemTooltip(t, item) {
|
|
1180
|
+
if (item.lastRead === null) return item.title;
|
|
1181
|
+
const date = (/* @__PURE__ */ new Date(item.lastRead * 1e3)).toLocaleDateString();
|
|
1182
|
+
return `${item.title}\n${tpl(t("item.lastRead"), { date })}`;
|
|
1183
|
+
}
|
|
1184
|
+
//#endregion
|
|
888
1185
|
//#region src/client/CollectionTree.tsx
|
|
889
1186
|
/**
|
|
890
1187
|
* 左栏:文库分类树 + 条目叶子(Zotero 风格)。
|
|
@@ -1062,12 +1359,13 @@ function Chevron({ open, onToggle }) {
|
|
|
1062
1359
|
function ItemRow({ ctx, item, depth }) {
|
|
1063
1360
|
const [menuOpen, setMenuOpen] = (0, react.useState)(false);
|
|
1064
1361
|
const meta = [item.creators, item.year].filter((part) => part !== "").join(" · ");
|
|
1362
|
+
const read = isRead(item);
|
|
1065
1363
|
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_deepseek_ai_dsh_client_ui_primitives.Menu, {
|
|
1066
1364
|
open: menuOpen,
|
|
1067
1365
|
onClose: () => setMenuOpen(false),
|
|
1068
1366
|
onSelect: (id) => {
|
|
1069
1367
|
setMenuOpen(false);
|
|
1070
|
-
if (id === "insert-item") ctx.onInsertItem(item
|
|
1368
|
+
if (id === "insert-item") ctx.onInsertItem(item);
|
|
1071
1369
|
},
|
|
1072
1370
|
align: "end",
|
|
1073
1371
|
portal: true,
|
|
@@ -1076,6 +1374,7 @@ function ItemRow({ ctx, item, depth }) {
|
|
|
1076
1374
|
label: ctx.t("action.insertItem")
|
|
1077
1375
|
}],
|
|
1078
1376
|
anchor: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
|
|
1377
|
+
title: itemTooltip(ctx.t, item),
|
|
1079
1378
|
onClick: () => ctx.onSelectItem(item.key),
|
|
1080
1379
|
onContextMenu: (event) => {
|
|
1081
1380
|
event.preventDefault();
|
|
@@ -1092,9 +1391,12 @@ function ItemRow({ ctx, item, depth }) {
|
|
|
1092
1391
|
style: iconBox,
|
|
1093
1392
|
children: glyph(ITEM_ICON)
|
|
1094
1393
|
}),
|
|
1394
|
+
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(ReadDot, { read }),
|
|
1095
1395
|
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
|
|
1096
|
-
style:
|
|
1097
|
-
|
|
1396
|
+
style: {
|
|
1397
|
+
...titleStyle,
|
|
1398
|
+
...readTitleStyle(read)
|
|
1399
|
+
},
|
|
1098
1400
|
children: item.title
|
|
1099
1401
|
}),
|
|
1100
1402
|
meta !== "" && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
|
|
@@ -1323,180 +1625,6 @@ function CollectionTree(props) {
|
|
|
1323
1625
|
});
|
|
1324
1626
|
}
|
|
1325
1627
|
//#endregion
|
|
1326
|
-
//#region src/client/annotationComment.ts
|
|
1327
|
-
/** 只有这些标签有意义;属性不会出现,仍容忍以防手改。 */
|
|
1328
|
-
const TAG_SOURCE = "<\\/?(?:b|i|sub|sup|br|div)\\b[^>]*>";
|
|
1329
|
-
function tagRegex() {
|
|
1330
|
-
return new RegExp(TAG_SOURCE, "giu");
|
|
1331
|
-
}
|
|
1332
|
-
/** 评论 → 渲染 token;不平衡的闭合标签直接忽略。 */
|
|
1333
|
-
function parseComment(comment) {
|
|
1334
|
-
const tokens = [];
|
|
1335
|
-
let bold = 0;
|
|
1336
|
-
let italic = 0;
|
|
1337
|
-
let sub = 0;
|
|
1338
|
-
let sup = 0;
|
|
1339
|
-
let last = 0;
|
|
1340
|
-
const pushText = (text) => {
|
|
1341
|
-
if (text === "") return;
|
|
1342
|
-
tokens.push({
|
|
1343
|
-
kind: "text",
|
|
1344
|
-
text,
|
|
1345
|
-
bold: bold > 0,
|
|
1346
|
-
italic: italic > 0,
|
|
1347
|
-
sub: sub > 0,
|
|
1348
|
-
sup: sup > 0
|
|
1349
|
-
});
|
|
1350
|
-
};
|
|
1351
|
-
for (const match of comment.matchAll(tagRegex())) {
|
|
1352
|
-
const index = match.index ?? 0;
|
|
1353
|
-
pushText(comment.slice(last, index));
|
|
1354
|
-
last = index + match[0].length;
|
|
1355
|
-
const tag = match[0].toLowerCase();
|
|
1356
|
-
const closing = tag.startsWith("</");
|
|
1357
|
-
const name = tag.replace(/[</>\s]/gu, "");
|
|
1358
|
-
if (name === "br" || name === "div") {
|
|
1359
|
-
if (!closing) tokens.push({
|
|
1360
|
-
kind: "break",
|
|
1361
|
-
text: "",
|
|
1362
|
-
bold: false,
|
|
1363
|
-
italic: false,
|
|
1364
|
-
sub: false,
|
|
1365
|
-
sup: false
|
|
1366
|
-
});
|
|
1367
|
-
continue;
|
|
1368
|
-
}
|
|
1369
|
-
const delta = closing ? -1 : 1;
|
|
1370
|
-
if (name === "b") bold = Math.max(0, bold + delta);
|
|
1371
|
-
else if (name === "i") italic = Math.max(0, italic + delta);
|
|
1372
|
-
else if (name === "sub") sub = Math.max(0, sub + delta);
|
|
1373
|
-
else if (name === "sup") sup = Math.max(0, sup + delta);
|
|
1374
|
-
}
|
|
1375
|
-
pushText(comment.slice(last));
|
|
1376
|
-
while (tokens[0]?.kind === "break") tokens.shift();
|
|
1377
|
-
while (tokens[tokens.length - 1]?.kind === "break") tokens.pop();
|
|
1378
|
-
return tokens;
|
|
1379
|
-
}
|
|
1380
|
-
/** 去标签与换行,供插入草稿使用。 */
|
|
1381
|
-
function commentPlain(comment) {
|
|
1382
|
-
return comment.replace(tagRegex(), " ").replace(/\s+/gu, " ").trim();
|
|
1383
|
-
}
|
|
1384
|
-
/** 是否有可显示的内容(纯空白不算)。 */
|
|
1385
|
-
function hasComment(comment) {
|
|
1386
|
-
return commentPlain(comment) !== "";
|
|
1387
|
-
}
|
|
1388
|
-
//#endregion
|
|
1389
|
-
//#region src/client/compose.ts
|
|
1390
|
-
/** 整目录插入时最多列出多少条(完整清单交给模型的 MCP 工具)。 */
|
|
1391
|
-
const COLLECTION_PREVIEW_LIMIT$1 = 50;
|
|
1392
|
-
function line(t, key, value) {
|
|
1393
|
-
return tpl(t(key), { value });
|
|
1394
|
-
}
|
|
1395
|
-
/** 单条目的引用文本。 */
|
|
1396
|
-
function itemText(t, detail) {
|
|
1397
|
-
const lines = [tpl(t("cmp.item.header"), {
|
|
1398
|
-
key: detail.key,
|
|
1399
|
-
title: detail.title
|
|
1400
|
-
})];
|
|
1401
|
-
if (detail.creators.length > 0) lines.push(line(t, "cmp.item.creators", detail.creators.join("、")));
|
|
1402
|
-
if (detail.year !== "") lines.push(line(t, "cmp.item.year", detail.year));
|
|
1403
|
-
if (detail.publicationTitle !== "") lines.push(line(t, "cmp.item.source", detail.publicationTitle));
|
|
1404
|
-
if (detail.doi !== "") lines.push(line(t, "cmp.item.doi", detail.doi));
|
|
1405
|
-
if (detail.url !== "") lines.push(line(t, "cmp.item.url", detail.url));
|
|
1406
|
-
if (detail.tags.length > 0) lines.push(line(t, "cmp.item.tags", detail.tags.join("、")));
|
|
1407
|
-
if (detail.abstract !== "") lines.push(line(t, "cmp.item.abstract", detail.abstract));
|
|
1408
|
-
return lines.join("\n");
|
|
1409
|
-
}
|
|
1410
|
-
/** 选中片段的摘录文本。 */
|
|
1411
|
-
function excerptText(t, detail, quote) {
|
|
1412
|
-
return `${tpl(t("cmp.excerpt.header"), {
|
|
1413
|
-
title: detail.title,
|
|
1414
|
-
key: detail.key
|
|
1415
|
-
})}\n${tpl(t("cmp.excerpt.quote"), { value: quote })}`;
|
|
1416
|
-
}
|
|
1417
|
-
/**
|
|
1418
|
-
* PDF 抽取会在汉字之间插空格(`硫 化 氢`),展示与插入时去掉;
|
|
1419
|
-
* 只处理汉字与汉字之间的空格,避免动到西文词距。
|
|
1420
|
-
*/
|
|
1421
|
-
function tidyAnnotation(text) {
|
|
1422
|
-
return text.replace(/\s+/gu, " ").replace(/(?<=[\u3400-\u9fff])\s+(?=[\u3400-\u9fff])/gu, "").trim();
|
|
1423
|
-
}
|
|
1424
|
-
/** 一条标注的引用文本:原文 + 类型 + 页码 + 评论 + 标注 key。 */
|
|
1425
|
-
function annotationText(t, detail, annotation) {
|
|
1426
|
-
const comment = commentPlain(annotation.comment);
|
|
1427
|
-
const quote = tidyAnnotation(annotation.text) || comment;
|
|
1428
|
-
const type = annotation.type === "underline" ? t("annotation.type.underline") : t("annotation.type.highlight");
|
|
1429
|
-
const lines = [excerptText(t, detail, quote), tpl(t("cmp.annotation.type"), { value: type })];
|
|
1430
|
-
if (annotation.pageLabel !== "") lines.push(tpl(t("cmp.annotation.page"), { value: annotation.pageLabel }));
|
|
1431
|
-
if (comment !== "" && quote !== comment) lines.push(tpl(t("cmp.annotation.comment"), { value: comment }));
|
|
1432
|
-
lines.push(tpl(t("cmp.annotation.key"), { value: annotation.key }));
|
|
1433
|
-
return lines.join("\n");
|
|
1434
|
-
}
|
|
1435
|
-
/** 命名实体的最小集合(与 Host 清洗后的笔记 HTML 对齐)。 */
|
|
1436
|
-
const NAMED_ENTITIES = {
|
|
1437
|
-
nbsp: " ",
|
|
1438
|
-
amp: String.fromCharCode(38),
|
|
1439
|
-
lt: String.fromCharCode(60),
|
|
1440
|
-
gt: String.fromCharCode(62),
|
|
1441
|
-
quot: String.fromCharCode(34),
|
|
1442
|
-
apos: String.fromCharCode(39)
|
|
1443
|
-
};
|
|
1444
|
-
/** 解码 HTML 实体;未识别的实体原样保留。 */
|
|
1445
|
-
function decodeEntities(text) {
|
|
1446
|
-
return text.replace(/&(#x[0-9a-f]+|#[0-9]+|[a-z]+);/giu, (match, body) => {
|
|
1447
|
-
if (body.startsWith("#x") || body.startsWith("#X")) {
|
|
1448
|
-
const code = Number.parseInt(body.slice(2), 16);
|
|
1449
|
-
return Number.isFinite(code) ? String.fromCodePoint(code) : match;
|
|
1450
|
-
}
|
|
1451
|
-
if (body.startsWith("#")) {
|
|
1452
|
-
const code = Number.parseInt(body.slice(1), 10);
|
|
1453
|
-
return Number.isFinite(code) ? String.fromCodePoint(code) : match;
|
|
1454
|
-
}
|
|
1455
|
-
return NAMED_ENTITIES[body.toLowerCase()] ?? match;
|
|
1456
|
-
});
|
|
1457
|
-
}
|
|
1458
|
-
/**
|
|
1459
|
-
* 笔记 HTML → 纯文本:块级标签转换行、列表项加 `- `,剥标签后解码实体。
|
|
1460
|
-
*
|
|
1461
|
-
* 只保留段落与列表结构,图片等没有文本的可选内容自然消失。
|
|
1462
|
-
*/
|
|
1463
|
-
function noteHtmlToText(html) {
|
|
1464
|
-
return decodeEntities(html.replace(/<br\s*\/?>/giu, "\n").replace(/<\/(?:p|div|h[1-6]|blockquote|li|tr|ul|ol|table|figure|figcaption|pre)>/giu, "\n").replace(/<li\b[^>]*>/giu, "- ").replace(/<[^>]*>/gu, "")).replace(/\u00a0/gu, " ").replace(/[ \t]+/gu, " ").replace(/ *\n */gu, "\n").replace(/(^|\n)- *\n/gu, "$1- ").replace(/\n{2,}(?=- )/gu, "\n").replace(/\n{3,}/gu, "\n\n").trim();
|
|
1465
|
-
}
|
|
1466
|
-
/** 单条笔记的引用文本:所属条目 + 正文纯文本 + 截断提示 + 更新日期 + 笔记 key。 */
|
|
1467
|
-
function noteText(t, detail, note) {
|
|
1468
|
-
const head = tpl(t("cmp.note.header"), {
|
|
1469
|
-
title: detail.title,
|
|
1470
|
-
key: detail.key
|
|
1471
|
-
});
|
|
1472
|
-
const body = noteHtmlToText(note.html);
|
|
1473
|
-
const lines = [head];
|
|
1474
|
-
if (body !== "") lines.push(body);
|
|
1475
|
-
if (note.truncated) lines.push(t("cmp.note.truncated"));
|
|
1476
|
-
const date = note.dateModified.slice(0, 10);
|
|
1477
|
-
if (date !== "") lines.push(tpl(t("cmp.note.date"), { value: date }));
|
|
1478
|
-
lines.push(tpl(t("cmp.note.key"), { value: note.key }));
|
|
1479
|
-
return lines.join("\n");
|
|
1480
|
-
}
|
|
1481
|
-
/** 整目录的引用文本(标题清单 + 稳定标识)。 */
|
|
1482
|
-
function collectionText(t, collection, items, total) {
|
|
1483
|
-
const head = tpl(t("cmp.collection.header"), {
|
|
1484
|
-
name: collection.name,
|
|
1485
|
-
key: collection.key,
|
|
1486
|
-
n: total
|
|
1487
|
-
});
|
|
1488
|
-
const preview = items.slice(0, COLLECTION_PREVIEW_LIMIT$1);
|
|
1489
|
-
const lines = preview.map((item) => {
|
|
1490
|
-
const meta = [item.creators, item.year].filter((part) => part !== "").join(", ");
|
|
1491
|
-
return tpl(t("cmp.collection.line"), {
|
|
1492
|
-
title: item.title,
|
|
1493
|
-
meta
|
|
1494
|
-
});
|
|
1495
|
-
});
|
|
1496
|
-
if (total > preview.length) lines.push(tpl(t("cmp.collection.more"), { n: preview.length }));
|
|
1497
|
-
return [head, ...lines].join("\n");
|
|
1498
|
-
}
|
|
1499
|
-
//#endregion
|
|
1500
1628
|
//#region src/client/inline.ts
|
|
1501
1629
|
/**
|
|
1502
1630
|
* 捕获草稿当前位置,再构建文本并插入。
|
|
@@ -2779,11 +2907,10 @@ const linkRowStyle = {
|
|
|
2779
2907
|
* 右侧栏面板容器:头部状态栏 + 左分类树 + 右详情。
|
|
2780
2908
|
*
|
|
2781
2909
|
* 两栏之间是可拖动的分隔条(宽度存 localStorage,跨会话/重启都记住)。
|
|
2782
|
-
* 数据与选择状态都在 {@link paneStore}
|
|
2783
|
-
*
|
|
2910
|
+
* 数据与选择状态都在 {@link paneStore} 的模块级单例里(跨会话共用同一块面板)。
|
|
2911
|
+
* 「添加到会话」走 ui-conversation 下发的会话级标准 prop:条目与整目录插引用
|
|
2912
|
+
* chip(见 reference.ts),摘录 / 标注 / 笔记仍是纯文本插入。
|
|
2784
2913
|
*/
|
|
2785
|
-
/** 整目录插入时拉取的条目数(与 compose 的预览上限一致)。 */
|
|
2786
|
-
const COLLECTION_PREVIEW_LIMIT = 50;
|
|
2787
2914
|
/** 左栏宽度的存储键与取值范围。 */
|
|
2788
2915
|
const TREE_WIDTH_KEY = "dsh-plugin-zotero.treeWidth";
|
|
2789
2916
|
const TREE_WIDTH_DEFAULT = 260;
|
|
@@ -2819,7 +2946,7 @@ function StatusDot({ ok, label }) {
|
|
|
2819
2946
|
} }), label]
|
|
2820
2947
|
});
|
|
2821
2948
|
}
|
|
2822
|
-
function ZoteroPane({ t, inputActions }) {
|
|
2949
|
+
function ZoteroPane({ t, inputActions, sessions, sessionId }) {
|
|
2823
2950
|
const { health, libraryName, collections, treeError, treeLoading, query, results, selectedItemKey, selectedCollection, detail, detailLoading, detailError, collectionBib, nodeItems, nodeErrors, toast } = usePaneState();
|
|
2824
2951
|
(0, react.useEffect)(() => {
|
|
2825
2952
|
loadTree();
|
|
@@ -2860,16 +2987,24 @@ function ZoteroPane({ t, inputActions }) {
|
|
|
2860
2987
|
else if (outcome === "no-editor") showToast(t("error.noEditor"));
|
|
2861
2988
|
else showToast(t("error.draftChanged"));
|
|
2862
2989
|
}, [t]);
|
|
2863
|
-
|
|
2864
|
-
|
|
2865
|
-
|
|
2866
|
-
|
|
2990
|
+
/**
|
|
2991
|
+
* 插一个引用 chip:草稿里只留 key,完整文本在提交那一刻由引用源的 codec 现取。
|
|
2992
|
+
*/
|
|
2993
|
+
const insertReference = (0, react.useCallback)((reference) => {
|
|
2994
|
+
if (!inputActions) {
|
|
2995
|
+
report("no-editor");
|
|
2996
|
+
return;
|
|
2997
|
+
}
|
|
2998
|
+
report(insertChip(sessions, sessionId, reference, inputActions.captureInsertion()) ? "inserted" : "draft-changed");
|
|
2867
2999
|
}, [
|
|
2868
|
-
detail,
|
|
2869
3000
|
inputActions,
|
|
2870
3001
|
report,
|
|
2871
|
-
|
|
3002
|
+
sessionId,
|
|
3003
|
+
sessions
|
|
2872
3004
|
]);
|
|
3005
|
+
const insertItem = (0, react.useCallback)((key, title) => {
|
|
3006
|
+
insertReference(itemReference(key, title));
|
|
3007
|
+
}, [insertReference]);
|
|
2873
3008
|
const insertExcerpt = (0, react.useCallback)(async (target, quote) => {
|
|
2874
3009
|
report(await insertBuilt(inputActions, () => excerptText(t, target, quote)));
|
|
2875
3010
|
}, [
|
|
@@ -2891,20 +3026,9 @@ function ZoteroPane({ t, inputActions }) {
|
|
|
2891
3026
|
report,
|
|
2892
3027
|
t
|
|
2893
3028
|
]);
|
|
2894
|
-
const insertCollection = (0, react.useCallback)(
|
|
2895
|
-
|
|
2896
|
-
|
|
2897
|
-
scope: collection.scope,
|
|
2898
|
-
collectionKey: collection.scope === void 0 ? collection.key : void 0,
|
|
2899
|
-
limit: COLLECTION_PREVIEW_LIMIT
|
|
2900
|
-
});
|
|
2901
|
-
return collectionText(t, collection, data.items, data.total);
|
|
2902
|
-
}));
|
|
2903
|
-
}, [
|
|
2904
|
-
inputActions,
|
|
2905
|
-
report,
|
|
2906
|
-
t
|
|
2907
|
-
]);
|
|
3029
|
+
const insertCollection = (0, react.useCallback)((collection) => {
|
|
3030
|
+
insertReference(collectionReference(collection.key, collection.name));
|
|
3031
|
+
}, [insertReference]);
|
|
2908
3032
|
/** 分类详情里的条目列表:与左侧树共用同一份缓存(选中分类时树已按需拉取)。 */
|
|
2909
3033
|
const collectionItems = selectedCollection === null ? void 0 : nodeItems[selectedCollection.key];
|
|
2910
3034
|
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
|
|
@@ -2994,21 +3118,31 @@ function ZoteroPane({ t, inputActions }) {
|
|
|
2994
3118
|
children: t("tree.empty")
|
|
2995
3119
|
}) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
|
|
2996
3120
|
style: { padding: "8px 6px" },
|
|
2997
|
-
children: results.map((item) =>
|
|
2998
|
-
|
|
2999
|
-
|
|
3000
|
-
|
|
3001
|
-
|
|
3002
|
-
|
|
3003
|
-
|
|
3004
|
-
|
|
3005
|
-
|
|
3006
|
-
children:
|
|
3007
|
-
|
|
3008
|
-
|
|
3009
|
-
|
|
3010
|
-
|
|
3011
|
-
|
|
3121
|
+
children: results.map((item) => {
|
|
3122
|
+
const read = isRead(item);
|
|
3123
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
|
|
3124
|
+
title: itemTooltip(t, item),
|
|
3125
|
+
onClick: () => openItem(item.key),
|
|
3126
|
+
style: {
|
|
3127
|
+
...resultRowStyle,
|
|
3128
|
+
background: selectedItemKey === item.key ? "rgba(127,127,127,0.16)" : "transparent"
|
|
3129
|
+
},
|
|
3130
|
+
children: [
|
|
3131
|
+
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(ReadDot, { read }),
|
|
3132
|
+
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
|
|
3133
|
+
style: {
|
|
3134
|
+
...resultTitleStyle,
|
|
3135
|
+
...readTitleStyle(read)
|
|
3136
|
+
},
|
|
3137
|
+
children: item.title
|
|
3138
|
+
}),
|
|
3139
|
+
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
|
|
3140
|
+
style: resultMetaStyle,
|
|
3141
|
+
children: [item.creators, item.year].filter((p) => p !== "").join(" · ")
|
|
3142
|
+
})
|
|
3143
|
+
]
|
|
3144
|
+
}, item.key);
|
|
3145
|
+
})
|
|
3012
3146
|
}) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)(CollectionTree, {
|
|
3013
3147
|
t,
|
|
3014
3148
|
collections,
|
|
@@ -3016,8 +3150,8 @@ function ZoteroPane({ t, inputActions }) {
|
|
|
3016
3150
|
selectedCollectionKey: selectedCollection?.key ?? null,
|
|
3017
3151
|
onSelectItem: openItem,
|
|
3018
3152
|
onSelectCollection: selectCollection,
|
|
3019
|
-
onInsertItem: (
|
|
3020
|
-
onInsertCollection:
|
|
3153
|
+
onInsertItem: (item) => insertItem(item.key, item.title),
|
|
3154
|
+
onInsertCollection: insertCollection
|
|
3021
3155
|
})
|
|
3022
3156
|
})]
|
|
3023
3157
|
}),
|
|
@@ -3073,7 +3207,7 @@ function ZoteroPane({ t, inputActions }) {
|
|
|
3073
3207
|
}) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ItemDetail, {
|
|
3074
3208
|
t,
|
|
3075
3209
|
detail,
|
|
3076
|
-
onInsertItem: () =>
|
|
3210
|
+
onInsertItem: () => insertItem(detail.key, detail.title),
|
|
3077
3211
|
onInsertExcerpt: (quote) => void insertExcerpt(detail, quote),
|
|
3078
3212
|
onInsertAnnotation: (annotation) => void insertAnnotation(detail, annotation),
|
|
3079
3213
|
onInsertNote: (note) => void insertNote(detail, note),
|
|
@@ -3098,7 +3232,7 @@ function ZoteroPane({ t, inputActions }) {
|
|
|
3098
3232
|
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_deepseek_ai_dsh_client_ui_primitives.Button, {
|
|
3099
3233
|
variant: "outline",
|
|
3100
3234
|
size: "sm",
|
|
3101
|
-
onClick: () =>
|
|
3235
|
+
onClick: () => insertCollection(selectedCollection),
|
|
3102
3236
|
children: t("action.insertCollection")
|
|
3103
3237
|
}),
|
|
3104
3238
|
selectedCollection.scope === void 0 && /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("h3", {
|
|
@@ -3134,18 +3268,28 @@ function ZoteroPane({ t, inputActions }) {
|
|
|
3134
3268
|
margin: "16px 0 6px"
|
|
3135
3269
|
},
|
|
3136
3270
|
children: t("detail.items")
|
|
3137
|
-
}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { children: collectionItems.map((item) =>
|
|
3138
|
-
|
|
3139
|
-
|
|
3140
|
-
|
|
3141
|
-
|
|
3142
|
-
|
|
3143
|
-
children:
|
|
3144
|
-
|
|
3145
|
-
|
|
3146
|
-
|
|
3147
|
-
|
|
3148
|
-
|
|
3271
|
+
}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { children: collectionItems.map((item) => {
|
|
3272
|
+
const read = isRead(item);
|
|
3273
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
|
|
3274
|
+
title: itemTooltip(t, item),
|
|
3275
|
+
onClick: () => openItem(item.key),
|
|
3276
|
+
style: resultRowStyle,
|
|
3277
|
+
children: [
|
|
3278
|
+
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(ReadDot, { read }),
|
|
3279
|
+
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
|
|
3280
|
+
style: {
|
|
3281
|
+
...resultTitleStyle,
|
|
3282
|
+
...readTitleStyle(read)
|
|
3283
|
+
},
|
|
3284
|
+
children: item.title
|
|
3285
|
+
}),
|
|
3286
|
+
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
|
|
3287
|
+
style: resultMetaStyle,
|
|
3288
|
+
children: [item.creators, item.year].filter((part) => part !== "").join(" · ")
|
|
3289
|
+
})
|
|
3290
|
+
]
|
|
3291
|
+
}, item.key);
|
|
3292
|
+
}) })] })
|
|
3149
3293
|
]
|
|
3150
3294
|
}) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
|
|
3151
3295
|
style: hintStyle,
|
|
@@ -3208,7 +3352,11 @@ const resultMetaStyle = {
|
|
|
3208
3352
|
*
|
|
3209
3353
|
* 在右侧栏注册一个「页」类型 tab(不给 patterns,走 guide 入口),并把面板
|
|
3210
3354
|
* 正文注册到 `sidebar.right.pane.tab` 席位。会话级标准 prop `inputActions`
|
|
3211
|
-
*
|
|
3355
|
+
* (ui-conversation 下发)与 `sessionId`(ui-session 下发)随槽位作用域透传给
|
|
3356
|
+
* 正文。
|
|
3357
|
+
*
|
|
3358
|
+
* 同时注册一个 Zotero 引用源(见 reference.ts):面板插的引用 chip 靠它的 codec
|
|
3359
|
+
* 在提交时展开成模型文本。它不进 `@` 菜单(候选恒空)。
|
|
3212
3360
|
*
|
|
3213
3361
|
* 注意:cordis 的对象式 inject 是「拦截 config」,不是 {required,optional},
|
|
3214
3362
|
* 因此这里用数组式 inject。
|
|
@@ -3220,7 +3368,9 @@ const inject = [
|
|
|
3220
3368
|
"slots",
|
|
3221
3369
|
"locale",
|
|
3222
3370
|
"sidebarRightTabs",
|
|
3223
|
-
"configForms"
|
|
3371
|
+
"configForms",
|
|
3372
|
+
"inputTriggers",
|
|
3373
|
+
"sessions"
|
|
3224
3374
|
];
|
|
3225
3375
|
function apply(ctx) {
|
|
3226
3376
|
ctx.effect(() => ctx.locale.register(NS, dicts), "dsh-plugin-zotero: dictionaries");
|
|
@@ -3236,9 +3386,12 @@ function apply(ctx) {
|
|
|
3236
3386
|
order: 10
|
|
3237
3387
|
}]
|
|
3238
3388
|
}), "dsh-plugin-zotero: sidebar tab");
|
|
3389
|
+
ctx.effect(() => ctx.inputTriggers.registerSource(createZoteroSource(t)), "dsh-plugin-zotero: reference codec");
|
|
3239
3390
|
const Body = (props) => (0, react.createElement)(ZoteroPane, {
|
|
3240
3391
|
t,
|
|
3241
|
-
|
|
3392
|
+
sessions: ctx.sessions,
|
|
3393
|
+
inputActions: props.inputActions,
|
|
3394
|
+
sessionId: props.sessionId
|
|
3242
3395
|
});
|
|
3243
3396
|
ctx.effect(() => ctx.slots.register({
|
|
3244
3397
|
name: "sidebar.right.pane.tab",
|
package/lib/zotero/client.d.ts
CHANGED
|
@@ -45,6 +45,8 @@ interface RawItemData {
|
|
|
45
45
|
note?: string;
|
|
46
46
|
/** 标注所属附件,仅 `itemType === 'annotation'` 有。 */
|
|
47
47
|
parentItem?: string | false;
|
|
48
|
+
/** 附件最后阅读时间(Unix 秒);本地 API 在附件 JSON 里直接给出。 */
|
|
49
|
+
lastRead?: number;
|
|
48
50
|
annotationType?: string;
|
|
49
51
|
annotationText?: string;
|
|
50
52
|
annotationComment?: string;
|
package/lib/zotero/client.js
CHANGED
|
@@ -173,7 +173,7 @@ function toNote(raw) {
|
|
|
173
173
|
truncated,
|
|
174
174
|
};
|
|
175
175
|
}
|
|
176
|
-
function toSummary(raw) {
|
|
176
|
+
function toSummary(raw, lastRead) {
|
|
177
177
|
const data = raw.data ?? {};
|
|
178
178
|
const itemType = data.itemType ?? '';
|
|
179
179
|
const hasAttachment = itemType === 'attachment'
|
|
@@ -193,8 +193,24 @@ function toSummary(raw) {
|
|
|
193
193
|
year: yearOf(raw.meta, data),
|
|
194
194
|
itemType,
|
|
195
195
|
hasAttachment,
|
|
196
|
+
lastRead,
|
|
196
197
|
};
|
|
197
198
|
}
|
|
199
|
+
/** 附件自己的 `lastRead` 就在它的 JSON 里,笔记没有,其余条目去问子附件。 */
|
|
200
|
+
function lastReadOf(raw) {
|
|
201
|
+
const data = raw.data ?? {};
|
|
202
|
+
if (data.itemType === 'attachment') {
|
|
203
|
+
const value = data.lastRead;
|
|
204
|
+
return Promise.resolve(typeof value === 'number' && value > 0 ? value : null);
|
|
205
|
+
}
|
|
206
|
+
if (data.itemType === 'note')
|
|
207
|
+
return Promise.resolve(null);
|
|
208
|
+
return itemLastRead(raw.key);
|
|
209
|
+
}
|
|
210
|
+
/** 列表归一化:已读状态并发取,单条失败按未读(不拖垮整页)。 */
|
|
211
|
+
async function toSummaries(raws) {
|
|
212
|
+
return Promise.all(raws.map(async (raw) => toSummary(raw, await lastReadOf(raw))));
|
|
213
|
+
}
|
|
198
214
|
/** 从库名 `library.name` 或条目数据里取文库名(缺省回退)。 */
|
|
199
215
|
function libraryNameOf(raw) {
|
|
200
216
|
const first = raw[0];
|
|
@@ -327,6 +343,42 @@ export async function listCollections() {
|
|
|
327
343
|
const UNFILED_SCAN_MAX = 1000;
|
|
328
344
|
/** 一页扫描条数(本地 API 上限 100)。 */
|
|
329
345
|
const PAGE_SIZE = 100;
|
|
346
|
+
// ---- 已读状态 ---------------------------------------------------------------
|
|
347
|
+
/** 已读状态缓存时长:`lastRead` 由 Zotero 阅读器写入,分钟级刷新足够。 */
|
|
348
|
+
const READ_STATE_TTL_MS = 60_000;
|
|
349
|
+
/** 单条已读状态缓存的上限;超了就整体丢弃(重新查一遍即可)。 */
|
|
350
|
+
const READ_STATE_CACHE_MAX = 5000;
|
|
351
|
+
const readStateCache = new Map();
|
|
352
|
+
/**
|
|
353
|
+
* 条目(父条目)的最晚阅读时间(Unix 秒),`null` 表示没读过。
|
|
354
|
+
*
|
|
355
|
+
* 已读状态只记在附件上,父条目要问它的子附件(原生 `getItemLastRead()` 同理)。
|
|
356
|
+
* 按条目查而不是扫全库:单条约 6ms,只查当前列表里的条目,大口库也不会卡。
|
|
357
|
+
* 取不到按未读处理——已读标志是附加信息,不能拖垮条目列表。
|
|
358
|
+
*/
|
|
359
|
+
async function itemLastRead(key) {
|
|
360
|
+
const cached = readStateCache.get(key);
|
|
361
|
+
if (cached !== undefined && Date.now() - cached.at < READ_STATE_TTL_MS)
|
|
362
|
+
return cached.lastRead;
|
|
363
|
+
try {
|
|
364
|
+
const { body } = await requestJson(`${libraryBase()}/items/${encode(key)}/children?itemType=attachment`);
|
|
365
|
+
let lastRead = null;
|
|
366
|
+
for (const child of Array.isArray(body) ? body : []) {
|
|
367
|
+
const value = child.data?.lastRead;
|
|
368
|
+
if (typeof value !== 'number' || value <= 0)
|
|
369
|
+
continue;
|
|
370
|
+
if (lastRead === null || value > lastRead)
|
|
371
|
+
lastRead = value;
|
|
372
|
+
}
|
|
373
|
+
if (readStateCache.size >= READ_STATE_CACHE_MAX)
|
|
374
|
+
readStateCache.clear();
|
|
375
|
+
readStateCache.set(key, { at: Date.now(), lastRead });
|
|
376
|
+
return lastRead;
|
|
377
|
+
}
|
|
378
|
+
catch {
|
|
379
|
+
return null;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
330
382
|
/** 把 options 折成「顶层条目」端点基址 + 查询参数。 */
|
|
331
383
|
function itemListQuery(options) {
|
|
332
384
|
const base = options.scope === 'publications'
|
|
@@ -356,12 +408,13 @@ async function listUnfiled(offset, limit) {
|
|
|
356
408
|
break;
|
|
357
409
|
for (const raw of body) {
|
|
358
410
|
if ((raw.data?.collections ?? []).length === 0)
|
|
359
|
-
all.push(
|
|
411
|
+
all.push(raw);
|
|
360
412
|
}
|
|
361
413
|
if (body.length < PAGE_SIZE)
|
|
362
414
|
break;
|
|
363
415
|
}
|
|
364
|
-
|
|
416
|
+
const page = all.slice(offset, offset + limit);
|
|
417
|
+
return { total: all.length, items: await toSummaries(page) };
|
|
365
418
|
}
|
|
366
419
|
/** 顶层条目列表(按虚拟分类、真实分类、或全库,或关键字搜索)。 */
|
|
367
420
|
export async function listItems(options) {
|
|
@@ -369,8 +422,8 @@ export async function listItems(options) {
|
|
|
369
422
|
return listUnfiled(options.offset, options.limit);
|
|
370
423
|
const { base, params } = itemListQuery(options);
|
|
371
424
|
const { body, total } = await requestJson(`${base}?${params.toString()}`);
|
|
372
|
-
const
|
|
373
|
-
return { total: total ??
|
|
425
|
+
const raws = Array.isArray(body) ? body : [];
|
|
426
|
+
return { total: total ?? raws.length, items: await toSummaries(raws) };
|
|
374
427
|
}
|
|
375
428
|
/** 子条目(附件 / 笔记)。 */
|
|
376
429
|
export async function getItemChildren(key) {
|
package/lib/zotero/types.d.ts
CHANGED
|
@@ -31,6 +31,11 @@ export interface ZoteroItemSummary {
|
|
|
31
31
|
itemType: string;
|
|
32
32
|
/** 是否有可读附件(决定详情里能否取全文)。 */
|
|
33
33
|
hasAttachment: boolean;
|
|
34
|
+
/**
|
|
35
|
+
* 子附件里最晚的阅读时间(Unix **秒**),与 Zotero 原生 `getItemLastRead()` 一致;
|
|
36
|
+
* `null` 表示从未读过。Zotero 不记录读到第几页,只有这一个「读过没有」的信号。
|
|
37
|
+
*/
|
|
38
|
+
lastRead: number | null;
|
|
34
39
|
}
|
|
35
40
|
/** 条目下的一个附件。 */
|
|
36
41
|
export interface ZoteroAttachment {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-plugin-zotero",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.9",
|
|
4
4
|
"description": "Install this plugin and DSH can operate your Zotero for you: search the library, read PDF full text, summarize highlights and notes, organize collections by topic, import your downloaded papers, and attach a translated version back to an item. The right sidebar also gains a Zotero library panel that brings an item, an excerpt or a whole collection into the conversation with one click.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"dsh",
|
|
@@ -35,12 +35,6 @@
|
|
|
35
35
|
"engines": {
|
|
36
36
|
"dsh": ">=0.2.1-rc.3 <0.3.0-0"
|
|
37
37
|
},
|
|
38
|
-
"scripts": {
|
|
39
|
-
"build": "tsc -p tsconfig.json",
|
|
40
|
-
"build:client": "tsdown",
|
|
41
|
-
"typecheck:client": "tsc -p tsconfig.client.json --noEmit",
|
|
42
|
-
"prepack": "pnpm build && pnpm build:client"
|
|
43
|
-
},
|
|
44
38
|
"peerDependencies": {
|
|
45
39
|
"@deepseek-ai/cordis": "^4.0.5",
|
|
46
40
|
"@deepseek-ai/schemastery": "^3.18.5"
|
|
@@ -70,5 +64,10 @@
|
|
|
70
64
|
],
|
|
71
65
|
"platform": "web"
|
|
72
66
|
}
|
|
67
|
+
},
|
|
68
|
+
"scripts": {
|
|
69
|
+
"build": "tsc -p tsconfig.json",
|
|
70
|
+
"build:client": "tsdown",
|
|
71
|
+
"typecheck:client": "tsc -p tsconfig.client.json --noEmit"
|
|
73
72
|
}
|
|
74
73
|
}
|