@diagrammo/dgmo 0.33.0 → 0.34.0
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.
- package/dist/advanced.cjs +329 -149
- package/dist/advanced.d.cts +6 -0
- package/dist/advanced.d.ts +6 -0
- package/dist/advanced.js +329 -149
- package/dist/auto.cjs +331 -151
- package/dist/auto.js +93 -93
- package/dist/auto.mjs +331 -151
- package/dist/cli.cjs +127 -127
- package/dist/index.cjs +330 -150
- package/dist/index.js +330 -150
- package/package.json +2 -2
- package/src/boxes-and-lines/label-placement.ts +339 -0
- package/src/boxes-and-lines/layout-search.ts +16 -2
- package/src/boxes-and-lines/layout.ts +32 -7
- package/src/boxes-and-lines/renderer.ts +67 -139
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@diagrammo/dgmo",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.34.0",
|
|
4
4
|
"description": "DGMO diagram markup language — parser, renderer, and color system",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -134,7 +134,7 @@
|
|
|
134
134
|
"check:deadcode": "knip",
|
|
135
135
|
"check:spelling": "cspell \"src/**/*.ts\" \"tests/**/*.ts\"",
|
|
136
136
|
"check:all": "pnpm check:deadcode && pnpm check:spelling && pnpm check:duplication && pnpm check:circular && pnpm check:deps && pnpm check:security && pnpm build && bash scripts/check-api.sh check && pnpm check:publish && pnpm check:types",
|
|
137
|
-
"check:circular": "madge --circular --extensions ts src/ --json | node -e \"const c=JSON.parse(require('fs').readFileSync('/dev/stdin','utf8')); const n=c.length; if(n>
|
|
137
|
+
"check:circular": "madge --circular --extensions ts src/ --json | node -e \"const c=JSON.parse(require('fs').readFileSync('/dev/stdin','utf8')); const n=c.length; if(n>7){console.error('New circular deps found ('+n+' > 7 known type-only/dynamic cycles)');process.exit(1)}else if(n>0){console.log(n+' known type-only/dynamic cycles (safe)')}else{console.log('No circular dependencies')}\"",
|
|
138
138
|
"check:deps": "depcheck --ignores='@diagrammo/dgmo,@codemirror/language,@lezer/*,husky,lint-staged,tsup,axe-core,type-coverage'",
|
|
139
139
|
"check:security": "pnpm audit --prod",
|
|
140
140
|
"check:publish": "publint",
|
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
// ============================================================
|
|
2
|
+
// Boxes and Lines Diagram — Edge label placement
|
|
3
|
+
// ============================================================
|
|
4
|
+
//
|
|
5
|
+
// Makes edge/arrow labels legible regardless of length or layout via a priority
|
|
6
|
+
// ladder (each step only fires when the previous can't make the label legible):
|
|
7
|
+
//
|
|
8
|
+
// 1. WRAP — cap label width and wrap onto up to MAX_LABEL_LINES lines.
|
|
9
|
+
// 2. MOVE — if the wrapped box still overlaps a node box, slide it to a clear
|
|
10
|
+
// spot near its own line (along the polyline, then a small
|
|
11
|
+
// perpendicular offset), staying proximal. No leader line.
|
|
12
|
+
// 3. RELAYOUT (last resort, owned by layout.ts) — re-run the search reserving
|
|
13
|
+
// dagre label space so a gap opens; this module only reports which
|
|
14
|
+
// labels stayed unresolved so the caller can decide to escalate.
|
|
15
|
+
//
|
|
16
|
+
// Placement runs in layout (not the renderer) so it sees node/group boxes and so
|
|
17
|
+
// the relayout decision can re-invoke the search. The renderer is a pure consumer
|
|
18
|
+
// of the labelX/labelY/labelWidth/labelHeight/labelLines fields written here.
|
|
19
|
+
|
|
20
|
+
import type { BLLayoutResult } from './layout';
|
|
21
|
+
import {
|
|
22
|
+
measureText,
|
|
23
|
+
wrapTextToWidth,
|
|
24
|
+
truncateText,
|
|
25
|
+
} from '../utils/text-measure';
|
|
26
|
+
|
|
27
|
+
// Keep in sync with EDGE_LABEL_FONT_SIZE in renderer.ts (ScaleContext is identity
|
|
28
|
+
// there, so layout-space px == screen px for the unscaled diagram coords).
|
|
29
|
+
export const EDGE_LABEL_FONT_SIZE = 11;
|
|
30
|
+
|
|
31
|
+
const LABEL_MAX_WIDTH = 160; // ≈ DESC_NODE_WIDTH + slack; flat (not canvas-relative)
|
|
32
|
+
const MAX_LABEL_LINES = 3;
|
|
33
|
+
const LABEL_LINE_HEIGHT = 1.3;
|
|
34
|
+
const H_PAD = 6; // horizontal halo padding (each side)
|
|
35
|
+
const V_PAD = 3; // vertical halo padding (each side)
|
|
36
|
+
const BOX_CLEAR_PAD = 4; // min clearance kept between a label box and a node box
|
|
37
|
+
const PERP_STEP = 8; // perpendicular offset increment (px)
|
|
38
|
+
const PERP_MAX = 40; // max perpendicular offset before giving up
|
|
39
|
+
const SLIDE_SAMPLES = 9; // arc-length samples per side when sliding along the edge
|
|
40
|
+
|
|
41
|
+
type Pt = { readonly x: number; readonly y: number };
|
|
42
|
+
|
|
43
|
+
interface Rect {
|
|
44
|
+
readonly minX: number;
|
|
45
|
+
readonly minY: number;
|
|
46
|
+
readonly maxX: number;
|
|
47
|
+
readonly maxY: number;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface MeasuredLabel {
|
|
51
|
+
readonly lines: readonly string[];
|
|
52
|
+
/** Halo box width, including 2·H_PAD. */
|
|
53
|
+
readonly width: number;
|
|
54
|
+
/** Halo box height, including 2·V_PAD. */
|
|
55
|
+
readonly height: number;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Wrap + measure an edge label into a bounded multi-line box. Long unbreakable
|
|
60
|
+
* tokens are hard-broken at the char boundary; overflow past MAX_LABEL_LINES is
|
|
61
|
+
* ellipsized so the box is bounded in both axes.
|
|
62
|
+
*/
|
|
63
|
+
export function measureEdgeLabel(
|
|
64
|
+
label: string,
|
|
65
|
+
fontSize: number = EDGE_LABEL_FONT_SIZE,
|
|
66
|
+
maxWidth: number = LABEL_MAX_WIDTH
|
|
67
|
+
): MeasuredLabel {
|
|
68
|
+
let lines = wrapTextToWidth(label, fontSize, maxWidth, { hardBreak: true });
|
|
69
|
+
if (lines.length > MAX_LABEL_LINES) {
|
|
70
|
+
const kept = lines.slice(0, MAX_LABEL_LINES - 1);
|
|
71
|
+
const rest = lines.slice(MAX_LABEL_LINES - 1).join(' ');
|
|
72
|
+
kept.push(truncateText(rest, fontSize, maxWidth));
|
|
73
|
+
lines = kept;
|
|
74
|
+
}
|
|
75
|
+
let textW = 0;
|
|
76
|
+
for (const ln of lines) {
|
|
77
|
+
const w = measureText(ln, fontSize);
|
|
78
|
+
if (w > textW) textW = w;
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
lines,
|
|
82
|
+
width: textW + 2 * H_PAD,
|
|
83
|
+
height: lines.length * fontSize * LABEL_LINE_HEIGHT + 2 * V_PAD,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Point at the half-way arc length along an edge polyline — the geometric
|
|
88
|
+
* centre of the connector, so a label seeds in the gap BETWEEN the two nodes
|
|
89
|
+
* rather than drifting under the target. Degenerate point lists fall back. */
|
|
90
|
+
export function edgePolylineMidpoint(points: ReadonlyArray<Pt>): Pt {
|
|
91
|
+
return pointAtArcFraction(points, 0.5).p;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Position + unit normal at arc-length fraction t∈[0,1] of a polyline. The
|
|
95
|
+
* normal is perpendicular to the local segment (for proximal perp offsets). */
|
|
96
|
+
function pointAtArcFraction(
|
|
97
|
+
points: ReadonlyArray<Pt>,
|
|
98
|
+
t: number
|
|
99
|
+
): { p: Pt; nx: number; ny: number } {
|
|
100
|
+
if (points.length === 0) return { p: { x: 0, y: 0 }, nx: 0, ny: 1 };
|
|
101
|
+
if (points.length === 1)
|
|
102
|
+
return { p: { x: points[0]!.x, y: points[0]!.y }, nx: 0, ny: 1 };
|
|
103
|
+
const segLen: number[] = [];
|
|
104
|
+
let total = 0;
|
|
105
|
+
for (let k = 1; k < points.length; k++) {
|
|
106
|
+
const len = Math.hypot(
|
|
107
|
+
points[k]!.x - points[k - 1]!.x,
|
|
108
|
+
points[k]!.y - points[k - 1]!.y
|
|
109
|
+
);
|
|
110
|
+
segLen.push(len);
|
|
111
|
+
total += len;
|
|
112
|
+
}
|
|
113
|
+
let want = total * Math.max(0, Math.min(1, t));
|
|
114
|
+
for (let k = 1; k < points.length; k++) {
|
|
115
|
+
const len = segLen[k - 1]!;
|
|
116
|
+
if (want <= len || k === points.length - 1) {
|
|
117
|
+
const f = len === 0 ? 0 : Math.min(1, want / len);
|
|
118
|
+
const ax = points[k - 1]!.x;
|
|
119
|
+
const ay = points[k - 1]!.y;
|
|
120
|
+
const bx = points[k]!.x;
|
|
121
|
+
const by = points[k]!.y;
|
|
122
|
+
const dx = bx - ax;
|
|
123
|
+
const dy = by - ay;
|
|
124
|
+
const dlen = Math.hypot(dx, dy) || 1;
|
|
125
|
+
// Unit normal = perpendicular to the local tangent.
|
|
126
|
+
return {
|
|
127
|
+
p: { x: ax + dx * f, y: ay + dy * f },
|
|
128
|
+
nx: -dy / dlen,
|
|
129
|
+
ny: dx / dlen,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
want -= len;
|
|
133
|
+
}
|
|
134
|
+
const last = points[points.length - 1]!;
|
|
135
|
+
return { p: { x: last.x, y: last.y }, nx: 0, ny: 1 };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function rectFromCenter(cx: number, cy: number, w: number, h: number): Rect {
|
|
139
|
+
return {
|
|
140
|
+
minX: cx - w / 2,
|
|
141
|
+
minY: cy - h / 2,
|
|
142
|
+
maxX: cx + w / 2,
|
|
143
|
+
maxY: cy + h / 2,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Does a label box centred at (cx,cy) overlap any obstacle (within pad)? */
|
|
148
|
+
function overlapsAny(
|
|
149
|
+
cx: number,
|
|
150
|
+
cy: number,
|
|
151
|
+
w: number,
|
|
152
|
+
h: number,
|
|
153
|
+
obstacles: readonly Rect[],
|
|
154
|
+
pad: number
|
|
155
|
+
): boolean {
|
|
156
|
+
const minX = cx - w / 2 - pad;
|
|
157
|
+
const minY = cy - h / 2 - pad;
|
|
158
|
+
const maxX = cx + w / 2 + pad;
|
|
159
|
+
const maxY = cy + h / 2 + pad;
|
|
160
|
+
for (const o of obstacles) {
|
|
161
|
+
if (minX < o.maxX && maxX > o.minX && minY < o.maxY && maxY > o.minY)
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Arc-length fractions sampled centre-outward (keeps the label proximal to the
|
|
168
|
+
* true midpoint), clamped away from the endpoints. */
|
|
169
|
+
function slideFractions(): number[] {
|
|
170
|
+
const out = [0.5];
|
|
171
|
+
for (let k = 1; k <= SLIDE_SAMPLES; k++) {
|
|
172
|
+
const off = k * 0.08;
|
|
173
|
+
if (0.5 - off >= 0.12) out.push(0.5 - off);
|
|
174
|
+
if (0.5 + off <= 0.88) out.push(0.5 + off);
|
|
175
|
+
}
|
|
176
|
+
return out;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** Find the nearest clear centre for a label box of (w,h) along/near `points`.
|
|
180
|
+
* Pass 1 slides along the line; pass 2 offsets perpendicular. null = no clear
|
|
181
|
+
* spot found within the proximity budget. */
|
|
182
|
+
function findClearPosition(
|
|
183
|
+
w: number,
|
|
184
|
+
h: number,
|
|
185
|
+
points: ReadonlyArray<Pt>,
|
|
186
|
+
obstacles: readonly Rect[]
|
|
187
|
+
): Pt | null {
|
|
188
|
+
const ts = slideFractions();
|
|
189
|
+
for (const t of ts) {
|
|
190
|
+
const { p } = pointAtArcFraction(points, t);
|
|
191
|
+
if (!overlapsAny(p.x, p.y, w, h, obstacles, BOX_CLEAR_PAD)) return p;
|
|
192
|
+
}
|
|
193
|
+
for (let mag = PERP_STEP; mag <= PERP_MAX; mag += PERP_STEP) {
|
|
194
|
+
for (const t of ts) {
|
|
195
|
+
const { p, nx, ny } = pointAtArcFraction(points, t);
|
|
196
|
+
for (const sign of [-1, 1]) {
|
|
197
|
+
const x = p.x + nx * mag * sign;
|
|
198
|
+
const y = p.y + ny * mag * sign;
|
|
199
|
+
if (!overlapsAny(x, y, w, h, obstacles, BOX_CLEAR_PAD)) return { x, y };
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return null;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
interface LabelBox {
|
|
207
|
+
edgeIdx: number;
|
|
208
|
+
cx: number;
|
|
209
|
+
cy: number;
|
|
210
|
+
w: number;
|
|
211
|
+
h: number;
|
|
212
|
+
lines: readonly string[];
|
|
213
|
+
resolved: boolean;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** Separate overlapping label boxes vertically (variable sizes), mirroring the
|
|
217
|
+
* old renderer pass. Runs after node-collision resolution; the caller re-checks
|
|
218
|
+
* node overlap afterwards. */
|
|
219
|
+
function resolveLabelOverlaps(boxes: LabelBox[]): void {
|
|
220
|
+
const MAX_PASSES = 8;
|
|
221
|
+
const PAD = 4;
|
|
222
|
+
for (let pass = 0; pass < MAX_PASSES; pass++) {
|
|
223
|
+
let moved = false;
|
|
224
|
+
for (let i = 0; i < boxes.length; i++) {
|
|
225
|
+
for (let j = i + 1; j < boxes.length; j++) {
|
|
226
|
+
const a = boxes[i]!;
|
|
227
|
+
const b = boxes[j]!;
|
|
228
|
+
const dx = Math.abs(a.cx - b.cx);
|
|
229
|
+
const dy = Math.abs(a.cy - b.cy);
|
|
230
|
+
const overlapX = (a.w + b.w) / 2 + PAD - dx;
|
|
231
|
+
const overlapY = (a.h + b.h) / 2 + PAD - dy;
|
|
232
|
+
if (overlapX > 0 && overlapY > 0) {
|
|
233
|
+
const shift = overlapY / 2 + 1;
|
|
234
|
+
if (a.cy < b.cy) {
|
|
235
|
+
a.cy -= shift;
|
|
236
|
+
b.cy += shift;
|
|
237
|
+
} else {
|
|
238
|
+
a.cy += shift;
|
|
239
|
+
b.cy -= shift;
|
|
240
|
+
}
|
|
241
|
+
moved = true;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
if (!moved) break;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export interface PlaceEdgeLabelsResult {
|
|
250
|
+
readonly layout: BLLayoutResult;
|
|
251
|
+
/** Edge indices whose label still overlaps a node box (escalate to relayout). */
|
|
252
|
+
readonly unresolved: number[];
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Wrap, then collision-resolve every edge label against node boxes (steps 1–2),
|
|
257
|
+
* writing labelX/labelY/labelWidth/labelHeight/labelLines onto each edge. Returns
|
|
258
|
+
* the edge indices that could not be cleared so the caller can trigger a
|
|
259
|
+
* last-resort relayout.
|
|
260
|
+
*/
|
|
261
|
+
export function placeEdgeLabels(
|
|
262
|
+
layout: BLLayoutResult,
|
|
263
|
+
opts?: { fontSize?: number; maxWidth?: number }
|
|
264
|
+
): PlaceEdgeLabelsResult {
|
|
265
|
+
const fontSize = opts?.fontSize ?? EDGE_LABEL_FONT_SIZE;
|
|
266
|
+
const maxWidth = opts?.maxWidth ?? LABEL_MAX_WIDTH;
|
|
267
|
+
|
|
268
|
+
// Obstacles = real node boxes + collapsed groups (drawn as boxes). Expanded
|
|
269
|
+
// groups are containers — their interior is valid label space, so they are NOT
|
|
270
|
+
// obstacles.
|
|
271
|
+
const obstacles: Rect[] = [];
|
|
272
|
+
for (const n of layout.nodes)
|
|
273
|
+
obstacles.push(rectFromCenter(n.x, n.y, n.width, n.height));
|
|
274
|
+
for (const g of layout.groups)
|
|
275
|
+
if (g.collapsed)
|
|
276
|
+
obstacles.push(rectFromCenter(g.x, g.y, g.width, g.height));
|
|
277
|
+
|
|
278
|
+
const boxes: LabelBox[] = [];
|
|
279
|
+
layout.edges.forEach((e, idx) => {
|
|
280
|
+
if (!e.label || e.points.length < 2) return;
|
|
281
|
+
const m = measureEdgeLabel(e.label, fontSize, maxWidth);
|
|
282
|
+
const mid = edgePolylineMidpoint(e.points);
|
|
283
|
+
let cy = mid.y;
|
|
284
|
+
// Fold in the parallel-edge fan offset so each line's label clears its line.
|
|
285
|
+
if (e.parallelCount > 1 && e.yOffset !== 0)
|
|
286
|
+
cy += (e.yOffset < 0 ? -1 : 1) * (m.height / 2);
|
|
287
|
+
boxes.push({
|
|
288
|
+
edgeIdx: idx,
|
|
289
|
+
cx: mid.x,
|
|
290
|
+
cy,
|
|
291
|
+
w: m.width,
|
|
292
|
+
h: m.height,
|
|
293
|
+
lines: m.lines,
|
|
294
|
+
resolved: true,
|
|
295
|
+
});
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
// Step 2: reposition any box that overlaps a node.
|
|
299
|
+
for (const box of boxes) {
|
|
300
|
+
if (!overlapsAny(box.cx, box.cy, box.w, box.h, obstacles, BOX_CLEAR_PAD))
|
|
301
|
+
continue;
|
|
302
|
+
const e = layout.edges[box.edgeIdx]!;
|
|
303
|
+
const clear = findClearPosition(box.w, box.h, e.points, obstacles);
|
|
304
|
+
if (clear) {
|
|
305
|
+
box.cx = clear.x;
|
|
306
|
+
box.cy = clear.y;
|
|
307
|
+
} else {
|
|
308
|
+
box.resolved = false;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// Separate stacked labels, then re-check node overlap (a vertical nudge can
|
|
313
|
+
// push a label back onto a box — that becomes an unresolved escalation).
|
|
314
|
+
resolveLabelOverlaps(boxes);
|
|
315
|
+
for (const box of boxes) {
|
|
316
|
+
if (overlapsAny(box.cx, box.cy, box.w, box.h, obstacles, BOX_CLEAR_PAD))
|
|
317
|
+
box.resolved = false;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
const boxByIdx = new Map<number, LabelBox>();
|
|
321
|
+
for (const b of boxes) boxByIdx.set(b.edgeIdx, b);
|
|
322
|
+
const edges = layout.edges.map((e, idx) => {
|
|
323
|
+
const b = boxByIdx.get(idx);
|
|
324
|
+
if (!b) return e;
|
|
325
|
+
return {
|
|
326
|
+
...e,
|
|
327
|
+
labelX: b.cx,
|
|
328
|
+
labelY: b.cy,
|
|
329
|
+
labelWidth: b.w,
|
|
330
|
+
labelHeight: b.h,
|
|
331
|
+
labelLines: b.lines,
|
|
332
|
+
};
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
return {
|
|
336
|
+
layout: { ...layout, edges },
|
|
337
|
+
unresolved: boxes.filter((b) => !b.resolved).map((b) => b.edgeIdx),
|
|
338
|
+
};
|
|
339
|
+
}
|
|
@@ -23,6 +23,7 @@ import {
|
|
|
23
23
|
} from './layout';
|
|
24
24
|
import { layeredCandidates } from './layout-layered';
|
|
25
25
|
import { groupedTierCandidates } from './layout-grouped';
|
|
26
|
+
import { measureEdgeLabel, EDGE_LABEL_FONT_SIZE } from './label-placement';
|
|
26
27
|
|
|
27
28
|
type Pt = { x: number; y: number };
|
|
28
29
|
|
|
@@ -917,9 +918,14 @@ export async function layoutBoxesAndLinesSearch(
|
|
|
917
918
|
* indicator. Omit it (CLI/export) and the search runs straight through with
|
|
918
919
|
* no added latency. */
|
|
919
920
|
onProgress?: (done: number, total: number, phase: string) => void;
|
|
921
|
+
/** Last-resort label legibility: reserve dagre label space for every labeled
|
|
922
|
+
* edge so the layout opens a gap wide enough for the (wrapped) label. Only
|
|
923
|
+
* set by layout.ts when steps 1–2 left a label overlapping a node box. */
|
|
924
|
+
reserveEdgeLabels?: boolean;
|
|
920
925
|
}
|
|
921
926
|
): Promise<BLLayoutResult> {
|
|
922
927
|
const hideDescriptions = opts?.hideDescriptions ?? false;
|
|
928
|
+
const reserveEdgeLabels = opts?.reserveEdgeLabels ?? false;
|
|
923
929
|
const onProgress = opts?.onProgress;
|
|
924
930
|
// Yield to a macrotask (lets the browser repaint between heavy placements);
|
|
925
931
|
// no-op when there's no progress observer so non-interactive callers pay
|
|
@@ -1323,8 +1329,16 @@ export async function layoutBoxesAndLinesSearch(
|
|
|
1323
1329
|
for (const e of ord(parsed.edges)) {
|
|
1324
1330
|
const s = srcRep(e.source);
|
|
1325
1331
|
const t = tgtRep(e.target);
|
|
1326
|
-
if (s !== t && g.hasNode(s) && g.hasNode(t))
|
|
1327
|
-
|
|
1332
|
+
if (s !== t && g.hasNode(s) && g.hasNode(t)) {
|
|
1333
|
+
// Last-resort label legibility: reserve a virtual label node on the edge
|
|
1334
|
+
// so dagre widens the rank gap to fit the (wrapped) label.
|
|
1335
|
+
let edgeLabel: object = {};
|
|
1336
|
+
if (reserveEdgeLabels && e.label) {
|
|
1337
|
+
const m = measureEdgeLabel(e.label, EDGE_LABEL_FONT_SIZE);
|
|
1338
|
+
edgeLabel = { width: m.width, height: m.height, labelpos: 'c' };
|
|
1339
|
+
}
|
|
1340
|
+
g.setEdge(s, t, edgeLabel, edgeKey(e));
|
|
1341
|
+
}
|
|
1328
1342
|
}
|
|
1329
1343
|
dagre.layout(g);
|
|
1330
1344
|
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
import type { ParsedBoxesAndLines, BLNode, BLGroup } from './types';
|
|
11
11
|
import { measureText, wrapTextToWidth } from '../utils/text-measure';
|
|
12
|
+
import { placeEdgeLabels } from './label-placement';
|
|
12
13
|
import {
|
|
13
14
|
resolveNotes,
|
|
14
15
|
buildPlacedNotes,
|
|
@@ -58,8 +59,14 @@ export interface BLLayoutEdge {
|
|
|
58
59
|
readonly bidirectional: boolean;
|
|
59
60
|
readonly lineNumber: number;
|
|
60
61
|
readonly points: ReadonlyArray<{ readonly x: number; readonly y: number }>;
|
|
62
|
+
/** Centre of the label box (set by label-placement). */
|
|
61
63
|
readonly labelX?: number;
|
|
62
64
|
readonly labelY?: number;
|
|
65
|
+
/** Wrapped label box dimensions + lines (set by label-placement; the renderer
|
|
66
|
+
* draws the halo + tspans straight from these). */
|
|
67
|
+
readonly labelWidth?: number;
|
|
68
|
+
readonly labelHeight?: number;
|
|
69
|
+
readonly labelLines?: readonly string[];
|
|
63
70
|
readonly yOffset: number;
|
|
64
71
|
readonly parallelCount: number;
|
|
65
72
|
readonly metadata: Readonly<Record<string, string>>;
|
|
@@ -205,7 +212,7 @@ export async function layoutBoxesAndLines(
|
|
|
205
212
|
}
|
|
206
213
|
): Promise<BLLayoutResult> {
|
|
207
214
|
const { layoutBoxesAndLinesSearch } = await import('./layout-search');
|
|
208
|
-
const
|
|
215
|
+
const searchOpts = {
|
|
209
216
|
...(layoutOptions?.hideDescriptions !== undefined && {
|
|
210
217
|
hideDescriptions: layoutOptions.hideDescriptions,
|
|
211
218
|
}),
|
|
@@ -215,14 +222,31 @@ export async function layoutBoxesAndLines(
|
|
|
215
222
|
...(layoutOptions?.onProgress !== undefined && {
|
|
216
223
|
onProgress: layoutOptions.onProgress,
|
|
217
224
|
}),
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
// (and shift the canvas to fit them).
|
|
221
|
-
return attachNotes(
|
|
222
|
-
applyParallelEdgeOffsets(searched),
|
|
225
|
+
};
|
|
226
|
+
const searched = await layoutBoxesAndLinesSearch(
|
|
223
227
|
parsed,
|
|
224
|
-
|
|
228
|
+
collapseInfo,
|
|
229
|
+
searchOpts
|
|
225
230
|
);
|
|
231
|
+
|
|
232
|
+
// Edge-label legibility (priority ladder): wrap + reposition labels on the
|
|
233
|
+
// chosen layout. If any label still can't clear a node box, escalate ONCE to a
|
|
234
|
+
// label-aware relayout that reserves dagre label space so a gap opens — and
|
|
235
|
+
// keep it only if it actually resolves more labels.
|
|
236
|
+
let placed = placeEdgeLabels(applyParallelEdgeOffsets(searched));
|
|
237
|
+
if (placed.unresolved.length > 0) {
|
|
238
|
+
const relaid = await layoutBoxesAndLinesSearch(parsed, collapseInfo, {
|
|
239
|
+
...searchOpts,
|
|
240
|
+
reserveEdgeLabels: true,
|
|
241
|
+
});
|
|
242
|
+
const relaidPlaced = placeEdgeLabels(applyParallelEdgeOffsets(relaid));
|
|
243
|
+
if (relaidPlaced.unresolved.length < placed.unresolved.length)
|
|
244
|
+
placed = relaidPlaced;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// Engine-agnostic post-processing: float notes (and shift the canvas to fit
|
|
248
|
+
// them) on the label-placed layout.
|
|
249
|
+
return attachNotes(placed.layout, parsed, layoutOptions?.collapsedNotes);
|
|
226
250
|
}
|
|
227
251
|
|
|
228
252
|
/**
|
|
@@ -312,6 +336,7 @@ function attachNotes(
|
|
|
312
336
|
points: e.points.map((pt) => ({ x: pt.x + shiftX, y: pt.y + shiftY })),
|
|
313
337
|
...(e.labelX !== undefined && { labelX: e.labelX + shiftX }),
|
|
314
338
|
...(e.labelY !== undefined && { labelY: e.labelY + shiftY }),
|
|
339
|
+
// labelWidth/labelHeight/labelLines are shift-invariant — carried via spread.
|
|
315
340
|
}))
|
|
316
341
|
: layout.edges;
|
|
317
342
|
const finalGroups = shifted
|