@grafloria/element 0.3.23 → 0.4.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.
Files changed (69) hide show
  1. package/README.md +7 -7
  2. package/package.json +7 -9
  3. package/src/index.d.ts +16 -16
  4. package/src/index.js +58 -339
  5. package/src/lib/dashboard-kit/dashboard.d.ts +1 -1
  6. package/src/lib/dashboard-kit/dashboard.js +22 -26
  7. package/src/lib/dashboard-kit/grid-binder.d.ts +1 -1
  8. package/src/lib/dashboard-kit/grid-binder.js +62 -65
  9. package/src/lib/dashboard-kit/grid-mapping.js +14 -26
  10. package/src/lib/dashboard-kit/index.d.ts +5 -5
  11. package/src/lib/dashboard-kit/index.js +5 -30
  12. package/src/lib/dashboard-kit/styles.js +4 -8
  13. package/src/lib/dashboard-kit/widgets.d.ts +1 -1
  14. package/src/lib/dashboard-kit/widgets.js +16 -26
  15. package/src/lib/diagram-kit/card.d.ts +2 -2
  16. package/src/lib/diagram-kit/card.js +22 -32
  17. package/src/lib/diagram-kit/editing.js +15 -20
  18. package/src/lib/diagram-kit/er.d.ts +1 -1
  19. package/src/lib/diagram-kit/er.js +15 -18
  20. package/src/lib/diagram-kit/handles.d.ts +4 -4
  21. package/src/lib/diagram-kit/handles.js +16 -29
  22. package/src/lib/diagram-kit/index.d.ts +10 -10
  23. package/src/lib/diagram-kit/index.js +10 -50
  24. package/src/lib/diagram-kit/join-guidance.js +9 -18
  25. package/src/lib/diagram-kit/rows.js +1 -4
  26. package/src/lib/diagram-kit/styles.js +4 -8
  27. package/src/lib/diagram-kit/uml.js +10 -13
  28. package/src/lib/diagram-kit/update.d.ts +1 -1
  29. package/src/lib/diagram-kit/update.js +19 -26
  30. package/src/lib/grafloria-flow-element.js +19 -24
  31. package/src/lib/grafloria.d.ts +4 -4
  32. package/src/lib/grafloria.js +12 -17
  33. package/src/lib/load.d.ts +3 -3
  34. package/src/lib/load.js +19 -22
  35. package/src/lib/node-type-registry.js +6 -14
  36. package/src/lib/stencil-kit/builders.js +4 -10
  37. package/src/lib/stencil-kit/card-builders.js +14 -17
  38. package/src/lib/stencil-kit/index.d.ts +7 -7
  39. package/src/lib/stencil-kit/index.js +6 -16
  40. package/src/lib/stencil-kit/palette.js +14 -17
  41. package/src/lib/stencil-kit/shape-data.js +25 -28
  42. package/src/lib/stencil-kit/styles.js +1 -4
  43. package/esm/src/index.js +0 -295
  44. package/esm/src/lib/dashboard-kit/dashboard.js +0 -590
  45. package/esm/src/lib/dashboard-kit/grid-binder.js +0 -1633
  46. package/esm/src/lib/dashboard-kit/grid-mapping.js +0 -164
  47. package/esm/src/lib/dashboard-kit/index.js +0 -8
  48. package/esm/src/lib/dashboard-kit/styles.js +0 -205
  49. package/esm/src/lib/dashboard-kit/widgets.js +0 -379
  50. package/esm/src/lib/diagram-kit/card.js +0 -199
  51. package/esm/src/lib/diagram-kit/editing.js +0 -448
  52. package/esm/src/lib/diagram-kit/er.js +0 -160
  53. package/esm/src/lib/diagram-kit/handles.js +0 -312
  54. package/esm/src/lib/diagram-kit/index.js +0 -15
  55. package/esm/src/lib/diagram-kit/join-guidance.js +0 -322
  56. package/esm/src/lib/diagram-kit/rows.js +0 -144
  57. package/esm/src/lib/diagram-kit/styles.js +0 -118
  58. package/esm/src/lib/diagram-kit/uml.js +0 -135
  59. package/esm/src/lib/diagram-kit/update.js +0 -244
  60. package/esm/src/lib/grafloria-flow-element.js +0 -266
  61. package/esm/src/lib/grafloria.js +0 -69
  62. package/esm/src/lib/load.js +0 -252
  63. package/esm/src/lib/node-type-registry.js +0 -42
  64. package/esm/src/lib/stencil-kit/builders.js +0 -18
  65. package/esm/src/lib/stencil-kit/card-builders.js +0 -105
  66. package/esm/src/lib/stencil-kit/index.js +0 -9
  67. package/esm/src/lib/stencil-kit/palette.js +0 -427
  68. package/esm/src/lib/stencil-kit/shape-data.js +0 -537
  69. package/esm/src/lib/stencil-kit/styles.js +0 -126
@@ -1,448 +0,0 @@
1
- /**
2
- * In-canvas editing chrome (P2 title rename + P4 column/member editing).
3
- *
4
- * Bound once per container by a kit's `finalize(api)` when `editable: true`.
5
- * It turns the editing affordances the card already draws (`card.ts` grows them
6
- * when editable) into gestures:
7
- *
8
- * - double-click the header (`.axk-entity-head` / `.axk-uml-name`) → rename
9
- * the table / class;
10
- * - double-click a column name (`.axk-col`) or a member (`.axk-member`) →
11
- * rename it in place;
12
- * - click the "+ add column" / "+ attribute" / "+ method" affordance → add
13
- * one, then inline-edit its name;
14
- * - click a row's "×" delete control → remove that column / member.
15
- *
16
- * Every mutation goes through {@link updateEntity} / {@link updateClass} — so it
17
- * is ONE undoable step and the card + ports + edges stay consistent.
18
- *
19
- * The inline editor is a real `<input>` mounted in the renderer's WORLD layer
20
- * via `createViewportPortal`, so it pans and zooms WITH the card. When there is
21
- * no world layer (a bare jsdom mount, or a host that swapped the renderer) it
22
- * falls back to an absolutely-positioned input in the container — the edit
23
- * still commits, it just doesn't track the camera.
24
- *
25
- * Control clicks are claimed in the CAPTURE phase (`stopPropagation`) so the
26
- * row-selection handler and the binder's node/​waypoint double-click never also
27
- * fire for the same gesture.
28
- */
29
- import { updateEntity, updateClass, addColumnAt, removeColumnAt, renameColumnAt } from './update';
30
- const bindings = new WeakMap();
31
- const ROW_SELECTOR = '.axk-row, .axk-member';
32
- /** Resolve the node id + the clicked row's global index (among selectable rows). */
33
- function locate(el) {
34
- const group = el.closest('[data-node-id]');
35
- if (!group)
36
- return null;
37
- const nodeId = group.getAttribute('data-node-id');
38
- if (!nodeId)
39
- return null;
40
- const rowEl = el.closest(ROW_SELECTOR);
41
- const rows = Array.from(group.querySelectorAll(ROW_SELECTOR));
42
- const rowIndex = rowEl ? rows.indexOf(rowEl) : -1;
43
- return { nodeId, rowIndex, group };
44
- }
45
- const kitEntity = (api, nodeId) => { var _a; return (_a = api.getModel().getNode(nodeId)) === null || _a === void 0 ? void 0 : _a.getMetadata('kitEntity'); };
46
- const kitClass = (api, nodeId) => { var _a; return (_a = api.getModel().getNode(nodeId)) === null || _a === void 0 ? void 0 : _a.getMetadata('kitClass'); };
47
- /** UML: split a global member index into its compartment + local index. */
48
- function umlSection(cls, rowIndex) {
49
- var _a;
50
- const attrs = (_a = cls.attributes) !== null && _a !== void 0 ? _a : [];
51
- return rowIndex < attrs.length
52
- ? { section: 'attributes', local: rowIndex }
53
- : { section: 'methods', local: rowIndex - attrs.length };
54
- }
55
- /**
56
- * Mount a focused `<input>` over `targetEl`, prefilled with `value`. Commits on
57
- * Enter/blur, cancels on Escape. Returns the input (already in the DOM).
58
- */
59
- /**
60
- * The inline editor currently open, if any.
61
- *
62
- * The editor is mounted in the renderer's WORLD layer, not inside the card, so
63
- * it does not die when the card re-renders. That is what lets it survive a
64
- * re-render mid-edit — and it is also why deleting the very row being edited
65
- * used to leave a stray input floating over the canvas: the row vanished, the
66
- * portal did not. Any structural edit now dismisses it first.
67
- */
68
- let activeEditor = null;
69
- /** Dismiss any open inline editor WITHOUT committing. */
70
- export function dismissInlineEditor() {
71
- const open = activeEditor;
72
- activeEditor = null;
73
- open === null || open === void 0 ? void 0 : open.cancel();
74
- }
75
- function addrOf(el) {
76
- const loc = locate(el);
77
- if (!loc || loc.rowIndex < 0)
78
- return null;
79
- const kind = el.closest('.axk-ty') ? 'type' : 'name';
80
- return { nodeId: loc.nodeId, rowIndex: loc.rowIndex, kind };
81
- }
82
- /** The cell `dir` steps away in reading order (name → type → next row's name). */
83
- function neighbourCell(el, dir) {
84
- const a = addrOf(el);
85
- if (!a)
86
- return null;
87
- if (dir === 1) {
88
- return a.kind === 'name'
89
- ? Object.assign(Object.assign({}, a), { kind: 'type' }) : Object.assign(Object.assign({}, a), { rowIndex: a.rowIndex + 1, kind: 'name' });
90
- }
91
- return a.kind === 'type'
92
- ? Object.assign(Object.assign({}, a), { kind: 'name' }) : Object.assign(Object.assign({}, a), { rowIndex: a.rowIndex - 1, kind: 'type' });
93
- }
94
- /** The same column, one row down. */
95
- function cellBelow(el) {
96
- const a = addrOf(el);
97
- return a ? Object.assign(Object.assign({}, a), { rowIndex: a.rowIndex + 1 }) : null;
98
- }
99
- /** Re-open the editor at an address, if that cell still exists. */
100
- function reopenAt(api, addr) {
101
- const card = api.container.querySelector(`[data-node-id="${cssEscape(addr.nodeId)}"]`);
102
- const row = card === null || card === void 0 ? void 0 : card.querySelectorAll('.axk-row, .axk-member')[addr.rowIndex];
103
- const cell = row === null || row === void 0 ? void 0 : row.querySelector(addr.kind === 'type' ? '.axk-ty' : '.axk-col');
104
- if (cell)
105
- beginRename(api, cell);
106
- }
107
- /** First occurrence wins, order preserved. */
108
- function dedupe(values) {
109
- return [...new Set(values)];
110
- }
111
- /** SQL-ish column types offered when editing a type cell. */
112
- export const ER_TYPE_SUGGESTIONS = [
113
- 'uuid', 'int', 'bigint', 'smallint', 'serial', 'decimal', 'numeric', 'real', 'double',
114
- 'varchar', 'text', 'char', 'boolean', 'date', 'time', 'timestamp', 'timestamptz',
115
- 'json', 'jsonb', 'bytea', 'enum',
116
- ];
117
- function openInlineEditor(api, targetEl, value, onCommit, suggestions) {
118
- const container = api.container;
119
- const doc = container.ownerDocument;
120
- const input = doc.createElement('input');
121
- input.className = 'axk-edit-input';
122
- input.value = value;
123
- input.spellcheck = false;
124
- input.setAttribute('autocomplete', 'off');
125
- // A type is a choice, not free prose. `<datalist>` gives a real combobox —
126
- // the browser filters the list AS YOU TYPE and still allows a value that is
127
- // not in it, which is what a schema editor needs (custom/domain types).
128
- let listEl = null;
129
- if (suggestions === null || suggestions === void 0 ? void 0 : suggestions.length) {
130
- listEl = doc.createElement('datalist');
131
- listEl.id = `axk-types-${Math.random().toString(36).slice(2, 8)}`;
132
- for (const t of suggestions) {
133
- const opt = doc.createElement('option');
134
- opt.value = t;
135
- listEl.appendChild(opt);
136
- }
137
- doc.body.appendChild(listEl);
138
- input.setAttribute('list', listEl.id);
139
- }
140
- // POSITIONING — screen space, deliberately.
141
- //
142
- // This used to mount the input in the renderer's world layer through a
143
- // viewport portal so it would scale and pan with the canvas. That produced a
144
- // steady trickle of defects: the caret landed between two rows (world/zoom
145
- // maths compounding with the card's own scroll offset), and — because the
146
- // world layer carries a CSS transform — the browser refused to place the
147
- // native <datalist> popup, so the type dropdown never opened.
148
- //
149
- // An inline editor lives for a few seconds. Pinning it over the cell's actual
150
- // screen rect is exact by construction (no maths to get wrong) and keeps it
151
- // out of any transformed ancestor, which is what the native popup needs. The
152
- // cost is that it does not follow a pan mid-edit; that is a fair trade for an
153
- // editor that is reliably WHERE THE CELL IS.
154
- const rect = targetEl.getBoundingClientRect();
155
- const host = container.getBoundingClientRect();
156
- const minW = (suggestions === null || suggestions === void 0 ? void 0 : suggestions.length) ? 132 : 72;
157
- let w = Math.max(rect.width, minW);
158
- let left = rect.left;
159
- // Never spill past the card: slide left to fit, shrink only if the card is
160
- // narrower than the floor.
161
- const card = targetEl.closest('.axk-entity, .axk-uml');
162
- if (card) {
163
- const cb = card.getBoundingClientRect();
164
- const pad = 4;
165
- if (w > cb.width - pad * 2)
166
- w = cb.width - pad * 2;
167
- const overflow = left + w - (cb.right - pad);
168
- if (overflow > 0)
169
- left -= overflow;
170
- if (left < cb.left + pad)
171
- left = cb.left + pad;
172
- }
173
- // `position: fixed` keys off the viewport, exactly like getBoundingClientRect,
174
- // so no ancestor offset or scroll correction can drift it.
175
- input.style.cssText =
176
- `position:fixed;left:${Math.round(left)}px;top:${Math.round(rect.top)}px;` +
177
- `width:${Math.round(w)}px;height:${Math.round(rect.height)}px;z-index:2147483000;`;
178
- void host; // container rect intentionally unused: fixed positioning needs none
179
- // Mounted on the CONTAINER, not document.body: hosts (and the kit's own
180
- // tests) look the editor up inside the diagram container, and moving it out
181
- // silently broke every one of those lookups. `position: fixed` still keys off
182
- // the viewport, so it stays exact — the point was only ever to escape the
183
- // world layer's CSS transform, which is a descendant of the container.
184
- container.appendChild(input);
185
- /** Guards double-settling (blur fires after Enter/Escape already handled it). */
186
- let done = false;
187
- const self = { cancel: () => undefined };
188
- const cleanup = () => {
189
- if (activeEditor === self)
190
- activeEditor = null;
191
- listEl === null || listEl === void 0 ? void 0 : listEl.remove();
192
- input.remove();
193
- };
194
- const commit = () => {
195
- if (done)
196
- return;
197
- done = true;
198
- const next = input.value.trim();
199
- cleanup();
200
- if (next.length)
201
- onCommit(next);
202
- };
203
- const cancel = () => {
204
- if (done)
205
- return;
206
- done = true;
207
- cleanup();
208
- };
209
- input.addEventListener('keydown', (e) => {
210
- // Keyboard navigation: TAB walks the cells (name → type → next row's name,
211
- // Shift+Tab back). Enter deliberately does NOT move — it commits and
212
- // closes, which is what every other inline editor in the app does and what
213
- // a user pressing Enter to "finish" expects. Navigation belongs on Tab.
214
- if (e.key === 'Tab') {
215
- const next = neighbourCell(targetEl, e.shiftKey ? -1 : 1);
216
- if (next) {
217
- e.preventDefault();
218
- commit();
219
- // Re-open on the next cell after the card has re-rendered.
220
- setTimeout(() => reopenAt(api, next), 0);
221
- return;
222
- }
223
- }
224
- if (e.key === 'Enter') {
225
- e.preventDefault();
226
- commit();
227
- }
228
- else if (e.key === 'Escape') {
229
- e.preventDefault();
230
- cancel();
231
- }
232
- e.stopPropagation();
233
- });
234
- // Registered AFTER commit/cancel exist so a structural edit can dismiss it.
235
- self.cancel = cancel;
236
- activeEditor = self;
237
- input.addEventListener('blur', commit);
238
- // Don't let a click INSIDE the input bubble to the canvas (deselect / pan).
239
- // Keep the canvas binder OUT of the editor. It listens on POINTER events and
240
- // preventDefaults them to own the gesture — which also suppressed the native
241
- // <datalist> popup, so the type dropdown either never opened or flashed open
242
- // and shut. Guarding `mousedown` alone was not enough: pointerdown fires
243
- // first and is what the binder acts on.
244
- for (const type of ['pointerdown', 'pointerup', 'mousedown', 'mouseup', 'click', 'dblclick']) {
245
- input.addEventListener(type, (e) => e.stopPropagation());
246
- }
247
- // Focus after mount so select() works.
248
- setTimeout(() => {
249
- input.focus();
250
- input.select();
251
- }, 0);
252
- return input;
253
- }
254
- /** Begin renaming whatever the clicked row/header targets. */
255
- function beginRename(api, target) {
256
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
257
- const loc = locate(target);
258
- if (!loc)
259
- return;
260
- const { nodeId, rowIndex } = loc;
261
- // Title (ER header / UML name).
262
- if (target.closest('.axk-entity-head')) {
263
- const ent = kitEntity(api, nodeId);
264
- openInlineEditor(api, target.closest('.axk-entity-head'), (_a = ent === null || ent === void 0 ? void 0 : ent.name) !== null && _a !== void 0 ? _a : nodeId, (name) => void updateEntity(api, nodeId, { name }));
265
- return;
266
- }
267
- if (target.closest('.axk-uml-name')) {
268
- const cls = kitClass(api, nodeId);
269
- openInlineEditor(api, target.closest('.axk-uml-name'), (_b = cls === null || cls === void 0 ? void 0 : cls.name) !== null && _b !== void 0 ? _b : nodeId, (name) => void updateClass(api, nodeId, { name }));
270
- return;
271
- }
272
- // ER column. Which CELL was double-clicked decides what gets edited and where
273
- // the input is mounted: the name span (`.axk-col`) and the type span
274
- // (`.axk-ty`) are SIBLINGS inside `.axk-row`. Editing only ever handled the
275
- // name and always anchored to it, so double-clicking a type either did
276
- // nothing or dropped the caret over the name — the wrong field in the wrong
277
- // place. A double-click anywhere else on the row falls back to the name,
278
- // anchored to the name cell, so the gesture is predictable everywhere.
279
- const typeEl = target.closest('.axk-ty');
280
- if (typeEl && rowIndex >= 0) {
281
- const ent = kitEntity(api, nodeId);
282
- if (!ent)
283
- return;
284
- openInlineEditor(api, typeEl, (_d = (_c = ent.columns[rowIndex]) === null || _c === void 0 ? void 0 : _c.type) !== null && _d !== void 0 ? _d : '', (type) => {
285
- const columns = ent.columns.map((c, i) => (i === rowIndex ? Object.assign(Object.assign({}, c), { type }) : c));
286
- void updateEntity(api, nodeId, { columns });
287
- },
288
- // Offer the types already used in THIS diagram first, then the standards —
289
- // a schema is usually internally consistent, so what you just typed
290
- // elsewhere is the likeliest next value.
291
- dedupe([...ent.columns.map((c) => c.type).filter(Boolean), ...ER_TYPE_SUGGESTIONS]));
292
- return;
293
- }
294
- const rowEl = target.closest('.axk-row');
295
- const colEl = (_f = (_e = target.closest('.axk-col')) !== null && _e !== void 0 ? _e : rowEl === null || rowEl === void 0 ? void 0 : rowEl.querySelector('.axk-col')) !== null && _f !== void 0 ? _f : null;
296
- if (colEl && rowIndex >= 0) {
297
- const ent = kitEntity(api, nodeId);
298
- if (!ent)
299
- return;
300
- openInlineEditor(api, colEl, (_h = (_g = ent.columns[rowIndex]) === null || _g === void 0 ? void 0 : _g.name) !== null && _h !== void 0 ? _h : '', (name) => void updateEntity(api, nodeId, { columns: renameColumnAt(ent.columns, rowIndex, name) }));
301
- return;
302
- }
303
- // UML member rename.
304
- const memberEl = target.closest('.axk-member');
305
- if (memberEl && rowIndex >= 0) {
306
- const cls = kitClass(api, nodeId);
307
- if (!cls)
308
- return;
309
- const { section, local } = umlSection(cls, rowIndex);
310
- const list = ((_j = cls[section]) !== null && _j !== void 0 ? _j : []).slice();
311
- openInlineEditor(api, (_k = memberEl.querySelector('.axk-mtext')) !== null && _k !== void 0 ? _k : memberEl, (_l = list[local]) !== null && _l !== void 0 ? _l : '', (next) => {
312
- list[local] = next;
313
- void updateClass(api, nodeId, { [section]: list });
314
- });
315
- }
316
- }
317
- /** Add a column / member (then inline-edit the new one). Returns true if handled. */
318
- function handleAdd(api, target) {
319
- var _a;
320
- const addEr = target.closest('.axk-entity-add');
321
- if (addEr) {
322
- const loc = locate(addEr);
323
- if (!loc)
324
- return true;
325
- const ent = kitEntity(api, loc.nodeId);
326
- if (!ent)
327
- return true;
328
- const columns = addColumnAt(ent.columns, { name: 'new_column', type: '' });
329
- void updateEntity(api, loc.nodeId, { columns }).then(() => {
330
- const newRow = api.container.querySelectorAll(`[data-node-id="${cssEscape(loc.nodeId)}"] .axk-row`)[columns.length - 1];
331
- const col = newRow === null || newRow === void 0 ? void 0 : newRow.querySelector('.axk-col');
332
- if (col)
333
- beginRename(api, col);
334
- });
335
- return true;
336
- }
337
- const addUml = target.closest('.axk-uml-add');
338
- if (addUml) {
339
- const loc = locate(addUml);
340
- if (!loc)
341
- return true;
342
- const cls = kitClass(api, loc.nodeId);
343
- if (!cls)
344
- return true;
345
- const comps = Array.from(loc.group.querySelectorAll('.axk-uml-comp'));
346
- const section = comps.indexOf(addUml.closest('.axk-uml-comp')) === 0 ? 'attributes' : 'methods';
347
- const list = [...((_a = cls[section]) !== null && _a !== void 0 ? _a : []), section === 'attributes' ? '+ field: type' : '+ method(): void'];
348
- void updateClass(api, loc.nodeId, { [section]: list });
349
- return true;
350
- }
351
- return false;
352
- }
353
- /** Delete the column / member whose "×" control was clicked. Returns true if handled. */
354
- function handleDelete(api, target) {
355
- var _a;
356
- const del = target.closest('.axk-col-del');
357
- if (!del)
358
- return false;
359
- const loc = locate(del);
360
- if (!loc || loc.rowIndex < 0)
361
- return true;
362
- const ent = kitEntity(api, loc.nodeId);
363
- if (ent) {
364
- void updateEntity(api, loc.nodeId, { columns: removeColumnAt(ent.columns, loc.rowIndex) });
365
- return true;
366
- }
367
- const cls = kitClass(api, loc.nodeId);
368
- if (cls) {
369
- const { section, local } = umlSection(cls, loc.rowIndex);
370
- const list = ((_a = cls[section]) !== null && _a !== void 0 ? _a : []).slice();
371
- list.splice(local, 1);
372
- void updateClass(api, loc.nodeId, { [section]: list });
373
- }
374
- return true;
375
- }
376
- function cssEscape(id) {
377
- return typeof CSS !== 'undefined' && CSS.escape ? CSS.escape(id) : id.replace(/"/g, '\\"');
378
- }
379
- /**
380
- * Wire the editing gestures onto a kit container. Idempotent per container
381
- * (a re-bind disposes the previous one), matching `bindRowInteractions`.
382
- */
383
- export function bindCardEditing(api) {
384
- var _a;
385
- const container = api.container;
386
- (_a = bindings.get(container)) === null || _a === void 0 ? void 0 : _a.dispose();
387
- const onClickCapture = (event) => {
388
- const target = event.target instanceof Element ? event.target : null;
389
- if (!target)
390
- return;
391
- // Claim control clicks BEFORE row-selection / canvas handlers see them.
392
- // An open editor is dismissed first: its row may be the one being removed,
393
- // and committing a rename into a structural change is a race even when it
394
- // is not (the card re-renders under the caret).
395
- if (target.closest('.axk-col-del') || target.closest('.axk-entity-add') || target.closest('.axk-uml-add')) {
396
- dismissInlineEditor();
397
- }
398
- if (handleDelete(api, target) || handleAdd(api, target)) {
399
- event.preventDefault();
400
- event.stopPropagation();
401
- }
402
- };
403
- const onDblClickCapture = (event) => {
404
- const target = event.target instanceof Element ? event.target : null;
405
- if (!target)
406
- return;
407
- if (target.closest('.axk-edit-input'))
408
- return; // don't re-open over our own input
409
- if (target.closest('.axk-entity-head') ||
410
- target.closest('.axk-uml-name') ||
411
- target.closest('.axk-col') ||
412
- target.closest('.axk-ty') ||
413
- target.closest('.axk-row') ||
414
- target.closest('.axk-member')) {
415
- event.preventDefault();
416
- event.stopPropagation();
417
- beginRename(api, target);
418
- }
419
- };
420
- // A control press must dismiss the editor BEFORE the browser's focus change
421
- // fires `blur` → `commit`. Doing it on `click` was too late: the rename had
422
- // already landed, so deleting the row you were editing renamed it first and
423
- // then removed a shifted index. preventDefault keeps focus in place too.
424
- const onMouseDownCapture = (event) => {
425
- const t = event.target instanceof Element ? event.target : null;
426
- if (!t)
427
- return;
428
- if (t.closest('.axk-col-del') || t.closest('.axk-entity-add') || t.closest('.axk-uml-add')) {
429
- dismissInlineEditor();
430
- event.preventDefault();
431
- }
432
- };
433
- container.addEventListener('mousedown', onMouseDownCapture, true);
434
- container.addEventListener('click', onClickCapture, true);
435
- container.addEventListener('dblclick', onDblClickCapture, true);
436
- const handle = {
437
- dispose() {
438
- container.removeEventListener('mousedown', onMouseDownCapture, true);
439
- container.removeEventListener('click', onClickCapture, true);
440
- container.removeEventListener('dblclick', onDblClickCapture, true);
441
- if (bindings.get(container) === handle)
442
- bindings.delete(container);
443
- },
444
- };
445
- bindings.set(container, handle);
446
- return handle;
447
- }
448
- //# sourceMappingURL=editing.js.map
@@ -1,160 +0,0 @@
1
- /**
2
- * ER diagram kit — typed builders for database entity-relationship diagrams.
3
- *
4
- * The packaged form of what demos/diagrams/table-er.html and er-advanced.html
5
- * hand-composed: HTML "table" cards (header, typed columns, PK/FK badges),
6
- * crow's-foot cardinality edges, and field-level FK→PK connections via
7
- * absolute-layout ports. An embedder writes data:
8
- *
9
- * ```ts
10
- * const spec = erDiagram({
11
- * entities: [
12
- * { id: 'CUSTOMER', columns: [{ name: 'id', type: 'int', pk: true }, …] },
13
- * { id: 'ORDER', columns: [{ name: 'customer_id', type: 'int', fk: true }, …] },
14
- * ],
15
- * relationships: [
16
- * { from: 'CUSTOMER', to: 'ORDER', label: 'places' }, // table-level
17
- * { from: 'ORDER.customer_id', to: 'CUSTOMER.id' }, // field-level
18
- * ],
19
- * });
20
- * render(spec, container);
21
- * ```
22
- */
23
- import { ensureDiagramKitStyles } from './styles';
24
- import { bindRowInteractions } from './rows';
25
- import { entityCardContent, entityAutoHeight, erRowCenterY, ER_ROW_H, ER_HEAD_H } from './card';
26
- import { bindCardEditing } from './editing';
27
- // Layout constants + the row-centre helper live in card.ts now (the ONE source
28
- // of truth shared with update.ts). Re-exported here so `import … from './er'`
29
- // keeps working.
30
- export { erRowCenterY, ER_ROW_H, ER_HEAD_H };
31
- const DEFAULT_WIDTH = 190;
32
- const CARDINALITY = {
33
- 'one-to-many': { tail: 'one', head: 'crow-foot' },
34
- 'one-to-one': { tail: 'one', head: 'one' },
35
- 'many-to-many': { tail: 'crow-foot', head: 'crow-foot' },
36
- 'one-to-zero-or-many': { tail: 'one', head: 'zero-or-many' },
37
- 'one-to-one-or-many': { tail: 'one', head: 'one-or-many' },
38
- };
39
- function parseEnd(ref, byId) {
40
- const dot = ref.indexOf('.');
41
- const entityId = dot === -1 ? ref : ref.slice(0, dot);
42
- const entity = byId.get(entityId);
43
- if (!entity)
44
- throw new Error(`erDiagram: unknown entity "${entityId}" in relationship end "${ref}"`);
45
- if (dot === -1)
46
- return { entity };
47
- const column = ref.slice(dot + 1);
48
- const rowIndex = entity.columns.findIndex((c) => c.name === column);
49
- if (rowIndex === -1) {
50
- throw new Error(`erDiagram: entity "${entityId}" has no column "${column}" (relationship end "${ref}")`);
51
- }
52
- return { entity, column, rowIndex };
53
- }
54
- /**
55
- * Build a render() spec for an ER diagram.
56
- *
57
- * Entities become HTML table cards; relationships become orthogonal edges with
58
- * crow's-foot cardinality. A `TABLE.column` end pins the edge to that row via
59
- * an absolute-layout port (the FK→PK look). Two edges landing on the same
60
- * column are automatically spread apart.
61
- */
62
- export function erDiagram(options) {
63
- var _a;
64
- ensureDiagramKitStyles();
65
- const byId = new Map(options.entities.map((e) => [e.id, e]));
66
- // Field-level ports are accumulated per entity while walking relationships.
67
- const portsByEntity = new Map();
68
- // How many edges already landed on a given entity row+side — drives the
69
- // spread (dy) so shared columns (a PK referenced twice) don't stack.
70
- const rowLandings = new Map();
71
- const width = (e) => { var _a; return (_a = e.width) !== null && _a !== void 0 ? _a : DEFAULT_WIDTH; };
72
- const fieldPort = (end, side) => {
73
- var _a, _b;
74
- const entity = end.entity;
75
- const key = `${entity.id}#${end.rowIndex}#${side}`;
76
- const landing = (_a = rowLandings.get(key)) !== null && _a !== void 0 ? _a : 0;
77
- rowLandings.set(key, landing + 1);
78
- // Spread repeat landings ±5px around the row centre: 0, -5, +5, -10, …
79
- const dy = landing === 0 ? 0 : (landing % 2 === 1 ? -1 : 1) * Math.ceil(landing / 2) * 5;
80
- const id = `${entity.id}__${end.column}__${side}__${landing}`;
81
- const w = width(entity);
82
- const port = {
83
- id,
84
- side,
85
- visible: false,
86
- layout: {
87
- strategy: 'absolute',
88
- args: {
89
- units: 'px',
90
- x: side === 'right' ? w : side === 'left' ? 0 : w / 2,
91
- y: erRowCenterY(end.rowIndex),
92
- dy,
93
- },
94
- },
95
- };
96
- const bucket = (_b = portsByEntity.get(entity.id)) !== null && _b !== void 0 ? _b : [];
97
- bucket.push(port);
98
- portsByEntity.set(entity.id, bucket);
99
- return id;
100
- };
101
- const edges = ((_a = options.relationships) !== null && _a !== void 0 ? _a : []).map((rel, i) => {
102
- var _a, _b, _c, _d, _e;
103
- const from = parseEnd(rel.from, byId);
104
- const to = parseEnd(rel.to, byId);
105
- const fromSide = (_a = rel.fromSide) !== null && _a !== void 0 ? _a : 'right';
106
- const toSide = (_b = rel.toSide) !== null && _b !== void 0 ? _b : 'left';
107
- const markers = typeof rel.cardinality === 'object'
108
- ? rel.cardinality
109
- : CARDINALITY[(_c = rel.cardinality) !== null && _c !== void 0 ? _c : 'one-to-many'];
110
- return {
111
- id: (_d = rel.id) !== null && _d !== void 0 ? _d : `er-rel-${i + 1}`,
112
- source: from.entity.id,
113
- target: to.entity.id,
114
- label: rel.label,
115
- type: 'orthogonal',
116
- sourceHandle: from.column !== undefined ? fieldPort(from, fromSide) : fromSide,
117
- targetHandle: to.column !== undefined ? fieldPort(to, toSide) : toSide,
118
- style: {
119
- stroke: (_e = rel.color) !== null && _e !== void 0 ? _e : '#64748b',
120
- strokeWidth: 1.5,
121
- arrowTail: { type: markers.tail, size: 8, filled: false },
122
- arrowHead: { type: markers.head, size: 9, filled: false },
123
- },
124
- };
125
- });
126
- const editable = options.editable === true;
127
- const nodes = options.entities.map((entity, i) => {
128
- var _a;
129
- return (Object.assign({ id: entity.id, position: (_a = entity.position) !== null && _a !== void 0 ? _a : { x: 60 + (i % 3) * 340, y: 60 + Math.floor(i / 3) * 280 }, size: { width: width(entity), height: entityAutoHeight(entity, editable) },
130
- // interactive: rows are real DOM targets (hover, row selection, inline
131
- // editing) — node drag/select stay geometric in the binder.
132
- metadata: Object.assign({ html: { content: entityCardContent(entity, editable), interactive: true }, kitEntity: entity, kitEditable: editable }, (options.rowSelection === false ? { kitRowSelection: false } : {})),
133
- // The card draws its own border — the node's default rectangle is hidden
134
- // (and re-suppressed on selection by the kit stylesheet).
135
- shape: { type: 'rect', fill: 'none', stroke: 'none' }, style: { fill: 'transparent', stroke: 'transparent', strokeWidth: 0 } }, (portsByEntity.has(entity.id) ? { ports: portsByEntity.get(entity.id) } : {})));
136
- });
137
- return {
138
- nodes,
139
- edges,
140
- finalize: (api) => {
141
- var _a, _b, _c, _d;
142
- const a = api;
143
- // The card draws its OWN selection ring — suppress the node resize handles
144
- // on the live model (they'd frame the card in a SECOND rectangle). Set here
145
- // because the render-input path does not honour a spec-level `behavior`.
146
- const model = (_a = a === null || a === void 0 ? void 0 : a.getModel) === null || _a === void 0 ? void 0 : _a.call(a);
147
- if (model)
148
- for (const e of options.entities)
149
- (_d = (_c = (_b = model.getNode) === null || _b === void 0 ? void 0 : _b.call(model, e.id)) === null || _c === void 0 ? void 0 : _c.setBehavior) === null || _d === void 0 ? void 0 : _d.call(_c, { resizable: false });
150
- if (options.rowSelection !== false && (a === null || a === void 0 ? void 0 : a.container))
151
- bindRowInteractions(a);
152
- // Editing chrome (dbl-click rename, add/delete column) — opt-in, and safe
153
- // to run alongside row selection (it claims control clicks in the capture
154
- // phase before the selection handler sees them).
155
- if (editable && (a === null || a === void 0 ? void 0 : a.container))
156
- bindCardEditing(a);
157
- },
158
- };
159
- }
160
- //# sourceMappingURL=er.js.map