@node-projects/web-component-designer-mermaid 0.1.0 → 0.1.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 (47) hide show
  1. package/{src/index.ts → dist/index.d.ts} +0 -3
  2. package/dist/index.js +17 -0
  3. package/dist/monaco/MermaidLanguage.d.ts +1 -0
  4. package/{src/monaco/MermaidLanguage.ts → dist/monaco/MermaidLanguage.js} +1 -4
  5. package/dist/services/MermaidConnectionRouting.d.ts +2 -0
  6. package/{src/services/MermaidConnectionRouting.ts → dist/services/MermaidConnectionRouting.js} +22 -45
  7. package/dist/services/MermaidDocumentPropertiesService.d.ts +24 -0
  8. package/{src/services/MermaidDocumentPropertiesService.ts → dist/services/MermaidDocumentPropertiesService.js} +25 -54
  9. package/dist/services/MermaidElementsService.d.ts +19 -0
  10. package/{src/services/MermaidElementsService.ts → dist/services/MermaidElementsService.js} +18 -35
  11. package/dist/services/MermaidLayoutCopyPasteService.d.ts +5 -0
  12. package/{src/services/MermaidLayoutCopyPasteService.ts → dist/services/MermaidLayoutCopyPasteService.js} +4 -6
  13. package/dist/services/MermaidLayoutPlacementService.d.ts +8 -0
  14. package/{src/services/MermaidLayoutPlacementService.ts → dist/services/MermaidLayoutPlacementService.js} +6 -11
  15. package/dist/services/MermaidParserService.d.ts +7 -0
  16. package/{src/services/MermaidParserService.ts → dist/services/MermaidParserService.js} +410 -705
  17. package/dist/services/MermaidPropertyGroupsService.d.ts +8 -0
  18. package/{src/services/MermaidPropertyGroupsService.ts → dist/services/MermaidPropertyGroupsService.js} +2 -7
  19. package/dist/services/mermaidGeometry.d.ts +31 -0
  20. package/{src/services/mermaidGeometry.ts → dist/services/mermaidGeometry.js} +13 -43
  21. package/dist/setupMermaidServiceContainer.d.ts +2 -0
  22. package/{src/setupMermaidServiceContainer.ts → dist/setupMermaidServiceContainer.js} +8 -28
  23. package/dist/toolbar/ConnectMermaidNodesTool.d.ts +20 -0
  24. package/{src/toolbar/ConnectMermaidNodesTool.ts → dist/toolbar/ConnectMermaidNodesTool.js} +26 -58
  25. package/dist/widgets/mermaid-edge.d.ts +45 -0
  26. package/{src/widgets/mermaid-edge.ts → dist/widgets/mermaid-edge.js} +39 -60
  27. package/dist/widgets/mermaid-flowchart-directive.d.ts +16 -0
  28. package/{src/widgets/mermaid-flowchart-directive.ts → dist/widgets/mermaid-flowchart-directive.js} +11 -23
  29. package/dist/widgets/mermaid-mindmap-node.d.ts +37 -0
  30. package/{src/widgets/mermaid-mindmap-node.ts → dist/widgets/mermaid-mindmap-node.js} +36 -55
  31. package/dist/widgets/mermaid-node.d.ts +75 -0
  32. package/{src/widgets/mermaid-node.ts → dist/widgets/mermaid-node.js} +78 -105
  33. package/dist/widgets/mermaid-requirement-node.d.ts +37 -0
  34. package/{src/widgets/mermaid-requirement-node.ts → dist/widgets/mermaid-requirement-node.js} +30 -49
  35. package/dist/widgets/mermaid-requirement-relationship.d.ts +28 -0
  36. package/{src/widgets/mermaid-requirement-relationship.ts → dist/widgets/mermaid-requirement-relationship.js} +21 -38
  37. package/dist/widgets/mermaid-sequence-message.d.ts +34 -0
  38. package/{src/widgets/mermaid-sequence-message.ts → dist/widgets/mermaid-sequence-message.js} +30 -44
  39. package/dist/widgets/mermaid-sequence-participant.d.ts +24 -0
  40. package/{src/widgets/mermaid-sequence-participant.ts → dist/widgets/mermaid-sequence-participant.js} +17 -28
  41. package/dist/widgets/mermaid-subgraph.d.ts +20 -0
  42. package/{src/widgets/mermaid-subgraph.ts → dist/widgets/mermaid-subgraph.js} +13 -25
  43. package/dist/widgets/views/mermaid-demo-view.d.ts +11 -0
  44. package/{src/widgets/views/mermaid-demo-view.ts → dist/widgets/views/mermaid-demo-view.js} +8 -18
  45. package/package.json +6 -1
  46. package/tsconfig.json +0 -20
  47. /package/{src → dist}/widgets/elements.json +0 -0
@@ -1,4 +1,4 @@
1
- import { DesignItem, IDesignItem, IHtmlParserService, IHtmlWriterOptions, IHtmlWriterService, InstanceServiceContainer, ITextWriter, ServiceContainer } from "@node-projects/web-component-designer";
1
+ import { DesignItem } from "@node-projects/web-component-designer";
2
2
  import { MermaidEdge } from "../widgets/mermaid-edge.js";
3
3
  import { MermaidNode, normalizeLabelLineBreaks } from "../widgets/mermaid-node.js";
4
4
  import { MermaidSequenceMessage } from "../widgets/mermaid-sequence-message.js";
@@ -8,111 +8,12 @@ import { MermaidRequirementNode } from "../widgets/mermaid-requirement-node.js";
8
8
  import { MermaidRequirementRelationship } from "../widgets/mermaid-requirement-relationship.js";
9
9
  import { MermaidSubgraph } from "../widgets/mermaid-subgraph.js";
10
10
  import { MermaidFlowchartDirective } from "../widgets/mermaid-flowchart-directive.js";
11
- import { getMermaidDocumentDiagramType, getMermaidDocumentFrontmatter, getMermaidDocumentTitle, mermaidDiagramTypeAttribute, mermaidFlowchartDirectionAttribute, mermaidFrontmatterAttribute, mermaidTitleAttribute, MermaidDocumentDiagramType } from "./MermaidDocumentPropertiesService.js";
12
- import { encodeWaypoints, FlowchartDirection, routeBetweenBounds } from "./mermaidGeometry.js";
13
-
14
- type ParsedNode = {
15
- id: string;
16
- label: string;
17
- shape: string;
18
- parentId?: string;
19
- classes?: string;
20
- style?: string;
21
- sourceRange?: { start: number; length: number };
22
- }
23
-
24
- type ParsedEdge = {
25
- from: string;
26
- to: string;
27
- fromNode?: ParsedNode;
28
- toNode?: ParsedNode;
29
- label: string;
30
- edgeType: string;
31
- connector?: string;
32
- edgeId?: string;
33
- animation?: string;
34
- connectorRange?: { start: number; length: number };
35
- sourceRange?: { start: number; length: number };
36
- }
37
-
38
- type ParsedSubgraph = {
39
- id: string;
40
- title: string;
41
- parentId?: string;
42
- direction?: FlowchartDirection;
43
- sourceRange?: { start: number; length: number };
44
- }
45
-
46
- type ParsedFlowchartDirective = {
47
- line: string;
48
- sourceRange?: { start: number; length: number };
49
- }
50
-
51
- type ParsedSequenceParticipant = {
52
- id: string;
53
- label: string;
54
- participantType: string;
55
- implicit?: boolean;
56
- sourceRange?: { start: number; length: number };
57
- }
58
-
59
- type ParsedSequenceMessage = {
60
- from: string;
61
- to: string;
62
- label: string;
63
- messageType: string;
64
- connector: string;
65
- sourceRange?: { start: number; length: number };
66
- }
67
-
68
- type ParsedMindmapNode = {
69
- id: string;
70
- parentId: string;
71
- label: string;
72
- shape: string;
73
- indent: number;
74
- sourceRange?: { start: number; length: number };
75
- }
76
-
77
- type ParsedRequirementNode = {
78
- id: string;
79
- kind: "requirement" | "element";
80
- requirementType?: string;
81
- requirementId?: string;
82
- text?: string;
83
- risk?: string;
84
- verifyMethod?: string;
85
- elementType?: string;
86
- docRef?: string;
87
- sourceRange?: { start: number; length: number };
88
- }
89
-
90
- type ParsedRequirementRelationship = {
91
- from: string;
92
- to: string;
93
- relationshipType: string;
94
- syntaxDirection: "forward" | "reverse";
95
- sourceRange?: { start: number; length: number };
96
- }
97
-
98
- type LineRecord = {
99
- text: string;
100
- trimmed: string;
101
- indent: number;
102
- start: number;
103
- }
104
-
105
- type ParsedMermaidDocument = {
106
- lines: LineRecord[];
107
- title: string;
108
- frontmatter: string;
109
- }
110
-
111
- export class MermaidParserService implements IHtmlParserService, IHtmlWriterService {
112
- options: IHtmlWriterOptions = {};
11
+ import { getMermaidDocumentDiagramType, getMermaidDocumentFrontmatter, getMermaidDocumentTitle, mermaidDiagramTypeAttribute, mermaidFlowchartDirectionAttribute, mermaidFrontmatterAttribute, mermaidTitleAttribute } from "./MermaidDocumentPropertiesService.js";
12
+ import { encodeWaypoints, routeBetweenBounds } from "./mermaidGeometry.js";
13
+ export class MermaidParserService {
14
+ options = {};
113
15
  supportsRootItemWrite = true;
114
-
115
- async parse(html: string, serviceContainer: ServiceContainer, instanceServiceContainer: InstanceServiceContainer, parseSnippet: boolean): Promise<IDesignItem[]> {
16
+ async parse(html, serviceContainer, instanceServiceContainer, parseSnippet) {
116
17
  const diagramKind = getDiagramKind(readMermaidDocument(html));
117
18
  if (diagramKind === "sequence")
118
19
  return parseSequenceDiagram(html, serviceContainer, instanceServiceContainer);
@@ -122,8 +23,7 @@ export class MermaidParserService implements IHtmlParserService, IHtmlWriterServ
122
23
  return parseRequirementDiagram(html, serviceContainer, instanceServiceContainer);
123
24
  return parseFlowchart(html, serviceContainer, instanceServiceContainer);
124
25
  }
125
-
126
- write(textWriter: ITextWriter, designItems: IDesignItem[], rootContainerKeepInline: boolean, updatePositions?: boolean) {
26
+ write(textWriter, designItems, rootContainerKeepInline, updatePositions) {
127
27
  const rootDesignItem = getRootDesignItem(designItems);
128
28
  const mermaidDesignItems = getMermaidDesignItems(designItems);
129
29
  const documentType = getWritableDocumentDiagramType(mermaidDesignItems.length ? mermaidDesignItems : designItems);
@@ -139,12 +39,10 @@ export class MermaidParserService implements IHtmlParserService, IHtmlWriterServ
139
39
  writeRequirementDiagram(textWriter, mermaidDesignItems, updatePositions, rootDesignItem);
140
40
  return;
141
41
  }
142
-
143
42
  writeFlowchart(textWriter, mermaidDesignItems, updatePositions, rootDesignItem);
144
43
  }
145
44
  }
146
-
147
- function getDiagramKind(document: ParsedMermaidDocument) {
45
+ function getDiagramKind(document) {
148
46
  const firstMeaningfulLine = document.lines.find(line => line.trimmed && !line.trimmed.startsWith("%%"))?.trimmed ?? "";
149
47
  if (equalsIgnoreCase(firstMeaningfulLine, "sequenceDiagram"))
150
48
  return "sequence";
@@ -154,272 +52,253 @@ function getDiagramKind(document: ParsedMermaidDocument) {
154
52
  return "requirementDiagram";
155
53
  return "flowchart";
156
54
  }
157
-
158
- async function parseFlowchart(html: string, serviceContainer: ServiceContainer, instanceServiceContainer: InstanceServiceContainer): Promise<IDesignItem[]> {
159
- const document = readMermaidDocument(html);
160
- const lines = document.lines;
161
- const designItems: IDesignItem[] = [];
162
- const nodes = new Map<string, ParsedNode>();
163
- const nodeBounds = new Map<string, { x: number; y: number; width: number; height: number }>();
164
- const edges: ParsedEdge[] = [];
165
- const subgraphs = new Map<string, ParsedSubgraph>();
166
- const directives: ParsedFlowchartDirective[] = [];
167
- const subgraphStack: ParsedSubgraph[] = [];
168
- let direction: FlowchartDirection = "TD";
169
- const scannedNodes = new Map<string, ParsedNode>();
170
- const scannedEdges: ParsedEdge[] = [];
171
-
172
- for (const line of lines) {
173
- if (!line.trimmed || line.trimmed.startsWith("%%"))
174
- continue;
175
-
176
- const subgraph = parseSubgraphStart(line.trimmed, line.start, subgraphStack[subgraphStack.length - 1]?.id);
177
- if (subgraph) {
178
- subgraphs.set(subgraph.id, subgraph);
179
- subgraphStack.push(subgraph);
180
- continue;
181
- }
182
-
183
- if (equalsIgnoreCase(line.trimmed, "end")) {
184
- subgraphStack.pop();
185
- continue;
186
- }
187
-
188
- const directive = parseFlowchartDirective(line.trimmed, line.start);
189
- if (directive) {
190
- directives.push(directive);
191
- continue;
192
- }
193
-
194
- const parsedDirection = parseFlowchartDirection(line.trimmed);
195
- if (parsedDirection) {
196
- if (subgraphStack.length)
197
- subgraphStack[subgraphStack.length - 1].direction = parsedDirection;
198
- else
199
- direction = parsedDirection;
200
- continue;
201
- }
202
- const parsedSubgraphDirection = parseDirectionStatement(line.trimmed);
203
- if (parsedSubgraphDirection && subgraphStack.length) {
204
- subgraphStack[subgraphStack.length - 1].direction = parsedSubgraphDirection;
205
- continue;
206
- }
207
-
208
- const edge = parseEdge(line.trimmed, line.start);
209
- if (edge) {
210
- scannedEdges.push(edge);
211
- mergeNode(scannedNodes, edge.fromNode);
212
- mergeNode(scannedNodes, edge.toNode);
213
- continue;
214
- }
215
-
216
- const node = parseNode(line.trimmed, line.start);
217
- if (node) {
218
- node.parentId = subgraphStack[subgraphStack.length - 1]?.id;
219
- mergeNode(scannedNodes, node);
220
- }
55
+ async function parseFlowchart(html, serviceContainer, instanceServiceContainer) {
56
+ const document = readMermaidDocument(html);
57
+ const lines = document.lines;
58
+ const designItems = [];
59
+ const nodes = new Map();
60
+ const nodeBounds = new Map();
61
+ const edges = [];
62
+ const subgraphs = new Map();
63
+ const directives = [];
64
+ const subgraphStack = [];
65
+ let direction = "TD";
66
+ const scannedNodes = new Map();
67
+ const scannedEdges = [];
68
+ for (const line of lines) {
69
+ if (!line.trimmed || line.trimmed.startsWith("%%"))
70
+ continue;
71
+ const subgraph = parseSubgraphStart(line.trimmed, line.start, subgraphStack[subgraphStack.length - 1]?.id);
72
+ if (subgraph) {
73
+ subgraphs.set(subgraph.id, subgraph);
74
+ subgraphStack.push(subgraph);
75
+ continue;
221
76
  }
222
-
223
- setMermaidDocumentAttributes(instanceServiceContainer, "flowchart", direction, document.title, document.frontmatter);
224
- const parsed = await getMermaidFlowchartData(html);
225
- if (parsed) {
226
- for (const node of parsed.nodes) {
227
- const scanned = scannedNodes.get(node.id);
228
- mergeNode(nodes, { ...node, parentId: scanned?.parentId, classes: scanned?.classes, style: scanned?.style, sourceRange: scanned?.sourceRange });
229
- }
230
- for (const edge of parsed.edges) {
231
- const scanned = getScannedEdge(scannedEdges, edge);
232
- edges.push({ ...edge, ...scanned });
233
- }
234
- } else {
235
- for (const node of scannedNodes.values())
236
- mergeNode(nodes, node);
237
- edges.push(...scannedEdges);
77
+ if (equalsIgnoreCase(line.trimmed, "end")) {
78
+ subgraphStack.pop();
79
+ continue;
238
80
  }
239
-
240
- const mermaidLayout = await getMermaidLayout(html, nodes);
241
- const designItemsById = new Map<string, IDesignItem>();
242
- let subgraphIndex = 0;
243
- for (const subgraph of subgraphs.values()) {
244
- const layout = mermaidLayout?.subgraphs?.get(subgraph.id);
245
- const element = new MermaidSubgraph();
246
- element.style.position = "absolute";
247
- element.style.left = (layout?.x ?? (30 + subgraphIndex * 40)) + "px";
248
- element.style.top = (layout?.y ?? (30 + subgraphIndex * 40)) + "px";
249
- element.style.width = (layout?.width ?? 360) + "px";
250
- element.style.height = (layout?.height ?? 240) + "px";
251
- element.setAttribute("subgraph-id", subgraph.id);
252
- element.setAttribute("title", subgraph.title);
253
- if (subgraph.direction)
254
- element.setAttribute("direction", subgraph.direction);
255
- const designItem = DesignItem.createDesignItemFromInstance(element, serviceContainer, instanceServiceContainer);
256
- designItemsById.set(subgraph.id, designItem);
257
- const parentItem = designItemsById.get(subgraph.parentId);
258
- if (parentItem)
259
- parentItem._insertChildInternal(designItem);
260
- else
261
- designItems.push(designItem);
262
- if (subgraph.sourceRange)
263
- instanceServiceContainer.designItemDocumentPositionService?.setPosition(designItem, subgraph.sourceRange);
264
- subgraphIndex++;
81
+ const directive = parseFlowchartDirective(line.trimmed, line.start);
82
+ if (directive) {
83
+ directives.push(directive);
84
+ continue;
265
85
  }
266
-
267
- let index = 0;
268
- for (const node of nodes.values()) {
269
- const layout = mermaidLayout?.nodes.get(node.id);
270
- const parentLayout = node.parentId ? mermaidLayout?.subgraphs?.get(node.parentId) : null;
271
- const fallbackPosition = getFallbackFlowchartNodePosition(index, nodes.size, direction);
272
- const left = layout ? layout.x - (parentLayout?.x ?? 0) : fallbackPosition.left;
273
- const top = layout ? layout.y - (parentLayout?.y ?? 0) : fallbackPosition.top;
274
- const width = layout?.width ?? getDefaultNodeSize(node.shape).width;
275
- const height = layout?.height ?? getDefaultNodeSize(node.shape).height;
276
- const element = new MermaidNode();
277
- element.style.position = "absolute";
278
- element.style.left = left + "px";
279
- element.style.top = top + "px";
280
- element.style.width = width + "px";
281
- element.style.height = height + "px";
282
- element.setAttribute("node-id", node.id);
283
- element.setAttribute("label", node.label);
284
- element.setAttribute("shape", node.shape);
285
- element.setAttribute("diagram-direction", direction);
286
- if (node.classes)
287
- element.setAttribute("classes", node.classes);
288
- if (node.style)
289
- element.setAttribute("style-directive", node.style);
290
- const designItem = DesignItem.createDesignItemFromInstance(element, serviceContainer, instanceServiceContainer);
291
- const parentItem = designItemsById.get(node.parentId);
292
- if (parentItem)
293
- parentItem._insertChildInternal(designItem);
86
+ const parsedDirection = parseFlowchartDirection(line.trimmed);
87
+ if (parsedDirection) {
88
+ if (subgraphStack.length)
89
+ subgraphStack[subgraphStack.length - 1].direction = parsedDirection;
294
90
  else
295
- designItems.push(designItem);
296
- designItemsById.set(node.id, designItem);
297
- if (node.sourceRange)
298
- instanceServiceContainer.designItemDocumentPositionService?.setPosition(designItem, node.sourceRange);
299
- nodeBounds.set(node.id, { x: layout?.x ?? left, y: layout?.y ?? top, width, height });
300
- index++;
91
+ direction = parsedDirection;
92
+ continue;
301
93
  }
302
-
303
- let edgeIndex = 0;
304
- for (const edge of edges) {
305
- const element = new MermaidEdge();
306
- element.style.position = "absolute";
307
- element.style.left = "40px";
308
- element.style.top = 60 + index * 110 + edgeIndex * 44 + "px";
309
- element.style.width = "220px";
310
- element.style.height = "28px";
311
- element.setAttribute("from", edge.from);
312
- element.setAttribute("to", edge.to);
313
- element.setAttribute("label", edge.label);
314
- element.setAttribute("edge-type", edge.edgeType);
315
- if (edge.connector)
316
- element.setAttribute("connector", edge.connector);
317
- if (edge.edgeId)
318
- element.setAttribute("edge-id", edge.edgeId);
319
- if (edge.animation)
320
- element.setAttribute("animation", edge.animation);
321
- element.setAttribute("diagram-direction", direction);
322
- const sourceBounds = nodeBounds.get(edge.from);
323
- const targetBounds = nodeBounds.get(edge.to);
324
- if (sourceBounds && targetBounds)
325
- element.setAttribute("waypoints", encodeWaypoints(routeBetweenBounds(sourceBounds, targetBounds, direction)));
326
- const designItem = DesignItem.createDesignItemFromInstance(element, serviceContainer, instanceServiceContainer);
327
- designItems.push(designItem);
328
- if (edge.sourceRange)
329
- instanceServiceContainer.designItemDocumentPositionService?.setPosition(designItem, edge.connectorRange ?? edge.sourceRange);
330
- edgeIndex++;
94
+ const parsedSubgraphDirection = parseDirectionStatement(line.trimmed);
95
+ if (parsedSubgraphDirection && subgraphStack.length) {
96
+ subgraphStack[subgraphStack.length - 1].direction = parsedSubgraphDirection;
97
+ continue;
331
98
  }
332
-
333
- let directiveIndex = 0;
334
- for (const directive of directives) {
335
- const element = new MermaidFlowchartDirective();
336
- element.style.position = "absolute";
337
- element.style.left = "40px";
338
- element.style.top = 60 + index * 110 + edgeIndex * 44 + directiveIndex * 28 + "px";
339
- element.style.width = "320px";
340
- element.style.height = "24px";
341
- element.setAttribute("line", directive.line);
342
- const designItem = DesignItem.createDesignItemFromInstance(element, serviceContainer, instanceServiceContainer);
343
- designItems.push(designItem);
344
- if (directive.sourceRange)
345
- instanceServiceContainer.designItemDocumentPositionService?.setPosition(designItem, directive.sourceRange);
346
- directiveIndex++;
99
+ const edge = parseEdge(line.trimmed, line.start);
100
+ if (edge) {
101
+ scannedEdges.push(edge);
102
+ mergeNode(scannedNodes, edge.fromNode);
103
+ mergeNode(scannedNodes, edge.toNode);
104
+ continue;
347
105
  }
348
-
349
- return designItems;
350
- }
351
-
352
- function writeFlowchart(textWriter: ITextWriter, designItems: IDesignItem[], updatePositions?: boolean, rootDesignItem?: IDesignItem) {
353
- const rootDirection = rootDesignItem?.getAttribute(mermaidFlowchartDirectionAttribute);
354
- const direction = isFlowchartDirection(rootDirection) ? rootDirection : getDiagramDirection(designItems);
355
- writeMermaidFrontmatter(textWriter, designItems, rootDesignItem);
356
- textWriter.writeLine("flowchart " + direction);
357
- const allFlowchartItems = getAllFlowchartItems(designItems);
358
- const nodeIdsUsedByEdges = getFlowchartNodeIdsUsedByEdges(allFlowchartItems);
359
- const nodeItemsById = getFlowchartNodeItemsById(allFlowchartItems);
360
- const explicitlyWrittenNodeIds = new Set<string>();
361
-
362
- const writeNodeOrSubgraph = (designItem: IDesignItem, depth: number) => {
363
- const element = designItem.element as HTMLElement & { createMermaid?: () => string };
364
- if (element.nodeName === "MERMAID-SUBGRAPH") {
365
- const subgraphElement = element as HTMLElement & { createMermaidStart?: () => string };
366
- writeDesignItemLine(textWriter, designItem, repeatText(" ", depth) + subgraphElement.createMermaidStart(), updatePositions);
367
- const subgraphDirection = element.getAttribute("direction");
368
- if (isFlowchartDirection(subgraphDirection))
369
- textWriter.writeLine(repeatText(" ", depth + 1) + "direction " + subgraphDirection);
370
- for (const child of designItem.children())
371
- writeNodeOrSubgraph(child, depth + 1);
372
- textWriter.writeLine(repeatText(" ", depth) + "end");
373
- } else if (element.nodeName === "MERMAID-NODE" && element.createMermaid && shouldWriteFlowchartNode(element, nodeIdsUsedByEdges)) {
374
- writeDesignItemLine(textWriter, designItem, repeatText(" ", depth) + element.createMermaid(), updatePositions);
375
- explicitlyWrittenNodeIds.add(element.getAttribute("node-id"));
376
- }
377
- };
378
-
379
- for (const designItem of designItems) {
380
- writeNodeOrSubgraph(designItem, 1);
106
+ const node = parseNode(line.trimmed, line.start);
107
+ if (node) {
108
+ node.parentId = subgraphStack[subgraphStack.length - 1]?.id;
109
+ mergeNode(scannedNodes, node);
110
+ }
111
+ }
112
+ setMermaidDocumentAttributes(instanceServiceContainer, "flowchart", direction, document.title, document.frontmatter);
113
+ const parsed = await getMermaidFlowchartData(html);
114
+ if (parsed) {
115
+ for (const node of parsed.nodes) {
116
+ const scanned = scannedNodes.get(node.id);
117
+ mergeNode(nodes, { ...node, parentId: scanned?.parentId, classes: scanned?.classes, style: scanned?.style, sourceRange: scanned?.sourceRange });
381
118
  }
382
- for (const designItem of allFlowchartItems) {
383
- const element = designItem.element as HTMLElement & { createMermaid?: () => string };
384
- if (element.nodeName === "MERMAID-EDGE" && element.createMermaid)
385
- writeFlowchartEdgeLine(textWriter, designItem, element, nodeItemsById, explicitlyWrittenNodeIds, updatePositions);
119
+ for (const edge of parsed.edges) {
120
+ const scanned = getScannedEdge(scannedEdges, edge);
121
+ edges.push({ ...edge, ...scanned });
122
+ }
123
+ }
124
+ else {
125
+ for (const node of scannedNodes.values())
126
+ mergeNode(nodes, node);
127
+ edges.push(...scannedEdges);
128
+ }
129
+ const mermaidLayout = await getMermaidLayout(html, nodes);
130
+ const designItemsById = new Map();
131
+ let subgraphIndex = 0;
132
+ for (const subgraph of subgraphs.values()) {
133
+ const layout = mermaidLayout?.subgraphs?.get(subgraph.id);
134
+ const element = new MermaidSubgraph();
135
+ element.style.position = "absolute";
136
+ element.style.left = (layout?.x ?? (30 + subgraphIndex * 40)) + "px";
137
+ element.style.top = (layout?.y ?? (30 + subgraphIndex * 40)) + "px";
138
+ element.style.width = (layout?.width ?? 360) + "px";
139
+ element.style.height = (layout?.height ?? 240) + "px";
140
+ element.setAttribute("subgraph-id", subgraph.id);
141
+ element.setAttribute("title", subgraph.title);
142
+ if (subgraph.direction)
143
+ element.setAttribute("direction", subgraph.direction);
144
+ const designItem = DesignItem.createDesignItemFromInstance(element, serviceContainer, instanceServiceContainer);
145
+ designItemsById.set(subgraph.id, designItem);
146
+ const parentItem = designItemsById.get(subgraph.parentId);
147
+ if (parentItem)
148
+ parentItem._insertChildInternal(designItem);
149
+ else
150
+ designItems.push(designItem);
151
+ if (subgraph.sourceRange)
152
+ instanceServiceContainer.designItemDocumentPositionService?.setPosition(designItem, subgraph.sourceRange);
153
+ subgraphIndex++;
154
+ }
155
+ let index = 0;
156
+ for (const node of nodes.values()) {
157
+ const layout = mermaidLayout?.nodes.get(node.id);
158
+ const parentLayout = node.parentId ? mermaidLayout?.subgraphs?.get(node.parentId) : null;
159
+ const fallbackPosition = getFallbackFlowchartNodePosition(index, nodes.size, direction);
160
+ const left = layout ? layout.x - (parentLayout?.x ?? 0) : fallbackPosition.left;
161
+ const top = layout ? layout.y - (parentLayout?.y ?? 0) : fallbackPosition.top;
162
+ const width = layout?.width ?? getDefaultNodeSize(node.shape).width;
163
+ const height = layout?.height ?? getDefaultNodeSize(node.shape).height;
164
+ const element = new MermaidNode();
165
+ element.style.position = "absolute";
166
+ element.style.left = left + "px";
167
+ element.style.top = top + "px";
168
+ element.style.width = width + "px";
169
+ element.style.height = height + "px";
170
+ element.setAttribute("node-id", node.id);
171
+ element.setAttribute("label", node.label);
172
+ element.setAttribute("shape", node.shape);
173
+ element.setAttribute("diagram-direction", direction);
174
+ if (node.classes)
175
+ element.setAttribute("classes", node.classes);
176
+ if (node.style)
177
+ element.setAttribute("style-directive", node.style);
178
+ const designItem = DesignItem.createDesignItemFromInstance(element, serviceContainer, instanceServiceContainer);
179
+ const parentItem = designItemsById.get(node.parentId);
180
+ if (parentItem)
181
+ parentItem._insertChildInternal(designItem);
182
+ else
183
+ designItems.push(designItem);
184
+ designItemsById.set(node.id, designItem);
185
+ if (node.sourceRange)
186
+ instanceServiceContainer.designItemDocumentPositionService?.setPosition(designItem, node.sourceRange);
187
+ nodeBounds.set(node.id, { x: layout?.x ?? left, y: layout?.y ?? top, width, height });
188
+ index++;
189
+ }
190
+ let edgeIndex = 0;
191
+ for (const edge of edges) {
192
+ const element = new MermaidEdge();
193
+ element.style.position = "absolute";
194
+ element.style.left = "40px";
195
+ element.style.top = 60 + index * 110 + edgeIndex * 44 + "px";
196
+ element.style.width = "220px";
197
+ element.style.height = "28px";
198
+ element.setAttribute("from", edge.from);
199
+ element.setAttribute("to", edge.to);
200
+ element.setAttribute("label", edge.label);
201
+ element.setAttribute("edge-type", edge.edgeType);
202
+ if (edge.connector)
203
+ element.setAttribute("connector", edge.connector);
204
+ if (edge.edgeId)
205
+ element.setAttribute("edge-id", edge.edgeId);
206
+ if (edge.animation)
207
+ element.setAttribute("animation", edge.animation);
208
+ element.setAttribute("diagram-direction", direction);
209
+ const sourceBounds = nodeBounds.get(edge.from);
210
+ const targetBounds = nodeBounds.get(edge.to);
211
+ if (sourceBounds && targetBounds)
212
+ element.setAttribute("waypoints", encodeWaypoints(routeBetweenBounds(sourceBounds, targetBounds, direction)));
213
+ const designItem = DesignItem.createDesignItemFromInstance(element, serviceContainer, instanceServiceContainer);
214
+ designItems.push(designItem);
215
+ if (edge.sourceRange)
216
+ instanceServiceContainer.designItemDocumentPositionService?.setPosition(designItem, edge.connectorRange ?? edge.sourceRange);
217
+ edgeIndex++;
218
+ }
219
+ let directiveIndex = 0;
220
+ for (const directive of directives) {
221
+ const element = new MermaidFlowchartDirective();
222
+ element.style.position = "absolute";
223
+ element.style.left = "40px";
224
+ element.style.top = 60 + index * 110 + edgeIndex * 44 + directiveIndex * 28 + "px";
225
+ element.style.width = "320px";
226
+ element.style.height = "24px";
227
+ element.setAttribute("line", directive.line);
228
+ const designItem = DesignItem.createDesignItemFromInstance(element, serviceContainer, instanceServiceContainer);
229
+ designItems.push(designItem);
230
+ if (directive.sourceRange)
231
+ instanceServiceContainer.designItemDocumentPositionService?.setPosition(designItem, directive.sourceRange);
232
+ directiveIndex++;
233
+ }
234
+ return designItems;
235
+ }
236
+ function writeFlowchart(textWriter, designItems, updatePositions, rootDesignItem) {
237
+ const rootDirection = rootDesignItem?.getAttribute(mermaidFlowchartDirectionAttribute);
238
+ const direction = isFlowchartDirection(rootDirection) ? rootDirection : getDiagramDirection(designItems);
239
+ writeMermaidFrontmatter(textWriter, designItems, rootDesignItem);
240
+ textWriter.writeLine("flowchart " + direction);
241
+ const allFlowchartItems = getAllFlowchartItems(designItems);
242
+ const nodeIdsUsedByEdges = getFlowchartNodeIdsUsedByEdges(allFlowchartItems);
243
+ const nodeItemsById = getFlowchartNodeItemsById(allFlowchartItems);
244
+ const explicitlyWrittenNodeIds = new Set();
245
+ const writeNodeOrSubgraph = (designItem, depth) => {
246
+ const element = designItem.element;
247
+ if (element.nodeName === "MERMAID-SUBGRAPH") {
248
+ const subgraphElement = element;
249
+ writeDesignItemLine(textWriter, designItem, repeatText(" ", depth) + subgraphElement.createMermaidStart(), updatePositions);
250
+ const subgraphDirection = element.getAttribute("direction");
251
+ if (isFlowchartDirection(subgraphDirection))
252
+ textWriter.writeLine(repeatText(" ", depth + 1) + "direction " + subgraphDirection);
253
+ for (const child of designItem.children())
254
+ writeNodeOrSubgraph(child, depth + 1);
255
+ textWriter.writeLine(repeatText(" ", depth) + "end");
386
256
  }
387
- for (const designItem of allFlowchartItems) {
388
- const element = designItem.element as HTMLElement & { createMermaid?: () => string };
389
- if (element.nodeName === "MERMAID-FLOWCHART-DIRECTIVE" && element.createMermaid)
390
- writeDesignItemLine(textWriter, designItem, " " + element.createMermaid(), updatePositions);
257
+ else if (element.nodeName === "MERMAID-NODE" && element.createMermaid && shouldWriteFlowchartNode(element, nodeIdsUsedByEdges)) {
258
+ writeDesignItemLine(textWriter, designItem, repeatText(" ", depth) + element.createMermaid(), updatePositions);
259
+ explicitlyWrittenNodeIds.add(element.getAttribute("node-id"));
391
260
  }
261
+ };
262
+ for (const designItem of designItems) {
263
+ writeNodeOrSubgraph(designItem, 1);
264
+ }
265
+ for (const designItem of allFlowchartItems) {
266
+ const element = designItem.element;
267
+ if (element.nodeName === "MERMAID-EDGE" && element.createMermaid)
268
+ writeFlowchartEdgeLine(textWriter, designItem, element, nodeItemsById, explicitlyWrittenNodeIds, updatePositions);
269
+ }
270
+ for (const designItem of allFlowchartItems) {
271
+ const element = designItem.element;
272
+ if (element.nodeName === "MERMAID-FLOWCHART-DIRECTIVE" && element.createMermaid)
273
+ writeDesignItemLine(textWriter, designItem, " " + element.createMermaid(), updatePositions);
274
+ }
392
275
  }
393
-
394
- function writeFlowchartEdgeLine(textWriter: ITextWriter, designItem: IDesignItem, element: HTMLElement & { createMermaid?: () => string }, nodeItemsById: Map<string, IDesignItem>, explicitlyWrittenNodeIds: Set<string>, updatePositions?: boolean) {
276
+ function writeFlowchartEdgeLine(textWriter, designItem, element, nodeItemsById, explicitlyWrittenNodeIds, updatePositions) {
395
277
  const start = textWriter.position;
396
278
  const line = indentEmbeddedNewlines(" " + element.createMermaid());
397
279
  textWriter.writeLine(line);
398
280
  if (!updatePositions)
399
281
  return;
400
-
401
282
  const from = element.getAttribute("from");
402
283
  const to = element.getAttribute("to");
403
284
  const fromIndex = from ? line.indexOf(from) : -1;
404
285
  const toIndex = to ? line.lastIndexOf(to) : -1;
405
-
406
286
  if (from && fromIndex >= 0 && !explicitlyWrittenNodeIds.has(from))
407
287
  nodeItemsById.get(from)?.instanceServiceContainer.designItemDocumentPositionService?.setPosition(nodeItemsById.get(from), { start: start + fromIndex, length: from.length });
408
288
  if (to && toIndex >= 0 && !explicitlyWrittenNodeIds.has(to))
409
289
  nodeItemsById.get(to)?.instanceServiceContainer.designItemDocumentPositionService?.setPosition(nodeItemsById.get(to), { start: start + toIndex, length: to.length });
410
-
411
290
  if (fromIndex >= 0 && toIndex > fromIndex) {
412
291
  const connectorStart = fromIndex + from.length;
413
292
  designItem.instanceServiceContainer.designItemDocumentPositionService?.setPosition(designItem, { start: start + connectorStart, length: toIndex - connectorStart });
414
- } else {
293
+ }
294
+ else {
415
295
  designItem.instanceServiceContainer.designItemDocumentPositionService?.setPosition(designItem, { start, length: line.length });
416
296
  }
417
297
  }
418
-
419
- function getFlowchartNodeIdsUsedByEdges(designItems: IDesignItem[]) {
420
- const ids = new Set<string>();
298
+ function getFlowchartNodeIdsUsedByEdges(designItems) {
299
+ const ids = new Set();
421
300
  for (const designItem of designItems) {
422
- const element = designItem.element as HTMLElement;
301
+ const element = designItem.element;
423
302
  if (element.nodeName === "MERMAID-EDGE") {
424
303
  const from = element.getAttribute("from");
425
304
  const to = element.getAttribute("to");
@@ -431,11 +310,10 @@ function getFlowchartNodeIdsUsedByEdges(designItems: IDesignItem[]) {
431
310
  }
432
311
  return ids;
433
312
  }
434
-
435
- function getFlowchartNodeItemsById(designItems: IDesignItem[]) {
436
- const nodeItemsById = new Map<string, IDesignItem>();
313
+ function getFlowchartNodeItemsById(designItems) {
314
+ const nodeItemsById = new Map();
437
315
  for (const designItem of designItems) {
438
- const element = designItem.element as HTMLElement;
316
+ const element = designItem.element;
439
317
  if (element.nodeName === "MERMAID-NODE") {
440
318
  const id = element.getAttribute("node-id");
441
319
  if (id)
@@ -444,10 +322,9 @@ function getFlowchartNodeItemsById(designItems: IDesignItem[]) {
444
322
  }
445
323
  return nodeItemsById;
446
324
  }
447
-
448
- function getAllFlowchartItems(designItems: IDesignItem[]) {
449
- const result: IDesignItem[] = [];
450
- const append = (item: IDesignItem) => {
325
+ function getAllFlowchartItems(designItems) {
326
+ const result = [];
327
+ const append = (item) => {
451
328
  result.push(item);
452
329
  for (const child of item.children())
453
330
  append(child);
@@ -456,8 +333,7 @@ function getAllFlowchartItems(designItems: IDesignItem[]) {
456
333
  append(designItem);
457
334
  return result;
458
335
  }
459
-
460
- function shouldWriteFlowchartNode(element: HTMLElement, nodeIdsUsedByEdges: Set<string>) {
336
+ function shouldWriteFlowchartNode(element, nodeIdsUsedByEdges) {
461
337
  const id = element.getAttribute("node-id");
462
338
  const label = element.getAttribute("label");
463
339
  const shape = element.getAttribute("shape") ?? "rectangle";
@@ -465,32 +341,27 @@ function shouldWriteFlowchartNode(element: HTMLElement, nodeIdsUsedByEdges: Set<
465
341
  return true;
466
342
  return shape !== "rectangle" || (label && label !== id);
467
343
  }
468
-
469
- async function parseSequenceDiagram(html: string, serviceContainer: ServiceContainer, instanceServiceContainer: InstanceServiceContainer): Promise<IDesignItem[]> {
344
+ async function parseSequenceDiagram(html, serviceContainer, instanceServiceContainer) {
470
345
  const document = readMermaidDocument(html);
471
346
  setMermaidDocumentAttributes(instanceServiceContainer, "sequenceDiagram", undefined, document.title, document.frontmatter);
472
347
  const lines = document.lines;
473
- const participants = new Map<string, ParsedSequenceParticipant>();
474
- const messages: ParsedSequenceMessage[] = [];
475
- const scannedParticipants = new Map<string, ParsedSequenceParticipant>();
476
- const scannedMessages: ParsedSequenceMessage[] = [];
477
-
348
+ const participants = new Map();
349
+ const messages = [];
350
+ const scannedParticipants = new Map();
351
+ const scannedMessages = [];
478
352
  for (const line of lines) {
479
353
  if (!line.trimmed || line.trimmed.startsWith("%%") || equalsIgnoreCase(line.trimmed, "sequenceDiagram"))
480
354
  continue;
481
-
482
355
  const participant = parseSequenceParticipant(line.trimmed, line.start);
483
356
  if (participant) {
484
357
  scannedParticipants.set(participant.id, participant);
485
358
  continue;
486
359
  }
487
-
488
360
  const message = parseSequenceMessage(line.trimmed, line.start);
489
361
  if (message) {
490
362
  scannedMessages.push(message);
491
363
  }
492
364
  }
493
-
494
365
  const parsed = await getMermaidSequenceData(html);
495
366
  if (parsed) {
496
367
  for (const participant of parsed.participants) {
@@ -507,19 +378,18 @@ async function parseSequenceDiagram(html: string, serviceContainer: ServiceConta
507
378
  sourceRange: scannedMessages[i]?.sourceRange,
508
379
  });
509
380
  }
510
- } else {
381
+ }
382
+ else {
511
383
  for (const participant of scannedParticipants.values())
512
384
  participants.set(participant.id, participant);
513
385
  messages.push(...scannedMessages);
514
386
  }
515
-
516
387
  for (const message of messages) {
517
388
  ensureSequenceParticipant(participants, message.from);
518
389
  ensureSequenceParticipant(participants, message.to);
519
390
  }
520
-
521
- const designItems: IDesignItem[] = [];
522
- const participantBounds = new Map<string, { x: number; y: number; width: number; height: number }>();
391
+ const designItems = [];
392
+ const participantBounds = new Map();
523
393
  let index = 0;
524
394
  for (const participant of participants.values()) {
525
395
  const width = 140;
@@ -544,7 +414,6 @@ async function parseSequenceDiagram(html: string, serviceContainer: ServiceConta
544
414
  participantBounds.set(participant.id, { x: left, y: top, width, height });
545
415
  index++;
546
416
  }
547
-
548
417
  let messageIndex = 0;
549
418
  for (const message of messages) {
550
419
  const fromBounds = participantBounds.get(message.from);
@@ -571,50 +440,42 @@ async function parseSequenceDiagram(html: string, serviceContainer: ServiceConta
571
440
  instanceServiceContainer.designItemDocumentPositionService?.setPosition(designItem, message.sourceRange);
572
441
  messageIndex++;
573
442
  }
574
-
575
443
  return designItems;
576
444
  }
577
-
578
- function writeSequenceDiagram(textWriter: ITextWriter, designItems: IDesignItem[], updatePositions?: boolean, rootDesignItem?: IDesignItem) {
445
+ function writeSequenceDiagram(textWriter, designItems, updatePositions, rootDesignItem) {
579
446
  writeMermaidFrontmatter(textWriter, designItems, rootDesignItem);
580
447
  textWriter.writeLine("sequenceDiagram");
581
448
  for (const designItem of designItems) {
582
- const element = designItem.element as HTMLElement & { createMermaid?: () => string };
449
+ const element = designItem.element;
583
450
  if (element.nodeName === "MERMAID-SEQUENCE-PARTICIPANT" && element.getAttribute("implicit") !== "true" && element.createMermaid)
584
451
  writeDesignItemLine(textWriter, designItem, " " + element.createMermaid(), updatePositions);
585
452
  }
586
453
  for (const designItem of designItems) {
587
- const element = designItem.element as HTMLElement & { createMermaid?: () => string };
454
+ const element = designItem.element;
588
455
  if (element.nodeName === "MERMAID-SEQUENCE-MESSAGE" && element.createMermaid)
589
456
  writeDesignItemLine(textWriter, designItem, " " + element.createMermaid(), updatePositions);
590
457
  }
591
458
  }
592
-
593
- async function parseMindmapDiagram(html: string, serviceContainer: ServiceContainer, instanceServiceContainer: InstanceServiceContainer): Promise<IDesignItem[]> {
459
+ async function parseMindmapDiagram(html, serviceContainer, instanceServiceContainer) {
594
460
  const document = readMermaidDocument(html);
595
461
  setMermaidDocumentAttributes(instanceServiceContainer, "mindmap", undefined, document.title, document.frontmatter);
596
-
597
- const parsedNodes: ParsedMindmapNode[] = [];
598
- const parentStack: ParsedMindmapNode[] = [];
462
+ const parsedNodes = [];
463
+ const parentStack = [];
599
464
  let index = 0;
600
-
601
465
  for (const line of document.lines) {
602
466
  if (!line.trimmed || line.trimmed.startsWith("%%") || equalsIgnoreCase(line.trimmed, "mindmap"))
603
467
  continue;
604
-
605
468
  while (parentStack.length && parentStack[parentStack.length - 1].indent >= line.indent)
606
469
  parentStack.pop();
607
-
608
470
  const parent = parentStack[parentStack.length - 1];
609
471
  const parsed = parseMindmapNode(line.trimmed, index, parent?.id ?? "", line.start, line.text.length, line.indent);
610
472
  parsedNodes.push(parsed);
611
473
  parentStack.push(parsed);
612
474
  index++;
613
475
  }
614
-
615
- const siblingsByParent = new Map<string, number>();
616
- const designItemsById = new Map<string, IDesignItem>();
617
- const rootDesignItems: IDesignItem[] = [];
476
+ const siblingsByParent = new Map();
477
+ const designItemsById = new Map();
478
+ const rootDesignItems = [];
618
479
  const mermaidLayout = await getMermaidMindmapLayout(html, parsedNodes);
619
480
  for (const node of parsedNodes) {
620
481
  const siblingIndex = siblingsByParent.get(node.parentId) ?? 0;
@@ -637,22 +498,21 @@ async function parseMindmapDiagram(html: string, serviceContainer: ServiceContai
637
498
  const parentItem = designItemsById.get(node.parentId);
638
499
  if (parentItem) {
639
500
  parentItem._insertChildInternal(designItem);
640
- } else {
501
+ }
502
+ else {
641
503
  rootDesignItems.push(designItem);
642
504
  }
643
505
  if (node.sourceRange)
644
506
  instanceServiceContainer.designItemDocumentPositionService?.setPosition(designItem, node.sourceRange);
645
507
  }
646
-
647
508
  return rootDesignItems;
648
509
  }
649
-
650
- function writeMindmapDiagram(textWriter: ITextWriter, designItems: IDesignItem[], updatePositions?: boolean, rootDesignItem?: IDesignItem) {
510
+ function writeMindmapDiagram(textWriter, designItems, updatePositions, rootDesignItem) {
651
511
  writeMermaidFrontmatter(textWriter, designItems, rootDesignItem);
652
512
  textWriter.writeLine("mindmap");
653
- const writeItems = (items: IDesignItem[], depth: number) => {
513
+ const writeItems = (items, depth) => {
654
514
  for (const item of sortMindmapItems(items)) {
655
- const element = item.element as HTMLElement & { createMermaid?: () => string };
515
+ const element = item.element;
656
516
  if (!element.createMermaid)
657
517
  continue;
658
518
  writeDesignItemLine(textWriter, item, repeatText(" ", depth + 1) + element.createMermaid(), updatePositions);
@@ -661,28 +521,24 @@ function writeMindmapDiagram(textWriter: ITextWriter, designItems: IDesignItem[]
661
521
  };
662
522
  writeItems(designItems.filter(designItem => designItem.element.localName === MermaidMindmapNode.is), 0);
663
523
  }
664
-
665
- async function parseRequirementDiagram(html: string, serviceContainer: ServiceContainer, instanceServiceContainer: InstanceServiceContainer): Promise<IDesignItem[]> {
524
+ async function parseRequirementDiagram(html, serviceContainer, instanceServiceContainer) {
666
525
  const document = readMermaidDocument(html);
667
526
  const lines = document.lines;
668
- const nodes = new Map<string, ParsedRequirementNode>();
669
- const relationships: ParsedRequirementRelationship[] = [];
670
- let direction: FlowchartDirection = "TB";
671
-
527
+ const nodes = new Map();
528
+ const relationships = [];
529
+ let direction = "TB";
672
530
  for (let i = 0; i < lines.length; i++) {
673
531
  const line = lines[i];
674
532
  if (!line.trimmed || line.trimmed.startsWith("%%") || equalsIgnoreCase(line.trimmed, "requirementDiagram"))
675
533
  continue;
676
-
677
534
  const parsedDirection = parseDirectionStatement(line.trimmed);
678
535
  if (parsedDirection) {
679
536
  direction = parsedDirection;
680
537
  continue;
681
538
  }
682
-
683
539
  const blockStart = parseRequirementBlockStart(line.trimmed);
684
540
  if (blockStart) {
685
- const blockLines: LineRecord[] = [];
541
+ const blockLines = [];
686
542
  const start = line.start;
687
543
  while (++i < lines.length) {
688
544
  if (lines[i].trimmed === "}")
@@ -694,16 +550,13 @@ async function parseRequirementDiagram(html: string, serviceContainer: ServiceCo
694
550
  nodes.set(node.id, node);
695
551
  continue;
696
552
  }
697
-
698
553
  const relationship = parseRequirementRelationship(line.trimmed, line.start);
699
554
  if (relationship)
700
555
  relationships.push(relationship);
701
556
  }
702
-
703
557
  setMermaidDocumentAttributes(instanceServiceContainer, "requirementDiagram", direction, document.title, document.frontmatter);
704
-
705
- const designItems: IDesignItem[] = [];
706
- const nodeBounds = new Map<string, { x: number; y: number; width: number; height: number }>();
558
+ const designItems = [];
559
+ const nodeBounds = new Map();
707
560
  const mermaidLayout = await getMermaidRequirementLayout(html, nodes);
708
561
  let index = 0;
709
562
  for (const node of nodes.values()) {
@@ -728,7 +581,8 @@ async function parseRequirementDiagram(html: string, serviceContainer: ServiceCo
728
581
  element.setAttribute("text", node.text ?? "");
729
582
  element.setAttribute("risk", node.risk ?? "");
730
583
  element.setAttribute("verify-method", node.verifyMethod ?? "");
731
- } else {
584
+ }
585
+ else {
732
586
  element.setAttribute("element-type", node.elementType ?? "");
733
587
  element.setAttribute("doc-ref", node.docRef ?? "");
734
588
  }
@@ -739,7 +593,6 @@ async function parseRequirementDiagram(html: string, serviceContainer: ServiceCo
739
593
  nodeBounds.set(node.id, { x: left, y: top, width, height });
740
594
  index++;
741
595
  }
742
-
743
596
  let relationshipIndex = 0;
744
597
  for (const relationship of relationships) {
745
598
  const element = new MermaidRequirementRelationship();
@@ -752,7 +605,8 @@ async function parseRequirementDiagram(html: string, serviceContainer: ServiceCo
752
605
  const targetBounds = nodeBounds.get(relationship.to);
753
606
  if (sourceBounds && targetBounds) {
754
607
  element.setAttribute("waypoints", encodeWaypoints(routeBetweenBounds(sourceBounds, targetBounds, direction)));
755
- } else {
608
+ }
609
+ else {
756
610
  element.style.left = "40px";
757
611
  element.style.top = 80 + index * 120 + relationshipIndex * 40 + "px";
758
612
  element.style.width = "220px";
@@ -764,11 +618,9 @@ async function parseRequirementDiagram(html: string, serviceContainer: ServiceCo
764
618
  instanceServiceContainer.designItemDocumentPositionService?.setPosition(designItem, relationship.sourceRange);
765
619
  relationshipIndex++;
766
620
  }
767
-
768
621
  return designItems;
769
622
  }
770
-
771
- function writeRequirementDiagram(textWriter: ITextWriter, designItems: IDesignItem[], updatePositions?: boolean, rootDesignItem?: IDesignItem) {
623
+ function writeRequirementDiagram(textWriter, designItems, updatePositions, rootDesignItem) {
772
624
  const rootDirection = rootDesignItem?.getAttribute(mermaidFlowchartDirectionAttribute);
773
625
  const direction = isRequirementDirection(rootDirection) ? rootDirection : "TB";
774
626
  writeMermaidFrontmatter(textWriter, designItems, rootDesignItem);
@@ -776,18 +628,17 @@ function writeRequirementDiagram(textWriter: ITextWriter, designItems: IDesignIt
776
628
  if (direction !== "TB")
777
629
  textWriter.writeLine(" direction " + direction);
778
630
  for (const designItem of designItems) {
779
- const element = designItem.element as HTMLElement & { createMermaid?: () => string };
631
+ const element = designItem.element;
780
632
  if (element.nodeName === "MERMAID-REQUIREMENT-NODE" && element.createMermaid)
781
633
  writeDesignItemLine(textWriter, designItem, " " + element.createMermaid(), updatePositions);
782
634
  }
783
635
  for (const designItem of designItems) {
784
- const element = designItem.element as HTMLElement & { createMermaid?: () => string };
636
+ const element = designItem.element;
785
637
  if (element.nodeName === "MERMAID-REQUIREMENT-RELATIONSHIP" && element.createMermaid)
786
638
  writeDesignItemLine(textWriter, designItem, " " + element.createMermaid(), updatePositions);
787
639
  }
788
640
  }
789
-
790
- function parseSequenceParticipant(line: string, sourceStart: number): ParsedSequenceParticipant {
641
+ function parseSequenceParticipant(line, sourceStart) {
791
642
  const cursor = new SourceCursor(line);
792
643
  const keyword = cursor.readIdentifier();
793
644
  if (!equalsIgnoreCase(keyword, "participant") && !equalsIgnoreCase(keyword, "actor"))
@@ -797,7 +648,6 @@ function parseSequenceParticipant(line: string, sourceStart: number): ParsedSequ
797
648
  const id = cursor.readIdentifier();
798
649
  if (!id)
799
650
  return null;
800
-
801
651
  let label = id;
802
652
  cursor.skipWhitespace();
803
653
  if (!cursor.atEnd()) {
@@ -808,7 +658,6 @@ function parseSequenceParticipant(line: string, sourceStart: number): ParsedSequ
808
658
  return null;
809
659
  label = cursor.readRest().trim();
810
660
  }
811
-
812
661
  return {
813
662
  id,
814
663
  label: unquoteLabel(label),
@@ -816,8 +665,7 @@ function parseSequenceParticipant(line: string, sourceStart: number): ParsedSequ
816
665
  sourceRange: { start: sourceStart, length: line.length },
817
666
  };
818
667
  }
819
-
820
- function parseSequenceMessage(line: string, sourceStart: number): ParsedSequenceMessage {
668
+ function parseSequenceMessage(line, sourceStart) {
821
669
  const parsed = parseBinaryStatement(line, ["-->>", "->>", "-->", "->", "--x", "-x", "--)", "-)"]);
822
670
  if (!parsed)
823
671
  return null;
@@ -828,7 +676,6 @@ function parseSequenceMessage(line: string, sourceStart: number): ParsedSequence
828
676
  const label = parsed.right.substring(separatorIndex + 1).trim();
829
677
  if (!to || !isIdentifier(to))
830
678
  return null;
831
-
832
679
  return {
833
680
  from: parsed.left,
834
681
  to,
@@ -838,13 +685,11 @@ function parseSequenceMessage(line: string, sourceStart: number): ParsedSequence
838
685
  sourceRange: { start: sourceStart, length: line.length },
839
686
  };
840
687
  }
841
-
842
- function ensureSequenceParticipant(participants: Map<string, ParsedSequenceParticipant>, id: string) {
688
+ function ensureSequenceParticipant(participants, id) {
843
689
  if (!participants.has(id))
844
690
  participants.set(id, { id, label: id, participantType: "participant", implicit: true });
845
691
  }
846
-
847
- function getSequenceMessageType(connector: string) {
692
+ function getSequenceMessageType(connector) {
848
693
  if (connector.includes(")"))
849
694
  return "async";
850
695
  if (connector.includes("--"))
@@ -855,28 +700,24 @@ function getSequenceMessageType(connector: string) {
855
700
  return "open";
856
701
  return "solid";
857
702
  }
858
-
859
703
  class SourceCursor {
860
- public position = 0;
861
-
862
- constructor(private readonly text: string) {
704
+ text;
705
+ position = 0;
706
+ constructor(text) {
707
+ this.text = text;
863
708
  }
864
-
865
709
  atEnd() {
866
710
  return this.position >= this.text.length;
867
711
  }
868
-
869
712
  skipWhitespace() {
870
713
  while (!this.atEnd() && isWhitespace(this.text[this.position]))
871
714
  this.position++;
872
715
  }
873
-
874
716
  skipRequiredWhitespace() {
875
717
  const start = this.position;
876
718
  this.skipWhitespace();
877
719
  return this.position > start;
878
720
  }
879
-
880
721
  readIdentifier() {
881
722
  if (this.atEnd() || !isIdentifierStart(this.text[this.position]))
882
723
  return "";
@@ -886,22 +727,19 @@ class SourceCursor {
886
727
  this.position++;
887
728
  return this.text.substring(start, this.position);
888
729
  }
889
-
890
730
  readRest() {
891
731
  const rest = this.text.substring(this.position);
892
732
  this.position = this.text.length;
893
733
  return rest;
894
734
  }
895
-
896
- consume(value: string) {
735
+ consume(value) {
897
736
  if (!this.text.startsWith(value, this.position))
898
737
  return false;
899
738
  this.position += value.length;
900
739
  return true;
901
740
  }
902
741
  }
903
-
904
- function parseFlowchartDirection(line: string): FlowchartDirection | null {
742
+ function parseFlowchartDirection(line) {
905
743
  const cursor = new SourceCursor(line);
906
744
  const keyword = cursor.readIdentifier();
907
745
  if (!equalsIgnoreCase(keyword, "graph") && !equalsIgnoreCase(keyword, "flowchart"))
@@ -911,8 +749,7 @@ function parseFlowchartDirection(line: string): FlowchartDirection | null {
911
749
  const direction = cursor.readIdentifier().toUpperCase();
912
750
  return isFlowchartDirection(direction) ? direction : null;
913
751
  }
914
-
915
- function parseDirectionStatement(line: string): FlowchartDirection | null {
752
+ function parseDirectionStatement(line) {
916
753
  const cursor = new SourceCursor(line);
917
754
  const keyword = cursor.readIdentifier();
918
755
  if (!equalsIgnoreCase(keyword, "direction"))
@@ -922,8 +759,7 @@ function parseDirectionStatement(line: string): FlowchartDirection | null {
922
759
  const direction = cursor.readIdentifier().toUpperCase();
923
760
  return isRequirementDirection(direction) ? direction : null;
924
761
  }
925
-
926
- function parseSubgraphStart(line: string, sourceStart: number, parentId?: string): ParsedSubgraph | null {
762
+ function parseSubgraphStart(line, sourceStart, parentId) {
927
763
  const cursor = new SourceCursor(line);
928
764
  const keyword = cursor.readIdentifier();
929
765
  if (!equalsIgnoreCase(keyword, "subgraph"))
@@ -943,8 +779,7 @@ function parseSubgraphStart(line: string, sourceStart: number, parentId?: string
943
779
  const title = unquoteLabel(rest);
944
780
  return { id: sanitizeDirectiveId(title), title, parentId, sourceRange: { start: sourceStart, length: line.length } };
945
781
  }
946
-
947
- function parseFlowchartDirective(line: string, sourceStart: number): ParsedFlowchartDirective | null {
782
+ function parseFlowchartDirective(line, sourceStart) {
948
783
  const cursor = new SourceCursor(line);
949
784
  const keyword = cursor.readIdentifier();
950
785
  if (!keyword)
@@ -955,17 +790,14 @@ function parseFlowchartDirective(line: string, sourceStart: number): ParsedFlowc
955
790
  return null;
956
791
  return null;
957
792
  }
958
-
959
- function parseBinaryStatement(line: string, operators: string[]) {
793
+ function parseBinaryStatement(line, operators) {
960
794
  const parsed = findOperator(line, operators);
961
795
  if (!parsed)
962
796
  return null;
963
-
964
797
  const leftRange = trimRange(line, 0, parsed.index);
965
798
  const rightRange = trimRange(line, parsed.index + parsed.operator.length, line.length);
966
799
  if (leftRange.start >= leftRange.end || rightRange.start >= rightRange.end)
967
800
  return null;
968
-
969
801
  return {
970
802
  left: line.substring(leftRange.start, leftRange.end),
971
803
  leftStart: leftRange.start,
@@ -975,8 +807,7 @@ function parseBinaryStatement(line: string, operators: string[]) {
975
807
  rightStart: rightRange.start,
976
808
  };
977
809
  }
978
-
979
- function parseRequirementBlockStart(line: string): { kind: "requirement" | "element"; type: string; id: string } | null {
810
+ function parseRequirementBlockStart(line) {
980
811
  const cursor = new SourceCursor(line);
981
812
  const type = cursor.readIdentifier();
982
813
  if (!type)
@@ -998,9 +829,8 @@ function parseRequirementBlockStart(line: string): { kind: "requirement" | "elem
998
829
  return null;
999
830
  return { kind, type, id };
1000
831
  }
1001
-
1002
- function parseRequirementBlock(blockStart: { kind: "requirement" | "element"; type: string; id: string }, lines: LineRecord[], sourceStart: number, sourceLength: number): ParsedRequirementNode {
1003
- const values = new Map<string, string>();
832
+ function parseRequirementBlock(blockStart, lines, sourceStart, sourceLength) {
833
+ const values = new Map();
1004
834
  for (const line of lines) {
1005
835
  const separator = findTopLevelColon(line.trimmed);
1006
836
  if (separator < 0)
@@ -1009,7 +839,6 @@ function parseRequirementBlock(blockStart: { kind: "requirement" | "element"; ty
1009
839
  const value = unquoteLabel(line.trimmed.substring(separator + 1).trim());
1010
840
  values.set(key, value);
1011
841
  }
1012
-
1013
842
  if (blockStart.kind === "element") {
1014
843
  return {
1015
844
  id: blockStart.id,
@@ -1019,7 +848,6 @@ function parseRequirementBlock(blockStart: { kind: "requirement" | "element"; ty
1019
848
  sourceRange: { start: sourceStart, length: sourceLength },
1020
849
  };
1021
850
  }
1022
-
1023
851
  return {
1024
852
  id: blockStart.id,
1025
853
  kind: "requirement",
@@ -1031,15 +859,13 @@ function parseRequirementBlock(blockStart: { kind: "requirement" | "element"; ty
1031
859
  sourceRange: { start: sourceStart, length: sourceLength },
1032
860
  };
1033
861
  }
1034
-
1035
- function parseRequirementRelationship(line: string, sourceStart: number): ParsedRequirementRelationship | null {
862
+ function parseRequirementRelationship(line, sourceStart) {
1036
863
  const forward = parseRequirementRelationshipForward(line, sourceStart);
1037
864
  if (forward)
1038
865
  return forward;
1039
866
  return parseRequirementRelationshipReverse(line, sourceStart);
1040
867
  }
1041
-
1042
- function parseRequirementRelationshipForward(line: string, sourceStart: number): ParsedRequirementRelationship | null {
868
+ function parseRequirementRelationshipForward(line, sourceStart) {
1043
869
  const cursor = new SourceCursor(line);
1044
870
  const from = cursor.readIdentifier();
1045
871
  if (!from)
@@ -1060,8 +886,7 @@ function parseRequirementRelationshipForward(line: string, sourceStart: number):
1060
886
  return null;
1061
887
  return { from, to, relationshipType, syntaxDirection: "forward", sourceRange: { start: sourceStart, length: line.length } };
1062
888
  }
1063
-
1064
- function parseRequirementRelationshipReverse(line: string, sourceStart: number): ParsedRequirementRelationship | null {
889
+ function parseRequirementRelationshipReverse(line, sourceStart) {
1065
890
  const cursor = new SourceCursor(line);
1066
891
  const to = cursor.readIdentifier();
1067
892
  if (!to)
@@ -1082,12 +907,10 @@ function parseRequirementRelationshipReverse(line: string, sourceStart: number):
1082
907
  return null;
1083
908
  return { from, to, relationshipType, syntaxDirection: "reverse", sourceRange: { start: sourceStart, length: line.length } };
1084
909
  }
1085
-
1086
- function parseFlowchartEdgeStatement(line: string) {
910
+ function parseFlowchartEdgeStatement(line) {
1087
911
  const parsed = parseBinaryStatement(line, getFlowchartOperators());
1088
912
  if (!parsed)
1089
913
  return null;
1090
-
1091
914
  let left = parsed.left;
1092
915
  let leftStart = parsed.leftStart;
1093
916
  let edgeId = "";
@@ -1104,7 +927,6 @@ function parseFlowchartEdgeStatement(line: string) {
1104
927
  }
1105
928
  }
1106
929
  }
1107
-
1108
930
  let label = "";
1109
931
  let labelPartLength = 0;
1110
932
  const right = parsed.right;
@@ -1128,10 +950,8 @@ function parseFlowchartEdgeStatement(line: string) {
1128
950
  };
1129
951
  }
1130
952
  }
1131
-
1132
953
  return { ...parsed, left, leftStart, label, labelPartLength, edgeId };
1133
954
  }
1134
-
1135
955
  function getFlowchartOperators() {
1136
956
  return [
1137
957
  "<-->", "<==>", "<-.->",
@@ -1142,8 +962,7 @@ function getFlowchartOperators() {
1142
962
  "---->", "--->", "-->", "-----", "----", "---",
1143
963
  ];
1144
964
  }
1145
-
1146
- function parseExpandedShape(value: string): { shape: string; label?: string } | null {
965
+ function parseExpandedShape(value) {
1147
966
  const cursor = new SourceCursor(value);
1148
967
  if (!cursor.consume("@{"))
1149
968
  return null;
@@ -1155,9 +974,8 @@ function parseExpandedShape(value: string): { shape: string; label?: string } |
1155
974
  return null;
1156
975
  return { shape, label: properties.get("label") };
1157
976
  }
1158
-
1159
- function parseDelimitedShapeValue(value: string): { label: string; shape: string } | null {
1160
- const candidates: Array<{ start: string; end: string; shape: string }> = [
977
+ function parseDelimitedShapeValue(value) {
978
+ const candidates = [
1161
979
  { start: "(((", end: ")))", shape: "doubleCircle" },
1162
980
  { start: "((", end: "))", shape: "circle" },
1163
981
  { start: "([", end: "])", shape: "stadium" },
@@ -1173,7 +991,6 @@ function parseDelimitedShapeValue(value: string): { label: string; shape: string
1173
991
  { start: "[\\", end: "/]", shape: "trapezoidAlt" },
1174
992
  { start: "[", end: "]", shape: "rectangle" },
1175
993
  ];
1176
-
1177
994
  for (const candidate of candidates) {
1178
995
  if (value.startsWith(candidate.start) && value.endsWith(candidate.end)) {
1179
996
  return {
@@ -1182,12 +999,10 @@ function parseDelimitedShapeValue(value: string): { label: string; shape: string
1182
999
  };
1183
1000
  }
1184
1001
  }
1185
-
1186
1002
  return null;
1187
1003
  }
1188
-
1189
- function parseObjectLiteralProperties(content: string) {
1190
- const properties = new Map<string, string>();
1004
+ function parseObjectLiteralProperties(content) {
1005
+ const properties = new Map();
1191
1006
  let start = 0;
1192
1007
  let quote = "";
1193
1008
  for (let i = 0; i <= content.length; i++) {
@@ -1214,8 +1029,7 @@ function parseObjectLiteralProperties(content: string) {
1214
1029
  }
1215
1030
  return properties;
1216
1031
  }
1217
-
1218
- function findOperator(line: string, operators: string[]) {
1032
+ function findOperator(line, operators) {
1219
1033
  let depth = 0;
1220
1034
  let quote = "";
1221
1035
  for (let i = 0; i < line.length; i++) {
@@ -1242,128 +1056,113 @@ function findOperator(line: string, operators: string[]) {
1242
1056
  }
1243
1057
  return null;
1244
1058
  }
1245
-
1246
- function trimRange(text: string, start: number, end: number) {
1059
+ function trimRange(text, start, end) {
1247
1060
  while (start < end && isWhitespace(text[start]))
1248
1061
  start++;
1249
1062
  while (end > start && isWhitespace(text[end - 1]))
1250
1063
  end--;
1251
1064
  return { start, end };
1252
1065
  }
1253
-
1254
- function countLeadingWhitespace(text: string) {
1066
+ function countLeadingWhitespace(text) {
1255
1067
  let count = 0;
1256
1068
  while (count < text.length && isWhitespace(text[count]))
1257
1069
  count++;
1258
1070
  return count;
1259
1071
  }
1260
-
1261
- function equalsIgnoreCase(a: string, b: string) {
1072
+ function equalsIgnoreCase(a, b) {
1262
1073
  return a.toLowerCase() === b.toLowerCase();
1263
1074
  }
1264
-
1265
- function isIdentifier(value: string) {
1075
+ function isIdentifier(value) {
1266
1076
  const cursor = new SourceCursor(value);
1267
1077
  const id = cursor.readIdentifier();
1268
1078
  cursor.skipWhitespace();
1269
1079
  return id.length === value.length || (id.length > 0 && cursor.atEnd());
1270
1080
  }
1271
-
1272
- function isIdentifierStart(char: string) {
1081
+ function isIdentifierStart(char) {
1273
1082
  return isAsciiLetter(char) || char === "_";
1274
1083
  }
1275
-
1276
- function isIdentifierPart(char: string) {
1084
+ function isIdentifierPart(char) {
1277
1085
  return isIdentifierStart(char) || isAsciiDigit(char) || char === "-";
1278
1086
  }
1279
-
1280
- function isAsciiLetter(char: string) {
1087
+ function isAsciiLetter(char) {
1281
1088
  if (!char)
1282
1089
  return false;
1283
1090
  const code = char.charCodeAt(0);
1284
1091
  return (code >= 65 && code <= 90) || (code >= 97 && code <= 122);
1285
1092
  }
1286
-
1287
- function isAsciiDigit(char: string) {
1093
+ function isAsciiDigit(char) {
1288
1094
  if (!char)
1289
1095
  return false;
1290
1096
  const code = char.charCodeAt(0);
1291
1097
  return code >= 48 && code <= 57;
1292
1098
  }
1293
-
1294
- function isWhitespace(char: string) {
1099
+ function isWhitespace(char) {
1295
1100
  return char === " " || char === "\t" || char === "\r" || char === "\n";
1296
1101
  }
1297
-
1298
- async function getMermaidFlowchartData(code: string): Promise<{ nodes: ParsedNode[]; edges: ParsedEdge[] } | null> {
1102
+ async function getMermaidFlowchartData(code) {
1299
1103
  try {
1300
1104
  const mermaid = await import("mermaid");
1301
1105
  mermaid.default.initialize({ startOnLoad: false, securityLevel: "loose" });
1302
1106
  const diagram = await mermaid.default.mermaidAPI.getDiagramFromText(code);
1303
- const db = diagram.db as any;
1107
+ const db = diagram.db;
1304
1108
  const vertices = db?.getVertices?.();
1305
1109
  const mermaidEdges = db?.getEdges?.();
1306
1110
  if (!vertices || !mermaidEdges)
1307
1111
  return null;
1308
-
1309
- const nodes: ParsedNode[] = Array.from(vertices.values()).map((vertex: any) => ({
1112
+ const nodes = Array.from(vertices.values()).map((vertex) => ({
1310
1113
  id: vertex.id,
1311
1114
  label: normalizeMermaidText(vertex.text ?? vertex.label ?? vertex.id),
1312
1115
  shape: mapMermaidVertexType(vertex.type),
1313
1116
  }));
1314
- const edges: ParsedEdge[] = mermaidEdges.map((edge: any) => ({
1117
+ const edges = mermaidEdges.map((edge) => ({
1315
1118
  from: edge.start,
1316
1119
  to: edge.end,
1317
1120
  label: normalizeMermaidText(edge.text ?? ""),
1318
1121
  edgeType: mapMermaidEdgeType(edge),
1319
1122
  }));
1320
-
1321
1123
  return { nodes, edges };
1322
- } catch {
1124
+ }
1125
+ catch {
1323
1126
  return null;
1324
1127
  }
1325
1128
  }
1326
-
1327
- async function getMermaidSequenceData(code: string): Promise<{ participants: ParsedSequenceParticipant[]; messages: ParsedSequenceMessage[] } | null> {
1129
+ async function getMermaidSequenceData(code) {
1328
1130
  try {
1329
1131
  const mermaid = await import("mermaid");
1330
1132
  mermaid.default.initialize({ startOnLoad: false, securityLevel: "loose" });
1331
1133
  const diagram = await mermaid.default.mermaidAPI.getDiagramFromText(code);
1332
- const db = diagram.db as any;
1134
+ const db = diagram.db;
1333
1135
  const actors = db?.getActors?.();
1334
1136
  const mermaidMessages = db?.getMessages?.();
1335
1137
  if (!actors || !mermaidMessages)
1336
1138
  return null;
1337
-
1338
- const participants: ParsedSequenceParticipant[] = Array.from(actors.entries()).map(([id, actor]: [string, any]) => ({
1139
+ const participants = Array.from(actors.entries()).map(([id, actor]) => ({
1339
1140
  id,
1340
1141
  label: actor.description ?? actor.name ?? id,
1341
1142
  participantType: actor.type ?? "participant",
1342
1143
  }));
1343
- const messages: ParsedSequenceMessage[] = mermaidMessages
1344
- .filter((message: any) => typeof message.from === "string" && typeof message.to === "string")
1345
- .map((message: any) => {
1346
- const connector = mapMermaidSequenceConnector(message.type);
1347
- return {
1348
- from: message.from,
1349
- to: message.to,
1350
- label: message.message ?? "",
1351
- messageType: getSequenceMessageType(connector),
1352
- connector,
1353
- };
1354
- });
1355
-
1144
+ const messages = mermaidMessages
1145
+ .filter((message) => typeof message.from === "string" && typeof message.to === "string")
1146
+ .map((message) => {
1147
+ const connector = mapMermaidSequenceConnector(message.type);
1148
+ return {
1149
+ from: message.from,
1150
+ to: message.to,
1151
+ label: message.message ?? "",
1152
+ messageType: getSequenceMessageType(connector),
1153
+ connector,
1154
+ };
1155
+ });
1356
1156
  return { participants, messages };
1357
- } catch {
1157
+ }
1158
+ catch {
1358
1159
  return null;
1359
1160
  }
1360
1161
  }
1361
-
1362
- function getScannedEdge(scannedEdges: ParsedEdge[], edge: ParsedEdge): Partial<ParsedEdge> {
1162
+ function getScannedEdge(scannedEdges, edge) {
1363
1163
  return scannedEdges.find(scannedEdge => scannedEdge.from === edge.from && scannedEdge.to === edge.to && scannedEdge.label === edge.label) ?? {};
1364
1164
  }
1365
-
1366
- function mapMermaidVertexType(type: string) {
1165
+ function mapMermaidVertexType(type) {
1367
1166
  const expanded = mapExpandedShape(type);
1368
1167
  if (expanded !== "rectangle")
1369
1168
  return expanded;
@@ -1398,8 +1197,7 @@ function mapMermaidVertexType(type: string) {
1398
1197
  return "rectangle";
1399
1198
  }
1400
1199
  }
1401
-
1402
- function mapMermaidEdgeType(edge: any) {
1200
+ function mapMermaidEdgeType(edge) {
1403
1201
  if (edge.stroke === "dotted")
1404
1202
  return "dotted";
1405
1203
  if (edge.stroke === "thick")
@@ -1408,8 +1206,7 @@ function mapMermaidEdgeType(edge: any) {
1408
1206
  return "open";
1409
1207
  return "arrow";
1410
1208
  }
1411
-
1412
- function mapMermaidSequenceConnector(type: number) {
1209
+ function mapMermaidSequenceConnector(type) {
1413
1210
  switch (type) {
1414
1211
  case 1:
1415
1212
  return "-->>";
@@ -1429,8 +1226,7 @@ function mapMermaidSequenceConnector(type: number) {
1429
1226
  return "->>";
1430
1227
  }
1431
1228
  }
1432
-
1433
- function normalizeMermaidText(value: any) {
1229
+ function normalizeMermaidText(value) {
1434
1230
  if (value == null)
1435
1231
  return "";
1436
1232
  if (typeof value === "string")
@@ -1447,39 +1243,33 @@ function normalizeMermaidText(value: any) {
1447
1243
  }
1448
1244
  return stripMarkdownString(value.toString());
1449
1245
  }
1450
-
1451
- function stripMarkdownString(value: string) {
1246
+ function stripMarkdownString(value) {
1452
1247
  let result = value;
1453
1248
  if (result.startsWith("`") && result.endsWith("`") && result.length >= 2)
1454
1249
  result = result.substring(1, result.length - 1);
1455
1250
  return normalizeLabelLineBreaks(result);
1456
1251
  }
1457
-
1458
- function mergeNode(nodes: Map<string, ParsedNode>, node: ParsedNode) {
1252
+ function mergeNode(nodes, node) {
1459
1253
  const existing = nodes.get(node.id);
1460
1254
  if (!existing || existing.label === existing.id || node.label !== node.id)
1461
1255
  nodes.set(node.id, node);
1462
1256
  }
1463
-
1464
- function parseNode(line: string, sourceStart?: number): ParsedNode {
1257
+ function parseNode(line, sourceStart) {
1465
1258
  const cursor = new SourceCursor(line);
1466
1259
  const id = cursor.readIdentifier();
1467
1260
  if (!id)
1468
1261
  return null;
1469
-
1470
1262
  cursor.skipWhitespace();
1471
1263
  const value = cursor.readRest().trim();
1472
1264
  const parsedShape = parseShapeValue(id, value);
1473
1265
  if (!parsedShape)
1474
1266
  return null;
1475
-
1476
1267
  return {
1477
1268
  ...parsedShape,
1478
1269
  sourceRange: sourceStart == null ? undefined : { start: sourceStart, length: line.length },
1479
1270
  };
1480
1271
  }
1481
-
1482
- function parseMindmapNode(line: string, index: number, parentId: string, sourceStart: number, sourceLength: number, indent: number): ParsedMindmapNode {
1272
+ function parseMindmapNode(line, index, parentId, sourceStart, sourceLength, indent) {
1483
1273
  const parsedNode = parseNode(line);
1484
1274
  if (parsedNode) {
1485
1275
  return {
@@ -1491,7 +1281,6 @@ function parseMindmapNode(line: string, index: number, parentId: string, sourceS
1491
1281
  sourceRange: { start: sourceStart, length: sourceLength },
1492
1282
  };
1493
1283
  }
1494
-
1495
1284
  const id = sanitizeMindmapId(line, index);
1496
1285
  return {
1497
1286
  id,
@@ -1502,20 +1291,16 @@ function parseMindmapNode(line: string, index: number, parentId: string, sourceS
1502
1291
  sourceRange: { start: sourceStart, length: sourceLength },
1503
1292
  };
1504
1293
  }
1505
-
1506
- function parseEdge(line: string, sourceStart: number): ParsedEdge & { fromNode: ParsedNode; toNode: ParsedNode } {
1294
+ function parseEdge(line, sourceStart) {
1507
1295
  const parsed = parseFlowchartEdgeStatement(line);
1508
1296
  if (!parsed)
1509
1297
  return null;
1510
-
1511
1298
  const fromNode = parseNodeToken(parsed.left);
1512
1299
  const toNode = parseNodeToken(parsed.right);
1513
1300
  if (!fromNode || !toNode)
1514
1301
  return null;
1515
-
1516
1302
  fromNode.sourceRange = { start: sourceStart + parsed.leftStart + parsed.left.indexOf(fromNode.id), length: fromNode.id.length };
1517
1303
  toNode.sourceRange = { start: sourceStart + parsed.rightStart + parsed.right.indexOf(toNode.id), length: toNode.id.length };
1518
-
1519
1304
  return {
1520
1305
  from: fromNode.id,
1521
1306
  to: toNode.id,
@@ -1529,32 +1314,25 @@ function parseEdge(line: string, sourceStart: number): ParsedEdge & { fromNode:
1529
1314
  sourceRange: { start: sourceStart, length: line.length },
1530
1315
  };
1531
1316
  }
1532
-
1533
- function parseNodeToken(token: string): ParsedNode {
1317
+ function parseNodeToken(token) {
1534
1318
  const node = parseNode(token);
1535
1319
  if (node)
1536
1320
  return node;
1537
-
1538
1321
  const id = new SourceCursor(token).readIdentifier();
1539
1322
  return id ? { id, label: id, shape: "rectangle" } : null;
1540
1323
  }
1541
-
1542
- function parseShapeValue(id: string, value: string): ParsedNode {
1324
+ function parseShapeValue(id, value) {
1543
1325
  if (!value)
1544
1326
  return { id, label: id, shape: "rectangle" };
1545
-
1546
1327
  const expandedShape = parseExpandedShape(value);
1547
1328
  if (expandedShape)
1548
1329
  return { id, label: expandedShape.label ?? id, shape: mapExpandedShape(expandedShape.shape) };
1549
-
1550
1330
  const shapeValue = parseDelimitedShapeValue(value);
1551
1331
  if (shapeValue)
1552
1332
  return { id, label: unquoteLabel(shapeValue.label), shape: shapeValue.shape };
1553
-
1554
1333
  return null;
1555
1334
  }
1556
-
1557
- function getEdgeType(connector: string) {
1335
+ function getEdgeType(connector) {
1558
1336
  if (connector.includes("~"))
1559
1337
  return "invisible";
1560
1338
  if (connector.includes("o"))
@@ -1571,8 +1349,7 @@ function getEdgeType(connector: string) {
1571
1349
  return "open";
1572
1350
  return "arrow";
1573
1351
  }
1574
-
1575
- function unquoteLabel(label: string) {
1352
+ function unquoteLabel(label) {
1576
1353
  let result = label.trim();
1577
1354
  if (result.length >= 2) {
1578
1355
  const first = result[0];
@@ -1582,8 +1359,7 @@ function unquoteLabel(label: string) {
1582
1359
  }
1583
1360
  return normalizeLabelLineBreaks(result);
1584
1361
  }
1585
-
1586
- function mapExpandedShape(shape: string) {
1362
+ function mapExpandedShape(shape) {
1587
1363
  switch (shape) {
1588
1364
  case "rect":
1589
1365
  case "proc":
@@ -1763,8 +1539,7 @@ function mapExpandedShape(shape: string) {
1763
1539
  return "rectangle";
1764
1540
  }
1765
1541
  }
1766
-
1767
- function mapMindmapShape(shape: string) {
1542
+ function mapMindmapShape(shape) {
1768
1543
  switch (shape) {
1769
1544
  case "rectangle":
1770
1545
  return "square";
@@ -1776,8 +1551,7 @@ function mapMindmapShape(shape: string) {
1776
1551
  return "default";
1777
1552
  }
1778
1553
  }
1779
-
1780
- function sanitizeMindmapId(label: string, index: number) {
1554
+ function sanitizeMindmapId(label, index) {
1781
1555
  let result = "";
1782
1556
  for (const char of label) {
1783
1557
  if (isIdentifierPart(char))
@@ -1788,8 +1562,7 @@ function sanitizeMindmapId(label: string, index: number) {
1788
1562
  result = result.trim();
1789
1563
  return result || "mindmap_" + index;
1790
1564
  }
1791
-
1792
- function sanitizeDirectiveId(label: string) {
1565
+ function sanitizeDirectiveId(label) {
1793
1566
  let result = "";
1794
1567
  for (const char of label) {
1795
1568
  if (isIdentifierPart(char))
@@ -1799,8 +1572,7 @@ function sanitizeDirectiveId(label: string) {
1799
1572
  }
1800
1573
  return result || "subgraph";
1801
1574
  }
1802
-
1803
- function getDefaultMindmapNodeSize(shape: string) {
1575
+ function getDefaultMindmapNodeSize(shape) {
1804
1576
  switch (shape) {
1805
1577
  case "circle":
1806
1578
  case "bang":
@@ -1811,14 +1583,12 @@ function getDefaultMindmapNodeSize(shape: string) {
1811
1583
  return { width: 150, height: 44 };
1812
1584
  }
1813
1585
  }
1814
-
1815
- function getDefaultRequirementNodeSize(node: ParsedRequirementNode) {
1586
+ function getDefaultRequirementNodeSize(node) {
1816
1587
  if (node.kind === "element")
1817
1588
  return { width: 170, height: node.docRef ? 108 : 84 };
1818
1589
  return { width: 220, height: 160 };
1819
1590
  }
1820
-
1821
- function getDefaultNodeSize(shape: string) {
1591
+ function getDefaultNodeSize(shape) {
1822
1592
  switch (shape) {
1823
1593
  case "decision":
1824
1594
  return { width: 120, height: 80 };
@@ -1844,8 +1614,7 @@ function getDefaultNodeSize(shape: string) {
1844
1614
  return { width: 120, height: 54 };
1845
1615
  }
1846
1616
  }
1847
-
1848
- function getFallbackRequirementNodePosition(index: number, total: number, direction: FlowchartDirection) {
1617
+ function getFallbackRequirementNodePosition(index, total, direction) {
1849
1618
  const columns = direction === "LR" || direction === "RL" ? Math.max(1, Math.ceil(total / 3)) : 3;
1850
1619
  const column = index % columns;
1851
1620
  const row = Math.floor(index / columns);
@@ -1857,8 +1626,7 @@ function getFallbackRequirementNodePosition(index: number, total: number, direct
1857
1626
  return { left, top: 40 + (Math.ceil(total / columns) - row - 1) * 230 };
1858
1627
  return { left, top };
1859
1628
  }
1860
-
1861
- function getFallbackFlowchartNodePosition(index: number, count: number, direction: FlowchartDirection) {
1629
+ function getFallbackFlowchartNodePosition(index, count, direction) {
1862
1630
  const xStep = 180;
1863
1631
  const yStep = 110;
1864
1632
  switch (direction) {
@@ -1874,12 +1642,10 @@ function getFallbackFlowchartNodePosition(index: number, count: number, directio
1874
1642
  return { left: 40, top: 40 + index * yStep };
1875
1643
  }
1876
1644
  }
1877
-
1878
- function readMermaidDocument(text: string): ParsedMermaidDocument {
1645
+ function readMermaidDocument(text) {
1879
1646
  const lines = getLineRecords(text);
1880
1647
  if (lines[0]?.trimmed !== "---")
1881
1648
  return { lines, title: "", frontmatter: "" };
1882
-
1883
1649
  let frontmatterEnd = -1;
1884
1650
  for (let i = 1; i < lines.length; i++) {
1885
1651
  if (lines[i].trimmed === "---") {
@@ -1887,20 +1653,16 @@ function readMermaidDocument(text: string): ParsedMermaidDocument {
1887
1653
  break;
1888
1654
  }
1889
1655
  }
1890
-
1891
1656
  if (frontmatterEnd < 0)
1892
1657
  return { lines, title: "", frontmatter: "" };
1893
-
1894
1658
  const frontmatterLines = lines.slice(1, frontmatterEnd);
1895
-
1896
1659
  return {
1897
1660
  lines: lines.filter((_, index) => index > frontmatterEnd),
1898
1661
  title: readFrontmatterTitle(frontmatterLines),
1899
1662
  frontmatter: readFrontmatterWithoutTitle(frontmatterLines),
1900
1663
  };
1901
1664
  }
1902
-
1903
- function readFrontmatterTitle(lines: LineRecord[]) {
1665
+ function readFrontmatterTitle(lines) {
1904
1666
  for (const line of lines) {
1905
1667
  const separator = line.trimmed.indexOf(":");
1906
1668
  if (separator < 0)
@@ -1911,9 +1673,8 @@ function readFrontmatterTitle(lines: LineRecord[]) {
1911
1673
  }
1912
1674
  return "";
1913
1675
  }
1914
-
1915
- function readFrontmatterWithoutTitle(lines: LineRecord[]) {
1916
- const remainingLines: string[] = [];
1676
+ function readFrontmatterWithoutTitle(lines) {
1677
+ const remainingLines = [];
1917
1678
  for (let i = 0; i < lines.length; i++) {
1918
1679
  const line = lines[i];
1919
1680
  const separator = line.trimmed.indexOf(":");
@@ -1924,8 +1685,7 @@ function readFrontmatterWithoutTitle(lines: LineRecord[]) {
1924
1685
  }
1925
1686
  return trimBlankLines(remainingLines).join("\n");
1926
1687
  }
1927
-
1928
- function trimBlankLines(lines: string[]) {
1688
+ function trimBlankLines(lines) {
1929
1689
  let start = 0;
1930
1690
  let end = lines.length;
1931
1691
  while (start < end && !lines[start].trim())
@@ -1934,9 +1694,8 @@ function trimBlankLines(lines: string[]) {
1934
1694
  end--;
1935
1695
  return lines.slice(start, end);
1936
1696
  }
1937
-
1938
- function getLineRecords(text: string) {
1939
- const records: LineRecord[] = [];
1697
+ function getLineRecords(text) {
1698
+ const records = [];
1940
1699
  let offset = 0;
1941
1700
  for (const line of splitLines(text)) {
1942
1701
  const leadingWhitespaceLength = countLeadingWhitespace(line);
@@ -1945,9 +1704,8 @@ function getLineRecords(text: string) {
1945
1704
  }
1946
1705
  return records;
1947
1706
  }
1948
-
1949
- function splitLines(text: string) {
1950
- const lines: string[] = [];
1707
+ function splitLines(text) {
1708
+ const lines = [];
1951
1709
  let start = 0;
1952
1710
  for (let i = 0; i < text.length; i++) {
1953
1711
  if (text[i] === "\n") {
@@ -1961,13 +1719,11 @@ function splitLines(text: string) {
1961
1719
  lines.push(text.substring(start));
1962
1720
  return lines;
1963
1721
  }
1964
-
1965
- function getDiagramDirection(designItems: IDesignItem[]) {
1722
+ function getDiagramDirection(designItems) {
1966
1723
  const rootDesignItem = getRootDesignItem(designItems);
1967
1724
  const rootDirection = rootDesignItem?.getAttribute(mermaidFlowchartDirectionAttribute);
1968
1725
  if (isFlowchartDirection(rootDirection))
1969
1726
  return rootDirection;
1970
-
1971
1727
  for (const designItem of designItems) {
1972
1728
  const direction = designItem.element.getAttribute("diagram-direction");
1973
1729
  if (isFlowchartDirection(direction))
@@ -1975,41 +1731,33 @@ function getDiagramDirection(designItems: IDesignItem[]) {
1975
1731
  }
1976
1732
  return inferDiagramDirection(designItems);
1977
1733
  }
1978
-
1979
- function inferDiagramDirection(designItems: IDesignItem[]): FlowchartDirection {
1734
+ function inferDiagramDirection(designItems) {
1980
1735
  const nodes = designItems
1981
1736
  .filter(designItem => designItem.element.localName === "mermaid-node")
1982
1737
  .map(designItem => {
1983
- const element = designItem.element as HTMLElement;
1984
- return {
1985
- left: Number.parseFloat(element.style.left || "0"),
1986
- top: Number.parseFloat(element.style.top || "0"),
1987
- };
1988
- });
1989
-
1738
+ const element = designItem.element;
1739
+ return {
1740
+ left: Number.parseFloat(element.style.left || "0"),
1741
+ top: Number.parseFloat(element.style.top || "0"),
1742
+ };
1743
+ });
1990
1744
  if (nodes.length < 2)
1991
1745
  return "TD";
1992
-
1993
1746
  const first = nodes[0];
1994
1747
  const last = nodes[nodes.length - 1];
1995
1748
  const dx = last.left - first.left;
1996
1749
  const dy = last.top - first.top;
1997
-
1998
1750
  if (Math.abs(dx) > Math.abs(dy))
1999
1751
  return dx >= 0 ? "LR" : "RL";
2000
-
2001
1752
  return dy >= 0 ? "TD" : "BT";
2002
1753
  }
2003
-
2004
- function isFlowchartDirection(value: string): value is FlowchartDirection {
1754
+ function isFlowchartDirection(value) {
2005
1755
  return value === "TB" || value === "TD" || value === "BT" || value === "RL" || value === "LR";
2006
1756
  }
2007
-
2008
- function isRequirementDirection(value: string): value is FlowchartDirection {
1757
+ function isRequirementDirection(value) {
2009
1758
  return value === "TB" || value === "BT" || value === "RL" || value === "LR";
2010
1759
  }
2011
-
2012
- function setMermaidDocumentAttributes(instanceServiceContainer: InstanceServiceContainer, diagramType: MermaidDocumentDiagramType, direction?: FlowchartDirection, title?: string, frontmatter?: string) {
1760
+ function setMermaidDocumentAttributes(instanceServiceContainer, diagramType, direction, title, frontmatter) {
2013
1761
  const rootDesignItem = instanceServiceContainer?.designerCanvas?.rootDesignItem;
2014
1762
  rootDesignItem?._withoutUndoSetAttribute(mermaidDiagramTypeAttribute, diagramType);
2015
1763
  if (direction)
@@ -2025,8 +1773,7 @@ function setMermaidDocumentAttributes(instanceServiceContainer: InstanceServiceC
2025
1773
  else
2026
1774
  rootDesignItem?._withoutUndoRemoveAttribute(mermaidFrontmatterAttribute);
2027
1775
  }
2028
-
2029
- function writeMermaidFrontmatter(textWriter: ITextWriter, designItems: IDesignItem[], rootDesignItem?: IDesignItem) {
1776
+ function writeMermaidFrontmatter(textWriter, designItems, rootDesignItem) {
2030
1777
  const root = rootDesignItem ?? getRootDesignItem(designItems);
2031
1778
  const title = getMermaidDocumentTitle(root);
2032
1779
  const frontmatter = getMermaidDocumentFrontmatter(root);
@@ -2041,15 +1788,13 @@ function writeMermaidFrontmatter(textWriter: ITextWriter, designItems: IDesignIt
2041
1788
  }
2042
1789
  textWriter.writeLine("---");
2043
1790
  }
2044
-
2045
- function getWritableDocumentDiagramType(designItems: IDesignItem[]): MermaidDocumentDiagramType {
1791
+ function getWritableDocumentDiagramType(designItems) {
2046
1792
  const hasSequenceItems = designItems.some(designItem => designItem.element.localName === MermaidSequenceParticipant.is || designItem.element.localName === MermaidSequenceMessage.is);
2047
1793
  const hasFlowchartItems = designItems.some(designItem => designItem.element.localName === MermaidNode.is || designItem.element.localName === MermaidEdge.is);
2048
1794
  const hasMindmapItems = designItems.some(designItem => designItem.element.localName === MermaidMindmapNode.is);
2049
1795
  const hasRequirementItems = designItems.some(designItem => designItem.element.localName === MermaidRequirementNode.is || designItem.element.localName === MermaidRequirementRelationship.is);
2050
1796
  const rootDesignItem = getRootDesignItem(designItems);
2051
1797
  const documentType = rootDesignItem ? getMermaidDocumentDiagramType(rootDesignItem) : null;
2052
-
2053
1798
  if (documentType === "requirementDiagram" && !hasFlowchartItems && !hasSequenceItems && !hasMindmapItems)
2054
1799
  return "requirementDiagram";
2055
1800
  if (documentType === "mindmap" && !hasFlowchartItems && !hasSequenceItems && !hasRequirementItems)
@@ -2066,71 +1811,60 @@ function getWritableDocumentDiagramType(designItems: IDesignItem[]): MermaidDocu
2066
1811
  return "sequenceDiagram";
2067
1812
  return "flowchart";
2068
1813
  }
2069
-
2070
- function getRootDesignItem(designItems: IDesignItem[]) {
1814
+ function getRootDesignItem(designItems) {
2071
1815
  return designItems[0]?.isRootItem ? designItems[0] : designItems[0]?.instanceServiceContainer?.designerCanvas?.rootDesignItem;
2072
1816
  }
2073
-
2074
- function getMermaidDesignItems(designItems: IDesignItem[]) {
1817
+ function getMermaidDesignItems(designItems) {
2075
1818
  if (designItems[0]?.isRootItem)
2076
1819
  return Array.from(designItems[0].children());
2077
1820
  return designItems;
2078
1821
  }
2079
-
2080
- function getMindmapSortValue(designItem: IDesignItem) {
2081
- const element = designItem.element as HTMLElement;
1822
+ function getMindmapSortValue(designItem) {
1823
+ const element = designItem.element;
2082
1824
  const top = Number.parseFloat(element.style.top || "0");
2083
1825
  const left = Number.parseFloat(element.style.left || "0");
2084
1826
  return top * 10000 + left;
2085
1827
  }
2086
-
2087
- function sortMindmapItems(items: IDesignItem[]) {
1828
+ function sortMindmapItems(items) {
2088
1829
  return [...items].sort((a, b) => getMindmapSortValue(a) - getMindmapSortValue(b));
2089
1830
  }
2090
-
2091
- function repeatText(value: string, count: number) {
1831
+ function repeatText(value, count) {
2092
1832
  let result = "";
2093
1833
  for (let i = 0; i < count; i++)
2094
1834
  result += value;
2095
1835
  return result;
2096
1836
  }
2097
-
2098
- function writeDesignItemLine(textWriter: ITextWriter, designItem: IDesignItem, line: string, updatePositions?: boolean) {
1837
+ function writeDesignItemLine(textWriter, designItem, line, updatePositions) {
2099
1838
  const start = textWriter.position;
2100
1839
  line = indentEmbeddedNewlines(line);
2101
1840
  textWriter.writeLine(line);
2102
1841
  if (updatePositions)
2103
1842
  designItem.instanceServiceContainer.designItemDocumentPositionService?.setPosition(designItem, { start, length: line.length });
2104
1843
  }
2105
-
2106
- function indentEmbeddedNewlines(line: string) {
1844
+ function indentEmbeddedNewlines(line) {
2107
1845
  const newlineIndex = line.indexOf("\n");
2108
1846
  if (newlineIndex < 0)
2109
1847
  return line;
2110
1848
  const indent = line.substring(0, countLeadingWhitespace(line));
2111
1849
  return line.replaceAll("\n", "\n" + indent);
2112
1850
  }
2113
-
2114
- async function getMermaidLayout(code: string, nodes: Map<string, ParsedNode>) {
1851
+ async function getMermaidLayout(code, nodes) {
2115
1852
  if (typeof document === "undefined" || typeof DOMParser === "undefined")
2116
1853
  return null;
2117
-
2118
1854
  try {
2119
1855
  const mermaid = await import("mermaid");
2120
1856
  const id = "mermaid-layout-" + Math.random().toString(36).slice(2);
2121
1857
  mermaid.default.initialize({ startOnLoad: false, securityLevel: "strict" });
2122
1858
  const result = await mermaid.default.render(id, code);
2123
1859
  const doc = new DOMParser().parseFromString(result.svg, "image/svg+xml");
2124
- const layoutNodes = new Map<string, { x: number; y: number; width: number; height: number }>();
2125
- const layoutSubgraphs = new Map<string, { x: number; y: number; width: number; height: number }>();
1860
+ const layoutNodes = new Map();
1861
+ const layoutSubgraphs = new Map();
2126
1862
  let minX = Number.POSITIVE_INFINITY;
2127
1863
  let minY = Number.POSITIVE_INFINITY;
2128
-
2129
1864
  for (const nodeElement of Array.from(doc.querySelectorAll("g.node"))) {
2130
1865
  const nodeId = nodeElement.getAttribute("data-id") ?? getFlowchartNodeIdFromDomId(nodeElement.id);
2131
1866
  if (!nodeId || !nodes.has(nodeId))
2132
1867
  continue;
2133
-
2134
1868
  const center = parseTranslate(nodeElement.getAttribute("transform"));
2135
1869
  const size = getSvgNodeSize(nodeElement);
2136
1870
  const layout = {
@@ -2143,7 +1877,6 @@ async function getMermaidLayout(code: string, nodes: Map<string, ParsedNode>) {
2143
1877
  minX = Math.min(minX, layout.x);
2144
1878
  minY = Math.min(minY, layout.y);
2145
1879
  }
2146
-
2147
1880
  for (const clusterElement of Array.from(doc.querySelectorAll("g.cluster"))) {
2148
1881
  const clusterId = getClusterIdFromDomId(clusterElement.id, id);
2149
1882
  if (!clusterId)
@@ -2163,10 +1896,8 @@ async function getMermaidLayout(code: string, nodes: Map<string, ParsedNode>) {
2163
1896
  minX = Math.min(minX, layout.x);
2164
1897
  minY = Math.min(minY, layout.y);
2165
1898
  }
2166
-
2167
1899
  if (!layoutNodes.size && !layoutSubgraphs.size)
2168
1900
  return null;
2169
-
2170
1901
  const offsetX = 40 - minX;
2171
1902
  const offsetY = 40 - minY;
2172
1903
  for (const layout of layoutNodes.values()) {
@@ -2177,27 +1908,24 @@ async function getMermaidLayout(code: string, nodes: Map<string, ParsedNode>) {
2177
1908
  layout.x += offsetX;
2178
1909
  layout.y += offsetY;
2179
1910
  }
2180
-
2181
1911
  return { nodes: layoutNodes, subgraphs: layoutSubgraphs };
2182
- } catch {
1912
+ }
1913
+ catch {
2183
1914
  return null;
2184
1915
  }
2185
1916
  }
2186
-
2187
- async function getMermaidRequirementLayout(code: string, nodes: Map<string, ParsedRequirementNode>) {
1917
+ async function getMermaidRequirementLayout(code, nodes) {
2188
1918
  if (typeof document === "undefined" || typeof DOMParser === "undefined")
2189
1919
  return null;
2190
-
2191
1920
  try {
2192
1921
  const mermaid = await import("mermaid");
2193
1922
  const id = "mermaid-requirement-layout-" + Math.random().toString(36).slice(2);
2194
1923
  mermaid.default.initialize({ startOnLoad: false, securityLevel: "strict" });
2195
1924
  const result = await mermaid.default.render(id, code);
2196
1925
  const doc = new DOMParser().parseFromString(result.svg, "image/svg+xml");
2197
- const layoutNodes = new Map<string, { x: number; y: number; width: number; height: number }>();
1926
+ const layoutNodes = new Map();
2198
1927
  let minX = Number.POSITIVE_INFINITY;
2199
1928
  let minY = Number.POSITIVE_INFINITY;
2200
-
2201
1929
  for (const nodeElement of Array.from(doc.querySelectorAll("g.node"))) {
2202
1930
  const nodeId = getRequirementNodeIdFromSvgId(nodeElement.id, id, nodes);
2203
1931
  const node = nodeId ? nodes.get(nodeId) : null;
@@ -2215,45 +1943,39 @@ async function getMermaidRequirementLayout(code: string, nodes: Map<string, Pars
2215
1943
  minX = Math.min(minX, layout.x);
2216
1944
  minY = Math.min(minY, layout.y);
2217
1945
  }
2218
-
2219
1946
  if (!layoutNodes.size)
2220
1947
  return null;
2221
-
2222
1948
  const offsetX = 40 - minX;
2223
1949
  const offsetY = 40 - minY;
2224
1950
  for (const layout of layoutNodes.values()) {
2225
1951
  layout.x += offsetX;
2226
1952
  layout.y += offsetY;
2227
1953
  }
2228
-
2229
1954
  return { nodes: layoutNodes };
2230
- } catch {
1955
+ }
1956
+ catch {
2231
1957
  return null;
2232
1958
  }
2233
1959
  }
2234
-
2235
- async function getMermaidMindmapLayout(code: string, nodes: ParsedMindmapNode[]) {
1960
+ async function getMermaidMindmapLayout(code, nodes) {
2236
1961
  if (typeof document === "undefined" || typeof DOMParser === "undefined")
2237
1962
  return null;
2238
-
2239
1963
  try {
2240
1964
  const mermaid = await import("mermaid");
2241
1965
  const id = "mermaid-mindmap-layout-" + Math.random().toString(36).slice(2);
2242
1966
  mermaid.default.initialize({ startOnLoad: false, securityLevel: "strict" });
2243
1967
  const result = await mermaid.default.render(id, code);
2244
1968
  const doc = new DOMParser().parseFromString(result.svg, "image/svg+xml");
2245
- const layoutNodes = new Map<string, { x: number; y: number; width: number; height: number }>();
1969
+ const layoutNodes = new Map();
2246
1970
  const candidateElements = Array.from(doc.querySelectorAll("g.node.mindmap-node"));
2247
1971
  let minX = Number.POSITIVE_INFINITY;
2248
1972
  let minY = Number.POSITIVE_INFINITY;
2249
1973
  let nextNodeIndex = 0;
2250
-
2251
1974
  for (const nodeElement of candidateElements) {
2252
1975
  if (nextNodeIndex >= nodes.length)
2253
1976
  break;
2254
1977
  if (!nodeElement.querySelector("text, foreignObject"))
2255
1978
  continue;
2256
-
2257
1979
  const node = nodes[nextNodeIndex++];
2258
1980
  const center = parseTranslateFromElementTree(nodeElement);
2259
1981
  const size = getMindmapWidgetSize(getSvgNodeSize(nodeElement), node.shape);
@@ -2267,27 +1989,24 @@ async function getMermaidMindmapLayout(code: string, nodes: ParsedMindmapNode[])
2267
1989
  minX = Math.min(minX, layout.x);
2268
1990
  minY = Math.min(minY, layout.y);
2269
1991
  }
2270
-
2271
1992
  if (!layoutNodes.size)
2272
1993
  return null;
2273
-
2274
1994
  const offsetX = 40 - minX;
2275
1995
  const offsetY = 40 - minY;
2276
1996
  for (const layout of layoutNodes.values()) {
2277
1997
  layout.x += offsetX;
2278
1998
  layout.y += offsetY;
2279
1999
  }
2280
-
2281
2000
  return { nodes: layoutNodes };
2282
- } catch {
2001
+ }
2002
+ catch {
2283
2003
  return null;
2284
2004
  }
2285
2005
  }
2286
-
2287
- function parseTranslateFromElementTree(element: Element) {
2006
+ function parseTranslateFromElementTree(element) {
2288
2007
  let x = 0;
2289
2008
  let y = 0;
2290
- let current: Element = element;
2009
+ let current = element;
2291
2010
  while (current) {
2292
2011
  const translate = parseTranslate(current.getAttribute("transform"));
2293
2012
  x += translate.x;
@@ -2296,16 +2015,14 @@ function parseTranslateFromElementTree(element: Element) {
2296
2015
  }
2297
2016
  return { x, y };
2298
2017
  }
2299
-
2300
- function parseTranslate(transform: string) {
2018
+ function parseTranslate(transform) {
2301
2019
  const values = parseTranslateValues(transform);
2302
2020
  return {
2303
2021
  x: values.x,
2304
2022
  y: values.y,
2305
2023
  };
2306
2024
  }
2307
-
2308
- function getFlowchartNodeIdFromDomId(domId: string) {
2025
+ function getFlowchartNodeIdFromDomId(domId) {
2309
2026
  const prefix = "flowchart-";
2310
2027
  if (!domId?.startsWith(prefix))
2311
2028
  return "";
@@ -2316,8 +2033,7 @@ function getFlowchartNodeIdFromDomId(domId: string) {
2316
2033
  const suffix = withoutPrefix.substring(lastDash + 1);
2317
2034
  return isAllDigits(suffix) ? withoutPrefix.substring(0, lastDash) : withoutPrefix;
2318
2035
  }
2319
-
2320
- function getRequirementNodeIdFromSvgId(svgId: string, renderId: string, nodes: Map<string, ParsedRequirementNode>) {
2036
+ function getRequirementNodeIdFromSvgId(svgId, renderId, nodes) {
2321
2037
  if (!svgId)
2322
2038
  return "";
2323
2039
  const prefix = renderId + "-";
@@ -2333,8 +2049,7 @@ function getRequirementNodeIdFromSvgId(svgId: string, renderId: string, nodes: M
2333
2049
  }
2334
2050
  return match;
2335
2051
  }
2336
-
2337
- function getClusterIdFromDomId(domId: string, renderId: string) {
2052
+ function getClusterIdFromDomId(domId, renderId) {
2338
2053
  if (!domId)
2339
2054
  return "";
2340
2055
  const prefixes = [renderId + "_flowchart-", "flowchart-", renderId + "-", "cluster-"];
@@ -2344,8 +2059,7 @@ function getClusterIdFromDomId(domId: string, renderId: string) {
2344
2059
  }
2345
2060
  return domId;
2346
2061
  }
2347
-
2348
- function parseTranslateValues(transform: string) {
2062
+ function parseTranslateValues(transform) {
2349
2063
  const result = { x: 0, y: 0 };
2350
2064
  if (!transform)
2351
2065
  return result;
@@ -2364,8 +2078,7 @@ function parseTranslateValues(transform: string) {
2364
2078
  result.y = Number.parseFloat(content.substring(separator + 1).trim()) || 0;
2365
2079
  return result;
2366
2080
  }
2367
-
2368
- function findTopLevelColon(text: string) {
2081
+ function findTopLevelColon(text) {
2369
2082
  let quote = "";
2370
2083
  for (let i = 0; i < text.length; i++) {
2371
2084
  const char = text[i];
@@ -2383,8 +2096,7 @@ function findTopLevelColon(text: string) {
2383
2096
  }
2384
2097
  return -1;
2385
2098
  }
2386
-
2387
- function isRequirementType(value: string) {
2099
+ function isRequirementType(value) {
2388
2100
  return equalsIgnoreCase(value, "requirement")
2389
2101
  || equalsIgnoreCase(value, "functionalRequirement")
2390
2102
  || equalsIgnoreCase(value, "interfaceRequirement")
@@ -2392,8 +2104,7 @@ function isRequirementType(value: string) {
2392
2104
  || equalsIgnoreCase(value, "physicalRequirement")
2393
2105
  || equalsIgnoreCase(value, "designConstraint");
2394
2106
  }
2395
-
2396
- function isRequirementRelationshipType(value: string) {
2107
+ function isRequirementRelationshipType(value) {
2397
2108
  return equalsIgnoreCase(value, "contains")
2398
2109
  || equalsIgnoreCase(value, "copies")
2399
2110
  || equalsIgnoreCase(value, "derives")
@@ -2402,8 +2113,7 @@ function isRequirementRelationshipType(value: string) {
2402
2113
  || equalsIgnoreCase(value, "refines")
2403
2114
  || equalsIgnoreCase(value, "traces");
2404
2115
  }
2405
-
2406
- function isAllDigits(value: string) {
2116
+ function isAllDigits(value) {
2407
2117
  if (!value)
2408
2118
  return false;
2409
2119
  for (const char of value) {
@@ -2412,8 +2122,7 @@ function isAllDigits(value: string) {
2412
2122
  }
2413
2123
  return true;
2414
2124
  }
2415
-
2416
- function getSvgNodeSize(nodeElement: Element) {
2125
+ function getSvgNodeSize(nodeElement) {
2417
2126
  const foreignObject = nodeElement.querySelector("foreignObject");
2418
2127
  if (foreignObject) {
2419
2128
  const width = Number.parseFloat(foreignObject.getAttribute("width") ?? "");
@@ -2421,7 +2130,6 @@ function getSvgNodeSize(nodeElement: Element) {
2421
2130
  if (Number.isFinite(width) && Number.isFinite(height))
2422
2131
  return { width, height };
2423
2132
  }
2424
-
2425
2133
  const rect = nodeElement.querySelector("rect");
2426
2134
  if (rect) {
2427
2135
  const width = Number.parseFloat(rect.getAttribute("width") ?? "");
@@ -2429,7 +2137,6 @@ function getSvgNodeSize(nodeElement: Element) {
2429
2137
  if (Number.isFinite(width) && Number.isFinite(height))
2430
2138
  return { width, height };
2431
2139
  }
2432
-
2433
2140
  const polygon = nodeElement.querySelector("polygon");
2434
2141
  if (polygon) {
2435
2142
  const points = splitWhitespace(polygon.getAttribute("points") ?? "").map(point => {
@@ -2442,20 +2149,17 @@ function getSvgNodeSize(nodeElement: Element) {
2442
2149
  return { width: Math.max(...xs) - Math.min(...xs), height: Math.max(...ys) - Math.min(...ys) };
2443
2150
  }
2444
2151
  }
2445
-
2446
2152
  return { width: 120, height: 54 };
2447
2153
  }
2448
-
2449
- function getMindmapWidgetSize(svgSize: { width: number; height: number }, shape: string) {
2154
+ function getMindmapWidgetSize(svgSize, shape) {
2450
2155
  const defaultSize = getDefaultMindmapNodeSize(shape);
2451
2156
  return {
2452
2157
  width: Math.max(svgSize.width, defaultSize.width),
2453
2158
  height: Math.max(svgSize.height, defaultSize.height),
2454
2159
  };
2455
2160
  }
2456
-
2457
- function splitWhitespace(text: string) {
2458
- const parts: string[] = [];
2161
+ function splitWhitespace(text) {
2162
+ const parts = [];
2459
2163
  let start = -1;
2460
2164
  for (let i = 0; i < text.length; i++) {
2461
2165
  if (isWhitespace(text[i])) {
@@ -2463,7 +2167,8 @@ function splitWhitespace(text: string) {
2463
2167
  parts.push(text.substring(start, i));
2464
2168
  start = -1;
2465
2169
  }
2466
- } else if (start < 0) {
2170
+ }
2171
+ else if (start < 0) {
2467
2172
  start = i;
2468
2173
  }
2469
2174
  }