Skip to content

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

:bug: Restore inline image renaming after undo https://github.com/siyuan-note/siyuan/issues/19978

Danield9bf6fddc5dc
diff --git a/app/src/protyle/render/imageDisplay.test.ts b/app/src/protyle/render/imageDisplay.test.ts
index 6dccb70fd8..e3981397d1 100644
--- a/app/src/protyle/render/imageDisplay.test.ts
+++ b/app/src/protyle/render/imageDisplay.test.ts
@@ -68,6 +68,15 @@ const browserCases = async (source: string, luteSource: string) => {
check.equal(root.querySelector(".img__name"), null);
check.equal(root.querySelector(".img__alt"), null);
check.equal(lute.BlockDOM2StdMd(root.innerHTML), original);
+ config.editor.displayImgName = true;
+ root.removeAttribute("data-notebook-id");
+ render(root);
+ const snapshot = root.innerHTML;
+ root.innerHTML = snapshot;
+ render(root);
+ const restoredName = root.querySelector<HTMLElement>(".img__name");
+ restoredName.click();
+ check.ok(restoredName.querySelector("input"), "restored image names remain editable");
return "Image display cases passed";
};
diff --git a/app/src/protyle/render/imageDisplay.ts b/app/src/protyle/render/imageDisplay.ts
index a20d2505d9..6c8dbe0299 100644
--- a/app/src/protyle/render/imageDisplay.ts
+++ b/app/src/protyle/render/imageDisplay.ts
@@ -1,6 +1,8 @@
import {getAssetExtension, getAssetName, isEncryptedBox} from "../../util/pathName";
import {renameAssetFile, validateName} from "../../editor/rename";
+const boundImageNames = new WeakSet<HTMLElement>();
+
const setImageName = (element: HTMLElement, path: string) => {
const text = document.createElement("span");
text.textContent = getAssetName(path) + getAssetExtension(path);
@@ -38,6 +40,10 @@ export const renderImageDisplay = (root: Element) => {
name.setAttribute("role", "button");
name.setAttribute("aria-label", window.siyuan.languages.rename);
container.insertBefore(name, image);
+ }
+ if (!boundImageNames.has(name)) {
+ boundImageNames.add(name);
+ setImageName(name, path);
name.addEventListener("mousedown", event => event.stopPropagation());
name.addEventListener("keydown", (event: KeyboardEvent) => {
if (event.target === name && (event.key === "Enter" || event.key === " ") && !event.isComposing) {
@@ -55,7 +61,7 @@ export const renderImageDisplay = (root: Element) => {
const wysiwyg = image.closest(".protyle-wysiwyg");
const source = image.getAttribute("data-src");
if (!wysiwyg || wysiwyg.getAttribute("data-readonly") === "true" || window.siyuan.config.readonly ||
- window.siyuan.isPublish || source.includes("?box=") ||
+ window.siyuan.isPublish || /[?&]box=/.test(source) ||
isEncryptedBox(image.closest("[data-notebook-id]")?.getAttribute("data-notebook-id"))) {
return;
}