实现pdf中划句子ai伴读的功能,修复pdf高亮显示的问题

This commit is contained in:
yhy
2026-08-29 23:18:15 +08:00
parent 0ed8f42029
commit 913e3675ae
14 changed files with 789 additions and 68 deletions
@@ -1,11 +1,15 @@
import { config } from "../../../../package.json";
import { getString } from "../../../utils/locale";
import { cacheReaderSelection } from "../../pdf/selection";
import { cacheReaderSelection, getCachedReaderSelectionPosition } from "../../pdf/selection";
import { highlightTextInPdf } from "../../pdf/readerSync";
import { getRegularItem, resolvePaperAttachment } from "../infrastructure/zotero/adapter";
import { paperIdFromCacheKey } from "../infrastructure/pdf/fileHash";
import { explainSelectionLecture } from "../application/explainSelectionLecture";
import { playSelectionExplainStandalone } from "../application/playSelectionExplain";
import {
beginSelectionExplainSession,
isSelectionExplainSessionActive,
playSelectionExplainExclusive,
} from "../application/playSelectionExplain";
import { lectureEvents } from "../infrastructure/events";
function tryString(value: unknown): string {
@@ -47,6 +51,7 @@ export async function runSelectionExplainFromReader(
}
cacheReaderSelection(text);
const sessionId = beginSelectionExplainSession();
showHeadline(getString("lecture-selection-explaining"));
try {
@@ -57,9 +62,12 @@ export async function runSelectionExplainFromReader(
onProgress: (message) => showHeadline(message),
});
if (!isSelectionExplainSessionActive(sessionId)) return;
await highlightTextInPdf(item, {
text: result.original,
page: result.page,
position: getCachedReaderSelectionPosition(result.original),
});
lectureEvents.emit("lecture:selection_explain", {
@@ -69,7 +77,9 @@ export async function runSelectionExplainFromReader(
result,
});
await playSelectionExplainStandalone(result);
if (!isSelectionExplainSessionActive(sessionId)) return;
await playSelectionExplainExclusive(result);
showHeadline(getString("lecture-selection-done"));
} catch (e) {
const msg = e instanceof Error ? e.message : String(e);
@@ -96,11 +106,11 @@ export function registerSelectionExplainPopup(): void {
"toolbar-button wide-button chatpapers-selection-explain-btn";
btn.textContent = getString("lecture-explain-selection-short");
btn.title = getString("lecture-explain-selection");
btn.addEventListener("click", (ev) => {
btn.addEventListener("click", (ev: Event) => {
ev.preventDefault();
ev.stopPropagation();
void runSelectionExplainFromReader(reader, text);
});
}, { once: true });
append(btn);
} catch (e) {
ztoolkit.log("[ChatPapers:Lecture] selection popup button failed", e);