From 2b45156fdca7722d3b50a81094ba39307ff1c81e Mon Sep 17 00:00:00 2001 From: fangsmile <892739385@qq.com> Date: Mon, 23 Nov 2026 11:58:00 +0800 Subject: [PATCH] fix: keep minimap viewport outline visible --- .../2026-11-23-minimap-viewport-clipping.json | 10 +++ .../__tests__/components/minimap.spec.ts | 57 +++++++++++++++- .../vgraph/src/components/minimap/index.ts | 68 +++++++++---------- 3 files changed, 98 insertions(+), 37 deletions(-) create mode 100644 common/changes/@visactor/vgraph/2026-11-23-minimap-viewport-clipping.json diff --git a/common/changes/@visactor/vgraph/2026-11-23-minimap-viewport-clipping.json b/common/changes/@visactor/vgraph/2026-11-23-minimap-viewport-clipping.json new file mode 100644 index 000000000..aafcf07df --- /dev/null +++ b/common/changes/@visactor/vgraph/2026-11-23-minimap-viewport-clipping.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@visactor/vgraph", + "comment": "Keep the minimap viewport outline visible at the map boundaries while preserving click and drag navigation.", + "type": "patch" + } + ], + "packageName": "@visactor/vgraph" +} diff --git a/packages/vgraph/__tests__/components/minimap.spec.ts b/packages/vgraph/__tests__/components/minimap.spec.ts index c4c063eee..525b6b32f 100644 --- a/packages/vgraph/__tests__/components/minimap.spec.ts +++ b/packages/vgraph/__tests__/components/minimap.spec.ts @@ -88,8 +88,8 @@ describe("src/minimap", () => { expect(viewport.style.display).toBe("block"); expect(viewport.style.left).toBe("94.66666666666667px"); expect(viewport.style.top).toBe("44.666666666666664px"); - expect(viewport.style.width).toBe("133.33333333333331px"); - expect(viewport.style.height).toBe("100px"); + expect(parseFloat(viewport.style.width)).toBeCloseTo(101.3333, 4); + expect(parseFloat(viewport.style.height)).toBeCloseTo(51.3333, 4); expect(canvas.children[0].children[2].children.length).toBe(1); const shape = canvas.children[0].children[2].children[0]; @@ -107,6 +107,59 @@ describe("src/minimap", () => { minimap.destroy(); }); + it("draws the visible intersection of an overflowing viewport", () => { + const minimap = new Minimap(graph, { width: 200, height: 100 }); + try { + minimap.setViewport(-5, -10, 160, 120); + expect(minimap.viewportStyles).toEqual({ + left: -5, + top: -10, + width: 160, + height: 120, + }); + expect(minimap.viewport.style.left).toBe("0px"); + expect(minimap.viewport.style.top).toBe("0px"); + expect(minimap.viewport.style.width).toBe("155px"); + expect(minimap.viewport.style.height).toBe("96px"); + expect(minimap.viewport.style.display).toBe("block"); + + minimap.setViewport(156, 72, 80, 50); + expect(minimap.viewport.style.width).toBe("40px"); + expect(minimap.viewport.style.height).toBe("24px"); + + minimap.setViewport(200, 12, 20, 20); + expect(minimap.viewport.style.display).toBe("none"); + } finally { + minimap.destroy(); + } + }); + + it("centers clicks using the full projected viewport", () => { + const minimap = new Minimap(graph, { width: 200, height: 100 }); + const translate = jest + .spyOn(graph, "translate") + .mockImplementation(() => {}); + const canvasContainer = minimap.canvas.get("container") as HTMLElement; + const clientBounds = jest + .spyOn(canvasContainer, "getBoundingClientRect") + .mockReturnValue({ + x: 30, + y: 40, + width: 196, + height: 96, + } as DOMRect); + try { + minimap.setViewport(-20, 15, 80, 40); + minimap.clickToCenter({ clientX: 40, clientY: 55 }); + const scale = graph.getZoomRatio() / minimap.ratio; + expect(translate).toHaveBeenCalledWith(10 * scale, 20 * scale); + } finally { + clientBounds.mockRestore(); + translate.mockRestore(); + minimap.destroy(); + } + }); + it("showEdges & setEdgeStyles should work", (done) => { const node2 = graph.add("node", { id: "node2", diff --git a/packages/vgraph/src/components/minimap/index.ts b/packages/vgraph/src/components/minimap/index.ts index 99d11affc..bfad09502 100644 --- a/packages/vgraph/src/components/minimap/index.ts +++ b/packages/vgraph/src/components/minimap/index.ts @@ -403,38 +403,45 @@ export class Minimap extends ComponentBase { } setViewport(left: number, top: number, width: number, height: number) { - const viewport = this.viewport; - const viewportStyles = { - left, - top, - width, - height, - }; - let show = true; - if ( + const canvasWidth = this.canvas.get("width"); + const canvasHeight = this.canvas.get("height"); + const visibleLeft = Math.max(0, left); + const visibleTop = Math.max(0, top); + const visibleWidth = Math.max( + 0, + Math.min(width - (visibleLeft - left), canvasWidth - visibleLeft) + ); + const visibleHeight = Math.max( + 0, + Math.min(height - (visibleTop - top), canvasHeight - visibleTop) + ); + const hideOverflow = this.options.hideWindowWhenOverflow !== false && left <= 0 && top <= 0 && - width > this.canvas.get("width") && - height > this.canvas.get("height") - ) { - show = false; - } - applyCss(viewport, { - left: left + "px", - top: top + "px", - width: width + "px", - height: height + "px", - display: show ? "block" : "none", + width > canvasWidth && + height > canvasHeight; + + // Keep the projected viewport for navigation, and draw its visible bounds. + this.viewportStyles = { left, top, width, height }; + applyCss(this.viewport, { + left: visibleLeft + "px", + top: visibleTop + "px", + width: visibleWidth + "px", + height: visibleHeight + "px", + display: + !hideOverflow && visibleWidth && visibleHeight ? "block" : "none", }); - this.viewportStyles = viewportStyles; } clickToCenter({ clientX, clientY }: { clientX: number; clientY: number }) { const graph = this.graph; - const bbox = this.viewport.getBoundingClientRect(); - const dx = bbox.x + bbox.width / 2 - clientX; - const dy = bbox.y + bbox.height / 2 - clientY; + const bounds = this.canvas.get("container").getBoundingClientRect(); + const { left, top, width, height } = this.viewportStyles; + const scaleX = bounds.width / this.canvas.get("width") || 1; + const scaleY = bounds.height / this.canvas.get("height") || 1; + const dx = bounds.x + (left + width / 2) * scaleX - clientX; + const dy = bounds.y + (top + height / 2) * scaleY - clientY; const zoomRatio = graph.getZoomRatio(); const ratio = this.ratio; graph.translate((dx / ratio) * zoomRatio, (dy / ratio) * zoomRatio); @@ -455,9 +462,7 @@ export class Minimap extends ComponentBase { if (!refresh) { return; } - const viewportStyles = this.viewportStyles; - let { left, top } = viewportStyles; - const { width, height } = viewportStyles; + const { left, top, width, height } = this.viewportStyles; let dx = this.lastPositions.x - e.clientX; let dy = this.lastPositions.y - e.clientY; @@ -480,14 +485,7 @@ export class Minimap extends ComponentBase { } } - left -= dx; - top -= dy; - applyCss(this.viewport, { - left: left + "px", - top: top + "px", - }); - viewportStyles.left = left; - viewportStyles.top = top; + this.setViewport(left - dx, top - dy, width, height); this.lastPositions = { x: e.clientX, y: e.clientY }; const ratio = this.ratio; const zoomRatio = this.graph.getZoomRatio();