Skip to content

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

:bug: Keep mind map content visible while panning https://github.com/siyuan-note/siyuan/issues/19833

Daniel20679e9b842c
diff --git a/app/src/protyle/render/listMindmap/model.test.ts b/app/src/protyle/render/listMindmap/model.test.ts
index aef7f4e811..49f4ee7643 100644
--- a/app/src/protyle/render/listMindmap/model.test.ts
+++ b/app/src/protyle/render/listMindmap/model.test.ts
@@ -2651,6 +2651,67 @@ const browserCases = async (sourceCode: string, css: string, taskSource: string,
check.ok(host.querySelector<HTMLElement>(".mindmap-view__node").getBoundingClientRect().bottom <=
fitInspector.getBoundingClientRect().top, "fit keeps content above the visible inspector");
fitInspector.hidden = true;
+
+ // 横向裁切不规则脑图后,滚轮和触摸平移仍保留实际节点,并在边界继续滚动文档。
+ {
+ let markdown = "* Root\n";
+ for (let depth = 1; depth <= 7; depth++) {
+ markdown += `${" ".repeat(depth)}* Branch ${depth}\n`;
+ }
+ for (let index = 0; index < 12; index++) {
+ markdown += `${" ".repeat(8)}* Leaf ${index}\n`;
+ }
+ const panModel = api.readListMindmap(reset(markdown));
+ const scrollHost = document.createElement("div");
+ scrollHost.className = "protyle-content";
+ scrollHost.style.cssText = "height: 340px; overflow-y: auto;";
+ const panHost = document.createElement("div");
+ panHost.style.cssText = "width: 400px; height: 300px;";
+ const followingContent = document.createElement("div");
+ followingContent.style.height = "3000px";
+ scrollHost.append(panHost, followingContent);
+ document.body.append(scrollHost);
+ const panView = new api.ListMindmapView({...options, host: panHost, model: panModel});
+ await settle();
+ panView.zoomAt(1);
+ const rootPosition = panView.positions.get(panModel.root.id);
+ panView.offsetX = 16 - rootPosition.x;
+ panView.offsetY = 120 - rootPosition.y;
+ panView.draw();
+ const viewport = panHost.querySelector<HTMLElement>(".mindmap-view__viewport");
+ const fixedX = panView.offsetX;
+ const checkVisibleNode = () => {
+ const bounds = viewport.getBoundingClientRect();
+ check.ok(Array.from(panHost.querySelectorAll<HTMLElement>(".mindmap-view__node")).some(element => {
+ const rect = element.getBoundingClientRect();
+ return !element.hidden && rect.left < bounds.right && rect.right > bounds.left &&
+ rect.top < bounds.bottom && rect.bottom > bounds.top;
+ }), "at least one actual node remains visible after panning the cropped map");
+ check.equal(panView.offsetX, fixedX, "vertical movement does not shift the horizontal position");
+ };
+ for (const deltaY of [100000, -100000]) {
+ const event = new WheelEvent("wheel", {deltaY, bubbles: true, cancelable: true});
+ viewport.dispatchEvent(event);
+ check.equal(event.defaultPrevented, true);
+ checkVisibleNode();
+ const next = new WheelEvent("wheel", {deltaY, bubbles: true, cancelable: true});
+ viewport.dispatchEvent(next);
+ check.equal(next.defaultPrevented, false, "the next wheel event is handed back to document scrolling");
+ }
+ panView.offsetY = 120 - rootPosition.y;
+ panView.draw();
+ panHost.querySelector<HTMLButtonElement>('[aria-label="cursorHand"]').click();
+ const touch = {pointerId: 601, pointerType: "touch", isPrimary: true, button: 0,
+ clientX: 100, clientY: 100, bubbles: true, cancelable: true};
+ viewport.dispatchEvent(new PointerEvent("pointerdown", touch));
+ viewport.dispatchEvent(new PointerEvent("pointermove", {...touch, clientY: -1900}));
+ checkVisibleNode();
+ check.ok(scrollHost.scrollTop > 0, "touch overflow scrolls the real document container");
+ viewport.dispatchEvent(new PointerEvent("pointerup", {...touch, clientY: -1900}));
+ panView.destroy();
+ scrollHost.remove();
+ }
+
lute.SetTabs(true);
const tabsContainer = document.createElement("div");
tabsContainer.innerHTML = lute.Md2BlockDOM("::: tabs\n@tab First\n\nOne\n@tab Second\n\nTwo\n:::\n");
diff --git a/app/src/protyle/render/listMindmap/pan.test.ts b/app/src/protyle/render/listMindmap/pan.test.ts
index 22fc3896df..f48bc40065 100644
--- a/app/src/protyle/render/listMindmap/pan.test.ts
+++ b/app/src/protyle/render/listMindmap/pan.test.ts
@@ -1,6 +1,6 @@
import {test} from "node:test";
import * as assert from "node:assert/strict";
-import {clampMindmapPanOffset} from "./pan";
+import {clampMindmapPanOffset, clampMindmapPanPosition} from "./pan";
test("mind map pan keeps content visible at both boundaries", () => {
assert.equal(clampMindmapPanOffset(-1000, 100, 500, 1, 300), -436);
@@ -17,3 +17,43 @@ test("small or scaled content remains reachable", () => {
test("unmeasured bounds do not move the view", () => {
assert.equal(clampMindmapPanOffset(12, Infinity, -Infinity, 1, 300), 12);
});
+
+test("sparse maps cannot be panned into empty corners of their overall bounding box", () => {
+ const rectangles = [
+ {x: 100, y: 100, width: 100, height: 100},
+ {x: 800, y: 800, width: 100, height: 100},
+ ];
+ for (const scale of [.25, 1, 2.5]) {
+ for (const x of [-5000, -500, 0, 200, 5000]) {
+ for (const y of [-5000, -500, 0, 200, 5000]) {
+ const next = clampMindmapPanPosition(x, y, rectangles, scale, 400, 300);
+ assert.ok(rectangles.some(rectangle => rectangle.x * scale + next.x < 400 &&
+ (rectangle.x + rectangle.width) * scale + next.x > 0 &&
+ rectangle.y * scale + next.y < 300 && (rectangle.y + rectangle.height) * scale + next.y > 0),
+ `at least one actual rectangle remains visible at scale ${scale}, offset ${x}, ${y}`);
+ }
+ }
+ }
+});
+
+test("single-axis panning keeps the other axis and uses content visible on that axis", () => {
+ const rectangles = [
+ {x: 100, y: 100, width: 100, height: 100},
+ {x: 800, y: 800, width: 100, height: 100},
+ ];
+ assert.deepEqual(clampMindmapPanPosition(0, -1000, rectangles, 1, 400, 300, "y"), {x: 0, y: -136});
+ assert.deepEqual(clampMindmapPanPosition(-1000, 0, rectangles, 1, 400, 300, "x"), {x: -136, y: 0});
+ assert.deepEqual(clampMindmapPanPosition(-499, -1000, rectangles, 1, 400, 300, "y"), {x: -499, y: -836},
+ "even a narrow horizontal slice of content is retained without horizontal drift");
+ assert.deepEqual(clampMindmapPanPosition(0, 0, rectangles, 1, 400, 300), {x: 0, y: 0});
+});
+
+test("tiny content, missing measurements and empty maps keep valid pan positions", () => {
+ assert.deepEqual(clampMindmapPanPosition(-1000, -1000, [{x: 10, y: 20, width: 10, height: 10}],
+ 1, 400, 300), {x: -10, y: -20});
+ assert.deepEqual(clampMindmapPanPosition(10, 20, [], 1, 400, 300), {x: 10, y: 20});
+ assert.deepEqual(clampMindmapPanPosition(10, 20, [{x: 0, y: 0, width: Infinity, height: 10}],
+ 1, 400, 300), {x: 10, y: 20});
+ assert.deepEqual(clampMindmapPanPosition(10, 20, [{x: 0, y: 0, width: 100, height: 100}],
+ 1, 0, 300), {x: 10, y: 20});
+});
diff --git a/app/src/protyle/render/listMindmap/pan.ts b/app/src/protyle/render/listMindmap/pan.ts
index f655d5ab56..af06d6cd56 100644
--- a/app/src/protyle/render/listMindmap/pan.ts
+++ b/app/src/protyle/render/listMindmap/pan.ts
@@ -8,3 +8,50 @@ export const clampMindmapPanOffset = (offset: number, start: number, end: number
const maximum = viewport - visible - start * scale;
return Math.min(maximum, Math.max(minimum, offset));
};
+
+export interface MindmapPanRect {
+ x: number;
+ y: number;
+ width: number;
+ height: number;
+}
+
+// 平移位置必须保留实际节点或文字,避免停在整体包围盒内的空白区域。
+export const clampMindmapPanPosition = (x: number, y: number, rectangles: MindmapPanRect[],
+ scale: number, width: number, height: number, axis?: "x" | "y") => {
+ if (![x, y, scale, width, height].every(Number.isFinite) || scale <= 0 || width <= 0 || height <= 0) {
+ return {x, y};
+ }
+ let closest: {x: number, y: number};
+ let distance = Infinity;
+ let fallback: {x: number, y: number};
+ let fallbackDistance = Infinity;
+ for (const rectangle of rectangles) {
+ if (rectangle.width <= 0 || rectangle.height <= 0 ||
+ ![rectangle.x, rectangle.y, rectangle.width, rectangle.height].every(Number.isFinite)) {
+ continue;
+ }
+ const nextX = clampMindmapPanOffset(x, rectangle.x, rectangle.x + rectangle.width, scale, width);
+ const nextY = clampMindmapPanOffset(y, rectangle.y, rectangle.y + rectangle.height, scale, height);
+ const nextDistance = (nextX - x) ** 2 + (nextY - y) ** 2;
+ if (nextDistance < fallbackDistance) {
+ fallback = {x: nextX, y: nextY};
+ fallbackDistance = nextDistance;
+ }
+ // 单轴平移沿用另一轴的位置,只在该轴仍与可视区域相交的内容中寻找边界。
+ if (axis === "y" && (rectangle.x * scale + x >= width || (rectangle.x + rectangle.width) * scale + x <= 0) ||
+ axis === "x" && (rectangle.y * scale + y >= height || (rectangle.y + rectangle.height) * scale + y <= 0)) {
+ continue;
+ }
+ const candidate = {x: axis === "y" ? x : nextX, y: axis === "x" ? y : nextY};
+ const candidateDistance = (candidate.x - x) ** 2 + (candidate.y - y) ** 2;
+ if (candidateDistance === 0) {
+ return candidate;
+ }
+ if (candidateDistance < distance) {
+ closest = candidate;
+ distance = candidateDistance;
+ }
+ }
+ return closest || fallback || {x, y};
+};
diff --git a/app/src/protyle/render/listMindmap/view.ts b/app/src/protyle/render/listMindmap/view.ts
index 498423bc92..201ee6d58b 100644
--- a/app/src/protyle/render/listMindmap/view.ts
+++ b/app/src/protyle/render/listMindmap/view.ts
@@ -17,7 +17,7 @@ import {findMindmapDrop} from "./drop";
import {destroyTabsRender, tabsRender} from "../tabsRender";
import {getListMindmapFoldStates, getListMindmapSiblingFoldStates} from "./fold";
import type {ListMindmapFoldTarget} from "./fold";
-import {clampMindmapPanOffset} from "./pan";
+import {clampMindmapPanOffset, clampMindmapPanPosition, MindmapPanRect} from "./pan";
import {getListMindmapSummaryParentID, getListMindmapSummaryRange, getListMindmapSummarySelection, getListMindmapSummarySnapshot,
layoutListMindmapSummaries} from "./summary";
import type {ListMindmapSummary, ListMindmapSummaryPosition, ListMindmapSummaryRange} from "./summary";
@@ -2771,9 +2771,8 @@ export class ListMindmapView {
return;
}
if (!this.fullscreenMarker && !event.shiftKey && !event.deltaX && event.deltaY) {
- const {left, top, right, bottom} = this.contentBounds();
- if (left * this.scale + this.offsetX >= 0 && right * this.scale + this.offsetX <= this.viewport.clientWidth &&
- top * this.scale + this.offsetY >= 0 && bottom * this.scale + this.offsetY <= this.viewport.clientHeight) {
+ const {top, bottom} = this.contentBounds();
+ if (top * this.scale + this.offsetY >= 0 && bottom * this.scale + this.offsetY <= this.viewport.clientHeight) {
return;
}
}
@@ -2791,11 +2790,14 @@ export class ListMindmapView {
};
private boundedPan(x: number, y: number) {
- const {left, top, right, bottom} = this.contentBounds();
- return {
- x: clampMindmapPanOffset(x, left, right, this.scale, this.viewport.clientWidth),
- y: clampMindmapPanOffset(y, top, bottom, this.scale, this.viewport.clientHeight),
- };
+ const rectangles = this.contentRectangles();
+ const {left, top, right, bottom} = this.contentBounds(rectangles);
+ const axis = x === this.offsetX && y !== this.offsetY ? "y" :
+ y === this.offsetY && x !== this.offsetX ? "x" : undefined;
+ return clampMindmapPanPosition(
+ clampMindmapPanOffset(x, left, right, this.scale, this.viewport.clientWidth),
+ clampMindmapPanOffset(y, top, bottom, this.scale, this.viewport.clientHeight),
+ rectangles, this.scale, this.viewport.clientWidth, this.viewport.clientHeight, axis);
}
private zoomAt(scale: number, x = this.viewport.clientWidth / 2, y = this.viewport.clientHeight / 2) {
@@ -2812,7 +2814,26 @@ export class ListMindmapView {
this.draw();
}
- private contentBounds() {
+ private contentRectangles(): MindmapPanRect[] {
+ const rectangles: MindmapPanRect[] = [];
+ this.positions.forEach(node => rectangles.push({x: node.x, y: node.y - 1, width: node.width, height: node.height + 1}));
+ this.summaryPositions.forEach(summary => rectangles.push({
+ x: summary.labelX, y: summary.labelY, width: summary.width, height: summary.height,
+ }));
+ this.relationElements.forEach(element => {
+ if (!element.hidden && element.style.visibility !== "hidden") {
+ const x = parseFloat(element.style.left);
+ const y = parseFloat(element.style.top);
+ if (Number.isFinite(x) && Number.isFinite(y)) {
+ rectangles.push({x: x - element.offsetWidth / 2, y: y - element.offsetHeight / 2,
+ width: element.offsetWidth, height: element.offsetHeight});
+ }
+ }
+ });
+ return rectangles;
+ }
+
+ private contentBounds(rectangles = this.contentRectangles()) {
// 按实际可见节点、关系线和文字计算边界,不包含布局预留的空白。
let left = Infinity;
let top = Infinity;
@@ -2824,21 +2845,10 @@ export class ListMindmapView {
right = Math.max(right, x + width);
bottom = Math.max(bottom, y + height);
};
- this.positions.forEach(node => include(node.x, node.y - 1, node.width, node.height + 1));
+ rectangles.forEach(rectangle => include(rectangle.x, rectangle.y, rectangle.width, rectangle.height));
this.relationRoutes.forEach(points => points.forEach(point => include(point.x, point.y)));
this.summaryPositions.forEach(summary => {
include(summary.x - 10, summary.top, summary.labelX + summary.width - summary.x + 10, summary.bottom - summary.top);
- include(summary.labelX, summary.labelY, summary.width, summary.height);
- });
- this.relationElements.forEach(element => {
- if (!element.hidden && element.style.visibility !== "hidden") {
- const x = parseFloat(element.style.left);
- const y = parseFloat(element.style.top);
- if (Number.isFinite(x) && Number.isFinite(y)) {
- include(x - element.offsetWidth / 2, y - element.offsetHeight / 2,
- element.offsetWidth, element.offsetHeight);
- }
- }
});
return {left, top, right, bottom};
}
diff --git a/app/src/protyle/render/listMindmap/wheel.test.ts b/app/src/protyle/render/listMindmap/wheel.test.ts
index cc4ecfe582..b5c07f123a 100644
--- a/app/src/protyle/render/listMindmap/wheel.test.ts
+++ b/app/src/protyle/render/listMindmap/wheel.test.ts
@@ -3,13 +3,13 @@ import {readFileSync} from "node:fs";
import {test} from "node:test";
import {runInNewContext} from "node:vm";
import {createSourceFile, isClassDeclaration, ScriptTarget, transpileModule} from "typescript";
-import {clampMindmapPanOffset} from "./pan";
+import {clampMindmapPanOffset, clampMindmapPanPosition} from "./pan";
const source = createSourceFile("view.ts", readFileSync("src/protyle/render/listMindmap/view.ts", "utf8"),
ScriptTarget.ES2021, true);
const view = source.statements.find(item => isClassDeclaration(item) && item.name?.text === "ListMindmapView");
assert.ok(view && isClassDeclaration(view));
-const names = ["wheel", "boundedPan"];
+const names = ["wheel", "boundedPan", "contentRectangles", "contentBounds", "pointerMove"];
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 {
@@ -18,7 +18,7 @@ const compiled = transpileModule(`class View {
globalThis.View = View;`, {compilerOptions: {target: ScriptTarget.ES2021}}).outputText;
const setup = () => {
- const context: any = {clampMindmapPanOffset, WheelEvent: {DOM_DELTA_LINE: 1, DOM_DELTA_PAGE: 2}};
+ const context: any = {clampMindmapPanOffset, clampMindmapPanPosition, WheelEvent: {DOM_DELTA_LINE: 1, DOM_DELTA_PAGE: 2}};
runInNewContext(compiled, context);
const target = new context.View();
let draws = 0;
@@ -29,7 +29,8 @@ const setup = () => {
clientWidth: 400, clientHeight: 300, contains: (element: unknown) => !!element,
getBoundingClientRect: () => ({left: 10, top: 20}),
},
- contentBounds: () => ({left: 100, top: 100, right: 300, bottom: 200}),
+ positions: new Map([["root", {x: 100, y: 101, width: 200, height: 99}]]),
+ summaryPositions: new Map(), relationElements: new Map(), relationRoutes: new Map(),
draw: () => draws++, zoomAt: (...args: number[]) => zooms.push(args),
});
return {target, draws: () => draws, zooms};
@@ -65,7 +66,7 @@ test("vertical wheel scrolls the document when the embedded mind map is fully vi
test("visibility uses scaled content bounds and current offsets, including exact viewport edges", () => {
const {target, draws} = setup();
- target.contentBounds = () => ({left: 100, top: 50, right: 900, bottom: 650});
+ target.positions = new Map([["root", {x: 100, y: 51, width: 800, height: 599}]]);
Object.assign(target, {scale: .5, offsetX: -50, offsetY: -25});
const event = wheel();
target.wheel(event.event);
@@ -75,15 +76,30 @@ test("visibility uses scaled content bounds and current offsets, including exact
assert.equal(target.offsetY, -25);
});
-test("partially clipped content keeps the existing vertical canvas scrolling", () => {
+test("horizontal clipping does not change pure vertical wheel handling", () => {
+ for (const x of [-101, 201]) {
+ const {target, draws} = setup();
+ target.offsetX = x;
+ for (const deltaY of [-20, 20]) {
+ const event = wheel({deltaY});
+ target.wheel(event.event);
+ assert.equal(event.prevented(), 0);
+ assert.equal(event.stopped(), 0);
+ assert.equal(draws(), 0);
+ assert.equal(target.offsetX, x);
+ assert.equal(target.offsetY, 0);
+ }
+ }
+});
+
+test("vertically clipped content keeps the original canvas scrolling range", () => {
for (const bounds of [
- {left: -1, top: 100, right: 300, bottom: 200},
- {left: 100, top: 100, right: 401, bottom: 200},
{left: 100, top: -1, right: 300, bottom: 200},
{left: 100, top: 100, right: 300, bottom: 301},
]) {
const {target, draws} = setup();
- target.contentBounds = () => bounds;
+ target.positions = new Map([["root", {x: bounds.left, y: bounds.top + 1,
+ width: bounds.right - bounds.left, height: bounds.bottom - bounds.top - 1}]]);
const event = wheel();
target.wheel(event.event);
assert.equal(event.prevented(), 1);
@@ -93,6 +109,73 @@ test("partially clipped content keeps the existing vertical canvas scrolling", (
}
});
+test("wheel retains actual visible content when extreme nodes are horizontally clipped", () => {
+ const {target} = setup();
+ target.positions = new Map([
+ ["visible", {x: 100, y: 101, width: 100, height: 79}],
+ ["clipped", {x: 600, y: 901, width: 100, height: 99}],
+ ]);
+ const event = wheel({deltaY: 2000});
+ target.wheel(event.event);
+ assert.equal(event.prevented(), 1);
+ assert.equal(target.offsetX, 0);
+ assert.equal(target.offsetY, -116);
+ assert.equal(target.positions.get("visible").y + 79 + target.offsetY, 64);
+ const next = wheel();
+ target.wheel(next.event);
+ assert.equal(next.prevented(), 0, "further vertical input scrolls the document");
+ assert.equal(target.offsetY, -116);
+});
+
+test("vertical wheel still pans beyond the content edge until only a small portion remains", () => {
+ const {target} = setup();
+ target.positions = new Map([["root", {x: 100, y: 101, width: 200, height: 599}]]);
+ target.offsetY = -400;
+ const event = wheel();
+ target.wheel(event.event);
+ assert.equal(event.prevented(), 1, "content reaching the viewport edge does not end canvas panning");
+ assert.equal(target.offsetY, -420);
+ target.wheel(wheel({deltaY: 2000}).event);
+ assert.equal(target.offsetY, -636);
+ const next = wheel();
+ target.wheel(next.event);
+ assert.equal(next.prevented(), 0);
+});
+
+test("visible relation and summary labels remain reachable as pan references", () => {
+ const {target} = setup();
+ const relation = {hidden: false, style: {left: "800", top: "900"}, offsetWidth: 100, offsetHeight: 40};
+ target.relationElements.set("label", relation);
+ target.summaryPositions.set("summary", {x: 600, top: 400, bottom: 500,
+ labelX: 600, labelY: 400, width: 100, height: 40});
+ assert.deepEqual({...target.boundedPan(-700, -800)}, {x: -700, y: -800});
+ assert.deepEqual({...target.boundedPan(-500, -300)}, {x: -500, y: -300});
+ relation.hidden = true;
+ assert.notDeepEqual({...target.boundedPan(-700, -800)}, {x: -700, y: -800},
+ "hidden annotations cannot keep an otherwise empty viewport within range");
+});
+
+test("mobile touch panning retains visible content and chains overflow to the document", () => {
+ const {target} = setup();
+ target.positions = new Map([
+ ["visible", {x: 100, y: 101, width: 100, height: 79}],
+ ["clipped", {x: 600, y: 901, width: 100, height: 99}],
+ ]);
+ const document = {scrollTop: 0};
+ target.pointer = {pointerId: 1, x: 0, y: 0, startX: 100, startY: 100};
+ target.viewport.classList = {add: () => {}};
+ target.options = {host: {closest: () => document}};
+ target.setHoveredLine = () => {};
+ const event = {pointerId: 1, clientX: 100, clientY: -1900, pointerType: "touch", preventDefault: () => {}};
+ target.pointerMove(event);
+ assert.equal(target.offsetX, 0);
+ assert.equal(target.offsetY, -116);
+ assert.equal(document.scrollTop, 1884);
+ target.pointerMove({...event, clientY: -1920});
+ assert.equal(target.offsetY, -116);
+ assert.equal(document.scrollTop, 1904, "further touch movement scrolls the document at the same content boundary");
+});
+
test("fullscreen, horizontal, diagonal and Shift wheel keep canvas panning", () => {
for (const options of [
{fullscreenMarker: {}, event: {}},