@diagc/cli 1.4.0 → 1.6.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.
- package/assets/guide/index.md +1 -1
- package/assets/skill/SKILL.md +1 -1
- package/assets/studio/assets/{index-B4EbTBLO.js → index-DRsUukzt.js} +48 -46
- package/assets/studio/index.html +1 -1
- package/dist/api/diagrams.d.ts +5 -0
- package/dist/api/diagrams.js +37 -24
- package/dist/api/dispatch.d.ts +3 -0
- package/dist/api/handlers.d.ts +1 -0
- package/dist/api/handlers.js +1 -0
- package/dist/api/pages.d.ts +21 -0
- package/dist/api/pages.js +50 -0
- package/dist/api/routes.d.ts +2 -0
- package/dist/api/routes.js +6 -0
- package/dist/cli/io.d.ts +5 -0
- package/dist/cli/io.js +4 -0
- package/dist/cli/parse.d.ts +7 -0
- package/dist/cli/parse.js +25 -0
- package/dist/cli/run.js +4 -0
- package/dist/cli.js +23 -1
- package/dist/commands/shared.d.ts +4 -0
- package/dist/commands/shared.js +13 -0
- package/dist/commands/show.d.ts +5 -0
- package/dist/commands/show.js +115 -0
- package/dist/commands/tui.d.ts +3 -0
- package/dist/commands/tui.js +36 -0
- package/dist/init.d.ts +1 -1
- package/dist/init.js +1 -1
- package/dist/publish/publish.d.ts +15 -1
- package/dist/publish/publish.js +22 -15
- package/dist/serve.d.ts +3 -0
- package/dist/serve.js +2 -2
- package/dist/studio.js +1 -0
- package/dist/text/accents.d.ts +9 -0
- package/dist/text/accents.js +83 -0
- package/dist/text/ansi.d.ts +28 -0
- package/dist/text/ansi.js +111 -0
- package/dist/text/braille.d.ts +28 -0
- package/dist/text/braille.js +58 -0
- package/dist/text/canvas.d.ts +65 -0
- package/dist/text/canvas.js +155 -0
- package/dist/text/content.d.ts +32 -0
- package/dist/text/content.js +90 -0
- package/dist/text/fish.d.ts +27 -0
- package/dist/text/fish.js +209 -0
- package/dist/text/folds.d.ts +14 -0
- package/dist/text/folds.js +45 -0
- package/dist/text/glyphs.d.ts +31 -0
- package/dist/text/glyphs.js +84 -0
- package/dist/text/layout.d.ts +61 -0
- package/dist/text/layout.js +334 -0
- package/dist/text/legend.d.ts +44 -0
- package/dist/text/legend.js +252 -0
- package/dist/text/paint.d.ts +25 -0
- package/dist/text/paint.js +208 -0
- package/dist/text/render.d.ts +11 -0
- package/dist/text/render.js +28 -0
- package/dist/text/scene.d.ts +40 -0
- package/dist/text/scene.js +50 -0
- package/dist/text/vocabulary.d.ts +37 -0
- package/dist/text/vocabulary.js +211 -0
- package/dist/text/width.d.ts +15 -0
- package/dist/text/width.js +181 -0
- package/dist/tui/actions.d.ts +10 -0
- package/dist/tui/actions.js +297 -0
- package/dist/tui/app.d.ts +31 -0
- package/dist/tui/app.js +389 -0
- package/dist/tui/details.d.ts +3 -0
- package/dist/tui/details.js +54 -0
- package/dist/tui/keys.d.ts +33 -0
- package/dist/tui/keys.js +113 -0
- package/dist/tui/kitty.d.ts +36 -0
- package/dist/tui/kitty.js +65 -0
- package/dist/tui/legend.d.ts +14 -0
- package/dist/tui/legend.js +18 -0
- package/dist/tui/nav.d.ts +25 -0
- package/dist/tui/nav.js +79 -0
- package/dist/tui/overview.d.ts +46 -0
- package/dist/tui/overview.js +115 -0
- package/dist/tui/pixels.d.ts +28 -0
- package/dist/tui/pixels.js +80 -0
- package/dist/tui/screen.d.ts +44 -0
- package/dist/tui/screen.js +335 -0
- package/dist/tui/state.d.ts +100 -0
- package/dist/tui/state.js +72 -0
- package/dist/tui/terminal.d.ts +21 -0
- package/dist/tui/terminal.js +53 -0
- package/dist/watch.d.ts +2 -0
- package/dist/watch.js +1 -0
- package/package.json +3 -2
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
import ELK from 'elkjs/lib/elk.bundled.js';
|
|
2
|
+
import { DOWN, LEFT, RIGHT, UP } from './glyphs.js';
|
|
3
|
+
import { cellWidth } from './width.js';
|
|
4
|
+
/**
|
|
5
|
+
* elk `layered`, in character cells: a node is as big as its text, and the gaps
|
|
6
|
+
* are a few cells. elk's options do not inherit into nested graphs, and every one it
|
|
7
|
+
* is not told falls back to its pixel-sized default (10, 12, 20 …), which in cells
|
|
8
|
+
* opens tens of rows between the levels of a container — so this set goes on the
|
|
9
|
+
* root and on every container alike.
|
|
10
|
+
*/
|
|
11
|
+
export const CELL_OPTIONS = {
|
|
12
|
+
'elk.algorithm': 'layered',
|
|
13
|
+
'elk.edgeRouting': 'ORTHOGONAL',
|
|
14
|
+
'elk.spacing.nodeNode': '4',
|
|
15
|
+
'elk.layered.spacing.nodeNodeBetweenLayers': '2',
|
|
16
|
+
'elk.spacing.edgeNode': '2',
|
|
17
|
+
'elk.spacing.edgeEdge': '1',
|
|
18
|
+
'elk.layered.spacing.edgeNodeBetweenLayers': '1',
|
|
19
|
+
'elk.layered.spacing.edgeEdgeBetweenLayers': '1',
|
|
20
|
+
'elk.spacing.edgeLabel': '1',
|
|
21
|
+
'elk.spacing.labelLabel': '0',
|
|
22
|
+
'elk.spacing.labelNode': '0',
|
|
23
|
+
'elk.spacing.portPort': '1',
|
|
24
|
+
'elk.spacing.componentComponent': '4',
|
|
25
|
+
'elk.separateConnectedComponents': 'true',
|
|
26
|
+
};
|
|
27
|
+
/** Inside a container's frame: the title row and a blank one above the children, a
|
|
28
|
+
* row below, two columns either side. */
|
|
29
|
+
const CONTAINER_PADDING = '[top=2,left=2,bottom=1,right=2]';
|
|
30
|
+
/** The gaps for a flow. A cell is about twice as tall as it is wide, so a sideways
|
|
31
|
+
* flow's gap between neighbours, counted in rows, is half a downward one's, counted in
|
|
32
|
+
* columns, and its gap between levels twice: turned, the boxes keep their spacing. Only
|
|
33
|
+
* those two gaps are scaled. The clearances around lines are a cell or two already,
|
|
34
|
+
* near the least a grid of cells can hold, so they stay as written in every flow. */
|
|
35
|
+
function cellOptions(direction) {
|
|
36
|
+
if (direction === 'DOWN' || direction === 'UP')
|
|
37
|
+
return CELL_OPTIONS;
|
|
38
|
+
return { ...CELL_OPTIONS, 'elk.spacing.nodeNode': '2', 'elk.layered.spacing.nodeNodeBetweenLayers': '4' };
|
|
39
|
+
}
|
|
40
|
+
const elk = new ELK();
|
|
41
|
+
function elkNode(vn, boxes, direction) {
|
|
42
|
+
const box = boxes.get(vn.id);
|
|
43
|
+
const width = box?.width ?? 5;
|
|
44
|
+
const height = box?.height ?? 3;
|
|
45
|
+
if (vn.state !== 'expanded' || vn.children.length === 0)
|
|
46
|
+
return { id: vn.id, width, height };
|
|
47
|
+
// layered works left to right and turns a vertical flow afterwards, and the minimum
|
|
48
|
+
// size of a hierarchical node is read before the turn: under DOWN or UP it has to
|
|
49
|
+
// be given as (height, width) to come out as (width, height). Measured on elkjs
|
|
50
|
+
// 0.12 with INCLUDE_CHILDREN — layout.test.ts pins all four directions.
|
|
51
|
+
const vertical = direction === 'DOWN' || direction === 'UP';
|
|
52
|
+
return {
|
|
53
|
+
id: vn.id,
|
|
54
|
+
layoutOptions: {
|
|
55
|
+
...cellOptions(direction),
|
|
56
|
+
'elk.direction': direction,
|
|
57
|
+
'elk.padding': CONTAINER_PADDING,
|
|
58
|
+
'elk.nodeSize.constraints': 'MINIMUM_SIZE',
|
|
59
|
+
'elk.nodeSize.minimum': vertical ? `(${height}, ${width})` : `(${width}, ${height})`,
|
|
60
|
+
},
|
|
61
|
+
children: vn.children.map((c) => elkNode(c, boxes, direction)),
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
function elkEdges(view, labels) {
|
|
65
|
+
return view.layoutEdges.map((e) => {
|
|
66
|
+
const text = labels.get(e.id) ?? '';
|
|
67
|
+
return {
|
|
68
|
+
id: e.id,
|
|
69
|
+
sources: [e.from],
|
|
70
|
+
targets: [e.to],
|
|
71
|
+
...(text !== '' ? { labels: [{ text, width: cellWidth(text), height: 1 }] } : {}),
|
|
72
|
+
};
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
/** elk's tree, read back into absolute cells. A box's edges are rounded, not its
|
|
76
|
+
* size, so two boxes elk set side by side stay side by side after rounding. */
|
|
77
|
+
function collect(root) {
|
|
78
|
+
const out = { boxes: new Map(), origins: new Map(), edges: [] };
|
|
79
|
+
const walk = (n, ax, ay) => {
|
|
80
|
+
const x = ax + (n.x ?? 0);
|
|
81
|
+
const y = ay + (n.y ?? 0);
|
|
82
|
+
out.origins.set(n.id, { x, y });
|
|
83
|
+
if (n !== root) {
|
|
84
|
+
const left = Math.round(x);
|
|
85
|
+
const top = Math.round(y);
|
|
86
|
+
out.boxes.set(n.id, {
|
|
87
|
+
x: left,
|
|
88
|
+
y: top,
|
|
89
|
+
width: Math.round(x + (n.width ?? 0)) - left,
|
|
90
|
+
height: Math.round(y + (n.height ?? 0)) - top,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
for (const edge of (n.edges ?? []))
|
|
94
|
+
out.edges.push({ edge, owner: { x, y } });
|
|
95
|
+
n.children?.forEach((c) => walk(c, x, y));
|
|
96
|
+
};
|
|
97
|
+
walk(root, 0, 0);
|
|
98
|
+
return out;
|
|
99
|
+
}
|
|
100
|
+
const onBorder = (p, b) => {
|
|
101
|
+
const inX = p.x >= b.x && p.x < b.x + b.width;
|
|
102
|
+
const inY = p.y >= b.y && p.y < b.y + b.height;
|
|
103
|
+
return (inX && (p.y === b.y || p.y === b.y + b.height - 1)) || (inY && (p.x === b.x || p.x === b.x + b.width - 1));
|
|
104
|
+
};
|
|
105
|
+
/** An end that lands on its box's border, moved one cell towards where the route
|
|
106
|
+
* comes from. elk ends a route ON the box's outline; a cell is either border or not,
|
|
107
|
+
* and the border cell belongs to the box, so the end — the arrowhead — has to sit in
|
|
108
|
+
* the next cell out (or in, for a container's edge to something inside it). */
|
|
109
|
+
function offBorder(end, from, box) {
|
|
110
|
+
if (box === undefined || !onBorder(end, box))
|
|
111
|
+
return end;
|
|
112
|
+
if (from.x !== end.x)
|
|
113
|
+
return { x: end.x + Math.sign(from.x - end.x), y: end.y };
|
|
114
|
+
if (from.y !== end.y)
|
|
115
|
+
return { x: end.x, y: end.y + Math.sign(from.y - end.y) };
|
|
116
|
+
return end;
|
|
117
|
+
}
|
|
118
|
+
/** Where a box is from an end of a route that stops beside it: the direction (a
|
|
119
|
+
* glyph mask bit) to step from the end to reach its border; undefined when the end is
|
|
120
|
+
* not beside it. */
|
|
121
|
+
export function towardBox(end, g) {
|
|
122
|
+
const inX = end.x >= g.x && end.x < g.x + g.width;
|
|
123
|
+
const inY = end.y >= g.y && end.y < g.y + g.height;
|
|
124
|
+
if (inX && end.y === g.y + g.height)
|
|
125
|
+
return UP;
|
|
126
|
+
if (inX && end.y === g.y - 1)
|
|
127
|
+
return DOWN;
|
|
128
|
+
if (inY && end.x === g.x - 1)
|
|
129
|
+
return RIGHT;
|
|
130
|
+
if (inY && end.x === g.x + g.width)
|
|
131
|
+
return LEFT;
|
|
132
|
+
return undefined;
|
|
133
|
+
}
|
|
134
|
+
/** One cell's step in each direction. */
|
|
135
|
+
export const STEP = {
|
|
136
|
+
[UP]: { x: 0, y: -1 },
|
|
137
|
+
[DOWN]: { x: 0, y: 1 },
|
|
138
|
+
[LEFT]: { x: -1, y: 0 },
|
|
139
|
+
[RIGHT]: { x: 1, y: 0 },
|
|
140
|
+
};
|
|
141
|
+
const encloses = (outer, inner) => outer.x <= inner.x &&
|
|
142
|
+
outer.y <= inner.y &&
|
|
143
|
+
outer.x + outer.width >= inner.x + inner.width &&
|
|
144
|
+
outer.y + outer.height >= inner.y + inner.height;
|
|
145
|
+
const clamp = (v, low, high) => Math.max(low, Math.min(v, high));
|
|
146
|
+
/** Where an end goes: beside its box (`offBorder`) — unless the box is a container
|
|
147
|
+
* holding the other end, where it goes on the container's frame. Between a child and
|
|
148
|
+
* its own frame there is no cell to spare, and an end moved outside would draw the
|
|
149
|
+
* edge leaving the box it points into. */
|
|
150
|
+
function placeEnd(end, next, box, other) {
|
|
151
|
+
if (box === undefined || other === undefined || box === other || !encloses(box, other))
|
|
152
|
+
return offBorder(end, next, box);
|
|
153
|
+
return { x: clamp(end.x, box.x, box.x + box.width - 1), y: clamp(end.y, box.y, box.y + box.height - 1) };
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* A route squeezed into one cell: a child flush against its own frame, where the
|
|
157
|
+
* cell is the frame's. From the child, the route starts on the child's border, so
|
|
158
|
+
* the cell has a way in; into the child, the cell is both ends, beside the child.
|
|
159
|
+
* Anything else that folds to one cell is no route at all.
|
|
160
|
+
*/
|
|
161
|
+
function oneCell(cell, source, target) {
|
|
162
|
+
const out = source === undefined ? undefined : towardBox(cell, source);
|
|
163
|
+
if (out !== undefined)
|
|
164
|
+
return [{ x: cell.x + STEP[out].x, y: cell.y + STEP[out].y }, cell];
|
|
165
|
+
if (target !== undefined && towardBox(cell, target) !== undefined)
|
|
166
|
+
return [cell, cell];
|
|
167
|
+
return [cell];
|
|
168
|
+
}
|
|
169
|
+
const sameCell = (a, b) => a.x === b.x && a.y === b.y;
|
|
170
|
+
/** Where an end beside a corner of its box goes: beside the nearest cell of that side
|
|
171
|
+
* that is not a corner, so the arrow points at the box and not at its corner. An end
|
|
172
|
+
* beside the middle of a side, or not beside the box at all, stays. */
|
|
173
|
+
function besideSide(end, box) {
|
|
174
|
+
const toward = towardBox(end, box);
|
|
175
|
+
if (toward === LEFT || toward === RIGHT)
|
|
176
|
+
return { x: end.x, y: clamp(end.y, box.y + 1, box.y + box.height - 2) };
|
|
177
|
+
if (toward === UP || toward === DOWN)
|
|
178
|
+
return { x: clamp(end.x, box.x + 1, box.x + box.width - 2), y: end.y };
|
|
179
|
+
return end;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* The route with its last point beside the side of its box (`besideSide`). A box one
|
|
183
|
+
* line of text tall has one row between its corners, and on a sideways flow elk's ends
|
|
184
|
+
* miss it: a lone end, in the middle of the side, is half a cell into that row and
|
|
185
|
+
* rounds onto the border's, and several are spread over the whole side. Where the
|
|
186
|
+
* last segment meets the side square on it moves along
|
|
187
|
+
* with the end, staying straight; on a route of one segment that moves the other end
|
|
188
|
+
* too, so where that one would leave the middle of its own box's side the route jogs
|
|
189
|
+
* halfway instead.
|
|
190
|
+
*/
|
|
191
|
+
function slideEnd(points, box, other) {
|
|
192
|
+
const end = points.at(-1);
|
|
193
|
+
const before = points.at(-2);
|
|
194
|
+
const to = box === undefined ? end : besideSide(end, box);
|
|
195
|
+
if (sameCell(to, end))
|
|
196
|
+
return points;
|
|
197
|
+
const [dx, dy] = [to.x - end.x, to.y - end.y];
|
|
198
|
+
const squareOn = dx !== 0 ? before.x === end.x : before.y === end.y;
|
|
199
|
+
if (!squareOn)
|
|
200
|
+
return [...points.slice(0, -1), to];
|
|
201
|
+
const moved = { x: before.x + dx, y: before.y + dy };
|
|
202
|
+
const stays = points.length > 2 ||
|
|
203
|
+
other === undefined ||
|
|
204
|
+
(towardBox(moved, other) !== undefined && sameCell(besideSide(moved, other), moved));
|
|
205
|
+
if (stays)
|
|
206
|
+
return [...points.slice(0, -2), moved, to];
|
|
207
|
+
const half = dy !== 0 ? Math.floor((before.x + end.x) / 2) : Math.floor((before.y + end.y) / 2);
|
|
208
|
+
const jog = dy !== 0
|
|
209
|
+
? [
|
|
210
|
+
{ x: half, y: before.y },
|
|
211
|
+
{ x: half, y: to.y },
|
|
212
|
+
]
|
|
213
|
+
: [
|
|
214
|
+
{ x: before.x, y: half },
|
|
215
|
+
{ x: to.x, y: half },
|
|
216
|
+
];
|
|
217
|
+
return [before, ...jog, to];
|
|
218
|
+
}
|
|
219
|
+
/** Both ends of a route beside the sides of their boxes, not their corners. */
|
|
220
|
+
function offCorners(points, source, target) {
|
|
221
|
+
const ended = slideEnd(points, target, source);
|
|
222
|
+
return orthogonal(slideEnd([...ended].reverse(), source, target).reverse());
|
|
223
|
+
}
|
|
224
|
+
/** Points with repeats and straight-through middles dropped, and a corner put in
|
|
225
|
+
* wherever rounding left two consecutive points apart on both axes. */
|
|
226
|
+
function orthogonal(points) {
|
|
227
|
+
const out = [];
|
|
228
|
+
for (const p of points) {
|
|
229
|
+
const last = out.at(-1);
|
|
230
|
+
if (last !== undefined && last.x === p.x && last.y === p.y)
|
|
231
|
+
continue;
|
|
232
|
+
if (last !== undefined && last.x !== p.x && last.y !== p.y)
|
|
233
|
+
out.push({ x: p.x, y: last.y });
|
|
234
|
+
out.push(p);
|
|
235
|
+
}
|
|
236
|
+
return out.filter((p, i) => {
|
|
237
|
+
const [a, b] = [out[i - 1], out[i + 1]];
|
|
238
|
+
return a === undefined || b === undefined || !((a.x === p.x && p.x === b.x) || (a.y === p.y && p.y === b.y));
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
function routeOf(entry, laid, labels) {
|
|
242
|
+
const { edge, owner } = entry;
|
|
243
|
+
const section = edge.sections?.[0];
|
|
244
|
+
if (section === undefined)
|
|
245
|
+
return undefined;
|
|
246
|
+
// Under INCLUDE_CHILDREN elk states a route in the frame of the endpoints' lowest
|
|
247
|
+
// common ancestor (`container`), not of the node the edge was declared on; see the
|
|
248
|
+
// renderer's collectLaid for the long version.
|
|
249
|
+
const o = (edge.container !== undefined ? laid.origins.get(edge.container) : undefined) ?? owner;
|
|
250
|
+
const raw = [section.startPoint, ...(section.bendPoints ?? []), section.endPoint].map((p) => ({
|
|
251
|
+
x: Math.round(o.x + p.x),
|
|
252
|
+
y: Math.round(o.y + p.y),
|
|
253
|
+
}));
|
|
254
|
+
const points = orthogonal(raw);
|
|
255
|
+
if (points.length < 2)
|
|
256
|
+
return undefined;
|
|
257
|
+
const [source, target] = [laid.boxes.get(edge.sources[0]), laid.boxes.get(edge.targets[0])];
|
|
258
|
+
points[0] = placeEnd(points[0], points[1], source, target);
|
|
259
|
+
points[points.length - 1] = placeEnd(points.at(-1), points.at(-2), target, source);
|
|
260
|
+
let placed = orthogonal(points);
|
|
261
|
+
if (placed.length >= 2)
|
|
262
|
+
placed = offCorners(placed, source, target);
|
|
263
|
+
if (placed.length === 1)
|
|
264
|
+
placed = oneCell(placed[0], source, target);
|
|
265
|
+
if (placed.length < 2)
|
|
266
|
+
return undefined;
|
|
267
|
+
const text = labels.get(edge.id) ?? '';
|
|
268
|
+
const spot = edge.labels?.[0];
|
|
269
|
+
return {
|
|
270
|
+
points: placed,
|
|
271
|
+
...(text !== '' && spot?.x !== undefined && spot.y !== undefined
|
|
272
|
+
? { label: { x: Math.round(o.x + spot.x), y: Math.round(o.y + spot.y), text } }
|
|
273
|
+
: {}),
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
/** Shifts everything so nothing sits left of or above the origin, and measures the
|
|
277
|
+
* picture as what it holds. */
|
|
278
|
+
function normalize(boxes, routes) {
|
|
279
|
+
const xs = [];
|
|
280
|
+
const ys = [];
|
|
281
|
+
const span = (x0, x1, y0, y1) => {
|
|
282
|
+
xs.push(x0, x1);
|
|
283
|
+
ys.push(y0, y1);
|
|
284
|
+
};
|
|
285
|
+
for (const b of boxes.values())
|
|
286
|
+
span(b.x, b.x + b.width - 1, b.y, b.y + b.height - 1);
|
|
287
|
+
for (const r of routes.values()) {
|
|
288
|
+
for (const p of r.points)
|
|
289
|
+
span(p.x, p.x, p.y, p.y);
|
|
290
|
+
if (r.label !== undefined)
|
|
291
|
+
span(r.label.x, r.label.x + cellWidth(r.label.text) - 1, r.label.y, r.label.y);
|
|
292
|
+
}
|
|
293
|
+
if (xs.length === 0)
|
|
294
|
+
return { width: 0, height: 0, boxes, routes };
|
|
295
|
+
const dx = -Math.min(0, ...xs);
|
|
296
|
+
const dy = -Math.min(0, ...ys);
|
|
297
|
+
const shift = (p) => ({ ...p, x: p.x + dx, y: p.y + dy });
|
|
298
|
+
if (dx !== 0 || dy !== 0) {
|
|
299
|
+
for (const [id, b] of boxes)
|
|
300
|
+
boxes.set(id, shift(b));
|
|
301
|
+
for (const [id, r] of routes) {
|
|
302
|
+
routes.set(id, { points: r.points.map(shift), ...(r.label !== undefined ? { label: shift(r.label) } : {}) });
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
return { width: Math.max(...xs) + dx + 1, height: Math.max(...ys) + dy + 1, boxes, routes };
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Lays the view out in cells. Every one of `layoutEdges` takes part, drawn or not,
|
|
309
|
+
* so that turning a layer on or off never moves a box (the renderer's rule); the
|
|
310
|
+
* routes of the edges actually drawn are picked from the result by id.
|
|
311
|
+
*/
|
|
312
|
+
export async function layoutCells(view, boxes, direction, labels) {
|
|
313
|
+
if (view.roots.length === 0)
|
|
314
|
+
return { width: 0, height: 0, boxes: new Map(), routes: new Map() };
|
|
315
|
+
const graph = {
|
|
316
|
+
id: '__root',
|
|
317
|
+
layoutOptions: {
|
|
318
|
+
...cellOptions(direction),
|
|
319
|
+
'elk.direction': direction,
|
|
320
|
+
'elk.padding': '[top=0,left=0,bottom=0,right=0]',
|
|
321
|
+
'elk.hierarchyHandling': 'INCLUDE_CHILDREN',
|
|
322
|
+
},
|
|
323
|
+
children: view.roots.map((vn) => elkNode(vn, boxes, direction)),
|
|
324
|
+
edges: elkEdges(view, labels),
|
|
325
|
+
};
|
|
326
|
+
const laid = collect(await elk.layout(graph));
|
|
327
|
+
const routes = new Map();
|
|
328
|
+
for (const entry of laid.edges) {
|
|
329
|
+
const route = routeOf(entry, laid, labels);
|
|
330
|
+
if (route !== undefined)
|
|
331
|
+
routes.set(entry.edge.id, route);
|
|
332
|
+
}
|
|
333
|
+
return normalize(laid.boxes, routes);
|
|
334
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { type LegendPosition } from '@diagc/core/internal';
|
|
2
|
+
import { type Cell, type Style } from './canvas.js';
|
|
3
|
+
import { type Glyphs } from './glyphs.js';
|
|
4
|
+
import type { Scene } from './scene.js';
|
|
5
|
+
export interface LegendRow {
|
|
6
|
+
/** `${section}:${key}`, which an item that names the same kind or type recaptions */
|
|
7
|
+
id: string;
|
|
8
|
+
/** `SWATCH` cells */
|
|
9
|
+
swatch: string;
|
|
10
|
+
style: Style;
|
|
11
|
+
label: string;
|
|
12
|
+
/** a shape, a line or a mark that says nothing about itself on the canvas: one such
|
|
13
|
+
* row is what offers a legend on a diagram that declared none */
|
|
14
|
+
vocabulary?: true;
|
|
15
|
+
}
|
|
16
|
+
export interface TextLegend {
|
|
17
|
+
title: string;
|
|
18
|
+
position: LegendPosition;
|
|
19
|
+
/** the diagram asked for it; one it did not ask for is only offered (the TUI's `L`) */
|
|
20
|
+
declared: boolean;
|
|
21
|
+
/** the sections with rows, each under the browser's title for it */
|
|
22
|
+
sections: {
|
|
23
|
+
title: string;
|
|
24
|
+
rows: LegendRow[];
|
|
25
|
+
}[];
|
|
26
|
+
}
|
|
27
|
+
/** What the legend is read from: a scene, before its legend is added to it. */
|
|
28
|
+
type Keyed = Pick<Scene, 'input' | 'view' | 'nodes' | 'notation' | 'accents'>;
|
|
29
|
+
/** The legend of a scene: the one its diagram declares, or the one it is offered for
|
|
30
|
+
* what its picture cannot say; none when there is nothing to key. */
|
|
31
|
+
export declare function legendOf(scene: Keyed, glyphs: Glyphs): TextLegend | undefined;
|
|
32
|
+
/** The cells the framed legend needs: its lines with a border and a cell of padding
|
|
33
|
+
* each side, and its title in the top border. */
|
|
34
|
+
export declare function legendSize(legend: TextLegend): {
|
|
35
|
+
width: number;
|
|
36
|
+
height: number;
|
|
37
|
+
};
|
|
38
|
+
/** The legend framed, its title in the top border as an open container's is. In less
|
|
39
|
+
* room than it needs its labels are cut and its last line says how many are left out. */
|
|
40
|
+
export declare function legendCells(legend: TextLegend, glyphs: Glyphs, room?: {
|
|
41
|
+
width: number;
|
|
42
|
+
height: number;
|
|
43
|
+
}): Cell[][];
|
|
44
|
+
export {};
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import { buildHierarchy, presetLayers, relationLayer, relationOnPlane, scopeToRoot, } from '@diagc/core/internal';
|
|
2
|
+
import { Canvas } from './canvas.js';
|
|
3
|
+
import { typeMark } from './content.js';
|
|
4
|
+
import { DOWN, LEFT, RIGHT } from './glyphs.js';
|
|
5
|
+
import { accentCss, CORNER, rgb, WEIGHT } from './paint.js';
|
|
6
|
+
import { KIND_LOOKS, kindLook, TYPE_LOOKS, typeLook } from './vocabulary.js';
|
|
7
|
+
import { cellWidth, fitCells, printable } from './width.js';
|
|
8
|
+
/** A swatch's cells: three, the width of a line with a mark at each end. */
|
|
9
|
+
const SWATCH = 3;
|
|
10
|
+
const TITLES = {
|
|
11
|
+
layers: 'Layers',
|
|
12
|
+
kinds: 'Connections',
|
|
13
|
+
types: 'Elements',
|
|
14
|
+
marks: 'Marks',
|
|
15
|
+
items: 'Notes',
|
|
16
|
+
};
|
|
17
|
+
/** What a legend with no `show` derives; the types are keyed only where they need it. */
|
|
18
|
+
const DEFAULT_SECTIONS = ['layers', 'kinds', 'marks'];
|
|
19
|
+
const MARKS = { pk: ['PK', 'Primary key'], fk: ['FK', 'Foreign key column'] };
|
|
20
|
+
const DIM = { dim: true };
|
|
21
|
+
/** `text` in the middle of the swatch's cells. */
|
|
22
|
+
function centre(text) {
|
|
23
|
+
const left = Math.max(0, (SWATCH - cellWidth(text)) >> 1);
|
|
24
|
+
return `${' '.repeat(left)}${text}${' '.repeat(Math.max(0, SWATCH - left - cellWidth(text)))}`;
|
|
25
|
+
}
|
|
26
|
+
const colour = (css) => {
|
|
27
|
+
const fg = rgb(css);
|
|
28
|
+
return fg === undefined ? {} : { fg };
|
|
29
|
+
};
|
|
30
|
+
/** Each key with its value where every entry for it agrees, else undefined: a swatch
|
|
31
|
+
* takes a colour only when all it stands for are drawn in that colour. */
|
|
32
|
+
function agreed(entries) {
|
|
33
|
+
const out = new Map();
|
|
34
|
+
for (const [key, value] of entries) {
|
|
35
|
+
if (!out.has(key))
|
|
36
|
+
out.set(key, value);
|
|
37
|
+
else if (out.get(key) !== value)
|
|
38
|
+
out.set(key, undefined);
|
|
39
|
+
}
|
|
40
|
+
return out;
|
|
41
|
+
}
|
|
42
|
+
/** The vocabulary's own order first (an authored one), then unknown ids alphabetically:
|
|
43
|
+
* never the order things are met in, which would reshuffle the key at every fold. */
|
|
44
|
+
function ordered(ids, known) {
|
|
45
|
+
const rank = new Map(known.map((k, i) => [k, i]));
|
|
46
|
+
const at = (id) => rank.get(id) ?? known.length;
|
|
47
|
+
return [...ids].sort((a, b) => at(a) - at(b) || a.localeCompare(b));
|
|
48
|
+
}
|
|
49
|
+
/** A line as the picture draws one of the kind: its weight, then its ends' marks. */
|
|
50
|
+
function lineSwatch(look, glyphs) {
|
|
51
|
+
const line = glyphs.line(LEFT | RIGHT, WEIGHT[look.line]);
|
|
52
|
+
const end = (mark, toward) => mark === undefined || mark === 'none' ? line : mark === 'arrow' ? glyphs.arrow(toward) : glyphs.mark(mark);
|
|
53
|
+
return `${end(look.start, 'left')}${line}${end(look.end, 'right')}`;
|
|
54
|
+
}
|
|
55
|
+
/** A box of the type as the picture draws it: the mark its name follows where it has
|
|
56
|
+
* one, else the top of its border, which carries its corners and its dashes. */
|
|
57
|
+
function shapeSwatch(type, look, glyphs) {
|
|
58
|
+
const mark = typeMark(type, look.shape, glyphs);
|
|
59
|
+
if (mark !== undefined)
|
|
60
|
+
return centre(mark);
|
|
61
|
+
const weight = look.shape === 'bar' ? 'heavy' : look.dashed === true ? 'dashed' : 'light';
|
|
62
|
+
const corner = CORNER[look.shape];
|
|
63
|
+
return `${glyphs.line(DOWN | RIGHT, weight, corner)}${glyphs.line(LEFT | RIGHT, weight)}${glyphs.line(DOWN | LEFT, weight, corner)}`;
|
|
64
|
+
}
|
|
65
|
+
const vocabulary = (caption) => caption !== undefined ? { vocabulary: true } : {};
|
|
66
|
+
/** The layers something in this view carries, folded away or not: what the plane holds,
|
|
67
|
+
* or a drill's inside. The browser's rule (legendRows' `drawableSlice`), so a layer's row
|
|
68
|
+
* does not come and go as its boxes fold. */
|
|
69
|
+
function carriedLayers({ model, plane, root }) {
|
|
70
|
+
const hierarchy = buildHierarchy(model, plane);
|
|
71
|
+
const viewed = plane ?? model.planes[0]?.id;
|
|
72
|
+
const visible = new Set(hierarchy.childrenOf.keys());
|
|
73
|
+
const { nodes, relations } = root !== undefined && hierarchy.childrenOf.has(root)
|
|
74
|
+
? scopeToRoot(model, hierarchy, root).model
|
|
75
|
+
: {
|
|
76
|
+
nodes: model.nodes.filter((n) => visible.has(n.id)),
|
|
77
|
+
relations: model.relations.filter((r) => visible.has(r.from) && visible.has(r.to)),
|
|
78
|
+
};
|
|
79
|
+
const carried = new Set();
|
|
80
|
+
for (const n of nodes) {
|
|
81
|
+
if (n.layer !== undefined)
|
|
82
|
+
carried.add(n.layer);
|
|
83
|
+
for (const c of n.columns ?? [])
|
|
84
|
+
if (c.layer !== undefined)
|
|
85
|
+
carried.add(c.layer);
|
|
86
|
+
}
|
|
87
|
+
// the effective layer: one a `layerRules` entry puts a relation on counts as a tag does
|
|
88
|
+
for (const r of relations) {
|
|
89
|
+
const layer = relationOnPlane(r, viewed) ? relationLayer(model, r) : undefined;
|
|
90
|
+
if (layer !== undefined)
|
|
91
|
+
carried.add(layer);
|
|
92
|
+
}
|
|
93
|
+
return carried;
|
|
94
|
+
}
|
|
95
|
+
function layerRows(scene, glyphs) {
|
|
96
|
+
const { model, plane, layers } = scene.input;
|
|
97
|
+
const carried = carriedLayers(scene.input);
|
|
98
|
+
const on = new Set(layers ?? presetLayers(model.planes, plane));
|
|
99
|
+
return model.layers
|
|
100
|
+
.filter((l) => carried.has(l.id) && on.has(l.id))
|
|
101
|
+
.map((l) => ({
|
|
102
|
+
id: `layers:${l.id}`,
|
|
103
|
+
swatch: centre(glyphs.mark('square')),
|
|
104
|
+
style: colour(l.tint),
|
|
105
|
+
label: printable(l.name),
|
|
106
|
+
}));
|
|
107
|
+
}
|
|
108
|
+
function kindRows(scene, glyphs) {
|
|
109
|
+
// the layer's tint, not a relation's own colour: that is one arrow's, not the kind's
|
|
110
|
+
const inks = agreed(scene.view.edges.map((e) => [e.kind, e.tint]));
|
|
111
|
+
return ordered(inks.keys(), Object.keys(KIND_LOOKS)).map((kind) => {
|
|
112
|
+
const look = kindLook({ kind }, scene.notation);
|
|
113
|
+
return {
|
|
114
|
+
id: `kinds:${kind}`,
|
|
115
|
+
swatch: lineSwatch(look, glyphs),
|
|
116
|
+
style: colour(inks.get(kind)),
|
|
117
|
+
label: look.legend ?? printable(kind),
|
|
118
|
+
...vocabulary(look.legend),
|
|
119
|
+
};
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
function typeRows(scene, glyphs) {
|
|
123
|
+
const { model } = scene.input;
|
|
124
|
+
const accents = agreed([...scene.nodes.values()].flatMap((vn) => vn.node.type === undefined
|
|
125
|
+
? []
|
|
126
|
+
: [[vn.node.type, accentCss(model, vn.node, scene.notation, scene.accents)]]));
|
|
127
|
+
return ordered(accents.keys(), Object.keys(TYPE_LOOKS)).map((type) => {
|
|
128
|
+
const look = typeLook(type, scene.notation);
|
|
129
|
+
// `label: ''` keeps a glyph type's box free of a subtitle; it is not a caption
|
|
130
|
+
const label = look.legend ?? (look.label === undefined || look.label === '' ? printable(type) : look.label);
|
|
131
|
+
return {
|
|
132
|
+
id: `types:${type}`,
|
|
133
|
+
swatch: shapeSwatch(type, look, glyphs),
|
|
134
|
+
style: colour(accents.get(type)),
|
|
135
|
+
label,
|
|
136
|
+
...vocabulary(look.legend),
|
|
137
|
+
};
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
/** A table's key columns, which the picture tags `PK` and `FK`. */
|
|
141
|
+
function markRows(scene) {
|
|
142
|
+
const seen = new Set();
|
|
143
|
+
for (const vn of scene.nodes.values()) {
|
|
144
|
+
if (typeLook(vn.node.type, scene.notation).shape !== 'table')
|
|
145
|
+
continue;
|
|
146
|
+
for (const c of vn.columns ?? vn.node.columns ?? []) {
|
|
147
|
+
if (c.pk === true)
|
|
148
|
+
seen.add('pk');
|
|
149
|
+
else if (c.fk === true)
|
|
150
|
+
seen.add('fk');
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return Object.keys(MARKS)
|
|
154
|
+
.filter((m) => seen.has(m))
|
|
155
|
+
.map((m) => ({ id: `marks:${m}`, swatch: centre(MARKS[m][0]), style: {}, label: MARKS[m][1], vocabulary: true }));
|
|
156
|
+
}
|
|
157
|
+
/** The hand-written rows. One naming a kind or a type already keyed recaptions that row
|
|
158
|
+
* in place, keeping its swatch; the rest are rows of their own. */
|
|
159
|
+
function itemRows(items, derived, notation, glyphs) {
|
|
160
|
+
return items.flatMap((item, i) => {
|
|
161
|
+
const label = printable(item.label);
|
|
162
|
+
const target = item.kind !== undefined ? `kinds:${item.kind}` : item.type !== undefined ? `types:${item.type}` : '';
|
|
163
|
+
const hit = derived.find((r) => r.id === target);
|
|
164
|
+
if (hit !== undefined) {
|
|
165
|
+
hit.label = label;
|
|
166
|
+
if (item.color !== undefined)
|
|
167
|
+
hit.style = colour(item.color);
|
|
168
|
+
return [];
|
|
169
|
+
}
|
|
170
|
+
const swatch = item.kind !== undefined
|
|
171
|
+
? lineSwatch(kindLook({ kind: item.kind }, notation), glyphs)
|
|
172
|
+
: item.type !== undefined
|
|
173
|
+
? shapeSwatch(item.type, typeLook(item.type, notation), glyphs)
|
|
174
|
+
: item.color !== undefined
|
|
175
|
+
? centre(glyphs.mark('square'))
|
|
176
|
+
: centre('');
|
|
177
|
+
return [{ id: `items:${i}`, swatch, style: colour(item.color), label }];
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
/** The legend of a scene: the one its diagram declares, or the one it is offered for
|
|
181
|
+
* what its picture cannot say; none when there is nothing to key. */
|
|
182
|
+
export function legendOf(scene, glyphs) {
|
|
183
|
+
const config = scene.input.model.legend ?? {};
|
|
184
|
+
const shown = config.show ?? DEFAULT_SECTIONS;
|
|
185
|
+
// As the browser: an explicit `show` to the letter; without one, types only where the
|
|
186
|
+
// box says nothing of its own — elsewhere a box prints its type and the key repeats it.
|
|
187
|
+
const types = shown.includes('types')
|
|
188
|
+
? typeRows(scene, glyphs)
|
|
189
|
+
: config.show === undefined
|
|
190
|
+
? typeRows(scene, glyphs).filter((r) => r.vocabulary === true)
|
|
191
|
+
: [];
|
|
192
|
+
const derived = {
|
|
193
|
+
layers: shown.includes('layers') ? layerRows(scene, glyphs) : [],
|
|
194
|
+
kinds: shown.includes('kinds') ? kindRows(scene, glyphs) : [],
|
|
195
|
+
types,
|
|
196
|
+
marks: shown.includes('marks') ? markRows(scene) : [],
|
|
197
|
+
};
|
|
198
|
+
const items = itemRows(config.items ?? [], [...derived.kinds, ...derived.types], scene.notation, glyphs);
|
|
199
|
+
const sections = ['layers', 'kinds', 'types', 'marks', 'items']
|
|
200
|
+
.map((s) => ({ title: TITLES[s], rows: s === 'items' ? items : derived[s] }))
|
|
201
|
+
.filter((s) => s.rows.length > 0);
|
|
202
|
+
const declared = scene.input.model.legend !== undefined;
|
|
203
|
+
if (sections.length === 0)
|
|
204
|
+
return undefined;
|
|
205
|
+
if (!declared && !sections.some((s) => s.rows.some((r) => r.vocabulary === true)))
|
|
206
|
+
return undefined;
|
|
207
|
+
return {
|
|
208
|
+
title: printable(config.title ?? 'Legend'),
|
|
209
|
+
position: config.position ?? 'bottom-right',
|
|
210
|
+
declared,
|
|
211
|
+
sections,
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
const linesOf = (legend) => legend.sections.flatMap((s) => [
|
|
215
|
+
{ text: s.title, style: DIM },
|
|
216
|
+
...s.rows.map((r) => ({ swatch: { text: r.swatch, style: r.style }, text: r.label, style: {} })),
|
|
217
|
+
]);
|
|
218
|
+
const lineWidth = (line) => (line.swatch === undefined ? 0 : SWATCH + 1) + cellWidth(line.text);
|
|
219
|
+
/** The cells the framed legend needs: its lines with a border and a cell of padding
|
|
220
|
+
* each side, and its title in the top border. */
|
|
221
|
+
export function legendSize(legend) {
|
|
222
|
+
const lines = linesOf(legend);
|
|
223
|
+
return {
|
|
224
|
+
width: Math.max(Math.max(...lines.map(lineWidth)) + 4, cellWidth(legend.title) + 6),
|
|
225
|
+
height: lines.length + 2,
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
/** The legend framed, its title in the top border as an open container's is. In less
|
|
229
|
+
* room than it needs its labels are cut and its last line says how many are left out. */
|
|
230
|
+
export function legendCells(legend, glyphs, room = legendSize(legend)) {
|
|
231
|
+
const natural = legendSize(legend);
|
|
232
|
+
const width = Math.min(natural.width, room.width);
|
|
233
|
+
const height = Math.min(natural.height, room.height);
|
|
234
|
+
const canvas = new Canvas(width, height);
|
|
235
|
+
canvas.rect({ x: 0, y: 0, width, height }, 'light', DIM);
|
|
236
|
+
canvas.text({ x: 2, y: 0 }, ` ${legend.title} `, { bold: true }, width - 2);
|
|
237
|
+
const lines = linesOf(legend);
|
|
238
|
+
const inside = Math.max(0, height - 2);
|
|
239
|
+
const shown = lines.length <= inside
|
|
240
|
+
? lines
|
|
241
|
+
: [...lines.slice(0, inside - 1), { text: `${glyphs.ellipsis} ${lines.length - inside + 1} more`, style: DIM }];
|
|
242
|
+
const maxX = width - 2;
|
|
243
|
+
shown.forEach((line, i) => {
|
|
244
|
+
let x = 2;
|
|
245
|
+
if (line.swatch !== undefined) {
|
|
246
|
+
canvas.text({ x, y: i + 1 }, line.swatch.text, line.swatch.style, maxX);
|
|
247
|
+
x += SWATCH + 1;
|
|
248
|
+
}
|
|
249
|
+
canvas.text({ x, y: i + 1 }, fitCells(line.text, maxX - x, glyphs.ellipsis), line.style, maxX);
|
|
250
|
+
});
|
|
251
|
+
return canvas.cells(glyphs);
|
|
252
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { DiagramModel, DiagramNode, NotationId } from '@diagc/core/internal';
|
|
2
|
+
import type { Accents } from './accents.js';
|
|
3
|
+
import { Canvas, type Color } from './canvas.js';
|
|
4
|
+
import { type Corner, type Glyphs, type Weight } from './glyphs.js';
|
|
5
|
+
import type { Scene } from './scene.js';
|
|
6
|
+
import { type LineStyle, type TextShape } from './vocabulary.js';
|
|
7
|
+
export interface PaintOptions {
|
|
8
|
+
glyphs: Glyphs;
|
|
9
|
+
/** the box the TUI has selected: drawn heavy, its edges bold, every other edge dim */
|
|
10
|
+
selected?: string;
|
|
11
|
+
}
|
|
12
|
+
export declare const WEIGHT: Record<LineStyle, Weight>;
|
|
13
|
+
export declare const CORNER: Partial<Record<TextShape, Corner>>;
|
|
14
|
+
export declare const rgb: (css: string | undefined) => Color | undefined;
|
|
15
|
+
/** A node's accent, as the renderer resolves it: its own colour, the one its notation
|
|
16
|
+
* gives it (a lane's, a bone's), the model's colour for its type (or for every type),
|
|
17
|
+
* then the notation's for the type. */
|
|
18
|
+
export declare function accentCss(model: DiagramModel, node: DiagramNode, notation: NotationId | undefined, accents: Accents): string | undefined;
|
|
19
|
+
/**
|
|
20
|
+
* The scene as a canvas, in layers: open containers' frames from the outside in,
|
|
21
|
+
* then the edges' lines, then the boxes over them (a box clears what is under it)
|
|
22
|
+
* and the frames' titles, then edge labels and the ends of the edges, last so
|
|
23
|
+
* nothing covers an arrowhead.
|
|
24
|
+
*/
|
|
25
|
+
export declare function paint(scene: Scene, options: PaintOptions): Canvas;
|