Skip to content

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

feat(schema-diagram): export diagram screenshot to PNG (#4331)

Liu Jiace8f5814efd
diff --git a/frontend/index.html b/frontend/index.html
index dc8d145622..10702dbfcf 100644
--- a/frontend/index.html
+++ b/frontend/index.html
@@ -8,6 +8,12 @@
</head>
<body class="text-base">
+ <div
+ id="capture-container"
+ class="absolute -z-50 overflow-visible"
+ style="right: -99999px; bottom: -99999px"
+ ></div>
+
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
<script>
diff --git a/frontend/package.json b/frontend/package.json
index b238c19dcf..267dbfd7f9 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -34,6 +34,7 @@
"escape-string-regexp": "5.0.0",
"google-protobuf": "^3.21.2",
"highlight.js": "11.6.0",
+ "html-to-image": "^1.11.4",
"inter-ui": "3.19.3",
"js-yaml": "4.1.0",
"lodash-es": "4.17.21",
diff --git a/frontend/pnpm-lock.yaml b/frontend/pnpm-lock.yaml
index 0590961182..3a3f4e2caa 100644
--- a/frontend/pnpm-lock.yaml
+++ b/frontend/pnpm-lock.yaml
@@ -56,6 +56,7 @@ specifiers:
eslint-plugin-vue: 9.7.0
google-protobuf: ^3.21.2
highlight.js: 11.6.0
+ html-to-image: ^1.11.4
inter-ui: 3.19.3
jquery: 3.6.1
js-yaml: 4.1.0
@@ -127,6 +128,7 @@ dependencies:
escape-string-regexp: 5.0.0
google-protobuf: 3.21.2
highlight.js: 11.6.0
+ html-to-image: 1.11.4
inter-ui: 3.19.3
js-yaml: 4.1.0
lodash-es: 4.17.21
@@ -451,8 +453,8 @@ packages:
vue-i18n:
optional: true
dependencies:
- '@intlify/message-compiler': 9.3.0-beta.14
- '@intlify/shared': 9.3.0-beta.14
+ '@intlify/message-compiler': 9.3.0-beta.16
+ '@intlify/shared': 9.3.0-beta.16
jsonc-eslint-parser: 1.4.1
source-map: 0.6.1
@@ -481,11 +483,11 @@ packages:
'@intlify/shared': 9.2.2
source-map: 0.6.1
- /@intlify/message-compiler/9.3.0-beta.14:
- resolution: {integrity: sha512-PlZ3pl+YYuql54Nq+26wv6ohIa8Ig6ALrvQI+f2zZKUtkupb49M4wyVN3bDQbFlgYVE7/u3n19BJSY8lEuX5Eg==}
+ /@intlify/message-compiler/9.3.0-beta.16:
+ resolution: {integrity: sha512-CGQI3xRcs1ET75eDQ0DUy3MRYOqTauRIIgaMoISKiF83gqRWg93FqN8lGMKcpBqaF4tI0JhsfosCaGiBL9+dnw==}
engines: {node: '>= 14'}
dependencies:
- '@intlify/shared': 9.3.0-beta.14
+ '@intlify/shared': 9.3.0-beta.16
source-map: 0.6.1
dev: true
@@ -493,8 +495,8 @@ packages:
resolution: {integrity: sha512-wRwTpsslgZS5HNyM7uDQYZtxnbI12aGiBZURX3BTR9RFIKKRWpllTsgzHWvj3HKm3Y2Sh5LPC1r0PDCKEhVn9Q==}
engines: {node: '>= 14'}
- /@intlify/shared/9.3.0-beta.14:
- resolution: {integrity: sha512-mJ/rFan+4uVsBAQSCAJnpQaPvSjQ49mJMNmGelTUbTDAmgf0oexYxwqtKOlFFyY3hmQ8lUDYaGQKuYrFgRuHnA==}
+ /@intlify/shared/9.3.0-beta.16:
+ resolution: {integrity: sha512-kXbm4svALe3lX+EjdJxfnabOphqS4yQ1Ge/iIlR8tvUiYRCoNz3hig1M4336iY++Dfx5ytEQJPNjIcknNIuvig==}
engines: {node: '>= 14'}
dev: true
@@ -514,7 +516,7 @@ packages:
optional: true
dependencies:
'@intlify/bundle-utils': [email protected]
- '@intlify/shared': 9.3.0-beta.14
+ '@intlify/shared': 9.3.0-beta.16
'@rollup/pluginutils': 4.2.1
debug: 4.3.4
fast-glob: 3.2.12
@@ -2771,6 +2773,10 @@ packages:
nopt: 1.0.10
dev: false
+ /html-to-image/1.11.4:
+ resolution: {integrity: sha512-I6jeGpjSI/pea+p/8q6HAWPxptZkEYaZ40ZLnpCHuhCJJnUHQ/YzMEQMNY/KMdPZJ4+GJIwo9dnJy76eW9p9cg==}
+ dev: false
+
/human-signals/2.1.0:
resolution: {integrity: sha512-B4FFZ6q/T2jhhksgkbEW3HBvWIfDW85snkQgawt07S7J5QXTk6BkNV+0yAeZrM5QpMAdYlocGoljn0sJ/WQkFw==}
engines: {node: '>=10.17.0'}
diff --git a/frontend/src/components/SchemaDiagram/Canvas/Canvas.vue b/frontend/src/components/SchemaDiagram/Canvas/Canvas.vue
index 6aa7a40ddc..31525c1f1e 100644
--- a/frontend/src/components/SchemaDiagram/Canvas/Canvas.vue
+++ b/frontend/src/components/SchemaDiagram/Canvas/Canvas.vue
@@ -14,6 +14,17 @@
<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>
+ <heroicons-outline:photo @click="handleScreenshot" />
+ </NButton>
+ </template>
+ <div class="whitespace-nowrap">Screenshot</div>
+ </NTooltip>
+ </NButtonGroup>
+
<NButtonGroup size="tiny" class="bg-white rounded">
<NTooltip>
<template #trigger>
@@ -35,12 +46,14 @@
/>
</div>
+ <DummyCanvas ref="dummy" :render-desktop="renderDummy" />
+
<slot />
</div>
</template>
<script lang="ts" setup>
-import { ref } from "vue";
+import { ref, useSlots } from "vue";
import { useEventListener } from "@vueuse/core";
import normalizeWheel from "normalize-wheel";
import { NButtonGroup, NButton } from "naive-ui";
@@ -50,11 +63,16 @@ import { Point } from "../types";
import { useDraggable, minmax, useSchemaDiagramContext } from "../common";
import ZoomButton from "./ZoomButton.vue";
import { fitView } from "./libs/fitView";
+import DummyCanvas from "./DummyCanvas.vue";
+import { pushNotification } from "@/store";
+
+const slots = useSlots();
const canvas = ref<Element>();
const desktop = ref<Element>();
+const dummy = ref<InstanceType<typeof DummyCanvas>>();
-const { zoom, position, panning, geometries, events } =
+const { database, busy, zoom, position, panning, geometries, events } =
useSchemaDiagramContext();
const zoomCenter = ref<Point>({ x: 0.5, y: 0.5 });
@@ -133,4 +151,23 @@ useEventListener(
},
true
);
+
+const renderDummy = () => {
+ return slots["desktop"]?.();
+};
+
+const handleScreenshot = async () => {
+ busy.value = true;
+ try {
+ await dummy.value?.capture(`${database.value.name}.png`, "png");
+ } catch {
+ pushNotification({
+ module: "bytebase",
+ style: "CRITICAL",
+ title: "Screenshot request failed",
+ });
+ } finally {
+ busy.value = false;
+ }
+};
</script>
diff --git a/frontend/src/components/SchemaDiagram/Canvas/DummyCanvas.vue b/frontend/src/components/SchemaDiagram/Canvas/DummyCanvas.vue
new file mode 100644
index 0000000000..ce2bb69053
--- /dev/null
+++ b/frontend/src/components/SchemaDiagram/Canvas/DummyCanvas.vue
@@ -0,0 +1,119 @@
+<template>
+ <teleport to="#capture-container">
+ <div
+ v-if="isCapturing"
+ ref="canvas"
+ class="relative overflow-hidden bg-gray-200 pointer-events-none z-10"
+ :style="{
+ width: `${width}px`,
+ height: `${height}px`,
+ }"
+ >
+ <div
+ class="absolute overflow-visible"
+ :style="{
+ width: `${width}px`,
+ height: `${height}px`,
+ }"
+ >
+ <DesktopRenderer />
+ </div>
+
+ <img
+ class="absolute z-50 opacity-50"
+ :style="{
+ width: `${logoWidth}px`,
+ height: 'auto',
+ left: `${LOGO_PADDING}px`,
+ bottom: `${LOGO_PADDING}px`,
+ }"
+ src="../../../assets/logo-full.svg"
+ alt="Bytebase"
+ />
+ </div>
+ </teleport>
+</template>
+
+<script lang="ts" setup>
+import { computed, defineComponent, nextTick, PropType, ref, VNode } from "vue";
+
+import {
+ calcBBox,
+ minmax,
+ provideSchemaDiagramContext,
+ useSchemaDiagramContext,
+} from "../common";
+
+const props = defineProps({
+ renderDesktop: {
+ type: Function as PropType<() => VNode | VNode[] | undefined>,
+ required: true,
+ },
+});
+
+const LOGO_PADDING = 10;
+
+const canvas = ref<HTMLDivElement>();
+
+const isCapturing = ref(false);
+
+const context = useSchemaDiagramContext();
+const { geometries } = context;
+
+const desktopBBox = computed(() => {
+ return calcBBox([...geometries.value]);
+});
+
+// Automatically set the logo's width according to the canvas size.
+const logoWidth = computed(() => {
+ const MIN_WIDTH = 120;
+ const MAX_WIDTH = 480;
+
+ return minmax(desktopBBox.value.width / 8, MIN_WIDTH, MAX_WIDTH);
+});
+
+const width = computed(() => {
+ return Math.max(
+ desktopBBox.value.width + desktopBBox.value.x * 2,
+ logoWidth.value + LOGO_PADDING * 2
+ );
+});
+
+const height = computed(() => {
+ const WIDTH_HEIGHT_RATIO = 4; // an approximate value.
+ // Leave a bottom padding to avoid the logo overlapping diagram contents.
+ const paddingBottom = logoWidth.value / WIDTH_HEIGHT_RATIO + LOGO_PADDING;
+ return desktopBBox.value.height + desktopBBox.value.y * 2 + paddingBottom;
+});
+
+const DesktopRenderer = defineComponent({
+ render: () => {
+ return props.renderDesktop();
+ },
+});
+
+const capture = async (filename: string, format: "png" | "jpg") => {
+ isCapturing.value = true;
+
+ await nextTick();
+
+ const node = canvas.value;
+ if (!node) return;
+
+ const [{ toPng }, { default: download }] = await Promise.all([
+ import("html-to-image"),
+ import("downloadjs"),
+ ]);
+ const dataUrl = await toPng(node, {});
+ download(dataUrl, filename, `image/${format}`);
+
+ isCapturing.value = false;
+};
+
+provideSchemaDiagramContext({
+ ...context,
+ dummy: ref(true),
+});
+
+defineExpose({ capture });
+</script>
diff --git a/frontend/src/components/SchemaDiagram/ER/TableNode.vue b/frontend/src/components/SchemaDiagram/ER/TableNode.vue
index 87a3fdf340..9101827d99 100644
--- a/frontend/src/components/SchemaDiagram/ER/TableNode.vue
+++ b/frontend/src/components/SchemaDiagram/ER/TableNode.vue
@@ -2,7 +2,7 @@
<div
class="absolute overflow-hidden 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)"
+ :bb-node-id="dummy ? `dummy-${idOfTable(table)}` : idOfTable(table)"
:bb-status="tableStatus(table)"
:style="{
left: `${rect.x}px`,
@@ -39,7 +39,7 @@
<tr
v-for="(column, i) in table.columns"
:key="i"
- :bb-column-name="column.name"
+ :bb-column-name="dummy ? `dummy-${column.name}` : column.name"
:bb-status="columnStatus(column)"
:class="columnClasses(column)"
>
@@ -101,6 +101,7 @@ const props = withDefaults(
{}
);
const {
+ dummy,
editable,
idOfTable,
rectOfTable,
diff --git a/frontend/src/components/SchemaDiagram/SchemaDiagram.vue b/frontend/src/components/SchemaDiagram/SchemaDiagram.vue
index 7b24ac417d..c73c7dda9c 100644
--- a/frontend/src/components/SchemaDiagram/SchemaDiagram.vue
+++ b/frontend/src/components/SchemaDiagram/SchemaDiagram.vue
@@ -17,7 +17,7 @@
</template>
<div
- v-if="!initialized"
+ v-if="busy || !initialized"
class="absolute inset-0 bg-white/40 flex items-center justify-center"
>
<BBSpin />
@@ -26,7 +26,7 @@
</template>
<script lang="ts" setup>
-import { computed, nextTick, ref, watch } from "vue";
+import { computed, nextTick, ref, toRef, watch } from "vue";
import { uniqueId } from "lodash-es";
import Emittery from "emittery";
@@ -81,10 +81,9 @@ const emit = defineEmits<{
const schemaList = computed(() => {
return props.databaseMetadata.schemas;
});
-const tableList = computed(() => {
- return schemaList.value.flatMap((schema) => schema.tables);
-});
const initialized = ref(false);
+const dummy = ref(false);
+const busy = ref(false);
const zoom = ref(1);
const position = ref<Point>({ x: 0, y: 0 });
const panning = ref(false);
@@ -202,8 +201,11 @@ events.on("edit-column", ({ schema, table, column, target }) =>
);
provideSchemaDiagramContext({
- tableList: computed(() => tableList.value),
+ database: toRef(props, "database"),
+ databaseMetadata: toRef(props, "databaseMetadata"),
editable: computed(() => props.editable),
+ dummy,
+ busy,
zoom,
position,
panning,
diff --git a/frontend/src/components/SchemaDiagram/common/geometry.ts b/frontend/src/components/SchemaDiagram/common/geometry.ts
index 0f9ee159f8..39abca1776 100644
--- a/frontend/src/components/SchemaDiagram/common/geometry.ts
+++ b/frontend/src/components/SchemaDiagram/common/geometry.ts
@@ -11,6 +11,9 @@ export const pointsOfRect = (rect: Rect): Point[] => {
export const calcBBox = (geometries: Geometry[]): Rect => {
const points = geometries.flatMap(pointsOfGeometry);
+ if (points.length === 0) {
+ return { x: 0, y: 0, width: 0, height: 0 };
+ }
const min: Point = { x: Number.MAX_VALUE, y: Number.MAX_VALUE };
const max: Point = { x: Number.MIN_VALUE, y: Number.MIN_VALUE };
diff --git a/frontend/src/components/SchemaDiagram/types/context.ts b/frontend/src/components/SchemaDiagram/types/context.ts
index 6480900342..62ebf4420c 100644
--- a/frontend/src/components/SchemaDiagram/types/context.ts
+++ b/frontend/src/components/SchemaDiagram/types/context.ts
@@ -1,12 +1,14 @@
-import { Ref } from "vue";
+import type { Ref } from "vue";
import type Emittery from "emittery";
-import {
- ColumnMetadata,
+import type {
+ DatabaseMetadata,
SchemaMetadata,
TableMetadata,
+ ColumnMetadata,
} from "@/types/proto/store/database";
-import { Geometry, Point, Rect } from "./geometry";
+import type { Database } from "@/types";
+import type { Geometry, Point, Rect } from "./geometry";
import { EditStatus } from "./edit";
// This is an abstract Schema Diagram context including states, methods and
@@ -14,10 +16,13 @@ import { EditStatus } from "./edit";
// and used in its descendants.
export type SchemaDiagramContext = {
// Props
- tableList: Ref<TableMetadata[]>;
+ database: Ref<Database>;
+ databaseMetadata: Ref<DatabaseMetadata>;
editable: Ref<boolean>;
// States
+ dummy: Ref<boolean>;
+ busy: Ref<boolean>;
zoom: Ref<number>;
position: Ref<Point>;
panning: Ref<boolean>;