An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Reduce transient toolbar clearance when fitting mind maps https://github.com/siyuan-note/siyuan/issues/20695
Daniel96ddbedcaf2c
diff --git a/app/src/protyle/render/listMindmap/fit.test.ts b/app/src/protyle/render/listMindmap/fit.test.tsindex fc404764fb..69aaf5a292 100644--- a/app/src/protyle/render/listMindmap/fit.test.ts+++ b/app/src/protyle/render/listMindmap/fit.test.ts@@ -21,6 +21,7 @@ const setup = (right = 200, bottom = 100) => {const target = new context.View();Object.assign(target, {scale: 2.5, offsetX: -200, offsetY: 150,+ options: {host: {classList: {contains: () => false}}},viewport: {clientWidth: 800, clientHeight: 420, offsetHeight: 420,getBoundingClientRect: () => ({left: 0, top: 0, right: 800, bottom: 420, width: 800, height: 420})},getToolbarBounds: (): unknown[] => [],@@ -42,7 +43,7 @@ test("fit restores a small map to its initial scale and centered position", () =assert.deepEqual(transform(target), initial);assert.equal(target.scale, 1);assert.equal(target.offsetX, 290);- assert.equal(target.offsetY, 130);+ assert.equal(target.offsetY, 155);target.fit();assert.deepEqual(transform(target), initial, "repeated fitting is stable");});@@ -50,15 +51,15 @@ test("fit restores a small map to its initial scale and centered position", () =test("fit still shrinks large maps and adapts to the current viewport", () => {const target = setup(2020, 1010);target.fit();- assert.equal(target.scale, .338);+ assert.equal(target.scale, .376);assert.equal((20 + 2020) / 2 * target.scale + target.offsetX, 400);- assert.equal((10 + 1010) / 2 * target.scale + target.offsetY, 185);+ assert.equal((10 + 1010) / 2 * target.scale + target.offsetY, 210);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, 125);+ assert.equal((10 + 1010) / 2 * target.scale + target.offsetY, 150);});test("fit keeps the existing minimum zoom and ignores an unavailable canvas", () => {@@ -75,15 +76,19 @@ test("fit keeps the existing minimum zoom and ignores an unavailable canvas", ()assert.deepEqual(transform(target), fitted);});-test("fit centers the whole map below visible toolbar controls and is stable across repeated clicks", () => {+test("fullscreen fit centers the whole map below the persistent toolbar and is stable across repeated clicks", () => {for (const height of [32, 64]) {const target = setup(2020, 1010);- target.getToolbarBounds = () => [{left: 500, top: 0, right: 780, bottom: height, width: 280, height}];+ target.options.host.classList.contains = (name: string) => name === "fullscreen";+ target.getToolbarBounds = (includePopup: boolean) => {+ assert.equal(includePopup, false, "the zoom popup does not reduce the fitting area");+ return [{left: 500, top: 0, right: 780, bottom: height, width: 280, height}];+ };target.fit();- const availableHeight = 420 - height - 16 - 66;+ const availableHeight = 420 - height - 16 - 16;assert.equal(target.scale, availableHeight / 1000);assert.ok(Math.abs(10 * target.scale + target.offsetY - height - 16) < 1e-9);- assert.ok(Math.abs(1010 * target.scale + target.offsetY - 420 + 66) < 1e-9);+ assert.ok(Math.abs(1010 * target.scale + target.offsetY - 420 + 16) < 1e-9);const fitted = transform(target);target.fit();assert.deepEqual(transform(target), fitted);@@ -92,30 +97,47 @@ test("fit centers the whole map below visible toolbar controls and is stable acrtest("fit converts scaled toolbar bounds to local canvas coordinates", () => {const target = setup(2020, 1010);+ target.options.host.classList.contains = (name: string) => name === "fullscreen";target.viewport.getBoundingClientRect = () =>({left: 10, top: 20, right: 410, bottom: 230, width: 400, height: 210});target.getToolbarBounds = () => [{left: 260, top: 20, right: 400, bottom: 44, width: 140, height: 24}];target.fit();- assert.equal(target.scale, .29);+ assert.equal(target.scale, .34);assert.ok(Math.abs(10 * target.scale + target.offsetY - 64) < 1e-9);- assert.ok(Math.abs(1010 * target.scale + target.offsetY - 354) < 1e-9);+ assert.ok(Math.abs(1010 * target.scale + target.offsetY - 404) < 1e-9);});test("toolbar controls outside the viewport do not reduce the fitting area", () => {const target = setup(2020, 1010);+ target.options.host.classList.contains = (name: string) => name === "fullscreen";target.getToolbarBounds = () => [{left: 810, top: 0, right: 900, bottom: 64, width: 90, height: 64},{left: 500, top: 430, right: 780, bottom: 480, width: 280, height: 50},];target.fit();- assert.equal(target.scale, .338);- assert.ok(Math.abs(10 * target.scale + target.offsetY - 16) < 1e-9);+ assert.equal(target.scale, .376);+ assert.ok(Math.abs(10 * target.scale + target.offsetY - 22) < 1e-9);});test("fit at minimum zoom keeps the map top below the toolbar when the map is too tall", () => {const target = setup(1020, 5010);+ target.options.host.classList.contains = (name: string) => name === "fullscreen";target.getToolbarBounds = () => [{left: 500, top: 0, right: 780, bottom: 48, width: 280, height: 48}];target.fit();assert.equal(target.scale, .15);assert.equal(10 * target.scale + target.offsetY, 64);});++test("document fit uses the same area with transient toolbars open or closed", () => {+ const target = setup(2020, 1010);+ target.fit();+ const fitted = transform(target);+ target.getToolbarBounds = () => {+ assert.fail("document fitting must not measure transient toolbar bounds");+ };+ target.fit();+ assert.deepEqual(transform(target), fitted);+ assert.ok(Math.abs(10 * target.scale + target.offsetY - 22) < 1e-9,+ "width-limited maps remain vertically centered");+ assert.ok(Math.abs(1010 * target.scale + target.offsetY - 398) < 1e-9);+});diff --git a/app/src/protyle/render/listMindmap/model.test.ts b/app/src/protyle/render/listMindmap/model.test.tsindex b1282376cd..38a2be12af 100644--- a/app/src/protyle/render/listMindmap/model.test.ts+++ b/app/src/protyle/render/listMindmap/model.test.ts@@ -1540,7 +1540,7 @@ const browserCases = async (sourceCode: string, css: string, taskSource: string,check.deepEqual([navigationView.offsetX, navigationView.offsetY], hiddenToolbarTransform,"a hidden toolbar does not move a visible node");- // 高脑图实际点击适应画布后,整图在工具栏下方缩放居中,重复点击保持稳定。+ // 实际点击适应画布时不为临时浮层预留空间,全屏仍避让常驻按钮组,重复点击保持稳定。const fittingSource = document.createElement("div");fittingSource.innerHTML = lute.Md2BlockDOM("* Root\n" +Array.from({length: 20}, (_value, index) => ` * Branch ${index}\n`).join(""));@@ -1611,18 +1611,31 @@ const browserCases = async (sourceCode: string, css: string, taskSource: string,check.ok(navigationView.scale < 1 && navigationView.scale > .15,`the tall map needs an unconstrained fit, mode=${mode}, width=${width}, scale=${navigationView.scale}`);const viewportBounds = navigationViewport.getBoundingClientRect();- const toolbarBottom = Math.max(...navigationView.getToolbarBounds().filter((bounds: DOMRect) =>+ const toolbarBottom = Math.max(...navigationView.getToolbarBounds(false).filter((bounds: DOMRect) =>bounds.right > viewportBounds.left && bounds.left < viewportBounds.right).map((bounds: DOMRect) => bounds.bottom));const fittedContent = navigationView.contentBounds();const contentTop = viewportBounds.top + (fittedContent.top * navigationView.scale + navigationView.offsetY) * outerScale;const contentBottom = viewportBounds.top + (fittedContent.bottom * navigationView.scale + navigationView.offsetY) * outerScale;- check.ok(contentTop >= toolbarBottom + 15 * outerScale,- `fit leaves the whole map below the toolbar, mode=${mode}, width=${width}`);- check.ok(contentBottom <= viewportBounds.bottom - 65 * outerScale, "fit preserves the bottom panel clearance");+ if (mode === "fullscreen") {+ check.ok(contentTop >= toolbarBottom + 15 * outerScale,+ `fullscreen fit clears the persistent toolbar, width=${width}`);+ } else {+ check.ok(Math.abs(contentTop - viewportBounds.top - 16 * outerScale) < 1,+ `document fit ignores triggered toolbars and the zoom popup, mode=${mode}, width=${width}`);+ }+ check.ok(Math.abs(contentBottom - viewportBounds.bottom + 16 * outerScale) < 1,+ "fit uses the bottom area without reserving space for the node menu");const fittedTransform = [navigationView.scale, navigationView.offsetX, navigationView.offsetY];await activateNavigationFit(mode === "focus", mode === "touch");check.deepEqual([navigationView.scale, navigationView.offsetX, navigationView.offsetY], fittedTransform,"repeated fit activation preserves scale and centering");+ navigationHost.focus();+ await moveNavigationPointer(false);+ check.equal(getComputedStyle(navigationToolbar.querySelector(".mindmap-view__zoom-actions")).display, "none",+ "the transient zoom popup can close after fitting");+ navigationView.fit();+ check.deepEqual([navigationView.scale, navigationView.offsetX, navigationView.offsetY], fittedTransform,+ "closing the transient popup does not change the fitted view");navigationToolbar.querySelector<HTMLButtonElement>('[aria-label="reset"]').click();check.equal(navigationView.scale, 1, "reset restores 100% independently of fit to canvas");}@@ -1640,6 +1653,22 @@ const browserCases = async (sourceCode: string, css: string, taskSource: string,const hiddenFitBounds = navigationView.contentBounds();check.ok(Math.abs(hiddenFitBounds.top * navigationView.scale + navigationView.offsetY - 16) < 1,"fitting with a hidden toolbar keeps the normal top margin");+ const withoutInspector = [navigationView.scale, navigationView.offsetX, navigationView.offsetY];+ navigationView.setReadOnly(false);+ navigationView.setInteractionActive(true);+ navigationView.selectNode(navigationView.model.root.children[0].id);+ await settle();+ check.equal(navigationInspector.hidden, false, "selecting a node opens the bottom menu");+ check.ok(navigationInspector.getBoundingClientRect().height > 0, "the bottom menu occupies actual screen space");+ navigationView.fit();+ check.deepEqual([navigationView.scale, navigationView.offsetX, navigationView.offsetY], withoutInspector,+ "opening the bottom node menu does not shrink the fitted map");+ navigationView.setInteractionActive(false);+ await settle();+ check.equal(navigationInspector.hidden, true, "leaving interaction mode hides the node menu");+ navigationView.fit();+ check.deepEqual([navigationView.scale, navigationView.offsetX, navigationView.offsetY], withoutInspector,+ "hiding the bottom node menu keeps the fitted view stable");for (const background of ["rgb(255, 255, 255)", "rgb(30, 30, 30)"]) {for (const width of [760, 320, 220]) {for (const fullscreen of [false, true]) {@@ -1673,7 +1702,7 @@ const browserCases = async (sourceCode: string, css: string, taskSource: string,lock.disabled = false;check.ok(navigationView.getToolbarBounds().some((rect: DOMRect) =>rect.left === groupBounds.left && rect.right === groupBounds.right && rect.bottom === groupBounds.bottom),- "fit and node reveal avoid the complete opaque toolbar group");+ "node reveal measures the complete opaque toolbar group");}}}diff --git a/app/src/protyle/render/listMindmap/view.ts b/app/src/protyle/render/listMindmap/view.tsindex cdff244f29..a63d92f315 100644--- a/app/src/protyle/render/listMindmap/view.ts+++ b/app/src/protyle/render/listMindmap/view.ts@@ -1161,15 +1161,16 @@ export class ListMindmapView {this.options.host.querySelector<HTMLElement>(`[data-mindmap-source-id="${id}"]`);}- private getToolbarBounds() {+ private getToolbarBounds(includePopup = true) {const bounds: DOMRect[] = [];const toolbarStyle = getComputedStyle(this.toolbar);if (this.toolbar.hidden || toolbarStyle.display === "none" || toolbarStyle.visibility === "hidden" ||Number(toolbarStyle.opacity) === 0) {return bounds;}- // 顶部遮挡范围只包含可见控件及展开的缩放面板,不包含左侧空白区域。- this.toolbar.querySelectorAll<HTMLElement>(".mindmap-view__toolbar-group, .mindmap-view__zoom-actions")+ // 节点定位避让可见控件及缩放面板;整图适配只考虑常驻按钮组,不包含左侧空白。+ this.toolbar.querySelectorAll<HTMLElement>(includePopup ?+ ".mindmap-view__toolbar-group, .mindmap-view__zoom-actions" : ".mindmap-view__toolbar-group").forEach(control => {const rect = control.getBoundingClientRect();const style = getComputedStyle(control);@@ -3426,21 +3427,21 @@ export class ListMindmapView {const viewport = this.viewport.getBoundingClientRect();const scaleY = viewport.height / (this.viewport.offsetHeight || viewport.height) || 1;let insetTop = 16;- // 整图适配在顶部控件下方缩放居中,外层缩放的屏幕尺寸换算为画布局部尺寸。- this.getToolbarBounds().forEach(bounds => {+ // 只为全屏常驻按钮组预留空间,文档内工具栏和临时浮层不缩小整图适配范围。+ const toolbarBounds = this.options.host.classList.contains("fullscreen") ? this.getToolbarBounds(false) : [];+ toolbarBounds.forEach(bounds => {if (bounds.right > viewport.left && bounds.left < viewport.right &&bounds.bottom > viewport.top && bounds.top < viewport.bottom) {insetTop = Math.max(insetTop, (bounds.bottom - viewport.top) / scaleY + 16);}});- // 底部为菜单、外侧间距和横向滚动条留出空间,避免选中节点时画布跳动。- const insetBottom = 66;+ const insetBottom = 16;const availableWidth = Math.max(1, width - 48);const availableHeight = Math.max(1, height - insetTop - insetBottom);this.scale = Math.min(maxScale, Math.max(.15, Math.min(availableWidth / Math.max(1, right - left),availableHeight / Math.max(1, bottom - top))));this.offsetX = width / 2 - (left + right) * this.scale / 2;- // 达到最小缩放仍无法容纳整图时,优先保证顶部正文避开工具栏。+ // 达到最小缩放仍无法容纳整图时,优先保证顶部正文留在可用区域内。this.offsetY = (bottom - top) * this.scale > availableHeight ? insetTop - top * this.scale :insetTop + availableHeight / 2 - (top + bottom) * this.scale / 2;this.draw();