@diagc/cli 1.5.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/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/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,14 @@
|
|
|
1
|
+
import { type DiagramModel, type LayoutOverlay, type NotationId } from '@diagc/core/internal';
|
|
2
|
+
export type Pins = Record<string, 'expanded' | 'collapsed'>;
|
|
3
|
+
/** The PNG export's folds: every container open, except those the layout keeps
|
|
4
|
+
* folded for the export (`layout.export.collapsed`). The viewer's `exportFolds`. */
|
|
5
|
+
export declare function exportPins(model: DiagramModel, overlay: LayoutOverlay | undefined): Pins;
|
|
6
|
+
/** Only the top `depth` levels open: a container whose shallowest way down from a root
|
|
7
|
+
* on the plane is fewer than `depth` steps is open, every other one folded. */
|
|
8
|
+
export declare function depthPins(model: DiagramModel, plane: string | undefined, depth: number): Pins;
|
|
9
|
+
/** The published page's opening folds: what the layout saved as unfolded for the plane. */
|
|
10
|
+
export declare function readerPins(model: DiagramModel, overlay: LayoutOverlay | undefined, plane: string | undefined): Pins;
|
|
11
|
+
/** `pins` with the containers that never fold — an activity lane, a trust boundary, a
|
|
12
|
+
* git branch — pinned open over them, as the renderer pins them. Unchanged (the same
|
|
13
|
+
* object) when there are none. */
|
|
14
|
+
export declare function withNeverFolds(model: DiagramModel, pins: Pins, notation: NotationId | undefined): Pins;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { buildHierarchy, openingPins, } from '@diagc/core/internal';
|
|
2
|
+
import { neverFolds } from './vocabulary.js';
|
|
3
|
+
const containers = (model) => [...new Set(model.containment.map((c) => c.parent))];
|
|
4
|
+
/** The PNG export's folds: every container open, except those the layout keeps
|
|
5
|
+
* folded for the export (`layout.export.collapsed`). The viewer's `exportFolds`. */
|
|
6
|
+
export function exportPins(model, overlay) {
|
|
7
|
+
const folded = new Set(overlay?.export?.collapsed ?? []);
|
|
8
|
+
return Object.fromEntries(containers(model).map((id) => [id, folded.has(id) ? 'collapsed' : 'expanded']));
|
|
9
|
+
}
|
|
10
|
+
/** Only the top `depth` levels open: a container whose shallowest way down from a root
|
|
11
|
+
* on the plane is fewer than `depth` steps is open, every other one folded. */
|
|
12
|
+
export function depthPins(model, plane, depth) {
|
|
13
|
+
const hierarchy = buildHierarchy(model, plane);
|
|
14
|
+
const depthOf = new Map(hierarchy.roots.map((id) => [id, 0]));
|
|
15
|
+
const queue = [...hierarchy.roots];
|
|
16
|
+
for (let i = 0; i < queue.length; i++) {
|
|
17
|
+
const id = queue[i];
|
|
18
|
+
for (const child of hierarchy.childrenOf.get(id) ?? []) {
|
|
19
|
+
if (depthOf.has(child))
|
|
20
|
+
continue;
|
|
21
|
+
depthOf.set(child, depthOf.get(id) + 1);
|
|
22
|
+
queue.push(child);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const pins = {};
|
|
26
|
+
for (const [id, kids] of hierarchy.childrenOf) {
|
|
27
|
+
if (kids.length > 0)
|
|
28
|
+
pins[id] = (depthOf.get(id) ?? 0) < depth ? 'expanded' : 'collapsed';
|
|
29
|
+
}
|
|
30
|
+
return pins;
|
|
31
|
+
}
|
|
32
|
+
/** The published page's opening folds: what the layout saved as unfolded for the plane. */
|
|
33
|
+
export function readerPins(model, overlay, plane) {
|
|
34
|
+
return openingPins(overlay, model, plane);
|
|
35
|
+
}
|
|
36
|
+
/** `pins` with the containers that never fold — an activity lane, a trust boundary, a
|
|
37
|
+
* git branch — pinned open over them, as the renderer pins them. Unchanged (the same
|
|
38
|
+
* object) when there are none. */
|
|
39
|
+
export function withNeverFolds(model, pins, notation) {
|
|
40
|
+
const parents = new Set(containers(model));
|
|
41
|
+
const open = model.nodes.filter((n) => parents.has(n.id) && neverFolds(n, notation));
|
|
42
|
+
if (open.length === 0)
|
|
43
|
+
return pins;
|
|
44
|
+
return { ...pins, ...Object.fromEntries(open.map((n) => [n.id, 'expanded'])) };
|
|
45
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { EndMark } from './vocabulary.js';
|
|
2
|
+
export declare const UP = 1;
|
|
3
|
+
export declare const DOWN = 2;
|
|
4
|
+
export declare const LEFT = 4;
|
|
5
|
+
export declare const RIGHT = 8;
|
|
6
|
+
export type Weight = 'light' | 'heavy' | 'dashed';
|
|
7
|
+
export type Corner = 'round' | 'chamfer';
|
|
8
|
+
export type ArrowDirection = 'up' | 'down' | 'left' | 'right';
|
|
9
|
+
export interface Glyphs {
|
|
10
|
+
/** the character for a cell whose lines leave it in `mask`'s directions; `corner`
|
|
11
|
+
* applies only while the mask is a plain corner */
|
|
12
|
+
line(mask: number, weight: Weight, corner?: Corner): string;
|
|
13
|
+
arrow(direction: ArrowDirection): string;
|
|
14
|
+
/** a line one cell a row, a fishbone's bone or tick: falling to the right, or rising */
|
|
15
|
+
slant(falling: boolean): string;
|
|
16
|
+
mark(mark: Exclude<EndMark, 'arrow' | 'none'>): string;
|
|
17
|
+
/** one cell, for text cut short */
|
|
18
|
+
ellipsis: string;
|
|
19
|
+
/** what a folded container's note starts with */
|
|
20
|
+
folded: string;
|
|
21
|
+
/** what a drill view's stand-in for an outside node starts with */
|
|
22
|
+
external: string;
|
|
23
|
+
/** the marks a glyph type's box starts with: an activity's start and end, a decision */
|
|
24
|
+
start: string;
|
|
25
|
+
end: string;
|
|
26
|
+
decision: string;
|
|
27
|
+
/** a causal link's delay, after its label */
|
|
28
|
+
delay: string;
|
|
29
|
+
}
|
|
30
|
+
export declare const UNICODE: Glyphs;
|
|
31
|
+
export declare const ASCII: Glyphs;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// The characters a picture is drawn with. A line is recorded on the canvas as the
|
|
2
|
+
// directions it leaves a cell in (a mask of the four below) and only turned into a
|
|
3
|
+
// character at the end, so wherever two lines meet the junction comes out right
|
|
4
|
+
// without the code that drew either knowing about the other.
|
|
5
|
+
export const UP = 1;
|
|
6
|
+
export const DOWN = 2;
|
|
7
|
+
export const LEFT = 4;
|
|
8
|
+
export const RIGHT = 8;
|
|
9
|
+
/** Index a 16-entry table by mask; a lone direction draws as the whole line. */
|
|
10
|
+
function table(chars) {
|
|
11
|
+
const list = [...chars];
|
|
12
|
+
return (mask) => list[mask];
|
|
13
|
+
}
|
|
14
|
+
// By mask: 0 U D UD L UL DL UDL R UR DR UDR LR ULR DLR all
|
|
15
|
+
const LIGHT = table(' │││─┘┐┤─└┌├─┴┬┼');
|
|
16
|
+
const HEAVY = table(' ┃┃┃━┛┓┫━┗┏┣━┻┳╋');
|
|
17
|
+
const CORNERS = new Set([DOWN | RIGHT, DOWN | LEFT, UP | RIGHT, UP | LEFT]);
|
|
18
|
+
const ROUND = { [DOWN | RIGHT]: '╭', [DOWN | LEFT]: '╮', [UP | RIGHT]: '╰', [UP | LEFT]: '╯' };
|
|
19
|
+
const CHAMFER = {
|
|
20
|
+
[DOWN | RIGHT]: '╱',
|
|
21
|
+
[DOWN | LEFT]: '╲',
|
|
22
|
+
[UP | RIGHT]: '╲',
|
|
23
|
+
[UP | LEFT]: '╱',
|
|
24
|
+
};
|
|
25
|
+
export const UNICODE = {
|
|
26
|
+
line(mask, weight, corner) {
|
|
27
|
+
if (corner !== undefined && CORNERS.has(mask))
|
|
28
|
+
return (corner === 'round' ? ROUND : CHAMFER)[mask];
|
|
29
|
+
if (weight === 'heavy')
|
|
30
|
+
return HEAVY(mask);
|
|
31
|
+
// The dashed set has no junctions: where a dashed line meets anything, the
|
|
32
|
+
// meeting reads better solid than broken.
|
|
33
|
+
if (weight === 'dashed' && (mask & (LEFT | RIGHT)) === 0)
|
|
34
|
+
return '╎';
|
|
35
|
+
if (weight === 'dashed' && (mask & (UP | DOWN)) === 0)
|
|
36
|
+
return '╌';
|
|
37
|
+
return LIGHT(mask);
|
|
38
|
+
},
|
|
39
|
+
arrow: (direction) => ({ up: '▲', down: '▼', left: '◀', right: '▶' })[direction],
|
|
40
|
+
slant: (falling) => (falling ? '╲' : '╱'),
|
|
41
|
+
mark: (mark) => ({ dot: '●', square: '■', diamond: '◆', many: '*', one: '1' })[mark],
|
|
42
|
+
ellipsis: '…',
|
|
43
|
+
folded: '+',
|
|
44
|
+
external: '↗',
|
|
45
|
+
start: '●',
|
|
46
|
+
end: '◉',
|
|
47
|
+
decision: '◇',
|
|
48
|
+
delay: '‖',
|
|
49
|
+
};
|
|
50
|
+
const ASCII_ROUND = {
|
|
51
|
+
[DOWN | RIGHT]: '.',
|
|
52
|
+
[DOWN | LEFT]: '.',
|
|
53
|
+
[UP | RIGHT]: "'",
|
|
54
|
+
[UP | LEFT]: "'",
|
|
55
|
+
};
|
|
56
|
+
const ASCII_CHAMFER = {
|
|
57
|
+
[DOWN | RIGHT]: '/',
|
|
58
|
+
[DOWN | LEFT]: '\\',
|
|
59
|
+
[UP | RIGHT]: '\\',
|
|
60
|
+
[UP | LEFT]: '/',
|
|
61
|
+
};
|
|
62
|
+
export const ASCII = {
|
|
63
|
+
line(mask, weight, corner) {
|
|
64
|
+
if (corner !== undefined && CORNERS.has(mask))
|
|
65
|
+
return (corner === 'round' ? ASCII_ROUND : ASCII_CHAMFER)[mask];
|
|
66
|
+
const vertical = (mask & (LEFT | RIGHT)) === 0;
|
|
67
|
+
const horizontal = (mask & (UP | DOWN)) === 0;
|
|
68
|
+
if (vertical)
|
|
69
|
+
return weight === 'dashed' ? ':' : '|';
|
|
70
|
+
if (horizontal)
|
|
71
|
+
return weight === 'dashed' ? '.' : weight === 'heavy' ? '=' : '-';
|
|
72
|
+
return '+';
|
|
73
|
+
},
|
|
74
|
+
arrow: (direction) => ({ up: '^', down: 'v', left: '<', right: '>' })[direction],
|
|
75
|
+
slant: (falling) => (falling ? '\\' : '/'),
|
|
76
|
+
mark: (mark) => ({ dot: 'o', square: '#', diamond: '@', many: '*', one: '1' })[mark],
|
|
77
|
+
ellipsis: '~',
|
|
78
|
+
folded: '+',
|
|
79
|
+
external: '->',
|
|
80
|
+
start: '*',
|
|
81
|
+
end: '@',
|
|
82
|
+
decision: '<>',
|
|
83
|
+
delay: '||',
|
|
84
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { CompiledView } from '@diagc/core/internal';
|
|
2
|
+
import type { CellPoint, CellRect } from './canvas.js';
|
|
3
|
+
import type { BoxText } from './content.js';
|
|
4
|
+
/** Where a box is, in whole cells from the picture's top-left corner. */
|
|
5
|
+
export type BoxGeometry = CellRect;
|
|
6
|
+
/** An edge's path. `points[0]` is the cell just outside the source box and the last
|
|
7
|
+
* point the cell just outside the target box, where the arrowhead goes; consecutive
|
|
8
|
+
* points differ on one axis only. On a fish (`FishDrawing`) a bone and a tick are
|
|
9
|
+
* slanted instead, one cell a row, and end on the line they meet. */
|
|
10
|
+
export interface Route {
|
|
11
|
+
points: CellPoint[];
|
|
12
|
+
label?: CellPoint & {
|
|
13
|
+
text: string;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
/** What a fishbone is drawn with besides boxes and routes, where it is drawn as the
|
|
17
|
+
* fish (fish.ts). */
|
|
18
|
+
export interface FishDrawing {
|
|
19
|
+
/** the spine, along row `y` from its tail to the head's border */
|
|
20
|
+
spine: {
|
|
21
|
+
y: number;
|
|
22
|
+
from: number;
|
|
23
|
+
to: number;
|
|
24
|
+
};
|
|
25
|
+
/** the effect, boxed at the spine's right end */
|
|
26
|
+
head: string;
|
|
27
|
+
/** the categories and causes, drawn as their words rather than boxed: each line
|
|
28
|
+
* where it starts. A node's box in `boxes` is the cells its words take. */
|
|
29
|
+
words: Map<string, (CellPoint & {
|
|
30
|
+
text: string;
|
|
31
|
+
})[]>;
|
|
32
|
+
/** the categories among `words`: their bones' labels */
|
|
33
|
+
labels: Set<string>;
|
|
34
|
+
}
|
|
35
|
+
export interface TextLayout {
|
|
36
|
+
width: number;
|
|
37
|
+
height: number;
|
|
38
|
+
boxes: Map<string, BoxGeometry>;
|
|
39
|
+
routes: Map<string, Route>;
|
|
40
|
+
fish?: FishDrawing;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* elk `layered`, in character cells: a node is as big as its text, and the gaps
|
|
44
|
+
* are a few cells. elk's options do not inherit into nested graphs, and every one it
|
|
45
|
+
* is not told falls back to its pixel-sized default (10, 12, 20 …), which in cells
|
|
46
|
+
* opens tens of rows between the levels of a container — so this set goes on the
|
|
47
|
+
* root and on every container alike.
|
|
48
|
+
*/
|
|
49
|
+
export declare const CELL_OPTIONS: Readonly<Record<string, string>>;
|
|
50
|
+
/** Where a box is from an end of a route that stops beside it: the direction (a
|
|
51
|
+
* glyph mask bit) to step from the end to reach its border; undefined when the end is
|
|
52
|
+
* not beside it. */
|
|
53
|
+
export declare function towardBox(end: CellPoint, g: BoxGeometry): number | undefined;
|
|
54
|
+
/** One cell's step in each direction. */
|
|
55
|
+
export declare const STEP: Readonly<Record<number, CellPoint>>;
|
|
56
|
+
/**
|
|
57
|
+
* Lays the view out in cells. Every one of `layoutEdges` takes part, drawn or not,
|
|
58
|
+
* so that turning a layer on or off never moves a box (the renderer's rule); the
|
|
59
|
+
* routes of the edges actually drawn are picked from the result by id.
|
|
60
|
+
*/
|
|
61
|
+
export declare function layoutCells(view: CompiledView, boxes: ReadonlyMap<string, BoxText>, direction: string, labels: ReadonlyMap<string, string>): Promise<TextLayout>;
|
|
@@ -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 {};
|