Skip to content

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

:bug: Display image names and descriptions immediately after insertion https://github.com/siyuan-note/siyuan/issues/19999

Daniel4f326efd14ea
diff --git a/app/src/protyle/wysiwyg/transaction.ts b/app/src/protyle/wysiwyg/transaction.ts
index 346aeb7cea..c525fb108e 100644
--- a/app/src/protyle/wysiwyg/transaction.ts
+++ b/app/src/protyle/wysiwyg/transaction.ts
@@ -25,6 +25,7 @@ import {Constants} from "../../constants";
import {blockRender} from "../render/blockRender";
import {renderEmbedHeadings} from "../render/embedHeading";
import {processRender} from "../util/processCode";
+import {renderImageDisplay} from "../render/imageDisplay";
import {highlightRender} from "../render/highlightRender";
import {hasClosestBlock, hasClosestByAttribute, hasTopClosestByAttribute, isInEmbedBlock} from "../util/hasClosest";
import {zoomOut} from "../../menus/protyle";
@@ -2491,6 +2492,12 @@ export const transaction = (protyle: IProtyle, doOperations: IOperation[], undoO
protyle.updated = true;
protyle.undo.add(doOperations, undoOperations, protyle);
}
+ // 本地插入和正在编辑的块保留现有 DOM,立即补充图片文件名和提示文本。
+ doOperations.forEach(operation => {
+ if ((operation.action === "insert" || operation.action === "update") && operation.data.includes("<img ")) {
+ protyle.wysiwyg.element.querySelectorAll(`[data-node-id="${operation.id}"]`).forEach(renderImageDisplay);
+ }
+ });
if (protyle?.lite) {
if (!options?.skipSync) {
syncTrackedRanges(protyle, doOperations, options?.trackedRangeInsertion);
diff --git a/app/src/protyle/wysiwyg/transactionImageDisplay.test.ts b/app/src/protyle/wysiwyg/transactionImageDisplay.test.ts
new file mode 100644
index 0000000000..3f1899527f
--- /dev/null
+++ b/app/src/protyle/wysiwyg/transactionImageDisplay.test.ts
@@ -0,0 +1,155 @@
+import * as assert from "node:assert/strict";
+import {execFile} from "node:child_process";
+import {mkdtempSync, readFileSync, rmSync, writeFileSync} from "node:fs";
+import {tmpdir} from "node:os";
+import * as path from "node:path";
+import {test} from "node:test";
+import {promisify} from "node:util";
+import {ModuleKind, ScriptTarget, transpileModule} from "typescript";
+
+const browserCases = (transactionSource: string, displaySource: string, luteSource: string) => {
+ const check: typeof assert = require("node:assert/strict");
+ new Function(luteSource)();
+ const lute = Lute.New();
+ const config = {editor: {displayImgName: true, displayImgAlt: true}, fileTree: {openFilesUseCurrentTab: false}};
+ Object.assign(window, {siyuan: {config, languages: {rename: "Rename"}}});
+ const getAssetExtension = (value: string) => require("node:path").posix.extname(value);
+ const displayDependencies = {
+ getAssetExtension,
+ getAssetName: (value: string) => require("node:path").posix.basename(value, getAssetExtension(value))
+ .replace(/-\d{14}-\w{7}$/, ""),
+ isEncryptedBox: () => false,
+ renameAsset: () => {},
+ };
+ const renderImageDisplay = new Function(...Object.keys(displayDependencies), displaySource +
+ "\nreturn renderImageDisplay;")(...Object.values(displayDependencies));
+ let queued = 0;
+ const identity = (html: string) => html;
+ const dependencies = {
+ renderImageDisplay,
+ Constants: {ATTRIBUTE_EDITING: "data-editing"},
+ prepareViewFoldTransaction: (_protyle: unknown, doOperations: unknown, undoOperations: unknown) =>
+ ({doOperations, undoOperations}),
+ getProtyleTransactionOwner: (): undefined => undefined,
+ cleanBlockSelectionModeHTML: identity,
+ cleanTableCellRichHTML: identity,
+ cleanTableVirtualizationHTML: identity,
+ cleanListMindmapHTML: identity,
+ restoreInlineElementBoundaryHTML: identity,
+ retainTableCellRichMetadata: identity,
+ normalizeHTMLAssetIFrameBlockDOM: identity,
+ cleanHeadingNumberOperations: () => {},
+ getEmbedChildOperationContext: (): undefined => undefined,
+ isInEmbedBlock: (): undefined => undefined,
+ syncTrackedRanges: () => {},
+ queueTransaction: () => queued++,
+ queueTransactionBatch: () => queued++,
+ processRender: () => { throw new Error("Local image edits must preserve the current DOM"); },
+ };
+ const exports = {} as typeof import("./transaction");
+ new Function("exports", "require", transactionSource)(exports, () => dependencies);
+ const root = document.createElement("div");
+ root.className = "protyle-wysiwyg";
+ document.body.append(root);
+ const protyle = {wysiwyg: {element: root}, options: {}, undo: {add: () => {}}, lite: false} as unknown as IProtyle;
+ const imageMarkdown = "![description](assets/example-20260930090000-abcdefg.png \"caption\")";
+
+ // 新段落、列表和表格中的图片均在事务发送前显示,保存内容及光标保持不变。
+ for (const markdown of [imageMarkdown, "- " + imageMarkdown,
+ "| Image |\n| --- |\n| " + imageMarkdown + " |", imageMarkdown + "\n\n" + imageMarkdown]) {
+ root.innerHTML = lute.Md2BlockDOM("anchor\n\n" + markdown);
+ const anchor = root.firstElementChild;
+ const inserted = Array.from(root.children).slice(1);
+ const range = document.createRange();
+ range.selectNodeContents(inserted[inserted.length - 1].querySelector("[contenteditable=\"true\"]"));
+ range.collapse(false);
+ getSelection().removeAllRanges();
+ getSelection().addRange(range);
+ const before = {node: range.startContainer, offset: range.startOffset};
+ const original = lute.BlockDOM2StdMd(root.innerHTML);
+ const operations = inserted.map(element => ({
+ action: "insert" as const, id: element.getAttribute("data-node-id"),
+ previousID: anchor.getAttribute("data-node-id"), data: element.outerHTML,
+ }));
+ const savedData = operations.map(operation => operation.data);
+ const previousQueued = queued;
+ exports.transaction(protyle, operations);
+ check.equal(queued, previousQueued + 1);
+ check.equal(root.querySelectorAll(".img__name").length, root.querySelectorAll(".img img").length);
+ root.querySelectorAll(".img__name").forEach(name => check.equal(name.textContent, "example.png"));
+ root.querySelectorAll(".img__alt").forEach(alt => check.equal(alt.textContent, "description"));
+ check.equal(root.querySelectorAll(".img__alt").length, root.querySelectorAll(".img img").length);
+ check.equal(lute.BlockDOM2StdMd(root.innerHTML), original);
+ check.deepEqual(operations.map(operation => operation.data), savedData);
+ check.equal(getSelection().anchorNode, before.node);
+ check.equal(getSelection().anchorOffset, before.offset);
+ }
+
+ // 行内插入和后续修改提示文本走当前编辑块的更新分支,无需重新打开文档。
+ root.innerHTML = lute.Md2BlockDOM("before " + imageMarkdown + " after");
+ const block = root.firstElementChild;
+ const editable = block.querySelector("[contenteditable=\"true\"]");
+ const range = document.createRange();
+ range.selectNodeContents(editable);
+ range.collapse(false);
+ getSelection().removeAllRanges();
+ getSelection().addRange(range);
+ const update = () => {
+ block.setAttribute("data-editing", "true");
+ exports.transaction(protyle, [{action: "update", id: block.getAttribute("data-node-id"), data: block.outerHTML}]);
+ check.equal(root.firstElementChild, block);
+ check.equal(getSelection().anchorNode, editable);
+ };
+ update();
+ check.equal(block.querySelector(".img__name").textContent, "example.png");
+ check.equal(block.querySelector(".img__alt").textContent, "description");
+ block.querySelector("img").alt = "updated description";
+ update();
+ check.equal(block.querySelector(".img__alt").textContent, "updated description");
+ config.editor.displayImgName = false;
+ config.editor.displayImgAlt = false;
+ update();
+ check.equal(block.querySelector(".img__name"), null);
+ check.equal(block.querySelector(".img__alt"), null);
+ return "Image transaction cases passed";
+};
+
+test("local image insertions and updates display names and descriptions before submission", {
+ skip: process.platform === "linux" && !process.env.DISPLAY && !process.env.WAYLAND_DISPLAY,
+ timeout: 45000,
+}, async () => {
+ const transactionSource = transpileModule(readFileSync(path.join(__dirname, "transaction.ts"), "utf8"), {
+ compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2021},
+ }).outputText;
+ const displaySource = transpileModule(readFileSync(path.join(__dirname, "../render/imageDisplay.ts"), "utf8")
+ .replace(/^import [\s\S]*?;\r?\n/gm, "").replace(/^export /gm, ""),
+ {compilerOptions: {target: ScriptTarget.ES2021}}).outputText;
+ const luteSource = readFileSync("stage/protyle/js/lute/lute.min.js", "utf8");
+ const temporary = mkdtempSync(path.join(tmpdir(), "siyuan-image-transaction-test-"));
+ const script = path.join(temporary, "run.cjs");
+ const code = "(() => { try { const __name = value => value; return (" + browserCases.toString() + ")(" +
+ JSON.stringify(transactionSource) + "," + JSON.stringify(displaySource) + "," + JSON.stringify(luteSource) +
+ "); } catch (error) { return error.stack; } })()";
+ writeFileSync(script, `const {app, BrowserWindow} = require("electron");
+app.setPath("userData", ${JSON.stringify(path.join(temporary, "profile"))});
+app.whenReady().then(async () => {
+ const win = new BrowserWindow({show: false, webPreferences: {nodeIntegration: true, contextIsolation: false}});
+ try {
+ await win.loadURL("data:text/html,<html><body></body></html>");
+ console.log(await win.webContents.executeJavaScript(${JSON.stringify(code)}));
+ app.exit(0);
+ } catch (error) { console.error(error); app.exit(1); }
+});`, "utf8");
+ const env = {...process.env};
+ delete env.ELECTRON_RUN_AS_NODE;
+ try {
+ const result = await promisify(execFile)(require("electron") as unknown as string, [script],
+ {env, windowsHide: true, timeout: 40000});
+ assert.match(result.stdout, /Image transaction cases passed/);
+ } finally {
+ if (path.dirname(path.resolve(temporary)) === path.resolve(tmpdir()) &&
+ path.basename(temporary).startsWith("siyuan-image-transaction-test-")) {
+ rmSync(temporary, {recursive: true, force: true});
+ }
+ }
+});