dbml-erd-viewer 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,17 +1,17 @@
1
1
  import { forwardRef as e, memo as t, useCallback as n, useEffect as r, useImperativeHandle as i, useMemo as a, useRef as o, useState as s } from "react";
2
2
  import { Background as c, ControlButton as l, Controls as u, Handle as d, MiniMap as f, Position as p, ReactFlow as ee, getNodesBounds as te, getSmoothStepPath as m, useEdgesState as ne, useNodesState as re } from "@xyflow/react";
3
- import * as ie from "@dbml/core";
4
- import { Fragment as h, jsx as g, jsxs as _ } from "react/jsx-runtime";
3
+ import * as h from "@dbml/core";
4
+ import { Fragment as g, jsx as _, jsxs as v } from "react/jsx-runtime";
5
5
  //#region src/parser/parseDbml.ts
6
- var v = ie, y = v.Parser ?? v.default?.Parser;
7
- if (!y) throw Error("Could not resolve `Parser` export from @dbml/core.");
8
- var b = y, x = "public", S = class extends Error {
6
+ var y = h, b = y.Parser ?? y.default?.Parser;
7
+ if (!b) throw Error("Could not resolve `Parser` export from @dbml/core.");
8
+ var x = b, S = "public", C = class extends Error {
9
9
  diagnostics;
10
10
  constructor(e, t) {
11
11
  super(e, t), this.name = "DbmlParseError", this.diagnostics = t?.diagnostics ?? [];
12
12
  }
13
13
  };
14
- function C(e) {
14
+ function w(e) {
15
15
  let t = e?.diags;
16
16
  return Array.isArray(t) ? t.map((e) => ({
17
17
  message: e.message ?? "Unknown error",
@@ -20,43 +20,43 @@ function C(e) {
20
20
  code: e.code
21
21
  })) : [];
22
22
  }
23
- function w(e, t) {
23
+ function T(e, t) {
24
24
  if (e.length === 0) return `Failed to parse DBML: ${t}`;
25
25
  let n = e.map((e) => ` • ${e.line == null ? "" : `Line ${e.line}${e.column == null ? "" : `:${e.column}`} — `}${e.message}`);
26
26
  return `${e.length === 1 ? "Failed to parse DBML:" : `Failed to parse DBML (${e.length} errors):`}\n${n.join("\n")}`;
27
27
  }
28
- function T(e) {
28
+ function E(e) {
29
29
  if (e) return typeof e == "string" ? e : e.value ?? void 0;
30
30
  }
31
- function E(e, t) {
32
- return e && e !== x ? `${e}.${t}` : t;
31
+ function D(e, t) {
32
+ return e && e !== S ? `${e}.${t}` : t;
33
33
  }
34
- function D(e) {
34
+ function O(e) {
35
35
  let t = /* @__PURE__ */ new Set();
36
36
  for (let n of e.fields) n.pk && t.add(n.name);
37
37
  for (let n of e.indexes ?? []) if (n.pk) for (let e of n.columns ?? []) e.type === "column" && e.value && t.add(e.value);
38
38
  return t;
39
39
  }
40
- function O(e) {
40
+ function ie(e) {
41
41
  let t;
42
42
  try {
43
- t = new b().parse(e, "dbmlv2");
43
+ t = new x().parse(e, "dbmlv2");
44
44
  } catch (e) {
45
- let t = C(e);
46
- throw new S(w(t, e instanceof Error ? e.message : String(e)), {
45
+ let t = w(e);
46
+ throw new C(T(t, e instanceof Error ? e.message : String(e)), {
47
47
  cause: e,
48
48
  diagnostics: t
49
49
  });
50
50
  }
51
51
  let n = /* @__PURE__ */ new Map();
52
52
  for (let e of t.schemas) for (let t of e.tables) {
53
- let r = E(e.name, t.name), i = D(t), a = /* @__PURE__ */ new Map();
53
+ let r = D(e.name, t.name), i = O(t), a = /* @__PURE__ */ new Map();
54
54
  for (let e of t.fields) {
55
55
  let t = i.has(e.name);
56
56
  a.set(e.name, {
57
57
  pk: t,
58
58
  notNull: t || !!e.not_null,
59
- note: T(e.note)
59
+ note: E(e.note)
60
60
  });
61
61
  }
62
62
  n.set(r, a);
@@ -71,7 +71,7 @@ function O(e) {
71
71
  for (let e of t.schemas) for (let t of e.refs) {
72
72
  if (t.endpoints.length !== 2) continue;
73
73
  let [n, c] = t.endpoints.map((t) => ({
74
- tableId: E(t.schemaName ?? e.name, t.tableName),
74
+ tableId: D(t.schemaName ?? e.name, t.tableName),
75
75
  columns: t.fieldNames,
76
76
  relation: t.relation
77
77
  })), l, u, d = n.relation === "*";
@@ -98,7 +98,7 @@ function O(e) {
98
98
  for (let e of t.schemas) {
99
99
  let t = e.name;
100
100
  for (let n of e.tables) {
101
- let e = E(t, n.name), r = D(n), i = n.fields.map((t) => ({
101
+ let e = D(t, n.name), r = O(n), i = n.fields.map((t) => ({
102
102
  name: t.name,
103
103
  type: t.type?.type_name ?? "unknown",
104
104
  pk: r.has(t.name),
@@ -106,14 +106,14 @@ function O(e) {
106
106
  unique: !!t.unique,
107
107
  increment: !!t.increment,
108
108
  defaultValue: t.dbdefault?.value,
109
- note: T(t.note),
109
+ note: E(t.note),
110
110
  isForeignKey: s.has(`${e}::${t.name}`)
111
111
  }));
112
112
  c.push({
113
113
  id: e,
114
114
  name: n.name,
115
- schema: t === x ? void 0 : t,
116
- note: T(n.note),
115
+ schema: t === S ? void 0 : t,
116
+ note: E(n.note),
117
117
  headerColor: n.headerColor ?? void 0,
118
118
  columns: i
119
119
  });
@@ -126,11 +126,23 @@ function O(e) {
126
126
  }
127
127
  //#endregion
128
128
  //#region src/layout/layout.ts
129
- var k = 240, A = 40, j = 28;
130
- function M(e) {
129
+ var k = 160, ae = 320, A = 320, j = 40, M = 28;
130
+ function N(e) {
131
131
  return 40 + Math.max(e.columns.length, 1) * 28;
132
132
  }
133
- function N(e) {
133
+ var P = 24, F = 8, I = 24, L = 12, oe = 24, R = 6.8, z = 7.4;
134
+ function B(e, t = {}) {
135
+ let n = t.minWidth ?? 160, r = t.maxWidth ?? 320, i = oe + V(e) * z;
136
+ for (let t of e.columns) {
137
+ let e = (t.pk || t.isForeignKey ? I : 0) + t.name.length * R, n = t.type.length * R + L;
138
+ i = Math.max(i, P + e + F + n);
139
+ }
140
+ return Math.round(Math.min(r, Math.max(n, i)));
141
+ }
142
+ function V(e) {
143
+ return (e.schema ? e.schema.length + 1 : 0) + e.name.length;
144
+ }
145
+ function H(e) {
134
146
  let t = /* @__PURE__ */ new Map();
135
147
  for (let n of e.tables) t.set(n.id, 0);
136
148
  let n = e.relations.map((e) => [e.from.tableId, e.to.tableId]).filter(([e, n]) => e !== n && t.has(e) && t.has(n));
@@ -144,57 +156,68 @@ function N(e) {
144
156
  }
145
157
  return t;
146
158
  }
147
- function P(e, t = {}) {
148
- let n = t.horizontalGap ?? 120, r = t.verticalGap ?? 40, i = N(e), a = /* @__PURE__ */ new Map();
159
+ function U(e, t = {}) {
160
+ let n = t.horizontalGap ?? 120, r = t.verticalGap ?? 40, i = {
161
+ minWidth: t.minNodeWidth,
162
+ maxWidth: t.maxNodeWidth
163
+ }, a = H(e), o = /* @__PURE__ */ new Map();
149
164
  for (let t of e.tables) {
150
- let e = i.get(t.id) ?? 0, n = a.get(e);
151
- n || (n = [], a.set(e, n)), n.push(t);
165
+ let e = a.get(t.id) ?? 0, n = o.get(e);
166
+ n || (n = [], o.set(e, n)), n.push(t);
152
167
  }
153
- let o = /* @__PURE__ */ new Map();
154
- return [...a.keys()].sort((e, t) => e - t).forEach((e, t) => {
155
- let i = a.get(e).sort((e, t) => e.name.localeCompare(t.name)), s = t * (240 + n), c = 0;
156
- for (let e of i) {
157
- let t = M(e);
158
- o.set(e.id, {
159
- x: s,
168
+ let s = /* @__PURE__ */ new Map(), c = [...o.keys()].sort((e, t) => e - t), l = 0;
169
+ for (let e of c) {
170
+ let t = o.get(e).sort((e, t) => e.name.localeCompare(t.name)), a = 0, c = 0;
171
+ for (let e of t) {
172
+ let t = B(e, i), n = N(e);
173
+ s.set(e.id, {
174
+ x: l,
160
175
  y: c,
161
- width: 240,
162
- height: t
163
- }), c += t + r;
176
+ width: t,
177
+ height: n
178
+ }), a = Math.max(a, t), c += n + r;
164
179
  }
165
- }), o;
180
+ l += a + n;
181
+ }
182
+ return s;
166
183
  }
167
184
  //#endregion
168
185
  //#region src/layout/dagre.ts
169
- async function F(e, t = {}) {
170
- let n = await import("@dagrejs/dagre"), r = t.horizontalGap ?? 120, i = t.verticalGap ?? 40, a = t.direction === "TB" ? "TB" : "LR", o = new n.graphlib.Graph();
171
- o.setGraph({
172
- rankdir: a,
186
+ async function W(e, t = {}) {
187
+ let n = await import("@dagrejs/dagre"), r = t.horizontalGap ?? 120, i = t.verticalGap ?? 40, a = {
188
+ minWidth: t.minNodeWidth,
189
+ maxWidth: t.maxNodeWidth
190
+ }, o = t.direction === "TB" ? "TB" : "LR", s = new n.graphlib.Graph();
191
+ s.setGraph({
192
+ rankdir: o,
173
193
  ranksep: r,
174
194
  nodesep: i
175
- }), o.setDefaultEdgeLabel(() => ({}));
176
- for (let t of e.tables) o.setNode(t.id, {
177
- width: 240,
178
- height: M(t)
195
+ }), s.setDefaultEdgeLabel(() => ({}));
196
+ for (let t of e.tables) s.setNode(t.id, {
197
+ width: B(t, a),
198
+ height: N(t)
179
199
  });
180
- for (let t of e.relations) o.hasNode(t.from.tableId) && o.hasNode(t.to.tableId) && o.setEdge(t.from.tableId, t.to.tableId);
181
- n.layout(o);
182
- let s = /* @__PURE__ */ new Map();
183
- for (let e of o.nodes()) {
184
- let t = o.node(e);
185
- t && s.set(e, {
200
+ for (let t of e.relations) s.hasNode(t.from.tableId) && s.hasNode(t.to.tableId) && s.setEdge(t.from.tableId, t.to.tableId);
201
+ n.layout(s);
202
+ let c = /* @__PURE__ */ new Map();
203
+ for (let e of s.nodes()) {
204
+ let t = s.node(e);
205
+ t && c.set(e, {
186
206
  x: t.x - t.width / 2,
187
207
  y: t.y - t.height / 2,
188
208
  width: t.width,
189
209
  height: t.height
190
210
  });
191
211
  }
192
- return s;
212
+ return c;
193
213
  }
194
214
  //#endregion
195
215
  //#region src/layout/elk.ts
196
- async function I(e, t = {}) {
216
+ async function G(e, t = {}) {
197
217
  let n = new (await (import("elkjs/lib/elk.bundled.js"))).default(), r = t.horizontalGap ?? 120, i = t.verticalGap ?? 40, a = {
218
+ minWidth: t.minNodeWidth,
219
+ maxWidth: t.maxNodeWidth
220
+ }, o = {
198
221
  id: "root",
199
222
  layoutOptions: {
200
223
  "elk.algorithm": "layered",
@@ -204,42 +227,42 @@ async function I(e, t = {}) {
204
227
  },
205
228
  children: e.tables.map((e) => ({
206
229
  id: e.id,
207
- width: 240,
208
- height: M(e)
230
+ width: B(e, a),
231
+ height: N(e)
209
232
  })),
210
233
  edges: e.relations.map((e, t) => ({
211
234
  id: `e${t}`,
212
235
  sources: [e.from.tableId],
213
236
  targets: [e.to.tableId]
214
237
  }))
215
- }, o = await n.layout(a), s = /* @__PURE__ */ new Map();
216
- for (let e of o.children ?? []) s.set(e.id, {
238
+ }, s = await n.layout(o), c = /* @__PURE__ */ new Map();
239
+ for (let e of s.children ?? []) c.set(e.id, {
217
240
  x: e.x ?? 0,
218
241
  y: e.y ?? 0,
219
- width: e.width ?? 240,
242
+ width: e.width ?? 160,
220
243
  height: e.height ?? 0
221
244
  });
222
- return s;
245
+ return c;
223
246
  }
224
247
  //#endregion
225
248
  //#region src/layout/computeLayout.ts
226
- var L = class extends Error {
249
+ var K = class extends Error {
227
250
  constructor(e, t) {
228
251
  super(e, t), this.name = "LayoutError";
229
252
  }
230
253
  };
231
- async function R(e, t = {}) {
254
+ async function q(e, t = {}) {
232
255
  let n = t.algorithm ?? "simple";
233
- if (n === "simple") return P(e, t);
256
+ if (n === "simple") return U(e, t);
234
257
  try {
235
- return n === "dagre" ? await F(e, t) : await I(e, t);
258
+ return n === "dagre" ? await W(e, t) : await G(e, t);
236
259
  } catch (e) {
237
- throw new L(`"${n}" layout failed. Is the optional dependency "${n === "dagre" ? "@dagrejs/dagre" : "elkjs"}" installed? (${e instanceof Error ? e.message : String(e)})`, { cause: e });
260
+ throw new K(`"${n}" layout failed. Is the optional dependency "${n === "dagre" ? "@dagrejs/dagre" : "elkjs"}" installed? (${e instanceof Error ? e.message : String(e)})`, { cause: e });
238
261
  }
239
262
  }
240
263
  //#endregion
241
264
  //#region src/export.ts
242
- var ae = [
265
+ var J = [
243
266
  "--dv-canvas",
244
267
  "--dv-bg",
245
268
  "--dv-border",
@@ -255,12 +278,12 @@ var ae = [
255
278
  "--dv-edge",
256
279
  "--dv-edge-active",
257
280
  "--dv-font"
258
- ], z = class extends Error {
281
+ ], Y = class extends Error {
259
282
  constructor(e, t) {
260
283
  super(e, t), this.name = "ExportError";
261
284
  }
262
285
  };
263
- function oe(e, t) {
286
+ function se(e, t) {
264
287
  let n = e.x, r = e.y, i = e.x + e.width, a = e.y + e.height;
265
288
  for (let e of t.querySelectorAll(".dv-erd-edge")) {
266
289
  let t;
@@ -278,12 +301,12 @@ function oe(e, t) {
278
301
  height: a - r
279
302
  };
280
303
  }
281
- async function B(e, t, n = {}) {
304
+ async function ce(e, t, n = {}) {
282
305
  let { type: r = "png", padding: i = 24, backgroundColor: a, pixelRatio: o = 2 } = n, s;
283
306
  try {
284
307
  s = await import("html-to-image");
285
308
  } catch (e) {
286
- throw new z("Export requires the optional dependency \"html-to-image\". Install it to enable PNG/SVG export.", { cause: e });
309
+ throw new Y("Export requires the optional dependency \"html-to-image\". Install it to enable PNG/SVG export.", { cause: e });
287
310
  }
288
311
  if (typeof document < "u" && document.fonts?.ready) try {
289
312
  await document.fonts.ready;
@@ -297,23 +320,23 @@ async function B(e, t, n = {}) {
297
320
  height: `${l}px`,
298
321
  transform: u
299
322
  }
300
- }, f = V(e, t, i);
323
+ }, f = le(e, t, i);
301
324
  try {
302
325
  return r === "svg" ? await s.toSvg(e, d) : await s.toPng(e, {
303
326
  ...d,
304
327
  pixelRatio: o
305
328
  });
306
329
  } catch (e) {
307
- throw new z(`Failed to render diagram image: ${e instanceof Error ? e.message : String(e)}`, { cause: e });
330
+ throw new Y(`Failed to render diagram image: ${e instanceof Error ? e.message : String(e)}`, { cause: e });
308
331
  } finally {
309
332
  f();
310
333
  }
311
334
  }
312
- function V(e, t, n) {
335
+ function le(e, t, n) {
313
336
  let r = [], i = e.closest(".dv-viewer");
314
337
  if (i) {
315
338
  let t = getComputedStyle(i);
316
- for (let n of ae) {
339
+ for (let n of J) {
317
340
  let i = t.getPropertyValue(n);
318
341
  if (!i) continue;
319
342
  let a = e.style.getPropertyValue(n);
@@ -331,49 +354,49 @@ function V(e, t, n) {
331
354
  }
332
355
  return () => r.forEach((e) => e());
333
356
  }
334
- function H(e, t) {
357
+ function ue(e, t) {
335
358
  let n = document.createElement("a");
336
359
  n.href = e, n.download = t, n.click();
337
360
  }
338
361
  //#endregion
339
362
  //#region src/components/handles.ts
340
- function U(e, t, n) {
363
+ function X(e, t, n) {
341
364
  return `${e}__${t}__${n}`;
342
365
  }
343
366
  //#endregion
344
367
  //#region src/components/TableNode.tsx
345
- function W(e) {
368
+ function de(e) {
346
369
  return 40 + e * 28 + 28 / 2;
347
370
  }
348
- function G({ column: e, top: t }) {
371
+ function fe({ column: e, top: t }) {
349
372
  let n = {
350
373
  top: t,
351
374
  opacity: 0
352
375
  };
353
- return /* @__PURE__ */ _(h, { children: [
354
- /* @__PURE__ */ g(d, {
355
- id: U(e, "left", "target"),
376
+ return /* @__PURE__ */ v(g, { children: [
377
+ /* @__PURE__ */ _(d, {
378
+ id: X(e, "left", "target"),
356
379
  type: "target",
357
380
  position: p.Left,
358
381
  style: n,
359
382
  isConnectable: !1
360
383
  }),
361
- /* @__PURE__ */ g(d, {
362
- id: U(e, "left", "source"),
384
+ /* @__PURE__ */ _(d, {
385
+ id: X(e, "left", "source"),
363
386
  type: "source",
364
387
  position: p.Left,
365
388
  style: n,
366
389
  isConnectable: !1
367
390
  }),
368
- /* @__PURE__ */ g(d, {
369
- id: U(e, "right", "target"),
391
+ /* @__PURE__ */ _(d, {
392
+ id: X(e, "right", "target"),
370
393
  type: "target",
371
394
  position: p.Right,
372
395
  style: n,
373
396
  isConnectable: !1
374
397
  }),
375
- /* @__PURE__ */ g(d, {
376
- id: U(e, "right", "source"),
398
+ /* @__PURE__ */ _(d, {
399
+ id: X(e, "right", "source"),
377
400
  type: "source",
378
401
  position: p.Right,
379
402
  style: n,
@@ -381,60 +404,69 @@ function G({ column: e, top: t }) {
381
404
  })
382
405
  ] });
383
406
  }
384
- function K({ data: e }) {
385
- let { table: t, highlightedColumns: n } = e;
386
- return /* @__PURE__ */ _("div", {
407
+ function Z({ data: e }) {
408
+ let { table: t, highlightedColumns: n, widthBounds: r } = e;
409
+ return /* @__PURE__ */ v("div", {
387
410
  className: "dv-table",
388
- style: { width: 240 },
389
- children: [/* @__PURE__ */ _("div", {
411
+ style: { width: B(t, r) },
412
+ children: [/* @__PURE__ */ v("div", {
390
413
  className: "dv-table__header",
391
414
  style: t.headerColor ? {
392
415
  background: t.headerColor,
393
416
  height: 40
394
417
  } : { height: 40 },
395
418
  title: t.note,
396
- children: [t.schema && /* @__PURE__ */ _("span", {
419
+ children: [t.schema && /* @__PURE__ */ v("span", {
397
420
  className: "dv-table__schema",
398
421
  children: [t.schema, "."]
399
- }), /* @__PURE__ */ g("span", {
422
+ }), /* @__PURE__ */ _("span", {
400
423
  className: "dv-table__name",
424
+ title: t.name,
401
425
  children: t.name
402
426
  })]
403
- }), /* @__PURE__ */ g("div", {
427
+ }), /* @__PURE__ */ _("div", {
404
428
  className: "dv-table__body",
405
429
  children: t.columns.map((e, t) => {
406
430
  let r = n?.has(e.name);
407
- return /* @__PURE__ */ _("div", {
431
+ return /* @__PURE__ */ v("div", {
408
432
  className: `dv-row${r ? " dv-row--highlighted" : ""}`,
409
433
  style: { height: 28 },
410
434
  title: e.note,
411
435
  children: [
412
- /* @__PURE__ */ _("span", {
436
+ /* @__PURE__ */ v("span", {
413
437
  className: `dv-row__name${e.pk ? " dv-row__name--pk" : ""}`,
414
438
  children: [
415
- e.pk && /* @__PURE__ */ g("span", {
439
+ e.pk && /* @__PURE__ */ _("span", {
416
440
  className: "dv-badge dv-badge--pk",
417
441
  title: "Primary key",
418
442
  children: "PK"
419
443
  }),
420
- e.isForeignKey && !e.pk && /* @__PURE__ */ g("span", {
444
+ e.isForeignKey && !e.pk && /* @__PURE__ */ _("span", {
421
445
  className: "dv-badge dv-badge--fk",
422
446
  title: "Foreign key",
423
447
  children: "FK"
424
448
  }),
425
- e.name
449
+ /* @__PURE__ */ _("span", {
450
+ className: "dv-row__label",
451
+ title: e.name,
452
+ children: e.name
453
+ })
426
454
  ]
427
455
  }),
428
- /* @__PURE__ */ _("span", {
456
+ /* @__PURE__ */ v("span", {
429
457
  className: "dv-row__type",
430
- children: [e.type, /* @__PURE__ */ g("span", {
458
+ children: [/* @__PURE__ */ _("span", {
459
+ className: "dv-row__typename",
460
+ title: e.type,
461
+ children: e.type
462
+ }), /* @__PURE__ */ _("span", {
431
463
  className: `dv-row__null${e.notNull ? " dv-row__null--notnull" : ""}`,
432
464
  title: e.notNull ? "Not null" : "Nullable"
433
465
  })]
434
466
  }),
435
- /* @__PURE__ */ g(G, {
467
+ /* @__PURE__ */ _(fe, {
436
468
  column: e.name,
437
- top: W(t)
469
+ top: de(t)
438
470
  })
439
471
  ]
440
472
  }, e.name);
@@ -442,18 +474,18 @@ function K({ data: e }) {
442
474
  })]
443
475
  });
444
476
  }
445
- var q = t(K), J = 8, Y = 13, X = 7, se = 13, ce = 16;
446
- function Z(e, t, n, r, i, a) {
477
+ var Q = t(Z), $ = 8, pe = 13, me = 7, he = 13, ge = 16;
478
+ function _e(e, t, n, r, i, a) {
447
479
  let o = -(n === p.Left ? 1 : -1), s = [], c = {
448
480
  stroke: i,
449
481
  strokeWidth: a
450
- }, l = (n, r) => s.push(/* @__PURE__ */ g("line", {
482
+ }, l = (n, r) => s.push(/* @__PURE__ */ _("line", {
451
483
  x1: e + o * n,
452
- y1: t - J,
484
+ y1: t - $,
453
485
  x2: e + o * n,
454
- y2: t + J,
486
+ y2: t + $,
455
487
  style: c
456
- }, r)), u = (n, r) => s.push(/* @__PURE__ */ g("circle", {
488
+ }, r)), u = (n, r) => s.push(/* @__PURE__ */ _("circle", {
457
489
  cx: e + o * n,
458
490
  cy: t,
459
491
  r: 4,
@@ -464,33 +496,33 @@ function Z(e, t, n, r, i, a) {
464
496
  }
465
497
  }, r));
466
498
  if (r.cardinality === "*") {
467
- let n = e + o * Y;
468
- s.push(/* @__PURE__ */ g("line", {
499
+ let n = e + o * pe;
500
+ s.push(/* @__PURE__ */ _("line", {
469
501
  x1: n,
470
502
  y1: t,
471
503
  x2: e,
472
- y2: t - J,
504
+ y2: t - $,
473
505
  style: c
474
- }, "cf1"), /* @__PURE__ */ g("line", {
506
+ }, "cf1"), /* @__PURE__ */ _("line", {
475
507
  x1: n,
476
508
  y1: t,
477
509
  x2: e,
478
510
  y2: t,
479
511
  style: c
480
- }, "cf2"), /* @__PURE__ */ g("line", {
512
+ }, "cf2"), /* @__PURE__ */ _("line", {
481
513
  x1: n,
482
514
  y1: t,
483
515
  x2: e,
484
- y2: t + J,
516
+ y2: t + $,
485
517
  style: c
486
518
  }, "cf3")), r.optional === !0 ? u(17, "fr") : l(14, "fb");
487
- } else l(X, "b1"), r.optional === !0 ? u(ce, "r") : r.optional === !1 && l(se, "b2");
488
- return /* @__PURE__ */ g("g", {
519
+ } else l(me, "b1"), r.optional === !0 ? u(ge, "r") : r.optional === !1 && l(he, "b2");
520
+ return /* @__PURE__ */ _("g", {
489
521
  className: "dv-erd-marker",
490
522
  children: s
491
523
  });
492
524
  }
493
- function le({ sourceX: e, sourceY: t, targetX: n, targetY: r, sourcePosition: i, targetPosition: a, data: o, selected: s }) {
525
+ function ve({ sourceX: e, sourceY: t, targetX: n, targetY: r, sourcePosition: i, targetPosition: a, data: o, selected: s }) {
494
526
  let [c] = m({
495
527
  sourceX: e,
496
528
  sourceY: t,
@@ -500,15 +532,15 @@ function le({ sourceX: e, sourceY: t, targetX: n, targetY: r, sourcePosition: i,
500
532
  targetPosition: a,
501
533
  borderRadius: 8
502
534
  }), l = o, u = l?.kind === "non-identifying", d = s || l?.hovered, f = d ? "var(--dv-edge-active)" : "var(--dv-edge)", p = d ? 2 : 1.5;
503
- return /* @__PURE__ */ _("g", {
535
+ return /* @__PURE__ */ v("g", {
504
536
  className: `dv-erd-edge${d ? " dv-erd-edge--active" : ""}`,
505
537
  children: [
506
- /* @__PURE__ */ g("path", {
538
+ /* @__PURE__ */ _("path", {
507
539
  className: "dv-erd-edge__interaction",
508
540
  d: c,
509
541
  fill: "none"
510
542
  }),
511
- /* @__PURE__ */ g("path", {
543
+ /* @__PURE__ */ _("path", {
512
544
  className: "dv-erd-edge__path",
513
545
  d: c,
514
546
  fill: "none",
@@ -518,15 +550,15 @@ function le({ sourceX: e, sourceY: t, targetX: n, targetY: r, sourcePosition: i,
518
550
  },
519
551
  strokeDasharray: u ? "6 4" : void 0
520
552
  }),
521
- l && Z(e, t, i, l.sourceEnd, f, p),
522
- l && Z(n, r, a, l.targetEnd, f, p)
553
+ l && _e(e, t, i, l.sourceEnd, f, p),
554
+ l && _e(n, r, a, l.targetEnd, f, p)
523
555
  ]
524
556
  });
525
557
  }
526
- var Q = t(le);
558
+ var ye = t(ve);
527
559
  //#endregion
528
560
  //#region src/components/useRelationHighlight.ts
529
- function ue(e, t, r) {
561
+ function be(e, t, r) {
530
562
  let [i, o] = s(null), c = a(() => {
531
563
  let t = /* @__PURE__ */ new Map();
532
564
  for (let n of e) t.set(n.id, n);
@@ -569,7 +601,7 @@ function ue(e, t, r) {
569
601
  }
570
602
  //#endregion
571
603
  //#region src/theme.ts
572
- var de = {
604
+ var xe = {
573
605
  canvas: "--dv-canvas",
574
606
  background: "--dv-bg",
575
607
  border: "--dv-border",
@@ -584,7 +616,7 @@ var de = {
584
616
  edge: "--dv-edge",
585
617
  edgeActive: "--dv-edge-active",
586
618
  fontFamily: "--dv-font"
587
- }, fe = {
619
+ }, Se = {
588
620
  canvas: "#fafbfc",
589
621
  background: "#ffffff",
590
622
  border: "#d9dee5",
@@ -598,7 +630,7 @@ var de = {
598
630
  foreignKey: "#2b6cb0",
599
631
  edge: "#94a3b8",
600
632
  edgeActive: "#2b6cb0"
601
- }, pe = {
633
+ }, Ce = {
602
634
  canvas: "#0f172a",
603
635
  background: "#1e293b",
604
636
  border: "#334155",
@@ -613,48 +645,48 @@ var de = {
613
645
  edge: "#64748b",
614
646
  edgeActive: "#60a5fa"
615
647
  };
616
- function me(e) {
648
+ function we(e) {
617
649
  if (!e) return {};
618
650
  let t = {};
619
651
  for (let n of Object.keys(e)) {
620
652
  let r = e[n];
621
- r != null && (t[de[n]] = r);
653
+ r != null && (t[xe[n]] = r);
622
654
  }
623
655
  return t;
624
656
  }
625
657
  //#endregion
626
658
  //#region src/components/DbmlViewer.tsx
627
- var he = [], ge = { table: q }, _e = { erd: Q };
628
- function ve() {
629
- return /* @__PURE__ */ _("svg", {
659
+ var Te = [], Ee = { table: Q }, De = { erd: ye };
660
+ function Oe() {
661
+ return /* @__PURE__ */ v("svg", {
630
662
  viewBox: "0 0 24 24",
631
663
  width: "100%",
632
664
  height: "100%",
633
665
  fill: "currentColor",
634
666
  "aria-hidden": "true",
635
667
  children: [
636
- /* @__PURE__ */ g("rect", {
668
+ /* @__PURE__ */ _("rect", {
637
669
  x: "3",
638
670
  y: "3",
639
671
  width: "8",
640
672
  height: "8",
641
673
  rx: "1.5"
642
674
  }),
643
- /* @__PURE__ */ g("rect", {
675
+ /* @__PURE__ */ _("rect", {
644
676
  x: "13",
645
677
  y: "3",
646
678
  width: "8",
647
679
  height: "8",
648
680
  rx: "1.5"
649
681
  }),
650
- /* @__PURE__ */ g("rect", {
682
+ /* @__PURE__ */ _("rect", {
651
683
  x: "3",
652
684
  y: "13",
653
685
  width: "8",
654
686
  height: "8",
655
687
  rx: "1.5"
656
688
  }),
657
- /* @__PURE__ */ g("rect", {
689
+ /* @__PURE__ */ _("rect", {
658
690
  x: "13",
659
691
  y: "13",
660
692
  width: "8",
@@ -664,7 +696,7 @@ function ve() {
664
696
  ]
665
697
  });
666
698
  }
667
- function ye(e, t, n, r) {
699
+ function ke(e, t, n, r) {
668
700
  let i = /* @__PURE__ */ new Map();
669
701
  for (let a of e.tables) {
670
702
  let e = t.get(a.id);
@@ -675,19 +707,45 @@ function ye(e, t, n, r) {
675
707
  }
676
708
  return i;
677
709
  }
678
- function be(e, t) {
710
+ function Ae(e, t) {
679
711
  if (e.length !== t.length) return !1;
680
712
  let n = new Set(e);
681
713
  return t.every((e) => n.has(e));
682
714
  }
683
- async function $(e, t, n) {
715
+ function je(e, t, n) {
716
+ return e.x < t.x + t.w + n && e.x + e.w + n > t.x && e.y < t.y + t.h + n && e.y + e.h + n > t.y;
717
+ }
718
+ function Me(e, t, n, r, i) {
719
+ let a = (e, t) => ({
720
+ x: t.x,
721
+ y: t.y,
722
+ w: B(e, i),
723
+ h: N(e)
724
+ }), o = [];
725
+ for (let r of e.tables) n(r.id) && o.push(a(r, t.get(r.id)));
726
+ for (let i of e.tables) {
727
+ if (n(i.id)) continue;
728
+ let e = t.get(i.id), s = a(i, e), c = 0;
729
+ for (; c++ < 1e3;) {
730
+ let t = o.filter((e) => je(s, e, r));
731
+ if (t.length === 0) break;
732
+ let n = Math.max(...t.map((e) => e.y + e.h));
733
+ e = {
734
+ x: e.x,
735
+ y: n + r
736
+ }, s = a(i, e);
737
+ }
738
+ t.set(i.id, e), o.push(s);
739
+ }
740
+ }
741
+ async function Ne(e, t, n) {
684
742
  try {
685
- return await R(e, t);
743
+ return await q(e, t);
686
744
  } catch (r) {
687
- return n(r instanceof L ? r : new L(String(r), { cause: r })), P(e, t);
745
+ return n(r instanceof K ? r : new K(String(r), { cause: r })), U(e, t);
688
746
  }
689
747
  }
690
- function xe(e, t) {
748
+ function Pe(e, t, n) {
691
749
  return e.tables.map((e) => ({
692
750
  id: e.id,
693
751
  type: "table",
@@ -695,18 +753,21 @@ function xe(e, t) {
695
753
  x: 0,
696
754
  y: 0
697
755
  },
698
- data: { table: e }
756
+ data: {
757
+ table: e,
758
+ widthBounds: n
759
+ }
699
760
  }));
700
761
  }
701
- function Se(e, t) {
762
+ function Fe(e, t) {
702
763
  return e.relations.map((e) => {
703
764
  let n = t.get(e.from.tableId), r = t.get(e.to.tableId), i = (n?.x ?? 0) > (r?.x ?? 0), a = i ? "left" : "right", o = i ? "right" : "left", s = e.from.columns[0] ?? "", c = e.to.columns[0] ?? "";
704
765
  return {
705
766
  id: e.id,
706
767
  source: e.from.tableId,
707
768
  target: e.to.tableId,
708
- sourceHandle: U(s, a, "source"),
709
- targetHandle: U(c, o, "target"),
769
+ sourceHandle: X(s, a, "source"),
770
+ targetHandle: X(c, o, "target"),
710
771
  type: "erd",
711
772
  data: {
712
773
  kind: e.kind,
@@ -722,26 +783,26 @@ function Se(e, t) {
722
783
  };
723
784
  });
724
785
  }
725
- var Ce = e(function({ dbml: e, className: t, style: s, theme: d, fitView: p = !0, showControls: m = !0, showMiniMap: ie = !1, showBackground: h = !0, layoutOptions: v, onParseError: y, onLayoutError: b, nodePositions: x, onNodePositionsChange: C }, w) {
726
- let T = a(() => {
786
+ var Ie = e(function({ dbml: e, className: t, style: s, theme: d, fitView: p = !0, showControls: m = !0, showMiniMap: h = !1, showBackground: g = !0, layoutOptions: y, onParseError: b, onLayoutError: x, nodePositions: S, onNodePositionsChange: w }, T) {
787
+ let E = a(() => {
727
788
  try {
728
789
  return {
729
790
  ok: !0,
730
- schema: O(e)
791
+ schema: ie(e)
731
792
  };
732
793
  } catch (e) {
733
794
  return {
734
795
  ok: !1,
735
- error: e instanceof S ? e : new S(String(e), { cause: e })
796
+ error: e instanceof C ? e : new C(String(e), { cause: e })
736
797
  };
737
798
  }
738
- }, [e]), [E, D, k] = re([]), [A, j, M] = ne([]), N = o(null), P = o(null), F = o(E);
739
- F.current = E, i(w, () => {
799
+ }, [e]), [D, O, k] = re([]), [ae, A, j] = ne([]), M = o(null), N = o(null), P = o(D);
800
+ P.current = D, i(T, () => {
740
801
  let e = async (e) => {
741
- let t = N.current, n = P.current?.querySelector(".react-flow__viewport");
742
- if (!t || !n) throw new z("The diagram is not ready to export yet.");
743
- let r = oe(te(t.getNodes()), n), i = e?.backgroundColor ?? (P.current ? getComputedStyle(P.current).backgroundColor : void 0);
744
- return B(n, r, {
802
+ let t = M.current, n = N.current?.querySelector(".react-flow__viewport");
803
+ if (!t || !n) throw new Y("The diagram is not ready to export yet.");
804
+ let r = se(te(t.getNodes()), n), i = e?.backgroundColor ?? (N.current ? getComputedStyle(N.current).backgroundColor : void 0);
805
+ return ce(n, r, {
745
806
  ...e,
746
807
  backgroundColor: i
747
808
  });
@@ -749,60 +810,83 @@ var Ce = e(function({ dbml: e, className: t, style: s, theme: d, fitView: p = !0
749
810
  return {
750
811
  toDataUrl: e,
751
812
  download: async (t, n) => {
752
- H(await e(n), t);
753
- }
813
+ ue(await e(n), t);
814
+ },
815
+ fitView: (e) => M.current?.fitView(e),
816
+ zoomIn: (e) => M.current?.zoomIn(e),
817
+ zoomOut: (e) => M.current?.zoomOut(e),
818
+ zoomTo: (e, t) => M.current?.zoomTo(e, t),
819
+ setCenter: (e, t, n) => M.current?.setCenter(e, t, n),
820
+ fitBounds: (e, t) => M.current?.fitBounds(e, t),
821
+ setViewport: (e, t) => M.current?.setViewport(e, t),
822
+ getViewport: () => M.current?.getViewport()
754
823
  };
755
824
  }, []);
756
- let I = o(b);
757
- I.current = b;
758
- let L = o(C);
759
- L.current = C;
760
- let R = o(x);
761
- R.current = x;
762
- let ae = n(() => {
763
- let e = L.current;
825
+ let F = o(x);
826
+ F.current = x;
827
+ let I = o(w);
828
+ I.current = w;
829
+ let L = o(S);
830
+ L.current = S;
831
+ let oe = n(() => {
832
+ let e = I.current;
764
833
  if (!e) return;
765
834
  let t = {};
766
- for (let e of F.current) t[e.id] = {
835
+ for (let e of P.current) t[e.id] = {
767
836
  x: e.position.x,
768
837
  y: e.position.y
769
838
  };
770
839
  e(t);
771
- }, []), { algorithm: V = "simple", direction: U = "LR", horizontalGap: W, verticalGap: G } = v ?? {}, K = o(null), q = `${V}|${U}|${W}|${G}`, J = n((e, t, n, r, i) => {
772
- let a = ye(e, r, i.saved, i.prior);
773
- if (D(xe(e, a)), j(Se(e, a)), K.current = {
840
+ }, []), { algorithm: R = "simple", direction: z = "LR", horizontalGap: B, verticalGap: V, minNodeWidth: H, maxNodeWidth: U } = y ?? {}, W = o(null), G = `${R}|${z}|${B}|${V}|${H}|${U}`, K = n((e, t, n, r, i) => {
841
+ let a = {
842
+ minWidth: H,
843
+ maxWidth: U
844
+ }, o = ke(e, r, i.saved, i.prior);
845
+ if (i.prior) {
846
+ let t = i.prior, n = i.saved;
847
+ Me(e, o, (e) => t.has(e) || n?.[e] !== void 0, V ?? 40, a);
848
+ }
849
+ if (O(Pe(e, o, a)), A(Fe(e, o)), W.current = {
774
850
  ids: t,
775
851
  optionsKey: n
776
852
  }, i.emit) {
777
853
  let e = {};
778
- a.forEach((t, n) => {
854
+ o.forEach((t, n) => {
779
855
  e[n] = t;
780
- }), L.current?.(e);
856
+ }), I.current?.(e);
781
857
  }
782
- i.fit && requestAnimationFrame(() => void N.current?.fitView());
783
- }, [D, j]);
858
+ i.fit && requestAnimationFrame(() => void M.current?.fitView());
859
+ }, [
860
+ O,
861
+ A,
862
+ V,
863
+ H,
864
+ U
865
+ ]);
784
866
  r(() => {
785
- if (!T.ok) {
786
- D([]), j([]), K.current = null;
867
+ if (!E.ok) {
868
+ O([]), A([]), W.current = null;
787
869
  return;
788
870
  }
789
- let e = T.schema, t = {
790
- algorithm: V,
791
- direction: U,
792
- horizontalGap: W,
793
- verticalGap: G
794
- }, n = e.tables.map((e) => e.id), r = new Map(F.current.map((e) => [e.id, {
871
+ let e = E.schema, t = {
872
+ algorithm: R,
873
+ direction: z,
874
+ horizontalGap: B,
875
+ verticalGap: V,
876
+ minNodeWidth: H,
877
+ maxNodeWidth: U
878
+ }, n = e.tables.map((e) => e.id), r = new Map(P.current.map((e) => [e.id, {
795
879
  x: e.position.x,
796
880
  y: e.position.y
797
- }])), i = K.current, a = n.every((e) => r.has(e));
798
- if (i && i.optionsKey === q && a && be(i.ids, n)) {
799
- J(e, n, q, /* @__PURE__ */ new Map(), { prior: r });
881
+ }])), i = W.current, a = n.every((e) => r.has(e));
882
+ if (i && i.optionsKey === G && a && Ae(i.ids, n)) {
883
+ K(e, n, G, /* @__PURE__ */ new Map(), { prior: r });
800
884
  return;
801
885
  }
802
- let o = i && i.optionsKey === q ? r : void 0, s = !1;
803
- return $(e, t, (e) => I.current?.(e)).then((t) => {
804
- s || J(e, n, q, t, {
805
- saved: R.current,
886
+ let o = i && i.optionsKey === G ? r : void 0, s = !1;
887
+ return Ne(e, t, (e) => F.current?.(e)).then((t) => {
888
+ s || K(e, n, G, t, {
889
+ saved: L.current,
806
890
  prior: o,
807
891
  fit: p
808
892
  });
@@ -810,65 +894,71 @@ var Ce = e(function({ dbml: e, className: t, style: s, theme: d, fitView: p = !0
810
894
  s = !0;
811
895
  };
812
896
  }, [
813
- T,
897
+ E,
898
+ R,
899
+ z,
900
+ B,
814
901
  V,
902
+ H,
815
903
  U,
816
- W,
817
904
  G,
818
- q,
819
905
  p,
820
- J,
821
- D,
822
- j
906
+ K,
907
+ O,
908
+ A
823
909
  ]);
824
- let Y = n(() => {
825
- if (!T.ok) return;
826
- let e = T.schema, t = {
827
- algorithm: V,
828
- direction: U,
829
- horizontalGap: W,
830
- verticalGap: G
910
+ let q = n(() => {
911
+ if (!E.ok) return;
912
+ let e = E.schema, t = {
913
+ algorithm: R,
914
+ direction: z,
915
+ horizontalGap: B,
916
+ verticalGap: V,
917
+ minNodeWidth: H,
918
+ maxNodeWidth: U
831
919
  }, n = e.tables.map((e) => e.id);
832
- $(e, t, (e) => I.current?.(e)).then((t) => {
833
- J(e, n, q, t, {
920
+ Ne(e, t, (e) => F.current?.(e)).then((t) => {
921
+ K(e, n, G, t, {
834
922
  emit: !0,
835
923
  fit: !0
836
924
  });
837
925
  });
838
926
  }, [
839
- T,
927
+ E,
928
+ R,
929
+ z,
930
+ B,
840
931
  V,
932
+ H,
841
933
  U,
842
- W,
843
934
  G,
844
- q,
845
- J
846
- ]), X = n(() => {
847
- T.ok || y?.(T.error);
848
- }, [T, y]);
849
- r(X, [X]);
850
- let { displayNodes: se, displayEdges: ce, onEdgeMouseEnter: Z, onEdgeMouseLeave: le } = ue(T.ok ? T.schema.relations : he, E, A), Q = {
935
+ K
936
+ ]), J = n(() => {
937
+ E.ok || b?.(E.error);
938
+ }, [E, b]);
939
+ r(J, [J]);
940
+ let { displayNodes: le, displayEdges: X, onEdgeMouseEnter: de, onEdgeMouseLeave: fe } = be(E.ok ? E.schema.relations : Te, D, ae), Z = {
851
941
  width: "100%",
852
942
  height: "100%",
853
- ...me(d),
943
+ ...we(d),
854
944
  ...s
855
945
  };
856
- return T.ok ? /* @__PURE__ */ g("div", {
857
- ref: P,
946
+ return E.ok ? /* @__PURE__ */ _("div", {
947
+ ref: N,
858
948
  className: `dv-viewer${t ? ` ${t}` : ""}`,
859
- style: Q,
860
- children: /* @__PURE__ */ _(ee, {
861
- nodes: se,
862
- edges: ce,
863
- nodeTypes: ge,
864
- edgeTypes: _e,
949
+ style: Z,
950
+ children: /* @__PURE__ */ v(ee, {
951
+ nodes: le,
952
+ edges: X,
953
+ nodeTypes: Ee,
954
+ edgeTypes: De,
865
955
  onNodesChange: k,
866
- onEdgesChange: M,
867
- onEdgeMouseEnter: Z,
868
- onEdgeMouseLeave: le,
869
- onNodeDragStop: ae,
956
+ onEdgesChange: j,
957
+ onEdgeMouseEnter: de,
958
+ onEdgeMouseLeave: fe,
959
+ onNodeDragStop: oe,
870
960
  onInit: (e) => {
871
- N.current = e;
961
+ M.current = e;
872
962
  },
873
963
  fitView: p,
874
964
  nodesConnectable: !1,
@@ -876,27 +966,27 @@ var Ce = e(function({ dbml: e, className: t, style: s, theme: d, fitView: p = !0
876
966
  minZoom: .1,
877
967
  proOptions: { hideAttribution: !0 },
878
968
  children: [
879
- h && /* @__PURE__ */ g(c, {}),
880
- m && /* @__PURE__ */ g(u, { children: /* @__PURE__ */ g(l, {
881
- onClick: Y,
969
+ g && /* @__PURE__ */ _(c, {}),
970
+ m && /* @__PURE__ */ _(u, { children: /* @__PURE__ */ _(l, {
971
+ onClick: q,
882
972
  title: "Auto layout",
883
973
  "aria-label": "Auto layout",
884
- children: /* @__PURE__ */ g(ve, {})
974
+ children: /* @__PURE__ */ _(Oe, {})
885
975
  }) }),
886
- ie && /* @__PURE__ */ g(f, {
976
+ h && /* @__PURE__ */ _(f, {
887
977
  pannable: !0,
888
978
  zoomable: !0
889
979
  })
890
980
  ]
891
981
  })
892
- }) : /* @__PURE__ */ g("div", {
982
+ }) : /* @__PURE__ */ _("div", {
893
983
  className: `dv-error${t ? ` ${t}` : ""}`,
894
- style: Q,
895
- children: /* @__PURE__ */ g("pre", {
984
+ style: Z,
985
+ children: /* @__PURE__ */ _("pre", {
896
986
  className: "dv-error__message",
897
- children: T.error.message
987
+ children: E.error.message
898
988
  })
899
989
  });
900
990
  });
901
991
  //#endregion
902
- export { S as DbmlParseError, Ce as DbmlViewer, Q as ErdEdge, z as ExportError, A as HEADER_HEIGHT, L as LayoutError, k as NODE_WIDTH, j as ROW_HEIGHT, q as TableNode, R as computeLayout, pe as darkTheme, H as downloadDataUrl, P as layoutSchema, fe as lightTheme, O as parseDbml, B as renderDiagram, M as tableHeight, me as themeToCssVars };
992
+ export { C as DbmlParseError, Ie as DbmlViewer, ye as ErdEdge, Y as ExportError, j as HEADER_HEIGHT, K as LayoutError, ae as MAX_NODE_WIDTH, k as MIN_NODE_WIDTH, A as NODE_WIDTH, M as ROW_HEIGHT, Q as TableNode, q as computeLayout, Ce as darkTheme, ue as downloadDataUrl, U as layoutSchema, Se as lightTheme, ie as parseDbml, ce as renderDiagram, N as tableHeight, B as tableWidth, we as themeToCssVars };