@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 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
- <table>
19
- <tr>
20
- <td width="50%"><img src="docs/assets/sequence-checkout-flow.png" alt="Sequence diagram" /></td>
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"><sub>Every image above is generated by excaliplant from the files in <a href="examples/"><code>examples/</code></a>. Browse all 35 in the <a href="docs/gallery.md"><b>gallery</b></a>.</sub></p>
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 | Start | Highlights |
110
+ | Diagram | Examples | Highlights |
87
111
  | --- | --- | --- |
88
- | [Activity diagram](docs/gallery.md#activity) | `@startuml` `@startactivity` | New syntax with if/switch/while/repeat/fork/split, partitions, notes and swimlanes. |
89
- | [Activity diagram (legacy syntax)](docs/gallery.md#legacy-activity) | `@startuml` | The classic `(*) --> "Activity"` syntax with sync bars and branches. |
90
- | [ArchiMate diagram](docs/gallery.md#archimate) | `@startuml` | ArchiMate elements by layer with icons, junctions and `Rel_*` macros. |
91
- | [AsciiMath formula](docs/gallery.md#math) | `@startmath` | AsciiMath formulas. |
92
- | [Chart](docs/gallery.md#chart) | `@startchart` | Bar, line, area and scatter charts. |
93
- | [Chen ER diagram](docs/gallery.md#chen) | `@startchen` | Chen ER diagrams: entities, weak entities, relationships, attributes, cardinalities. |
94
- | [Chronology](docs/gallery.md#chronology) | `@startchronology` | Events and spans on a time line with links. |
95
- | [Class / IE diagram](docs/gallery.md#class) | `@startuml` | Classes, interfaces, enums, generics, members, packages, all UML relations, IE crow's feet. |
96
- | [Component diagram](docs/gallery.md#component) | `@startuml` | Components, interfaces, ports, packages, nodes — and C4 via `!include <C4/...>`. |
97
- | [Creole text](docs/gallery.md#creole) | `@startcreole` | Rich text with headings, lists, tables and links. |
98
- | [Deployment diagram](docs/gallery.md#deployment) | `@startuml` | Nodes, clouds, databases, queues, artifacts and nested deployment targets. |
99
- | [Ditaa ASCII art](docs/gallery.md#ditaa) | `@startditaa` `@startuml` | ASCII art boxes, lines, arrows and colour tags. |
100
- | [EBNF railroad](docs/gallery.md#ebnf) | `@startebnf` | EBNF grammars as railroad diagrams. |
101
- | [File tree](docs/gallery.md#files) | `@startfiles` | File trees from path lists with notes. |
102
- | [Gantt chart](docs/gallery.md#gantt) | `@startgantt` `@startuml` | Tasks, dependencies, milestones, progress, resources, closed days, today marker. |
103
- | [Graphviz DOT](docs/gallery.md#dot) | `@startdot` | Graphviz DOT graphs laid out with ELK. |
104
- | [JSON data](docs/gallery.md#json) | `@startjson` | JSON documents as linked tables with highlights. |
105
- | [LaTeX formula](docs/gallery.md#latex) | `@startlatex` | LaTeX formulas. |
106
- | [MindMap](docs/gallery.md#mindmap) | `@startmindmap` | OrgMode/Markdown outlines on both sides, colours, boxless nodes. |
107
- | [Network diagram](docs/gallery.md#nwdiag) | `@startnwdiag` `@startuml` | Networks, addresses, multi-homed nodes, groups and peer links. |
108
- | [Object diagram](docs/gallery.md#object) | `@startuml` | Objects with fields, maps (`key => value`) and links. |
109
- | [Regex railroad](docs/gallery.md#regex) | `@startregex` | Regular expressions as railroad diagrams. |
110
- | [Salt wireframe](docs/gallery.md#salt) | `@startsalt` `@startuml` | Wireframes: grids, buttons, inputs, check boxes, trees, tabs, group boxes. |
111
- | [Sequence diagram](docs/gallery.md#sequence) | `@startuml` `@startsequence` | Participants, all arrow styles, activations, notes, fragments, dividers, autonumber, boxes. |
112
- | [State diagram](docs/gallery.md#state) | `@startuml` | Composite states, concurrent regions, choice, fork/join, history, entry/exit actions. |
113
- | [Timing diagram](docs/gallery.md#timing) | `@startuml` `@starttiming` | Robust, concise, clock, binary and analog signals with messages and constraints. |
114
- | [Use case diagram](docs/gallery.md#usecase) | `@startuml` | Actors, use cases, system boundaries, include/extend. |
115
- | [Work breakdown structure](docs/gallery.md#wbs) | `@startwbs` | Work breakdown structures as org charts. |
116
- | [YAML data](docs/gallery.md#yaml) | `@startyaml` | YAML documents as linked tables. |
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@grethel-labs/excaliplant",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "Render PlantUML as hand-drawn Excalidraw diagrams, SVG and PNG.",
5
5
  "type": "module",
6
6
  "main": "./src/index.mjs",
@@ -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: Math.min(theme.roughness, 0.6),
96
+ roughness: theme.edgeRoughness,
97
97
  from: ids.get(e.from.id) ?? null,
98
98
  to: ids.get(e.to.id) ?? null,
99
99
  });
@@ -215,7 +215,6 @@ function drawData(laid, c) {
215
215
  stroke: theme.muted,
216
216
  strokeWidth: 1.5,
217
217
  end: "arrow",
218
- curved: true,
219
218
  });
220
219
  draw(target);
221
220
  }
@@ -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
- // horizontal runs
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
- if (
64
- H.has(at(r, c)) &&
65
- (at(r, c) === "-" || at(r, c) === "=" || (H.has(at(r, c + 1)) && /[-=]/.test(at(r, c + 1))))
66
- ) {
67
- const start = c;
68
- let dashed = false;
69
- while (H.has(at(r, c)) || (at(r, c) === "<" && c === start) || at(r, c) === ">") {
70
- if (at(r, c) === "=") dashed = true;
71
- c++;
72
- if (at(r, c - 1) === ">") break;
73
- }
74
- if (c - start >= 2 || /[-=]/.test(at(r, start))) {
75
- segments.push({ x1: start, y1: r, x2: c - 1, y2: r, dashed });
76
- for (let k = start; k < c; k++) if (!CORNER.has(at(r, k))) used[r][k] = true;
77
- }
78
- } else c++;
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
- // vertical runs
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
- if (V.has(at(r, c)) && (at(r, c) === "|" || at(r, c) === ":" || /[|:]/.test(at(r + 1, c)))) {
86
- const start = r;
87
- let dashed = false;
88
- while (V.has(at(r, c)) || at(r, c) === "^" || at(r, c) === "v") {
89
- if (at(r, c) === ":") dashed = true;
90
- r++;
91
- if (at(r - 1, c) === "v") break;
92
- }
93
- if (r - start >= 2 || /[|:]/.test(at(start, c))) {
94
- segments.push({ x1: c, y1: start, x2: c, y2: r - 1, dashed });
95
- for (let k = start; k < r; k++) if (!CORNER.has(at(k, c))) used[k][c] = true;
96
- }
97
- } else r++;
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
- c.line(
274
- [
275
- [px(s.x1), py(s.y1)],
276
- [px(s.x2), py(s.y2)],
277
- ],
278
- { stroke: ink, strokeWidth: 1.5, strokeStyle: s.dashed ? "dashed" : "solid" },
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
- for (const node of nodes) {
32
- const id = canvas.group(() => drawNode(node, canvas, theme));
33
- if (id) ids.set(node.id, id);
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
- drawEdge(edge, from, to, canvas, ids);
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
- let ty = y + 7;
139
- for (const b of geo.blocks) {
140
- canvas.richText(b, x + 12, ty, { color: textColor, align: "left" });
141
- ty += b.height;
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
- for (const b of geo.blocks) {
167
- canvas.richText(b, x + (w - b.width) / 2, ty, { color: textColor });
168
- ty += b.height;
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 b = edge.labelBox;
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 (box && laid) canvas.richText(laid, box.x + 2, box.y, { color: labelStyle.text });
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