@zyro-sec/merman 0.0.0-stage → 1.0.2
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/package.json +29 -4
- package/script/layout-audit.ts +98 -0
- package/src/core/canvas.test.ts +203 -0
- package/src/core/canvas.ts +228 -0
- package/src/core/color/style.test.ts +24 -0
- package/src/core/color/style.ts +56 -0
- package/src/core/drawing.test.ts +116 -0
- package/src/core/drawing.ts +301 -0
- package/src/core/geometry.test.ts +112 -0
- package/src/core/geometry.ts +347 -0
- package/src/core/mermaid.ts +38 -0
- package/src/core/render-grid.ts +51 -0
- package/src/core/spatial.test.ts +87 -0
- package/src/core/spatial.ts +243 -0
- package/src/core/text-lines.ts +32 -0
- package/src/core/text.test.ts +33 -0
- package/src/core/text.ts +44 -0
- package/src/detect.ts +17 -0
- package/src/diagnostics.ts +16 -0
- package/src/flowchart/drawing.ts +340 -0
- package/src/flowchart/flowchart.test.ts +2800 -0
- package/src/flowchart/labels.test.ts +127 -0
- package/src/flowchart/labels.ts +144 -0
- package/src/flowchart/layout.ts +1427 -0
- package/src/flowchart/options.ts +12 -0
- package/src/flowchart/parser.ts +467 -0
- package/src/flowchart/render.ts +10 -0
- package/src/flowchart/routing.test.ts +368 -0
- package/src/flowchart/routing.ts +1542 -0
- package/src/flowchart/style.ts +79 -0
- package/src/flowchart/types.ts +65 -0
- package/src/gantt/diagram.test.ts +124 -0
- package/src/gantt/diagram.ts +8 -0
- package/src/gantt/drawing.ts +191 -0
- package/src/gantt/parser.ts +161 -0
- package/src/gantt/render-grid.ts +17 -0
- package/src/gantt/style.ts +36 -0
- package/src/gantt/types.ts +44 -0
- package/src/gitgraph/diagram.test.ts +183 -0
- package/src/gitgraph/diagram.ts +8 -0
- package/src/gitgraph/drawing.ts +234 -0
- package/src/gitgraph/parser.ts +219 -0
- package/src/gitgraph/render-grid.ts +17 -0
- package/src/gitgraph/style.ts +37 -0
- package/src/gitgraph/types.ts +36 -0
- package/src/layout-audit.test.ts +92 -0
- package/src/markdown.ts +336 -0
- package/src/palette.test.ts +103 -0
- package/src/palette.ts +75 -0
- package/src/plugin.ts +15 -0
- package/src/sequence/diagram.test.ts +757 -0
- package/src/sequence/diagram.ts +8 -0
- package/src/sequence/drawing.ts +287 -0
- package/src/sequence/options.ts +8 -0
- package/src/sequence/parser.ts +282 -0
- package/src/sequence/placement.test.ts +219 -0
- package/src/sequence/placement.ts +684 -0
- package/src/sequence/render-grid.ts +22 -0
- package/src/sequence/style.ts +81 -0
- package/src/sequence/types.ts +74 -0
- package/src/state/diagram.test.ts +1386 -0
- package/src/state/diagram.ts +8 -0
- package/src/state/drawing.ts +360 -0
- package/src/state/endpoint.ts +15 -0
- package/src/state/layout.test.ts +314 -0
- package/src/state/layout.ts +1134 -0
- package/src/state/note.ts +59 -0
- package/src/state/options.ts +11 -0
- package/src/state/parser.ts +224 -0
- package/src/state/render-grid.ts +22 -0
- package/src/state/routing.test.ts +693 -0
- package/src/state/routing.ts +1522 -0
- package/src/state/search.ts +103 -0
- package/src/state/style.test.ts +113 -0
- package/src/state/style.ts +52 -0
- package/src/state/types.ts +63 -0
- package/src/state/visible-model.test.ts +84 -0
- package/src/state/visible-model.ts +77 -0
- package/src/test/diagnostics.test.ts +129 -0
- package/src/test/diagram.ts +32 -0
- package/src/test/layout-audit/fixtures.ts +538 -0
- package/src/test/layout-audit/harness.ts +522 -0
- package/src/test/markdown.test.ts +474 -0
- package/src/timeline/diagram.test.ts +188 -0
- package/src/timeline/diagram.ts +8 -0
- package/src/timeline/drawing.ts +109 -0
- package/src/timeline/parser.ts +119 -0
- package/src/timeline/render-grid.ts +17 -0
- package/src/timeline/style.ts +36 -0
- package/src/timeline/types.ts +31 -0
- package/tsconfig.json +10 -0
- package/README.md +0 -3
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { drawStateDiagramGrid } from "./drawing.js"
|
|
2
|
+
import { parseMermaidStateDiagram } from "./parser.js"
|
|
3
|
+
import { renderStateGridText } from "./render-grid.js"
|
|
4
|
+
import type { StateDiagramRenderOptions } from "./types.js"
|
|
5
|
+
|
|
6
|
+
export function renderStateDiagram(content: string, options: StateDiagramRenderOptions = {}): string {
|
|
7
|
+
return renderStateGridText(drawStateDiagramGrid(parseMermaidStateDiagram(content), options))
|
|
8
|
+
}
|
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
import { BorderChars, type BorderCharacters, type BorderStyle } from "@opentui/core"
|
|
2
|
+
import { DiagramCanvas, DiagramCanvasSizeError, type DiagramCanvasCell } from "../core/canvas.js"
|
|
3
|
+
import { directionBetween, orthogonalPathPoints, type DiagramDirection } from "../core/geometry.js"
|
|
4
|
+
import {
|
|
5
|
+
diagramArrowHead,
|
|
6
|
+
diagramLineGlyph,
|
|
7
|
+
drawDiagramFrame,
|
|
8
|
+
fillDiagramFrameInterior,
|
|
9
|
+
mergeDiagramLineGlyph,
|
|
10
|
+
} from "../core/drawing.js"
|
|
11
|
+
import { DIAGRAM_LABEL_PADDING_X, diagramTextWidth } from "../core/text.js"
|
|
12
|
+
import {
|
|
13
|
+
createStateDiagramLayout,
|
|
14
|
+
expandCompositeBoundsForFeedback,
|
|
15
|
+
expandCompositeBoundsForInternalTransitions,
|
|
16
|
+
separateExternalBoundsFromComposites,
|
|
17
|
+
translateStateDiagramLayout,
|
|
18
|
+
type StateDiagramBoxBounds as BoxBounds,
|
|
19
|
+
type StateDiagramNoteBounds as StateNoteBounds,
|
|
20
|
+
} from "./layout.js"
|
|
21
|
+
import { stateDiagramNoteConnector } from "./note.js"
|
|
22
|
+
import { DEFAULT_STATE_ARROW_HEAD_STYLE, DEFAULT_STATE_BORDER_STYLE, normalizeStateMinStateGap } from "./options.js"
|
|
23
|
+
import type { StateGrid } from "./render-grid.js"
|
|
24
|
+
import {
|
|
25
|
+
createStateTransitionJunctionPlans,
|
|
26
|
+
createStateTransitionRenderPlans,
|
|
27
|
+
measureStateTransitionLabel,
|
|
28
|
+
type StateTransitionRenderPlan,
|
|
29
|
+
} from "./routing.js"
|
|
30
|
+
import { createStateSearchBudget } from "./search.js"
|
|
31
|
+
import { NOTE_CONNECTOR_RAMP_STYLES, STATE_DEPARTURE_RAMP_STYLES } from "./style.js"
|
|
32
|
+
import type {
|
|
33
|
+
StateCellStyle,
|
|
34
|
+
StateDiagram,
|
|
35
|
+
StateDiagramArrowHeadStyle,
|
|
36
|
+
StateDiagramRenderOptions,
|
|
37
|
+
StateDiagramState,
|
|
38
|
+
} from "./types.js"
|
|
39
|
+
import { isHiddenCompositeMarker, prepareVisibleStateDiagram } from "./visible-model.js"
|
|
40
|
+
|
|
41
|
+
type StateCell = DiagramCanvasCell<StateCellStyle>
|
|
42
|
+
|
|
43
|
+
function translateTransitionPlans(
|
|
44
|
+
plans: readonly StateTransitionRenderPlan[],
|
|
45
|
+
dx: number,
|
|
46
|
+
dy: number,
|
|
47
|
+
): StateTransitionRenderPlan[] {
|
|
48
|
+
return plans.map((plan) => ({
|
|
49
|
+
...plan,
|
|
50
|
+
cells: plan.cells.map((cell) => ({ ...cell, x: cell.x + dx, y: cell.y + dy })),
|
|
51
|
+
path: plan.path.map(([x, y]) => [x + dx, y + dy]),
|
|
52
|
+
label: plan.label ? { ...plan.label, x: plan.label.x + dx, y: plan.label.y + dy } : undefined,
|
|
53
|
+
}))
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function makeGrid(width: number, height: number): StateGrid {
|
|
57
|
+
return new DiagramCanvas(width, height, {
|
|
58
|
+
mergeCell: (existing, incoming): StateCell => {
|
|
59
|
+
const existingIsTransition = existing.style === "transition" || existing.style?.startsWith("stateDepartureRamp")
|
|
60
|
+
const incomingIsTransition = incoming.style === "transition" || incoming.style?.startsWith("stateDepartureRamp")
|
|
61
|
+
const shouldMerge = incomingIsTransition && existingIsTransition
|
|
62
|
+
return {
|
|
63
|
+
...incoming,
|
|
64
|
+
char: shouldMerge
|
|
65
|
+
? (mergeDiagramLineGlyph(existing.char, incoming.char, "rounded") ?? incoming.char)
|
|
66
|
+
: incoming.char,
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
})
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function setCell(grid: StateGrid, x: number, y: number, char: string, style?: StateCellStyle): void {
|
|
73
|
+
grid.setCell(x, y, char, style)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function setText(grid: StateGrid, x: number, y: number, text: string, style?: StateCellStyle): void {
|
|
77
|
+
grid.setText(x, y, text, style)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function drawBox(
|
|
81
|
+
grid: StateGrid,
|
|
82
|
+
state: StateDiagramState,
|
|
83
|
+
bounds: BoxBounds,
|
|
84
|
+
lines: string[],
|
|
85
|
+
borderStyle: BorderStyle,
|
|
86
|
+
): void {
|
|
87
|
+
if (isHiddenCompositeMarker(state)) return
|
|
88
|
+
|
|
89
|
+
if (state.kind !== "state") {
|
|
90
|
+
setCell(grid, bounds.left, bounds.top, state.label, state.kind)
|
|
91
|
+
return
|
|
92
|
+
}
|
|
93
|
+
fillDiagramFrameInterior(bounds, (x, y) => setCell(grid, x, y, " ", "state"))
|
|
94
|
+
drawStateFrame(grid, bounds, BorderChars[borderStyle], "stateBorder")
|
|
95
|
+
lines.forEach((line, index) => {
|
|
96
|
+
setText(grid, bounds.left + 2, bounds.top + 1 + index, line, "state")
|
|
97
|
+
})
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function drawStateFrame(grid: StateGrid, bounds: BoxBounds, chars: BorderCharacters, style: StateCellStyle): void {
|
|
101
|
+
drawDiagramFrame(bounds, chars, (x, y, char) => setCell(grid, x, y, char, style))
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function drawContainerFrame(
|
|
105
|
+
grid: StateGrid,
|
|
106
|
+
bounds: BoxBounds,
|
|
107
|
+
label: string,
|
|
108
|
+
chars: BorderCharacters,
|
|
109
|
+
style: StateCellStyle,
|
|
110
|
+
): void {
|
|
111
|
+
drawDiagramFrame(bounds, chars, (x, y, char) => setCell(grid, x, y, char, style))
|
|
112
|
+
drawContainerLabel(grid, bounds, label, style)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function drawContainerLabel(grid: StateGrid, bounds: BoxBounds, label: string, style: StateCellStyle): void {
|
|
116
|
+
if (label) setText(grid, bounds.left + 2, bounds.top, ` ${label} `, style)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function noteConnectorGlyph(directions: ReadonlySet<DiagramDirection>): string {
|
|
120
|
+
const chars = BorderChars.double
|
|
121
|
+
const up = directions.has("up")
|
|
122
|
+
const down = directions.has("down")
|
|
123
|
+
const left = directions.has("left")
|
|
124
|
+
const right = directions.has("right")
|
|
125
|
+
if (up && down && left && right) return chars.cross
|
|
126
|
+
if (up && down && right) return chars.leftT
|
|
127
|
+
if (up && down && left) return chars.rightT
|
|
128
|
+
if (left && right && down) return chars.topT
|
|
129
|
+
if (left && right && up) return chars.bottomT
|
|
130
|
+
if (up && right) return chars.bottomLeft
|
|
131
|
+
if (up && left) return chars.bottomRight
|
|
132
|
+
if (down && right) return chars.topLeft
|
|
133
|
+
if (down && left) return chars.topRight
|
|
134
|
+
if (up || down) return chars.vertical
|
|
135
|
+
return chars.horizontal
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function drawNote(grid: StateGrid, bounds: StateNoteBounds, target: BoxBounds): void {
|
|
139
|
+
const chars = BorderChars.double
|
|
140
|
+
const connector = stateDiagramNoteConnector(bounds, target)
|
|
141
|
+
const points = orthogonalPathPoints(connector.points)
|
|
142
|
+
for (const [index, point] of points.entries()) {
|
|
143
|
+
const directions = new Set<DiagramDirection>()
|
|
144
|
+
const previous = points[index - 1]
|
|
145
|
+
const next = points[index + 1]
|
|
146
|
+
if (previous) directions.add(directionBetween(point, previous)!)
|
|
147
|
+
if (next) directions.add(directionBetween(point, next)!)
|
|
148
|
+
const distanceFromNote = points.length - index - 1
|
|
149
|
+
const style = distanceFromNote < 3 ? NOTE_CONNECTOR_RAMP_STYLES[2 - distanceFromNote]! : "noteConnector"
|
|
150
|
+
setCell(grid, point.x, point.y, noteConnectorGlyph(directions), style)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
drawContainerFrame(grid, bounds, "", chars, "noteBorder")
|
|
154
|
+
setCell(
|
|
155
|
+
grid,
|
|
156
|
+
bounds.note.position === "right" ? bounds.left : bounds.left + bounds.width - 1,
|
|
157
|
+
connector.connectorY,
|
|
158
|
+
bounds.note.position === "right" ? chars.rightT : chars.leftT,
|
|
159
|
+
"noteBorder",
|
|
160
|
+
)
|
|
161
|
+
bounds.lines.forEach((line, index) => setText(grid, bounds.left + 2, bounds.top + 1 + index, line, "noteText"))
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function drawTransitionRenderPlan(
|
|
165
|
+
grid: StateGrid,
|
|
166
|
+
plan: StateTransitionRenderPlan,
|
|
167
|
+
arrowHeadStyle: StateDiagramArrowHeadStyle,
|
|
168
|
+
rampDeparture: boolean,
|
|
169
|
+
): void {
|
|
170
|
+
const departure = new Map(
|
|
171
|
+
rampDeparture
|
|
172
|
+
? plan.path.slice(0, 3).map(([x, y], index) => [`${x}:${y}`, STATE_DEPARTURE_RAMP_STYLES[index]!])
|
|
173
|
+
: [],
|
|
174
|
+
)
|
|
175
|
+
for (const cell of plan.cells) {
|
|
176
|
+
const char = cell.arrowDirection ? diagramArrowHead(cell.arrowDirection, arrowHeadStyle) : cell.char
|
|
177
|
+
setCell(grid, cell.x, cell.y, char, departure.get(`${cell.x}:${cell.y}`) ?? "transition")
|
|
178
|
+
}
|
|
179
|
+
if (plan.label) {
|
|
180
|
+
plan.label.lines.forEach((line, index) => {
|
|
181
|
+
const y = plan.label!.y + index
|
|
182
|
+
const leftX = plan.label!.x - DIAGRAM_LABEL_PADDING_X
|
|
183
|
+
if (grid.getCell(leftX, y)?.char === " ") setCell(grid, leftX, y, " ", "label")
|
|
184
|
+
setText(grid, plan.label!.x, y, line, "label")
|
|
185
|
+
const rightX = plan.label!.x + diagramTextWidth(line) + DIAGRAM_LABEL_PADDING_X - 1
|
|
186
|
+
if (grid.getCell(rightX, y)?.char === " ") setCell(grid, rightX, y, " ", "label")
|
|
187
|
+
})
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function drawTransitionJunctionPlans(
|
|
192
|
+
grid: StateGrid,
|
|
193
|
+
diagram: StateDiagram,
|
|
194
|
+
bounds: Map<string, BoxBounds>,
|
|
195
|
+
renderPlans: readonly StateTransitionRenderPlan[],
|
|
196
|
+
): void {
|
|
197
|
+
for (const plan of createStateTransitionJunctionPlans(diagram, bounds, renderPlans)) {
|
|
198
|
+
const style = plan.kind === "choice" ? "choice" : "transition"
|
|
199
|
+
const char = plan.kind === "choice" ? "◆" : diagramLineGlyph(plan.connections, "rounded")
|
|
200
|
+
setCell(grid, plan.bounds.left, plan.bounds.top, char, style)
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export function drawStateDiagramGrid(sourceDiagram: StateDiagram, options: StateDiagramRenderOptions = {}): StateGrid {
|
|
205
|
+
return createStateDiagramDrawing(sourceDiagram, options).grid
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export function createStateDiagramDrawing(sourceDiagram: StateDiagram, options: StateDiagramRenderOptions = {}) {
|
|
209
|
+
const direction = options.direction ?? sourceDiagram.direction
|
|
210
|
+
if (direction !== "LR" && direction !== "RL") {
|
|
211
|
+
return createStateDiagramDrawingWithDirection(sourceDiagram, options, direction)
|
|
212
|
+
}
|
|
213
|
+
if (options.layoutMaxWidth === undefined || !Number.isFinite(options.layoutMaxWidth)) {
|
|
214
|
+
return createStateDiagramDrawingWithDirection(sourceDiagram, options, direction)
|
|
215
|
+
}
|
|
216
|
+
const fallbackDirection = direction === "RL" ? "BT" : "TB"
|
|
217
|
+
const maxWidth = Math.max(1, Math.trunc(options.layoutMaxWidth))
|
|
218
|
+
const drawing = (() => {
|
|
219
|
+
try {
|
|
220
|
+
return createStateDiagramDrawingWithDirection(sourceDiagram, options, direction)
|
|
221
|
+
} catch (error) {
|
|
222
|
+
if (error instanceof DiagramCanvasSizeError) return undefined
|
|
223
|
+
throw error
|
|
224
|
+
}
|
|
225
|
+
})()
|
|
226
|
+
if (!drawing) return createStateDiagramDrawingWithDirection(sourceDiagram, options, fallbackDirection)
|
|
227
|
+
if (drawing.grid.getTextSize({ trimTop: true, trimBottom: true }).width <= maxWidth) {
|
|
228
|
+
return drawing
|
|
229
|
+
}
|
|
230
|
+
const fallback = createStateDiagramDrawingWithDirection(sourceDiagram, options, fallbackDirection)
|
|
231
|
+
if (
|
|
232
|
+
fallback.grid.getTextSize({ trimTop: true, trimBottom: true }).width >=
|
|
233
|
+
drawing.grid.getTextSize({ trimTop: true, trimBottom: true }).width
|
|
234
|
+
) {
|
|
235
|
+
return drawing
|
|
236
|
+
}
|
|
237
|
+
return fallback
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function createStateDiagramDrawingWithDirection(
|
|
241
|
+
sourceDiagram: StateDiagram,
|
|
242
|
+
options: StateDiagramRenderOptions,
|
|
243
|
+
direction: StateDiagram["direction"],
|
|
244
|
+
) {
|
|
245
|
+
const diagram = prepareVisibleStateDiagram(
|
|
246
|
+
direction === sourceDiagram.direction ? sourceDiagram : { ...sourceDiagram, direction },
|
|
247
|
+
)
|
|
248
|
+
const borderStyle = options.borderStyle ?? DEFAULT_STATE_BORDER_STYLE
|
|
249
|
+
const arrowHeadStyle = options.arrowHeadStyle ?? DEFAULT_STATE_ARROW_HEAD_STYLE
|
|
250
|
+
const minStateGap = normalizeStateMinStateGap(options.minStateGap)
|
|
251
|
+
const searchBudget = createStateSearchBudget()
|
|
252
|
+
const layout = createStateDiagramLayout(diagram, {
|
|
253
|
+
minStateGap,
|
|
254
|
+
searchBudget,
|
|
255
|
+
})
|
|
256
|
+
const { bounds, sizes, compositeBounds, noteBounds } = layout
|
|
257
|
+
let allBounds = [...bounds.values(), ...noteBounds]
|
|
258
|
+
let maxY = Math.max(0, ...allBounds.map((bound) => bound.top + bound.height))
|
|
259
|
+
const feedbackLaneY = maxY + 3
|
|
260
|
+
const feedbackTopY = Math.min(0, ...allBounds.map((bound) => bound.top)) - 3
|
|
261
|
+
expandCompositeBoundsForFeedback(diagram, bounds, compositeBounds, feedbackLaneY)
|
|
262
|
+
let transitionPlans: StateTransitionRenderPlan[] = []
|
|
263
|
+
const separationAttempts = diagram.states.length + diagram.composites.length + 1
|
|
264
|
+
for (let attempt = 0; attempt < separationAttempts; attempt++) {
|
|
265
|
+
transitionPlans = createStateTransitionRenderPlans(diagram, bounds, feedbackLaneY, {
|
|
266
|
+
feedbackTopY,
|
|
267
|
+
noteBounds,
|
|
268
|
+
searchBudget,
|
|
269
|
+
})
|
|
270
|
+
expandCompositeBoundsForInternalTransitions(diagram, compositeBounds, transitionPlans)
|
|
271
|
+
if (!separateExternalBoundsFromComposites(diagram, layout)) break
|
|
272
|
+
if (attempt === separationAttempts - 1) throw new Error("State composite separation did not converge")
|
|
273
|
+
}
|
|
274
|
+
const connectorPoints = noteBounds.flatMap((bound) => bound.connector?.points ?? [])
|
|
275
|
+
const contentLeft = Math.min(
|
|
276
|
+
0,
|
|
277
|
+
...[...bounds.values(), ...noteBounds].map((bound) => bound.left),
|
|
278
|
+
...connectorPoints.map((point) => point.x),
|
|
279
|
+
...transitionPlans.flatMap((plan) => [
|
|
280
|
+
...plan.cells.map((cell) => cell.x),
|
|
281
|
+
...(plan.label ? [plan.label.x - DIAGRAM_LABEL_PADDING_X] : []),
|
|
282
|
+
]),
|
|
283
|
+
)
|
|
284
|
+
const contentTop = Math.min(
|
|
285
|
+
0,
|
|
286
|
+
...[...bounds.values(), ...noteBounds].map((bound) => bound.top),
|
|
287
|
+
...connectorPoints.map((point) => point.y),
|
|
288
|
+
...transitionPlans.flatMap((plan) => [...plan.cells.map((cell) => cell.y), ...(plan.label ? [plan.label.y] : [])]),
|
|
289
|
+
)
|
|
290
|
+
if (contentLeft < 0 || contentTop < 0) {
|
|
291
|
+
translateStateDiagramLayout(layout, -contentLeft, -contentTop)
|
|
292
|
+
transitionPlans = translateTransitionPlans(transitionPlans, -contentLeft, -contentTop)
|
|
293
|
+
}
|
|
294
|
+
allBounds = [...bounds.values(), ...noteBounds]
|
|
295
|
+
const translatedConnectorPoints = noteBounds.flatMap((bound) => bound.connector?.points ?? [])
|
|
296
|
+
const maxX = Math.max(
|
|
297
|
+
0,
|
|
298
|
+
...allBounds.map((bound) => bound.left + bound.width),
|
|
299
|
+
...translatedConnectorPoints.map((point) => point.x + 1),
|
|
300
|
+
)
|
|
301
|
+
maxY = Math.max(
|
|
302
|
+
0,
|
|
303
|
+
...allBounds.map((bound) => bound.top + bound.height),
|
|
304
|
+
...translatedConnectorPoints.map((point) => point.y + 1),
|
|
305
|
+
)
|
|
306
|
+
const transitionLabelSizes = diagram.transitions.map((transition) => measureStateTransitionLabel(transition.label))
|
|
307
|
+
const maxTransitionLabelWidth = Math.max(0, ...transitionLabelSizes.map((size) => size.width))
|
|
308
|
+
const maxTransitionLabelLines = Math.max(0, ...transitionLabelSizes.map((size) => size.height))
|
|
309
|
+
const transitionRight = Math.max(
|
|
310
|
+
maxX,
|
|
311
|
+
...transitionPlans.flatMap((plan) => [
|
|
312
|
+
...plan.cells.map((cell) => cell.x + 1),
|
|
313
|
+
...(plan.label
|
|
314
|
+
? [plan.label.x + measureStateTransitionLabel(plan.route.transition.label).width + DIAGRAM_LABEL_PADDING_X]
|
|
315
|
+
: []),
|
|
316
|
+
]),
|
|
317
|
+
)
|
|
318
|
+
const transitionBottom = Math.max(
|
|
319
|
+
maxY,
|
|
320
|
+
...transitionPlans.flatMap((plan) => [
|
|
321
|
+
...plan.cells.map((cell) => cell.y + 1),
|
|
322
|
+
...(plan.label ? [plan.label.y + plan.label.lines.length] : []),
|
|
323
|
+
]),
|
|
324
|
+
)
|
|
325
|
+
const grid = makeGrid(
|
|
326
|
+
Math.max(maxX + Math.max(24, maxTransitionLabelWidth + 4), transitionRight + 2),
|
|
327
|
+
Math.max(maxY + 8 + maxTransitionLabelLines, transitionBottom + 2),
|
|
328
|
+
)
|
|
329
|
+
for (const composite of diagram.composites) {
|
|
330
|
+
const bound = compositeBounds.get(composite.id)
|
|
331
|
+
if (!bound) continue
|
|
332
|
+
drawContainerFrame(grid, bound, composite.label, BorderChars[borderStyle], "composite")
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
for (const state of diagram.states) {
|
|
336
|
+
const bound = bounds.get(state.id)
|
|
337
|
+
const size = sizes.get(state.id)
|
|
338
|
+
if (!bound || !size) continue
|
|
339
|
+
drawBox(grid, state, bound, size.lines, borderStyle)
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
for (const plan of transitionPlans) {
|
|
343
|
+
const source = diagram.states.find((state) => state.id === plan.route.transition.from)
|
|
344
|
+
drawTransitionRenderPlan(grid, plan, arrowHeadStyle, source?.kind === "state")
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
drawTransitionJunctionPlans(grid, diagram, bounds, transitionPlans)
|
|
348
|
+
|
|
349
|
+
for (const composite of diagram.composites) {
|
|
350
|
+
const bound = compositeBounds.get(composite.id)
|
|
351
|
+
if (bound) drawContainerLabel(grid, bound, composite.label, "compositeLabel")
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
for (const noteBound of noteBounds) {
|
|
355
|
+
const target = bounds.get(noteBound.note.target)
|
|
356
|
+
if (target) drawNote(grid, noteBound, target)
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
return { grid, diagram, layout, transitionPlans }
|
|
360
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export function stateDiagramStartMarkerId(scope?: string): string {
|
|
2
|
+
return scope ? `${scope}.__start` : "__start"
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export function stateDiagramEndMarkerId(scope?: string): string {
|
|
6
|
+
return scope ? `${scope}.__end` : "__end"
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function stateDiagramMarkerId(position: "from" | "to", scope?: string): string {
|
|
10
|
+
return position === "from" ? stateDiagramStartMarkerId(scope) : stateDiagramEndMarkerId(scope)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function normalizeStateDiagramEndpoint(value: string, position: "from" | "to", scope?: string): string {
|
|
14
|
+
return value === "[*]" ? stateDiagramMarkerId(position, scope) : value
|
|
15
|
+
}
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test"
|
|
2
|
+
import { spatialPathClaim } from "../core/spatial.js"
|
|
3
|
+
import { diagramTextWidth } from "../core/text.js"
|
|
4
|
+
import type { StateDiagram } from "./types.js"
|
|
5
|
+
import { createStateDiagramLayout, expandCompositeBoundsForInternalTransitions } from "./layout.js"
|
|
6
|
+
import { stateDiagramNoteConnector } from "./note.js"
|
|
7
|
+
import { parseMermaidStateDiagram } from "./parser.js"
|
|
8
|
+
import { createStateTransitionRenderPlans } from "./routing.js"
|
|
9
|
+
import { prepareVisibleStateDiagram } from "./visible-model.js"
|
|
10
|
+
|
|
11
|
+
describe("StateDiagramLayout", () => {
|
|
12
|
+
test("lays out horizontal main-path states before branch states", () => {
|
|
13
|
+
const diagram: StateDiagram = {
|
|
14
|
+
direction: "LR",
|
|
15
|
+
states: [
|
|
16
|
+
{ id: "A", label: "A", kind: "state" },
|
|
17
|
+
{ id: "B", label: "B", kind: "state" },
|
|
18
|
+
{ id: "C", label: "C", kind: "state" },
|
|
19
|
+
],
|
|
20
|
+
transitions: [
|
|
21
|
+
{ from: "A", to: "B", label: "main" },
|
|
22
|
+
{ from: "A", to: "C", label: "branch" },
|
|
23
|
+
],
|
|
24
|
+
composites: [],
|
|
25
|
+
notes: [],
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const layout = createStateDiagramLayout(diagram, { minStateGap: 5 })
|
|
29
|
+
const a = layout.bounds.get("A")!
|
|
30
|
+
const b = layout.bounds.get("B")!
|
|
31
|
+
const c = layout.bounds.get("C")!
|
|
32
|
+
|
|
33
|
+
expect(a.left).toBeLessThan(b.left)
|
|
34
|
+
expect(c.top).toBeGreaterThan(a.top)
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
test("aligns a reconverging side branch under the parallel main-path stage", () => {
|
|
38
|
+
const diagram: StateDiagram = {
|
|
39
|
+
direction: "LR",
|
|
40
|
+
states: ["Fork", "Upper", "Lower", "Join"].map((id) => ({ id, label: id, kind: "state" })),
|
|
41
|
+
transitions: [
|
|
42
|
+
{ from: "Fork", to: "Upper", label: "upper" },
|
|
43
|
+
{ from: "Fork", to: "Lower", label: "lower" },
|
|
44
|
+
{ from: "Upper", to: "Join", label: "join" },
|
|
45
|
+
{ from: "Lower", to: "Join", label: "join" },
|
|
46
|
+
],
|
|
47
|
+
composites: [],
|
|
48
|
+
notes: [],
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const layout = createStateDiagramLayout(diagram, { minStateGap: 5 })
|
|
52
|
+
const upper = layout.bounds.get("Upper")!
|
|
53
|
+
const lower = layout.bounds.get("Lower")!
|
|
54
|
+
|
|
55
|
+
expect(lower.centerX).toBe(upper.centerX)
|
|
56
|
+
expect(lower.top).toBeGreaterThan(upper.top)
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
test("places note bounds outside their target state", () => {
|
|
60
|
+
const diagram: StateDiagram = {
|
|
61
|
+
direction: "LR",
|
|
62
|
+
states: [
|
|
63
|
+
{ id: "A", label: "A", kind: "state" },
|
|
64
|
+
{ id: "B", label: "B", kind: "state" },
|
|
65
|
+
],
|
|
66
|
+
transitions: [{ from: "A", to: "B", label: "next" }],
|
|
67
|
+
composites: [],
|
|
68
|
+
notes: [{ target: "A", position: "right", lines: ["note"] }],
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const layout = createStateDiagramLayout(diagram, { minStateGap: 5 })
|
|
72
|
+
const target = layout.bounds.get("A")!
|
|
73
|
+
const note = layout.noteBounds[0]!
|
|
74
|
+
|
|
75
|
+
expect(note.left).toBeGreaterThanOrEqual(target.left + target.width)
|
|
76
|
+
expect(note.lines).toEqual(["note"])
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
test("widens only the horizontal gap that carries a long label", () => {
|
|
80
|
+
const diagram: StateDiagram = {
|
|
81
|
+
direction: "LR",
|
|
82
|
+
states: [
|
|
83
|
+
{ id: "A", label: "A", kind: "state" },
|
|
84
|
+
{ id: "B", label: "B", kind: "state" },
|
|
85
|
+
{ id: "C", label: "C", kind: "state" },
|
|
86
|
+
],
|
|
87
|
+
transitions: [
|
|
88
|
+
{ from: "A", to: "B", label: "a transition label requiring substantially more room" },
|
|
89
|
+
{ from: "B", to: "C", label: "ok" },
|
|
90
|
+
],
|
|
91
|
+
composites: [],
|
|
92
|
+
notes: [],
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const layout = createStateDiagramLayout(diagram, { minStateGap: 5 })
|
|
96
|
+
const a = layout.bounds.get("A")!
|
|
97
|
+
const b = layout.bounds.get("B")!
|
|
98
|
+
const c = layout.bounds.get("C")!
|
|
99
|
+
const longGap = b.left - (a.left + a.width)
|
|
100
|
+
const shortGap = c.left - (b.left + b.width)
|
|
101
|
+
|
|
102
|
+
expect(longGap).toBeGreaterThan(shortGap)
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
test("reserves notes and connectors from final transition geometry", () => {
|
|
106
|
+
const diagram = prepareVisibleStateDiagram(
|
|
107
|
+
parseMermaidStateDiagram(`stateDiagram-v2
|
|
108
|
+
direction LR
|
|
109
|
+
A --> B: advance
|
|
110
|
+
B --> C: continue
|
|
111
|
+
note right of B: first note
|
|
112
|
+
note right of B: second note
|
|
113
|
+
note left of C: left note`),
|
|
114
|
+
)
|
|
115
|
+
const layout = createStateDiagramLayout(diagram, { minStateGap: 5 })
|
|
116
|
+
const plans = createStateTransitionRenderPlans(diagram, layout.bounds, 30, { noteBounds: layout.noteBounds })
|
|
117
|
+
const occupiedByNote = layout.noteBounds.map((noteBound) => {
|
|
118
|
+
const target = layout.bounds.get(noteBound.note.target)!
|
|
119
|
+
const connector = spatialPathClaim(
|
|
120
|
+
`connector:${noteBound.id}`,
|
|
121
|
+
`connector:${noteBound.id}`,
|
|
122
|
+
"boundary",
|
|
123
|
+
stateDiagramNoteConnector(noteBound, target).points,
|
|
124
|
+
)
|
|
125
|
+
return new Set([
|
|
126
|
+
...Array.from({ length: noteBound.height }, (_, dy) =>
|
|
127
|
+
Array.from({ length: noteBound.width }, (_, dx) => `${noteBound.left + dx}:${noteBound.top + dy}`),
|
|
128
|
+
).flat(),
|
|
129
|
+
...connector.spans.flatMap((span) =>
|
|
130
|
+
Array.from({ length: span.toX - span.fromX + 1 }, (_, dx) => `${span.fromX + dx}:${span.y}`),
|
|
131
|
+
),
|
|
132
|
+
])
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
for (const [index, occupied] of occupiedByNote.entries()) {
|
|
136
|
+
for (const other of occupiedByNote.slice(index + 1)) {
|
|
137
|
+
expect([...occupied].some((cell) => other.has(cell))).toBe(false)
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
const noteCells = new Set(occupiedByNote.flatMap((occupied) => [...occupied]))
|
|
141
|
+
for (const plan of plans) {
|
|
142
|
+
expect(plan.path.some(([x, y]) => noteCells.has(`${x}:${y}`))).toBe(false)
|
|
143
|
+
if (!plan.label) continue
|
|
144
|
+
const width = Math.max(...plan.label.lines.map(diagramTextWidth))
|
|
145
|
+
expect(
|
|
146
|
+
plan.label.lines.some((_, dy) =>
|
|
147
|
+
Array.from({ length: width }, (_, dx) => `${plan.label!.x + dx}:${plan.label!.y + dy}`).some((cell) =>
|
|
148
|
+
noteCells.has(cell),
|
|
149
|
+
),
|
|
150
|
+
),
|
|
151
|
+
).toBe(false)
|
|
152
|
+
}
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
test("finalizes nested composite bounds after transition-aware note placement", () => {
|
|
156
|
+
const diagram = prepareVisibleStateDiagram(
|
|
157
|
+
parseMermaidStateDiagram(`stateDiagram-v2
|
|
158
|
+
direction LR
|
|
159
|
+
state Outer {
|
|
160
|
+
state Inner {
|
|
161
|
+
A --> B: internal route
|
|
162
|
+
note right of B: nested note
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
Outer --> Done: leave composite`),
|
|
166
|
+
)
|
|
167
|
+
const layout = createStateDiagramLayout(diagram, { minStateGap: 5 })
|
|
168
|
+
const note = layout.noteBounds[0]!
|
|
169
|
+
const inner = layout.compositeBounds.get("Inner")!
|
|
170
|
+
const outer = layout.compositeBounds.get("Outer")!
|
|
171
|
+
const done = layout.bounds.get("Done")!
|
|
172
|
+
|
|
173
|
+
for (const composite of [inner, outer]) {
|
|
174
|
+
expect(note.left).toBeGreaterThan(composite.left)
|
|
175
|
+
expect(note.top).toBeGreaterThan(composite.top)
|
|
176
|
+
expect(note.left + note.width).toBeLessThan(composite.left + composite.width)
|
|
177
|
+
expect(note.top + note.height).toBeLessThan(composite.top + composite.height)
|
|
178
|
+
}
|
|
179
|
+
expect(
|
|
180
|
+
done.left < outer.left + outer.width &&
|
|
181
|
+
done.left + done.width > outer.left &&
|
|
182
|
+
done.top < outer.top + outer.height &&
|
|
183
|
+
done.top + done.height > outer.top,
|
|
184
|
+
).toBe(false)
|
|
185
|
+
|
|
186
|
+
const maxY = Math.max(...[...layout.bounds.values(), note].map((bound) => bound.top + bound.height))
|
|
187
|
+
const plans = createStateTransitionRenderPlans(diagram, layout.bounds, maxY + 3, { noteBounds: layout.noteBounds })
|
|
188
|
+
const noteCells = new Set(
|
|
189
|
+
Array.from({ length: note.height }, (_, dy) =>
|
|
190
|
+
Array.from({ length: note.width }, (_, dx) => `${note.left + dx}:${note.top + dy}`),
|
|
191
|
+
).flat(),
|
|
192
|
+
)
|
|
193
|
+
expect(plans.every((plan) => plan.path.every(([x, y]) => !noteCells.has(`${x}:${y}`)))).toBe(true)
|
|
194
|
+
})
|
|
195
|
+
|
|
196
|
+
test.each([
|
|
197
|
+
["LR", -1],
|
|
198
|
+
["RL", 1],
|
|
199
|
+
] as const)("keeps a reciprocal pair on the %s axis", (direction, expectedSign) => {
|
|
200
|
+
const diagram = prepareVisibleStateDiagram(
|
|
201
|
+
parseMermaidStateDiagram(`stateDiagram-v2
|
|
202
|
+
direction ${direction}
|
|
203
|
+
A --> B: forward
|
|
204
|
+
B --> A: backward`),
|
|
205
|
+
)
|
|
206
|
+
const layout = createStateDiagramLayout(diagram, { minStateGap: 5 })
|
|
207
|
+
const a = layout.bounds.get("A")!
|
|
208
|
+
const b = layout.bounds.get("B")!
|
|
209
|
+
|
|
210
|
+
expect(a.centerY).toBe(b.centerY)
|
|
211
|
+
expect(Math.sign(a.centerX - b.centerX)).toBe(expectedSign)
|
|
212
|
+
})
|
|
213
|
+
|
|
214
|
+
test("continues the horizontal backbone through a reciprocal pair after an entry marker", () => {
|
|
215
|
+
const diagram = prepareVisibleStateDiagram(
|
|
216
|
+
parseMermaidStateDiagram(`stateDiagram-v2
|
|
217
|
+
direction LR
|
|
218
|
+
[*] --> Clean
|
|
219
|
+
Clean --> Dirty: edit
|
|
220
|
+
Dirty --> Clean: save
|
|
221
|
+
Dirty --> Closing: request close
|
|
222
|
+
Closing --> [*]: closed`),
|
|
223
|
+
)
|
|
224
|
+
const layout = createStateDiagramLayout(diagram, { minStateGap: 5 })
|
|
225
|
+
const clean = layout.bounds.get("Clean")!
|
|
226
|
+
const dirty = layout.bounds.get("Dirty")!
|
|
227
|
+
const closing = layout.bounds.get("Closing")!
|
|
228
|
+
|
|
229
|
+
expect(clean.centerY).toBe(dirty.centerY)
|
|
230
|
+
expect(dirty.centerY).toBe(closing.centerY)
|
|
231
|
+
expect(clean.centerX).toBeLessThan(dirty.centerX)
|
|
232
|
+
expect(dirty.centerX).toBeLessThan(closing.centerX)
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
test("does not reserve an unused right-side lane for aligned nested composite transitions", () => {
|
|
236
|
+
const diagram = prepareVisibleStateDiagram(
|
|
237
|
+
parseMermaidStateDiagram(`stateDiagram-v2
|
|
238
|
+
direction TB
|
|
239
|
+
state Session {
|
|
240
|
+
[*] --> Open
|
|
241
|
+
state Open {
|
|
242
|
+
[*] --> Clean
|
|
243
|
+
Clean --> Dirty: edit
|
|
244
|
+
Dirty --> Clean: save
|
|
245
|
+
Dirty --> [*]
|
|
246
|
+
}
|
|
247
|
+
Open --> Closing: request close
|
|
248
|
+
Closing --> Open: cancel
|
|
249
|
+
Closing --> [*]: closed
|
|
250
|
+
note right of Dirty: unsaved changes
|
|
251
|
+
}
|
|
252
|
+
[*] --> Session: hydrate
|
|
253
|
+
Session --> [*]: release`),
|
|
254
|
+
)
|
|
255
|
+
const layout = createStateDiagramLayout(diagram, { minStateGap: 5 })
|
|
256
|
+
const open = layout.compositeBounds.get("Open")!
|
|
257
|
+
const session = layout.compositeBounds.get("Session")!
|
|
258
|
+
|
|
259
|
+
expect(session.width).toBeLessThanOrEqual(open.width + 4)
|
|
260
|
+
})
|
|
261
|
+
|
|
262
|
+
test.each(["TB", "TD"] as const)(
|
|
263
|
+
"contains nested internal feedback with strict margins in %s diagrams",
|
|
264
|
+
(direction) => {
|
|
265
|
+
const diagram = prepareVisibleStateDiagram(
|
|
266
|
+
parseMermaidStateDiagram(`stateDiagram-v2
|
|
267
|
+
direction ${direction}
|
|
268
|
+
state Outer {
|
|
269
|
+
state Inner {
|
|
270
|
+
A --> B: down
|
|
271
|
+
B --> A: up
|
|
272
|
+
note right of B: nested note
|
|
273
|
+
}
|
|
274
|
+
}`),
|
|
275
|
+
)
|
|
276
|
+
const layout = createStateDiagramLayout(diagram, { minStateGap: 5 })
|
|
277
|
+
const plans = createStateTransitionRenderPlans(diagram, layout.bounds, 30, { noteBounds: layout.noteBounds })
|
|
278
|
+
expandCompositeBoundsForInternalTransitions(diagram, layout.compositeBounds, plans)
|
|
279
|
+
const inner = layout.compositeBounds.get("Inner")!
|
|
280
|
+
const outer = layout.compositeBounds.get("Outer")!
|
|
281
|
+
const note = layout.noteBounds[0]!
|
|
282
|
+
|
|
283
|
+
for (const composite of [inner, outer]) {
|
|
284
|
+
for (const plan of plans) {
|
|
285
|
+
expect(
|
|
286
|
+
plan.path.every(
|
|
287
|
+
([x, y]) =>
|
|
288
|
+
x > composite.left &&
|
|
289
|
+
x < composite.left + composite.width - 1 &&
|
|
290
|
+
y > composite.top &&
|
|
291
|
+
y < composite.top + composite.height - 1,
|
|
292
|
+
),
|
|
293
|
+
).toBe(true)
|
|
294
|
+
}
|
|
295
|
+
expect(
|
|
296
|
+
note.connector!.points.every(
|
|
297
|
+
(point) =>
|
|
298
|
+
point.x > composite.left &&
|
|
299
|
+
point.x < composite.left + composite.width - 1 &&
|
|
300
|
+
point.y > composite.top &&
|
|
301
|
+
point.y < composite.top + composite.height - 1,
|
|
302
|
+
),
|
|
303
|
+
).toBe(true)
|
|
304
|
+
}
|
|
305
|
+
expect(new Set(note.connector!.points.map((point) => point.y))).toEqual(
|
|
306
|
+
new Set([layout.bounds.get("B")!.centerY]),
|
|
307
|
+
)
|
|
308
|
+
expect(inner.left - outer.left).toBeGreaterThanOrEqual(2)
|
|
309
|
+
expect(inner.top - outer.top).toBeGreaterThanOrEqual(2)
|
|
310
|
+
expect(outer.left + outer.width - (inner.left + inner.width)).toBeGreaterThanOrEqual(2)
|
|
311
|
+
expect(outer.top + outer.height - (inner.top + inner.height)).toBeGreaterThanOrEqual(2)
|
|
312
|
+
},
|
|
313
|
+
)
|
|
314
|
+
})
|