An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Refresh copied PDF rectangle annotation images https://github.com/siyuan-note/siyuan/issues/15974
Vanessa110df4e76161
diff --git a/app/src/asset/anno.ts b/app/src/asset/anno.tsindex 08677d8ab8..b50a649bed 100644--- a/app/src/asset/anno.ts+++ b/app/src/asset/anno.ts@@ -11,7 +11,8 @@ import {isMobile} from "../util/functions";import {confirmDialog} from "../dialog/confirmDialog";import {escapeAttr, escapeHtml} from "../util/escape";import {filesize} from "filesize";-import {hideRectResizeHandles, moveRectBounds, resizeRectBounds} from "./rectAnnotationResize";+import md5 from "blueimp-md5";+import {getRectImageName, hideRectResizeHandles, moveRectBounds, resizeRectBounds} from "./rectAnnotationResize";import type {RectBounds, RectResizeDirection} from "./rectAnnotationResize";const RECT_RESIZE_MIN_SIZE = 8;@@ -903,13 +904,19 @@ export const hlPDFRect = (element: HTMLElement, id: string) => {};const copyAnno = (idPath: string, fileName: string, pdf: any) => {- const mode = rectElement.getAttribute("data-mode");- const content = rectElement.getAttribute("data-content");+ const annotationElement = rectElement;+ const mode = annotationElement.getAttribute("data-mode");+ const content = annotationElement.getAttribute("data-content");+ const pageElement = hasClosestByClassName(annotationElement, "page");+ const pageIndex = pageElement ? parseInt(pageElement.getAttribute("data-page-number")) - 1 : -1;+ const annotation = getConfig(pdf)?.[annotationElement.getAttribute("data-node-id")] as IPdfAnno;+ const positions = annotation?.pages?.find(item => item.index === pageIndex)?.positions;+ const positionHash = positions ? md5(JSON.stringify(positions)).substring(0, 7) : "";setTimeout(() => {if (mode === "rect" ||- (mode === "" && rectElement.childElementCount === 1 && content.startsWith(fileName)) // 兼容历史,以前没有 mode+ (mode === "" && annotationElement.childElementCount === 1 && content.startsWith(fileName)) // 兼容历史,以前没有 mode) {- getRectImgData(pdf).then((imageData) => {+ getRectImgData(pdf, annotationElement).then((imageData) => {fetch(imageData.url).then((response) => {return response.blob();}).then((blob) => {@@ -919,7 +926,7 @@ const copyAnno = (idPath: string, fileName: string, pdf: any) => {}confirmDialog(msg ? window.siyuan.languages.upload : "", msg, () => {const formData = new FormData();- const imageName = content.substring(0, content.length - 22) + (imageData.rotation ? `${imageData.rotation}-` : "") + content.substring(content.length - 22) + ".png";+ const imageName = getRectImageName(content, imageData.rotation, positionHash);formData.append("file[]", blob, imageName);formData.append("skipIfDuplicated", "true");fetchPost(Constants.UPLOAD_ADDRESS, formData, (response) => {@@ -935,8 +942,8 @@ const copyAnno = (idPath: string, fileName: string, pdf: any) => {}, Constants.TIMEOUT_DBLCLICK);};-async function getRectImgData(pdfObj: any) {- const pageElement = hasClosestByClassName(rectElement, "page");+async function getRectImgData(pdfObj: any, annotationElement: HTMLElement) {+ const pageElement = hasClosestByClassName(annotationElement, "page");if (!pageElement) {return;}@@ -965,7 +972,7 @@ async function getRectImgData(pdfObj: any) {viewport: captureViewport}).promise;- const rectStyle = (rectElement.firstElementChild as HTMLElement).style;+ const rectStyle = (annotationElement.firstElementChild as HTMLElement).style;const captureImageData = captureCtx.getImageData(CAPTURE_SCALE_RATIO * parseFloat(rectStyle.left),CAPTURE_SCALE_RATIO * parseFloat(rectStyle.top),diff --git a/app/src/asset/rectAnnotationResize.test.ts b/app/src/asset/rectAnnotationResize.test.tsindex b64dd3fcc2..ecd59897db 100644--- a/app/src/asset/rectAnnotationResize.test.ts+++ b/app/src/asset/rectAnnotationResize.test.ts@@ -1,6 +1,6 @@import {describe, it} from "node:test";import * as assert from "node:assert/strict";-import {hideRectResizeHandles, moveRectBounds, resizeRectBounds} from "./rectAnnotationResize";+import {getRectImageName, hideRectResizeHandles, moveRectBounds, resizeRectBounds} from "./rectAnnotationResize";const initial = {left: 20, top: 30, right: 80, bottom: 90};const boundary = {left: 0, top: 0, right: 100, bottom: 120};@@ -58,6 +58,16 @@ describe("rectangle annotation resizing", () => {{left: 40, top: 60, right: 100, bottom: 120});});+ it("uses the rectangle position hash in copied image names", () => {+ const content = "example-P1-20260809120000-abcdefg";+ assert.equal(getRectImageName(content, 0, "0123456"),+ "example-P1-20260809120000-0123456.png");+ assert.equal(getRectImageName(content, 90, "7654321"),+ "example-P1-90-20260809120000-7654321.png");+ assert.equal(getRectImageName(content, 0, ""),+ "example-P1-20260809120000-abcdefg.png");+ });+it("keeps resized rectangles within the page", () => {assert.deepEqual(resizeRectBounds(initial, boundary, "nw", -20, -10, 8),{left: 0, top: 0, right: 80, bottom: 90});diff --git a/app/src/asset/rectAnnotationResize.ts b/app/src/asset/rectAnnotationResize.tsindex db7279c5ee..79e0689610 100644--- a/app/src/asset/rectAnnotationResize.ts+++ b/app/src/asset/rectAnnotationResize.ts@@ -40,6 +40,12 @@ export const moveRectBounds = (initial: RectBounds, boundary: RectBounds, deltaX};};+export const getRectImageName = (content: string, rotation: number, positionHash: string) => {+ const annotationID = content.substring(content.length - 22);+ const imageID = positionHash ? annotationID.substring(0, 15) + positionHash.substring(0, 7) : annotationID;+ return content.substring(0, content.length - 22) + (rotation ? `${rotation}-` : "") + imageID + ".png";+};+export const hideRectResizeHandles = (element: ParentNode) => {element.querySelectorAll(".pdf__rect--selected").forEach(item => {item.classList.remove("pdf__rect--selected");