Skip to content

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

fix(erd): account for stroke widths in trunk clearance

To3akaRin307f6b984ae6
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 05a5f542..428ac061 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,7 +12,7 @@ All notable changes are documented here. Format loosely follows [Keep a Changelo
### Fixed
-- **ER 图主干桥接隔离(#896)。** 自动总线不再跨越、接触或覆盖无关关系及其他已生成的桥接;存在冲突时保留分离的主干段,避免校验通过的图看起来多出关系。实体位置、显式途经点、关系基数及正常共享主干保持不变。
+- **ER 图主干桥接隔离(#896)。** 自动总线不再跨越、接触或覆盖无关关系及其他已生成的桥接;检查包含双方实际描边宽度(含显式 `width`),存在冲突时保留分离的主干段,避免校验通过的图看起来多出关系。实体位置、显式途经点、关系基数及正常共享主干保持不变。
- **WebM 录制失败清理 (#622)。** 画布、捕获流、编码器启动、逐帧绘制或停止失败时,录制 Promise 现在会拒绝并释放已创建的流、临时 URL、动画帧和定时器;晚到的编码事件不能再将失败录制报告为成功。正常编码与最佳努力的最终片段 flush 行为保持不变。
diff --git a/archify.zip b/archify.zip
index 8f9b5b82..45040eb6 100644
Binary files a/archify.zip and b/archify.zip differ
diff --git a/archify/renderers/erd/README.md b/archify/renderers/erd/README.md
index d7631575..1713bd68 100644
--- a/archify/renderers/erd/README.md
+++ b/archify/renderers/erd/README.md
@@ -101,7 +101,9 @@ renderer:
(`route`, `via`, `labelAt`) keeps its own line. Logical routes still traverse
the trunk, so every gate, label, and the layout report see the full geometry.
自动补出的主干桥接还会检查无关关系及已接受的其他桥接;出现交叉、接触或
- 重叠时保留分离的主干段,避免把独立关系画成同一条总线。这个回退不移动
+ 重叠时保留分离的主干段,避免把独立关系画成同一条总线。检查按双方描边的
+ 半宽之和留出间隔,并计入无关关系的显式 `width`;中心线未相交但描边接触
+ 也会阻止桥接。这个回退不移动
实体、显式途经点或端口,也不改变关系基数;无障碍的同簇分支仍可共享主干。
- Entity boxes are opaque obstacles. When a third entity sits between two
aligned anchors, the shared obstacle grid routes around it, and a route that
diff --git a/archify/renderers/erd/render-erd.mjs b/archify/renderers/erd/render-erd.mjs
index 5f2a56b4..58d4bdce 100644
--- a/archify/renderers/erd/render-erd.mjs
+++ b/archify/renderers/erd/render-erd.mjs
@@ -627,6 +627,11 @@ const TRUNK_SNAP = LANE_STEP;
const TRUNK_BRIDGE = ERD_PORT_SPACING + 8;
const TRUNK_CAP = 10;
const TRUNK_LEG = 12;
+const DEFAULT_RELATIONSHIP_STROKE_WIDTH = 1.5;
+
+function relationshipStrokeWidth(relationship) {
+ return Number.isFinite(relationship.width) ? relationship.width : DEFAULT_RELATIONSHIP_STROKE_WIDTH;
+}
const isHorizontalSide = (side) => (side === 'left' || side === 'right');
@@ -859,7 +864,7 @@ function bundledTrunkPaths() {
const paths = [];
const bridged = [];
const routed = relationships.filter(renderableRelationship)
- .map((relationship) => ({ relationship, points: pathFor(relationship).points }));
+ .map((relationship) => ({ relationship, points: pathFor(relationship).points, width: relationshipStrokeWidth(relationship) }));
for (const [key, runs] of clusters) {
const [axis, coordinate] = key.split(':').slice(0, 2).map(Number);
const members = new Set(runs.map((run) => run.relationship));
@@ -887,9 +892,12 @@ function bundledTrunkPaths() {
x: probe.start[0], y: probe.start[1],
width: probe.end[0] - probe.start[0], height: probe.end[1] - probe.start[1],
};
- const conflicts = routed.some(({ relationship, points }) => !members.has(relationship)
- && points.slice(1).some((end, index) => segmentIntersectsRect({ start: points[index], end }, box)))
- || bridged.some((other) => segmentIntersectsRect(other, box));
+ // 两条线的半宽之和才是描边的接触边界,显式加粗关系也使用实际绘制宽度。
+ const conflicts = routed.some(({ relationship, points, width }) => !members.has(relationship)
+ && points.slice(1).some((end, index) => segmentIntersectsRect(
+ { start: points[index], end }, box, (DEFAULT_RELATIONSHIP_STROKE_WIDTH + width) / 2,
+ )))
+ || bridged.some((other) => segmentIntersectsRect(other, box, DEFAULT_RELATIONSHIP_STROKE_WIDTH));
if (!conflicts && ![...entities.values()].some((entity) => segmentIntersectsRect(probe, entity))) {
bridged.push(probe);
last.high = segment.high;
@@ -903,7 +911,7 @@ function bundledTrunkPaths() {
const points = axis === 0
? [[coordinate, segment.low], [coordinate, segment.high]]
: [[segment.low, coordinate], [segment.high, coordinate]];
- paths.push(` <path data-er-trunk="" data-composition-points="${routePointsValue(points)}" d="${roundedPath(points, 8)}" class="${cls}" stroke-width="1.5"/>`);
+ paths.push(` <path data-er-trunk="" data-composition-points="${routePointsValue(points)}" d="${roundedPath(points, 8)}" class="${cls}" stroke-width="${DEFAULT_RELATIONSHIP_STROKE_WIDTH}"/>`);
}
}
return paths.join('\n');
@@ -1408,7 +1416,7 @@ function renderRelationshipPath(relationship, index) {
const [cls] = arrowClassMap[markerStyleOf(relationship)] || arrowClassMap.default;
const routed = pathFor(relationship);
const d = bundledRelationshipD(routed, relationshipTrunkRuns(relationship));
- const strokeWidth = Number.isFinite(relationship.width) ? relationship.width : 1.5;
+ const strokeWidth = relationshipStrokeWidth(relationship);
return ` <path ${focusEdgeAttrs(relationship.from, relationship.to, relationship.label, index, relationship.id)} data-er-ends="${relationshipEnds(relationship).join(' ')}" data-composition-points="${routePointsValue(routed.points)}" d="${d}" class="${cls}"${animateAttr(er.meta, 'edge', index)} stroke-width="${strokeWidth}" marker-start="url(#${cardinalityMarkerId(relationship, 'from')})" marker-end="url(#${cardinalityMarkerId(relationship, 'to')})"/>`;
}
diff --git a/test/erd-trunk-bridges.test.mjs b/test/erd-trunk-bridges.test.mjs
index 5a99490c..ea881ac2 100644
--- a/test/erd-trunk-bridges.test.mjs
+++ b/test/erd-trunk-bridges.test.mjs
@@ -11,7 +11,7 @@ const cli = fileURLToPath(new URL('../archify/bin/archify.mjs', import.meta.url)
const epsilon = 0.0001;
const attributes = tag => Object.fromEntries([...tag.matchAll(/([\w-]+)="([^"]*)"/g)].map(match => [match[1], match[2]]));
-function fixture({ horizontal = false, conflict, dashed = false } = {}) {
+function fixture({ horizontal = false, conflict, dashed = false, offset = 0, width } = {}) {
const table = (id, pos, rows = 3) => ({
id, label: id, pos, width: rows * 20 + 36,
attributes: Array.from({ length: rows }, (_, index) => ({ name: `f${index}`, type: 'int' })),
@@ -28,13 +28,14 @@ function fixture({ horizontal = false, conflict, dashed = false } = {}) {
if (conflict) {
// 两条逻辑主干之间留下 24px 空隙;独立关系的显式 U 形路线进入该空隙。
// 相切用 16px 回转段,圆角恰好在总线上相接;重叠用 20px 留下真正的直线墨迹。
- const x = conflict === 'crossing' ? 164 : 172;
- const [top, bottom] = conflict === 'touch' ? [130, 146] : [128, 148];
+ const x = conflict === 'crossing' ? 164 : 172 + offset;
+ const [top, bottom] = width > 1.5 || conflict === 'touch' ? [130, 146] : [128, 148];
document.relationships.push({
id: 'c_d', from: 'c', to: 'd', fromCardinality: 'many', toCardinality: 'one',
fromSide: 'left', toSide: 'left',
via: [[400, 48], [400, top], [x, top], [x, bottom], [400, bottom], [400, 328]],
...(dashed ? { identifying: false } : {}),
+ ...(width === undefined ? {} : { width }),
});
}
if (horizontal) {
@@ -135,3 +136,45 @@ test('ERD clear vertical and horizontal fan-in gaps retain one continuous shared
}), '同一总线的合法分支间隙仍须连通');
}
});
+
+function pointDistance(point, { start, end }) {
+ const dx = end[0] - start[0], dy = end[1] - start[1];
+ const lengthSquared = dx * dx + dy * dy;
+ const ratio = lengthSquared ? Math.max(0, Math.min(1,
+ ((point[0] - start[0]) * dx + (point[1] - start[1]) * dy) / lengthSquared)) : 0;
+ return Math.hypot(point[0] - start[0] - ratio * dx, point[1] - start[1] - ratio * dy);
+}
+
+function centerlineDistance(trunk, route) {
+ if (hitsTrunk(trunk, route)) return 0;
+ return Math.min(pointDistance(trunk.start, route), pointDistance(trunk.end, route),
+ pointDistance(route.start, trunk), pointDistance(route.end, trunk));
+}
+
+for (const horizontal of [false, true]) {
+ test(`ERD ${horizontal ? 'horizontal' : 'vertical'} bridge clearance covers both rendered stroke widths`, t => {
+ for (const [offset, width] of [[0.1, undefined], [1.2, undefined], [1.5, undefined], [2.5, 4]]) {
+ const { trunks, relationships } = render(t, fixture({ conflict: 'near', horizontal, offset, width }));
+ const independent = relationships.find(route => route.attrs['data-edge-id'] === 'c_d');
+ for (const trunk of trunks) {
+ const separation = (Number(trunk.attrs['stroke-width']) + Number(independent.attrs['stroke-width'])) / 2;
+ assert.ok(trunk.segments.every(bus => independent.segments.every(segment =>
+ centerlineDistance(bus, segment) > separation + epsilon)),
+ `距离 ${offset} 的实际描边不能被桥接连在一起,包含显式加粗关系`);
+ }
+ }
+ });
+
+ test(`ERD ${horizontal ? 'horizontal' : 'vertical'} bridges survive when nearby strokes remain separate`, t => {
+ for (const [offset, width] of [[1.6, undefined], [2.85, 4]]) {
+ const { trunks, relationships } = render(t, fixture({ conflict: 'near', horizontal, offset, width }));
+ const ports = relationships.filter(route => route.attrs['data-edge-id'] !== 'c_d').map(route => route.points.at(-1));
+ const along = horizontal ? 0 : 1;
+ assert.ok(trunks.some(trunk => {
+ const values = trunk.points.map(point => point[along]);
+ return Math.min(...values) <= Math.min(...ports.map(point => point[along]))
+ && Math.max(...values) >= Math.max(...ports.map(point => point[along]));
+ }), '有足够描边间距时仍保留连通主干,不能用过大的固定间距一律删除桥接');
+ }
+ });
+}