Skip to content

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

:bug: Keep focused mind map nodes clear of bottom panels https://github.com/siyuan-note/siyuan/issues/20594

Daniel9d8cbf840800
diff --git a/app/src/protyle/render/listMindmap/model.test.ts b/app/src/protyle/render/listMindmap/model.test.ts
index 49f4ee7643..1a529041d5 100644
--- a/app/src/protyle/render/listMindmap/model.test.ts
+++ b/app/src/protyle/render/listMindmap/model.test.ts
@@ -1056,6 +1056,67 @@ const browserCases = async (sourceCode: string, css: string, taskSource: string,
check.deepEqual([navigationView.scale, navigationView.offsetX, navigationView.offsetY], transform,
"navigating to a visible node preserves the viewport");
}
+ // 新节点和方向键导航均避开底部菜单,窄画布、缩放及菜单尺寸变化后仍按实际遮挡范围定位。
+ const navigationSource = list.cloneNode(true) as HTMLElement;
+ const addedSource = document.createElement("div");
+ addedSource.innerHTML = lute.Md2BlockDOM("- New node\n");
+ const addedItem = addedSource.querySelector<HTMLElement>('[data-type="NodeListItem"]');
+ check.ok(api.addListMindmapNode(navigationSource, beta, "after", addedItem));
+ navigationView.setReadOnly(false);
+ navigationView.update(api.readListMindmap(navigationSource));
+ await settle();
+ const addedNode = navigationHost.querySelector<HTMLElement>(`[data-mindmap-id="${addedItem.dataset.nodeId}"]`);
+ const navigationInspector = navigationHost.querySelector<HTMLElement>(".mindmap-view__inspector");
+ const placeUnderInspector = (bounds: DOMRect) => {
+ const rect = addedNode.getBoundingClientRect();
+ const viewportBounds = navigationViewport.getBoundingClientRect();
+ const scaleX = viewportBounds.width / navigationViewport.offsetWidth;
+ const scaleY = viewportBounds.height / navigationViewport.offsetHeight;
+ navigationView.offsetX += (bounds.left + bounds.width / 2 - rect.left - rect.width / 2) / scaleX;
+ navigationView.offsetY += (bounds.top + 8 * scaleY - rect.bottom) / scaleY;
+ navigationView.draw();
+ };
+ for (const width of [400, 240, 300]) {
+ const scale = width === 300 ? .5 : 1;
+ navigationHost.style.width = `${width}px`;
+ navigationHost.style.transform = `scale(${scale})`;
+ navigationHost.style.transformOrigin = "top left";
+ navigationInspector.style.paddingTop = width === 240 ? "18px" : "4px";
+ navigationInspector.style.paddingBottom = navigationInspector.style.paddingTop;
+ navigationView.refreshLayout();
+ await settle();
+ for (const keyboard of [false, true]) {
+ navigationView.selectNode(beta);
+ const menuBounds = navigationInspector.getBoundingClientRect();
+ check.ok(menuBounds.width > 0 && menuBounds.height > 0);
+ placeUnderInspector(menuBounds);
+ const before = addedNode.getBoundingClientRect();
+ check.ok(before.bottom > menuBounds.top && before.top < menuBounds.bottom);
+ navigationView.refreshLayout();
+ if (keyboard) {
+ navigationHost.dispatchEvent(new KeyboardEvent("keydown", {
+ key: "ArrowDown", bubbles: true, cancelable: true,
+ }));
+ } else {
+ navigationView.focusNode(addedItem.dataset.nodeId);
+ }
+ await settle();
+ const after = addedNode.getBoundingClientRect();
+ check.equal(navigationView.getSelectedId(), addedItem.dataset.nodeId);
+ check.ok(after.bottom <= navigationInspector.getBoundingClientRect().top - 15 * scale,
+ `node stays above the menu, width=${width}, keyboard=${keyboard}`);
+ check.ok(after.top >= navigationViewport.getBoundingClientRect().top + 15 * scale);
+ }
+ }
+ const visibleMenuBounds = navigationInspector.getBoundingClientRect();
+ navigationView.setReadOnly(true);
+ await settle();
+ placeUnderInspector(visibleMenuBounds);
+ const hiddenMenuTransform = [navigationView.offsetX, navigationView.offsetY];
+ navigationView.focusNode(addedItem.dataset.nodeId);
+ await settle();
+ check.deepEqual([navigationView.offsetX, navigationView.offsetY], hiddenMenuTransform,
+ "a hidden menu does not reduce the node's visible area");
navigationView.destroy();
navigationHost.remove();
diff --git a/app/src/protyle/render/listMindmap/view.ts b/app/src/protyle/render/listMindmap/view.ts
index 201ee6d58b..a5ad5dced2 100644
--- a/app/src/protyle/render/listMindmap/view.ts
+++ b/app/src/protyle/render/listMindmap/view.ts
@@ -870,13 +870,29 @@ export class ListMindmapView {
if (!viewport.width || !viewport.height || !rect.width || !rect.height) {
return;
}
- const margin = 16;
- const x = rect.left < viewport.left + margin ? viewport.left + margin - rect.left :
- rect.right > viewport.right - margin ? viewport.right - margin - rect.right : 0;
- const y = rect.top < viewport.top + margin ? viewport.top + margin - rect.top :
- rect.bottom > viewport.bottom - margin ? viewport.bottom - margin - rect.bottom : 0;
+ const scaleX = viewport.width / (this.viewport.offsetWidth || viewport.width);
+ const scaleY = viewport.height / (this.viewport.offsetHeight || viewport.height);
+ const marginX = 16 * scaleX;
+ const marginY = 16 * scaleY;
+ const x = rect.left < viewport.left + marginX ? viewport.left + marginX - rect.left :
+ rect.right > viewport.right - marginX ? viewport.right - marginX - rect.right : 0;
+ let bottom = viewport.bottom - marginY;
+ // 底部浮层按实际尺寸避让,横向定位后仍不相交的节点无需额外上移。
+ [this.inspector, this.summaryControls].forEach(panel => {
+ if (panel.hidden) {
+ return;
+ }
+ const bounds = panel.getBoundingClientRect();
+ if (bounds.width && bounds.height && bounds.bottom > viewport.top && bounds.top < viewport.bottom &&
+ rect.left + x < bounds.right && rect.right + x > bounds.left) {
+ bottom = Math.min(bottom, bounds.top - marginY);
+ }
+ });
+ // 可用高度不足以完整显示节点时,优先保留节点顶部的正文。
+ const y = rect.top < viewport.top + marginY ? viewport.top + marginY - rect.top :
+ rect.bottom > bottom ? Math.max(viewport.top + marginY - rect.top, bottom - rect.bottom) : 0;
if (x || y) {
- const bounded = this.boundedPan(this.offsetX + x, this.offsetY + y);
+ const bounded = this.boundedPan(this.offsetX + x / scaleX, this.offsetY + y / scaleY);
this.offsetX = bounded.x;
this.offsetY = bounded.y;
this.draw();
diff --git a/app/src/protyle/render/listMindmap/viewportFocus.test.ts b/app/src/protyle/render/listMindmap/viewportFocus.test.ts
index a2cc905a6c..ef7fa4007f 100644
--- a/app/src/protyle/render/listMindmap/viewportFocus.test.ts
+++ b/app/src/protyle/render/listMindmap/viewportFocus.test.ts
@@ -24,9 +24,10 @@ const setup = () => {
const host = {focus: () => focus = "canvas"};
const target = Object.assign(new context.View(), {
options: {host}, editingId: "new", model: {nodes: new Map([["new", {}]])},
- selectedId: "new", tooltip: {},
+ selectedId: "new", tooltip: {}, inspector: {hidden: true}, summaryControls: {hidden: true},
nodeElements: new Map(), offsetX: 0, offsetY: 0,
viewport: {contains: (element: unknown) => !!element, setPointerCapture: () => {},
+ offsetWidth: 400, offsetHeight: 300,
getBoundingClientRect: () => ({left: 0, top: 0, right: 400, bottom: 300, width: 400, height: 300})},
selectNode: () => {}, findLine: (): unknown => undefined, selectLine: () => {},
clearSelection: () => { target.selectedId = undefined; },
@@ -76,6 +77,82 @@ test("focusing visible, missing or removed nodes does not pan", () => {
assert.equal(drawn(), 0);
});
+test("node focus avoids the actual bottom panel bounds after the pending layout", () => {
+ for (const panel of ["inspector", "summaryControls"]) {
+ for (const top of [250, 210]) {
+ const {target, frames, drawn} = setup();
+ target.frame = 1;
+ target[panel] = {hidden: false, getBoundingClientRect: () =>
+ ({left: 80, right: 320, top, bottom: 292, width: 240, height: 292 - top})};
+ target.nodeElements.set("new", {getBoundingClientRect: () =>
+ ({left: 100, top: 240, right: 180, bottom: 280, width: 80, height: 40})});
+ target.focusNode("new");
+ assert.equal(drawn(), 0);
+ frames.forEach(action => action());
+ assert.equal(280 + target.offsetY, top - 16);
+ assert.equal(target.offsetX, 0);
+ assert.equal(drawn(), 1);
+ }
+ }
+});
+
+test("hidden, unmeasured and non-overlapping panels do not move an unobstructed node", () => {
+ for (const bounds of [
+ {left: 200, right: 320, top: 250, bottom: 292, width: 120, height: 42},
+ {left: 80, right: 320, top: 310, bottom: 352, width: 240, height: 42},
+ {left: 0, right: 0, top: 0, bottom: 0, width: 0, height: 0},
+ ]) {
+ const {target, drawn} = setup();
+ target.inspector = {hidden: false, getBoundingClientRect: () => bounds};
+ target.nodeElements.set("new", {getBoundingClientRect: () =>
+ ({left: 100, top: 240, right: 180, bottom: 280, width: 80, height: 40})});
+ target.focusNode("new");
+ assert.equal(drawn(), 0);
+ }
+ const {target, drawn} = setup();
+ target.inspector = {hidden: true, getBoundingClientRect: () => {
+ throw new Error("hidden panels do not need measuring");
+ }};
+ target.nodeElements.set("new", {getBoundingClientRect: () =>
+ ({left: 100, top: 240, right: 180, bottom: 280, width: 80, height: 40})});
+ target.focusNode("new");
+ assert.equal(drawn(), 0);
+});
+
+test("horizontal reveal also avoids a panel at the destination", () => {
+ const {target} = setup();
+ target.inspector = {hidden: false, getBoundingClientRect: () =>
+ ({left: 80, right: 390, top: 250, bottom: 292, width: 310, height: 42})};
+ target.nodeElements.set("new", {getBoundingClientRect: () =>
+ ({left: 420, top: 240, right: 500, bottom: 280, width: 80, height: 40})});
+ target.focusNode("new");
+ assert.equal(target.offsetX, -116);
+ assert.equal(target.offsetY, -46);
+});
+
+test("scaled canvases convert menu clearance into canvas pan coordinates", () => {
+ const {target} = setup();
+ target.viewport.getBoundingClientRect = () =>
+ ({left: 0, top: 0, right: 200, bottom: 150, width: 200, height: 150});
+ target.inspector = {hidden: false, getBoundingClientRect: () =>
+ ({left: 40, right: 160, top: 125, bottom: 146, width: 120, height: 21})};
+ target.nodeElements.set("new", {getBoundingClientRect: () =>
+ ({left: 50, top: 120, right: 90, bottom: 140, width: 40, height: 20})});
+ target.focusNode("new");
+ assert.equal(target.offsetX, 0);
+ assert.equal(target.offsetY, -46);
+});
+
+test("a node taller than the unobstructed area keeps its top visible", () => {
+ const {target} = setup();
+ target.inspector = {hidden: false, getBoundingClientRect: () =>
+ ({left: 80, right: 320, top: 210, bottom: 292, width: 240, height: 82})};
+ target.nodeElements.set("new", {getBoundingClientRect: () =>
+ ({left: 100, top: 100, right: 180, bottom: 320, width: 80, height: 220})});
+ target.focusNode("new");
+ assert.equal(100 + target.offsetY, 16);
+});
+
test("mouse, right-button and primary touch panning preserve the editing focus", () => {
for (const overrides of [{}, {button: 2}, {pointerType: "touch"}]) {
const {target, event, focus, finished} = setup();