Skip to content

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

:bug: Keep fitted mind maps below top toolbar https://github.com/siyuan-note/siyuan/issues/20594

Danielc83ce6087897
diff --git a/app/src/protyle/render/listMindmap/fit.test.ts b/app/src/protyle/render/listMindmap/fit.test.ts
index 2ba39ce920..fc404764fb 100644
--- a/app/src/protyle/render/listMindmap/fit.test.ts
+++ b/app/src/protyle/render/listMindmap/fit.test.ts
@@ -21,7 +21,9 @@ const setup = (right = 200, bottom = 100) => {
const target = new context.View();
Object.assign(target, {
scale: 2.5, offsetX: -200, offsetY: 150,
- viewport: {clientWidth: 800, clientHeight: 420},
+ viewport: {clientWidth: 800, clientHeight: 420, offsetHeight: 420,
+ getBoundingClientRect: () => ({left: 0, top: 0, right: 800, bottom: 420, width: 800, height: 420})},
+ getToolbarBounds: (): unknown[] => [],
positions: new Map([["root", {}]]),
contentBounds: () => ({left: 20, top: 10, right, bottom}),
draw: () => {},
@@ -72,3 +74,48 @@ test("fit keeps the existing minimum zoom and ignores an unavailable canvas", ()
target.fit();
assert.deepEqual(transform(target), fitted);
});
+
+test("fit centers the whole map below visible toolbar controls and is stable across repeated clicks", () => {
+ for (const height of [32, 64]) {
+ const target = setup(2020, 1010);
+ target.getToolbarBounds = () => [{left: 500, top: 0, right: 780, bottom: height, width: 280, height}];
+ target.fit();
+ const availableHeight = 420 - height - 16 - 66;
+ assert.equal(target.scale, availableHeight / 1000);
+ assert.ok(Math.abs(10 * target.scale + target.offsetY - height - 16) < 1e-9);
+ assert.ok(Math.abs(1010 * target.scale + target.offsetY - 420 + 66) < 1e-9);
+ const fitted = transform(target);
+ target.fit();
+ assert.deepEqual(transform(target), fitted);
+ }
+});
+
+test("fit converts scaled toolbar bounds to local canvas coordinates", () => {
+ const target = setup(2020, 1010);
+ target.viewport.getBoundingClientRect = () =>
+ ({left: 10, top: 20, right: 410, bottom: 230, width: 400, height: 210});
+ target.getToolbarBounds = () => [{left: 260, top: 20, right: 400, bottom: 44, width: 140, height: 24}];
+ target.fit();
+ assert.equal(target.scale, .29);
+ assert.ok(Math.abs(10 * target.scale + target.offsetY - 64) < 1e-9);
+ assert.ok(Math.abs(1010 * target.scale + target.offsetY - 354) < 1e-9);
+});
+
+test("toolbar controls outside the viewport do not reduce the fitting area", () => {
+ const target = setup(2020, 1010);
+ target.getToolbarBounds = () => [
+ {left: 810, top: 0, right: 900, bottom: 64, width: 90, height: 64},
+ {left: 500, top: 430, right: 780, bottom: 480, width: 280, height: 50},
+ ];
+ target.fit();
+ assert.equal(target.scale, .338);
+ assert.ok(Math.abs(10 * target.scale + target.offsetY - 16) < 1e-9);
+});
+
+test("fit at minimum zoom keeps the map top below the toolbar when the map is too tall", () => {
+ const target = setup(1020, 5010);
+ target.getToolbarBounds = () => [{left: 500, top: 0, right: 780, bottom: 48, width: 280, height: 48}];
+ target.fit();
+ assert.equal(target.scale, .15);
+ assert.equal(10 * target.scale + target.offsetY, 64);
+});
diff --git a/app/src/protyle/render/listMindmap/model.test.ts b/app/src/protyle/render/listMindmap/model.test.ts
index d8cc922010..b922456e23 100644
--- a/app/src/protyle/render/listMindmap/model.test.ts
+++ b/app/src/protyle/render/listMindmap/model.test.ts
@@ -1209,6 +1209,75 @@ const browserCases = async (sourceCode: string, css: string, taskSource: string,
await settle();
check.deepEqual([navigationView.offsetX, navigationView.offsetY], hiddenToolbarTransform,
"a hidden toolbar does not move a visible node");
+
+ // 高脑图实际点击适应画布后,整图在工具栏下方缩放居中,重复点击保持稳定。
+ const fittingSource = document.createElement("div");
+ fittingSource.innerHTML = lute.Md2BlockDOM("* Root\n" +
+ Array.from({length: 20}, (_value, index) => ` * Branch ${index}\n`).join(""));
+ navigationView.update(api.readListMindmap(fittingSource.firstElementChild as HTMLElement));
+ navigationHost.style.height = "420px";
+ const activateNavigationFit = async (keyboard: boolean) => {
+ navigationHost.scrollIntoView({block: "start"});
+ const bounds = navigationFit().getBoundingClientRect();
+ const point = {x: Math.round(bounds.left + bounds.width / 2), y: Math.round(bounds.top + bounds.height / 2)};
+ if (keyboard) {
+ navigationFit().focus();
+ }
+ await require("electron").ipcRenderer.invoke("mindmap-view-native-input", keyboard ? [
+ {type: "keyDown", keyCode: "Return"}, {type: "char", keyCode: "\r"}, {type: "keyUp", keyCode: "Return"},
+ ] : [
+ {type: "mouseMove", ...point},
+ {type: "mouseDown", ...point, button: "left", clickCount: 1},
+ {type: "mouseUp", ...point, button: "left", clickCount: 1},
+ ]);
+ await settle();
+ };
+ for (const mode of ["hover", "focus", "fullscreen"]) {
+ for (const width of [760, 400, 300]) {
+ const outerScale = width === 300 ? .5 : 1;
+ navigationView.setReadOnly(width !== 400);
+ navigationHost.classList.toggle("fullscreen", mode === "fullscreen");
+ navigationHost.style.width = `${width}px`;
+ navigationHost.style.transform = `scale(${outerScale})`;
+ navigationToolbar.style.paddingTop = width === 400 ? "18px" : "";
+ navigationView.refreshLayout();
+ navigationHost.focus();
+ await moveNavigationPointer(mode === "hover");
+ navigationView.scale = 1;
+ navigationView.offsetX = 100;
+ navigationView.offsetY = -100;
+ navigationView.draw();
+ await activateNavigationFit(mode === "focus");
+ check.equal(getComputedStyle(navigationToolbar).opacity, "1");
+ check.ok(navigationView.scale < 1 && navigationView.scale > .15,
+ `the tall map needs an unconstrained fit, mode=${mode}, width=${width}, scale=${navigationView.scale}`);
+ const viewportBounds = navigationViewport.getBoundingClientRect();
+ const toolbarBottom = Math.max(...navigationView.getToolbarBounds().filter((bounds: DOMRect) =>
+ bounds.right > viewportBounds.left && bounds.left < viewportBounds.right).map((bounds: DOMRect) => bounds.bottom));
+ const fittedContent = navigationView.contentBounds();
+ const contentTop = viewportBounds.top + (fittedContent.top * navigationView.scale + navigationView.offsetY) * outerScale;
+ const contentBottom = viewportBounds.top + (fittedContent.bottom * navigationView.scale + navigationView.offsetY) * outerScale;
+ check.ok(contentTop >= toolbarBottom + 15 * outerScale,
+ `fit leaves the whole map below the toolbar, mode=${mode}, width=${width}`);
+ check.ok(contentBottom <= viewportBounds.bottom - 65 * outerScale, "fit preserves the bottom panel clearance");
+ const fittedTransform = [navigationView.scale, navigationView.offsetX, navigationView.offsetY];
+ await activateNavigationFit(mode === "focus");
+ check.deepEqual([navigationView.scale, navigationView.offsetX, navigationView.offsetY], fittedTransform,
+ "repeated fit activation preserves scale and centering");
+ }
+ }
+ navigationHost.classList.remove("fullscreen");
+ navigationHost.style.width = "760px";
+ navigationHost.style.transform = "";
+ navigationToolbar.style.paddingTop = "";
+ navigationHost.focus();
+ navigationView.refreshLayout();
+ await moveNavigationPointer(false);
+ check.equal(getComputedStyle(navigationToolbar).opacity, "0");
+ navigationView.fit();
+ const hiddenFitBounds = navigationView.contentBounds();
+ check.ok(Math.abs(hiddenFitBounds.top * navigationView.scale + navigationView.offsetY - 16) < 1,
+ "fitting with a hidden toolbar keeps the normal top margin");
navigationView.destroy();
navigationHost.remove();
diff --git a/app/src/protyle/render/listMindmap/view.ts b/app/src/protyle/render/listMindmap/view.ts
index ba97cd6a88..044531c3b4 100644
--- a/app/src/protyle/render/listMindmap/view.ts
+++ b/app/src/protyle/render/listMindmap/view.ts
@@ -856,6 +856,26 @@ export class ListMindmapView {
this.options.host.querySelector<HTMLElement>(`[data-mindmap-source-id="${id}"]`);
}
+ private getToolbarBounds() {
+ const bounds: DOMRect[] = [];
+ const toolbarStyle = getComputedStyle(this.toolbar);
+ if (this.toolbar.hidden || toolbarStyle.display === "none" || toolbarStyle.visibility === "hidden" ||
+ Number(toolbarStyle.opacity) === 0) {
+ return bounds;
+ }
+ // 顶部遮挡范围只包含可见控件及展开的缩放面板,不包含左侧空白区域。
+ this.toolbar.querySelectorAll<HTMLElement>(".block__icon, .mindmap-view__zoom, .mindmap-view__zoom-actions")
+ .forEach(control => {
+ const rect = control.getBoundingClientRect();
+ const style = getComputedStyle(control);
+ if (rect.width && rect.height && style.display !== "none" && style.visibility !== "hidden" &&
+ Number(style.opacity) !== 0) {
+ bounds.push(rect);
+ }
+ });
+ return bounds;
+ }
+
public revealNode(id: string) {
const node = this.nodeElements.get(id);
if (!node || node.hidden) {
@@ -877,21 +897,12 @@ export class ListMindmapView {
const x = rect.left < viewport.left + marginX ? viewport.left + marginX - rect.left :
rect.right > viewport.right - marginX ? viewport.right - marginX - rect.right : 0;
let top = viewport.top + marginY;
- const toolbarStyle = getComputedStyle(this.toolbar);
- // 顶部仅避让可见控件及展开的缩放面板,保留工具栏左侧空白区域。
- if (!this.toolbar.hidden && toolbarStyle.display !== "none" && toolbarStyle.visibility !== "hidden" &&
- Number(toolbarStyle.opacity) !== 0) {
- this.toolbar.querySelectorAll<HTMLElement>(".block__icon, .mindmap-view__zoom, .mindmap-view__zoom-actions")
- .forEach(control => {
- const bounds = control.getBoundingClientRect();
- const style = getComputedStyle(control);
- if (bounds.width && bounds.height && bounds.bottom > viewport.top && bounds.top < viewport.bottom &&
- rect.left + x < bounds.right && rect.right + x > bounds.left &&
- style.display !== "none" && style.visibility !== "hidden" && Number(style.opacity) !== 0) {
- top = Math.max(top, bounds.bottom + marginY);
- }
- });
- }
+ this.getToolbarBounds().forEach(bounds => {
+ if (bounds.bottom > viewport.top && bounds.top < viewport.bottom &&
+ rect.left + x < bounds.right && rect.right + x > bounds.left) {
+ top = Math.max(top, bounds.bottom + marginY);
+ }
+ });
let bottom = viewport.bottom - marginY;
// 底部浮层按实际尺寸避让,横向定位后仍不相交的节点无需额外上移。
[this.inspector, this.summaryControls].forEach(panel => {
@@ -2907,7 +2918,16 @@ export class ListMindmapView {
// 先更新关系文字的位置,再按画布上下留白缩放并居中。
this.draw();
const {left, top, right, bottom} = this.contentBounds();
- const insetTop = 16;
+ const viewport = this.viewport.getBoundingClientRect();
+ const scaleY = viewport.height / (this.viewport.offsetHeight || viewport.height) || 1;
+ let insetTop = 16;
+ // 整图适配在顶部控件下方缩放居中,外层缩放的屏幕尺寸换算为画布局部尺寸。
+ this.getToolbarBounds().forEach(bounds => {
+ if (bounds.right > viewport.left && bounds.left < viewport.right &&
+ bounds.bottom > viewport.top && bounds.top < viewport.bottom) {
+ insetTop = Math.max(insetTop, (bounds.bottom - viewport.top) / scaleY + 16);
+ }
+ });
// 底部为菜单、外侧间距和横向滚动条留出空间,避免选中节点时画布跳动。
const insetBottom = 66;
const availableWidth = Math.max(1, width - 48);
@@ -2915,7 +2935,9 @@ export class ListMindmapView {
this.scale = Math.min(maxScale, Math.max(.15, Math.min(availableWidth / Math.max(1, right - left),
availableHeight / Math.max(1, bottom - top))));
this.offsetX = width / 2 - (left + right) * this.scale / 2;
- this.offsetY = insetTop + availableHeight / 2 - (top + bottom) * this.scale / 2;
+ // 达到最小缩放仍无法容纳整图时,优先保证顶部正文避开工具栏。
+ this.offsetY = (bottom - top) * this.scale > availableHeight ? insetTop - top * this.scale :
+ insetTop + availableHeight / 2 - (top + bottom) * this.scale / 2;
this.draw();
}
diff --git a/app/src/protyle/render/listMindmap/viewportFocus.test.ts b/app/src/protyle/render/listMindmap/viewportFocus.test.ts
index f7eebc01d3..199a6d354f 100644
--- a/app/src/protyle/render/listMindmap/viewportFocus.test.ts
+++ b/app/src/protyle/render/listMindmap/viewportFocus.test.ts
@@ -8,7 +8,7 @@ const source = createSourceFile("view.ts", readFileSync("src/protyle/render/list
ScriptTarget.ES2021, true);
const view = source.statements.find(item => isClassDeclaration(item) && item.name?.text === "ListMindmapView");
assert.ok(view && isClassDeclaration(view));
-const names = ["focusNode", "revealNode", "isPanTarget", "dismissControls", "pointerDown", "preparePointer", "beginPointer", "pointerUp"];
+const names = ["focusNode", "getToolbarBounds", "revealNode", "isPanTarget", "dismissControls", "pointerDown", "preparePointer", "beginPointer", "pointerUp"];
const methods = view.members.filter(item => item.name && names.includes(item.name.getText(source)));
assert.equal(methods.length, names.length);
const compiled = transpileModule(`class View { ${methods.map(item => item.getText(source)).join("\n")} }