@zyro-sec/merman 1.0.2 → 1.0.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/package.json +2 -2
- package/script/layout-audit.ts +98 -98
- package/src/core/canvas.ts +228 -228
- package/src/core/color/style.ts +56 -56
- package/src/core/drawing.ts +301 -301
- package/src/core/geometry.ts +347 -347
- package/src/core/mermaid.ts +38 -38
- package/src/core/render-grid.ts +51 -51
- package/src/core/spatial.ts +243 -243
- package/src/core/text-lines.ts +32 -32
- package/src/core/text.ts +44 -44
- package/src/detect.ts +17 -17
- package/src/diagnostics.ts +16 -16
- package/src/flowchart/drawing.ts +340 -340
- package/src/flowchart/labels.ts +144 -144
- package/src/flowchart/layout.ts +1427 -1427
- package/src/flowchart/options.ts +12 -12
- package/src/flowchart/parser.ts +467 -467
- package/src/flowchart/render.ts +10 -10
- package/src/flowchart/routing.ts +1542 -1542
- package/src/flowchart/style.ts +79 -79
- package/src/flowchart/types.ts +65 -65
- package/src/gantt/diagram.ts +8 -8
- package/src/gantt/drawing.ts +191 -191
- package/src/gantt/parser.ts +161 -161
- package/src/gantt/render-grid.ts +17 -17
- package/src/gantt/style.ts +36 -36
- package/src/gantt/types.ts +44 -44
- package/src/gitgraph/diagram.ts +8 -8
- package/src/gitgraph/drawing.ts +234 -234
- package/src/gitgraph/parser.ts +219 -219
- package/src/gitgraph/render-grid.ts +17 -17
- package/src/gitgraph/style.ts +37 -37
- package/src/gitgraph/types.ts +36 -36
- package/src/markdown.ts +336 -336
- package/src/palette.ts +75 -75
- package/src/plugin.ts +15 -15
- package/src/sequence/diagram.ts +8 -8
- package/src/sequence/drawing.ts +287 -287
- package/src/sequence/options.ts +8 -8
- package/src/sequence/parser.ts +282 -282
- package/src/sequence/placement.ts +684 -684
- package/src/sequence/render-grid.ts +22 -22
- package/src/sequence/style.ts +81 -81
- package/src/sequence/types.ts +74 -74
- package/src/state/diagram.ts +8 -8
- package/src/state/drawing.ts +360 -360
- package/src/state/endpoint.ts +15 -15
- package/src/state/layout.ts +1134 -1134
- package/src/state/note.ts +59 -59
- package/src/state/options.ts +11 -11
- package/src/state/parser.ts +224 -224
- package/src/state/render-grid.ts +22 -22
- package/src/state/routing.ts +1522 -1522
- package/src/state/search.ts +103 -103
- package/src/state/style.ts +52 -52
- package/src/state/types.ts +63 -63
- package/src/state/visible-model.ts +77 -77
- package/src/test/diagram.ts +32 -32
- package/src/test/layout-audit/fixtures.ts +538 -538
- package/src/test/layout-audit/harness.ts +522 -522
- package/src/timeline/diagram.ts +8 -8
- package/src/timeline/drawing.ts +109 -109
- package/src/timeline/parser.ts +119 -119
- package/src/timeline/render-grid.ts +17 -17
- package/src/timeline/style.ts +36 -36
- package/src/timeline/types.ts +31 -31
- package/tsconfig.json +10 -10
- package/src/core/canvas.test.ts +0 -203
- package/src/core/color/style.test.ts +0 -24
- package/src/core/drawing.test.ts +0 -116
- package/src/core/geometry.test.ts +0 -112
- package/src/core/spatial.test.ts +0 -87
- package/src/core/text.test.ts +0 -33
- package/src/flowchart/flowchart.test.ts +0 -2800
- package/src/flowchart/labels.test.ts +0 -127
- package/src/flowchart/routing.test.ts +0 -368
- package/src/gantt/diagram.test.ts +0 -124
- package/src/gitgraph/diagram.test.ts +0 -183
- package/src/layout-audit.test.ts +0 -92
- package/src/palette.test.ts +0 -103
- package/src/sequence/diagram.test.ts +0 -757
- package/src/sequence/placement.test.ts +0 -219
- package/src/state/diagram.test.ts +0 -1386
- package/src/state/layout.test.ts +0 -314
- package/src/state/routing.test.ts +0 -693
- package/src/state/style.test.ts +0 -113
- package/src/state/visible-model.test.ts +0 -84
- package/src/test/diagnostics.test.ts +0 -129
- package/src/test/markdown.test.ts +0 -474
- package/src/timeline/diagram.test.ts +0 -188
package/src/sequence/drawing.ts
CHANGED
|
@@ -1,287 +1,287 @@
|
|
|
1
|
-
import { BorderChars, type BorderStyle } from "@opentui/core"
|
|
2
|
-
import { DiagramCanvas } from "../core/canvas.js"
|
|
3
|
-
import { diagramTextWidth } from "../core/text.js"
|
|
4
|
-
import { DEFAULT_FRAGMENT_BORDER_STYLE } from "./options.js"
|
|
5
|
-
import {
|
|
6
|
-
createSequencePlacementPlan,
|
|
7
|
-
type SequenceGroupPlacement,
|
|
8
|
-
type SequenceHorizontalBounds,
|
|
9
|
-
type SequenceStepPlacement,
|
|
10
|
-
} from "./placement.js"
|
|
11
|
-
import type { SequenceGrid } from "./render-grid.js"
|
|
12
|
-
import { SEQUENCE_FADE_STEPS as FADE_STEPS } from "./style.js"
|
|
13
|
-
import type {
|
|
14
|
-
MessageStyle,
|
|
15
|
-
SequenceArrowHead,
|
|
16
|
-
SequenceCellStyle,
|
|
17
|
-
SequenceDiagram,
|
|
18
|
-
SequenceDiagramRenderOptions,
|
|
19
|
-
} from "./types.js"
|
|
20
|
-
|
|
21
|
-
const SEQUENCE_BORDER = BorderChars.rounded
|
|
22
|
-
|
|
23
|
-
function centeredStart(center: number, text: string): number {
|
|
24
|
-
return center - Math.floor(diagramTextWidth(text) / 2)
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function arrowHeadChar(head: SequenceArrowHead | undefined, direction: 1 | -1): string {
|
|
28
|
-
switch (head) {
|
|
29
|
-
case "open":
|
|
30
|
-
return direction === 1 ? ">" : "<"
|
|
31
|
-
case "cross":
|
|
32
|
-
return "✕"
|
|
33
|
-
case "async":
|
|
34
|
-
return direction === 1 ? ")" : "("
|
|
35
|
-
default:
|
|
36
|
-
return direction === 1 ? "►" : "◄"
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function createGrid(width: number, height: number): SequenceGrid {
|
|
41
|
-
return new DiagramCanvas(width, height)
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function setCell(grid: SequenceGrid, x: number, y: number, char: string, style?: SequenceCellStyle): void {
|
|
45
|
-
grid.setCell(x, y, char, style)
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function setText(grid: SequenceGrid, x: number, y: number, text: string, style?: SequenceCellStyle): void {
|
|
49
|
-
grid.setText(Math.max(0, x), y, text, style)
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function setArrowDepartureFade(
|
|
53
|
-
grid: SequenceGrid,
|
|
54
|
-
x: number,
|
|
55
|
-
y: number,
|
|
56
|
-
direction: 1 | -1,
|
|
57
|
-
style: SequenceCellStyle,
|
|
58
|
-
): void {
|
|
59
|
-
setCell(
|
|
60
|
-
grid,
|
|
61
|
-
x,
|
|
62
|
-
y,
|
|
63
|
-
direction === 1 ? SEQUENCE_BORDER.leftT : SEQUENCE_BORDER.rightT,
|
|
64
|
-
`${style}Fade1` as SequenceCellStyle,
|
|
65
|
-
)
|
|
66
|
-
for (let step = 2; step <= 5; step++) {
|
|
67
|
-
setCell(grid, x + direction * (step - 1), y, SEQUENCE_BORDER.horizontal, `${style}Fade${step}` as SequenceCellStyle)
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function groupVerticalChar(existing: string | undefined): string | undefined {
|
|
72
|
-
switch (existing) {
|
|
73
|
-
case undefined:
|
|
74
|
-
case " ":
|
|
75
|
-
return SEQUENCE_BORDER.vertical
|
|
76
|
-
case SEQUENCE_BORDER.vertical:
|
|
77
|
-
return SEQUENCE_BORDER.vertical
|
|
78
|
-
default:
|
|
79
|
-
return undefined
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
function setGroupVerticalCell(grid: SequenceGrid, x: number, y: number): void {
|
|
84
|
-
const existing = grid.getCell(x, y)?.char
|
|
85
|
-
const char = groupVerticalChar(existing)
|
|
86
|
-
if (char) setCell(grid, x, y, char, "group")
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function renderParticipantGroups(
|
|
90
|
-
grid: SequenceGrid,
|
|
91
|
-
groupBounds: readonly SequenceGroupPlacement[],
|
|
92
|
-
bottomY: number,
|
|
93
|
-
): void {
|
|
94
|
-
for (const bounds of groupBounds) {
|
|
95
|
-
for (let x = bounds.leftX; x <= bounds.rightX; x++) {
|
|
96
|
-
setCell(grid, x, 0, SEQUENCE_BORDER.horizontal, "group")
|
|
97
|
-
setCell(grid, x, bottomY, SEQUENCE_BORDER.horizontal, "group")
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
setCell(grid, bounds.leftX, 0, SEQUENCE_BORDER.topLeft, "group")
|
|
101
|
-
setCell(grid, bounds.rightX, 0, SEQUENCE_BORDER.topRight, "group")
|
|
102
|
-
setCell(grid, bounds.leftX, bottomY, SEQUENCE_BORDER.bottomLeft, "group")
|
|
103
|
-
setCell(grid, bounds.rightX, bottomY, SEQUENCE_BORDER.bottomRight, "group")
|
|
104
|
-
|
|
105
|
-
for (let y = 1; y < bottomY; y++) {
|
|
106
|
-
setGroupVerticalCell(grid, bounds.leftX, y)
|
|
107
|
-
setGroupVerticalCell(grid, bounds.rightX, y)
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
if (bounds.labelText) {
|
|
111
|
-
setText(grid, bounds.leftX + 2, 0, bounds.labelText, "group")
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
function drawFragmentWalls(
|
|
117
|
-
grid: SequenceGrid,
|
|
118
|
-
bounds: SequenceHorizontalBounds,
|
|
119
|
-
startY: number,
|
|
120
|
-
endY: number,
|
|
121
|
-
borderStyle: BorderStyle,
|
|
122
|
-
): void {
|
|
123
|
-
if (endY < startY) return
|
|
124
|
-
const border = BorderChars[borderStyle]
|
|
125
|
-
|
|
126
|
-
for (let y = startY; y <= endY; y++) {
|
|
127
|
-
setCell(grid, bounds.leftX, y, border.vertical, "fragment")
|
|
128
|
-
setCell(grid, bounds.rightX, y, border.vertical, "fragment")
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
function renderFragment(
|
|
133
|
-
grid: SequenceGrid,
|
|
134
|
-
placement: Extract<SequenceStepPlacement, { type: "fragment" }>,
|
|
135
|
-
borderStyle: BorderStyle,
|
|
136
|
-
): void {
|
|
137
|
-
const { bounds, fragment, labelText: label, y } = placement
|
|
138
|
-
const border = BorderChars[borderStyle]
|
|
139
|
-
const { leftX, rightX } = bounds
|
|
140
|
-
|
|
141
|
-
const leftChar =
|
|
142
|
-
fragment.kind === "alt" || fragment.kind === "loop"
|
|
143
|
-
? border.topLeft
|
|
144
|
-
: fragment.kind === "else"
|
|
145
|
-
? border.leftT
|
|
146
|
-
: border.bottomLeft
|
|
147
|
-
const rightChar =
|
|
148
|
-
fragment.kind === "alt" || fragment.kind === "loop"
|
|
149
|
-
? border.topRight
|
|
150
|
-
: fragment.kind === "else"
|
|
151
|
-
? border.rightT
|
|
152
|
-
: border.bottomRight
|
|
153
|
-
|
|
154
|
-
for (let x = leftX; x <= rightX; x++) {
|
|
155
|
-
setCell(grid, x, y, border.horizontal, "fragment")
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
setCell(grid, leftX, y, leftChar, "fragment")
|
|
159
|
-
setCell(grid, rightX, y, rightChar, "fragment")
|
|
160
|
-
if (label) {
|
|
161
|
-
setText(grid, leftX + 2, y, label, "fragmentLabel")
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
function renderSelfMessage(
|
|
166
|
-
grid: SequenceGrid,
|
|
167
|
-
placement: Extract<SequenceStepPlacement, { type: "selfMessage" }>,
|
|
168
|
-
style: MessageStyle,
|
|
169
|
-
): void {
|
|
170
|
-
const { centerX, rightX, topY: topRow, bottomY: bottomRow, labelLines, message } = placement
|
|
171
|
-
|
|
172
|
-
setArrowDepartureFade(grid, centerX, topRow, 1, style)
|
|
173
|
-
for (let x = centerX + FADE_STEPS.length; x < rightX; x++) {
|
|
174
|
-
setCell(grid, x, topRow, SEQUENCE_BORDER.horizontal, style)
|
|
175
|
-
}
|
|
176
|
-
setCell(grid, rightX, topRow, SEQUENCE_BORDER.topRight, style)
|
|
177
|
-
|
|
178
|
-
for (let lineIndex = 0; lineIndex < labelLines.length; lineIndex++) {
|
|
179
|
-
const y = topRow + lineIndex + 1
|
|
180
|
-
setCell(grid, centerX, y, SEQUENCE_BORDER.vertical, "lifeline")
|
|
181
|
-
setText(grid, centerX + 2, y, labelLines[lineIndex]!, style)
|
|
182
|
-
setCell(grid, rightX, y, SEQUENCE_BORDER.vertical, style)
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
for (let x = centerX + 1; x < rightX; x++) {
|
|
186
|
-
setCell(grid, x, bottomRow, SEQUENCE_BORDER.horizontal, style)
|
|
187
|
-
}
|
|
188
|
-
const headX = message.head === undefined ? centerX : centerX + 1
|
|
189
|
-
setCell(grid, headX, bottomRow, arrowHeadChar(message.head, -1), style)
|
|
190
|
-
setCell(grid, rightX, bottomRow, SEQUENCE_BORDER.bottomRight, style)
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
function renderNote(grid: SequenceGrid, placement: Extract<SequenceStepPlacement, { type: "note" }>): void {
|
|
194
|
-
placement.textLines.forEach((line, index) =>
|
|
195
|
-
setText(grid, placement.textX, placement.textY + index, line, "noteBadge"),
|
|
196
|
-
)
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
export function drawSequenceDiagramGrid(
|
|
200
|
-
diagram: SequenceDiagram,
|
|
201
|
-
options: SequenceDiagramRenderOptions = {},
|
|
202
|
-
): SequenceGrid {
|
|
203
|
-
const plan = createSequencePlacementPlan(diagram, options)
|
|
204
|
-
if (plan.width === 0 || plan.height === 0) return createGrid(0, 0)
|
|
205
|
-
const fragmentBorderStyle = options.fragmentBorderStyle ?? DEFAULT_FRAGMENT_BORDER_STYLE
|
|
206
|
-
const grid = createGrid(plan.width, plan.height)
|
|
207
|
-
|
|
208
|
-
if (plan.groups.length > 0) renderParticipantGroups(grid, plan.groups, plan.height - 1)
|
|
209
|
-
|
|
210
|
-
for (const placement of plan.participants) {
|
|
211
|
-
const { centerX: center, headerLeftX, headerRightX, labelLines } = placement
|
|
212
|
-
const { participantHeaderTopY, participantHeaderY, participantRuleY, lifelineStartY, lifelineEndY } = plan.rows
|
|
213
|
-
|
|
214
|
-
if (options.compact) {
|
|
215
|
-
labelLines.forEach((line, index) =>
|
|
216
|
-
setText(grid, centeredStart(center, line), participantHeaderY + index, line, "participant"),
|
|
217
|
-
)
|
|
218
|
-
} else {
|
|
219
|
-
labelLines.forEach((line, index) =>
|
|
220
|
-
setText(grid, centeredStart(center, line), participantHeaderTopY + index, line, "participant"),
|
|
221
|
-
)
|
|
222
|
-
for (let x = headerLeftX; x <= headerRightX; x++) {
|
|
223
|
-
setCell(grid, x, participantRuleY, SEQUENCE_BORDER.horizontal, "participant")
|
|
224
|
-
}
|
|
225
|
-
setCell(grid, center, participantRuleY, SEQUENCE_BORDER.topT, "participant")
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
for (let y = lifelineStartY; y <= lifelineEndY; y++) {
|
|
229
|
-
const distance = y - lifelineStartY
|
|
230
|
-
const style = !options.compact && distance < 3 ? (`lifelineRamp${distance + 1}` as SequenceCellStyle) : "lifeline"
|
|
231
|
-
setCell(grid, center, y, SEQUENCE_BORDER.vertical, style)
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
for (const placement of plan.steps) {
|
|
236
|
-
if (placement.type === "note") {
|
|
237
|
-
renderNote(grid, placement)
|
|
238
|
-
continue
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
if (placement.type === "fragment") {
|
|
242
|
-
if (placement.wallsBefore) {
|
|
243
|
-
drawFragmentWalls(
|
|
244
|
-
grid,
|
|
245
|
-
placement.wallsBefore.bounds,
|
|
246
|
-
placement.wallsBefore.startY,
|
|
247
|
-
placement.wallsBefore.endY,
|
|
248
|
-
fragmentBorderStyle,
|
|
249
|
-
)
|
|
250
|
-
}
|
|
251
|
-
renderFragment(grid, placement, fragmentBorderStyle)
|
|
252
|
-
continue
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
const message = placement.message
|
|
256
|
-
const messageStyle: MessageStyle = message.style === "dashed" ? "response" : "request"
|
|
257
|
-
|
|
258
|
-
if (placement.type === "selfMessage") {
|
|
259
|
-
renderSelfMessage(grid, placement, messageStyle)
|
|
260
|
-
continue
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
if (!placement.inlineLabel) {
|
|
264
|
-
for (let lineIndex = 0; lineIndex < placement.labelLines.length; lineIndex++) {
|
|
265
|
-
setText(grid, placement.labelX, placement.labelY + lineIndex, placement.labelLines[lineIndex]!, messageStyle)
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
for (let x = placement.leftX + 1; x < placement.rightX; x++) {
|
|
270
|
-
setCell(grid, x, placement.arrowY, SEQUENCE_BORDER.horizontal, messageStyle)
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
setArrowDepartureFade(grid, placement.fromX, placement.arrowY, placement.direction, messageStyle)
|
|
274
|
-
setCell(grid, placement.headX, placement.arrowY, arrowHeadChar(message.head, placement.direction), messageStyle)
|
|
275
|
-
if (placement.inlineLabel) setText(grid, placement.labelX, placement.labelY, placement.inlineLabel, messageStyle)
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
for (const activation of plan.activations) {
|
|
279
|
-
for (let y = activation.startY; y <= activation.endY; y++) {
|
|
280
|
-
if (grid.getCell(activation.centerX, y)?.char === SEQUENCE_BORDER.vertical) {
|
|
281
|
-
setCell(grid, activation.centerX, y, "┃", "lifeline")
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
return grid
|
|
287
|
-
}
|
|
1
|
+
import { BorderChars, type BorderStyle } from "@opentui/core"
|
|
2
|
+
import { DiagramCanvas } from "../core/canvas.js"
|
|
3
|
+
import { diagramTextWidth } from "../core/text.js"
|
|
4
|
+
import { DEFAULT_FRAGMENT_BORDER_STYLE } from "./options.js"
|
|
5
|
+
import {
|
|
6
|
+
createSequencePlacementPlan,
|
|
7
|
+
type SequenceGroupPlacement,
|
|
8
|
+
type SequenceHorizontalBounds,
|
|
9
|
+
type SequenceStepPlacement,
|
|
10
|
+
} from "./placement.js"
|
|
11
|
+
import type { SequenceGrid } from "./render-grid.js"
|
|
12
|
+
import { SEQUENCE_FADE_STEPS as FADE_STEPS } from "./style.js"
|
|
13
|
+
import type {
|
|
14
|
+
MessageStyle,
|
|
15
|
+
SequenceArrowHead,
|
|
16
|
+
SequenceCellStyle,
|
|
17
|
+
SequenceDiagram,
|
|
18
|
+
SequenceDiagramRenderOptions,
|
|
19
|
+
} from "./types.js"
|
|
20
|
+
|
|
21
|
+
const SEQUENCE_BORDER = BorderChars.rounded
|
|
22
|
+
|
|
23
|
+
function centeredStart(center: number, text: string): number {
|
|
24
|
+
return center - Math.floor(diagramTextWidth(text) / 2)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function arrowHeadChar(head: SequenceArrowHead | undefined, direction: 1 | -1): string {
|
|
28
|
+
switch (head) {
|
|
29
|
+
case "open":
|
|
30
|
+
return direction === 1 ? ">" : "<"
|
|
31
|
+
case "cross":
|
|
32
|
+
return "✕"
|
|
33
|
+
case "async":
|
|
34
|
+
return direction === 1 ? ")" : "("
|
|
35
|
+
default:
|
|
36
|
+
return direction === 1 ? "►" : "◄"
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function createGrid(width: number, height: number): SequenceGrid {
|
|
41
|
+
return new DiagramCanvas(width, height)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function setCell(grid: SequenceGrid, x: number, y: number, char: string, style?: SequenceCellStyle): void {
|
|
45
|
+
grid.setCell(x, y, char, style)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function setText(grid: SequenceGrid, x: number, y: number, text: string, style?: SequenceCellStyle): void {
|
|
49
|
+
grid.setText(Math.max(0, x), y, text, style)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function setArrowDepartureFade(
|
|
53
|
+
grid: SequenceGrid,
|
|
54
|
+
x: number,
|
|
55
|
+
y: number,
|
|
56
|
+
direction: 1 | -1,
|
|
57
|
+
style: SequenceCellStyle,
|
|
58
|
+
): void {
|
|
59
|
+
setCell(
|
|
60
|
+
grid,
|
|
61
|
+
x,
|
|
62
|
+
y,
|
|
63
|
+
direction === 1 ? SEQUENCE_BORDER.leftT : SEQUENCE_BORDER.rightT,
|
|
64
|
+
`${style}Fade1` as SequenceCellStyle,
|
|
65
|
+
)
|
|
66
|
+
for (let step = 2; step <= 5; step++) {
|
|
67
|
+
setCell(grid, x + direction * (step - 1), y, SEQUENCE_BORDER.horizontal, `${style}Fade${step}` as SequenceCellStyle)
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function groupVerticalChar(existing: string | undefined): string | undefined {
|
|
72
|
+
switch (existing) {
|
|
73
|
+
case undefined:
|
|
74
|
+
case " ":
|
|
75
|
+
return SEQUENCE_BORDER.vertical
|
|
76
|
+
case SEQUENCE_BORDER.vertical:
|
|
77
|
+
return SEQUENCE_BORDER.vertical
|
|
78
|
+
default:
|
|
79
|
+
return undefined
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function setGroupVerticalCell(grid: SequenceGrid, x: number, y: number): void {
|
|
84
|
+
const existing = grid.getCell(x, y)?.char
|
|
85
|
+
const char = groupVerticalChar(existing)
|
|
86
|
+
if (char) setCell(grid, x, y, char, "group")
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function renderParticipantGroups(
|
|
90
|
+
grid: SequenceGrid,
|
|
91
|
+
groupBounds: readonly SequenceGroupPlacement[],
|
|
92
|
+
bottomY: number,
|
|
93
|
+
): void {
|
|
94
|
+
for (const bounds of groupBounds) {
|
|
95
|
+
for (let x = bounds.leftX; x <= bounds.rightX; x++) {
|
|
96
|
+
setCell(grid, x, 0, SEQUENCE_BORDER.horizontal, "group")
|
|
97
|
+
setCell(grid, x, bottomY, SEQUENCE_BORDER.horizontal, "group")
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
setCell(grid, bounds.leftX, 0, SEQUENCE_BORDER.topLeft, "group")
|
|
101
|
+
setCell(grid, bounds.rightX, 0, SEQUENCE_BORDER.topRight, "group")
|
|
102
|
+
setCell(grid, bounds.leftX, bottomY, SEQUENCE_BORDER.bottomLeft, "group")
|
|
103
|
+
setCell(grid, bounds.rightX, bottomY, SEQUENCE_BORDER.bottomRight, "group")
|
|
104
|
+
|
|
105
|
+
for (let y = 1; y < bottomY; y++) {
|
|
106
|
+
setGroupVerticalCell(grid, bounds.leftX, y)
|
|
107
|
+
setGroupVerticalCell(grid, bounds.rightX, y)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (bounds.labelText) {
|
|
111
|
+
setText(grid, bounds.leftX + 2, 0, bounds.labelText, "group")
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function drawFragmentWalls(
|
|
117
|
+
grid: SequenceGrid,
|
|
118
|
+
bounds: SequenceHorizontalBounds,
|
|
119
|
+
startY: number,
|
|
120
|
+
endY: number,
|
|
121
|
+
borderStyle: BorderStyle,
|
|
122
|
+
): void {
|
|
123
|
+
if (endY < startY) return
|
|
124
|
+
const border = BorderChars[borderStyle]
|
|
125
|
+
|
|
126
|
+
for (let y = startY; y <= endY; y++) {
|
|
127
|
+
setCell(grid, bounds.leftX, y, border.vertical, "fragment")
|
|
128
|
+
setCell(grid, bounds.rightX, y, border.vertical, "fragment")
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function renderFragment(
|
|
133
|
+
grid: SequenceGrid,
|
|
134
|
+
placement: Extract<SequenceStepPlacement, { type: "fragment" }>,
|
|
135
|
+
borderStyle: BorderStyle,
|
|
136
|
+
): void {
|
|
137
|
+
const { bounds, fragment, labelText: label, y } = placement
|
|
138
|
+
const border = BorderChars[borderStyle]
|
|
139
|
+
const { leftX, rightX } = bounds
|
|
140
|
+
|
|
141
|
+
const leftChar =
|
|
142
|
+
fragment.kind === "alt" || fragment.kind === "loop"
|
|
143
|
+
? border.topLeft
|
|
144
|
+
: fragment.kind === "else"
|
|
145
|
+
? border.leftT
|
|
146
|
+
: border.bottomLeft
|
|
147
|
+
const rightChar =
|
|
148
|
+
fragment.kind === "alt" || fragment.kind === "loop"
|
|
149
|
+
? border.topRight
|
|
150
|
+
: fragment.kind === "else"
|
|
151
|
+
? border.rightT
|
|
152
|
+
: border.bottomRight
|
|
153
|
+
|
|
154
|
+
for (let x = leftX; x <= rightX; x++) {
|
|
155
|
+
setCell(grid, x, y, border.horizontal, "fragment")
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
setCell(grid, leftX, y, leftChar, "fragment")
|
|
159
|
+
setCell(grid, rightX, y, rightChar, "fragment")
|
|
160
|
+
if (label) {
|
|
161
|
+
setText(grid, leftX + 2, y, label, "fragmentLabel")
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function renderSelfMessage(
|
|
166
|
+
grid: SequenceGrid,
|
|
167
|
+
placement: Extract<SequenceStepPlacement, { type: "selfMessage" }>,
|
|
168
|
+
style: MessageStyle,
|
|
169
|
+
): void {
|
|
170
|
+
const { centerX, rightX, topY: topRow, bottomY: bottomRow, labelLines, message } = placement
|
|
171
|
+
|
|
172
|
+
setArrowDepartureFade(grid, centerX, topRow, 1, style)
|
|
173
|
+
for (let x = centerX + FADE_STEPS.length; x < rightX; x++) {
|
|
174
|
+
setCell(grid, x, topRow, SEQUENCE_BORDER.horizontal, style)
|
|
175
|
+
}
|
|
176
|
+
setCell(grid, rightX, topRow, SEQUENCE_BORDER.topRight, style)
|
|
177
|
+
|
|
178
|
+
for (let lineIndex = 0; lineIndex < labelLines.length; lineIndex++) {
|
|
179
|
+
const y = topRow + lineIndex + 1
|
|
180
|
+
setCell(grid, centerX, y, SEQUENCE_BORDER.vertical, "lifeline")
|
|
181
|
+
setText(grid, centerX + 2, y, labelLines[lineIndex]!, style)
|
|
182
|
+
setCell(grid, rightX, y, SEQUENCE_BORDER.vertical, style)
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
for (let x = centerX + 1; x < rightX; x++) {
|
|
186
|
+
setCell(grid, x, bottomRow, SEQUENCE_BORDER.horizontal, style)
|
|
187
|
+
}
|
|
188
|
+
const headX = message.head === undefined ? centerX : centerX + 1
|
|
189
|
+
setCell(grid, headX, bottomRow, arrowHeadChar(message.head, -1), style)
|
|
190
|
+
setCell(grid, rightX, bottomRow, SEQUENCE_BORDER.bottomRight, style)
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function renderNote(grid: SequenceGrid, placement: Extract<SequenceStepPlacement, { type: "note" }>): void {
|
|
194
|
+
placement.textLines.forEach((line, index) =>
|
|
195
|
+
setText(grid, placement.textX, placement.textY + index, line, "noteBadge"),
|
|
196
|
+
)
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export function drawSequenceDiagramGrid(
|
|
200
|
+
diagram: SequenceDiagram,
|
|
201
|
+
options: SequenceDiagramRenderOptions = {},
|
|
202
|
+
): SequenceGrid {
|
|
203
|
+
const plan = createSequencePlacementPlan(diagram, options)
|
|
204
|
+
if (plan.width === 0 || plan.height === 0) return createGrid(0, 0)
|
|
205
|
+
const fragmentBorderStyle = options.fragmentBorderStyle ?? DEFAULT_FRAGMENT_BORDER_STYLE
|
|
206
|
+
const grid = createGrid(plan.width, plan.height)
|
|
207
|
+
|
|
208
|
+
if (plan.groups.length > 0) renderParticipantGroups(grid, plan.groups, plan.height - 1)
|
|
209
|
+
|
|
210
|
+
for (const placement of plan.participants) {
|
|
211
|
+
const { centerX: center, headerLeftX, headerRightX, labelLines } = placement
|
|
212
|
+
const { participantHeaderTopY, participantHeaderY, participantRuleY, lifelineStartY, lifelineEndY } = plan.rows
|
|
213
|
+
|
|
214
|
+
if (options.compact) {
|
|
215
|
+
labelLines.forEach((line, index) =>
|
|
216
|
+
setText(grid, centeredStart(center, line), participantHeaderY + index, line, "participant"),
|
|
217
|
+
)
|
|
218
|
+
} else {
|
|
219
|
+
labelLines.forEach((line, index) =>
|
|
220
|
+
setText(grid, centeredStart(center, line), participantHeaderTopY + index, line, "participant"),
|
|
221
|
+
)
|
|
222
|
+
for (let x = headerLeftX; x <= headerRightX; x++) {
|
|
223
|
+
setCell(grid, x, participantRuleY, SEQUENCE_BORDER.horizontal, "participant")
|
|
224
|
+
}
|
|
225
|
+
setCell(grid, center, participantRuleY, SEQUENCE_BORDER.topT, "participant")
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
for (let y = lifelineStartY; y <= lifelineEndY; y++) {
|
|
229
|
+
const distance = y - lifelineStartY
|
|
230
|
+
const style = !options.compact && distance < 3 ? (`lifelineRamp${distance + 1}` as SequenceCellStyle) : "lifeline"
|
|
231
|
+
setCell(grid, center, y, SEQUENCE_BORDER.vertical, style)
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
for (const placement of plan.steps) {
|
|
236
|
+
if (placement.type === "note") {
|
|
237
|
+
renderNote(grid, placement)
|
|
238
|
+
continue
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
if (placement.type === "fragment") {
|
|
242
|
+
if (placement.wallsBefore) {
|
|
243
|
+
drawFragmentWalls(
|
|
244
|
+
grid,
|
|
245
|
+
placement.wallsBefore.bounds,
|
|
246
|
+
placement.wallsBefore.startY,
|
|
247
|
+
placement.wallsBefore.endY,
|
|
248
|
+
fragmentBorderStyle,
|
|
249
|
+
)
|
|
250
|
+
}
|
|
251
|
+
renderFragment(grid, placement, fragmentBorderStyle)
|
|
252
|
+
continue
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const message = placement.message
|
|
256
|
+
const messageStyle: MessageStyle = message.style === "dashed" ? "response" : "request"
|
|
257
|
+
|
|
258
|
+
if (placement.type === "selfMessage") {
|
|
259
|
+
renderSelfMessage(grid, placement, messageStyle)
|
|
260
|
+
continue
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
if (!placement.inlineLabel) {
|
|
264
|
+
for (let lineIndex = 0; lineIndex < placement.labelLines.length; lineIndex++) {
|
|
265
|
+
setText(grid, placement.labelX, placement.labelY + lineIndex, placement.labelLines[lineIndex]!, messageStyle)
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
for (let x = placement.leftX + 1; x < placement.rightX; x++) {
|
|
270
|
+
setCell(grid, x, placement.arrowY, SEQUENCE_BORDER.horizontal, messageStyle)
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
setArrowDepartureFade(grid, placement.fromX, placement.arrowY, placement.direction, messageStyle)
|
|
274
|
+
setCell(grid, placement.headX, placement.arrowY, arrowHeadChar(message.head, placement.direction), messageStyle)
|
|
275
|
+
if (placement.inlineLabel) setText(grid, placement.labelX, placement.labelY, placement.inlineLabel, messageStyle)
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
for (const activation of plan.activations) {
|
|
279
|
+
for (let y = activation.startY; y <= activation.endY; y++) {
|
|
280
|
+
if (grid.getCell(activation.centerX, y)?.char === SEQUENCE_BORDER.vertical) {
|
|
281
|
+
setCell(grid, activation.centerX, y, "┃", "lifeline")
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
return grid
|
|
287
|
+
}
|
package/src/sequence/options.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type { BorderStyle } from "@opentui/core"
|
|
2
|
-
|
|
3
|
-
export const DEFAULT_MIN_PARTICIPANT_GAP = 18
|
|
4
|
-
export const DEFAULT_FRAGMENT_BORDER_STYLE = "rounded" satisfies BorderStyle
|
|
5
|
-
|
|
6
|
-
export function normalizeSequenceMinParticipantGap(value: number | undefined): number {
|
|
7
|
-
return value === undefined || !Number.isFinite(value) ? DEFAULT_MIN_PARTICIPANT_GAP : Math.max(1, Math.floor(value))
|
|
8
|
-
}
|
|
1
|
+
import type { BorderStyle } from "@opentui/core"
|
|
2
|
+
|
|
3
|
+
export const DEFAULT_MIN_PARTICIPANT_GAP = 18
|
|
4
|
+
export const DEFAULT_FRAGMENT_BORDER_STYLE = "rounded" satisfies BorderStyle
|
|
5
|
+
|
|
6
|
+
export function normalizeSequenceMinParticipantGap(value: number | undefined): number {
|
|
7
|
+
return value === undefined || !Number.isFinite(value) ? DEFAULT_MIN_PARTICIPANT_GAP : Math.max(1, Math.floor(value))
|
|
8
|
+
}
|