feat(architecture): Add ids in generated SVG

This commit is contained in:
quilicicf
2025-09-06 14:15:48 +02:00
parent 3840451fda
commit 2812a0d12a
3 changed files with 62 additions and 5 deletions

View File

@@ -0,0 +1,5 @@
---
'mermaid': minor
---
Add IDs in architecture diagrams

View File

@@ -0,0 +1,48 @@
import { describe } from 'vitest';
import { draw } from './architectureRenderer.js';
import { Diagram } from '../../Diagram.js';
import { addDetector } from '../../diagram-api/detectType.js';
import architectureDetector from './architectureDetector.js';
import { ensureNodeFromSelector, jsdomIt } from '../../tests/util.js';
const { id, detector, loader } = architectureDetector;
addDetector(id, detector, loader); // Add architecture schemas to Mermaid
describe('architecture diagram SVGs', () => {
jsdomIt('should add ids', async () => {
const svgNode = await drawDiagram(`
architecture-beta
group api(cloud)[API]
service db(database)[Database] in api
service disk1(disk)[Storage] in api
service disk2(disk)[Storage] in api
service server(server)[Server] in api
db:L -- R:server
disk1:T -- B:server
disk2:T -- B:db
`);
const nodesForGroup = svgNode.querySelectorAll(`#group-api`);
expect(nodesForGroup.length).toBe(1);
const serviceIds = [...svgNode.querySelectorAll(`[id^=service-]`)].map(({ id }) => id).sort();
expect(serviceIds).toStrictEqual([
'service-db',
'service-disk1',
'service-disk2',
'service-server',
]);
const edgeIds = [...svgNode.querySelectorAll(`.edge[id^=L_]`)].map(({ id }) => id).sort();
expect(edgeIds).toStrictEqual(['L_db_server_0', 'L_disk1_server_0', 'L_disk2_db_0']);
});
});
async function drawDiagram(diagramText: string): Promise<Element> {
const diagram = await Diagram.fromText(diagramText, {});
await draw('NOT_USED', 'svg', '1.0.0', diagram);
return ensureNodeFromSelector('#svg');
}

View File

@@ -20,6 +20,7 @@ import {
type ArchitectureJunction, type ArchitectureJunction,
type ArchitectureService, type ArchitectureService,
} from './architectureTypes.js'; } from './architectureTypes.js';
import { getEdgeId } from '../../utils.js';
export const drawEdges = async function ( export const drawEdges = async function (
edgesEl: D3Element, edgesEl: D3Element,
@@ -91,7 +92,8 @@ export const drawEdges = async function (
g.insert('path') g.insert('path')
.attr('d', `M ${startX},${startY} L ${midX},${midY} L${endX},${endY} `) .attr('d', `M ${startX},${startY} L ${midX},${midY} L${endX},${endY} `)
.attr('class', 'edge'); .attr('class', 'edge')
.attr('id', getEdgeId(source, target, { prefix: 'L' }));
if (sourceArrow) { if (sourceArrow) {
const xShift = isArchitectureDirectionX(sourceDir) const xShift = isArchitectureDirectionX(sourceDir)
@@ -206,8 +208,9 @@ export const drawGroups = async function (
if (data.type === 'group') { if (data.type === 'group') {
const { h, w, x1, y1 } = node.boundingBox(); const { h, w, x1, y1 } = node.boundingBox();
groupsEl const groupsNode = groupsEl.append('rect');
.append('rect') groupsNode
.attr('id', `group-${data.id}`)
.attr('x', x1 + halfIconSize) .attr('x', x1 + halfIconSize)
.attr('y', y1 + halfIconSize) .attr('y', y1 + halfIconSize)
.attr('width', w) .attr('width', w)
@@ -262,6 +265,7 @@ export const drawGroups = async function (
')' ')'
); );
} }
db.setElementForId(data.id, groupsNode);
} }
}) })
); );
@@ -342,9 +346,9 @@ export const drawServices = async function (
); );
} }
serviceElem.attr('class', 'architecture-service'); serviceElem.attr('id', `service-${service.id}`).attr('class', 'architecture-service');
const { width, height } = serviceElem._groups[0][0].getBBox(); const { width, height } = serviceElem.node().getBBox();
service.width = width; service.width = width;
service.height = height; service.height = height;
db.setElementForId(service.id, serviceElem); db.setElementForId(service.id, serviceElem);