Skip to content

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

:sparkles: Support mobile mind map pinch zoom https://github.com/siyuan-note/siyuan/issues/19865

Daniel7232e4a87525
diff --git a/app/src/protyle/render/listMindmap/pinch.test.ts b/app/src/protyle/render/listMindmap/pinch.test.ts
new file mode 100644
index 0000000000..4c83ed1805
--- /dev/null
+++ b/app/src/protyle/render/listMindmap/pinch.test.ts
@@ -0,0 +1,89 @@
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {test} from "node:test";
+import {runInNewContext} from "node:vm";
+import {createSourceFile, isClassDeclaration, ScriptTarget, transpileModule} from "typescript";
+
+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 = ["pinchStart", "pinchMove", "pinchEnd", "pointerDown", "pointerMove", "zoomAt"];
+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")}
+}
+globalThis.View = View;`, {compilerOptions: {target: ScriptTarget.ES2021}}).outputText;
+
+const setup = () => {
+ const context: any = {clearTimeout};
+ runInNewContext(compiled, context);
+ const target = new context.View();
+ let cancelled = 0;
+ Object.assign(target, {
+ scale: 1, offsetX: 0, offsetY: 0, pinching: false,
+ viewport: {contains: (element: {outside?: boolean}) => !element.outside,
+ getBoundingClientRect: () => ({left: 10, top: 20})},
+ cancelPointer: () => { cancelled++; target.pendingPointerId = undefined; },
+ boundedPan: (x: number, y: number) => ({x, y}), draw: () => {},
+ });
+ return {target, cancelled: () => cancelled};
+};
+
+const touch = (clientX: number, clientY = 120, excluded = false, outside = false) => ({
+ clientX, clientY, target: {closest: () => excluded, outside},
+});
+const event = (...touches: ReturnType<typeof touch>[]) => ({touches, preventDefault() {}});
+
+test("pinch cancels dragging and zooms around the finger midpoint within existing limits", () => {
+ const {target, cancelled} = setup();
+ target.pendingPointerId = 1;
+ target.pinchStart(event(touch(60), touch(160)));
+ assert.equal(cancelled(), 1);
+ assert.equal(target.pendingPointerId, undefined);
+ assert.equal(target.suppressLinkClick, true);
+ target.pinchMove(event(touch(10), touch(210)));
+ assert.equal(target.scale, 2);
+ assert.equal(target.offsetX, -100);
+ assert.equal(target.offsetY, -100);
+ target.pinchMove(event(touch(-1000), touch(1000)));
+ assert.equal(target.scale, 2.5);
+ target.pinchMove(event(touch(100), touch(101)));
+ assert.equal(target.scale, .15);
+});
+
+test("remaining fingers cannot start a drag and a new gesture starts at the current scale", () => {
+ const {target} = setup();
+ target.pinchStart(event(touch(60), touch(160)));
+ target.pinchMove(event(touch(10), touch(210)));
+ target.pinchEnd(event(touch(10)));
+ assert.equal(target.pinching, true);
+ target.pointerDown({});
+ target.pointerMove({});
+ target.pinchMove(event(touch(20)));
+ assert.equal(target.scale, 2);
+ target.pinchEnd(event());
+ assert.equal(target.pinching, false);
+ target.pinchStart(event(touch(60), touch(160)));
+ target.pinchMove(event(touch(85), touch(135)));
+ assert.equal(target.scale, 1);
+});
+
+test("editing controls and touches outside the map do not activate pinch", () => {
+ const {target, cancelled} = setup();
+ target.pinchStart(event(touch(60)));
+ target.pinchStart(event(touch(60), touch(160, 120, true)));
+ target.pinchStart(event(touch(60), touch(160, 120, false, true)));
+ target.pointerDown({pointerType: "touch", isPrimary: false});
+ assert.equal(target.pinching, false);
+ assert.equal(cancelled(), 0);
+ target.pinchStart(event(touch(60), touch(60)));
+ target.pinchMove(event(touch(50), touch(100)));
+ assert.equal(target.scale, 1);
+ target.pinchStart(event(touch(50), touch(100), touch(150)));
+ target.pinchMove(event(touch(0), touch(100), touch(200)));
+ assert.equal(target.scale, 1);
+ target.pinchEnd(event());
+ assert.equal(target.pinching, false);
+});
diff --git a/app/src/protyle/render/listMindmap/view.ts b/app/src/protyle/render/listMindmap/view.ts
index eae7826f91..fc6ca41032 100644
--- a/app/src/protyle/render/listMindmap/view.ts
+++ b/app/src/protyle/render/listMindmap/view.ts
@@ -130,6 +130,8 @@ export class ListMindmapView {
private editingId?: string;
private pointer?: PointerState;
private pointerCapture?: HTMLElement;
+ private pinching = false;
+ private pinch?: {distance: number, scale: number};
private pendingPointerId?: number;
private endInteraction?: () => void;
private linkTimer = 0;
@@ -190,6 +192,14 @@ export class ListMindmapView {
this.listen(this.viewport, "pointermove", this.pointerMove);
this.listen(this.viewport, "pointerleave", () => this.setHoveredLine());
this.listen(this.viewport, "pointerup", this.pointerUp);
+ this.listen(this.viewport, "touchstart", this.pinchStart, {passive: false});
+ this.listen(this.viewport, "touchmove", this.pinchMove, {passive: false});
+ this.listen(this.viewport, "touchend", this.pinchEnd, {passive: false});
+ this.listen(this.viewport, "touchcancel", this.pinchEnd, {passive: false});
+ this.listen(window, "blur", () => {
+ this.pinching = false;
+ this.pinch = undefined;
+ });
this.listen(this.viewport, "contextmenu", (event: MouseEvent) => {
if (this.suppressPanContextMenu) {
event.preventDefault();
@@ -1442,7 +1452,55 @@ export class ListMindmapView {
}
}
+ private pinchStart = (event: TouchEvent) => {
+ if (event.touches.length !== 2 || Array.from(event.touches).some(touch =>
+ !this.viewport.contains(touch.target as Node) ||
+ (touch.target as Element).closest("button, input, select, textarea, audio, video, iframe, .mindmap-view__node--editing"))) {
+ this.pinch = undefined;
+ return;
+ }
+ event.preventDefault();
+ this.cancelPointer();
+ clearTimeout(this.linkTimer);
+ this.suppressLinkClick = true;
+ this.pinching = true;
+ const [first, second] = Array.from(event.touches);
+ this.pinch = {distance: Math.hypot(first.clientX - second.clientX, first.clientY - second.clientY), scale: this.scale};
+ };
+
+ private pinchMove = (event: TouchEvent) => {
+ if (!this.pinching) {
+ return;
+ }
+ event.preventDefault();
+ if (!this.pinch || event.touches.length !== 2 || this.pinch.distance === 0) {
+ return;
+ }
+ const [first, second] = Array.from(event.touches);
+ const bounds = this.viewport.getBoundingClientRect();
+ this.zoomAt(this.pinch.scale * Math.hypot(first.clientX - second.clientX, first.clientY - second.clientY) /
+ this.pinch.distance, (first.clientX + second.clientX) / 2 - bounds.left,
+ (first.clientY + second.clientY) / 2 - bounds.top);
+ };
+
+ private pinchEnd = (event: TouchEvent) => {
+ if (!this.pinching) {
+ return;
+ }
+ event.preventDefault();
+ this.pinch = undefined;
+ // 双指结束后等待所有手指离开,避免剩余手指触发节点拖动或点击。
+ if (event.touches.length === 0) {
+ this.pinching = false;
+ } else if (event.touches.length === 2) {
+ this.pinchStart(event);
+ }
+ };
+
private pointerDown = (event: PointerEvent) => {
+ if (this.pinching || event.pointerType === "touch" && !event.isPrimary) {
+ return;
+ }
clearTimeout(this.linkTimer);
this.suppressLinkClick = false;
this.suppressPanContextMenu = false;
@@ -1526,6 +1584,9 @@ export class ListMindmapView {
}
private pointerMove = (event: PointerEvent) => {
+ if (this.pinching) {
+ return;
+ }
if (this.relationFrom && !this.pointer?.rightButton) {
const bounds = this.viewport.getBoundingClientRect();
const id = (event.target as Element).closest<HTMLElement>(".mindmap-view__node")?.dataset.mindmapId;
@@ -1821,7 +1882,7 @@ export class ListMindmapView {
private doubleClick = (event: MouseEvent) => {
clearTimeout(this.linkTimer);
- if (this.options.readOnly || this.relationFrom || this.panning) {
+ if (this.options.readOnly || this.relationFrom || this.panning || this.pinching) {
return;
}
const target = event.target as HTMLElement;