@zombie-mermaid/svg-renderer 2.2.1

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.
@@ -0,0 +1,74 @@
1
+ /**
2
+ * Layout engine for zombie-mermaid (ELK.js based).
3
+ *
4
+ * Converts MermaidGraph to ELK's JSON format, runs layout, and converts
5
+ * the result back to PositionedGraph. This is the core layout engine used
6
+ * by all graph-based diagram types (flowcharts, state, ER, class).
7
+ *
8
+ * ELK (Eclipse Layout Kernel) features:
9
+ * - Native orthogonal edge routing (no post-processing needed)
10
+ * - Proper handling of compound nodes (subgraphs)
11
+ * - Support for disconnected graphs
12
+ * - Direction overrides per subgraph
13
+ * - Sophisticated algorithms for complex graphs
14
+ *
15
+ * Uses elk.bundled.js (pure synchronous JS, no WASM/Workers).
16
+ * Safe for Electron, Node, and browser environments.
17
+ *
18
+ * This file is the public entry point (layoutGraphSync, convertToElkFormat).
19
+ * The implementation is split across src/layout-engine/ by pipeline stage:
20
+ * - to-elk.ts MermaidGraph → ELK JSON input
21
+ * - from-elk.ts ELK result → PositionedGraph, plus shape clipping
22
+ * - layer-alignment.ts post-process: snap same-layer nodes to a uniform position
23
+ * - edge-bundling.ts post-process: merge fan-out/fan-in edges into shared trunks
24
+ * See packages/svg-renderer/src/layout-engine/constants.ts for the shared DEFAULTS used across stages.
25
+ */
26
+
27
+ import type { ElkNode } from 'elkjs'
28
+ import type {
29
+ MermaidGraph,
30
+ PositionedGraph,
31
+ FlowchartRenderOptions,
32
+ } from '@zombie-mermaid/core'
33
+ import { elkLayoutSync } from './elk-instance.ts'
34
+ import { resolveFontSizes } from './styles.ts'
35
+ import { DEFAULTS } from './layout-engine/constants.ts'
36
+ import { mermaidToElk } from './layout-engine/to-elk.ts'
37
+ import { elkToPositioned } from './layout-engine/from-elk.ts'
38
+
39
+ // ============================================================================
40
+ // Public API
41
+ // ============================================================================
42
+
43
+ /**
44
+ * Lay out a parsed MermaidGraph using ELK.js (synchronous).
45
+ * Returns a fully positioned graph ready for rendering.
46
+ */
47
+ export function layoutGraphSync(
48
+ graph: MermaidGraph,
49
+ options: FlowchartRenderOptions = {},
50
+ ): PositionedGraph {
51
+ const opts = {
52
+ ...DEFAULTS,
53
+ ...options,
54
+ fontSizes: resolveFontSizes(options.fontSizes),
55
+ }
56
+ const elkGraph = mermaidToElk(graph, opts)
57
+ const result = elkLayoutSync(elkGraph, options.layoutCache)
58
+ return elkToPositioned(result, graph, opts.mergeEdges)
59
+ }
60
+
61
+ /**
62
+ * Convert MermaidGraph to ELK format (for benchmarking conversion overhead).
63
+ */
64
+ export function convertToElkFormat(
65
+ graph: MermaidGraph,
66
+ options: FlowchartRenderOptions = {},
67
+ ): ElkNode {
68
+ const opts = {
69
+ ...DEFAULTS,
70
+ ...options,
71
+ fontSizes: resolveFontSizes(options.fontSizes),
72
+ }
73
+ return mermaidToElk(graph, opts)
74
+ }
package/src/layout.ts ADDED
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Layout module for flowchart and state diagrams.
3
+ *
4
+ * Uses ELK.js for graph layout — battle-tested, full subgraph support,
5
+ * orthogonal edge routing, and direction overrides.
6
+ */
7
+
8
+ export { layoutGraphSync } from './layout-engine.ts'