@chengchenccc/tui 0.1.1-rc.1

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