Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat(schema-diagram): fit content within view (#3956)

Liu Jiebbb3ceb2964
diff --git a/frontend/src/components/SchemaDiagram/Canvas/Canvas.vue b/frontend/src/components/SchemaDiagram/Canvas/Canvas.vue
index f86789825e..7b99fb3c35 100644
--- a/frontend/src/components/SchemaDiagram/Canvas/Canvas.vue
+++ b/frontend/src/components/SchemaDiagram/Canvas/Canvas.vue
@@ -14,6 +14,19 @@
<div class="!absolute right-2 bottom-2 flex items-center gap-x-2">
<slot name="controls" />
+ <NButtonGroup size="tiny" class="bg-white rounded">
+ <NTooltip>
+ <template #trigger>
+ <NButton tooltip>
+ <Square2x2 @click="handleFitView" />
+ </NButton>
+ </template>
+ <div class="whitespace-nowrap">
+ {{ $t("schema-diagram.fit-content-with-view") }}
+ </div>
+ </NTooltip>
+ </NButtonGroup>
+
<ZoomButton
:min="ZOOM_RANGE.min"
:max="ZOOM_RANGE.max"
@@ -30,15 +43,19 @@
import { ref } from "vue";
import { useEventListener } from "@vueuse/core";
import normalizeWheel from "normalize-wheel";
+import { NButtonGroup, NButton } from "naive-ui";
+import Square2x2 from "~icons/heroicons-outline/squares-2x2";
import { Position } from "../types";
import { useDraggable, minmax, useSchemaDiagramContext } from "../common";
import ZoomButton from "./ZoomButton.vue";
+import { fitView } from "./libs/fitView";
const canvas = ref<Element>();
const desktop = ref<Element>();
-const { zoom, position } = useSchemaDiagramContext();
+const { tableList, rectOfTable, zoom, position, events } =
+ useSchemaDiagramContext();
const zoomCenter = ref<Position>({ x: 0.5, y: 0.5 });
const ZOOM_RANGE = {
@@ -46,6 +63,23 @@ const ZOOM_RANGE = {
min: 0.05, // 5%
};
+const handleFitView = () => {
+ if (!canvas.value) return;
+ const rects = tableList.value.map(rectOfTable);
+ const layout = fitView(
+ canvas.value,
+ rects,
+ [10, 20, 40, 20] /* paddings [T,R,B,L] */,
+ [ZOOM_RANGE.min, 1.0] /* [zoomMin, zoomMax] */
+ );
+ zoom.value = layout.zoom;
+ position.value = {
+ x: layout.rect.x,
+ y: layout.rect.y,
+ };
+};
+events.on("fit-view", handleFitView);
+
const handleZoom = (delta: number, center: Position = { x: 0.5, y: 0.5 }) => {
if (!canvas.value) return "";
diff --git a/frontend/src/components/SchemaDiagram/Canvas/libs/fitView.ts b/frontend/src/components/SchemaDiagram/Canvas/libs/fitView.ts
new file mode 100644
index 0000000000..fa498898b8
--- /dev/null
+++ b/frontend/src/components/SchemaDiagram/Canvas/libs/fitView.ts
@@ -0,0 +1,72 @@
+import { calcBBox, minmax, pointsOfRect } from "../../common";
+import { Rect } from "../../types";
+
+export const fitView = (
+ canvas: Element,
+ rects: Rect[],
+ paddings: number[] = [0, 0, 0, 0], // [T,R,B,L]
+ zoomRange: number[] = [0.5, 2]
+) => {
+ const contentBBox = calcBBox(rects.flatMap(pointsOfRect));
+ if (
+ contentBBox.width < Number.EPSILON ||
+ contentBBox.height < Number.EPSILON
+ ) {
+ return { zoom: 1, rect: contentBBox };
+ }
+
+ const canvasBBox = canvas.getBoundingClientRect();
+ const [paddingTop, paddingRight, paddingBottom, paddingLeft] = paddings;
+ const viewBBox = {
+ x: paddingLeft,
+ y: paddingTop,
+ width: canvasBBox.width - paddingLeft - paddingRight,
+ height: canvasBBox.height - paddingTop - paddingBottom,
+ };
+
+ const layout = calcFitRect(contentBBox, viewBBox, zoomRange);
+ layout.rect.x += paddingLeft;
+ layout.rect.y += paddingTop;
+ return layout;
+};
+
+const calcFitRect = (content: Rect, view: Rect, zoomRange: number[]) => {
+ const contentMarginTop = content.y;
+ const contentMarginLeft = content.x;
+
+ const contentWHRatio = content.width / content.height;
+ const canvasWHRatio = view.width / view.height;
+
+ if (contentWHRatio > canvasWHRatio) {
+ // content is wider than canvas, fit horizontally
+ const targetWidth = view.width;
+ const targetZoom = targetWidth / content.width;
+ const zoom = minmax(targetZoom, zoomRange[0], zoomRange[1]);
+ const targetSize = {
+ width: targetWidth,
+ height: content.height * zoom,
+ };
+ const targetPos = {
+ x: -contentMarginLeft * zoom,
+ y: (view.height - targetSize.height) / 2 - contentMarginTop * zoom,
+ };
+
+ const rect = { ...targetSize, ...targetPos };
+ return { zoom, rect };
+ } else {
+ // content is taller than canvas, fit vertically
+ const targetHeight = view.height;
+ const targetZoom = targetHeight / content.height;
+ const zoom = minmax(targetZoom, zoomRange[0], zoomRange[1]);
+ const targetSize = {
+ width: content.width * zoom,
+ height: targetHeight,
+ };
+ const targetPos = {
+ x: (view.width - targetSize.width) / 2 - contentMarginLeft * zoom,
+ y: -contentMarginTop * zoom,
+ };
+ const rect = { ...targetSize, ...targetPos };
+ return { zoom, rect };
+ }
+};
diff --git a/frontend/src/components/SchemaDiagram/ER/TableNode.vue b/frontend/src/components/SchemaDiagram/ER/TableNode.vue
index a7be2c9004..3be8dab23e 100644
--- a/frontend/src/components/SchemaDiagram/ER/TableNode.vue
+++ b/frontend/src/components/SchemaDiagram/ER/TableNode.vue
@@ -1,6 +1,6 @@
<template>
<div
- class="absolute rounded-md shadow-lg border-b border-gray-200 bg-white w-[14rem] divide-y z-[10]"
+ class="absolute rounded-md shadow-lg border-b border-gray-200 bg-white w-[16rem] divide-y z-[10]"
bb-node-type="table"
:bb-node-id="idOfTable(table)"
:style="{
@@ -33,16 +33,20 @@
/>
</td>
<td class="w-auto text-xs py-1.5">
- <div class="whitespace-pre-wrap break-words">
+ <div class="whitespace-pre-wrap break-words pr-1.5">
{{ column.name }}
</div>
</td>
- <td class="w-20 text-xs text-gray-400 py-1.5">
- <div class="flex items-center w-full overflow-hidden pr-2">
- <EllipsisText class="w-full text-right">
- {{ column.type }}
- </EllipsisText>
- </div>
+ <td class="w-[6rem] text-xs text-gray-400 py-1.5 pr-1.5">
+ <NEllipsis
+ class="w-full text-right"
+ :tooltip="{
+ showArrow: false,
+ contentStyle: tooltipStyle,
+ }"
+ >
+ {{ column.type }}
+ </NEllipsis>
</td>
</tr>
</table>
@@ -51,6 +55,7 @@
<script lang="ts" setup>
import { computed } from "vue";
+import { NEllipsis } from "naive-ui";
import { hashCode } from "@/bbkit/BBUtil";
import { TableMetadata } from "@/types/proto/database";
@@ -64,6 +69,12 @@ const props = withDefaults(
);
const { idOfTable, rectOfTable } = useSchemaDiagramContext();
+const tooltipStyle = `
+ max-width: 20rem;
+ white-space: pre-wrap;
+ word-break: break-all
+`;
+
const COLOR_LIST = [
"#64748B",
"#EF4444",
diff --git a/frontend/src/components/SchemaDiagram/SchemaDiagram.vue b/frontend/src/components/SchemaDiagram/SchemaDiagram.vue
index 388d148bee..5a2f746827 100644
--- a/frontend/src/components/SchemaDiagram/SchemaDiagram.vue
+++ b/frontend/src/components/SchemaDiagram/SchemaDiagram.vue
@@ -97,6 +97,11 @@ provideSchemaDiagramContext({
events,
});
-// autoLayout at the first time the diagram is mounted.
-onMounted(layout);
+// autoLayout and fit view at the first time the diagram is mounted.
+onMounted(() => {
+ layout();
+ nextTick(() => {
+ events.emit("fit-view");
+ });
+});
</script>
diff --git a/frontend/src/components/SchemaDiagram/common/geometry.ts b/frontend/src/components/SchemaDiagram/common/geometry.ts
new file mode 100644
index 0000000000..7f95e3d1b4
--- /dev/null
+++ b/frontend/src/components/SchemaDiagram/common/geometry.ts
@@ -0,0 +1,27 @@
+import { Position, Rect } from "../types";
+
+export const pointsOfRect = (rect: Rect): Position[] => {
+ const { x, y, width, height } = rect;
+ const nw = { x, y };
+ const ne = { x: x + width, y };
+ const sw = { x, y: y + height };
+ const se = { x: x + width, y: y + height };
+ return [nw, ne, sw, se];
+};
+
+export const calcBBox = (points: Position[]): Rect => {
+ const min: Position = { x: Number.MAX_VALUE, y: Number.MAX_VALUE };
+ const max: Position = { x: Number.MIN_VALUE, y: Number.MIN_VALUE };
+ points.forEach(({ x, y }) => {
+ if (x > max.x) max.x = x;
+ if (y > max.y) max.y = y;
+ if (x < min.x) min.x = x;
+ if (y < min.y) min.y = y;
+ });
+ return {
+ x: min.x,
+ y: min.y,
+ width: max.x - min.x,
+ height: max.y - min.y,
+ };
+};
diff --git a/frontend/src/components/SchemaDiagram/common/index.ts b/frontend/src/components/SchemaDiagram/common/index.ts
index fc4b498ff2..852c71aac2 100644
--- a/frontend/src/components/SchemaDiagram/common/index.ts
+++ b/frontend/src/components/SchemaDiagram/common/index.ts
@@ -1,4 +1,5 @@
export * from "./context";
export * from "./useDraggable";
export * from "./utils";
+export * from "./geometry";
export * from "./schema";
diff --git a/frontend/src/components/SchemaDiagram/types/context.ts b/frontend/src/components/SchemaDiagram/types/context.ts
index b30ee5868d..3df5f65dd1 100644
--- a/frontend/src/components/SchemaDiagram/types/context.ts
+++ b/frontend/src/components/SchemaDiagram/types/context.ts
@@ -27,5 +27,6 @@ export type SchemaDiagramContext = {
events: Emittery<{
render: undefined;
layout: undefined;
+ "fit-view": undefined;
}>;
};
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index 0eb57e771a..a703a55df2 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -1738,6 +1738,7 @@
}
},
"schema-diagram": {
- "self": "Schema Diagram"
+ "self": "Schema Diagram",
+ "fit-content-with-view": "Fit content within view"
}
}
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index a9c16af5ac..332063a3f1 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -1739,6 +1739,7 @@
}
},
"schema-diagram": {
- "self": "Schema 展示图"
+ "self": "Schema 展示图",
+ "fit-content-with-view": "自动适应视图"
}
}