Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Cancel PDF annotation copying after viewer teardown https://github.com/siyuan-note/siyuan/issues/20394

Vanessac9a3f7b2707b
diff --git a/app/src/asset/anno.ts b/app/src/asset/anno.ts
index 0fb4852de2..f399d2f8b9 100644
--- a/app/src/asset/anno.ts
+++ b/app/src/asset/anno.ts
@@ -1010,13 +1010,20 @@ export const hlPDFRect = (element: HTMLElement, id: string) => {
const copyAnno = (idPath: string, fileName: string, pdf: any, annotationElement = rectElement, automatic = false) => {
const request = {};
rectCopyRequests.set(annotationElement, request);
+ const pdfDocument = pdf.pdfDocument;
+ // 复制过程跨越计时器和异步截图,关闭或替换阅读器后不再访问页面及写入剪贴板。
+ const isActive = () => !pdf._destroyed && pdf.pdfDocument === pdfDocument &&
+ annotationElement.isConnected && rectCopyRequests.get(annotationElement) === request;
const canCopy = async () => {
+ if (!isActive()) {
+ return false;
+ }
if (!automatic) {
return true;
}
try {
const text = await readText(true);
- return rectCopyRequests.get(annotationElement) === request && annotationElement.isConnected &&
+ return isActive() &&
typeof text === "string" && text.startsWith(`<<${idPath} `);
} catch (error) {
return false;
@@ -1041,7 +1048,10 @@ const copyAnno = (idPath: string, fileName: string, pdf: any, annotationElement
if (!position || pageIndex < 0) {
return;
}
- getRectImgData(pdf, pageIndex + 1, position).then((imageData) => {
+ getRectImgData(pdf, pageIndex + 1, position).then(async (imageData) => {
+ if (!await canCopy()) {
+ return;
+ }
let msg = "";
if (!automatic && Constants.SIZE_UPLOAD_TIP_SIZE <= imageData.blob.size) {
msg = window.siyuan.languages.uploadFileTooLarge.replace("${x}", content + ".png")
@@ -1076,7 +1086,9 @@ const copyAnno = (idPath: string, fileName: string, pdf: any, annotationElement
});
});
}).catch((error) => {
- console.error(error);
+ if (isActive()) {
+ console.error(error);
+ }
});
} else {
writeText(`<<${idPath} "${content}">>`);
diff --git a/app/tests/pdfAnnotationInteractions.test.js b/app/tests/pdfAnnotationInteractions.test.js
index 8958c384d2..5a6031dc0e 100644
--- a/app/tests/pdfAnnotationInteractions.test.js
+++ b/app/tests/pdfAnnotationInteractions.test.js
@@ -118,6 +118,9 @@ async function runElectron() {
await wait();container.remove();
}
pdf.appConfig.mainContainer.dispatchEvent(new Event("scroll"));
+ // 手动复制尚在等待截图时关闭阅读器,不能再读取已销毁的页面。
+ window.pdfTestMobile = false;
+ element.querySelector('.pdf__util [data-type="copy"]').click();
await pdf.destroy(); element.remove(); await wait();
const immediate = document.createElement("div"); immediate.className = "testViewer";
immediate.innerHTML = getPdfViewerHTML();document.body.append(immediate);