@liminis/diagrams 0.1.2 → 0.1.3
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 +13 -11
- package/dist/bin/render-c4.d.ts +20 -1
- package/dist/bin/render-c4.js +84 -2
- package/dist/core/layout.js +46 -29
- package/dist/react/C4InteractiveRenderer.d.ts +24 -1
- package/dist/react/C4InteractiveRenderer.js +8 -5
- package/dist/react/renderer.d.ts +23 -1
- package/dist/react/renderer.js +25 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -7,13 +7,15 @@ editing.
|
|
|
7
7
|
Extracted from [`@liminis/editor`](https://github.com/verveguy/liminis-editor), where it
|
|
8
8
|
renders ` ```c4 ` fenced code blocks. Nothing here is bound to that editor.
|
|
9
9
|
|
|
10
|
+
Documentation: **[v3rv.com/liminis-diagrams](https://v3rv.com/liminis-diagrams/)**.
|
|
11
|
+
|
|
10
12
|
## Demo
|
|
11
13
|
|
|
12
|
-
**[https://v3rv.com/liminis-diagrams/](https://v3rv.com/liminis-diagrams/)** — edit
|
|
14
|
+
**[https://v3rv.com/liminis-diagrams/demo/](https://v3rv.com/liminis-diagrams/demo/)** — edit
|
|
13
15
|
C4-PlantUML source and see it re-render live, drag nodes to reposition them, toggle dark
|
|
14
16
|
mode, and switch between a few preset diagrams. The demo keeps dragged positions in
|
|
15
17
|
memory only, for as long as the tab is open — this package has no persistence of its
|
|
16
|
-
own (see [Recipe 3](
|
|
18
|
+
own (see [Recipe 3](https://v3rv.com/liminis-diagrams/recipes/#recipe-3-position-persistence--the-hosts-choice)),
|
|
17
19
|
and neither does this demo.
|
|
18
20
|
|
|
19
21
|
## Install
|
|
@@ -25,12 +27,12 @@ npm install @liminis/diagrams
|
|
|
25
27
|
`react` and `react-dom` are **optional** peers. Installing the package gets you
|
|
26
28
|
`@dagrejs/dagre` and nothing else, so `@liminis/diagrams/core` works in a CLI or CI job
|
|
27
29
|
with no React on disk. Install the peers if you use `/react` or `/server` — see
|
|
28
|
-
[
|
|
30
|
+
[Architecture](https://v3rv.com/liminis-diagrams/architecture/) for why the split exists and which entry
|
|
29
31
|
point to pick.
|
|
30
32
|
|
|
31
33
|
## Not sure this package does what you're assuming?
|
|
32
34
|
|
|
33
|
-
Read [Limitations](
|
|
35
|
+
Read [Limitations](https://v3rv.com/liminis-diagrams/#limitations--read-this-first) before you build
|
|
34
36
|
against this package. In short: no editing UI, no persistence, element IDs aren't
|
|
35
37
|
stable across diagrams, no cross-diagram links.
|
|
36
38
|
|
|
@@ -85,21 +87,21 @@ import { C4InteractiveRenderer } from '@liminis/diagrams/react';
|
|
|
85
87
|
|
|
86
88
|
Pass `manualPositions` to `layoutC4Diagram` to bypass dagre for the elements you have
|
|
87
89
|
positions for. Persisting them is entirely your call — see
|
|
88
|
-
[Recipe 3](
|
|
90
|
+
[Recipe 3](https://v3rv.com/liminis-diagrams/recipes/#recipe-3-position-persistence--the-hosts-choice) for a worked
|
|
89
91
|
example (including how `@liminis/editor` does it) and why this package itself never
|
|
90
92
|
writes them anywhere.
|
|
91
93
|
|
|
92
94
|
## Render on the command line
|
|
93
95
|
|
|
94
96
|
```bash
|
|
95
|
-
npx --package=@liminis/diagrams -- render-c4 diagram.puml
|
|
97
|
+
npx --package=@liminis/diagrams --package=react --package=react-dom -- render-c4 diagram.puml
|
|
96
98
|
# diagram.puml -> diagram.svg
|
|
97
99
|
```
|
|
98
100
|
|
|
99
101
|
Useful for pre-rendering diagrams so a plain `` is enough for
|
|
100
102
|
GitHub (or any markdown renderer) to show them — see
|
|
101
|
-
[
|
|
102
|
-
[
|
|
103
|
+
[Rendering diagrams on GitHub](https://v3rv.com/liminis-diagrams/github-integration/) for the CI recipe, and
|
|
104
|
+
[Rendering diagrams in Claude Code](https://v3rv.com/liminis-diagrams/claude-code-integration/) for getting Claude
|
|
103
105
|
to render real diagrams instead of hand-drawing them.
|
|
104
106
|
|
|
105
107
|
## Supported syntax
|
|
@@ -107,13 +109,13 @@ to render real diagrams instead of hand-drawing them.
|
|
|
107
109
|
`Person`, `System`, `Container`, `Component` and their `_Ext` / `Db` / `Queue` variants,
|
|
108
110
|
plus `Deployment_Node`, `Node`, and `InfrastructureNode` variants; boundary macros;
|
|
109
111
|
`Rel` (with directional variants) and `BiRel`. See
|
|
110
|
-
[
|
|
112
|
+
[the C4-PlantUML reference](https://v3rv.com/liminis-diagrams/dsl-reference/) for the full macro table and exactly
|
|
111
113
|
which directives (`@startuml`, `!include`, `SHOW_LEGEND()`, `LAYOUT_*`, …) are applied
|
|
112
114
|
versus silently stripped.
|
|
113
115
|
|
|
114
116
|
## Documentation
|
|
115
117
|
|
|
116
|
-
Building a tool on top of this package? [
|
|
118
|
+
Building a tool on top of this package? [the documentation site](https://v3rv.com/liminis-diagrams/) covers the
|
|
117
119
|
entry-point boundary, the full DSL reference, the data model, and runnable recipes for
|
|
118
120
|
headless rendering, embedding the interactive renderer, and position persistence.
|
|
119
121
|
|
|
@@ -122,7 +124,7 @@ headless rendering, embedding the interactive renderer, and position persistence
|
|
|
122
124
|
The commit history predates this repository: it was recovered from
|
|
123
125
|
`verveguy/liminis` (`liminis-app/src/editor/app/editor/c4/`, later
|
|
124
126
|
`packages/editor/src/app/editor/c4/`) and carries development from 2026-03-18 onward.
|
|
125
|
-
`git log --follow` works across the move.
|
|
127
|
+
`git log --follow` works across the move.
|
|
126
128
|
|
|
127
129
|
## License
|
|
128
130
|
|
package/dist/bin/render-c4.d.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* meant for pre-rendering diagrams in CI so that a plain `` in
|
|
7
7
|
* a markdown file is enough for GitHub (or any other markdown renderer) to show
|
|
8
8
|
* it — no live rendering service, no image-provider proxy, nothing to host.
|
|
9
|
-
* See
|
|
9
|
+
* See https://v3rv.com/liminis-diagrams/github-integration/ for the recipe this exists for.
|
|
10
10
|
*
|
|
11
11
|
* No dependency is added for argument parsing: flags are hand-rolled to match
|
|
12
12
|
* the style of the other scripts in this repo (guard-publish.mjs,
|
|
@@ -14,6 +14,8 @@
|
|
|
14
14
|
*/
|
|
15
15
|
export interface Options {
|
|
16
16
|
files: string[];
|
|
17
|
+
/** Extract ```c4 fences from markdown inputs instead of treating the whole file as source. */
|
|
18
|
+
fromMarkdown: boolean;
|
|
17
19
|
dark: boolean;
|
|
18
20
|
out?: string;
|
|
19
21
|
outDir?: string;
|
|
@@ -27,4 +29,21 @@ export declare function outputPathFor(inputPath: string, options: Options): stri
|
|
|
27
29
|
* (without the `render-c4: ` prefix `main` adds).
|
|
28
30
|
*/
|
|
29
31
|
export declare function validateStdinCombination(options: Options): string | null;
|
|
32
|
+
/**
|
|
33
|
+
* ```c4 fences in a markdown file, with the 1-based line the fence body starts on
|
|
34
|
+
* so errors point at the real location in the file rather than at the snippet.
|
|
35
|
+
*
|
|
36
|
+
* Diagrams live in fenced blocks far more often than in standalone .puml files —
|
|
37
|
+
* that is how @liminis/editor stores them, how this package's own docs are
|
|
38
|
+
* written, and what a markdown-based diagram wiki produces. A renderer that only
|
|
39
|
+
* understood whole files would not fit the common case.
|
|
40
|
+
*
|
|
41
|
+
* A fence tagged `invalid` is skipped: a page documenting parse errors needs
|
|
42
|
+
* source that does not parse, and that is content rather than a defect.
|
|
43
|
+
*/
|
|
44
|
+
export declare function extractC4Fences(markdown: string): {
|
|
45
|
+
source: string;
|
|
46
|
+
line: number;
|
|
47
|
+
index: number;
|
|
48
|
+
}[];
|
|
30
49
|
export declare function renderFiles(options: Options): number;
|
package/dist/bin/render-c4.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* meant for pre-rendering diagrams in CI so that a plain `` in
|
|
7
7
|
* a markdown file is enough for GitHub (or any other markdown renderer) to show
|
|
8
8
|
* it — no live rendering service, no image-provider proxy, nothing to host.
|
|
9
|
-
* See
|
|
9
|
+
* See https://v3rv.com/liminis-diagrams/github-integration/ for the recipe this exists for.
|
|
10
10
|
*
|
|
11
11
|
* No dependency is added for argument parsing: flags are hand-rolled to match
|
|
12
12
|
* the style of the other scripts in this repo (guard-publish.mjs,
|
|
@@ -22,6 +22,8 @@ function printUsage() {
|
|
|
22
22
|
Render C4-PlantUML source files to SVG.
|
|
23
23
|
|
|
24
24
|
Options:
|
|
25
|
+
--from-markdown Read fenced c4 blocks out of markdown inputs rather than
|
|
26
|
+
treating each whole file as diagram source
|
|
25
27
|
--dark Render in dark mode
|
|
26
28
|
-o, --out <file> Output path (only valid with exactly one input file)
|
|
27
29
|
--out-dir <dir> Write outputs here, preserving basenames (.svg extension)
|
|
@@ -31,7 +33,7 @@ Options:
|
|
|
31
33
|
`);
|
|
32
34
|
}
|
|
33
35
|
export function parseArgs(argv) {
|
|
34
|
-
const options = { files: [], dark: false, check: false, stdin: false };
|
|
36
|
+
const options = { files: [], fromMarkdown: false, dark: false, check: false, stdin: false };
|
|
35
37
|
for (let i = 0; i < argv.length; i++) {
|
|
36
38
|
const arg = argv[i];
|
|
37
39
|
switch (arg) {
|
|
@@ -41,6 +43,9 @@ export function parseArgs(argv) {
|
|
|
41
43
|
case '--dark':
|
|
42
44
|
options.dark = true;
|
|
43
45
|
break;
|
|
46
|
+
case '--from-markdown':
|
|
47
|
+
options.fromMarkdown = true;
|
|
48
|
+
break;
|
|
44
49
|
case '--check':
|
|
45
50
|
options.check = true;
|
|
46
51
|
break;
|
|
@@ -92,7 +97,84 @@ export function validateStdinCombination(options) {
|
|
|
92
97
|
}
|
|
93
98
|
return null;
|
|
94
99
|
}
|
|
100
|
+
/**
|
|
101
|
+
* ```c4 fences in a markdown file, with the 1-based line the fence body starts on
|
|
102
|
+
* so errors point at the real location in the file rather than at the snippet.
|
|
103
|
+
*
|
|
104
|
+
* Diagrams live in fenced blocks far more often than in standalone .puml files —
|
|
105
|
+
* that is how @liminis/editor stores them, how this package's own docs are
|
|
106
|
+
* written, and what a markdown-based diagram wiki produces. A renderer that only
|
|
107
|
+
* understood whole files would not fit the common case.
|
|
108
|
+
*
|
|
109
|
+
* A fence tagged `invalid` is skipped: a page documenting parse errors needs
|
|
110
|
+
* source that does not parse, and that is content rather than a defect.
|
|
111
|
+
*/
|
|
112
|
+
export function extractC4Fences(markdown) {
|
|
113
|
+
const out = [];
|
|
114
|
+
const fence = /^```c4([^\n]*)\n([\s\S]*?)\n```$/gm;
|
|
115
|
+
let index = 0;
|
|
116
|
+
for (const match of markdown.matchAll(fence)) {
|
|
117
|
+
if (/\binvalid\b/.test(match[1] ?? ''))
|
|
118
|
+
continue;
|
|
119
|
+
index++;
|
|
120
|
+
const before = markdown.slice(0, match.index ?? 0);
|
|
121
|
+
out.push({
|
|
122
|
+
source: match[2],
|
|
123
|
+
line: before.split('\n').length + 1,
|
|
124
|
+
index,
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
return out;
|
|
128
|
+
}
|
|
129
|
+
function renderMarkdownFences(options) {
|
|
130
|
+
let failures = 0;
|
|
131
|
+
for (const inputPath of options.files) {
|
|
132
|
+
let markdown;
|
|
133
|
+
try {
|
|
134
|
+
markdown = readFileSync(inputPath, 'utf-8');
|
|
135
|
+
}
|
|
136
|
+
catch (err) {
|
|
137
|
+
failures++;
|
|
138
|
+
console.error(`${inputPath}: ${err instanceof Error ? err.message : String(err)}`);
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
const fences = extractC4Fences(markdown);
|
|
142
|
+
if (fences.length === 0)
|
|
143
|
+
continue;
|
|
144
|
+
for (const fence of fences) {
|
|
145
|
+
const { svg, errors } = renderC4DiagramToSVG(fence.source, options.dark);
|
|
146
|
+
if (errors.length > 0) {
|
|
147
|
+
failures++;
|
|
148
|
+
for (const error of errors) {
|
|
149
|
+
// Offset into the containing file, so the message is navigable.
|
|
150
|
+
console.error(`${inputPath}:${fence.line + error.line - 1}:${error.column}: ${error.message}`);
|
|
151
|
+
}
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
if (options.check)
|
|
155
|
+
continue;
|
|
156
|
+
const base = basename(inputPath, extname(inputPath));
|
|
157
|
+
const svgName = `${base}-${fence.index}.svg`;
|
|
158
|
+
const outPath = options.outDir
|
|
159
|
+
? join(options.outDir, svgName)
|
|
160
|
+
: join(dirname(inputPath), svgName);
|
|
161
|
+
try {
|
|
162
|
+
mkdirSync(dirname(outPath), { recursive: true });
|
|
163
|
+
writeFileSync(outPath, svg);
|
|
164
|
+
}
|
|
165
|
+
catch (err) {
|
|
166
|
+
failures++;
|
|
167
|
+
console.error(`${inputPath}: failed to write ${outPath}: ${err instanceof Error ? err.message : String(err)}`);
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
console.log(`${inputPath} [${fence.index}] -> ${outPath}`);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return failures > 0 ? 2 : 0;
|
|
174
|
+
}
|
|
95
175
|
export function renderFiles(options) {
|
|
176
|
+
if (options.fromMarkdown)
|
|
177
|
+
return renderMarkdownFences(options);
|
|
96
178
|
if (options.out && options.files.length > 1) {
|
|
97
179
|
console.error('render-c4: -o/--out only applies with a single input file');
|
|
98
180
|
return 1;
|
package/dist/core/layout.js
CHANGED
|
@@ -757,29 +757,63 @@ function layoutWithManualPositions(diagram, options, manualPositions) {
|
|
|
757
757
|
}
|
|
758
758
|
// Calculate edges using existing function
|
|
759
759
|
const edges = calculateEdges(diagram.relationships, nodeMap);
|
|
760
|
-
|
|
761
|
-
|
|
760
|
+
return { nodes: allNodes, edges, ...boundsFor(allNodes, edges) };
|
|
761
|
+
}
|
|
762
|
+
/**
|
|
763
|
+
* The viewBox and dimensions that actually contain the laid-out diagram.
|
|
764
|
+
*
|
|
765
|
+
* Shared by both layout paths, because they were only ever different by
|
|
766
|
+
* accident. The manual path always computed a real bounding box — dragging a
|
|
767
|
+
* node above or left of the origin produces negative coordinates, and the
|
|
768
|
+
* viewBox origin moves to cover them rather than shifting every node, which
|
|
769
|
+
* would desynchronise rendered positions from the positions a host persists.
|
|
770
|
+
*
|
|
771
|
+
* The auto path hardcoded `viewBoxX: 0, viewBoxY: 0` on the premise that dagre
|
|
772
|
+
* never emits negative coordinates. That premise was wrong, and documented as
|
|
773
|
+
* fact in data-model.md. Dagre's own output is non-negative, but the
|
|
774
|
+
* cross-boundary alignment pass that runs after it shifts elements outside a
|
|
775
|
+
* boundary to line up with their targets inside it, and that can move them left
|
|
776
|
+
* of the origin. The result was a `viewBox` starting at 0 with content at
|
|
777
|
+
* negative x: clipped, and unreachable by scrolling, because a viewBox is a
|
|
778
|
+
* window rather than a canvas. Four lines of C4 reproduce it — a Person outside
|
|
779
|
+
* a System_Boundary with a Rel to something inside.
|
|
780
|
+
*
|
|
781
|
+
* Edge points are included as well as node rects: an edge routed around a
|
|
782
|
+
* boundary can leave the union of the node rectangles.
|
|
783
|
+
*
|
|
784
|
+
* Diagrams that were already correct are unaffected. Where nothing sits within
|
|
785
|
+
* BOUNDARY_PADDING of the origin, `Math.min(0, …)` yields 0 and the width and
|
|
786
|
+
* height are what they were.
|
|
787
|
+
*/
|
|
788
|
+
function boundsFor(nodes, edges) {
|
|
762
789
|
let minX = Infinity;
|
|
763
790
|
let minY = Infinity;
|
|
764
791
|
let maxX = 0;
|
|
765
792
|
let maxY = 0;
|
|
766
|
-
for (const node of
|
|
793
|
+
for (const node of nodes) {
|
|
767
794
|
minX = Math.min(minX, node.x);
|
|
768
795
|
minY = Math.min(minY, node.y);
|
|
769
796
|
maxX = Math.max(maxX, node.x + node.width);
|
|
770
797
|
maxY = Math.max(maxY, node.y + node.height);
|
|
771
798
|
}
|
|
772
|
-
|
|
773
|
-
|
|
799
|
+
for (const edge of edges) {
|
|
800
|
+
for (const point of edge.points) {
|
|
801
|
+
minX = Math.min(minX, point.x);
|
|
802
|
+
minY = Math.min(minY, point.y);
|
|
803
|
+
maxX = Math.max(maxX, point.x);
|
|
804
|
+
maxY = Math.max(maxY, point.y);
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
// An empty diagram leaves the minima at Infinity; treat it as the origin.
|
|
808
|
+
if (!Number.isFinite(minX))
|
|
809
|
+
minX = 0;
|
|
810
|
+
if (!Number.isFinite(minY))
|
|
811
|
+
minY = 0;
|
|
774
812
|
const viewBoxX = Math.min(0, minX - BOUNDARY_PADDING);
|
|
775
813
|
const viewBoxY = Math.min(0, minY - BOUNDARY_PADDING);
|
|
776
|
-
const width = maxX + BOUNDARY_PADDING - viewBoxX;
|
|
777
|
-
const height = maxY + BOUNDARY_PADDING - viewBoxY;
|
|
778
814
|
return {
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
width,
|
|
782
|
-
height,
|
|
815
|
+
width: maxX + BOUNDARY_PADDING - viewBoxX,
|
|
816
|
+
height: maxY + BOUNDARY_PADDING - viewBoxY,
|
|
783
817
|
viewBoxX,
|
|
784
818
|
viewBoxY,
|
|
785
819
|
};
|
|
@@ -826,24 +860,7 @@ export function layoutC4Diagram(diagram, options, manualPositions) {
|
|
|
826
860
|
alignCrossBoundaryElements(layoutNodes, diagram.relationships, nodeMap);
|
|
827
861
|
// Calculate edges
|
|
828
862
|
const edges = calculateEdges(diagram.relationships, nodeMap);
|
|
829
|
-
|
|
830
|
-
let width = 0;
|
|
831
|
-
let height = 0;
|
|
832
|
-
for (const node of allNodes) {
|
|
833
|
-
width = Math.max(width, node.x + node.width);
|
|
834
|
-
height = Math.max(height, node.y + node.height);
|
|
835
|
-
}
|
|
836
|
-
// Add margin
|
|
837
|
-
width += BOUNDARY_PADDING;
|
|
838
|
-
height += BOUNDARY_PADDING;
|
|
839
|
-
const result = {
|
|
840
|
-
nodes: allNodes,
|
|
841
|
-
edges,
|
|
842
|
-
width,
|
|
843
|
-
height,
|
|
844
|
-
viewBoxX: 0,
|
|
845
|
-
viewBoxY: 0,
|
|
846
|
-
};
|
|
863
|
+
const result = { nodes: allNodes, edges, ...boundsFor(allNodes, edges) };
|
|
847
864
|
roundGeometryInPlace(result);
|
|
848
865
|
return result;
|
|
849
866
|
}
|
|
@@ -25,6 +25,29 @@ export interface C4InteractiveRendererProps {
|
|
|
25
25
|
x: number;
|
|
26
26
|
y: number;
|
|
27
27
|
}>) => void;
|
|
28
|
+
/**
|
|
29
|
+
* Scale factor for the rendered diagram. 1 is actual size; 2 draws it twice as
|
|
30
|
+
* large. Defaults to 1, so a host that does not care is unaffected.
|
|
31
|
+
*
|
|
32
|
+
* This scales the SVG's rendered `width`/`height` and leaves the `viewBox`
|
|
33
|
+
* alone, rather than applying a CSS transform. Three things follow, and they
|
|
34
|
+
* are the reason for doing it this way:
|
|
35
|
+
*
|
|
36
|
+
* - Dragging keeps working untouched. Screen coordinates are mapped through
|
|
37
|
+
* `getScreenCTM().inverse()`, and that matrix already carries the ratio
|
|
38
|
+
* between the viewBox and the rendered size — so a node still lands under
|
|
39
|
+
* the pointer at any zoom, with no arithmetic anywhere in the drag code.
|
|
40
|
+
* - The diagram genuinely occupies more space, so a scrolling container
|
|
41
|
+
* scrolls it. A CSS `transform` is painted after layout: the element still
|
|
42
|
+
* reports its unscaled size, and the overflow it appears to produce cannot
|
|
43
|
+
* be scrolled to.
|
|
44
|
+
* - Text stays sharp. It is re-rendered at the new scale rather than being a
|
|
45
|
+
* bitmap stretched over it.
|
|
46
|
+
*
|
|
47
|
+
* Controls are deliberately not included. What a zoom control should look like
|
|
48
|
+
* is the host's business — this package renders diagrams.
|
|
49
|
+
*/
|
|
50
|
+
zoom?: number;
|
|
28
51
|
}
|
|
29
52
|
/**
|
|
30
53
|
* Interactive C4 diagram renderer with drag support.
|
|
@@ -32,5 +55,5 @@ export interface C4InteractiveRendererProps {
|
|
|
32
55
|
* When isEditMode is true, nodes can be dragged to new positions.
|
|
33
56
|
* Edges and boundaries are recalculated in real-time during drag.
|
|
34
57
|
*/
|
|
35
|
-
export declare function C4InteractiveRenderer({ diagram, isDarkMode, isEditMode, manualPositions, onPositionChange, }: C4InteractiveRendererProps): JSX.Element;
|
|
58
|
+
export declare function C4InteractiveRenderer({ diagram, isDarkMode, isEditMode, manualPositions, onPositionChange, zoom, }: C4InteractiveRendererProps): JSX.Element;
|
|
36
59
|
export default C4InteractiveRenderer;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
2
|
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
3
|
-
import { C4RendererContent, computeLegendInfo } from './renderer.js';
|
|
3
|
+
import { C4RendererContent, computeLegendInfo, normaliseZoom } from './renderer.js';
|
|
4
4
|
import { layoutC4Diagram } from '../core/layout.js';
|
|
5
5
|
import { useC4DiagramDrag } from './hooks/useC4DiagramDrag.js';
|
|
6
6
|
/** Synthetic ID used to store legend position in manual positions map */
|
|
@@ -51,7 +51,7 @@ function collectDescendantIds(elements) {
|
|
|
51
51
|
* When isEditMode is true, nodes can be dragged to new positions.
|
|
52
52
|
* Edges and boundaries are recalculated in real-time during drag.
|
|
53
53
|
*/
|
|
54
|
-
export function C4InteractiveRenderer({ diagram, isDarkMode, isEditMode, manualPositions, onPositionChange, }) {
|
|
54
|
+
export function C4InteractiveRenderer({ diagram, isDarkMode, isEditMode, manualPositions, onPositionChange, zoom = 1, }) {
|
|
55
55
|
const svgRef = useRef(null);
|
|
56
56
|
// Local positions during drag (merged with persisted positions)
|
|
57
57
|
const [dragPositions, setDragPositions] = useState({});
|
|
@@ -222,12 +222,15 @@ export function C4InteractiveRenderer({ diagram, isDarkMode, isEditMode, manualP
|
|
|
222
222
|
return pos ?? null;
|
|
223
223
|
}, [effectivePositions]);
|
|
224
224
|
// Render with interactive wrappers
|
|
225
|
-
return (_jsx(C4InteractiveSvg, { layout: layout, isDarkMode: isDarkMode, isEditMode: isEditMode, draggedNodeId: draggedNodeId, svgRef: svgRef, onNodeMouseDown: startNodeDrag, legendInfo: legendInfo, legendPositionOverride: legendPositionOverride }));
|
|
225
|
+
return (_jsx(C4InteractiveSvg, { layout: layout, isDarkMode: isDarkMode, isEditMode: isEditMode, draggedNodeId: draggedNodeId, svgRef: svgRef, onNodeMouseDown: startNodeDrag, legendInfo: legendInfo, legendPositionOverride: legendPositionOverride, zoom: zoom }));
|
|
226
226
|
}
|
|
227
227
|
/**
|
|
228
228
|
* SVG wrapper that adds interactive overlays for drag handling.
|
|
229
229
|
*/
|
|
230
|
-
function C4InteractiveSvg({ layout, isDarkMode, isEditMode, draggedNodeId, svgRef, onNodeMouseDown, legendInfo, legendPositionOverride, }) {
|
|
230
|
+
function C4InteractiveSvg({ layout, isDarkMode, isEditMode, draggedNodeId, svgRef, onNodeMouseDown, legendInfo, legendPositionOverride, zoom, }) {
|
|
231
|
+
// Guarded here as well as in C4Renderer: both are public entry points, and a
|
|
232
|
+
// host reaching this one never passes through the other.
|
|
233
|
+
const scale = normaliseZoom(zoom);
|
|
231
234
|
// Get colors based on theme
|
|
232
235
|
const handleColor = isDarkMode ? '#a0a0a0' : '#505050';
|
|
233
236
|
// Create hit areas for each node (and legend if present)
|
|
@@ -276,7 +279,7 @@ function C4InteractiveSvg({ layout, isDarkMode, isEditMode, draggedNodeId, svgRe
|
|
|
276
279
|
}
|
|
277
280
|
return { svgWidth: w, svgHeight: h };
|
|
278
281
|
}, [layout.width, layout.height, layout.viewBoxX, layout.viewBoxY, legendInfo, legendPositionOverride]);
|
|
279
|
-
return (_jsx("div", { style: { position: 'relative' }, children: _jsxs("svg", { ref: svgRef, width: svgWidth, height: svgHeight, viewBox: `${layout.viewBoxX} ${layout.viewBoxY} ${svgWidth} ${svgHeight}`, xmlns: "http://www.w3.org/2000/svg", "data-diagram": "c4", style: {
|
|
282
|
+
return (_jsx("div", { style: { position: 'relative' }, children: _jsxs("svg", { ref: svgRef, width: svgWidth * scale, height: svgHeight * scale, viewBox: `${layout.viewBoxX} ${layout.viewBoxY} ${svgWidth} ${svgHeight}`, xmlns: "http://www.w3.org/2000/svg", "data-diagram": "c4", style: {
|
|
280
283
|
fontFamily: 'system-ui, -apple-system, sans-serif',
|
|
281
284
|
cursor: isEditMode ? (draggedNodeId ? 'grabbing' : 'default') : 'default',
|
|
282
285
|
}, children: [_jsx(C4RendererContent, { layout: layout, isDarkMode: isDarkMode, legendPositionOverride: legendPositionOverride }), isEditMode && (_jsx("g", { className: "interactive-layer", children: hitAreas.map((area) => (_jsx("rect", { "data-node-id": area.id, x: area.x, y: area.y, width: area.width, height: area.height, fill: "transparent", stroke: "transparent", style: {
|
package/dist/react/renderer.d.ts
CHANGED
|
@@ -27,11 +27,33 @@ export declare function computeLegendInfo(layout: LayoutResult): {
|
|
|
27
27
|
width: number;
|
|
28
28
|
height: number;
|
|
29
29
|
} | null;
|
|
30
|
+
/**
|
|
31
|
+
* A zoom factor that cannot produce an invalid SVG.
|
|
32
|
+
*
|
|
33
|
+
* `width`/`height` are attributes on a public entry point, so whatever a host
|
|
34
|
+
* passes ends up in the DOM. `0` collapses the diagram, a negative number is
|
|
35
|
+
* invalid, and `NaN` — the likely one, arriving from an uninitialised state
|
|
36
|
+
* variable or a division — renders nothing at all with no error to explain it.
|
|
37
|
+
* Anything that is not a positive finite number falls back to actual size.
|
|
38
|
+
*
|
|
39
|
+
* The upper bound is a guard against a browser being asked to lay out a
|
|
40
|
+
* hundred-thousand-pixel SVG, not a considered maximum; the lower bound is the
|
|
41
|
+
* point past which a diagram is a smudge. A host wanting more can scale the
|
|
42
|
+
* container.
|
|
43
|
+
*/
|
|
44
|
+
export declare function normaliseZoom(zoom: number): number;
|
|
30
45
|
export interface C4RendererProps {
|
|
31
46
|
/** Layout result from the layout engine */
|
|
32
47
|
layout: LayoutResult;
|
|
33
48
|
/** Whether dark mode is enabled */
|
|
34
49
|
isDarkMode: boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Scale factor for the rendered diagram. 1 is actual size. Scales the SVG's
|
|
52
|
+
* width/height and leaves the viewBox alone, so text is re-rendered at the new
|
|
53
|
+
* scale rather than stretched, and a scrolling container can actually scroll
|
|
54
|
+
* the result. Defaults to 1.
|
|
55
|
+
*/
|
|
56
|
+
zoom?: number;
|
|
35
57
|
}
|
|
36
58
|
/**
|
|
37
59
|
* C4 Diagram SVG Renderer.
|
|
@@ -39,7 +61,7 @@ export interface C4RendererProps {
|
|
|
39
61
|
* Pure component: takes layout + theme, returns SVG.
|
|
40
62
|
* Used directly in the editor and via renderToStaticMarkup for publishing.
|
|
41
63
|
*/
|
|
42
|
-
export declare function C4Renderer({ layout, isDarkMode }: C4RendererProps): JSX.Element;
|
|
64
|
+
export declare function C4Renderer({ layout, isDarkMode, zoom }: C4RendererProps): JSX.Element;
|
|
43
65
|
/**
|
|
44
66
|
* Renders C4 diagram content (nodes, edges, legend) as SVG group elements.
|
|
45
67
|
* Exported for use by C4InteractiveRenderer to embed diagram content
|
package/dist/react/renderer.js
CHANGED
|
@@ -523,13 +523,36 @@ function renderNode(node, colors, allNodes, parentMap) {
|
|
|
523
523
|
return _jsx(Container, { node: node, colors: colors, allNodes: allNodes, parentMap: parentMap }, node.id);
|
|
524
524
|
}
|
|
525
525
|
}
|
|
526
|
+
// =============================================================================
|
|
527
|
+
// MAIN COMPONENT
|
|
528
|
+
// =============================================================================
|
|
529
|
+
/**
|
|
530
|
+
* A zoom factor that cannot produce an invalid SVG.
|
|
531
|
+
*
|
|
532
|
+
* `width`/`height` are attributes on a public entry point, so whatever a host
|
|
533
|
+
* passes ends up in the DOM. `0` collapses the diagram, a negative number is
|
|
534
|
+
* invalid, and `NaN` — the likely one, arriving from an uninitialised state
|
|
535
|
+
* variable or a division — renders nothing at all with no error to explain it.
|
|
536
|
+
* Anything that is not a positive finite number falls back to actual size.
|
|
537
|
+
*
|
|
538
|
+
* The upper bound is a guard against a browser being asked to lay out a
|
|
539
|
+
* hundred-thousand-pixel SVG, not a considered maximum; the lower bound is the
|
|
540
|
+
* point past which a diagram is a smudge. A host wanting more can scale the
|
|
541
|
+
* container.
|
|
542
|
+
*/
|
|
543
|
+
export function normaliseZoom(zoom) {
|
|
544
|
+
if (!Number.isFinite(zoom) || zoom <= 0)
|
|
545
|
+
return 1;
|
|
546
|
+
return Math.min(Math.max(zoom, 0.05), 50);
|
|
547
|
+
}
|
|
526
548
|
/**
|
|
527
549
|
* C4 Diagram SVG Renderer.
|
|
528
550
|
*
|
|
529
551
|
* Pure component: takes layout + theme, returns SVG.
|
|
530
552
|
* Used directly in the editor and via renderToStaticMarkup for publishing.
|
|
531
553
|
*/
|
|
532
|
-
export function C4Renderer({ layout, isDarkMode }) {
|
|
554
|
+
export function C4Renderer({ layout, isDarkMode, zoom = 1 }) {
|
|
555
|
+
const scale = normaliseZoom(zoom);
|
|
533
556
|
const colors = getColors(isDarkMode);
|
|
534
557
|
const boundaryNodes = layout.nodes.filter((n) => n.element.type === 'system' &&
|
|
535
558
|
((n.children?.length ?? 0) > 0 || n.element.properties.style === 'boundary'));
|
|
@@ -546,7 +569,7 @@ export function C4Renderer({ layout, isDarkMode }) {
|
|
|
546
569
|
totalWidth = lp.totalWidth;
|
|
547
570
|
totalHeight = lp.totalHeight;
|
|
548
571
|
}
|
|
549
|
-
return (_jsxs("svg", { width: totalWidth, height: totalHeight, viewBox: `${layout.viewBoxX} ${layout.viewBoxY} ${totalWidth} ${totalHeight}`, xmlns: "http://www.w3.org/2000/svg", "data-diagram": "c4", style: { fontFamily: 'system-ui, -apple-system, sans-serif' }, children: [_jsx("g", { className: "boundaries-layer", children: boundaryNodes.map((node) => renderNode(node, colors, layout.nodes, parentMap)) }), _jsx("g", { className: "nodes-layer", children: regularNodes.map((node) => renderNode(node, colors, layout.nodes, parentMap)) }), _jsx("g", { className: "edges-layer", children: processedEdges.map((edge, i) => (_jsx(EdgeComponent, { edge: edge, colors: colors }, `${edge.source}-${edge.target}-${i}`))) }), legendPlacement && legendEntries.length > 0 && (_jsx(Legend, { entries: legendEntries, placement: legendPlacement, colors: colors }))] }));
|
|
572
|
+
return (_jsxs("svg", { width: totalWidth * scale, height: totalHeight * scale, viewBox: `${layout.viewBoxX} ${layout.viewBoxY} ${totalWidth} ${totalHeight}`, xmlns: "http://www.w3.org/2000/svg", "data-diagram": "c4", style: { fontFamily: 'system-ui, -apple-system, sans-serif' }, children: [_jsx("g", { className: "boundaries-layer", children: boundaryNodes.map((node) => renderNode(node, colors, layout.nodes, parentMap)) }), _jsx("g", { className: "nodes-layer", children: regularNodes.map((node) => renderNode(node, colors, layout.nodes, parentMap)) }), _jsx("g", { className: "edges-layer", children: processedEdges.map((edge, i) => (_jsx(EdgeComponent, { edge: edge, colors: colors }, `${edge.source}-${edge.target}-${i}`))) }), legendPlacement && legendEntries.length > 0 && (_jsx(Legend, { entries: legendEntries, placement: legendPlacement, colors: colors }))] }));
|
|
550
573
|
}
|
|
551
574
|
export function C4RendererContent({ layout, isDarkMode, legendPositionOverride }) {
|
|
552
575
|
const colors = getColors(isDarkMode);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@liminis/diagrams",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "C4 architecture diagrams: parse C4-PlantUML, lay out with dagre, render to SVG",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"//repository": "Not cosmetic, and not optional. npm matches this URL against the GitHub Actions OIDC claim when publishing with --provenance; without it the registry rejects the publish outright (E422) after the release tag has already been cut. That is exactly how 0.1.0's first release attempt failed (#6). The `git+https://` scheme and the `.git` suffix are both part of the match \u2014 the SSH form does not work.",
|