@oh-my-pi/pi-utils 16.0.6 → 16.0.8
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/CHANGELOG.md +10 -0
- package/dist/types/mermaid-ascii.d.ts +1 -1
- package/dist/types/vendor/mermaid-ascii/ascii/ansi.d.ts +41 -0
- package/dist/types/vendor/mermaid-ascii/ascii/canvas.d.ts +89 -0
- package/dist/types/vendor/mermaid-ascii/ascii/class-diagram.d.ts +7 -0
- package/dist/types/vendor/mermaid-ascii/ascii/converter.d.ts +12 -0
- package/dist/types/vendor/mermaid-ascii/ascii/draw.d.ts +66 -0
- package/dist/types/vendor/mermaid-ascii/ascii/edge-bundling.d.ts +48 -0
- package/dist/types/vendor/mermaid-ascii/ascii/edge-routing.d.ts +43 -0
- package/dist/types/vendor/mermaid-ascii/ascii/er-diagram.d.ts +7 -0
- package/dist/types/vendor/mermaid-ascii/ascii/grid.d.ts +56 -0
- package/dist/types/vendor/mermaid-ascii/ascii/index.d.ts +65 -0
- package/dist/types/vendor/mermaid-ascii/ascii/multiline-utils.d.ts +27 -0
- package/dist/types/vendor/mermaid-ascii/ascii/pathfinder.d.ts +17 -0
- package/dist/types/vendor/mermaid-ascii/ascii/sequence.d.ts +7 -0
- package/dist/types/vendor/mermaid-ascii/ascii/shapes/circle.d.ts +11 -0
- package/dist/types/vendor/mermaid-ascii/ascii/shapes/corners.d.ts +34 -0
- package/dist/types/vendor/mermaid-ascii/ascii/shapes/diamond.d.ts +11 -0
- package/dist/types/vendor/mermaid-ascii/ascii/shapes/hexagon.d.ts +11 -0
- package/dist/types/vendor/mermaid-ascii/ascii/shapes/index.d.ts +26 -0
- package/dist/types/vendor/mermaid-ascii/ascii/shapes/rectangle.d.ts +31 -0
- package/dist/types/vendor/mermaid-ascii/ascii/shapes/rounded.d.ts +11 -0
- package/dist/types/vendor/mermaid-ascii/ascii/shapes/special.d.ts +59 -0
- package/dist/types/vendor/mermaid-ascii/ascii/shapes/stadium.d.ts +17 -0
- package/dist/types/vendor/mermaid-ascii/ascii/shapes/state.d.ts +30 -0
- package/dist/types/vendor/mermaid-ascii/ascii/shapes/types.d.ts +55 -0
- package/dist/types/vendor/mermaid-ascii/ascii/types.d.ts +206 -0
- package/dist/types/vendor/mermaid-ascii/ascii/validate.d.ts +51 -0
- package/dist/types/vendor/mermaid-ascii/ascii/xychart.d.ts +2 -0
- package/dist/types/vendor/mermaid-ascii/class/parser.d.ts +6 -0
- package/dist/types/vendor/mermaid-ascii/class/types.d.ts +102 -0
- package/dist/types/vendor/mermaid-ascii/er/parser.d.ts +6 -0
- package/dist/types/vendor/mermaid-ascii/er/types.d.ts +76 -0
- package/dist/types/vendor/mermaid-ascii/index.d.ts +1 -0
- package/dist/types/vendor/mermaid-ascii/multiline-utils.d.ts +9 -0
- package/dist/types/vendor/mermaid-ascii/parser.d.ts +7 -0
- package/dist/types/vendor/mermaid-ascii/sequence/parser.d.ts +6 -0
- package/dist/types/vendor/mermaid-ascii/sequence/types.d.ts +130 -0
- package/dist/types/vendor/mermaid-ascii/text-metrics.d.ts +21 -0
- package/dist/types/vendor/mermaid-ascii/types.d.ts +114 -0
- package/dist/types/vendor/mermaid-ascii/xychart/colors.d.ts +25 -0
- package/dist/types/vendor/mermaid-ascii/xychart/parser.d.ts +6 -0
- package/dist/types/vendor/mermaid-ascii/xychart/types.d.ts +145 -0
- package/package.json +2 -3
- package/src/mermaid-ascii.ts +1 -1
- package/src/vendor/mermaid-ascii/NOTICE +33 -0
- package/src/vendor/mermaid-ascii/ascii/ansi.ts +409 -0
- package/src/vendor/mermaid-ascii/ascii/canvas.ts +476 -0
- package/src/vendor/mermaid-ascii/ascii/class-diagram.ts +699 -0
- package/src/vendor/mermaid-ascii/ascii/converter.ts +271 -0
- package/src/vendor/mermaid-ascii/ascii/draw.ts +1382 -0
- package/src/vendor/mermaid-ascii/ascii/edge-bundling.ts +328 -0
- package/src/vendor/mermaid-ascii/ascii/edge-routing.ts +297 -0
- package/src/vendor/mermaid-ascii/ascii/er-diagram.ts +441 -0
- package/src/vendor/mermaid-ascii/ascii/grid.ts +578 -0
- package/src/vendor/mermaid-ascii/ascii/index.ts +187 -0
- package/src/vendor/mermaid-ascii/ascii/multiline-utils.ts +78 -0
- package/src/vendor/mermaid-ascii/ascii/pathfinder.ts +215 -0
- package/src/vendor/mermaid-ascii/ascii/sequence.ts +460 -0
- package/src/vendor/mermaid-ascii/ascii/shapes/circle.ts +27 -0
- package/src/vendor/mermaid-ascii/ascii/shapes/corners.ts +127 -0
- package/src/vendor/mermaid-ascii/ascii/shapes/diamond.ts +27 -0
- package/src/vendor/mermaid-ascii/ascii/shapes/hexagon.ts +27 -0
- package/src/vendor/mermaid-ascii/ascii/shapes/index.ts +101 -0
- package/src/vendor/mermaid-ascii/ascii/shapes/rectangle.ts +175 -0
- package/src/vendor/mermaid-ascii/ascii/shapes/rounded.ts +27 -0
- package/src/vendor/mermaid-ascii/ascii/shapes/special.ts +296 -0
- package/src/vendor/mermaid-ascii/ascii/shapes/stadium.ts +114 -0
- package/src/vendor/mermaid-ascii/ascii/shapes/state.ts +192 -0
- package/src/vendor/mermaid-ascii/ascii/shapes/types.ts +73 -0
- package/src/vendor/mermaid-ascii/ascii/types.ts +273 -0
- package/src/vendor/mermaid-ascii/ascii/validate.ts +120 -0
- package/src/vendor/mermaid-ascii/ascii/xychart.ts +875 -0
- package/src/vendor/mermaid-ascii/class/parser.ts +290 -0
- package/src/vendor/mermaid-ascii/class/types.ts +121 -0
- package/src/vendor/mermaid-ascii/er/parser.ts +181 -0
- package/src/vendor/mermaid-ascii/er/types.ts +91 -0
- package/src/vendor/mermaid-ascii/index.ts +14 -0
- package/src/vendor/mermaid-ascii/multiline-utils.ts +30 -0
- package/src/vendor/mermaid-ascii/parser.ts +645 -0
- package/src/vendor/mermaid-ascii/sequence/parser.ts +207 -0
- package/src/vendor/mermaid-ascii/sequence/types.ts +146 -0
- package/src/vendor/mermaid-ascii/text-metrics.ts +71 -0
- package/src/vendor/mermaid-ascii/types.ts +164 -0
- package/src/vendor/mermaid-ascii/xychart/colors.ts +140 -0
- package/src/vendor/mermaid-ascii/xychart/parser.ts +115 -0
- package/src/vendor/mermaid-ascii/xychart/types.ts +150 -0
|
@@ -0,0 +1,699 @@
|
|
|
1
|
+
// ============================================================================
|
|
2
|
+
// ASCII renderer — class diagrams
|
|
3
|
+
//
|
|
4
|
+
// Renders classDiagram text to ASCII/Unicode art.
|
|
5
|
+
// Each class is a multi-compartment box (header | attributes | methods).
|
|
6
|
+
// Relationships are drawn as lines between classes with UML markers.
|
|
7
|
+
//
|
|
8
|
+
// Layout: level-based top-down. "From" classes are placed above "to" classes
|
|
9
|
+
// for all relationship types, matching ELK/mermaid.com behavior.
|
|
10
|
+
// Relationship lines use simple Manhattan routing (vertical + horizontal).
|
|
11
|
+
// ============================================================================
|
|
12
|
+
|
|
13
|
+
import { parseClassDiagram } from '../class/parser'
|
|
14
|
+
import type { ClassDiagram, ClassNode, ClassMember, ClassRelationship, RelationshipType } from '../class/types'
|
|
15
|
+
import type { Canvas, AsciiConfig, RoleCanvas, CharRole, AsciiTheme, ColorMode } from './types'
|
|
16
|
+
import { mkCanvas, mkRoleCanvas, canvasToString, increaseSize, increaseRoleCanvasSize, setRole } from './canvas'
|
|
17
|
+
import { drawMultiBox } from './draw'
|
|
18
|
+
import { splitLines } from './multiline-utils'
|
|
19
|
+
import { displayWidth, toCells } from '../text-metrics'
|
|
20
|
+
|
|
21
|
+
/** Classify a character from a box drawing as 'border' or 'text'. */
|
|
22
|
+
function classifyBoxChar(ch: string): CharRole {
|
|
23
|
+
if (/^[┌┐└┘├┤┬┴┼│─╭╮╰╯+\-|]$/.test(ch)) return 'border'
|
|
24
|
+
return 'text'
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// ============================================================================
|
|
28
|
+
// Class member formatting
|
|
29
|
+
// ============================================================================
|
|
30
|
+
|
|
31
|
+
/** Format a class member as a display string: visibility + name + optional type */
|
|
32
|
+
function formatMember(m: ClassMember): string {
|
|
33
|
+
const vis = m.visibility || ''
|
|
34
|
+
const type = m.type ? `: ${m.type}` : ''
|
|
35
|
+
return `${vis}${m.name}${type}`
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Build the text sections for a class box: [header], [attributes], [methods] */
|
|
39
|
+
function buildClassSections(cls: ClassNode): string[][] {
|
|
40
|
+
// Header section: optional annotation + class name (may be multi-line)
|
|
41
|
+
const header: string[] = []
|
|
42
|
+
if (cls.annotation) header.push(`<<${cls.annotation}>>`)
|
|
43
|
+
// Support multi-line class names
|
|
44
|
+
const nameLines = splitLines(cls.label)
|
|
45
|
+
header.push(...nameLines)
|
|
46
|
+
|
|
47
|
+
// Attributes section
|
|
48
|
+
const attrs = cls.attributes.map(formatMember)
|
|
49
|
+
|
|
50
|
+
// Methods section
|
|
51
|
+
const methods = cls.methods.map(formatMember)
|
|
52
|
+
|
|
53
|
+
// If no attrs and no methods, just return header (1-section box)
|
|
54
|
+
if (attrs.length === 0 && methods.length === 0) return [header]
|
|
55
|
+
// If no methods, return header + attrs (2-section box)
|
|
56
|
+
if (methods.length === 0) return [header, attrs]
|
|
57
|
+
// Full 3-section box
|
|
58
|
+
return [header, attrs, methods]
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// ============================================================================
|
|
62
|
+
// Relationship marker characters
|
|
63
|
+
// ============================================================================
|
|
64
|
+
|
|
65
|
+
interface RelMarker {
|
|
66
|
+
/** Relationship type (determines marker shape) */
|
|
67
|
+
type: RelationshipType
|
|
68
|
+
/** Which end the marker is placed at */
|
|
69
|
+
markerAt: 'from' | 'to'
|
|
70
|
+
/** Whether the line is dashed */
|
|
71
|
+
dashed: boolean
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Build the marker metadata for a relationship.
|
|
76
|
+
* The actual marker character will be determined at placement time based on line direction.
|
|
77
|
+
*/
|
|
78
|
+
function getRelMarker(type: RelationshipType, markerAt: 'from' | 'to'): RelMarker {
|
|
79
|
+
const dashed = type === 'dependency' || type === 'realization'
|
|
80
|
+
return { type, markerAt, dashed }
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Get the UML marker shape character for a relationship type.
|
|
85
|
+
* For directional arrows (association/dependency), the direction parameter
|
|
86
|
+
* specifies which way the arrow should point.
|
|
87
|
+
*/
|
|
88
|
+
function getMarkerShape(
|
|
89
|
+
type: RelationshipType,
|
|
90
|
+
useAscii: boolean,
|
|
91
|
+
direction?: 'up' | 'down' | 'left' | 'right'
|
|
92
|
+
): string {
|
|
93
|
+
switch (type) {
|
|
94
|
+
case 'inheritance':
|
|
95
|
+
case 'realization':
|
|
96
|
+
// Hollow triangle - rotate based on line direction
|
|
97
|
+
// Triangle points TOWARD the parent class
|
|
98
|
+
if (direction === 'down') {
|
|
99
|
+
// Line goes down (parent above, child below) - triangle points UP
|
|
100
|
+
return useAscii ? '^' : '△'
|
|
101
|
+
} else if (direction === 'up') {
|
|
102
|
+
// Line goes up (parent below, child above) - triangle points DOWN
|
|
103
|
+
return useAscii ? 'v' : '▽'
|
|
104
|
+
} else if (direction === 'left') {
|
|
105
|
+
// Line goes left - triangle points LEFT
|
|
106
|
+
return useAscii ? '>' : '◁'
|
|
107
|
+
} else {
|
|
108
|
+
// Default: line goes right - triangle points RIGHT
|
|
109
|
+
return useAscii ? '<' : '▷'
|
|
110
|
+
}
|
|
111
|
+
case 'composition':
|
|
112
|
+
// Filled diamond - omnidirectional shape
|
|
113
|
+
return useAscii ? '*' : '◆'
|
|
114
|
+
case 'aggregation':
|
|
115
|
+
// Hollow diamond - omnidirectional shape
|
|
116
|
+
return useAscii ? 'o' : '◇'
|
|
117
|
+
case 'association':
|
|
118
|
+
case 'dependency':
|
|
119
|
+
// Directional arrow - rotate based on line direction
|
|
120
|
+
if (direction === 'down') {
|
|
121
|
+
return useAscii ? 'v' : '▼'
|
|
122
|
+
} else if (direction === 'up') {
|
|
123
|
+
return useAscii ? '^' : '▲'
|
|
124
|
+
} else if (direction === 'left') {
|
|
125
|
+
return useAscii ? '<' : '◀'
|
|
126
|
+
} else {
|
|
127
|
+
// Default to right (or when direction not specified)
|
|
128
|
+
return useAscii ? '>' : '▶'
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// ============================================================================
|
|
134
|
+
// Layout and rendering
|
|
135
|
+
// ============================================================================
|
|
136
|
+
|
|
137
|
+
/** Positioned class node on the canvas */
|
|
138
|
+
interface PlacedClass {
|
|
139
|
+
cls: ClassNode
|
|
140
|
+
sections: string[][]
|
|
141
|
+
x: number
|
|
142
|
+
y: number
|
|
143
|
+
width: number
|
|
144
|
+
height: number
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Render a Mermaid class diagram to ASCII/Unicode text.
|
|
149
|
+
*
|
|
150
|
+
* Pipeline: parse → build boxes → level-based layout → draw boxes → draw relationships → string.
|
|
151
|
+
*/
|
|
152
|
+
export function renderClassAscii(text: string, config: AsciiConfig, colorMode?: ColorMode, theme?: AsciiTheme): string {
|
|
153
|
+
const lines = text.split('\n').map(l => l.trim()).filter(l => l.length > 0 && !l.startsWith('%%'))
|
|
154
|
+
const diagram = parseClassDiagram(lines)
|
|
155
|
+
|
|
156
|
+
if (diagram.classes.length === 0) return ''
|
|
157
|
+
|
|
158
|
+
const useAscii = config.useAscii
|
|
159
|
+
const hGap = 4 // horizontal gap between class boxes
|
|
160
|
+
const vGap = 3 // vertical gap between levels (enough for relationship lines)
|
|
161
|
+
|
|
162
|
+
// --- Build box dimensions for each class ---
|
|
163
|
+
const classSections = new Map<string, string[][]>()
|
|
164
|
+
const classBoxW = new Map<string, number>()
|
|
165
|
+
const classBoxH = new Map<string, number>()
|
|
166
|
+
|
|
167
|
+
for (const cls of diagram.classes) {
|
|
168
|
+
const sections = buildClassSections(cls)
|
|
169
|
+
classSections.set(cls.id, sections)
|
|
170
|
+
|
|
171
|
+
// Compute box dimensions from drawMultiBox logic
|
|
172
|
+
let maxTextW = 0
|
|
173
|
+
for (const section of sections) {
|
|
174
|
+
for (const line of section) maxTextW = Math.max(maxTextW, displayWidth(line))
|
|
175
|
+
}
|
|
176
|
+
const boxW = maxTextW + 4 // 2 border + 2 padding
|
|
177
|
+
|
|
178
|
+
let totalLines = 0
|
|
179
|
+
for (const section of sections) totalLines += Math.max(section.length, 1)
|
|
180
|
+
const boxH = totalLines + (sections.length - 1) + 2 // section lines + dividers + top/bottom border
|
|
181
|
+
|
|
182
|
+
classBoxW.set(cls.id, boxW)
|
|
183
|
+
classBoxH.set(cls.id, boxH)
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// --- Assign levels: topological sort based on directed relationships ---
|
|
187
|
+
// All relationship types place "from" above "to" in the layout, matching
|
|
188
|
+
// ELK's layered algorithm and the official mermaid.com renderer behavior.
|
|
189
|
+
// For "Animal <|-- Dog": from="Animal", to="Dog" → Animal above Dog.
|
|
190
|
+
//
|
|
191
|
+
// Every relationship type (including association and dependency) forces nodes
|
|
192
|
+
// to different levels. Same-row routing for mixed diagrams causes collisions:
|
|
193
|
+
// detour lines overlap with cross-level routing, and labels overwrite box borders.
|
|
194
|
+
|
|
195
|
+
const classById = new Map<string, ClassNode>()
|
|
196
|
+
for (const cls of diagram.classes) classById.set(cls.id, cls)
|
|
197
|
+
|
|
198
|
+
const parents = new Map<string, Set<string>>() // child → set of parent IDs
|
|
199
|
+
const children = new Map<string, Set<string>>() // parent → set of child IDs
|
|
200
|
+
|
|
201
|
+
for (const rel of diagram.relationships) {
|
|
202
|
+
// For inheritance/realization, the marker (hollow triangle) points to the parent.
|
|
203
|
+
// - `Animal <|-- Dog` (markerAt='from'): Animal is parent, Dog is child
|
|
204
|
+
// - `Bird ..|> Flyable` (markerAt='to'): Flyable is parent, Bird is child
|
|
205
|
+
// For other relationships, use the default from→to direction.
|
|
206
|
+
const isHierarchical = rel.type === 'inheritance' || rel.type === 'realization'
|
|
207
|
+
const parentId = isHierarchical && rel.markerAt === 'to' ? rel.to : rel.from
|
|
208
|
+
const childId = isHierarchical && rel.markerAt === 'to' ? rel.from : rel.to
|
|
209
|
+
|
|
210
|
+
if (!parents.has(childId)) parents.set(childId, new Set())
|
|
211
|
+
parents.get(childId)!.add(parentId)
|
|
212
|
+
if (!children.has(parentId)) children.set(parentId, new Set())
|
|
213
|
+
children.get(parentId)!.add(childId)
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// BFS from roots (classes that have no parents) to assign levels.
|
|
217
|
+
// Cap at classes.length - 1 to prevent infinite loops on cyclic graphs
|
|
218
|
+
// (e.g. View --> Model and Model ..> View would otherwise push levels
|
|
219
|
+
// upward forever). In a DAG the longest path has at most N-1 edges.
|
|
220
|
+
const level = new Map<string, number>()
|
|
221
|
+
const roots = diagram.classes.filter(c => !parents.has(c.id) || parents.get(c.id)!.size === 0)
|
|
222
|
+
const queue: string[] = roots.map(c => c.id)
|
|
223
|
+
for (const id of queue) level.set(id, 0)
|
|
224
|
+
|
|
225
|
+
const levelCap = diagram.classes.length - 1
|
|
226
|
+
let qi = 0
|
|
227
|
+
while (qi < queue.length) {
|
|
228
|
+
const id = queue[qi++]!
|
|
229
|
+
const childSet = children.get(id)
|
|
230
|
+
if (!childSet) continue
|
|
231
|
+
for (const childId of childSet) {
|
|
232
|
+
const newLevel = (level.get(id) ?? 0) + 1
|
|
233
|
+
if (newLevel > levelCap) continue // cycle detected — skip to prevent infinite loop
|
|
234
|
+
if (!level.has(childId) || level.get(childId)! < newLevel) {
|
|
235
|
+
level.set(childId, newLevel)
|
|
236
|
+
queue.push(childId)
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// Assign remaining (unconnected) classes to level 0
|
|
242
|
+
for (const cls of diagram.classes) {
|
|
243
|
+
if (!level.has(cls.id)) level.set(cls.id, 0)
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// --- Position classes by level ---
|
|
247
|
+
// Group classes by level
|
|
248
|
+
const maxLevel = Math.max(...[...level.values()], 0)
|
|
249
|
+
const levelGroups: string[][] = Array.from({ length: maxLevel + 1 }, () => [])
|
|
250
|
+
for (const cls of diagram.classes) {
|
|
251
|
+
levelGroups[level.get(cls.id)!]!.push(cls.id)
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Compute positions: each level is a row, classes in a row are spaced horizontally
|
|
255
|
+
const placed = new Map<string, PlacedClass>()
|
|
256
|
+
let currentY = 0
|
|
257
|
+
|
|
258
|
+
for (let lv = 0; lv <= maxLevel; lv++) {
|
|
259
|
+
const group = levelGroups[lv]!
|
|
260
|
+
if (group.length === 0) continue
|
|
261
|
+
|
|
262
|
+
let currentX = 0
|
|
263
|
+
let maxH = 0
|
|
264
|
+
|
|
265
|
+
for (const id of group) {
|
|
266
|
+
const cls = classById.get(id)!
|
|
267
|
+
const w = classBoxW.get(id)!
|
|
268
|
+
const h = classBoxH.get(id)!
|
|
269
|
+
placed.set(id, {
|
|
270
|
+
cls,
|
|
271
|
+
sections: classSections.get(id)!,
|
|
272
|
+
x: currentX,
|
|
273
|
+
y: currentY,
|
|
274
|
+
width: w,
|
|
275
|
+
height: h,
|
|
276
|
+
})
|
|
277
|
+
currentX += w + hGap
|
|
278
|
+
maxH = Math.max(maxH, h)
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
currentY += maxH + vGap
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// --- Create canvas ---
|
|
285
|
+
let totalW = 0
|
|
286
|
+
let totalH = 0
|
|
287
|
+
for (const p of placed.values()) {
|
|
288
|
+
totalW = Math.max(totalW, p.x + p.width)
|
|
289
|
+
totalH = Math.max(totalH, p.y + p.height)
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// Extra space for relationship lines that may go below/beside
|
|
293
|
+
totalW += 4
|
|
294
|
+
totalH += 2
|
|
295
|
+
|
|
296
|
+
const canvas = mkCanvas(totalW - 1, totalH - 1)
|
|
297
|
+
const rc = mkRoleCanvas(totalW - 1, totalH - 1)
|
|
298
|
+
|
|
299
|
+
/** Set a character on the canvas and track its role. */
|
|
300
|
+
function setC(x: number, y: number, ch: string, role: CharRole): void {
|
|
301
|
+
if (x >= 0 && x < canvas.length && y >= 0 && y < (canvas[0]?.length ?? 0)) {
|
|
302
|
+
canvas[x]![y] = ch
|
|
303
|
+
setRole(rc, x, y, role)
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// --- Draw class boxes ---
|
|
308
|
+
for (const p of placed.values()) {
|
|
309
|
+
const boxCanvas = drawMultiBox(p.sections, useAscii)
|
|
310
|
+
// Copy box onto main canvas at (p.x, p.y) with role tracking
|
|
311
|
+
for (let bx = 0; bx < boxCanvas.length; bx++) {
|
|
312
|
+
for (let by = 0; by < boxCanvas[0]!.length; by++) {
|
|
313
|
+
const ch = boxCanvas[bx]![by]!
|
|
314
|
+
if (ch !== ' ') {
|
|
315
|
+
const cx = p.x + bx
|
|
316
|
+
const cy = p.y + by
|
|
317
|
+
if (cx < totalW && cy < totalH) {
|
|
318
|
+
setC(cx, cy, ch, classifyBoxChar(ch))
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// --- Build occupancy map for collision avoidance ---
|
|
326
|
+
// Track which x positions are occupied at each y level (to avoid routing through boxes)
|
|
327
|
+
const boxOccupancy: { x1: number; x2: number; y1: number; y2: number }[] = []
|
|
328
|
+
for (const p of placed.values()) {
|
|
329
|
+
boxOccupancy.push({
|
|
330
|
+
x1: p.x,
|
|
331
|
+
x2: p.x + p.width - 1,
|
|
332
|
+
y1: p.y,
|
|
333
|
+
y2: p.y + p.height - 1,
|
|
334
|
+
})
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/** Check if a point (x, y) is inside any class box */
|
|
338
|
+
function isInsideBox(x: number, y: number, excludeIds?: Set<string>): boolean {
|
|
339
|
+
for (const [id, p] of placed.entries()) {
|
|
340
|
+
if (excludeIds?.has(id)) continue
|
|
341
|
+
if (x >= p.x && x <= p.x + p.width - 1 && y >= p.y && y <= p.y + p.height - 1) {
|
|
342
|
+
return true
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
return false
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** Find a clear vertical column for routing that doesn't pass through any boxes */
|
|
349
|
+
function findClearColumn(startX: number, y1: number, y2: number, excludeIds: Set<string>): number {
|
|
350
|
+
// Try the original column first
|
|
351
|
+
let clear = true
|
|
352
|
+
for (let y = Math.min(y1, y2); y <= Math.max(y1, y2); y++) {
|
|
353
|
+
if (isInsideBox(startX, y, excludeIds)) {
|
|
354
|
+
clear = false
|
|
355
|
+
break
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
if (clear) return startX
|
|
359
|
+
|
|
360
|
+
// Try columns to the left and right, alternating
|
|
361
|
+
for (let offset = 1; offset < totalW + 10; offset++) {
|
|
362
|
+
// Try right
|
|
363
|
+
const rightX = startX + offset
|
|
364
|
+
clear = true
|
|
365
|
+
for (let y = Math.min(y1, y2); y <= Math.max(y1, y2); y++) {
|
|
366
|
+
if (isInsideBox(rightX, y, excludeIds)) {
|
|
367
|
+
clear = false
|
|
368
|
+
break
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
if (clear) return rightX
|
|
372
|
+
|
|
373
|
+
// Try left
|
|
374
|
+
const leftX = startX - offset
|
|
375
|
+
if (leftX >= 0) {
|
|
376
|
+
clear = true
|
|
377
|
+
for (let y = Math.min(y1, y2); y <= Math.max(y1, y2); y++) {
|
|
378
|
+
if (isInsideBox(leftX, y, excludeIds)) {
|
|
379
|
+
clear = false
|
|
380
|
+
break
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
if (clear) return leftX
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// Fallback to right edge of canvas + some extra space
|
|
388
|
+
return totalW + 2
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
// --- Draw relationship lines ---
|
|
392
|
+
const H = useAscii ? '-' : '─'
|
|
393
|
+
const V = useAscii ? '|' : '│'
|
|
394
|
+
const dashH = useAscii ? '.' : '╌'
|
|
395
|
+
const dashV = useAscii ? ':' : '┊'
|
|
396
|
+
|
|
397
|
+
for (const rel of diagram.relationships) {
|
|
398
|
+
const fromP = placed.get(rel.from)
|
|
399
|
+
const toP = placed.get(rel.to)
|
|
400
|
+
if (!fromP || !toP) continue
|
|
401
|
+
|
|
402
|
+
const marker = getRelMarker(rel.type, rel.markerAt)
|
|
403
|
+
const lineH = marker.dashed ? dashH : H
|
|
404
|
+
const lineV = marker.dashed ? dashV : V
|
|
405
|
+
|
|
406
|
+
// Exclude source and target boxes from collision detection
|
|
407
|
+
const excludeIds = new Set([rel.from, rel.to])
|
|
408
|
+
|
|
409
|
+
// Connection points: center-bottom of source → center-top of target
|
|
410
|
+
const fromCX = fromP.x + Math.floor(fromP.width / 2)
|
|
411
|
+
const fromBY = fromP.y + fromP.height - 1
|
|
412
|
+
const toCX = toP.x + Math.floor(toP.width / 2)
|
|
413
|
+
const toTY = toP.y
|
|
414
|
+
|
|
415
|
+
// Route: Manhattan routing with collision avoidance
|
|
416
|
+
// If target is below source: vertical down from source, horizontal if needed, vertical down to target
|
|
417
|
+
// If same row: horizontal line with a small vertical detour above or below
|
|
418
|
+
if (fromBY < toTY) {
|
|
419
|
+
// Target is below source — routing with collision avoidance
|
|
420
|
+
// Find a clear vertical column for the ENTIRE path from source to target
|
|
421
|
+
const routeX = findClearColumn(fromCX, fromBY + 1, toTY - 1, excludeIds)
|
|
422
|
+
const needsDetour = routeX !== fromCX
|
|
423
|
+
|
|
424
|
+
// Expand canvas if needed to accommodate routing column
|
|
425
|
+
if (routeX >= totalW) {
|
|
426
|
+
increaseSize(canvas, routeX + 2, totalH)
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
if (needsDetour) {
|
|
430
|
+
// COLLISION CASE: Route around intermediate boxes
|
|
431
|
+
// Path: source center → horizontal to routeX → vertical to entry → horizontal to target center
|
|
432
|
+
|
|
433
|
+
const exitY = fromBY + 1
|
|
434
|
+
const entryY = toTY - 1
|
|
435
|
+
|
|
436
|
+
// 1. Horizontal from source center to route column
|
|
437
|
+
const lx1 = Math.min(fromCX, routeX)
|
|
438
|
+
const rx1 = Math.max(fromCX, routeX)
|
|
439
|
+
for (let x = lx1; x <= rx1; x++) {
|
|
440
|
+
setC(x, exitY, lineH, 'line')
|
|
441
|
+
}
|
|
442
|
+
if (!useAscii && exitY < (canvas[0]?.length ?? 0)) {
|
|
443
|
+
if (fromCX < routeX) {
|
|
444
|
+
setC(fromCX, exitY, '└', 'corner')
|
|
445
|
+
setC(routeX, exitY, '┐', 'corner')
|
|
446
|
+
} else {
|
|
447
|
+
setC(fromCX, exitY, '┘', 'corner')
|
|
448
|
+
setC(routeX, exitY, '┌', 'corner')
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
// 2. Vertical at routeX from exit to entry
|
|
453
|
+
for (let y = exitY + 1; y <= entryY; y++) {
|
|
454
|
+
setC(routeX, y, lineV, 'line')
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
// 3. Horizontal from routeX to target center at entry
|
|
458
|
+
if (routeX !== toCX) {
|
|
459
|
+
const lx2 = Math.min(routeX, toCX)
|
|
460
|
+
const rx2 = Math.max(routeX, toCX)
|
|
461
|
+
for (let x = lx2; x <= rx2; x++) {
|
|
462
|
+
setC(x, entryY, lineH, 'line')
|
|
463
|
+
}
|
|
464
|
+
if (!useAscii && entryY < (canvas[0]?.length ?? 0)) {
|
|
465
|
+
if (routeX < toCX) {
|
|
466
|
+
setC(routeX, entryY, '└', 'corner')
|
|
467
|
+
setC(toCX, entryY, '┐', 'corner')
|
|
468
|
+
} else {
|
|
469
|
+
setC(routeX, entryY, '┘', 'corner')
|
|
470
|
+
setC(toCX, entryY, '┌', 'corner')
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
// Markers for detour case
|
|
476
|
+
if (marker.markerAt === 'to') {
|
|
477
|
+
const markerChar = getMarkerShape(marker.type, useAscii, 'down')
|
|
478
|
+
setC(toCX, entryY, markerChar, 'arrow')
|
|
479
|
+
}
|
|
480
|
+
if (marker.markerAt === 'from') {
|
|
481
|
+
const markerChar = getMarkerShape(marker.type, useAscii, 'down')
|
|
482
|
+
setC(fromCX, fromBY + 1, markerChar, 'arrow')
|
|
483
|
+
}
|
|
484
|
+
} else {
|
|
485
|
+
// NO COLLISION CASE: Use original midpoint-based routing
|
|
486
|
+
// Path: source center → vertical to midY → horizontal at midY → vertical to target
|
|
487
|
+
|
|
488
|
+
const midY = fromBY + Math.floor((toTY - fromBY) / 2)
|
|
489
|
+
|
|
490
|
+
// 1. Vertical from source bottom to midY
|
|
491
|
+
for (let y = fromBY + 1; y <= midY; y++) {
|
|
492
|
+
setC(fromCX, y, lineV, 'line')
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
// 2. Horizontal from fromCX to toCX at midY (if needed)
|
|
496
|
+
if (fromCX !== toCX && midY < (canvas[0]?.length ?? 0)) {
|
|
497
|
+
const lx = Math.min(fromCX, toCX)
|
|
498
|
+
const rx = Math.max(fromCX, toCX)
|
|
499
|
+
for (let x = lx; x <= rx; x++) {
|
|
500
|
+
setC(x, midY, lineH, 'line')
|
|
501
|
+
}
|
|
502
|
+
if (!useAscii) {
|
|
503
|
+
setC(fromCX, midY, fromCX < toCX ? '└' : '┘', 'corner')
|
|
504
|
+
setC(toCX, midY, fromCX < toCX ? '┐' : '┌', 'corner')
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
// 3. Vertical from midY to target top
|
|
509
|
+
for (let y = midY + 1; y < toTY; y++) {
|
|
510
|
+
setC(toCX, y, lineV, 'line')
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
// Markers for no-collision case
|
|
514
|
+
if (marker.markerAt === 'to') {
|
|
515
|
+
setC(toCX, toTY - 1, getMarkerShape(marker.type, useAscii, 'down'), 'arrow')
|
|
516
|
+
}
|
|
517
|
+
if (marker.markerAt === 'from') {
|
|
518
|
+
setC(fromCX, fromBY + 1, getMarkerShape(marker.type, useAscii, 'down'), 'arrow')
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
} else if (toP.y + toP.height - 1 < fromP.y) {
|
|
522
|
+
// Target is ABOVE source — draw upward from source top to target bottom
|
|
523
|
+
const fromTY = fromP.y
|
|
524
|
+
const toBY = toP.y + toP.height - 1
|
|
525
|
+
const midY = toBY + Math.floor((fromTY - toBY) / 2)
|
|
526
|
+
|
|
527
|
+
for (let y = fromTY - 1; y >= midY; y--) {
|
|
528
|
+
setC(fromCX, y, lineV, 'line')
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
if (fromCX !== toCX) {
|
|
532
|
+
const lx = Math.min(fromCX, toCX)
|
|
533
|
+
const rx = Math.max(fromCX, toCX)
|
|
534
|
+
for (let x = lx; x <= rx; x++) {
|
|
535
|
+
setC(x, midY, lineH, 'line')
|
|
536
|
+
}
|
|
537
|
+
if (!useAscii && midY >= 0 && midY < totalH) {
|
|
538
|
+
setC(fromCX, midY, fromCX < toCX ? '┌' : '┐', 'corner')
|
|
539
|
+
setC(toCX, midY, fromCX < toCX ? '┘' : '└', 'corner')
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
for (let y = midY - 1; y > toBY; y--) {
|
|
544
|
+
setC(toCX, y, lineV, 'line')
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
// Draw markers - arrows point in the direction of the vertical segment (upward)
|
|
548
|
+
if (marker.markerAt === 'from') {
|
|
549
|
+
const markerChar = getMarkerShape(marker.type, useAscii, 'up')
|
|
550
|
+
const my = fromTY - 1
|
|
551
|
+
for (let i = 0; i < markerChar.length; i++) {
|
|
552
|
+
setC(fromCX - Math.floor(markerChar.length / 2) + i, my, markerChar[i]!, 'arrow')
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
if (marker.markerAt === 'to') {
|
|
556
|
+
const isHierarchical = marker.type === 'inheritance' || marker.type === 'realization'
|
|
557
|
+
const markerDir = isHierarchical ? 'down' : 'up'
|
|
558
|
+
const markerChar = getMarkerShape(marker.type, useAscii, markerDir)
|
|
559
|
+
const my = toBY + 1
|
|
560
|
+
for (let i = 0; i < markerChar.length; i++) {
|
|
561
|
+
setC(toCX - Math.floor(markerChar.length / 2) + i, my, markerChar[i]!, 'arrow')
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
} else {
|
|
565
|
+
// Same level — draw horizontal line with a detour below both boxes
|
|
566
|
+
const detourY = Math.max(fromBY, toP.y + toP.height - 1) + 2
|
|
567
|
+
increaseSize(canvas, totalW, detourY + 1)
|
|
568
|
+
increaseRoleCanvasSize(rc, totalW, detourY + 1)
|
|
569
|
+
|
|
570
|
+
// Vertical down from source
|
|
571
|
+
for (let y = fromBY + 1; y <= detourY; y++) {
|
|
572
|
+
setC(fromCX, y, lineV, 'line')
|
|
573
|
+
}
|
|
574
|
+
// Horizontal
|
|
575
|
+
const lx = Math.min(fromCX, toCX)
|
|
576
|
+
const rx = Math.max(fromCX, toCX)
|
|
577
|
+
for (let x = lx; x <= rx; x++) {
|
|
578
|
+
setC(x, detourY, lineH, 'line')
|
|
579
|
+
}
|
|
580
|
+
// Vertical up to target
|
|
581
|
+
for (let y = detourY - 1; y >= toP.y + toP.height; y--) {
|
|
582
|
+
setC(toCX, y, lineV, 'line')
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
// Draw markers - same-level routing uses vertical segments at both ends
|
|
586
|
+
if (marker.markerAt === 'from') {
|
|
587
|
+
const markerChar = getMarkerShape(marker.type, useAscii, 'down')
|
|
588
|
+
const my = fromBY + 1
|
|
589
|
+
for (let i = 0; i < markerChar.length; i++) {
|
|
590
|
+
setC(fromCX - Math.floor(markerChar.length / 2) + i, my, markerChar[i]!, 'arrow')
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
if (marker.markerAt === 'to') {
|
|
594
|
+
const markerChar = getMarkerShape(marker.type, useAscii, 'up')
|
|
595
|
+
const my = toP.y + toP.height
|
|
596
|
+
for (let i = 0; i < markerChar.length; i++) {
|
|
597
|
+
setC(toCX - Math.floor(markerChar.length / 2) + i, my, markerChar[i]!, 'arrow')
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
// Draw relationship label at midpoint if present (supports multi-line)
|
|
603
|
+
// Add padding around the label for readability
|
|
604
|
+
if (rel.label) {
|
|
605
|
+
const lines = splitLines(rel.label)
|
|
606
|
+
const maxLabelWidth = Math.max(...lines.map(l => displayWidth(l))) + 2 // +2 for padding
|
|
607
|
+
|
|
608
|
+
// Calculate ideal label position based on routing direction
|
|
609
|
+
let baseMidY: number
|
|
610
|
+
let idealMidX: number
|
|
611
|
+
|
|
612
|
+
if (fromBY < toTY) {
|
|
613
|
+
// Target below source: place in gap between source bottom and target top
|
|
614
|
+
baseMidY = Math.floor((fromBY + 1 + toTY - 1) / 2)
|
|
615
|
+
idealMidX = Math.floor((fromCX + toCX) / 2)
|
|
616
|
+
} else if (toP.y + toP.height - 1 < fromP.y) {
|
|
617
|
+
// Target above source: place in gap between target bottom and source top
|
|
618
|
+
const toBY = toP.y + toP.height - 1
|
|
619
|
+
baseMidY = Math.floor((toBY + 1 + fromP.y - 1) / 2)
|
|
620
|
+
idealMidX = Math.floor((fromCX + toCX) / 2)
|
|
621
|
+
} else {
|
|
622
|
+
// Same level: place label at midpoint of the detour line
|
|
623
|
+
baseMidY = Math.max(fromBY, toP.y + toP.height - 1) + 2
|
|
624
|
+
idealMidX = Math.floor((fromCX + toCX) / 2)
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
// Find a clear vertical position for the label (not inside any box)
|
|
628
|
+
let labelY = baseMidY
|
|
629
|
+
const halfHeight = Math.floor(lines.length / 2)
|
|
630
|
+
|
|
631
|
+
// Check if any label line would be inside a box
|
|
632
|
+
let labelInBox = false
|
|
633
|
+
for (let i = 0; i < lines.length; i++) {
|
|
634
|
+
const y = labelY - halfHeight + i
|
|
635
|
+
const idealLabelStart = idealMidX - Math.floor(maxLabelWidth / 2)
|
|
636
|
+
const labelStart = Math.max(0, idealLabelStart)
|
|
637
|
+
// Check if this line overlaps any box
|
|
638
|
+
for (let x = labelStart; x < labelStart + maxLabelWidth; x++) {
|
|
639
|
+
if (isInsideBox(x, y, excludeIds)) {
|
|
640
|
+
labelInBox = true
|
|
641
|
+
break
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
if (labelInBox) break
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
// If label is inside a box, find the gap between boxes
|
|
648
|
+
if (labelInBox) {
|
|
649
|
+
// Find the gap between source and target boxes
|
|
650
|
+
const gapTop = fromBY + 1
|
|
651
|
+
const gapBottom = toTY - 1
|
|
652
|
+
|
|
653
|
+
// Place label in the middle of the gap, outside any intermediate box
|
|
654
|
+
for (let y = gapTop; y <= gapBottom; y++) {
|
|
655
|
+
let clearRow = true
|
|
656
|
+
const idealLabelStart = idealMidX - Math.floor(maxLabelWidth / 2)
|
|
657
|
+
const labelStart = Math.max(0, idealLabelStart)
|
|
658
|
+
for (let x = labelStart; x < labelStart + maxLabelWidth; x++) {
|
|
659
|
+
if (isInsideBox(x, y, excludeIds)) {
|
|
660
|
+
clearRow = false
|
|
661
|
+
break
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
if (clearRow) {
|
|
665
|
+
labelY = y
|
|
666
|
+
break
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
// Center lines vertically around labelY
|
|
672
|
+
const startY = labelY - halfHeight
|
|
673
|
+
|
|
674
|
+
for (let lineIdx = 0; lineIdx < lines.length; lineIdx++) {
|
|
675
|
+
const paddedLine = ` ${lines[lineIdx]!} ` // Add space padding on both sides
|
|
676
|
+
const cells = toCells(paddedLine)
|
|
677
|
+
// Calculate label start, but ensure it doesn't go negative
|
|
678
|
+
const idealLabelStart = idealMidX - Math.floor(cells.length / 2)
|
|
679
|
+
const labelStart = Math.max(0, idealLabelStart)
|
|
680
|
+
const y = startY + lineIdx
|
|
681
|
+
// Ensure canvas is wide enough for the label
|
|
682
|
+
const labelEnd = labelStart + cells.length
|
|
683
|
+
if (labelEnd > 0 && y >= 0) {
|
|
684
|
+
increaseSize(canvas, Math.max(labelEnd, 1), Math.max(y + 1, 1))
|
|
685
|
+
increaseRoleCanvasSize(rc, Math.max(labelEnd, 1), Math.max(y + 1, 1))
|
|
686
|
+
}
|
|
687
|
+
// Clear the area first (overwrite line characters) then draw the padded label
|
|
688
|
+
for (let i = 0; i < cells.length; i++) {
|
|
689
|
+
const lx = labelStart + i
|
|
690
|
+
if (lx >= 0 && y >= 0) {
|
|
691
|
+
setC(lx, y, cells[i]!, 'text')
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
return canvasToString(canvas, { roleCanvas: rc, colorMode, theme })
|
|
699
|
+
}
|