An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:lipstick: Improve mind map canvas fitting https://github.com/siyuan-note/siyuan/issues/20020
Daniel8496fdd86c29
diff --git a/app/src/protyle/render/listMindmap/fit.test.ts b/app/src/protyle/render/listMindmap/fit.test.tsnew file mode 100644index 0000000000..3638bfd744--- /dev/null+++ b/app/src/protyle/render/listMindmap/fit.test.ts@@ -0,0 +1,74 @@+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 fit = view.members.find(item => item.name?.getText(source) === "fit");+assert.ok(fit);+const compiled = transpileModule(`class View {+ ${fit.getText(source)}+}+globalThis.View = View;`, {compilerOptions: {target: ScriptTarget.ES2021}}).outputText;++const setup = (right = 200, bottom = 100) => {+ const context: any = {};+ runInNewContext(compiled, context);+ const target = new context.View();+ Object.assign(target, {+ scale: 2.5, offsetX: -200, offsetY: 150,+ viewport: {clientWidth: 800, clientHeight: 420},+ positions: new Map([["root", {}]]),+ contentBounds: () => ({left: 20, top: 10, right, bottom}),+ draw: () => {},+ });+ return target;+};++const transform = (target: ReturnType<typeof setup>) => [target.scale, target.offsetX, target.offsetY];++test("fit restores a small map to its initial scale and centered position", () => {+ const target = setup();+ target.fit(1);+ const initial = transform(target);+ Object.assign(target, {scale: 2.5, offsetX: -200, offsetY: 150});+ target.fit();+ assert.deepEqual(transform(target), initial);+ assert.equal(target.scale, 1);+ assert.equal(target.offsetX, 290);+ assert.equal(target.offsetY, 142);+ target.fit();+ assert.deepEqual(transform(target), initial, "repeated fitting is stable");+});++test("fit still shrinks large maps and adapts to the current viewport", () => {+ const target = setup(2020, 1010);+ target.fit();+ assert.equal(target.scale, .362);+ assert.equal((20 + 2020) / 2 * target.scale + target.offsetX, 400);+ assert.equal((10 + 1010) / 2 * target.scale + target.offsetY, 197);+ target.viewport.clientWidth = 360;+ target.viewport.clientHeight = 300;+ target.fit();+ assert.equal(target.scale, .156);+ assert.equal((20 + 2020) / 2 * target.scale + target.offsetX, 180);+ assert.equal((10 + 1010) / 2 * target.scale + target.offsetY, 137);+});++test("fit keeps the existing minimum zoom and ignores an unavailable canvas", () => {+ const target = setup(10020, 10010);+ target.fit();+ assert.equal(target.scale, .15);+ const fitted = transform(target);+ target.viewport.clientWidth = 0;+ target.fit();+ assert.deepEqual(transform(target), fitted);+ target.viewport.clientWidth = 800;+ target.positions.clear();+ target.fit();+ assert.deepEqual(transform(target), fitted);+});diff --git a/app/src/protyle/render/listMindmap/model.test.ts b/app/src/protyle/render/listMindmap/model.test.tsindex 9c6c7570aa..94961881d6 100644--- a/app/src/protyle/render/listMindmap/model.test.ts+++ b/app/src/protyle/render/listMindmap/model.test.ts@@ -2180,6 +2180,7 @@ 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 initialTransform = [single.scale, single.offsetX, single.offsetY];const panButton = host.querySelector<HTMLButtonElement>('[aria-label="cursorHand"]');const panViewport = host.querySelector<HTMLElement>(".mindmap-view__viewport");check.equal(getComputedStyle(panViewport).cursor, "default");@@ -2203,8 +2204,11 @@ const browserCases = async (sourceCode: string, css: string, taskSource: string,check.equal(panButton.getAttribute("aria-pressed"), "false");check.equal(getComputedStyle(panViewport).cursor, "default");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");+ single.zoomAt(2.5);+ check.equal(single.scale, 2.5, "manual zoom can still enlarge a small map");+ host.querySelector<HTMLButtonElement>('[aria-label="listMindmapFit"]').click();+ check.deepEqual([single.scale, single.offsetX, single.offsetY], initialTransform,+ "fit restores the initial position and scale after panning and zooming");const fitViewport = host.querySelector<HTMLElement>(".mindmap-view__viewport").getBoundingClientRect();const fitNode = host.querySelector<HTMLElement>(".mindmap-view__node").getBoundingClientRect();check.ok(Math.abs((fitNode.left + fitNode.right) / 2 - (fitViewport.left + fitViewport.right) / 2) < 1,diff --git a/app/src/protyle/render/listMindmap/view.ts b/app/src/protyle/render/listMindmap/view.tsindex 50ccf9d18a..1669b528eb 100644--- a/app/src/protyle/render/listMindmap/view.ts+++ b/app/src/protyle/render/listMindmap/view.ts@@ -993,7 +993,7 @@ export class ListMindmapView {this.fitPrint();} else if (this.initialFit) {this.initialFit = false;- this.fit(1);+ this.fit();} else {const bounded = this.boundedPan(this.offsetX, this.offsetY);this.offsetX = bounded.x;@@ -2498,7 +2498,7 @@ export class ListMindmapView {this.draw();}- public fit(maxScale = 2.5) {+ public fit(maxScale = 1) {const width = this.viewport.clientWidth;const height = this.viewport.clientHeight;if (!this.positions.size || !width || !height) {