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: Restore image OCR actions after undo and source changes https://github.com/siyuan-note/siyuan/issues/19977
Daniel677c1260beb2
diff --git a/app/src/asset/imageOCR.test.ts b/app/src/asset/imageOCR.test.tsindex 879e1c26f9..86161d2e2e 100644--- a/app/src/asset/imageOCR.test.ts+++ b/app/src/asset/imageOCR.test.ts@@ -63,7 +63,7 @@ const browserCases = async (source: string, actionsSource: string) => {render(root);render(root);check.equal(root.querySelectorAll(".protyle-action__ocr").length, 1);- const action = root.querySelector<HTMLElement>(".protyle-action__ocr");+ let action = root.querySelector<HTMLElement>(".protyle-action__ocr");action.click();await new Promise(resolve => setTimeout(resolve, 30));check.equal(copies, 1);@@ -76,6 +76,16 @@ const browserCases = async (source: string, actionsSource: string) => {root.setAttribute("data-readonly", "true");action.dispatchEvent(new MouseEvent("dblclick", {bubbles: true}));check.equal(opens, 1);+ const snapshot = root.innerHTML;+ root.innerHTML = snapshot;+ render(root);+ action = root.querySelector<HTMLElement>(".protyle-action__ocr");+ action.click();+ await new Promise(resolve => setTimeout(resolve, 30));+ check.equal(copies, 2, "restored image actions still copy OCR text");+ root.querySelector("img").setAttribute("data-src", "https://example.com/image.png");+ render(root);+ check.equal(root.querySelector(".protyle-action__ocr"), null);return "Image OCR cases passed";};diff --git a/app/src/menus/protyle.ts b/app/src/menus/protyle.tsindex 5274f029be..4425784eb0 100644--- a/app/src/menus/protyle.ts+++ b/app/src/menus/protyle.ts@@ -49,6 +49,7 @@ import * as dayjs from "dayjs";import {blockRender} from "../protyle/render/blockRender";import {renameAsset} from "../editor/rename";import {renderImageDisplay} from "../protyle/render/imageDisplay";+import {renderImageActions} from "../protyle/render/imageActions";import {electronUndo} from "../protyle/undo";import {pushBack} from "../mobile/util/MobileBackFoward";import {copyPNGByLink, exportAsset, writeAssetToClipboard} from "./util";@@ -1610,6 +1611,7 @@ export const imgMenu = (protyle: IProtyle, range: Range, assetElement: HTMLElemeimgElement.setAttribute("alt", textElements[2].value.replace(/\n|\r\n|\r|\u2028|\u2029/g, ""));renderImageDisplay(assetElement);+ renderImageActions(assetElement);nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));updateTransaction(protyle, nodeElement, html);};diff --git a/app/src/protyle/render/imageActions.ts b/app/src/protyle/render/imageActions.tsindex 61468692b9..f30eb39f9c 100644--- a/app/src/protyle/render/imageActions.ts+++ b/app/src/protyle/render/imageActions.ts@@ -2,15 +2,28 @@ import {Constants} from "../../constants";import {copyImageOCRText, openImageOCR} from "../../asset/imageOCR";import {isEncryptedBox} from "../../util/pathName";+const boundImageActions = new WeakSet<HTMLElement>();+export const renderImageActions = (root: Element) => {root.querySelectorAll<HTMLImageElement>(".img img").forEach(image => {const actions = image.parentElement.querySelector(".protyle-icons");- if (!actions || actions.querySelector(".protyle-action__ocr") || window.siyuan.isPublish ||+ const existing = actions?.querySelector<HTMLElement>(".protyle-action__ocr");+ if (!actions || window.siyuan.isPublish ||!image.closest(".protyle-wysiwyg") || !image.getAttribute("data-src")?.startsWith("assets/") ||+ /[?&]box=/.test(image.getAttribute("data-src")) ||isEncryptedBox(image.closest("[data-notebook-id]")?.getAttribute("data-notebook-id"))) {+ existing?.remove();+ if (actions?.childElementCount === 1) {+ actions.querySelector(".protyle-icon--last")?.classList.replace("protyle-icon--last", "protyle-icon--only");+ }+ return;+ }+ if (existing && boundImageActions.has(existing)) {return;}+ existing?.remove();const action = document.createElement("span");+ boundImageActions.add(action);action.className = "protyle-icon protyle-icon--first protyle-action__ocr ariaLabel";action.tabIndex = 0;action.setAttribute("role", "button");@@ -23,7 +36,7 @@ export const renderImageActions = (root: Element) => {event.preventDefault();clearTimeout(timer);timer = window.setTimeout(() => {- if (image.isConnected) {+ if (image.isConnected && action.isConnected) {copyImageOCRText(image.getAttribute("data-src"));}}, Constants.TIMEOUT_DBLCLICK);