Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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"
}
57 changes: 55 additions & 2 deletions packages/vgraph/__tests__/components/minimap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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",
Expand Down
68 changes: 33 additions & 35 deletions packages/vgraph/src/components/minimap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;

Expand All @@ -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();
Expand Down
Loading