Skip to content

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

:bug: Repair image interactions in mind maps https://github.com/siyuan-note/siyuan/issues/20724

Danieldf66aa2bf4a7
diff --git a/app/src/protyle/render/imageResize.test.ts b/app/src/protyle/render/imageResize.test.ts
index f0ba44ac8f..9a197dfa29 100644
--- a/app/src/protyle/render/imageResize.test.ts
+++ b/app/src/protyle/render/imageResize.test.ts
@@ -159,6 +159,68 @@ const browserCases = async (source: string, resizeSource: string, luteSource: st
check.equal(root.querySelectorAll(".protyle-block-resize").length, 3, "replay does not duplicate controls");
}
}
+ // 脑图隔离冒泡事件,图片缩放仍需收到节点内部的移动和松开事件。
+ for (const isolated of [false, true]) {
+ const image = await create();
+ tap(image);
+ const events = new AbortController();
+ if (isolated) {
+ for (const type of ["mousemove", "mouseup"]) {
+ root.addEventListener(type, event => event.stopPropagation(), {signal: events.signal});
+ }
+ }
+ updates.length = 0;
+ const width = image.clientWidth;
+ begin(image, "width", new MouseEvent("mousedown", {clientX: 300, clientY: 200}));
+ image.dispatchEvent(new MouseEvent("mousemove", {bubbles: true, clientX: 280, clientY: 200}));
+ check.equal(image.clientWidth, width - 20, "native movement resizes once, including inside an isolated editor");
+ image.dispatchEvent(new MouseEvent("mouseup", {bubbles: true}));
+ check.equal(updates.length, 1, "releasing inside the image commits one resize");
+ check.equal(image.closest(".img").classList.contains("img--drag"), false);
+ check.equal(image.parentElement.querySelector(".touch-resize-active"), null);
+ check.equal(document.onmousemove, null);
+ image.dispatchEvent(new MouseEvent("mousemove", {bubbles: true, clientX: 260, clientY: 200}));
+ check.equal(image.clientWidth, width - 20, "release removes the capture listener");
+ events.abort();
+ }
+ const mindmapNode = document.createElement("div");
+ mindmapNode.className = "mindmap-view__node mindmap-view__node--editing";
+ mindmapNode.style.position = "relative";
+ document.body.append(mindmapNode);
+ mindmapNode.append(root);
+ root.style.width = "max-content";
+ root.style.maxWidth = "100%";
+ root.style.padding = "0";
+ const isolatedEvents = new AbortController();
+ for (const type of ["mousemove", "mouseup"]) {
+ mindmapNode.addEventListener(type, event => event.stopPropagation(), {signal: isolatedEvents.signal});
+ }
+ for (const scale of [0.65, 1, 1.5]) {
+ for (const centered of [false, true]) {
+ const image = await create();
+ mindmapNode.style.transform = `scale(${scale})`;
+ const block = root.firstElementChild as HTMLElement;
+ block.style.textAlign = centered ? "center" : "";
+ await settle();
+ updates.length = 0;
+ begin(image, "width", new MouseEvent("mousedown", {clientX: 300, clientY: 200}));
+ image.dispatchEvent(new MouseEvent("mousemove", {bubbles: true,
+ clientX: 300 + 40 * scale / (centered ? 2 : 1), clientY: 200}));
+ check.equal(image.clientWidth, 240, "mindmap images can grow beyond the current editor width at every canvas scale");
+ image.dispatchEvent(new MouseEvent("mousemove", {bubbles: true, clientX: 3000, clientY: 200}));
+ check.ok(image.clientWidth > 240 && image.clientWidth <= 288,
+ `enlarging respects the node maximum width and padding: width=${image.clientWidth}, scale=${scale}, centered=${centered}`);
+ check.equal(mindmapNode.offsetWidth, 300, "the image can fill the node up to its width limit");
+ image.dispatchEvent(new MouseEvent("mouseup", {bubbles: true}));
+ check.equal(updates.length, 1);
+ check.equal(document.onmousemove, null);
+ }
+ }
+ isolatedEvents.abort();
+ mindmapNode.replaceWith(root);
+ root.style.width = "";
+ root.style.maxWidth = "";
+ root.style.padding = "";
const bridgeCore = {};
new Function("exports", coreSource)(bridgeCore);
const bridge = {} as {initTouchDragBridge: () => void};
@@ -169,6 +231,10 @@ const browserCases = async (source: string, resizeSource: string, luteSource: st
});
Object.assign(window, {siyuan: {touchDragActive: false}});
bridge.initTouchDragBridge();
+ const touchIsolation = new AbortController();
+ for (const type of ["mousemove", "mouseup"]) {
+ root.addEventListener(type, event => event.stopPropagation(), {signal: touchIsolation.signal});
+ }
for (const axis of ["width", "height", "both"]) {
const image = await create();
tap(image);
@@ -208,6 +274,7 @@ const browserCases = async (source: string, resizeSource: string, luteSource: st
check.equal(handle.classList.contains("touch-resize-active"), false);
check.equal(getComputedStyle(handle).visibility, "visible", "cancelling a drag preserves selected handles");
}
+ touchIsolation.abort();
for (const [width, height] of [[24, 12], [200, 20], [40, 100]]) {
const compact = await create();
compact.style.width = width + "px";
diff --git a/app/src/protyle/render/listMindmap/editor.test.ts b/app/src/protyle/render/listMindmap/editor.test.ts
index c74b26dffc..15afe947e4 100644
--- a/app/src/protyle/render/listMindmap/editor.test.ts
+++ b/app/src/protyle/render/listMindmap/editor.test.ts
@@ -331,6 +331,22 @@ const browserCases = async (sourceCode: string, css: string, cleanupSource: stri
imageEditor.editor.pasteImages(images);
check.equal(imageEditor.state.uploads.length, 1, "closed editors do not accept another upload");
await imageEditor.remove();
+ for (const resized of [false, true]) {
+ const current = create('<div data-node-id="image-body" data-type="NodeParagraph"><span data-type="img" class="img">' +
+ '<span class="protyle-image-container" style="width: 180px;"><img src="assets/image.png" alt="description"></span></span></div>');
+ const image = current.wysiwyg.querySelector("img");
+ image.closest(".img").classList.add("img--select", "img--drag");
+ image.parentElement.classList.add("protyle-image-container--compact");
+ image.parentElement.insertAdjacentHTML("beforeend", '<span class="protyle-image-resize"><span class="touch-resize-active"></span></span>');
+ if (resized) {
+ image.parentElement.style.width = "140px";
+ }
+ check.equal(await current.editor.finish(), true);
+ check.equal(current.state.saves.length, resized ? 1 : 0, "image UI changes alone do not create a content save");
+ check.equal(current.element.querySelector(".img--select, .img--drag, .protyle-image-resize, .protyle-image-container--compact"), null);
+ check.equal(current.element.querySelector("img").parentElement.style.width, resized ? "140px" : "180px");
+ await current.remove();
+ }
// 关闭等待内层编辑结束时启动上传,也不能提前销毁外层编辑器。
const uploadingEditor = create();
const nestedGate = gate();
diff --git a/app/src/protyle/render/listMindmap/model.test.ts b/app/src/protyle/render/listMindmap/model.test.ts
index 366d0eeff4..ab111e6b6f 100644
--- a/app/src/protyle/render/listMindmap/model.test.ts
+++ b/app/src/protyle/render/listMindmap/model.test.ts
@@ -959,6 +959,23 @@ const browserCases = async (sourceCode: string, css: string, taskSource: string,
check.equal(api.readListMindmap(list).nodes.get(editable.id).children.length, 2);
check.equal(childList.outerHTML, childHTML);
+ // 图片编辑状态不进入脑图快照,单独的正文片段也要清理,并保留实际图片尺寸和说明。
+ const imageSnapshot = document.createElement("div");
+ imageSnapshot.innerHTML = lute.Md2BlockDOM('![description](assets/image.png "caption")');
+ const snapshotImage = imageSnapshot.querySelector("img");
+ snapshotImage.parentElement.className = "protyle-image-container";
+ snapshotImage.parentElement.style.width = "180px";
+ const cleanImage = api.cleanListMindmapHTML(imageSnapshot.innerHTML);
+ snapshotImage.closest(".img").classList.add("img--select", "img--drag");
+ snapshotImage.parentElement.classList.add("protyle-image-container--compact");
+ snapshotImage.parentElement.insertAdjacentHTML("beforeend", '<span class="protyle-image-resize">' +
+ '<span class="protyle-block-resize touch-resize-active" data-resize-axis="both"></span></span>');
+ check.equal(api.cleanListMindmapHTML(imageSnapshot.innerHTML), cleanImage);
+ for (const state of ["img--select", "img--drag"]) {
+ check.equal(api.cleanListMindmapHTML(`<span class="img ${state}"><img src="assets/image.png"></span>`),
+ '<span class="img"><img src="assets/image.png"></span>');
+ }
+
// 渲染和编辑标记不会进入持久化 DOM,列表视图属性及关系线配置仍然保留。
list.setAttribute("custom-sy-list-mindmap", "1");
list.setAttribute("data-mindmap-view-rendered", "true");
diff --git a/app/src/protyle/render/listMindmap/model.ts b/app/src/protyle/render/listMindmap/model.ts
index 919d7e3195..ef46804981 100644
--- a/app/src/protyle/render/listMindmap/model.ts
+++ b/app/src/protyle/render/listMindmap/model.ts
@@ -344,7 +344,10 @@ export const readListMindmap = (list: HTMLElement): ListMindmapModel => {
};
const cleanListMindmapDOM = (root: Element | DocumentFragment) => {
- root.querySelectorAll(".mindmap-view, .list-mindmap").forEach(element => element.remove());
+ root.querySelectorAll(".mindmap-view, .list-mindmap, .protyle-image-resize").forEach(element => element.remove());
+ root.querySelectorAll(".img--select, .img--drag, .protyle-image-container--compact").forEach(element => {
+ element.classList.remove("img--select", "img--drag", "protyle-image-container--compact");
+ });
// 页签导航和面板标识由渲染器生成,不参与正文比较或脑图节点事务。
root.querySelectorAll(".tabs-header, .tabs-divider, .tabs-attributes").forEach(element => element.remove());
root.querySelectorAll('[data-type="NodeTabs"], [data-type="NodeTabItem"]').forEach(element => {
@@ -377,7 +380,8 @@ const cleanListMindmapDOM = (root: Element | DocumentFragment) => {
};
export const cleanListMindmapHTML = (html: string): string => {
- if (!html.includes("mindmap-view") && !html.includes("list-mindmap") && !html.includes("NodeTabs")) {
+ if (!html.includes("mindmap-view") && !html.includes("list-mindmap") && !html.includes("NodeTabs") &&
+ !html.includes("protyle-image") && !html.includes("img--select") && !html.includes("img--drag")) {
return html;
}
const template = document.createElement("template");
diff --git a/app/src/protyle/render/listMindmap/preview.test.ts b/app/src/protyle/render/listMindmap/preview.test.ts
index 6f3c5ac438..7523c43b8b 100644
--- a/app/src/protyle/render/listMindmap/preview.test.ts
+++ b/app/src/protyle/render/listMindmap/preview.test.ts
@@ -168,7 +168,10 @@ const cases = async (source: string, css: string) => {
(type === "o" ? "1." : `<svg><use href="#${type === "u" ? "iconDot" : "iconCheck"}"></use></svg>`) +
`</div><div class="p" data-type="NodeParagraph" data-table-cell-node><div data-table-cell-content>Item ${index}</div>` +
'<div class="protyle-attr"></div></div><div class="protyle-attr"></div></div>').join("") +
- '</div><span class="img"><span class="protyle-action">Image controls</span></span>' +
+ '</div><span data-type="img" class="img img--select img--drag"><span class="protyle-image-container protyle-image-container--compact" style="width: 180px;">' +
+ '<img src="assets/image.png" alt="description"><span class="protyle-image-resize" style="width: 0px; height: 0px;">' +
+ '<span class="protyle-block-resize touch-resize-active" data-resize-axis="both"></span></span></span>' +
+ '<span class="protyle-action">Image controls</span></span>' +
'</div></td></tr></tbody></table><div class="protyle-action__table"></div>');
const originalTable = richTable.outerHTML;
const map = document.createElement("div");
@@ -191,6 +194,10 @@ const cases = async (source: string, css: string) => {
check.equal(richPreview.querySelector(".protyle-action--order").textContent, "1.");
check.equal(richPreview.querySelector(".protyle-action--task use").getAttribute("href"), "#iconCheck");
check.equal(map.querySelector(".img .protyle-action, .protyle-attr, .protyle-action__table"), null);
+ check.equal(map.querySelector(".protyle-image-resize, .protyle-block-resize, .img--select, .img--drag, .protyle-image-container--compact"), null,
+ "image previews never retain hidden-source handles or selection states");
+ check.equal(map.querySelector(".img img").getAttribute("src"), "assets/image.png");
+ check.equal((map.querySelector(".img img").parentElement as HTMLElement).style.width, "180px");
check.equal(map.querySelector("[data-node-id], [draggable], [contenteditable]"), null);
check.equal(richTable.outerHTML, originalTable, "preview rendering leaves the source table intact");
for (const size of [16, 32]) {
@@ -283,6 +290,8 @@ app.whenReady().then(async () => {
const result = await promisify(execFile)(executable, [script], {env, timeout: 15000, windowsHide: true});
assert.match(result.stdout, /Special block previews passed/);
} finally {
- rmSync(temporary, {recursive: true, force: true});
+ if (path.dirname(path.resolve(temporary)) === path.resolve(tmpdir()) && path.basename(temporary).startsWith("siyuan-mindmap-preview-")) {
+ rmSync(temporary, {recursive: true, force: true, maxRetries: 20, retryDelay: 100});
+ }
}
});
diff --git a/app/src/protyle/render/listMindmap/view.ts b/app/src/protyle/render/listMindmap/view.ts
index 8f506efdd2..a56ab784dd 100644
--- a/app/src/protyle/render/listMindmap/view.ts
+++ b/app/src/protyle/render/listMindmap/view.ts
@@ -804,9 +804,10 @@ export class ListMindmapView {
});
// 富文本单元格的列表标记保留为只读内容,其他编辑控件不进入预览。
clone.querySelectorAll(".protyle-attr, .protyle-action:not(.li[data-table-cell-node] > .protyle-action), " +
- ".protyle-action__table, .protyle-icons, .mindmap-view")
+ ".protyle-action__table, .protyle-icons, .protyle-image-resize, .mindmap-view")
.forEach(item => item.remove());
[clone, ...Array.from(clone.querySelectorAll<HTMLElement>("*"))].forEach((item) => {
+ item.classList.remove("img--select", "img--drag", "protyle-image-container--compact");
// 页签预览使用独立 DOM 标识切换正文,不携带可提交事务的块身份。
if (["NodeTabs", "NodeTabItem"].includes(item.getAttribute("data-type"))) {
item.id = this.previewID + item.getAttribute("data-node-id");
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts
index 6ad62c036f..9d54e2a879 100644
--- a/app/src/protyle/wysiwyg/index.ts
+++ b/app/src/protyle/wysiwyg/index.ts
@@ -2039,8 +2039,14 @@ export class WYSIWYG {
const imageRect = dragElement.getBoundingClientRect();
const imageScale = imageRect.width / dragWidth || 1;
const imageMultiple = !imgElement.style.minWidth && nodeElement.style.textAlign !== "center" ? 1 : 2;
- const imageMaxWidth = Math.max(17, (mostRight - imageRect.left -
- (imageMultiple === 2 ? imageRect.width / 2 : 0)) * imageMultiple / imageScale);
+ const mindmapNode = dragElement.tagName === "IMG" ?
+ nodeElement.closest<HTMLElement>(".mindmap-view__node--editing") : null;
+ const mindmapStyle = mindmapNode ? getComputedStyle(mindmapNode) : null;
+ // 节点按内容撑开,当前编辑器宽度不能作为放大图片的上限。
+ const imageMaxWidth = Math.max(17, mindmapNode && Number.isFinite(parseFloat(mindmapStyle.maxWidth)) ?
+ parseFloat(mindmapStyle.maxWidth) - parseFloat(mindmapStyle.paddingRight) - parseFloat(mindmapStyle.borderRightWidth) -
+ (imageRect.left - mindmapNode.getBoundingClientRect().left) / imageScale :
+ (mostRight - imageRect.left - (imageMultiple === 2 ? imageRect.width / 2 : 0)) * imageMultiple / imageScale);
const mermaidInsets = mermaidStyle?.boxSizing === "content-box" ?
parseFloat(mermaidStyle.paddingLeft) + parseFloat(mermaidStyle.paddingRight) +
parseFloat(mermaidStyle.borderLeftWidth) + parseFloat(mermaidStyle.borderRightWidth) : 0;
@@ -2053,7 +2059,8 @@ export class WYSIWYG {
imgElement.classList.add("img--drag");
img3115(imgElement);
}
- documentSelf.onmousemove = (moveEvent: MouseEvent) => {
+ const resizeEvents = new AbortController();
+ const resizeMove = (moveEvent: MouseEvent) => {
if (isMermaid) {
nodeElement.style.width = getImageResizeWidth(dragWidth, dragHeight,
(moveEvent.clientX - x) / mermaidScale, 0, "width", isCenter ? 2 : 1, mermaidMaxWidth) + "px";
@@ -2098,7 +2105,15 @@ export class WYSIWYG {
}
};
+ // 脑图编辑器会隔离冒泡事件;捕获阶段继续缩放,保留触摸桥接直接调用的接口。
+ documentSelf.addEventListener("mousemove", resizeMove, {capture: true, signal: resizeEvents.signal});
+ documentSelf.onmousemove = (moveEvent: MouseEvent) => {
+ if (moveEvent.eventPhase === Event.NONE) {
+ resizeMove(moveEvent);
+ }
+ };
documentSelf.onmouseup = () => {
+ resizeEvents.abort();
documentSelf.onmousemove = null;
documentSelf.onmouseup = null;
documentSelf.ondragstart = null;
@@ -2112,6 +2127,7 @@ export class WYSIWYG {
updateTransaction(protyle, nodeElement, html);
}
};
+ documentSelf.addEventListener("mouseup", documentSelf.onmouseup, {capture: true, signal: resizeEvents.signal});
return;
}
// table cell select