Skip to content

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

:sparkles: Add a hand tool for panning mind maps https://github.com/siyuan-note/siyuan/issues/18831

Vanessa0aaf6189120e
diff --git a/app/src/assets/scss/protyle/_list-mindmap.scss b/app/src/assets/scss/protyle/_list-mindmap.scss
index 0be442b943..f0c5a2bac1 100644
--- a/app/src/assets/scss/protyle/_list-mindmap.scss
+++ b/app/src/assets/scss/protyle/_list-mindmap.scss
@@ -142,6 +142,18 @@
pointer-events: none;
}
+ &__viewport--pan {
+ cursor: grab;
+
+ .list-mindmap__world {
+ pointer-events: none;
+ }
+
+ &.list-mindmap__viewport--dragging {
+ cursor: grabbing;
+ }
+ }
+
&__world {
position: absolute;
left: 0;
diff --git a/app/src/protyle/render/listMindmap/model.test.ts b/app/src/protyle/render/listMindmap/model.test.ts
index baed48d622..6edcc87c5b 100644
--- a/app/src/protyle/render/listMindmap/model.test.ts
+++ b/app/src/protyle/render/listMindmap/model.test.ts
@@ -1454,6 +1454,21 @@ const browserCases = async (sourceCode: string, css: string, taskSource: string,
const single = new api.ListMindmapView({...options, model: api.readListMindmap(singleList)});
await settle();
check.equal(single.scale, 1, "initial layout keeps a single node at its normal size");
+ const panButton = host.querySelector<HTMLButtonElement>('[aria-label="cursorHand"]');
+ panButton.click();
+ check.equal(panButton.getAttribute("aria-pressed"), "true");
+ const panNode = host.querySelector<HTMLElement>(".list-mindmap__node");
+ check.equal(getComputedStyle(panNode).pointerEvents, "none");
+ const panBefore = {x: single.offsetX, y: single.offsetY};
+ panNode.dispatchEvent(new PointerEvent("pointerdown", {bubbles: true, pointerId: 81, clientX: 100, clientY: 100}));
+ panNode.dispatchEvent(new PointerEvent("pointermove", {bubbles: true, pointerId: 81, clientX: 140, clientY: 125}));
+ panNode.dispatchEvent(new PointerEvent("pointerup", {bubbles: true, pointerId: 81, clientX: 140, clientY: 125}));
+ check.equal(single.offsetX, panBefore.x + 40, "hand tool pans from a node instead of moving it");
+ check.equal(single.offsetY, panBefore.y + 25);
+ check.equal(host.querySelector(".list-mindmap__ghost"), null);
+ panButton.click();
+ check.equal(panButton.getAttribute("aria-pressed"), "false");
+ check.ok(getComputedStyle(panNode).pointerEvents !== "none");
single.fit();
check.ok(single.scale > 1 && single.scale <= 2.5, "fit enlarges small maps within the zoom limit");
const fitViewport = host.querySelector<HTMLElement>(".list-mindmap__viewport").getBoundingClientRect();
diff --git a/app/src/protyle/render/listMindmap/view.ts b/app/src/protyle/render/listMindmap/view.ts
index a352608c0a..be0bfd3e13 100644
--- a/app/src/protyle/render/listMindmap/view.ts
+++ b/app/src/protyle/render/listMindmap/view.ts
@@ -123,6 +123,7 @@ export class ListMindmapView {
private suppressLinkClick = false;
private ghost?: HTMLDivElement;
private scale = 1;
+ private panning = false;
private offsetX = 0;
private offsetY = 0;
private foldAnchor?: {id: string, collapsed: boolean};
@@ -357,12 +358,25 @@ export class ListMindmapView {
this.toolbar.append(createElement("span", "list-mindmap__spacer fn__flex-1"));
if (!this.options.readOnly) {
add("relation", "connect", "iconRoute", () => {
+ this.setPanning(false);
this.inspector.hidden = true;
this.relationFrom = this.relationFrom ? undefined : this.selectedId;
this.relationPreview = undefined;
this.updateSelection();
});
}
+ add("pan", "cursorHand", "iconHand", () => {
+ this.finishThen(() => {
+ this.finishRelationEdit?.(true);
+ this.cancelPointer();
+ this.setPanning(!this.panning);
+ this.relationFrom = undefined;
+ this.relationPreview = undefined;
+ this.inspector.hidden = true;
+ this.updateSelection();
+ });
+ });
+ this.setPanning(this.panning);
const zoomControl = createElement("div", "list-mindmap__zoom-control");
const zoomActions = createElement("div", "list-mindmap__zoom-actions");
this.zoomLabel.tabIndex = 0;
@@ -381,6 +395,14 @@ export class ListMindmapView {
add("fullscreen", "fullscreen", "iconFullscreen", () => this.toggleFullscreen());
}
+ private setPanning(value: boolean) {
+ this.panning = value;
+ this.viewport.classList.toggle("list-mindmap__viewport--pan", value);
+ const button = this.buttons.get("pan");
+ button?.classList.toggle("block__icon--active", value);
+ button?.setAttribute("aria-pressed", String(value));
+ }
+
public update(model: ListMindmapModel) {
if (this.pointer?.relation) {
this.cancelPointer();
@@ -1262,7 +1284,11 @@ export class ListMindmapView {
};
private beginPointer(event: PointerEvent, id?: string) {
- if (!id && !this.options.readOnly && !this.relationFrom) {
+ if (this.panning) {
+ id = undefined;
+ event.preventDefault();
+ }
+ if (!this.panning && !id && !this.options.readOnly && !this.relationFrom) {
const line = this.findLine(event);
const points = line?.relation && this.relationRoutes.get(line.id);
if (points) {
@@ -1328,6 +1354,10 @@ export class ListMindmapView {
}
const pointer = this.pointer;
if (!pointer) {
+ if (this.panning) {
+ this.setHoveredLine();
+ return;
+ }
const target = event.target as Element;
const relation = target.closest<HTMLElement>(".list-mindmap__relation[data-relation-id]");
const line = target.closest(".list-mindmap__node, input, button") ? undefined : this.findLine(event);
@@ -1459,7 +1489,7 @@ export class ListMindmapView {
this.cancelPointer();
if (moved && id && targetId && placement) {
this.options.onMove?.(id, targetId, placement);
- } else if (!moved && !id && !this.options.readOnly) {
+ } else if (!moved && !id && !this.options.readOnly && !this.panning) {
this.selectLine(event);
}
};
@@ -1569,7 +1599,7 @@ export class ListMindmapView {
return;
}
clearTimeout(this.linkTimer);
- if (this.suppressLinkClick || this.relationFrom || event.detail > 1) {
+ if (this.panning || this.suppressLinkClick || this.relationFrom || event.detail > 1) {
event.preventDefault();
return;
}
@@ -1592,7 +1622,7 @@ export class ListMindmapView {
private doubleClick = (event: MouseEvent) => {
clearTimeout(this.linkTimer);
- if (this.options.readOnly || this.relationFrom) {
+ if (this.options.readOnly || this.relationFrom || this.panning) {
return;
}
const target = event.target as HTMLElement;