@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,208 @@
|
|
|
1
|
+
import { parseColor } from './ansi.js';
|
|
2
|
+
import { Canvas } from './canvas.js';
|
|
3
|
+
import { DOWN, LEFT, RIGHT, UP } from './glyphs.js';
|
|
4
|
+
import { STEP, towardBox } from './layout.js';
|
|
5
|
+
import { kindLook, typeLook } from './vocabulary.js';
|
|
6
|
+
import { cellWidth } from './width.js';
|
|
7
|
+
export const WEIGHT = { solid: 'light', dashed: 'dashed', heavy: 'heavy' };
|
|
8
|
+
export const CORNER = {
|
|
9
|
+
rounded: 'round',
|
|
10
|
+
cylinder: 'round',
|
|
11
|
+
dot: 'round',
|
|
12
|
+
hexagon: 'chamfer',
|
|
13
|
+
diamond: 'chamfer',
|
|
14
|
+
};
|
|
15
|
+
const LINE_STYLE = {
|
|
16
|
+
title: { bold: true },
|
|
17
|
+
subtitle: { dim: true },
|
|
18
|
+
note: { dim: true },
|
|
19
|
+
row: {},
|
|
20
|
+
};
|
|
21
|
+
export const rgb = (css) => {
|
|
22
|
+
const parsed = css === undefined ? undefined : parseColor(css);
|
|
23
|
+
return parsed === undefined ? undefined : { rgb: parsed };
|
|
24
|
+
};
|
|
25
|
+
/** A node's accent, as the renderer resolves it: its own colour, the one its notation
|
|
26
|
+
* gives it (a lane's, a bone's), the model's colour for its type (or for every type),
|
|
27
|
+
* then the notation's for the type. */
|
|
28
|
+
export function accentCss(model, node, notation, accents) {
|
|
29
|
+
const typeColor = node.type !== undefined ? model.typeColors?.[node.type] : undefined;
|
|
30
|
+
const byNotation = accents.nodes.get(node.id);
|
|
31
|
+
return node.color ?? byNotation ?? typeColor ?? model.typeColors?.['*'] ?? typeLook(node.type, notation).accent;
|
|
32
|
+
}
|
|
33
|
+
/** How a box's border is drawn: heavy when selected (and for a fork bar), dashed
|
|
34
|
+
* for a boundary, in the node's accent. */
|
|
35
|
+
function borderInk(p, id) {
|
|
36
|
+
const box = p.scene.boxes.get(id);
|
|
37
|
+
const fg = rgb(accentCss(p.scene.input.model, p.scene.nodes.get(id).node, p.scene.notation, p.scene.accents));
|
|
38
|
+
const weight = id === p.selected || box.shape === 'bar' ? 'heavy' : box.dashed ? 'dashed' : 'light';
|
|
39
|
+
return { weight, style: fg !== undefined ? { fg } : {} };
|
|
40
|
+
}
|
|
41
|
+
/** An open container's frame. Its title is written later (`drawTitle`). */
|
|
42
|
+
function drawFrame(p, id, g) {
|
|
43
|
+
const ink = borderInk(p, id);
|
|
44
|
+
p.canvas.rect(g, ink.weight, ink.style, CORNER[p.scene.boxes.get(id).shape]);
|
|
45
|
+
}
|
|
46
|
+
/** An open container's title, in its top border. Written after the edges: a line that
|
|
47
|
+
* crosses the border there loses a cell, where the other way round the title would
|
|
48
|
+
* lose a letter. */
|
|
49
|
+
function drawTitle(p, id, g) {
|
|
50
|
+
const title = p.scene.boxes.get(id).lines[0]?.text ?? '';
|
|
51
|
+
const style = { bold: true, inverse: id === p.selected };
|
|
52
|
+
if (title !== '')
|
|
53
|
+
p.canvas.text({ x: g.x + 2, y: g.y }, ` ${title} `, style, g.x + g.width - 2);
|
|
54
|
+
}
|
|
55
|
+
/** A leaf or folded box: border, shape details, and its lines (centred, but a table's
|
|
56
|
+
* rows left-aligned so their columns line up). `weight` overrides the border's. */
|
|
57
|
+
function drawBox(p, id, g, weight) {
|
|
58
|
+
const box = p.scene.boxes.get(id);
|
|
59
|
+
const ink = { ...borderInk(p, id), ...(weight !== undefined ? { weight } : {}) };
|
|
60
|
+
const rule = (y) => p.canvas.segment({ x: g.x, y }, { x: g.x + g.width - 1, y }, ink.weight, ink.style);
|
|
61
|
+
p.canvas.clear({ x: g.x + 1, y: g.y + 1, width: g.width - 2, height: g.height - 2 });
|
|
62
|
+
p.canvas.rect(g, ink.weight, ink.style, CORNER[box.shape]);
|
|
63
|
+
let y = g.y + 1;
|
|
64
|
+
if (box.shape === 'cylinder')
|
|
65
|
+
rule(y++);
|
|
66
|
+
let ruled = false;
|
|
67
|
+
for (const line of box.lines) {
|
|
68
|
+
if (line.role === 'row' && !ruled) {
|
|
69
|
+
rule(y++);
|
|
70
|
+
ruled = true;
|
|
71
|
+
}
|
|
72
|
+
const x = line.role === 'row' ? g.x + 2 : g.x + Math.floor((g.width - cellWidth(line.text)) / 2);
|
|
73
|
+
const style = line.role === 'title' && id === p.selected ? { bold: true, inverse: true } : LINE_STYLE[line.role];
|
|
74
|
+
p.canvas.text({ x, y: y++ }, line.text, style, g.x + g.width - 1);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function edgeInk(p, edge) {
|
|
78
|
+
const weight = WEIGHT[kindLook(edge, p.scene.notation).line];
|
|
79
|
+
const fg = rgb(edge.style?.color ?? edge.tint ?? p.scene.accents.edges.get(edge.id));
|
|
80
|
+
const base = fg !== undefined ? { fg } : {};
|
|
81
|
+
if (p.selected === undefined)
|
|
82
|
+
return { weight, style: base };
|
|
83
|
+
const mine = edge.from === p.selected || edge.to === p.selected;
|
|
84
|
+
return { weight, style: { ...base, ...(mine ? { bold: true } : { dim: true }) } };
|
|
85
|
+
}
|
|
86
|
+
const OPPOSITE = { [UP]: DOWN, [DOWN]: UP, [LEFT]: RIGHT, [RIGHT]: LEFT };
|
|
87
|
+
const ARROW = { [UP]: 'up', [DOWN]: 'down', [LEFT]: 'left', [RIGHT]: 'right' };
|
|
88
|
+
/** Runs the line from an end into the border beside it, so the border shows a
|
|
89
|
+
* junction (`┬`, `├`, …) where the line leaves or meets the box. */
|
|
90
|
+
function join(p, end, g, ink) {
|
|
91
|
+
const dir = g === undefined ? undefined : towardBox(end, g);
|
|
92
|
+
if (dir === undefined)
|
|
93
|
+
return;
|
|
94
|
+
p.canvas.stroke(end, dir, ink.weight, ink.style);
|
|
95
|
+
const step = STEP[dir];
|
|
96
|
+
p.canvas.stroke({ x: end.x + step.x, y: end.y + step.y }, OPPOSITE[dir], 'light');
|
|
97
|
+
}
|
|
98
|
+
/** Which way a mark at an end points: at the box beside it. Not along the last
|
|
99
|
+
* segment, which can run alongside the box — elk's last stub into a box is often under
|
|
100
|
+
* a cell, and gone once the end is moved off the border. */
|
|
101
|
+
function arrowOf(end, prev, box) {
|
|
102
|
+
const toward = box === undefined ? undefined : towardBox(end, box);
|
|
103
|
+
if (toward !== undefined)
|
|
104
|
+
return ARROW[toward];
|
|
105
|
+
if (end.y !== prev.y)
|
|
106
|
+
return end.y > prev.y ? 'down' : 'up';
|
|
107
|
+
return end.x > prev.x ? 'right' : 'left';
|
|
108
|
+
}
|
|
109
|
+
/** One end of an edge: its mark, or with none the line run into the border. */
|
|
110
|
+
function drawEnd(p, edge, end) {
|
|
111
|
+
const points = p.scene.layout.routes.get(edge.id).points;
|
|
112
|
+
if (points.length < 2)
|
|
113
|
+
return;
|
|
114
|
+
const look = kindLook(edge, p.scene.notation);
|
|
115
|
+
const [at, prev] = end === 'start' ? [points[0], points[1]] : [points.at(-1), points.at(-2)];
|
|
116
|
+
const box = p.scene.layout.boxes.get(end === 'start' ? edge.from : edge.to);
|
|
117
|
+
const mark = end === 'start' ? (look.start ?? 'none') : look.end;
|
|
118
|
+
const ink = edgeInk(p, edge);
|
|
119
|
+
if (mark === 'none')
|
|
120
|
+
join(p, at, box, ink);
|
|
121
|
+
else if (mark === 'arrow')
|
|
122
|
+
p.canvas.put(at, p.glyphs.arrow(arrowOf(at, prev, box)), ink.style);
|
|
123
|
+
else
|
|
124
|
+
p.canvas.put(at, p.glyphs.mark(mark), ink.style);
|
|
125
|
+
}
|
|
126
|
+
const isFrame = (scene, id) => {
|
|
127
|
+
const vn = scene.nodes.get(id);
|
|
128
|
+
return vn.state === 'expanded' && vn.children.length > 0;
|
|
129
|
+
};
|
|
130
|
+
/** A route of a fish: its straight runs as lines, its slants one character a cell, up
|
|
131
|
+
* to the cell it ends on, which is the line it meets. */
|
|
132
|
+
function drawFishRoute(p, points, ink) {
|
|
133
|
+
for (let i = 1; i < points.length; i++) {
|
|
134
|
+
const [a, b] = [points[i - 1], points[i]];
|
|
135
|
+
if (a.x === b.x || a.y === b.y) {
|
|
136
|
+
p.canvas.segment(a, b, ink.weight, ink.style);
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
const [dx, dy] = [Math.sign(b.x - a.x), Math.sign(b.y - a.y)];
|
|
140
|
+
const slant = p.glyphs.slant(dx === dy);
|
|
141
|
+
for (let x = a.x, y = a.y; x !== b.x; x += dx, y += dy)
|
|
142
|
+
p.canvas.put({ x, y }, slant, ink.style);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* A fishbone as the fish (fish.ts): the lines first, then the head's box, heavy, and
|
|
147
|
+
* the spine run into its border, then the words, a bone's label bold in the bone's
|
|
148
|
+
* colour, and last the boxes of what is not on the fish. A word is selected as a title
|
|
149
|
+
* is, in reverse.
|
|
150
|
+
*/
|
|
151
|
+
function paintFish(p, fish, edges) {
|
|
152
|
+
const { layout } = p.scene;
|
|
153
|
+
for (const edge of edges)
|
|
154
|
+
drawFishRoute(p, layout.routes.get(edge.id).points, edgeInk(p, edge));
|
|
155
|
+
drawBox(p, fish.head, layout.boxes.get(fish.head), 'heavy');
|
|
156
|
+
const { y, from, to } = fish.spine;
|
|
157
|
+
p.canvas.segment({ x: from, y }, { x: to, y }, 'heavy', borderInk(p, fish.head).style);
|
|
158
|
+
for (const [id, lines] of fish.words) {
|
|
159
|
+
const label = fish.labels.has(id);
|
|
160
|
+
const style = {
|
|
161
|
+
...(label ? { ...borderInk(p, id).style, bold: true } : {}),
|
|
162
|
+
...(id === p.selected ? { bold: true, inverse: true } : {}),
|
|
163
|
+
};
|
|
164
|
+
for (const line of lines)
|
|
165
|
+
p.canvas.text(line, line.text, style);
|
|
166
|
+
}
|
|
167
|
+
for (const [id, g] of layout.boxes)
|
|
168
|
+
if (id !== fish.head && !fish.words.has(id))
|
|
169
|
+
drawBox(p, id, g);
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* The scene as a canvas, in layers: open containers' frames from the outside in,
|
|
173
|
+
* then the edges' lines, then the boxes over them (a box clears what is under it)
|
|
174
|
+
* and the frames' titles, then edge labels and the ends of the edges, last so
|
|
175
|
+
* nothing covers an arrowhead.
|
|
176
|
+
*/
|
|
177
|
+
export function paint(scene, options) {
|
|
178
|
+
const { layout } = scene;
|
|
179
|
+
const p = { canvas: new Canvas(layout.width, layout.height), scene, ...options };
|
|
180
|
+
const drawn = [...scene.nodes.keys()].flatMap((id) => {
|
|
181
|
+
const g = layout.boxes.get(id);
|
|
182
|
+
return g === undefined ? [] : [{ id, g, frame: isFrame(scene, id) }];
|
|
183
|
+
});
|
|
184
|
+
const edges = scene.view.edges.filter((e) => layout.routes.has(e.id));
|
|
185
|
+
if (layout.fish !== undefined) {
|
|
186
|
+
paintFish(p, layout.fish, edges);
|
|
187
|
+
return p.canvas;
|
|
188
|
+
}
|
|
189
|
+
for (const { id, g, frame } of drawn)
|
|
190
|
+
if (frame)
|
|
191
|
+
drawFrame(p, id, g);
|
|
192
|
+
for (const edge of edges) {
|
|
193
|
+
const { points } = layout.routes.get(edge.id);
|
|
194
|
+
const ink = edgeInk(p, edge);
|
|
195
|
+
for (let i = 1; i < points.length; i++)
|
|
196
|
+
p.canvas.segment(points[i - 1], points[i], ink.weight, ink.style);
|
|
197
|
+
}
|
|
198
|
+
for (const { id, g, frame } of drawn)
|
|
199
|
+
(frame ? drawTitle : drawBox)(p, id, g);
|
|
200
|
+
for (const edge of edges) {
|
|
201
|
+
const label = layout.routes.get(edge.id).label;
|
|
202
|
+
if (label !== undefined)
|
|
203
|
+
p.canvas.text(label, label.text, edgeInk(p, edge).style);
|
|
204
|
+
drawEnd(p, edge, 'start');
|
|
205
|
+
drawEnd(p, edge, 'end');
|
|
206
|
+
}
|
|
207
|
+
return p.canvas;
|
|
208
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type ColorMode } from './ansi.js';
|
|
2
|
+
import { type SceneInput } from './scene.js';
|
|
3
|
+
export interface TextOptions {
|
|
4
|
+
/** plain ASCII in place of box-drawing characters */
|
|
5
|
+
ascii: boolean;
|
|
6
|
+
color: ColorMode;
|
|
7
|
+
}
|
|
8
|
+
/** A diagram's view as lines of text, with the legend it declares; none when the view
|
|
9
|
+
* has nothing in it. A legend only offered (for shapes that say nothing of themselves)
|
|
10
|
+
* is left out, as the published picture leaves it out. */
|
|
11
|
+
export declare function renderToLines(input: SceneInput, options: TextOptions): Promise<string[]>;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { serialize } from './ansi.js';
|
|
2
|
+
import { ASCII, UNICODE } from './glyphs.js';
|
|
3
|
+
import { legendCells } from './legend.js';
|
|
4
|
+
import { paint } from './paint.js';
|
|
5
|
+
import { buildScene } from './scene.js';
|
|
6
|
+
/** The picture with its legend beside it, as the corner it names would have it: over
|
|
7
|
+
* or under the picture with a blank row between, against its left or right edge. Not
|
|
8
|
+
* over the picture, as on a canvas, where a printed picture would lose what it covers.
|
|
9
|
+
* A legend wider than the picture starts at the left edge either way: set against the
|
|
10
|
+
* right one, it would push the picture along for the sake of its key. */
|
|
11
|
+
function withLegend(picture, legend, position) {
|
|
12
|
+
const indent = position.endsWith('right') ? Math.max(0, (picture[0]?.length ?? 0) - (legend[0]?.length ?? 0)) : 0;
|
|
13
|
+
const placed = legend.map((row) => [...Array.from({ length: indent }, () => ({ ch: ' ', style: {} })), ...row]);
|
|
14
|
+
return position.startsWith('top') ? [...placed, [], ...picture] : [...picture, [], ...placed];
|
|
15
|
+
}
|
|
16
|
+
/** A diagram's view as lines of text, with the legend it declares; none when the view
|
|
17
|
+
* has nothing in it. A legend only offered (for shapes that say nothing of themselves)
|
|
18
|
+
* is left out, as the published picture leaves it out. */
|
|
19
|
+
export async function renderToLines(input, options) {
|
|
20
|
+
const glyphs = options.ascii ? ASCII : UNICODE;
|
|
21
|
+
const scene = await buildScene(input, glyphs);
|
|
22
|
+
if (scene.layout.boxes.size === 0)
|
|
23
|
+
return [];
|
|
24
|
+
const picture = paint(scene, { glyphs }).cells(glyphs);
|
|
25
|
+
const legend = scene.legend?.declared === true ? scene.legend : undefined;
|
|
26
|
+
const cells = legend === undefined ? picture : withLegend(picture, legendCells(legend, glyphs), legend.position);
|
|
27
|
+
return serialize(cells, options.color);
|
|
28
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { type CompiledView, type DiagramModel, type HierarchyIndex, type LayoutOverlay, type NotationId, type ViewNode } from '@diagc/core/internal';
|
|
2
|
+
import { type Accents } from './accents.js';
|
|
3
|
+
import { type BoxText } from './content.js';
|
|
4
|
+
import { type Pins } from './folds.js';
|
|
5
|
+
import type { Glyphs } from './glyphs.js';
|
|
6
|
+
import { type TextLayout } from './layout.js';
|
|
7
|
+
import { type TextLegend } from './legend.js';
|
|
8
|
+
/** What a text picture is a picture of. */
|
|
9
|
+
export interface SceneInput {
|
|
10
|
+
model: DiagramModel;
|
|
11
|
+
overlay?: LayoutOverlay;
|
|
12
|
+
/** absent = the model's first plane */
|
|
13
|
+
plane?: string;
|
|
14
|
+
/** the layers drawn; absent = the plane's presets (compileView's own rule) */
|
|
15
|
+
layers?: string[];
|
|
16
|
+
pins: Pins;
|
|
17
|
+
/** draw this container's interior only (the drill view) */
|
|
18
|
+
root?: string;
|
|
19
|
+
}
|
|
20
|
+
/** A laid-out view: everything painting and the TUI's navigation read. */
|
|
21
|
+
export interface Scene {
|
|
22
|
+
input: SceneInput;
|
|
23
|
+
/** the compiled view, its `edges` cut to the ones the notation draws */
|
|
24
|
+
view: CompiledView;
|
|
25
|
+
layout: TextLayout;
|
|
26
|
+
boxes: Map<string, BoxText>;
|
|
27
|
+
/** every drawn node by id, in drawing order (parents before children) */
|
|
28
|
+
nodes: Map<string, ViewNode>;
|
|
29
|
+
/** the box each drawn node is drawn inside; undefined at the top level */
|
|
30
|
+
parentOf: Map<string, string | undefined>;
|
|
31
|
+
/** the plane's containment, over the nodes the active layers show */
|
|
32
|
+
hierarchy: HierarchyIndex;
|
|
33
|
+
notation?: NotationId;
|
|
34
|
+
/** the colours the notation gives boxes and lines that set none */
|
|
35
|
+
accents: Accents;
|
|
36
|
+
/** the key to what is drawn: declared by the diagram, or offered for what its
|
|
37
|
+
* shapes cannot say; absent when there is nothing to key */
|
|
38
|
+
legend?: TextLegend;
|
|
39
|
+
}
|
|
40
|
+
export declare function buildScene(input: SceneInput, glyphs: Glyphs): Promise<Scene>;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { activeNotation, buildHierarchy, compileView, defined, defaultLayoutDirection, FISHBONE_NOTATION, layoutPlaneKey, presetLayers, } from '@diagc/core/internal';
|
|
2
|
+
import { notationAccents } from './accents.js';
|
|
3
|
+
import { boxText, edgeText } from './content.js';
|
|
4
|
+
import { fishCells } from './fish.js';
|
|
5
|
+
import { withNeverFolds } from './folds.js';
|
|
6
|
+
import { layoutCells } from './layout.js';
|
|
7
|
+
import { legendOf } from './legend.js';
|
|
8
|
+
import { hidesKind } from './vocabulary.js';
|
|
9
|
+
const DIRECTIONS = new Set(['DOWN', 'UP', 'LEFT', 'RIGHT']);
|
|
10
|
+
/** The way the plane flows: its saved layout setting, else the model's default. */
|
|
11
|
+
function directionOf(input) {
|
|
12
|
+
const saved = input.overlay?.settings?.[layoutPlaneKey(input.model, input.plane)]?.direction;
|
|
13
|
+
return saved !== undefined && DIRECTIONS.has(saved) ? saved : defaultLayoutDirection(input.model);
|
|
14
|
+
}
|
|
15
|
+
export async function buildScene(input, glyphs) {
|
|
16
|
+
const { model, plane } = input;
|
|
17
|
+
const notation = activeNotation(model.planes, plane, model.notation);
|
|
18
|
+
const compiled = compileView(model, {
|
|
19
|
+
pins: withNeverFolds(model, input.pins, notation),
|
|
20
|
+
...(plane !== undefined ? { plane } : {}),
|
|
21
|
+
...(input.layers !== undefined ? { activeLayers: input.layers } : {}),
|
|
22
|
+
...(input.root !== undefined ? { root: input.root } : {}),
|
|
23
|
+
});
|
|
24
|
+
// Only the drawn set loses a notation's hidden kinds: layoutEdges keep them, the
|
|
25
|
+
// rule that keeps a layer toggle from moving a box (the renderer does the same).
|
|
26
|
+
const view = { ...compiled, edges: compiled.edges.filter((e) => !hidesKind(e.kind, notation)) };
|
|
27
|
+
const hierarchy = buildHierarchy(model, plane, new Set(input.layers ?? presetLayers(model.planes, plane)));
|
|
28
|
+
const nodes = new Map();
|
|
29
|
+
const parentOf = new Map();
|
|
30
|
+
const boxes = new Map();
|
|
31
|
+
const walk = (vn, parent) => {
|
|
32
|
+
nodes.set(vn.id, vn);
|
|
33
|
+
parentOf.set(vn.id, parent);
|
|
34
|
+
const inside = hierarchy.childrenOf.get(vn.id)?.length ?? 0;
|
|
35
|
+
boxes.set(vn.id, boxText(vn, { inside, glyphs, ...(notation !== undefined ? { notation } : {}) }));
|
|
36
|
+
for (const child of vn.children)
|
|
37
|
+
walk(child, vn.id);
|
|
38
|
+
};
|
|
39
|
+
for (const root of view.roots)
|
|
40
|
+
walk(root, undefined);
|
|
41
|
+
const labels = new Map(view.layoutEdges.map((e) => [e.id, edgeText(e, glyphs)]));
|
|
42
|
+
// A fishbone is the fish, as on the page, whatever way the plane's settings say to flow.
|
|
43
|
+
const layout = notation === FISHBONE_NOTATION
|
|
44
|
+
? fishCells(model, view, boxes)
|
|
45
|
+
: await layoutCells(view, boxes, directionOf(input), labels);
|
|
46
|
+
const accents = notationAccents(model, plane, notation, view.edges);
|
|
47
|
+
const scene = { input, view, layout, boxes, nodes, parentOf, hierarchy, accents, ...defined({ notation }) };
|
|
48
|
+
const legend = legendOf(scene, glyphs);
|
|
49
|
+
return legend === undefined ? scene : { ...scene, legend };
|
|
50
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { type DiagramNode, type NotationId, type ViewEdge } from '@diagc/core/internal';
|
|
2
|
+
/** The shapes a terminal can tell apart. */
|
|
3
|
+
export type TextShape = 'box' | 'rounded' | 'cylinder' | 'hexagon' | 'diamond' | 'dot' | 'bar' | 'note' | 'table';
|
|
4
|
+
export interface TypeLook {
|
|
5
|
+
shape: TextShape;
|
|
6
|
+
/** the subtitle in place of the raw type id; `''` prints none (the renderer's `label`) */
|
|
7
|
+
label?: string;
|
|
8
|
+
dashed?: boolean;
|
|
9
|
+
/** a container of this type is always drawn open (the renderer's `alwaysExpanded`) */
|
|
10
|
+
neverFolds?: boolean;
|
|
11
|
+
/** the border colour a notation gives the type (C4's blues) */
|
|
12
|
+
accent?: string;
|
|
13
|
+
/** what a legend calls the type (the renderer's `legendLabel`): set only on a shape
|
|
14
|
+
* that says nothing about itself, which a legend keys without being asked */
|
|
15
|
+
legend?: string;
|
|
16
|
+
}
|
|
17
|
+
export type LineStyle = 'solid' | 'dashed' | 'heavy';
|
|
18
|
+
export type EndMark = 'arrow' | 'none' | 'dot' | 'square' | 'diamond' | 'many' | 'one';
|
|
19
|
+
export interface KindLook {
|
|
20
|
+
line: LineStyle;
|
|
21
|
+
end: EndMark;
|
|
22
|
+
start?: EndMark;
|
|
23
|
+
/** what a legend calls the kind, as `TypeLook.legend` */
|
|
24
|
+
legend?: string;
|
|
25
|
+
}
|
|
26
|
+
/** One entry per type the renderer styles, in its order. */
|
|
27
|
+
export declare const TYPE_LOOKS: Readonly<Record<string, TypeLook>>;
|
|
28
|
+
/** One entry per kind the renderer styles, in its order. */
|
|
29
|
+
export declare const KIND_LOOKS: Readonly<Record<string, KindLook>>;
|
|
30
|
+
export declare function typeLook(type: string | undefined, notation: NotationId | undefined): TypeLook;
|
|
31
|
+
export declare function kindLook(edge: Pick<ViewEdge, 'kind' | 'style'>, notation: NotationId | undefined): KindLook;
|
|
32
|
+
/** Kinds a notation never draws as lines: a plan's roles become chips on the zone. */
|
|
33
|
+
export declare function hidesKind(kind: string, notation: NotationId | undefined): boolean;
|
|
34
|
+
/** The subtitle a box prints under its name, as the renderer prints it: the type's
|
|
35
|
+
* label, or the raw type id for a type without one; `''` for none. */
|
|
36
|
+
export declare function subtitleOf(node: DiagramNode, notation: NotationId | undefined): string;
|
|
37
|
+
export declare function neverFolds(node: DiagramNode, notation: NotationId | undefined): boolean;
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { GIT_NOTATION, GIT_STAGE_TYPE, isActivityChrome, isPlanRole, } from '@diagc/core/internal';
|
|
2
|
+
const box = (label, extra = {}) => ({
|
|
3
|
+
shape: 'box',
|
|
4
|
+
...(label !== undefined ? { label } : {}),
|
|
5
|
+
...extra,
|
|
6
|
+
});
|
|
7
|
+
const shaped = (shape, label, extra = {}) => ({
|
|
8
|
+
shape,
|
|
9
|
+
...(label !== undefined ? { label } : {}),
|
|
10
|
+
...extra,
|
|
11
|
+
});
|
|
12
|
+
const dashed = { dashed: true };
|
|
13
|
+
const open = { neverFolds: true };
|
|
14
|
+
/** One entry per type the renderer styles, in its order. */
|
|
15
|
+
export const TYPE_LOOKS = {
|
|
16
|
+
system: box(undefined, dashed),
|
|
17
|
+
platform: box(undefined, dashed),
|
|
18
|
+
service: box(),
|
|
19
|
+
database: shaped('cylinder'),
|
|
20
|
+
'aws-rds': shaped('cylinder'),
|
|
21
|
+
table: shaped('cylinder'),
|
|
22
|
+
'db-table': shaped('table'),
|
|
23
|
+
queue: shaped('rounded'),
|
|
24
|
+
infra: shaped('hexagon'),
|
|
25
|
+
person: shaped('rounded'),
|
|
26
|
+
team: shaped('rounded'),
|
|
27
|
+
comment: shaped('rounded'),
|
|
28
|
+
'c4-person': box('[Person]'),
|
|
29
|
+
'c4-person-external': box('[Person]'),
|
|
30
|
+
'c4-system': box('[Software System]'),
|
|
31
|
+
'c4-system-external': box('[Software System]'),
|
|
32
|
+
'c4-enterprise-boundary': box('[Enterprise]', dashed),
|
|
33
|
+
'c4-system-boundary': box('[System]', dashed),
|
|
34
|
+
'c4-group': box('[Group]', dashed),
|
|
35
|
+
'c4-container': box('[Container]'),
|
|
36
|
+
'c4-container-external': box('[Container]'),
|
|
37
|
+
'c4-container-web': box('[Container]'),
|
|
38
|
+
'c4-container-spa': box('[Container]'),
|
|
39
|
+
'c4-container-mobile': box('[Container]'),
|
|
40
|
+
'c4-container-desktop': box('[Container]'),
|
|
41
|
+
'c4-container-api': box('[Container]'),
|
|
42
|
+
'c4-container-function': box('[Container]'),
|
|
43
|
+
'c4-container-cli': box('[Container]'),
|
|
44
|
+
'c4-container-db': shaped('cylinder', '[Container]'),
|
|
45
|
+
'c4-container-blob': shaped('cylinder', '[Container]'),
|
|
46
|
+
'c4-container-search': shaped('cylinder', '[Container]'),
|
|
47
|
+
'c4-container-queue': shaped('rounded', '[Container]'),
|
|
48
|
+
'c4-container-boundary': box('[Container]', dashed),
|
|
49
|
+
'c4-component': box('[Component]'),
|
|
50
|
+
'c4-component-external': box('[Component]'),
|
|
51
|
+
'c4-component-db': shaped('cylinder', '[Component]'),
|
|
52
|
+
'c4-component-queue': shaped('rounded', '[Component]'),
|
|
53
|
+
'c4-deployment-node': box('[Deployment Node]', dashed),
|
|
54
|
+
'c4-infrastructure-node': shaped('hexagon', '[Infrastructure Node]'),
|
|
55
|
+
'c4-container-instance': box('[Container Instance]'),
|
|
56
|
+
'c4-class': box('[Class]'),
|
|
57
|
+
'c4-interface': box('[Interface]', dashed),
|
|
58
|
+
'c4-enum': box('[Enumeration]'),
|
|
59
|
+
'aws-group': box(''),
|
|
60
|
+
'aws-account': box(''),
|
|
61
|
+
'aws-cloud': box(''),
|
|
62
|
+
'aws-vpc': box(''),
|
|
63
|
+
'aws-region': box('', dashed),
|
|
64
|
+
'aws-auto-scaling-group': box('', dashed),
|
|
65
|
+
'aws-az': box('', dashed),
|
|
66
|
+
'aws-subnet-public': box(''),
|
|
67
|
+
'aws-subnet-private': box(''),
|
|
68
|
+
'activity-frame': box(undefined, open),
|
|
69
|
+
'activity-lane': box(undefined, open),
|
|
70
|
+
'activity-region': box(undefined, { ...dashed, ...open, legend: 'Interruptible region' }),
|
|
71
|
+
'activity-action': shaped('rounded', '', { legend: 'Action' }),
|
|
72
|
+
'activity-decision': shaped('diamond', '', { legend: 'Decision / merge' }),
|
|
73
|
+
'activity-bar': shaped('bar', '', { legend: 'Fork / join' }),
|
|
74
|
+
'activity-start': shaped('dot', '', { legend: 'Start' }),
|
|
75
|
+
'activity-end': shaped('dot', '', { legend: 'End' }),
|
|
76
|
+
'activity-send': box('', { legend: 'Send signal' }),
|
|
77
|
+
'activity-receive': box('', { legend: 'Receive signal' }),
|
|
78
|
+
'activity-object': box('', { legend: 'Object' }),
|
|
79
|
+
'activity-note': shaped('note', ''),
|
|
80
|
+
'so-decision': shaped('rounded', ''),
|
|
81
|
+
'so-consequence-positive': shaped('rounded', ''),
|
|
82
|
+
'so-consequence-negative': shaped('rounded', ''),
|
|
83
|
+
'so-consequence-neutral': shaped('rounded', ''),
|
|
84
|
+
'fb-effect': box(''),
|
|
85
|
+
'fb-category': box(''),
|
|
86
|
+
'fb-cause': box(''),
|
|
87
|
+
'deploy-environment': box('[Environment]', { legend: 'Environment' }),
|
|
88
|
+
'deploy-region': box('[Region]', { legend: 'Region' }),
|
|
89
|
+
'deploy-zone': box('[Availability zone]', { ...dashed, legend: 'Availability zone' }),
|
|
90
|
+
'deploy-network': box('[Network]', { legend: 'Network' }),
|
|
91
|
+
'deploy-subnet-public': box('[Public subnet]', { legend: 'Public subnet' }),
|
|
92
|
+
'deploy-subnet-private': box('[Private subnet]', { legend: 'Private subnet' }),
|
|
93
|
+
'deploy-cluster': box('[Cluster]', { ...dashed, legend: 'Cluster' }),
|
|
94
|
+
'deploy-host': box('[Host]'),
|
|
95
|
+
'deploy-service': shaped('rounded', '[Service]'),
|
|
96
|
+
'deploy-database': shaped('cylinder', '[Database]'),
|
|
97
|
+
'deploy-queue': shaped('rounded', '[Queue]'),
|
|
98
|
+
'deploy-storage': shaped('cylinder', '[Storage]'),
|
|
99
|
+
'deploy-load-balancer': shaped('hexagon', '[Load balancer]'),
|
|
100
|
+
'deploy-gateway': shaped('hexagon', '[Gateway]'),
|
|
101
|
+
'deploy-firewall': box('[Firewall]'),
|
|
102
|
+
'deploy-internet': shaped('rounded', '[External network]', dashed),
|
|
103
|
+
'tm-entity': box('', { legend: 'External entity' }),
|
|
104
|
+
'tm-process': shaped('rounded', '', { legend: 'Process' }),
|
|
105
|
+
'tm-store': shaped('cylinder', '', { legend: 'Data store' }),
|
|
106
|
+
'tm-boundary': box('', { ...dashed, ...open, legend: 'Trust boundary' }),
|
|
107
|
+
'plan-zone': shaped('rounded', '', { legend: 'Zone' }),
|
|
108
|
+
'plan-event': shaped('diamond', '', { legend: 'Event' }),
|
|
109
|
+
};
|
|
110
|
+
const solid = { line: 'solid', end: 'arrow' };
|
|
111
|
+
/** One entry per kind the renderer styles, in its order. */
|
|
112
|
+
export const KIND_LOOKS = {
|
|
113
|
+
sync: solid,
|
|
114
|
+
async: { line: 'dashed', end: 'arrow' },
|
|
115
|
+
reads: solid,
|
|
116
|
+
writes: { line: 'heavy', end: 'arrow' },
|
|
117
|
+
'hosted-on': { line: 'dashed', end: 'arrow' },
|
|
118
|
+
flow: solid,
|
|
119
|
+
mixed: { line: 'heavy', end: 'arrow' },
|
|
120
|
+
fk: { line: 'solid', end: 'one', start: 'many', legend: 'Foreign key: many to one' },
|
|
121
|
+
control: { ...solid, legend: 'Control flow' },
|
|
122
|
+
'object-flow': { line: 'dashed', end: 'arrow', legend: 'Object flow' },
|
|
123
|
+
interrupt: { ...solid, legend: 'Interrupt' },
|
|
124
|
+
'note-link': { line: 'dashed', end: 'none', legend: 'Note link' },
|
|
125
|
+
influence: solid,
|
|
126
|
+
network: solid,
|
|
127
|
+
'leads-to': solid,
|
|
128
|
+
'cause-of': solid,
|
|
129
|
+
'data-flow': { ...solid, legend: 'Data flow' },
|
|
130
|
+
owns: { ...solid, legend: 'Owns' },
|
|
131
|
+
executes: { ...solid, legend: 'Executes' },
|
|
132
|
+
checks: { ...solid, legend: 'Checks' },
|
|
133
|
+
};
|
|
134
|
+
// What a notation's profile changes (packages/renderer/src/notations.ts). Not pinned
|
|
135
|
+
// by the test: the profiles are code, not tables, and these are the few looks a
|
|
136
|
+
// terminal can show — C4's person shape and colours, deployment's solid node, git's
|
|
137
|
+
// round commits and headless dashed links.
|
|
138
|
+
const C4_COLOURS = {
|
|
139
|
+
'c4-person': '#08427b',
|
|
140
|
+
'c4-person-external': '#999999',
|
|
141
|
+
'c4-system': '#1168bd',
|
|
142
|
+
'c4-system-external': '#999999',
|
|
143
|
+
'c4-container-external': '#999999',
|
|
144
|
+
'c4-component': '#85bbf0',
|
|
145
|
+
'c4-component-db': '#85bbf0',
|
|
146
|
+
'c4-component-queue': '#85bbf0',
|
|
147
|
+
'c4-component-external': '#999999',
|
|
148
|
+
...Object.fromEntries(['', '-web', '-spa', '-mobile', '-desktop', '-api', '-function', '-cli', '-db', '-blob', '-search', '-queue'].map((suffix) => [`c4-container${suffix}`, '#438dd5'])),
|
|
149
|
+
};
|
|
150
|
+
const NOTATION_TYPES = {
|
|
151
|
+
c4: Object.fromEntries(Object.entries(C4_COLOURS).map(([type, accent]) => [
|
|
152
|
+
type,
|
|
153
|
+
{ ...TYPE_LOOKS[type], accent, ...(type.startsWith('c4-person') ? { shape: 'rounded' } : {}) },
|
|
154
|
+
])),
|
|
155
|
+
deployment: { 'c4-deployment-node': box('[Deployment Node]') },
|
|
156
|
+
// a commit is a dot and a branch a lane in the browser: neither prints its type
|
|
157
|
+
[GIT_NOTATION]: { commit: shaped('dot', ''), branch: box('', open), [GIT_STAGE_TYPE]: box('') },
|
|
158
|
+
plan: { 'plan-zone': { ...TYPE_LOOKS['plan-zone'], ...open } },
|
|
159
|
+
};
|
|
160
|
+
const NOTATION_KINDS = {
|
|
161
|
+
[GIT_NOTATION]: {
|
|
162
|
+
commit: { line: 'dashed', end: 'none' },
|
|
163
|
+
branch: { line: 'dashed', end: 'none' },
|
|
164
|
+
merge: { line: 'dashed', end: 'none' },
|
|
165
|
+
},
|
|
166
|
+
};
|
|
167
|
+
export function typeLook(type, notation) {
|
|
168
|
+
if (type === undefined)
|
|
169
|
+
return { shape: 'box' };
|
|
170
|
+
const own = notation !== undefined ? NOTATION_TYPES[notation]?.[type] : undefined;
|
|
171
|
+
return own ?? TYPE_LOOKS[type] ?? { shape: 'box' };
|
|
172
|
+
}
|
|
173
|
+
export function kindLook(edge, notation) {
|
|
174
|
+
const own = notation !== undefined ? NOTATION_KINDS[notation]?.[edge.kind] : undefined;
|
|
175
|
+
const base = own ?? KIND_LOOKS[edge.kind] ?? solid;
|
|
176
|
+
const style = edge.style;
|
|
177
|
+
if (style === undefined)
|
|
178
|
+
return base;
|
|
179
|
+
const line = style.line === 'dashed' || style.line === 'dotted'
|
|
180
|
+
? 'dashed'
|
|
181
|
+
: (style.width ?? 0) >= 2.5
|
|
182
|
+
? 'heavy'
|
|
183
|
+
: style.line === 'solid'
|
|
184
|
+
? 'solid'
|
|
185
|
+
: base.line;
|
|
186
|
+
return { ...base, line, ...(style.end !== undefined ? { end: style.end } : {}) };
|
|
187
|
+
}
|
|
188
|
+
/** Kinds a notation never draws as lines: a plan's roles become chips on the zone. */
|
|
189
|
+
export function hidesKind(kind, notation) {
|
|
190
|
+
return notation === 'plan' && isPlanRole(kind);
|
|
191
|
+
}
|
|
192
|
+
/** The renderer's `typeSubtitle`: `[Container]` + `Java` → `[Container: Java]`. An
|
|
193
|
+
* empty label stays empty, so a glyph type's technology cannot resurrect it. */
|
|
194
|
+
function typeSubtitle(label, technology) {
|
|
195
|
+
if (technology === undefined || label === '')
|
|
196
|
+
return label;
|
|
197
|
+
return label.endsWith(']') ? `${label.slice(0, -1)}: ${technology}]` : `${label}: ${technology}`;
|
|
198
|
+
}
|
|
199
|
+
/** The subtitle a box prints under its name, as the renderer prints it: the type's
|
|
200
|
+
* label, or the raw type id for a type without one; `''` for none. */
|
|
201
|
+
export function subtitleOf(node, notation) {
|
|
202
|
+
// The activity frame, lanes and regions are drawn as bodies of their own, which
|
|
203
|
+
// print the name alone; their registry entries carry no label only because they
|
|
204
|
+
// are never drawn as a plain box.
|
|
205
|
+
if (node.type === undefined || isActivityChrome(node.type))
|
|
206
|
+
return '';
|
|
207
|
+
return typeSubtitle(typeLook(node.type, notation).label ?? node.type, node.technology);
|
|
208
|
+
}
|
|
209
|
+
export function neverFolds(node, notation) {
|
|
210
|
+
return typeLook(node.type, notation).neverFolds === true;
|
|
211
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** A C0 or C1 control character, or DEL: a terminal acts on one (a line break, an
|
|
2
|
+
* escape) instead of drawing it. */
|
|
3
|
+
export declare const isControl: (cp: number) => boolean;
|
|
4
|
+
/** `text` with every control character a space: what a name, a label or a description
|
|
5
|
+
* may hold (a line break typed in the studio, a tab) and a terminal must not be sent. */
|
|
6
|
+
export declare function printable(text: string): string;
|
|
7
|
+
/** The cells one code point takes: 0 for a mark, a joiner or a control character. */
|
|
8
|
+
export declare function charWidth(cp: number): 0 | 1 | 2;
|
|
9
|
+
/** The cells `text` takes in a terminal. */
|
|
10
|
+
export declare function cellWidth(text: string): number;
|
|
11
|
+
/** `text` cut to `max` cells, ending in `ellipsis` (one cell) when anything was cut. */
|
|
12
|
+
export declare function fitCells(text: string, max: number, ellipsis?: string): string;
|
|
13
|
+
/** `text` wrapped at spaces into lines of at most `max` cells; a word wider than a
|
|
14
|
+
* line is split. Empty text is one empty line, so a box always has a title row. */
|
|
15
|
+
export declare function wrapCells(text: string, max: number): string[];
|