Skip to content

Turn any idea, plan, or codebase into a beautiful interactive diagram. An agent skill for Claude Code, Codex, and more.

fix(architecture): slide boundary titles off crossing routes

tt-a1ifa693c14d735
diff --git a/archify/renderers/architecture/render-architecture.mjs b/archify/renderers/architecture/render-architecture.mjs
index 39670e71..7f704b18 100644
--- a/archify/renderers/architecture/render-architecture.mjs
+++ b/archify/renderers/architecture/render-architecture.mjs
@@ -17,6 +17,7 @@ import {
asArray,
isFinitePoint,
rectsOverlap,
+ segmentIntersectsRect,
cleanEndpointSideProblems,
cleanFlowProblems,
cleanCrossingProblems,
@@ -252,6 +253,37 @@ function horizontalOverlap(left, right) {
return left.x < right.x + right.width && left.x + left.width > right.x;
}
+// A route through a title disappears under its mask and reads as struck-through
+// text. Keep the left rail position when it is clear; otherwise slide the title
+// along its rail to the nearest clear position inside the frame.
+let titleRouteSegments = null;
+function slideTitleOffRoutes(boundary, title, placedTitles) {
+ if (!enforcesBoundaryTitleComposition) return;
+ titleRouteSegments ||= asArray(arch.connections)
+ .filter((conn) => components.has(conn.from) && components.has(conn.to))
+ .flatMap((conn) => {
+ const points = pathFor(conn).points;
+ return points.slice(1).map((end, index) => ({ start: points[index], end }));
+ });
+ const clear = (x) => {
+ const rect = { ...title, x };
+ return !titleRouteSegments.some((segment) => segmentIntersectsRect(segment, rect, 2))
+ && ![...placedTitles, ...components.values()].some((other) => rectsOverlap(rect, other));
+ };
+ if (clear(title.x)) return;
+ const left = boundary.x + layout.boundaryLabelFrameInset;
+ const right = boundary.x + boundary.width - layout.boundaryLabelFrameInset - title.width;
+ const band = titleRouteSegments.filter(({ start, end }) => (
+ Math.max(start[1], end[1]) >= title.y - 2 && Math.min(start[1], end[1]) <= title.y + title.height + 2
+ ));
+ const x = [right, ...band.flatMap(({ start, end }) => [
+ Math.max(start[0], end[0]) + 8, Math.min(start[0], end[0]) - 8 - title.width,
+ ])].filter((value) => value >= left && value <= right)
+ .sort((a, b) => a - b)
+ .find(clear);
+ if (x !== undefined) title.x = x;
+}
+
function layoutBoundaryTitles(rawBoundaries, minimumFontSize) {
const placedTitles = [];
const measured = new Map();
@@ -280,6 +312,7 @@ function layoutBoundaryTitles(rawBoundaries, minimumFontSize) {
Infinity,
);
}
+ slideTitleOffRoutes(boundary, title, placedTitles);
placedTitles.push(title);
measured.set(index, { boundary, title });
}
diff --git a/test/architecture-route-bends.test.mjs b/test/architecture-route-bends.test.mjs
index d06a7b9f..a8c49905 100644
--- a/test/architecture-route-bends.test.mjs
+++ b/test/architecture-route-bends.test.mjs
@@ -85,3 +85,27 @@ test('architecture straightens a dogleg between facing nodes that overlap across
assert.equal(points[0][1], points[1][1]);
assert.ok(points[0][1] >= 146 && points[0][1] <= 164, JSON.stringify(points));
});
+
+test('showcase slides a boundary title off a route that crosses its rail', t => {
+ const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'archify-title-route-'));
+ t.after(() => fs.rmSync(directory, { recursive: true, force: true }));
+ const input = path.join(directory, 'diagram.json');
+ const output = path.join(directory, 'diagram.html');
+ fs.writeFileSync(input, JSON.stringify({
+ schema_version: 1,
+ diagram_type: 'architecture',
+ meta: { title: 'Title', output: 'diagram.html', quality_profile: 'showcase' },
+ components: [
+ { id: 'cache', type: 'database', label: 'Cache', pos: [40, 40], size: [100, 64] },
+ { id: 'api', type: 'backend', label: 'API', pos: [60, 220], size: [260, 80] },
+ ],
+ connections: [{ id: 'read', from: 'api', to: 'cache' }],
+ boundaries: [{ kind: 'region', label: 'Backend services', wraps: ['api'] }],
+ }));
+ const result = spawnSync(process.execPath, [cli, 'render', 'architecture', input, output, '--quality', 'showcase'], { cwd: directory, encoding: 'utf8' });
+ assert.equal(result.status, 0, result.stderr);
+ const html = fs.readFileSync(output, 'utf8');
+ const [, x, width] = html.match(/structural-frame-label-mask" x="([\d.]+)" y="[\d.]+" width="([\d.]+)"/).map(Number);
+ const routeX = Number(html.match(/data-edge-id="read"[^>]*data-composition-points="([\d.]+),/)[1]);
+ assert.ok(routeX < x - 2 || routeX > x + width + 2, JSON.stringify({ x, width, routeX }));
+});