Skip to content

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

refactor: extract abstract geometry types (#4329)

Liu Ji1abfb18ab4bc
diff --git a/frontend/src/components/SchemaDiagram/Canvas/Canvas.vue b/frontend/src/components/SchemaDiagram/Canvas/Canvas.vue
index 38f852bdab..6aa7a40ddc 100644
--- a/frontend/src/components/SchemaDiagram/Canvas/Canvas.vue
+++ b/frontend/src/components/SchemaDiagram/Canvas/Canvas.vue
@@ -54,7 +54,7 @@ import { fitView } from "./libs/fitView";
const canvas = ref<Element>();
const desktop = ref<Element>();
-const { tableList, rectOfTable, zoom, position, panning, events } =
+const { zoom, position, panning, geometries, events } =
useSchemaDiagramContext();
const zoomCenter = ref<Point>({ x: 0.5, y: 0.5 });
@@ -65,10 +65,9 @@ const ZOOM_RANGE = {
const handleFitView = () => {
if (!canvas.value) return;
- const rects = tableList.value.map(rectOfTable);
const layout = fitView(
canvas.value,
- rects,
+ [...geometries.value],
[10, 20, 40, 20] /* paddings [T,R,B,L] */,
[ZOOM_RANGE.min, 1.0] /* [zoomMin, zoomMax] */
);
diff --git a/frontend/src/components/SchemaDiagram/Canvas/libs/fitView.ts b/frontend/src/components/SchemaDiagram/Canvas/libs/fitView.ts
index d1301163f7..ff0c60fe71 100644
--- a/frontend/src/components/SchemaDiagram/Canvas/libs/fitView.ts
+++ b/frontend/src/components/SchemaDiagram/Canvas/libs/fitView.ts
@@ -1,13 +1,13 @@
-import { calcBBox, minmax, pointsOfRect } from "../../common";
-import { Rect } from "../../types";
+import { calcBBox, minmax } from "../../common";
+import { Geometry, Rect } from "../../types";
export const fitView = (
canvas: Element,
- rects: Rect[],
+ geometries: Geometry[],
paddings: number[] = [0, 0, 0, 0], // [T,R,B,L]
zoomRange: number[] = [0.5, 2]
) => {
- const contentBBox = calcBBox(rects.flatMap(pointsOfRect));
+ const contentBBox = calcBBox(geometries);
if (
contentBBox.width < Number.EPSILON ||
contentBBox.height < Number.EPSILON
diff --git a/frontend/src/components/SchemaDiagram/ER/ForeignKeyLine.vue b/frontend/src/components/SchemaDiagram/ER/ForeignKeyLine.vue
index 88f71fdb54..38bfd277ce 100644
--- a/frontend/src/components/SchemaDiagram/ER/ForeignKeyLine.vue
+++ b/frontend/src/components/SchemaDiagram/ER/ForeignKeyLine.vue
@@ -19,6 +19,7 @@ import { Point, Rect, Path, ForeignKey } from "../types";
import {
segmentOverlap1D,
SegmentOverlap1D,
+ useGeometry,
useSchemaDiagramContext,
} from "../common";
@@ -149,4 +150,6 @@ const updatePath = () => {
events.on("render", () => void updatePath());
onMounted(updatePath);
+
+useGeometry(path);
</script>
diff --git a/frontend/src/components/SchemaDiagram/ER/TableNode.vue b/frontend/src/components/SchemaDiagram/ER/TableNode.vue
index 05635dd9ec..87a3fdf340 100644
--- a/frontend/src/components/SchemaDiagram/ER/TableNode.vue
+++ b/frontend/src/components/SchemaDiagram/ER/TableNode.vue
@@ -5,8 +5,8 @@
:bb-node-id="idOfTable(table)"
:bb-status="tableStatus(table)"
:style="{
- left: `${position.x}px`,
- top: `${position.y}px`,
+ left: `${rect.x}px`,
+ top: `${rect.y}px`,
}"
>
<h3
@@ -85,7 +85,12 @@ import {
SchemaMetadata,
TableMetadata,
} from "@/types/proto/store/database";
-import { useSchemaDiagramContext, isPrimaryKey, isIndex } from "../common";
+import {
+ useSchemaDiagramContext,
+ isPrimaryKey,
+ isIndex,
+ useGeometry,
+} from "../common";
import { VueClass } from "@/utils";
const props = withDefaults(
@@ -184,5 +189,7 @@ const tableStatusText = computed(() => {
return "";
});
-const position = computed(() => rectOfTable(props.table));
+const rect = computed(() => rectOfTable(props.table));
+
+useGeometry(rect);
</script>
diff --git a/frontend/src/components/SchemaDiagram/SchemaDiagram.vue b/frontend/src/components/SchemaDiagram/SchemaDiagram.vue
index efcb34a331..7b24ac417d 100644
--- a/frontend/src/components/SchemaDiagram/SchemaDiagram.vue
+++ b/frontend/src/components/SchemaDiagram/SchemaDiagram.vue
@@ -44,6 +44,7 @@ import {
SchemaDiagramContext,
ForeignKey,
EditStatus,
+ Geometry,
} from "./types";
import Canvas from "./Canvas";
import { TableNode, autoLayout, GraphNodeItem, GraphEdgeItem } from "./ER";
@@ -87,6 +88,7 @@ const initialized = ref(false);
const zoom = ref(1);
const position = ref<Point>({ x: 0, y: 0 });
const panning = ref(false);
+const geometries = ref(new Set<Geometry>());
const render = () => {
nextTick(() => {
@@ -205,6 +207,7 @@ provideSchemaDiagramContext({
zoom,
position,
panning,
+ geometries,
idOfTable,
rectOfTable,
render,
diff --git a/frontend/src/components/SchemaDiagram/common/context.ts b/frontend/src/components/SchemaDiagram/common/context.ts
index 35bd0cac98..2122bcc8a9 100644
--- a/frontend/src/components/SchemaDiagram/common/context.ts
+++ b/frontend/src/components/SchemaDiagram/common/context.ts
@@ -1,5 +1,6 @@
-import { inject, InjectionKey, provide } from "vue";
-import { SchemaDiagramContext } from "../types";
+import { inject, InjectionKey, provide, unref, watchEffect } from "vue";
+import type { MaybeRef } from "@/types";
+import type { Geometry, SchemaDiagramContext } from "../types";
export const KEY = Symbol(
"bb.schema-diagram"
@@ -12,3 +13,14 @@ export const useSchemaDiagramContext = () => {
export const provideSchemaDiagramContext = (context: SchemaDiagramContext) => {
provide(KEY, context);
};
+
+export const useGeometry = (geometry: MaybeRef<Geometry>) => {
+ const context = useSchemaDiagramContext();
+ watchEffect((onCleanup) => {
+ const g = unref(geometry);
+ context.geometries.value.add(g);
+ onCleanup(() => {
+ context.geometries.value.delete(g);
+ });
+ });
+};
diff --git a/frontend/src/components/SchemaDiagram/common/geometry.ts b/frontend/src/components/SchemaDiagram/common/geometry.ts
index 551135f68b..0f9ee159f8 100644
--- a/frontend/src/components/SchemaDiagram/common/geometry.ts
+++ b/frontend/src/components/SchemaDiagram/common/geometry.ts
@@ -1,4 +1,4 @@
-import { Point, Rect } from "../types";
+import { Geometry, Path, Point, Rect } from "../types";
export const pointsOfRect = (rect: Rect): Point[] => {
const { x, y, width, height } = rect;
@@ -9,7 +9,9 @@ export const pointsOfRect = (rect: Rect): Point[] => {
return [nw, ne, sw, se];
};
-export const calcBBox = (points: Point[]): Rect => {
+export const calcBBox = (geometries: Geometry[]): Rect => {
+ const points = geometries.flatMap(pointsOfGeometry);
+
const min: Point = { x: Number.MAX_VALUE, y: Number.MAX_VALUE };
const max: Point = { x: Number.MIN_VALUE, y: Number.MIN_VALUE };
points.forEach(({ x, y }) => {
@@ -62,3 +64,38 @@ export const segmentOverlap1D = (
throw new Error(`should never be here a=${a}, b=${b}, c=${c}, d=${d}`);
};
+export const isPoint = (geometry: Geometry): geometry is Point => {
+ if (Array.isArray(geometry)) {
+ return false;
+ }
+ return (
+ typeof geometry["x"] === "number" &&
+ typeof geometry["y"] === "number" &&
+ typeof (geometry as Rect)["width"] === "undefined" &&
+ typeof (geometry as Rect)["height"] === "undefined"
+ );
+};
+
+export const isPath = (geometry: Geometry): geometry is Path => {
+ return Array.isArray(geometry);
+};
+
+export const isRect = (geometry: Geometry): geometry is Rect => {
+ if (Array.isArray(geometry)) {
+ return false;
+ }
+ return (
+ typeof geometry["x"] === "number" &&
+ typeof geometry["y"] === "number" &&
+ typeof (geometry as Rect)["width"] === "number" &&
+ typeof (geometry as Rect)["height"] === "number"
+ );
+};
+
+export const pointsOfGeometry = (g: Geometry): Point[] => {
+ if (isPoint(g)) return [g];
+ if (isPath(g)) return g;
+ if (isRect(g)) return pointsOfRect(g);
+
+ throw new Error(`'${String(g)}' is not a geometry.`);
+};
diff --git a/frontend/src/components/SchemaDiagram/types/context.ts b/frontend/src/components/SchemaDiagram/types/context.ts
index b725158acc..6480900342 100644
--- a/frontend/src/components/SchemaDiagram/types/context.ts
+++ b/frontend/src/components/SchemaDiagram/types/context.ts
@@ -6,7 +6,7 @@ import {
SchemaMetadata,
TableMetadata,
} from "@/types/proto/store/database";
-import { Point, Rect } from "./geometry";
+import { Geometry, Point, Rect } from "./geometry";
import { EditStatus } from "./edit";
// This is an abstract Schema Diagram context including states, methods and
@@ -21,6 +21,7 @@ export type SchemaDiagramContext = {
zoom: Ref<number>;
position: Ref<Point>;
panning: Ref<boolean>;
+ geometries: Ref<Set<Geometry>>;
// Methods
idOfTable: (table: TableMetadata) => string;
diff --git a/frontend/src/components/SchemaDiagram/types/geometry.ts b/frontend/src/components/SchemaDiagram/types/geometry.ts
index d028c027cb..f9fa8e2a3e 100644
--- a/frontend/src/components/SchemaDiagram/types/geometry.ts
+++ b/frontend/src/components/SchemaDiagram/types/geometry.ts
@@ -11,3 +11,5 @@ export type Point = {
export type Rect = Point & Size;
export type Path = Point[];
+
+export type Geometry = Point | Path | Rect;