@grethel-labs/excaliplant 1.0.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +69 -45
- package/package.json +1 -1
- package/src/diagrams/activity/index.mjs +1 -1
- package/src/diagrams/data/index.mjs +0 -1
- package/src/diagrams/ditaa/index.mjs +107 -41
- package/src/diagrams/graph/draw.mjs +121 -22
- package/src/diagrams/graph/layout.mjs +121 -2
- package/src/diagrams/graph/parse.mjs +5 -2
- package/src/diagrams/nwdiag/index.mjs +2 -2
- package/src/diagrams/railroad/index.mjs +2 -2
- package/src/diagrams/sequence/index.mjs +27 -5
- package/src/diagrams/sequence/layout.mjs +6 -2
- package/src/diagrams/sequence/parse.mjs +12 -0
- package/src/diagrams/tree/index.mjs +5 -4
- package/src/draw/canvas.mjs +4 -1
- package/src/draw/theme.mjs +11 -1
- package/src/layout/labels.mjs +236 -0
- package/types/diagrams/ditaa/index.d.mts +6 -0
- package/types/diagrams/graph/draw.d.mts +4 -2
- package/types/diagrams/sequence/layout.d.mts +1 -0
- package/types/diagrams/sequence/parse.d.mts +4 -0
- package/types/draw/theme.d.mts +4 -0
- package/types/layout/labels.d.mts +117 -0
package/README.md
CHANGED
|
@@ -15,22 +15,46 @@
|
|
|
15
15
|
<a href="./LICENSE"><img src="https://img.shields.io/npm/l/@grethel-labs/excaliplant.svg" alt="MIT license" /></a>
|
|
16
16
|
</p>
|
|
17
17
|
|
|
18
|
-
<
|
|
19
|
-
<
|
|
20
|
-
|
|
21
|
-
<td width="50%"><img src="docs/assets/class-shop-domain.png" alt="Class diagram" /></td>
|
|
22
|
-
</tr>
|
|
23
|
-
<tr>
|
|
24
|
-
<td><img src="docs/assets/activity-order-processing.png" alt="Activity diagram" /></td>
|
|
25
|
-
<td><img src="docs/assets/component-c4-container.png" alt="C4 container diagram" /></td>
|
|
26
|
-
</tr>
|
|
27
|
-
<tr>
|
|
28
|
-
<td><img src="docs/assets/state-order-lifecycle.png" alt="State diagram" /></td>
|
|
29
|
-
<td><img src="docs/assets/mindmap-excaliplant.png" alt="Mind map" /><br /><br /><img src="docs/assets/json-api-response.png" alt="JSON diagram" /></td>
|
|
30
|
-
</tr>
|
|
31
|
-
</table>
|
|
18
|
+
<p align="center">
|
|
19
|
+
<a href="https://grethel-labs.github.io/excaliplant/"><img src="docs/assets/carousel.svg" alt="Animated showcase of excaliplant diagram types" width="100%" /></a>
|
|
20
|
+
</p>
|
|
32
21
|
|
|
33
|
-
<p align="center"
|
|
22
|
+
<p align="center">
|
|
23
|
+
<a href="https://grethel-labs.github.io/excaliplant/#sequence"><kbd>Sequence</kbd></a>
|
|
24
|
+
<a href="https://grethel-labs.github.io/excaliplant/#class"><kbd>Class & IE</kbd></a>
|
|
25
|
+
<a href="https://grethel-labs.github.io/excaliplant/#activity"><kbd>Activity</kbd></a>
|
|
26
|
+
<a href="https://grethel-labs.github.io/excaliplant/#state"><kbd>State</kbd></a>
|
|
27
|
+
<a href="https://grethel-labs.github.io/excaliplant/#component"><kbd>Component & C4</kbd></a>
|
|
28
|
+
<a href="https://grethel-labs.github.io/excaliplant/#usecase"><kbd>Use case</kbd></a>
|
|
29
|
+
<a href="https://grethel-labs.github.io/excaliplant/#deployment"><kbd>Deployment</kbd></a>
|
|
30
|
+
<a href="https://grethel-labs.github.io/excaliplant/#object"><kbd>Object</kbd></a>
|
|
31
|
+
<a href="https://grethel-labs.github.io/excaliplant/#timing"><kbd>Timing</kbd></a>
|
|
32
|
+
<a href="https://grethel-labs.github.io/excaliplant/#gantt"><kbd>Gantt</kbd></a>
|
|
33
|
+
<a href="https://grethel-labs.github.io/excaliplant/#mindmap"><kbd>Mind map</kbd></a>
|
|
34
|
+
<a href="https://grethel-labs.github.io/excaliplant/#wbs"><kbd>WBS</kbd></a>
|
|
35
|
+
<a href="https://grethel-labs.github.io/excaliplant/#json"><kbd>JSON</kbd></a>
|
|
36
|
+
<a href="https://grethel-labs.github.io/excaliplant/#yaml"><kbd>YAML</kbd></a>
|
|
37
|
+
<a href="https://grethel-labs.github.io/excaliplant/#archimate"><kbd>ArchiMate</kbd></a>
|
|
38
|
+
<a href="https://grethel-labs.github.io/excaliplant/#chen"><kbd>Chen ER</kbd></a>
|
|
39
|
+
<a href="https://grethel-labs.github.io/excaliplant/#nwdiag"><kbd>Network</kbd></a>
|
|
40
|
+
<a href="https://grethel-labs.github.io/excaliplant/#salt"><kbd>Salt wireframe</kbd></a>
|
|
41
|
+
<a href="https://grethel-labs.github.io/excaliplant/#ebnf"><kbd>EBNF</kbd></a>
|
|
42
|
+
<a href="https://grethel-labs.github.io/excaliplant/#regex"><kbd>Regex</kbd></a>
|
|
43
|
+
<a href="https://grethel-labs.github.io/excaliplant/#dot"><kbd>Graphviz DOT</kbd></a>
|
|
44
|
+
<a href="https://grethel-labs.github.io/excaliplant/#chronology"><kbd>Chronology</kbd></a>
|
|
45
|
+
<a href="https://grethel-labs.github.io/excaliplant/#files"><kbd>Files</kbd></a>
|
|
46
|
+
<a href="https://grethel-labs.github.io/excaliplant/#chart"><kbd>Chart</kbd></a>
|
|
47
|
+
<a href="https://grethel-labs.github.io/excaliplant/#math"><kbd>AsciiMath</kbd></a>
|
|
48
|
+
<a href="https://grethel-labs.github.io/excaliplant/#latex"><kbd>LaTeX</kbd></a>
|
|
49
|
+
<a href="https://grethel-labs.github.io/excaliplant/#ditaa"><kbd>Ditaa</kbd></a>
|
|
50
|
+
<a href="https://grethel-labs.github.io/excaliplant/#legacy-activity"><kbd>Legacy activity</kbd></a>
|
|
51
|
+
<a href="https://grethel-labs.github.io/excaliplant/#creole"><kbd>Creole</kbd></a>
|
|
52
|
+
</p>
|
|
53
|
+
|
|
54
|
+
<p align="center">
|
|
55
|
+
<a href="https://grethel-labs.github.io/excaliplant/"><b>▶ Click through all 41 examples in the interactive gallery</b></a><br />
|
|
56
|
+
<sub>Every diagram is rendered by excaliplant from <a href="examples/"><code>examples/</code></a> — with PlantUML source, SVG and <code>.excalidraw</code> downloads.</sub>
|
|
57
|
+
</p>
|
|
34
58
|
|
|
35
59
|
## Why excaliplant?
|
|
36
60
|
|
|
@@ -83,37 +107,37 @@ Each diagram type is a small module with four hooks — `detect`, `parse`, `layo
|
|
|
83
107
|
|
|
84
108
|
## Supported diagrams
|
|
85
109
|
|
|
86
|
-
| Diagram |
|
|
110
|
+
| Diagram | Examples | Highlights |
|
|
87
111
|
| --- | --- | --- |
|
|
88
|
-
| [
|
|
89
|
-
| [
|
|
90
|
-
| [
|
|
91
|
-
| [
|
|
92
|
-
| [
|
|
93
|
-
| [
|
|
94
|
-
| [
|
|
95
|
-
| [
|
|
96
|
-
| [
|
|
97
|
-
| [
|
|
98
|
-
| [
|
|
99
|
-
| [
|
|
100
|
-
| [
|
|
101
|
-
| [
|
|
102
|
-
| [
|
|
103
|
-
| [
|
|
104
|
-
| [
|
|
105
|
-
| [
|
|
106
|
-
| [
|
|
107
|
-
| [
|
|
108
|
-
| [
|
|
109
|
-
| [
|
|
110
|
-
| [
|
|
111
|
-
| [
|
|
112
|
-
| [
|
|
113
|
-
| [
|
|
114
|
-
| [
|
|
115
|
-
| [
|
|
116
|
-
| [
|
|
112
|
+
| [Sequence](https://grethel-labs.github.io/excaliplant/#sequence) | [4](docs/gallery.md#sequence) | Participants, all arrow styles, activations, notes, fragments, dividers, autonumber, boxes. |
|
|
113
|
+
| [Class & IE](https://grethel-labs.github.io/excaliplant/#class) | [3](docs/gallery.md#class) | Classes, interfaces, enums, generics, members, packages, all UML relations, IE crow's feet. |
|
|
114
|
+
| [Activity](https://grethel-labs.github.io/excaliplant/#activity) | [3](docs/gallery.md#activity) | New syntax with if/switch/while/repeat/fork/split, partitions, notes and swimlanes. |
|
|
115
|
+
| [State](https://grethel-labs.github.io/excaliplant/#state) | [2](docs/gallery.md#state) | Composite states, concurrent regions, choice, fork/join, history, entry/exit actions. |
|
|
116
|
+
| [Component & C4](https://grethel-labs.github.io/excaliplant/#component) | [2](docs/gallery.md#component) | Components, interfaces, ports, packages, nodes — and C4 via `!include <C4/...>`. |
|
|
117
|
+
| [Use case](https://grethel-labs.github.io/excaliplant/#usecase) | [2](docs/gallery.md#usecase) | Actors, use cases, system boundaries, include/extend. |
|
|
118
|
+
| [Deployment](https://grethel-labs.github.io/excaliplant/#deployment) | [2](docs/gallery.md#deployment) | Nodes, clouds, databases, queues, artifacts and nested deployment targets. |
|
|
119
|
+
| [Object](https://grethel-labs.github.io/excaliplant/#object) | [1](docs/gallery.md#object) | Objects with fields, maps (`key => value`) and links. |
|
|
120
|
+
| [Timing](https://grethel-labs.github.io/excaliplant/#timing) | [2](docs/gallery.md#timing) | Robust, concise, clock, binary and analog signals with messages and constraints. |
|
|
121
|
+
| [Gantt](https://grethel-labs.github.io/excaliplant/#gantt) | [1](docs/gallery.md#gantt) | Tasks, dependencies, milestones, progress, resources, closed days, today marker. |
|
|
122
|
+
| [Mind map](https://grethel-labs.github.io/excaliplant/#mindmap) | [1](docs/gallery.md#mindmap) | OrgMode/Markdown outlines on both sides, colours, boxless nodes. |
|
|
123
|
+
| [WBS](https://grethel-labs.github.io/excaliplant/#wbs) | [1](docs/gallery.md#wbs) | Work breakdown structures as org charts. |
|
|
124
|
+
| [JSON](https://grethel-labs.github.io/excaliplant/#json) | [1](docs/gallery.md#json) | JSON documents as linked tables with highlights. |
|
|
125
|
+
| [YAML](https://grethel-labs.github.io/excaliplant/#yaml) | [1](docs/gallery.md#yaml) | YAML documents as linked tables. |
|
|
126
|
+
| [ArchiMate](https://grethel-labs.github.io/excaliplant/#archimate) | [1](docs/gallery.md#archimate) | ArchiMate elements by layer with icons, junctions and `Rel_*` macros. |
|
|
127
|
+
| [Chen ER](https://grethel-labs.github.io/excaliplant/#chen) | [1](docs/gallery.md#chen) | Chen ER diagrams: entities, weak entities, relationships, attributes, cardinalities. |
|
|
128
|
+
| [Network](https://grethel-labs.github.io/excaliplant/#nwdiag) | [1](docs/gallery.md#nwdiag) | Networks, addresses, multi-homed nodes, groups and peer links. |
|
|
129
|
+
| [Salt wireframe](https://grethel-labs.github.io/excaliplant/#salt) | [1](docs/gallery.md#salt) | Wireframes: grids, buttons, inputs, check boxes, trees, tabs, group boxes. |
|
|
130
|
+
| [EBNF](https://grethel-labs.github.io/excaliplant/#ebnf) | [1](docs/gallery.md#ebnf) | EBNF grammars as railroad diagrams. |
|
|
131
|
+
| [Regex](https://grethel-labs.github.io/excaliplant/#regex) | [1](docs/gallery.md#regex) | Regular expressions as railroad diagrams. |
|
|
132
|
+
| [Graphviz DOT](https://grethel-labs.github.io/excaliplant/#dot) | [1](docs/gallery.md#dot) | Graphviz DOT graphs laid out with ELK. |
|
|
133
|
+
| [Chronology](https://grethel-labs.github.io/excaliplant/#chronology) | [1](docs/gallery.md#chronology) | Events and spans on a time line with links. |
|
|
134
|
+
| [Files](https://grethel-labs.github.io/excaliplant/#files) | [1](docs/gallery.md#files) | File trees from path lists with notes. |
|
|
135
|
+
| [Chart](https://grethel-labs.github.io/excaliplant/#chart) | [1](docs/gallery.md#chart) | Bar, line, area and scatter charts. |
|
|
136
|
+
| [AsciiMath](https://grethel-labs.github.io/excaliplant/#math) | [1](docs/gallery.md#math) | AsciiMath formulas. |
|
|
137
|
+
| [LaTeX](https://grethel-labs.github.io/excaliplant/#latex) | [1](docs/gallery.md#latex) | LaTeX formulas. |
|
|
138
|
+
| [Ditaa](https://grethel-labs.github.io/excaliplant/#ditaa) | [1](docs/gallery.md#ditaa) | ASCII art boxes, lines, arrows and colour tags. |
|
|
139
|
+
| [Legacy activity](https://grethel-labs.github.io/excaliplant/#legacy-activity) | [1](docs/gallery.md#legacy-activity) | The classic `(*) --> "Activity"` syntax with sync bars and branches. |
|
|
140
|
+
| [Creole](https://grethel-labs.github.io/excaliplant/#creole) | [1](docs/gallery.md#creole) | Rich text with headings, lists, tables and links. |
|
|
117
141
|
|
|
118
142
|
## PlantUML language support
|
|
119
143
|
|
package/package.json
CHANGED
|
@@ -93,7 +93,7 @@ function drawActivity(laid, c) {
|
|
|
93
93
|
strokeWidth: 1.5,
|
|
94
94
|
strokeStyle: e.style?.lineStyle ?? "solid",
|
|
95
95
|
end: intoJunction ? null : "triangle",
|
|
96
|
-
roughness:
|
|
96
|
+
roughness: theme.edgeRoughness,
|
|
97
97
|
from: ids.get(e.from.id) ?? null,
|
|
98
98
|
to: ids.get(e.to.id) ?? null,
|
|
99
99
|
});
|
|
@@ -52,49 +52,78 @@ function parseDitaa(lines, module) {
|
|
|
52
52
|
function trace(model) {
|
|
53
53
|
const g = model.grid;
|
|
54
54
|
const at = (/** @type {number} */ r, /** @type {number} */ c) => g[r]?.[c] ?? " ";
|
|
55
|
-
/** @type {{ x1: number, y1: number, x2: number, y2: number, dashed: boolean }[]} */
|
|
55
|
+
/** @type {{ x1: number, y1: number, x2: number, y2: number, dashed: boolean, startHead: boolean, endHead: boolean }[]} */
|
|
56
56
|
const segments = [];
|
|
57
57
|
/** @type {boolean[][]} */
|
|
58
58
|
const used = g.map((r) => r.map(() => false));
|
|
59
|
-
//
|
|
59
|
+
// Line runs. Arrowhead characters at either end belong to the run, so the
|
|
60
|
+
// segment becomes a real arrow whose tip reaches the neighbouring border.
|
|
61
|
+
/** @param {number} r @param {number} c */
|
|
62
|
+
const isH = (r, c) => H.has(at(r, c));
|
|
63
|
+
/** @param {number} r @param {number} c */
|
|
64
|
+
const isV = (r, c) => V.has(at(r, c));
|
|
60
65
|
for (let r = 0; r < g.length; r++) {
|
|
61
66
|
let c = 0;
|
|
62
67
|
while (c < g[r].length) {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
68
|
+
const ch = at(r, c);
|
|
69
|
+
const opensWithHead = ch === "<" && /[-=]/.test(at(r, c + 1));
|
|
70
|
+
if (!opensWithHead && !(isH(r, c) && (/[-=]/.test(ch) || /[-=]/.test(at(r, c + 1))))) {
|
|
71
|
+
c++;
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
const start = c;
|
|
75
|
+
let dashed = false;
|
|
76
|
+
if (opensWithHead) c++;
|
|
77
|
+
while (isH(r, c)) {
|
|
78
|
+
if (at(r, c) === "=") dashed = true;
|
|
79
|
+
c++;
|
|
80
|
+
}
|
|
81
|
+
const endHead = at(r, c) === ">" && /[-=]/.test(at(r, c - 1));
|
|
82
|
+
if (endHead) c++;
|
|
83
|
+
if (c - start >= 2 || /[-=]/.test(at(r, start))) {
|
|
84
|
+
segments.push({
|
|
85
|
+
x1: start,
|
|
86
|
+
y1: r,
|
|
87
|
+
x2: c - 1,
|
|
88
|
+
y2: r,
|
|
89
|
+
dashed,
|
|
90
|
+
startHead: opensWithHead,
|
|
91
|
+
endHead,
|
|
92
|
+
});
|
|
93
|
+
for (let k = start; k < c; k++) if (!CORNER.has(at(r, k))) used[r][k] = true;
|
|
94
|
+
}
|
|
79
95
|
}
|
|
80
96
|
}
|
|
81
|
-
|
|
82
|
-
for (let c = 0; c < Math.max(...g.map((r) => r.length), 0); c++) {
|
|
97
|
+
for (let c = 0; c < Math.max(...g.map((row) => row.length), 0); c++) {
|
|
83
98
|
let r = 0;
|
|
84
99
|
while (r < g.length) {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
100
|
+
const ch = at(r, c);
|
|
101
|
+
const opensWithHead = ch === "^" && /[|:]/.test(at(r + 1, c));
|
|
102
|
+
if (!opensWithHead && !(isV(r, c) && (/[|:]/.test(ch) || /[|:]/.test(at(r + 1, c))))) {
|
|
103
|
+
r++;
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
const start = r;
|
|
107
|
+
let dashed = false;
|
|
108
|
+
if (opensWithHead) r++;
|
|
109
|
+
while (isV(r, c)) {
|
|
110
|
+
if (at(r, c) === ":") dashed = true;
|
|
111
|
+
r++;
|
|
112
|
+
}
|
|
113
|
+
const endHead = at(r, c) === "v" && /[|:]/.test(at(r - 1, c));
|
|
114
|
+
if (endHead) r++;
|
|
115
|
+
if (r - start >= 2 || /[|:]/.test(at(start, c))) {
|
|
116
|
+
segments.push({
|
|
117
|
+
x1: c,
|
|
118
|
+
y1: start,
|
|
119
|
+
x2: c,
|
|
120
|
+
y2: r - 1,
|
|
121
|
+
dashed,
|
|
122
|
+
startHead: opensWithHead,
|
|
123
|
+
endHead,
|
|
124
|
+
});
|
|
125
|
+
for (let k = start; k < r; k++) if (!CORNER.has(at(k, c))) used[k][c] = true;
|
|
126
|
+
}
|
|
98
127
|
}
|
|
99
128
|
}
|
|
100
129
|
// boxes: '+' (or rounded corner) top-left with matching corners
|
|
@@ -163,6 +192,7 @@ function trace(model) {
|
|
|
163
192
|
for (let r = 0; r < g.length; r++) {
|
|
164
193
|
for (let c = 0; c < g[r].length; c++) {
|
|
165
194
|
const ch = at(r, c);
|
|
195
|
+
if (used[r][c]) continue;
|
|
166
196
|
if (ch === ">" && H.has(at(r, c - 1))) heads.push({ r, c, dir: "right" });
|
|
167
197
|
else if (ch === "<" && H.has(at(r, c + 1))) heads.push({ r, c, dir: "left" });
|
|
168
198
|
else if (ch === "^" && V.has(at(r + 1, c))) heads.push({ r, c, dir: "up" });
|
|
@@ -207,7 +237,7 @@ function trace(model) {
|
|
|
207
237
|
else c++;
|
|
208
238
|
}
|
|
209
239
|
}
|
|
210
|
-
return { segments, boxes, heads, points, texts };
|
|
240
|
+
return { segments, boxes, heads, points, texts, grid: g };
|
|
211
241
|
}
|
|
212
242
|
|
|
213
243
|
/**
|
|
@@ -270,13 +300,49 @@ function drawDitaa(laid, c) {
|
|
|
270
300
|
(s.x1 === s.x2 && (s.x1 === b.c1 || s.x1 === b.c2) && s.y1 >= b.r1 && s.y2 <= b.r2),
|
|
271
301
|
);
|
|
272
302
|
if (inBox) continue;
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
303
|
+
const horizontal = s.y1 === s.y2;
|
|
304
|
+
/** @type {[number, number]} */
|
|
305
|
+
const a = [px(s.x1), py(s.y1)];
|
|
306
|
+
/** @type {[number, number]} */
|
|
307
|
+
const b = [px(s.x2), py(s.y2)];
|
|
308
|
+
// Arrow tips reach the edge of their cell, or the border line right behind it.
|
|
309
|
+
const tip = (
|
|
310
|
+
/** @type {[number, number]} */ p,
|
|
311
|
+
/** @type {number} */ dir,
|
|
312
|
+
/** @type {number} */ r,
|
|
313
|
+
/** @type {number} */ col,
|
|
314
|
+
) => {
|
|
315
|
+
const nr = horizontal ? r : r + dir;
|
|
316
|
+
const nc = horizontal ? col + dir : col;
|
|
317
|
+
const border = /[|+\-=:]/.test(laid.grid[nr]?.[nc] ?? " ");
|
|
318
|
+
const reach = border ? (horizontal ? CW : CH) : (horizontal ? CW : CH) / 2;
|
|
319
|
+
return /** @type {[number, number]} */ (
|
|
320
|
+
horizontal ? [p[0] + dir * reach, p[1]] : [p[0], p[1] + dir * reach]
|
|
321
|
+
);
|
|
322
|
+
};
|
|
323
|
+
if (s.startHead) {
|
|
324
|
+
const t = tip(a, -1, s.y1, s.x1);
|
|
325
|
+
a[0] = t[0];
|
|
326
|
+
a[1] = t[1];
|
|
327
|
+
}
|
|
328
|
+
if (s.endHead) {
|
|
329
|
+
const t = tip(b, 1, s.y2, s.x2);
|
|
330
|
+
b[0] = t[0];
|
|
331
|
+
b[1] = t[1];
|
|
332
|
+
}
|
|
333
|
+
const style = {
|
|
334
|
+
stroke: ink,
|
|
335
|
+
strokeWidth: 1.5,
|
|
336
|
+
strokeStyle: /** @type {"dashed" | "solid"} */ (s.dashed ? "dashed" : "solid"),
|
|
337
|
+
roughness: c.theme.edgeRoughness,
|
|
338
|
+
};
|
|
339
|
+
if (s.startHead || s.endHead)
|
|
340
|
+
c.arrow([a, b], {
|
|
341
|
+
...style,
|
|
342
|
+
start: s.startHead ? "triangle" : null,
|
|
343
|
+
end: s.endHead ? "triangle" : null,
|
|
344
|
+
});
|
|
345
|
+
else c.line([a, b], style);
|
|
280
346
|
}
|
|
281
347
|
for (const h of laid.heads) {
|
|
282
348
|
const x = px(h.c);
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
import { shape as shapeDef, arc } from "../../draw/shapes.mjs";
|
|
7
7
|
import { roleStyle } from "../../draw/theme.mjs";
|
|
8
8
|
import { TRANSPARENT, mixColors } from "../../syntax/color.mjs";
|
|
9
|
+
import { Obstacles, alongPolyline, nearEnd, segmentInBox } from "../../layout/labels.mjs";
|
|
9
10
|
|
|
10
11
|
/** @typedef {import("../../model/graph.mjs").GraphModel} GraphModel */
|
|
11
12
|
/** @typedef {import("../../model/graph.mjs").GraphNode} GraphNode */
|
|
@@ -28,17 +29,42 @@ export function drawGraph(model, canvas) {
|
|
|
28
29
|
const ids = new Map();
|
|
29
30
|
const nodes = [...model.nodes.values()].filter((n) => !n.hidden && !n.removed);
|
|
30
31
|
nodes.sort((a, b) => Number(b.container) - Number(a.container) || a.depth - b.depth);
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
|
|
33
|
+
// Everything labels must keep clear of: nodes, connector lines and their heads.
|
|
34
|
+
const obstacles = new Obstacles();
|
|
35
|
+
for (const n of nodes) {
|
|
36
|
+
if (!n.container)
|
|
37
|
+
obstacles.addBox(
|
|
38
|
+
{ x: n.x, y: n.y, w: n.width, h: n.height },
|
|
39
|
+
{ weight: 3, owner: `node:${n.id}` },
|
|
40
|
+
);
|
|
34
41
|
}
|
|
42
|
+
/** @type {{ edge: import("../../model/graph.mjs").GraphEdge, from: GraphNode, to: GraphNode, pts: [number, number][] }[]} */
|
|
43
|
+
const visible = [];
|
|
35
44
|
for (const edge of model.edges) {
|
|
36
45
|
if (edge.hidden || edge.points.length < 2) continue;
|
|
37
46
|
const from = model.nodes.get(edge.from);
|
|
38
47
|
const to = model.nodes.get(edge.to);
|
|
39
48
|
if (!from || !to || from.hidden || to.hidden) continue;
|
|
40
|
-
|
|
49
|
+
/** @type {[number, number][]} */
|
|
50
|
+
const pts = edge.points.map((p) => [p[0], p[1]]);
|
|
51
|
+
pts[0] = clip(pts[0], pts[1], from);
|
|
52
|
+
pts[pts.length - 1] = clip(pts[pts.length - 1], pts[pts.length - 2], to);
|
|
53
|
+
visible.push({ edge, from, to, pts });
|
|
54
|
+
obstacles.addPolyline(pts, { owner: edge.id });
|
|
55
|
+
if (edge.startHead) obstacles.addHead(pts[0], pts[1]);
|
|
56
|
+
if (edge.endHead) obstacles.addHead(pts[pts.length - 1], pts[pts.length - 2]);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** @type {(() => void)[]} */
|
|
60
|
+
const headers = [];
|
|
61
|
+
for (const node of nodes) {
|
|
62
|
+
const id = canvas.group(() => drawNode(node, canvas, theme, headers));
|
|
63
|
+
if (id) ids.set(node.id, id);
|
|
41
64
|
}
|
|
65
|
+
for (const v of visible) drawEdge(v.edge, v.from, v.to, canvas, ids, v.pts, obstacles);
|
|
66
|
+
// Container titles last: placed where lines cross least, drawn above the lines.
|
|
67
|
+
for (const draw of headers) draw.call(null, obstacles);
|
|
42
68
|
}
|
|
43
69
|
|
|
44
70
|
/**
|
|
@@ -61,9 +87,10 @@ export function nodeStyle(node, theme) {
|
|
|
61
87
|
* @param {GraphNode} node
|
|
62
88
|
* @param {Canvas} canvas
|
|
63
89
|
* @param {import("../../draw/theme.mjs").Theme} theme
|
|
90
|
+
* @param {((obstacles: Obstacles) => void)[] | null} [headers] Deferred container titles.
|
|
64
91
|
* @returns {string | null} Bindable element id.
|
|
65
92
|
*/
|
|
66
|
-
export function drawNode(node, canvas, theme) {
|
|
93
|
+
export function drawNode(node, canvas, theme, headers = null) {
|
|
67
94
|
const s = nodeStyle(node, theme);
|
|
68
95
|
const style = {
|
|
69
96
|
stroke: s.stroke,
|
|
@@ -80,7 +107,7 @@ export function drawNode(node, canvas, theme) {
|
|
|
80
107
|
const fill =
|
|
81
108
|
node.colors.fill ??
|
|
82
109
|
(s.fill === TRANSPARENT ? TRANSPARENT : mixColors(s.fill, theme.background, 0.6));
|
|
83
|
-
return drawContainer(node, canvas, { ...style, fill }, s.text);
|
|
110
|
+
return drawContainer(node, canvas, { ...style, fill }, s.text, headers);
|
|
84
111
|
}
|
|
85
112
|
if (geo?.kind === "class") return drawClass(node, canvas, style, s.text, theme);
|
|
86
113
|
const def = shapeDef(node.shape);
|
|
@@ -117,7 +144,7 @@ export function drawNode(node, canvas, theme) {
|
|
|
117
144
|
* @param {import("../../draw/canvas.mjs").Style} style
|
|
118
145
|
* @param {string} textColor
|
|
119
146
|
*/
|
|
120
|
-
function drawContainer(node, canvas, style, textColor) {
|
|
147
|
+
function drawContainer(node, canvas, style, textColor, headers = null) {
|
|
121
148
|
const { x, y, width: w, height: h } = node;
|
|
122
149
|
const geo = node.data.geo;
|
|
123
150
|
const def = shapeDef(node.shape === "package" ? "rectangle" : node.shape);
|
|
@@ -135,11 +162,30 @@ function drawContainer(node, canvas, style, textColor) {
|
|
|
135
162
|
{ ...style, closed: true },
|
|
136
163
|
);
|
|
137
164
|
id = canvas.rect(x, y + tabH, w, h - tabH, style);
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
165
|
+
const drawTab = (/** @type {Obstacles | null} */ obstacles) => {
|
|
166
|
+
const bw = Math.max(0, ...geo.blocks.map((/** @type {any} */ b) => b.width));
|
|
167
|
+
const bh = geo.blocks.reduce(
|
|
168
|
+
(/** @type {number} */ sum, /** @type {any} */ b) => sum + b.height,
|
|
169
|
+
0,
|
|
170
|
+
);
|
|
171
|
+
const box = { x: x + 8, y: y + 5, w: bw + 8, h: bh + 4 };
|
|
172
|
+
if (obstacles?.segments.some((sg) => segmentInBox(sg.a, sg.b, box) > 0)) {
|
|
173
|
+
canvas.rect(box.x, box.y, box.w, box.h, {
|
|
174
|
+
fill: canvas.theme.background,
|
|
175
|
+
stroke: TRANSPARENT,
|
|
176
|
+
strokeWidth: 1,
|
|
177
|
+
roughness: 0,
|
|
178
|
+
opacity: 85,
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
let ty = y + 7;
|
|
182
|
+
for (const b of geo.blocks) {
|
|
183
|
+
canvas.richText(b, x + 12, ty, { color: textColor, align: "left" });
|
|
184
|
+
ty += b.height;
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
if (headers) headers.push(drawTab);
|
|
188
|
+
else drawTab(null);
|
|
143
189
|
return id;
|
|
144
190
|
}
|
|
145
191
|
id = def.draw(canvas, x, y, w, h, style);
|
|
@@ -163,10 +209,42 @@ function drawContainer(node, canvas, style, textColor) {
|
|
|
163
209
|
}
|
|
164
210
|
return id;
|
|
165
211
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
212
|
+
const top = ty;
|
|
213
|
+
const drawTitle = (/** @type {Obstacles | null} */ obstacles) => {
|
|
214
|
+
const bw = Math.max(0, ...geo.blocks.map((/** @type {any} */ b) => b.width));
|
|
215
|
+
const bh = geo.blocks.reduce(
|
|
216
|
+
(/** @type {number} */ sum, /** @type {any} */ b) => sum + b.height,
|
|
217
|
+
0,
|
|
218
|
+
);
|
|
219
|
+
// Prefer the centre, then the corners, whichever is crossed by fewest lines.
|
|
220
|
+
const xs = [x + (w - bw) / 2, x + 14, x + w - bw - 14];
|
|
221
|
+
const box = obstacles
|
|
222
|
+
? obstacles.place(
|
|
223
|
+
xs.map((cx, i) => ({ x: cx - 4, y: top - 2, w: bw + 8, h: bh + 4, penalty: i * 30 })),
|
|
224
|
+
{ owner: `title:${node.id}` },
|
|
225
|
+
)
|
|
226
|
+
: { x: xs[0] - 4, y: top - 2, w: bw + 8, h: bh + 4 };
|
|
227
|
+
// A plate only where a line actually runs behind the title.
|
|
228
|
+
const crossed = obstacles
|
|
229
|
+
? obstacles.segments.some((sg) => segmentInBox(sg.a, sg.b, box) > 0)
|
|
230
|
+
: false;
|
|
231
|
+
if (crossed) {
|
|
232
|
+
canvas.rect(box.x, box.y, box.w, box.h, {
|
|
233
|
+
fill: canvas.theme.background,
|
|
234
|
+
stroke: TRANSPARENT,
|
|
235
|
+
strokeWidth: 1,
|
|
236
|
+
roughness: 0,
|
|
237
|
+
opacity: 85,
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
let cy = top;
|
|
241
|
+
for (const b of geo.blocks) {
|
|
242
|
+
canvas.richText(b, box.x + 4 + (bw - b.width) / 2, cy, { color: textColor });
|
|
243
|
+
cy += b.height;
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
if (headers) headers.push(drawTitle);
|
|
247
|
+
else drawTitle(null);
|
|
170
248
|
if (node.role === "compositeState") {
|
|
171
249
|
canvas.line(
|
|
172
250
|
[
|
|
@@ -402,14 +480,14 @@ function clip(p, q, node) {
|
|
|
402
480
|
* @param {Canvas} canvas
|
|
403
481
|
* @param {Map<string, string>} ids
|
|
404
482
|
*/
|
|
405
|
-
export function drawEdge(edge, from, to, canvas, ids) {
|
|
483
|
+
export function drawEdge(edge, from, to, canvas, ids, points = null, obstacles = null) {
|
|
406
484
|
const theme = canvas.theme;
|
|
407
485
|
const s = roleStyle(theme, edge.role === "note" ? "edge" : edge.role, {
|
|
408
486
|
stroke: edge.color ?? undefined,
|
|
409
487
|
});
|
|
410
488
|
/** @type {[number, number][]} */
|
|
411
|
-
const pts = edge.points.map((p) => [p[0], p[1]]);
|
|
412
|
-
if (pts.length >= 2) {
|
|
489
|
+
const pts = points ?? edge.points.map((p) => [p[0], p[1]]);
|
|
490
|
+
if (!points && pts.length >= 2) {
|
|
413
491
|
pts[0] = clip(pts[0], pts[1], from);
|
|
414
492
|
pts[pts.length - 1] = clip(pts[pts.length - 1], pts[pts.length - 2], to);
|
|
415
493
|
}
|
|
@@ -426,7 +504,16 @@ export function drawEdge(edge, from, to, canvas, ids) {
|
|
|
426
504
|
});
|
|
427
505
|
const labelStyle = roleStyle(theme, "edgeLabel");
|
|
428
506
|
if (edge.labelBox && edge.data.labelLayout) {
|
|
429
|
-
const
|
|
507
|
+
const elk = edge.labelBox;
|
|
508
|
+
const placed = obstacles
|
|
509
|
+
? obstacles.place(
|
|
510
|
+
alongPolyline(pts, elk.width, elk.height, {
|
|
511
|
+
preferred: { x: elk.x, y: elk.y, w: elk.width, h: elk.height },
|
|
512
|
+
}),
|
|
513
|
+
{ owner: edge.id },
|
|
514
|
+
)
|
|
515
|
+
: { x: elk.x, y: elk.y, w: elk.width, h: elk.height };
|
|
516
|
+
const b = { x: placed.x, y: placed.y, width: placed.w, height: placed.h };
|
|
430
517
|
const laid = edge.data.labelLayout;
|
|
431
518
|
canvas.group(() => {
|
|
432
519
|
canvas.rect(b.x, b.y, b.width, b.height, {
|
|
@@ -464,9 +551,21 @@ export function drawEdge(edge, from, to, canvas, ids) {
|
|
|
464
551
|
});
|
|
465
552
|
}
|
|
466
553
|
for (const which of /** @type {const} */ (["fromLabel", "toLabel"])) {
|
|
467
|
-
const box = edge[`${which}Box`];
|
|
468
554
|
const laid = edge.data[`${which}Layout`];
|
|
469
|
-
if (
|
|
555
|
+
if (!laid || pts.length < 2) continue;
|
|
556
|
+
const atStart = which === "fromLabel";
|
|
557
|
+
const tip = atStart ? pts[0] : pts[pts.length - 1];
|
|
558
|
+
const prev = atStart ? pts[1] : pts[pts.length - 2];
|
|
559
|
+
const w = laid.width + 4;
|
|
560
|
+
const h = laid.height;
|
|
561
|
+
// Beside the line, beyond the arrowhead, away from other labels and lines.
|
|
562
|
+
const box = obstacles
|
|
563
|
+
? obstacles.place(nearEnd(tip, prev, w, h), { owner: edge.id })
|
|
564
|
+
: (() => {
|
|
565
|
+
const b = edge[`${which}Box`];
|
|
566
|
+
return b ? { x: b.x, y: b.y, w: b.width, h: b.height } : null;
|
|
567
|
+
})();
|
|
568
|
+
if (box) canvas.richText(laid, box.x + 2, box.y, { color: labelStyle.text });
|
|
470
569
|
}
|
|
471
570
|
}
|
|
472
571
|
|