dbml-erd-viewer 0.1.0

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.
@@ -0,0 +1,902 @@
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
+ 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";
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 {
9
+ diagnostics;
10
+ constructor(e, t) {
11
+ super(e, t), this.name = "DbmlParseError", this.diagnostics = t?.diagnostics ?? [];
12
+ }
13
+ };
14
+ function C(e) {
15
+ let t = e?.diags;
16
+ return Array.isArray(t) ? t.map((e) => ({
17
+ message: e.message ?? "Unknown error",
18
+ line: e.location?.start?.line,
19
+ column: e.location?.start?.column,
20
+ code: e.code
21
+ })) : [];
22
+ }
23
+ function w(e, t) {
24
+ if (e.length === 0) return `Failed to parse DBML: ${t}`;
25
+ let n = e.map((e) => ` • ${e.line == null ? "" : `Line ${e.line}${e.column == null ? "" : `:${e.column}`} — `}${e.message}`);
26
+ return `${e.length === 1 ? "Failed to parse DBML:" : `Failed to parse DBML (${e.length} errors):`}\n${n.join("\n")}`;
27
+ }
28
+ function T(e) {
29
+ if (e) return typeof e == "string" ? e : e.value ?? void 0;
30
+ }
31
+ function E(e, t) {
32
+ return e && e !== x ? `${e}.${t}` : t;
33
+ }
34
+ function D(e) {
35
+ let t = /* @__PURE__ */ new Set();
36
+ for (let n of e.fields) n.pk && t.add(n.name);
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
+ return t;
39
+ }
40
+ function O(e) {
41
+ let t;
42
+ try {
43
+ t = new b().parse(e, "dbmlv2");
44
+ } catch (e) {
45
+ let t = C(e);
46
+ throw new S(w(t, e instanceof Error ? e.message : String(e)), {
47
+ cause: e,
48
+ diagnostics: t
49
+ });
50
+ }
51
+ let n = /* @__PURE__ */ new Map();
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();
54
+ for (let e of t.fields) {
55
+ let t = i.has(e.name);
56
+ a.set(e.name, {
57
+ pk: t,
58
+ notNull: t || !!e.not_null,
59
+ note: T(e.note)
60
+ });
61
+ }
62
+ n.set(r, a);
63
+ }
64
+ let r = (e) => {
65
+ let t = n.get(e.tableId);
66
+ return !t || e.columns.length === 0 ? !1 : e.columns.every((e) => t.get(e)?.pk === !0);
67
+ }, i = (e) => e.columns.some((t) => {
68
+ let r = n.get(e.tableId)?.get(t);
69
+ return r ? !r.notNull && !r.pk : !0;
70
+ }), a = (e) => e.columns.some((t) => n.get(e.tableId)?.get(t)?.note?.toLowerCase().includes("optional") ?? !1), o = [], s = /* @__PURE__ */ new Set();
71
+ for (let e of t.schemas) for (let t of e.refs) {
72
+ if (t.endpoints.length !== 2) continue;
73
+ let [n, c] = t.endpoints.map((t) => ({
74
+ tableId: E(t.schemaName ?? e.name, t.tableName),
75
+ columns: t.fieldNames,
76
+ relation: t.relation
77
+ })), l, u, d = n.relation === "*";
78
+ if (d !== (c.relation === "*")) l = d ? n : c, u = d ? c : n;
79
+ else {
80
+ let e = r(n), t = r(c);
81
+ e && !t ? (l = c, u = n) : (l = n, u = c);
82
+ }
83
+ let f = r(l), p = i(l);
84
+ l.columns.forEach((e) => s.add(`${l.tableId}::${e}`)), o.push({
85
+ id: `${l.tableId}.${l.columns.join("-")}__${u.tableId}.${u.columns.join("-")}`,
86
+ kind: f ? "identifying" : "non-identifying",
87
+ from: {
88
+ ...l,
89
+ optional: a(l) ? !0 : void 0
90
+ },
91
+ to: {
92
+ ...u,
93
+ optional: p
94
+ }
95
+ });
96
+ }
97
+ let c = [];
98
+ for (let e of t.schemas) {
99
+ let t = e.name;
100
+ for (let n of e.tables) {
101
+ let e = E(t, n.name), r = D(n), i = n.fields.map((t) => ({
102
+ name: t.name,
103
+ type: t.type?.type_name ?? "unknown",
104
+ pk: r.has(t.name),
105
+ notNull: r.has(t.name) || !!t.not_null,
106
+ unique: !!t.unique,
107
+ increment: !!t.increment,
108
+ defaultValue: t.dbdefault?.value,
109
+ note: T(t.note),
110
+ isForeignKey: s.has(`${e}::${t.name}`)
111
+ }));
112
+ c.push({
113
+ id: e,
114
+ name: n.name,
115
+ schema: t === x ? void 0 : t,
116
+ note: T(n.note),
117
+ headerColor: n.headerColor ?? void 0,
118
+ columns: i
119
+ });
120
+ }
121
+ }
122
+ return {
123
+ tables: c,
124
+ relations: o
125
+ };
126
+ }
127
+ //#endregion
128
+ //#region src/layout/layout.ts
129
+ var k = 240, A = 40, j = 28;
130
+ function M(e) {
131
+ return 40 + Math.max(e.columns.length, 1) * 28;
132
+ }
133
+ function N(e) {
134
+ let t = /* @__PURE__ */ new Map();
135
+ for (let n of e.tables) t.set(n.id, 0);
136
+ let n = e.relations.map((e) => [e.from.tableId, e.to.tableId]).filter(([e, n]) => e !== n && t.has(e) && t.has(n));
137
+ for (let r = 0; r < e.tables.length; r++) {
138
+ let e = !1;
139
+ for (let [r, i] of n) {
140
+ let n = t.get(r) + 1;
141
+ t.get(i) < n && (t.set(i, n), e = !0);
142
+ }
143
+ if (!e) break;
144
+ }
145
+ return t;
146
+ }
147
+ function P(e, t = {}) {
148
+ let n = t.horizontalGap ?? 120, r = t.verticalGap ?? 40, i = N(e), a = /* @__PURE__ */ new Map();
149
+ 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);
152
+ }
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,
160
+ y: c,
161
+ width: 240,
162
+ height: t
163
+ }), c += t + r;
164
+ }
165
+ }), o;
166
+ }
167
+ //#endregion
168
+ //#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,
173
+ ranksep: r,
174
+ nodesep: i
175
+ }), o.setDefaultEdgeLabel(() => ({}));
176
+ for (let t of e.tables) o.setNode(t.id, {
177
+ width: 240,
178
+ height: M(t)
179
+ });
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, {
186
+ x: t.x - t.width / 2,
187
+ y: t.y - t.height / 2,
188
+ width: t.width,
189
+ height: t.height
190
+ });
191
+ }
192
+ return s;
193
+ }
194
+ //#endregion
195
+ //#region src/layout/elk.ts
196
+ async function I(e, t = {}) {
197
+ let n = new (await (import("elkjs/lib/elk.bundled.js"))).default(), r = t.horizontalGap ?? 120, i = t.verticalGap ?? 40, a = {
198
+ id: "root",
199
+ layoutOptions: {
200
+ "elk.algorithm": "layered",
201
+ "elk.direction": t.direction === "TB" ? "DOWN" : "RIGHT",
202
+ "elk.layered.spacing.nodeNodeBetweenLayers": String(r),
203
+ "elk.spacing.nodeNode": String(i)
204
+ },
205
+ children: e.tables.map((e) => ({
206
+ id: e.id,
207
+ width: 240,
208
+ height: M(e)
209
+ })),
210
+ edges: e.relations.map((e, t) => ({
211
+ id: `e${t}`,
212
+ sources: [e.from.tableId],
213
+ targets: [e.to.tableId]
214
+ }))
215
+ }, o = await n.layout(a), s = /* @__PURE__ */ new Map();
216
+ for (let e of o.children ?? []) s.set(e.id, {
217
+ x: e.x ?? 0,
218
+ y: e.y ?? 0,
219
+ width: e.width ?? 240,
220
+ height: e.height ?? 0
221
+ });
222
+ return s;
223
+ }
224
+ //#endregion
225
+ //#region src/layout/computeLayout.ts
226
+ var L = class extends Error {
227
+ constructor(e, t) {
228
+ super(e, t), this.name = "LayoutError";
229
+ }
230
+ };
231
+ async function R(e, t = {}) {
232
+ let n = t.algorithm ?? "simple";
233
+ if (n === "simple") return P(e, t);
234
+ try {
235
+ return n === "dagre" ? await F(e, t) : await I(e, t);
236
+ } 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 });
238
+ }
239
+ }
240
+ //#endregion
241
+ //#region src/export.ts
242
+ var ae = [
243
+ "--dv-canvas",
244
+ "--dv-bg",
245
+ "--dv-border",
246
+ "--dv-header-bg",
247
+ "--dv-header-fg",
248
+ "--dv-row-fg",
249
+ "--dv-type-fg",
250
+ "--dv-row-hover",
251
+ "--dv-row-highlight",
252
+ "--dv-pk",
253
+ "--dv-fk",
254
+ "--dv-null",
255
+ "--dv-edge",
256
+ "--dv-edge-active",
257
+ "--dv-font"
258
+ ], z = class extends Error {
259
+ constructor(e, t) {
260
+ super(e, t), this.name = "ExportError";
261
+ }
262
+ };
263
+ function oe(e, t) {
264
+ let n = e.x, r = e.y, i = e.x + e.width, a = e.y + e.height;
265
+ for (let e of t.querySelectorAll(".dv-erd-edge")) {
266
+ let t;
267
+ try {
268
+ t = e.getBBox();
269
+ } catch {
270
+ continue;
271
+ }
272
+ t.width === 0 && t.height === 0 || (n = Math.min(n, t.x), r = Math.min(r, t.y), i = Math.max(i, t.x + t.width), a = Math.max(a, t.y + t.height));
273
+ }
274
+ return {
275
+ x: n,
276
+ y: r,
277
+ width: i - n,
278
+ height: a - r
279
+ };
280
+ }
281
+ async function B(e, t, n = {}) {
282
+ let { type: r = "png", padding: i = 24, backgroundColor: a, pixelRatio: o = 2 } = n, s;
283
+ try {
284
+ s = await import("html-to-image");
285
+ } catch (e) {
286
+ throw new z("Export requires the optional dependency \"html-to-image\". Install it to enable PNG/SVG export.", { cause: e });
287
+ }
288
+ if (typeof document < "u" && document.fonts?.ready) try {
289
+ await document.fonts.ready;
290
+ } catch {}
291
+ let c = Math.ceil(t.width + i * 2), l = Math.ceil(t.height + i * 2), u = `translate(${-t.x + i}px, ${-t.y + i}px) scale(1)`, d = {
292
+ width: c,
293
+ height: l,
294
+ backgroundColor: a,
295
+ style: {
296
+ width: `${c}px`,
297
+ height: `${l}px`,
298
+ transform: u
299
+ }
300
+ }, f = V(e, t, i);
301
+ try {
302
+ return r === "svg" ? await s.toSvg(e, d) : await s.toPng(e, {
303
+ ...d,
304
+ pixelRatio: o
305
+ });
306
+ } catch (e) {
307
+ throw new z(`Failed to render diagram image: ${e instanceof Error ? e.message : String(e)}`, { cause: e });
308
+ } finally {
309
+ f();
310
+ }
311
+ }
312
+ function V(e, t, n) {
313
+ let r = [], i = e.closest(".dv-viewer");
314
+ if (i) {
315
+ let t = getComputedStyle(i);
316
+ for (let n of ae) {
317
+ let i = t.getPropertyValue(n);
318
+ if (!i) continue;
319
+ let a = e.style.getPropertyValue(n);
320
+ e.style.setProperty(n, i), r.push(() => {
321
+ a ? e.style.setProperty(n, a) : e.style.removeProperty(n);
322
+ });
323
+ }
324
+ }
325
+ let a = `${Math.ceil(t.x + t.width + n)}px`, o = `${Math.ceil(t.y + t.height + n)}px`;
326
+ for (let t of e.querySelectorAll(".react-flow__edges svg")) {
327
+ let { width: e, height: n } = t.style;
328
+ t.style.width = a, t.style.height = o, r.push(() => {
329
+ t.style.width = e, t.style.height = n;
330
+ });
331
+ }
332
+ return () => r.forEach((e) => e());
333
+ }
334
+ function H(e, t) {
335
+ let n = document.createElement("a");
336
+ n.href = e, n.download = t, n.click();
337
+ }
338
+ //#endregion
339
+ //#region src/components/handles.ts
340
+ function U(e, t, n) {
341
+ return `${e}__${t}__${n}`;
342
+ }
343
+ //#endregion
344
+ //#region src/components/TableNode.tsx
345
+ function W(e) {
346
+ return 40 + e * 28 + 28 / 2;
347
+ }
348
+ function G({ column: e, top: t }) {
349
+ let n = {
350
+ top: t,
351
+ opacity: 0
352
+ };
353
+ return /* @__PURE__ */ _(h, { children: [
354
+ /* @__PURE__ */ g(d, {
355
+ id: U(e, "left", "target"),
356
+ type: "target",
357
+ position: p.Left,
358
+ style: n,
359
+ isConnectable: !1
360
+ }),
361
+ /* @__PURE__ */ g(d, {
362
+ id: U(e, "left", "source"),
363
+ type: "source",
364
+ position: p.Left,
365
+ style: n,
366
+ isConnectable: !1
367
+ }),
368
+ /* @__PURE__ */ g(d, {
369
+ id: U(e, "right", "target"),
370
+ type: "target",
371
+ position: p.Right,
372
+ style: n,
373
+ isConnectable: !1
374
+ }),
375
+ /* @__PURE__ */ g(d, {
376
+ id: U(e, "right", "source"),
377
+ type: "source",
378
+ position: p.Right,
379
+ style: n,
380
+ isConnectable: !1
381
+ })
382
+ ] });
383
+ }
384
+ function K({ data: e }) {
385
+ let { table: t, highlightedColumns: n } = e;
386
+ return /* @__PURE__ */ _("div", {
387
+ className: "dv-table",
388
+ style: { width: 240 },
389
+ children: [/* @__PURE__ */ _("div", {
390
+ className: "dv-table__header",
391
+ style: t.headerColor ? {
392
+ background: t.headerColor,
393
+ height: 40
394
+ } : { height: 40 },
395
+ title: t.note,
396
+ children: [t.schema && /* @__PURE__ */ _("span", {
397
+ className: "dv-table__schema",
398
+ children: [t.schema, "."]
399
+ }), /* @__PURE__ */ g("span", {
400
+ className: "dv-table__name",
401
+ children: t.name
402
+ })]
403
+ }), /* @__PURE__ */ g("div", {
404
+ className: "dv-table__body",
405
+ children: t.columns.map((e, t) => {
406
+ let r = n?.has(e.name);
407
+ return /* @__PURE__ */ _("div", {
408
+ className: `dv-row${r ? " dv-row--highlighted" : ""}`,
409
+ style: { height: 28 },
410
+ title: e.note,
411
+ children: [
412
+ /* @__PURE__ */ _("span", {
413
+ className: `dv-row__name${e.pk ? " dv-row__name--pk" : ""}`,
414
+ children: [
415
+ e.pk && /* @__PURE__ */ g("span", {
416
+ className: "dv-badge dv-badge--pk",
417
+ title: "Primary key",
418
+ children: "PK"
419
+ }),
420
+ e.isForeignKey && !e.pk && /* @__PURE__ */ g("span", {
421
+ className: "dv-badge dv-badge--fk",
422
+ title: "Foreign key",
423
+ children: "FK"
424
+ }),
425
+ e.name
426
+ ]
427
+ }),
428
+ /* @__PURE__ */ _("span", {
429
+ className: "dv-row__type",
430
+ children: [e.type, /* @__PURE__ */ g("span", {
431
+ className: `dv-row__null${e.notNull ? " dv-row__null--notnull" : ""}`,
432
+ title: e.notNull ? "Not null" : "Nullable"
433
+ })]
434
+ }),
435
+ /* @__PURE__ */ g(G, {
436
+ column: e.name,
437
+ top: W(t)
438
+ })
439
+ ]
440
+ }, e.name);
441
+ })
442
+ })]
443
+ });
444
+ }
445
+ var q = t(K), J = 8, Y = 13, X = 7, se = 13, ce = 16;
446
+ function Z(e, t, n, r, i, a) {
447
+ let o = -(n === p.Left ? 1 : -1), s = [], c = {
448
+ stroke: i,
449
+ strokeWidth: a
450
+ }, l = (n, r) => s.push(/* @__PURE__ */ g("line", {
451
+ x1: e + o * n,
452
+ y1: t - J,
453
+ x2: e + o * n,
454
+ y2: t + J,
455
+ style: c
456
+ }, r)), u = (n, r) => s.push(/* @__PURE__ */ g("circle", {
457
+ cx: e + o * n,
458
+ cy: t,
459
+ r: 4,
460
+ style: {
461
+ stroke: i,
462
+ strokeWidth: a,
463
+ fill: "var(--dv-bg)"
464
+ }
465
+ }, r));
466
+ if (r.cardinality === "*") {
467
+ let n = e + o * Y;
468
+ s.push(/* @__PURE__ */ g("line", {
469
+ x1: n,
470
+ y1: t,
471
+ x2: e,
472
+ y2: t - J,
473
+ style: c
474
+ }, "cf1"), /* @__PURE__ */ g("line", {
475
+ x1: n,
476
+ y1: t,
477
+ x2: e,
478
+ y2: t,
479
+ style: c
480
+ }, "cf2"), /* @__PURE__ */ g("line", {
481
+ x1: n,
482
+ y1: t,
483
+ x2: e,
484
+ y2: t + J,
485
+ style: c
486
+ }, "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", {
489
+ className: "dv-erd-marker",
490
+ children: s
491
+ });
492
+ }
493
+ function le({ sourceX: e, sourceY: t, targetX: n, targetY: r, sourcePosition: i, targetPosition: a, data: o, selected: s }) {
494
+ let [c] = m({
495
+ sourceX: e,
496
+ sourceY: t,
497
+ sourcePosition: i,
498
+ targetX: n,
499
+ targetY: r,
500
+ targetPosition: a,
501
+ borderRadius: 8
502
+ }), 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", {
504
+ className: `dv-erd-edge${d ? " dv-erd-edge--active" : ""}`,
505
+ children: [
506
+ /* @__PURE__ */ g("path", {
507
+ className: "dv-erd-edge__interaction",
508
+ d: c,
509
+ fill: "none"
510
+ }),
511
+ /* @__PURE__ */ g("path", {
512
+ className: "dv-erd-edge__path",
513
+ d: c,
514
+ fill: "none",
515
+ style: {
516
+ stroke: f,
517
+ strokeWidth: p
518
+ },
519
+ strokeDasharray: u ? "6 4" : void 0
520
+ }),
521
+ l && Z(e, t, i, l.sourceEnd, f, p),
522
+ l && Z(n, r, a, l.targetEnd, f, p)
523
+ ]
524
+ });
525
+ }
526
+ var Q = t(le);
527
+ //#endregion
528
+ //#region src/components/useRelationHighlight.ts
529
+ function ue(e, t, r) {
530
+ let [i, o] = s(null), c = a(() => {
531
+ let t = /* @__PURE__ */ new Map();
532
+ for (let n of e) t.set(n.id, n);
533
+ return t;
534
+ }, [e]), l = a(() => {
535
+ let e = /* @__PURE__ */ new Map(), t = i ? c.get(i) : void 0;
536
+ if (!t) return e;
537
+ for (let n of [t.from, t.to]) {
538
+ let t = e.get(n.tableId) ?? /* @__PURE__ */ new Set();
539
+ n.columns.forEach((e) => t.add(e)), e.set(n.tableId, t);
540
+ }
541
+ return e;
542
+ }, [i, c]);
543
+ return {
544
+ displayNodes: a(() => i ? t.map((e) => {
545
+ let t = l.get(e.id);
546
+ return t ? {
547
+ ...e,
548
+ data: {
549
+ ...e.data,
550
+ highlightedColumns: t
551
+ }
552
+ } : e;
553
+ }) : t, [
554
+ t,
555
+ i,
556
+ l
557
+ ]),
558
+ displayEdges: a(() => i ? r.map((e) => e.id === i ? {
559
+ ...e,
560
+ data: {
561
+ ...e.data,
562
+ hovered: !0
563
+ },
564
+ zIndex: 1
565
+ } : e) : r, [r, i]),
566
+ onEdgeMouseEnter: n((e, t) => o(t.id), []),
567
+ onEdgeMouseLeave: n(() => o(null), [])
568
+ };
569
+ }
570
+ //#endregion
571
+ //#region src/theme.ts
572
+ var de = {
573
+ canvas: "--dv-canvas",
574
+ background: "--dv-bg",
575
+ border: "--dv-border",
576
+ headerBackground: "--dv-header-bg",
577
+ headerForeground: "--dv-header-fg",
578
+ rowForeground: "--dv-row-fg",
579
+ typeForeground: "--dv-type-fg",
580
+ rowHover: "--dv-row-hover",
581
+ rowHighlight: "--dv-row-highlight",
582
+ primaryKey: "--dv-pk",
583
+ foreignKey: "--dv-fk",
584
+ edge: "--dv-edge",
585
+ edgeActive: "--dv-edge-active",
586
+ fontFamily: "--dv-font"
587
+ }, fe = {
588
+ canvas: "#fafbfc",
589
+ background: "#ffffff",
590
+ border: "#d9dee5",
591
+ headerBackground: "#2d3748",
592
+ headerForeground: "#ffffff",
593
+ rowForeground: "#1a202c",
594
+ typeForeground: "#718096",
595
+ rowHover: "#f1f5f9",
596
+ rowHighlight: "#dbeafe",
597
+ primaryKey: "#b7791f",
598
+ foreignKey: "#2b6cb0",
599
+ edge: "#94a3b8",
600
+ edgeActive: "#2b6cb0"
601
+ }, pe = {
602
+ canvas: "#0f172a",
603
+ background: "#1e293b",
604
+ border: "#334155",
605
+ headerBackground: "#334155",
606
+ headerForeground: "#f1f5f9",
607
+ rowForeground: "#e2e8f0",
608
+ typeForeground: "#94a3b8",
609
+ rowHover: "#334155",
610
+ rowHighlight: "#1e3a5f",
611
+ primaryKey: "#d69e2e",
612
+ foreignKey: "#4299e1",
613
+ edge: "#64748b",
614
+ edgeActive: "#60a5fa"
615
+ };
616
+ function me(e) {
617
+ if (!e) return {};
618
+ let t = {};
619
+ for (let n of Object.keys(e)) {
620
+ let r = e[n];
621
+ r != null && (t[de[n]] = r);
622
+ }
623
+ return t;
624
+ }
625
+ //#endregion
626
+ //#region src/components/DbmlViewer.tsx
627
+ var he = [], ge = { table: q }, _e = { erd: Q };
628
+ function ve() {
629
+ return /* @__PURE__ */ _("svg", {
630
+ viewBox: "0 0 24 24",
631
+ width: "100%",
632
+ height: "100%",
633
+ fill: "currentColor",
634
+ "aria-hidden": "true",
635
+ children: [
636
+ /* @__PURE__ */ g("rect", {
637
+ x: "3",
638
+ y: "3",
639
+ width: "8",
640
+ height: "8",
641
+ rx: "1.5"
642
+ }),
643
+ /* @__PURE__ */ g("rect", {
644
+ x: "13",
645
+ y: "3",
646
+ width: "8",
647
+ height: "8",
648
+ rx: "1.5"
649
+ }),
650
+ /* @__PURE__ */ g("rect", {
651
+ x: "3",
652
+ y: "13",
653
+ width: "8",
654
+ height: "8",
655
+ rx: "1.5"
656
+ }),
657
+ /* @__PURE__ */ g("rect", {
658
+ x: "13",
659
+ y: "13",
660
+ width: "8",
661
+ height: "8",
662
+ rx: "1.5"
663
+ })
664
+ ]
665
+ });
666
+ }
667
+ function ye(e, t, n, r) {
668
+ let i = /* @__PURE__ */ new Map();
669
+ for (let a of e.tables) {
670
+ let e = t.get(a.id);
671
+ i.set(a.id, n?.[a.id] ?? r?.get(a.id) ?? {
672
+ x: e?.x ?? 0,
673
+ y: e?.y ?? 0
674
+ });
675
+ }
676
+ return i;
677
+ }
678
+ function be(e, t) {
679
+ if (e.length !== t.length) return !1;
680
+ let n = new Set(e);
681
+ return t.every((e) => n.has(e));
682
+ }
683
+ async function $(e, t, n) {
684
+ try {
685
+ return await R(e, t);
686
+ } catch (r) {
687
+ return n(r instanceof L ? r : new L(String(r), { cause: r })), P(e, t);
688
+ }
689
+ }
690
+ function xe(e, t) {
691
+ return e.tables.map((e) => ({
692
+ id: e.id,
693
+ type: "table",
694
+ position: t.get(e.id) ?? {
695
+ x: 0,
696
+ y: 0
697
+ },
698
+ data: { table: e }
699
+ }));
700
+ }
701
+ function Se(e, t) {
702
+ return e.relations.map((e) => {
703
+ 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
+ return {
705
+ id: e.id,
706
+ source: e.from.tableId,
707
+ target: e.to.tableId,
708
+ sourceHandle: U(s, a, "source"),
709
+ targetHandle: U(c, o, "target"),
710
+ type: "erd",
711
+ data: {
712
+ kind: e.kind,
713
+ sourceEnd: {
714
+ cardinality: e.from.relation,
715
+ optional: e.from.optional
716
+ },
717
+ targetEnd: {
718
+ cardinality: e.to.relation,
719
+ optional: e.to.optional
720
+ }
721
+ }
722
+ };
723
+ });
724
+ }
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(() => {
727
+ try {
728
+ return {
729
+ ok: !0,
730
+ schema: O(e)
731
+ };
732
+ } catch (e) {
733
+ return {
734
+ ok: !1,
735
+ error: e instanceof S ? e : new S(String(e), { cause: e })
736
+ };
737
+ }
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, () => {
740
+ 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, {
745
+ ...e,
746
+ backgroundColor: i
747
+ });
748
+ };
749
+ return {
750
+ toDataUrl: e,
751
+ download: async (t, n) => {
752
+ H(await e(n), t);
753
+ }
754
+ };
755
+ }, []);
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;
764
+ if (!e) return;
765
+ let t = {};
766
+ for (let e of F.current) t[e.id] = {
767
+ x: e.position.x,
768
+ y: e.position.y
769
+ };
770
+ 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 = {
774
+ ids: t,
775
+ optionsKey: n
776
+ }, i.emit) {
777
+ let e = {};
778
+ a.forEach((t, n) => {
779
+ e[n] = t;
780
+ }), L.current?.(e);
781
+ }
782
+ i.fit && requestAnimationFrame(() => void N.current?.fitView());
783
+ }, [D, j]);
784
+ r(() => {
785
+ if (!T.ok) {
786
+ D([]), j([]), K.current = null;
787
+ return;
788
+ }
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, {
795
+ x: e.position.x,
796
+ 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 });
800
+ return;
801
+ }
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,
806
+ prior: o,
807
+ fit: p
808
+ });
809
+ }), () => {
810
+ s = !0;
811
+ };
812
+ }, [
813
+ T,
814
+ V,
815
+ U,
816
+ W,
817
+ G,
818
+ q,
819
+ p,
820
+ J,
821
+ D,
822
+ j
823
+ ]);
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
831
+ }, n = e.tables.map((e) => e.id);
832
+ $(e, t, (e) => I.current?.(e)).then((t) => {
833
+ J(e, n, q, t, {
834
+ emit: !0,
835
+ fit: !0
836
+ });
837
+ });
838
+ }, [
839
+ T,
840
+ V,
841
+ U,
842
+ W,
843
+ 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 = {
851
+ width: "100%",
852
+ height: "100%",
853
+ ...me(d),
854
+ ...s
855
+ };
856
+ return T.ok ? /* @__PURE__ */ g("div", {
857
+ ref: P,
858
+ className: `dv-viewer${t ? ` ${t}` : ""}`,
859
+ style: Q,
860
+ children: /* @__PURE__ */ _(ee, {
861
+ nodes: se,
862
+ edges: ce,
863
+ nodeTypes: ge,
864
+ edgeTypes: _e,
865
+ onNodesChange: k,
866
+ onEdgesChange: M,
867
+ onEdgeMouseEnter: Z,
868
+ onEdgeMouseLeave: le,
869
+ onNodeDragStop: ae,
870
+ onInit: (e) => {
871
+ N.current = e;
872
+ },
873
+ fitView: p,
874
+ nodesConnectable: !1,
875
+ elementsSelectable: !0,
876
+ minZoom: .1,
877
+ proOptions: { hideAttribution: !0 },
878
+ children: [
879
+ h && /* @__PURE__ */ g(c, {}),
880
+ m && /* @__PURE__ */ g(u, { children: /* @__PURE__ */ g(l, {
881
+ onClick: Y,
882
+ title: "Auto layout",
883
+ "aria-label": "Auto layout",
884
+ children: /* @__PURE__ */ g(ve, {})
885
+ }) }),
886
+ ie && /* @__PURE__ */ g(f, {
887
+ pannable: !0,
888
+ zoomable: !0
889
+ })
890
+ ]
891
+ })
892
+ }) : /* @__PURE__ */ g("div", {
893
+ className: `dv-error${t ? ` ${t}` : ""}`,
894
+ style: Q,
895
+ children: /* @__PURE__ */ g("pre", {
896
+ className: "dv-error__message",
897
+ children: T.error.message
898
+ })
899
+ });
900
+ });
901
+ //#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 };