@sentropic/diagram-canvas 0.2.0 → 0.2.3
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/README.md +6 -2
- package/dist/camera.d.ts +14 -2
- package/dist/camera.js +26 -3
- package/dist/presentation/diagramSceneModel.d.ts +60 -1
- package/dist/presentation/diagramSceneModel.js +91 -15
- package/dist/presentation/renderSceneSvg.js +12 -4
- package/dist/presentation/sceneLabelClearance.d.ts +8 -0
- package/dist/presentation/sceneLabelClearance.js +85 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -348,8 +348,12 @@ empty penalty, hysteresis); absent or empty, the DS defaults apply.
|
|
|
348
348
|
maximum display scale (0.62: readout 100 %, a card renders at about
|
|
349
349
|
285 × 124 CSS px) and holds a fit at the readability floor (0.45: an 18 px
|
|
350
350
|
edge label renders at 8.1 CSS px) when the drawing is larger, reporting
|
|
351
|
-
`pans`; `clampZoom` and `zoomReadout` complete it.
|
|
352
|
-
|
|
351
|
+
`pans`; `clampZoom` and `zoomReadout` complete it. Given a `zoomQuantum`, the
|
|
352
|
+
applied fit is floored to that grid, never rounded up past the fit box;
|
|
353
|
+
`sceneFit` passes `FIT_QUANTUM` (1e-3), and a scene host prints that one zoom
|
|
354
|
+
everywhere: world transform, camera state, `data-zoom`, `--st-diagram-zoom`,
|
|
355
|
+
flow hit size and painted-bounds read. No DOM lives here: a scene host passes
|
|
356
|
+
its measured sizes in.
|
|
353
357
|
|
|
354
358
|
The acceptance scenes, the §7 metrics helper, the column baseline as recorded
|
|
355
359
|
before the placement changed (`baseline.json`), the numbers of the shipped
|
package/dist/camera.d.ts
CHANGED
|
@@ -37,17 +37,26 @@ export interface CameraFitInput {
|
|
|
37
37
|
readonly viewWidth: number;
|
|
38
38
|
/** `window.innerHeight`, CSS px. */
|
|
39
39
|
readonly windowHeight: number;
|
|
40
|
+
/** Measured horizontal scrollbar thickness (`offsetHeight - clientHeight`), CSS px. */
|
|
41
|
+
readonly scrollbarHeight?: number;
|
|
40
42
|
readonly maxDisplayScale?: number;
|
|
41
43
|
/** Optional higher readability floor; never lowers the absolute 0.45 floor. */
|
|
42
44
|
readonly minReadableScale?: number;
|
|
45
|
+
/**
|
|
46
|
+
* Grid the applied fit zoom lands on, scale units. The fit is floored to
|
|
47
|
+
* it, never rounded: an applied zoom above `zoomFit` would paint past the
|
|
48
|
+
* fit box. The display cap and the readability floor are token values and
|
|
49
|
+
* stay exact. Absent: the applied zoom is the exact fit.
|
|
50
|
+
*/
|
|
51
|
+
readonly zoomQuantum?: number;
|
|
43
52
|
}
|
|
44
53
|
export interface CameraFit {
|
|
45
|
-
/** The §5.1 fit box, CSS px. */
|
|
54
|
+
/** The usable §5.1 fit box, excluding scrollbars, CSS px. */
|
|
46
55
|
readonly fitWidth: number;
|
|
47
56
|
readonly fitHeight: number;
|
|
48
57
|
/** Zoom framing the whole drawing on both axes, never above the maximum display scale. */
|
|
49
58
|
readonly zoomFit: number;
|
|
50
|
-
/** Zoom a fit or reset applies: `zoomFit` clamped to the readability floor. */
|
|
59
|
+
/** Zoom a fit or reset applies: `zoomFit`, floored to `zoomQuantum` when one is given, clamped to the readability floor. */
|
|
51
60
|
readonly zoom: number;
|
|
52
61
|
/** Interactive range: zoom-out stops at the readable fit, zoom-in at the display cap. */
|
|
53
62
|
readonly minZoom: number;
|
|
@@ -57,6 +66,9 @@ export interface CameraFit {
|
|
|
57
66
|
}
|
|
58
67
|
/** Screen-space inset for non-scaling strokes and CSS layout rounding. */
|
|
59
68
|
export declare const FIT_PAINT_INSET = 1;
|
|
69
|
+
/** Largest multiple of `quantum` that is not above `value`. A quantum that
|
|
70
|
+
* is not a finite positive number defines no grid: the value is returned. */
|
|
71
|
+
export declare function floorToQuantum(value: number, quantum: number): number;
|
|
60
72
|
/** §5.1: `fitW = viewW`, `fitH = min(viewW × 9/16, 0.85 × windowH)`. */
|
|
61
73
|
export declare function fitBox(viewWidth: number, windowHeight: number): {
|
|
62
74
|
readonly width: number;
|
package/dist/camera.js
CHANGED
|
@@ -29,6 +29,22 @@ export const FIT_WINDOW_HEIGHT_SHARE = 0.85;
|
|
|
29
29
|
export const FIT_ASPECT = 16 / 9;
|
|
30
30
|
/** Screen-space inset for non-scaling strokes and CSS layout rounding. */
|
|
31
31
|
export const FIT_PAINT_INSET = 1;
|
|
32
|
+
/** Largest multiple of `quantum` that is not above `value`. A quantum that
|
|
33
|
+
* is not a finite positive number defines no grid: the value is returned. */
|
|
34
|
+
export function floorToQuantum(value, quantum) {
|
|
35
|
+
if (!(quantum > 0) || !Number.isFinite(quantum) || !Number.isFinite(value))
|
|
36
|
+
return value;
|
|
37
|
+
const perUnit = 1 / quantum;
|
|
38
|
+
let steps = Math.floor(value * perUnit);
|
|
39
|
+
// The product is rounded: it can land one ulp on either side of an exact
|
|
40
|
+
// multiple (0.501 × 1000 = 500.99999999999994). Settle on the largest
|
|
41
|
+
// step that is not above the value, so the result never exceeds it.
|
|
42
|
+
if (steps / perUnit > value)
|
|
43
|
+
steps -= 1;
|
|
44
|
+
else if ((steps + 1) / perUnit <= value)
|
|
45
|
+
steps += 1;
|
|
46
|
+
return steps / perUnit;
|
|
47
|
+
}
|
|
32
48
|
/** §5.1: `fitW = viewW`, `fitH = min(viewW × 9/16, 0.85 × windowH)`. */
|
|
33
49
|
export function fitBox(viewWidth, windowHeight) {
|
|
34
50
|
return { width: viewWidth, height: Math.min(viewWidth / FIT_ASPECT, FIT_WINDOW_HEIGHT_SHARE * windowHeight) };
|
|
@@ -36,17 +52,24 @@ export function fitBox(viewWidth, windowHeight) {
|
|
|
36
52
|
export function cameraFit(input) {
|
|
37
53
|
const maxZoom = Math.max(DEFAULT_MIN_READABLE_SCALE, input.maxDisplayScale ?? DEFAULT_MAX_DISPLAY_SCALE);
|
|
38
54
|
const readableFloor = Math.min(maxZoom, Math.max(DEFAULT_MIN_READABLE_SCALE, input.minReadableScale ?? DEFAULT_MIN_READABLE_SCALE));
|
|
39
|
-
const
|
|
55
|
+
const frame = fitBox(input.viewWidth, input.windowHeight);
|
|
56
|
+
// Hosts add the scrollbar back to the outer height; fit and placement use
|
|
57
|
+
// only the drawable area, keeping the combined height inside the fit box.
|
|
58
|
+
const box = { width: frame.width, height: Math.max(0, frame.height - (input.scrollbarHeight ?? 0)) };
|
|
40
59
|
const bounds = input.bounds;
|
|
41
60
|
if (bounds === undefined || !(bounds.width > 0) || !(bounds.height > 0)) {
|
|
42
61
|
// Empty scene: readout 100 %.
|
|
43
62
|
return { fitWidth: box.width, fitHeight: box.height, zoomFit: maxZoom, zoom: maxZoom, minZoom: maxZoom, maxZoom, pans: false };
|
|
44
63
|
}
|
|
45
64
|
const zoomFit = Math.min(Math.max(0, box.width - 2 * FIT_PAINT_INSET) / bounds.width, Math.max(0, box.height - 2 * FIT_PAINT_INSET) / bounds.height, maxZoom);
|
|
65
|
+
// The applied zoom is quantised here, once, and downward: every consumer
|
|
66
|
+
// (transform, camera state, painted-bounds read) gets this single value,
|
|
67
|
+
// and the fit height below is the one of the drawing at that same zoom.
|
|
68
|
+
const applied = input.zoomQuantum === undefined || zoomFit >= maxZoom ? zoomFit : floorToQuantum(zoomFit, input.zoomQuantum);
|
|
46
69
|
// All hosts and camera inputs share this range. A small frame or a large
|
|
47
70
|
// unfolded scene reaches the floor, then scrolls instead of shrinking text.
|
|
48
|
-
const zoom = Math.max(readableFloor,
|
|
49
|
-
const fitHeight =
|
|
71
|
+
const zoom = Math.max(readableFloor, applied);
|
|
72
|
+
const fitHeight = Math.min(box.height, Math.ceil((bounds.height * zoom + 2 * FIT_PAINT_INSET) * 64) / 64);
|
|
50
73
|
return { fitWidth: box.width, fitHeight, zoomFit, zoom, minZoom: zoom, maxZoom, pans: zoom > zoomFit };
|
|
51
74
|
}
|
|
52
75
|
/** Z-1/Z-7: any requested zoom lands inside `[minZoom, maxZoom]` of the current fit. */
|
|
@@ -122,6 +122,47 @@ export interface SceneNodeModel {
|
|
|
122
122
|
readonly text: string;
|
|
123
123
|
readonly erd?: ErdCard;
|
|
124
124
|
}
|
|
125
|
+
/**
|
|
126
|
+
* Paint plane of a node, relative to the connectors. A scene paints, bottom
|
|
127
|
+
* to top: every `frame`, the connectors, every `box`, then badges and labels.
|
|
128
|
+
*
|
|
129
|
+
* A `frame` is the surface of an expanded zone: it carries an opaque fill and
|
|
130
|
+
* holds the boxes its relations join, so a connector painted under it is
|
|
131
|
+
* hidden along its whole run inside the zone while its label, painted last,
|
|
132
|
+
* stays visible. The frame also paints its own footer text where routes run:
|
|
133
|
+
* the connector layer is masked over it (`sceneConnectorMask`), so a route
|
|
134
|
+
* leaving a zone passes behind its footer text. A `box` is atomic (card, collapsed zone, ERD card, legend
|
|
135
|
+
* key, label node): a route ends on its border and stays under it.
|
|
136
|
+
*
|
|
137
|
+
* The order matches the scene frame (`scene.ts`): a container sits on a
|
|
138
|
+
* negative plane, a connector between two cards on plane 0, under the cards.
|
|
139
|
+
*/
|
|
140
|
+
export type ScenePaintPlane = "frame" | "box";
|
|
141
|
+
export declare function scenePaintPlane(node: Pick<SceneNodeModel, "kind" | "collapsed">): ScenePaintPlane;
|
|
142
|
+
/** The nodes of one paint plane, in the scene's own bottom-to-top order. */
|
|
143
|
+
export declare function sceneNodesOnPlane<T extends Pick<SceneNodeModel, "kind" | "collapsed">>(nodes: readonly T[], plane: ScenePaintPlane): readonly T[];
|
|
144
|
+
/** World margin of the connector mask around the scene bounds: stroke, arrow heads and markers stay inside. */
|
|
145
|
+
export declare const CONNECTOR_MASK_MARGIN = 64;
|
|
146
|
+
/**
|
|
147
|
+
* World slack of a connector cutout beyond the label clearance. The cutout is
|
|
148
|
+
* derived from the measured footer width and the frozen line box; the slack
|
|
149
|
+
* absorbs the difference between that measure and the rendered ink: measured
|
|
150
|
+
* in a browser at the readability floor, the ink reaches half a world unit
|
|
151
|
+
* past the measured box, and a connector end is painted one stroke further.
|
|
152
|
+
*/
|
|
153
|
+
export declare const CONNECTOR_CUTOUT_SLACK = 3;
|
|
154
|
+
export interface SceneConnectorMask {
|
|
155
|
+
/** The painted region: the scene bounds grown by `CONNECTOR_MASK_MARGIN`. */
|
|
156
|
+
readonly region: Rect;
|
|
157
|
+
/** The boxes cut out of it (`SceneModel.connectorCutouts`). */
|
|
158
|
+
readonly cutouts: readonly Rect[];
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* The mask of the connector layer: everything is painted but the footer text
|
|
162
|
+
* of the expanded zones. `undefined` when there is nothing to cut out (no
|
|
163
|
+
* expanded zone with a footer): the layer then needs no mask at all.
|
|
164
|
+
*/
|
|
165
|
+
export declare function sceneConnectorMask(scene: Pick<SceneModel, "bounds" | "connectorCutouts">): SceneConnectorMask | undefined;
|
|
125
166
|
export interface SceneEdgeModel {
|
|
126
167
|
readonly directed: boolean;
|
|
127
168
|
readonly arrows: readonly FlowArrow[];
|
|
@@ -172,6 +213,15 @@ export interface SceneModel {
|
|
|
172
213
|
readonly nodes: readonly SceneNodeModel[];
|
|
173
214
|
readonly edges: readonly SceneEdgeModel[];
|
|
174
215
|
readonly labels: readonly SceneLabelModel[];
|
|
216
|
+
/**
|
|
217
|
+
* Footer text box of every expanded zone, grown by the label clearance: the
|
|
218
|
+
* one text a frame paints where routes run. A route may cross a zone footer
|
|
219
|
+
* or follow it on its way out (the router keeps every route out of a title
|
|
220
|
+
* band, not out of a footer band), so the connector layer is masked over
|
|
221
|
+
* these boxes: a connector is painted above the frame surface and passes
|
|
222
|
+
* behind the footer text, never through it.
|
|
223
|
+
*/
|
|
224
|
+
readonly connectorCutouts: readonly Rect[];
|
|
175
225
|
/**
|
|
176
226
|
* Fit bbox (BF-7): `rectUnion(scene.bounds, placed label boxes)`.
|
|
177
227
|
* `undefined` for an empty scene.
|
|
@@ -194,10 +244,19 @@ export interface SceneCameraInput {
|
|
|
194
244
|
readonly bounds?: Rect;
|
|
195
245
|
readonly viewWidth: number;
|
|
196
246
|
readonly windowHeight: number;
|
|
247
|
+
readonly scrollbarHeight?: number;
|
|
197
248
|
readonly maxDisplayScale?: number;
|
|
198
249
|
readonly minReadableScale?: number;
|
|
199
250
|
}
|
|
200
|
-
/**
|
|
251
|
+
/**
|
|
252
|
+
* Quantum of the applied fit zoom, scale units. `sceneFit` floors every fit
|
|
253
|
+
* to it, so a fit does not follow sub-quantum changes of its bounds. The
|
|
254
|
+
* hosts print the resulting zoom as it is: one value for the world
|
|
255
|
+
* transform, the camera state, `data-zoom`, `--st-diagram-zoom`, the flow
|
|
256
|
+
* hit size and the painted-bounds read. Nothing is rounded again downstream.
|
|
257
|
+
*/
|
|
258
|
+
export declare const FIT_QUANTUM = 0.001;
|
|
259
|
+
/** §5.1 / BF-2 fit of the scene bounds, as `cameraFit` computes it on the `FIT_QUANTUM` grid. */
|
|
201
260
|
export declare function sceneFit(input: SceneCameraInput): CameraFit;
|
|
202
261
|
/**
|
|
203
262
|
* The camera zoom for a request (Z-1, Z-7, Z-8). `undefined` means « no
|
|
@@ -13,11 +13,11 @@ import { measureErdCard, renderErdMarkerSvg, erdText, wrapErdText, placeErdRoleL
|
|
|
13
13
|
* (layout, scene, camera). The semantic-document and view types are read
|
|
14
14
|
* off the layout signature, so this package names no other diagram layer.
|
|
15
15
|
*/
|
|
16
|
-
import { buildScene, cameraFit, clampZoom, CONTENT_INSET, DEFAULT_ALLOCATED_LINES, DEFAULT_LAYOUT_METRICS, DEFAULT_MIN_READABLE_SCALE, layoutFolded, layoutMeasured, placeEdgeLabels, rectUnion, relationRoute, sceneNodes, sceneEdges, } from "../index.js";
|
|
16
|
+
import { buildScene, cameraFit, clampZoom, CONTENT_INSET, DEFAULT_ALLOCATED_LINES, DEFAULT_LAYOUT_METRICS, DEFAULT_MIN_READABLE_SCALE, FIT_PAINT_INSET, layoutFolded, layoutMeasured, placeEdgeLabels, rectUnion, relationRoute, sceneNodes, sceneEdges, } from "../index.js";
|
|
17
17
|
import { measureBundleLabel } from "../layout/bundleLabel.js";
|
|
18
18
|
import { getDiagramGlyph } from "./registry.js";
|
|
19
|
-
import { ZONE_GEOMETRY } from "./diagramCardModel.js";
|
|
20
|
-
import {
|
|
19
|
+
import { lineBoxHeight, ZONE_GEOMETRY } from "./diagramCardModel.js";
|
|
20
|
+
import { clearSceneLabels } from "./sceneLabelClearance.js";
|
|
21
21
|
/**
|
|
22
22
|
* `targetWidth` handed to the engine when the host supplies none. The
|
|
23
23
|
* measured path never re-packs on it (D-3) and the host never reads the
|
|
@@ -115,6 +115,37 @@ export function collapsedOccurrencesOf(view) {
|
|
|
115
115
|
}
|
|
116
116
|
return found;
|
|
117
117
|
}
|
|
118
|
+
export function scenePaintPlane(node) {
|
|
119
|
+
return node.kind === "zone" && !node.collapsed ? "frame" : "box";
|
|
120
|
+
}
|
|
121
|
+
/** The nodes of one paint plane, in the scene's own bottom-to-top order. */
|
|
122
|
+
export function sceneNodesOnPlane(nodes, plane) {
|
|
123
|
+
return nodes.filter((node) => scenePaintPlane(node) === plane);
|
|
124
|
+
}
|
|
125
|
+
/** World margin of the connector mask around the scene bounds: stroke, arrow heads and markers stay inside. */
|
|
126
|
+
export const CONNECTOR_MASK_MARGIN = 64;
|
|
127
|
+
/**
|
|
128
|
+
* World slack of a connector cutout beyond the label clearance. The cutout is
|
|
129
|
+
* derived from the measured footer width and the frozen line box; the slack
|
|
130
|
+
* absorbs the difference between that measure and the rendered ink: measured
|
|
131
|
+
* in a browser at the readability floor, the ink reaches half a world unit
|
|
132
|
+
* past the measured box, and a connector end is painted one stroke further.
|
|
133
|
+
*/
|
|
134
|
+
export const CONNECTOR_CUTOUT_SLACK = 3;
|
|
135
|
+
/**
|
|
136
|
+
* The mask of the connector layer: everything is painted but the footer text
|
|
137
|
+
* of the expanded zones. `undefined` when there is nothing to cut out (no
|
|
138
|
+
* expanded zone with a footer): the layer then needs no mask at all.
|
|
139
|
+
*/
|
|
140
|
+
export function sceneConnectorMask(scene) {
|
|
141
|
+
if (scene.bounds === undefined || scene.connectorCutouts.length === 0)
|
|
142
|
+
return undefined;
|
|
143
|
+
const { x, y, width, height } = scene.bounds;
|
|
144
|
+
return {
|
|
145
|
+
region: { x: x - CONNECTOR_MASK_MARGIN, y: y - CONNECTOR_MASK_MARGIN, width: width + 2 * CONNECTOR_MASK_MARGIN, height: height + 2 * CONNECTOR_MASK_MARGIN },
|
|
146
|
+
cutouts: scene.connectorCutouts,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
118
149
|
const byCodeUnit = (left, right) => (left < right ? -1 : left > right ? 1 : 0);
|
|
119
150
|
function edgeLabelOf(document, relation) {
|
|
120
151
|
const attributes = document.relations[relation]?.attributes;
|
|
@@ -167,7 +198,14 @@ export function resolveScene(result, input) {
|
|
|
167
198
|
const measuredLabels = new Map((result.measurements?.slots ?? []).filter((slot) => slot.role === "edge-label").map((slot) => [slot.occurrence, slot.layout]));
|
|
168
199
|
const headerHeights = new Map((result.measurements?.headers ?? []).map((header) => [header.occurrence, header.height]));
|
|
169
200
|
const blockers = [];
|
|
170
|
-
|
|
201
|
+
const connectorCutouts = [];
|
|
202
|
+
const footerWidths = new Map((result.measurements?.slots ?? []).filter((slot) => slot.role === "footer").map((slot) => [slot.occurrence, slot.layout.width]));
|
|
203
|
+
const margin = input.metrics?.labelMargin ?? DEFAULT_LAYOUT_METRICS.labelMargin;
|
|
204
|
+
// Non-scaling connector paint is widest in world units at the readability
|
|
205
|
+
// floor. Reserve it as well as the label-padding token supplied by hosts.
|
|
206
|
+
const paintClearance = margin + ZONE_GEOMETRY.borderWidth / (2 * DEFAULT_MIN_READABLE_SCALE);
|
|
207
|
+
const zoneInset = input.metrics?.containerPadding ?? ZONE_GEOMETRY.contentInsetInline;
|
|
208
|
+
for (const node of nodes) {
|
|
171
209
|
if (node.kind !== "zone" || node.collapsed) {
|
|
172
210
|
blockers.push(node.rect);
|
|
173
211
|
continue;
|
|
@@ -182,10 +220,27 @@ export function resolveScene(result, input) {
|
|
|
182
220
|
width: node.rect.width,
|
|
183
221
|
height: CONTENT_INSET + height + ZONE_GEOMETRY.ruleHeight / 2 + ruleClearance,
|
|
184
222
|
});
|
|
185
|
-
|
|
186
|
-
|
|
223
|
+
// A zone footer has one allocated line. Reserve its fixed chrome from
|
|
224
|
+
// the presentation instead of measuring the scene again during replay.
|
|
225
|
+
const reserve = node.presentation?.text.footer?.trim()
|
|
226
|
+
? lineBoxHeight(ZONE_GEOMETRY.footer.size) + ZONE_GEOMETRY.ruleHeight + 2 * ZONE_GEOMETRY.ruleMargin + ZONE_GEOMETRY.footerBottomInset : 0;
|
|
227
|
+
if (reserve > 0) {
|
|
187
228
|
blockers.push({ x: node.rect.x, y: node.rect.y + node.rect.height - reserve - ZONE_GEOMETRY.borderWidth - ruleClearance,
|
|
188
229
|
width: node.rect.width, height: reserve + ZONE_GEOMETRY.borderWidth + ruleClearance });
|
|
230
|
+
// The footer line as `layoutZone` seats it: one line box pinned above
|
|
231
|
+
// the bottom border, from the content inset, as wide as its measured
|
|
232
|
+
// text (the whole inner width when the result carries no measurement).
|
|
233
|
+
const inner = Math.max(0, node.rect.width - 2 * zoneInset);
|
|
234
|
+
const lineHeight = lineBoxHeight(ZONE_GEOMETRY.footer.size);
|
|
235
|
+
const bottom = node.rect.y + node.rect.height - ZONE_GEOMETRY.borderWidth - ZONE_GEOMETRY.footerBottomInset;
|
|
236
|
+
const pad = paintClearance + CONNECTOR_CUTOUT_SLACK;
|
|
237
|
+
connectorCutouts.push({
|
|
238
|
+
x: node.rect.x + zoneInset - pad,
|
|
239
|
+
y: bottom - lineHeight - pad,
|
|
240
|
+
width: Math.min(footerWidths.get(node.occurrence) ?? inner, inner) + 2 * pad,
|
|
241
|
+
height: lineHeight + 2 * pad,
|
|
242
|
+
});
|
|
243
|
+
}
|
|
189
244
|
}
|
|
190
245
|
const allFlows = flowIndex(input.document);
|
|
191
246
|
const flowTargets = result.flowTargets ?? {};
|
|
@@ -256,7 +311,6 @@ export function resolveScene(result, input) {
|
|
|
256
311
|
labelLines.set(id, lines);
|
|
257
312
|
entries.push({ id, route: points, size: semantic?.profile === 'erd@1' ? { width: ERD_GEOMETRY.labelWidth, height: lines.length * ERD_GEOMETRY.lineHeight } : { width: layout.width, height: layout.height } });
|
|
258
313
|
}
|
|
259
|
-
const margin = input.metrics?.labelMargin ?? DEFAULT_LAYOUT_METRICS.labelMargin;
|
|
260
314
|
const labels = [];
|
|
261
315
|
let bounds;
|
|
262
316
|
for (const item of items) {
|
|
@@ -267,13 +321,15 @@ export function resolveScene(result, input) {
|
|
|
267
321
|
const placements = erd ? placeErdRoleLabels(entries, blockers, margin) : result.labelBoxes === undefined
|
|
268
322
|
? placeEdgeLabels(entries, blockers, margin)
|
|
269
323
|
: new Map(Object.entries(result.labelBoxes).map(([id, box]) => [id, { box }]));
|
|
324
|
+
// A label is the label of a drawn connector: a placement whose relation has
|
|
325
|
+
// no route in this scene is dropped, so a label never paints without a line.
|
|
326
|
+
const labelled = new Set(entries.map((entry) => entry.id));
|
|
270
327
|
for (const [id, placement] of placements) {
|
|
271
|
-
if (placement === undefined)
|
|
328
|
+
if (placement === undefined || !labelled.has(id))
|
|
272
329
|
continue;
|
|
273
330
|
labels.push({ occurrence: id, lines: labelLines.get(id) ?? [], box: placement.box, ...(erd ? { erd: true } : {}), ...("leader" in placement ? { leader: placement.leader } : {}),
|
|
274
331
|
...(result.edgeBundles?.[id] === undefined ? {} : { title: result.edgeBundles[id].title }),
|
|
275
332
|
...(result.edgeBundles?.[id]?.relations === undefined ? {} : { bundle: result.edgeBundles[id].chip ?? measureBundleLabel(result.edgeBundles[id].relations, input.measureLines, input.metrics?.bundleLabelMaxWidth) }) });
|
|
276
|
-
bounds = bounds === undefined ? placement.box : rectUnion(bounds, placement.box);
|
|
277
333
|
}
|
|
278
334
|
const badges = [];
|
|
279
335
|
for (const step of flows.scenarios.flatMap((scenario) => scenario.steps)) {
|
|
@@ -291,6 +347,15 @@ export function resolveScene(result, input) {
|
|
|
291
347
|
for (const arrows of Object.values(result.arrowBoxes ?? {}))
|
|
292
348
|
for (const arrow of arrows)
|
|
293
349
|
bounds = bounds === undefined ? arrow.box : rectUnion(bounds, arrow.box);
|
|
350
|
+
const clearedLabels = clearSceneLabels(labels, edges.map(edge => edge.points), [...blockers, ...badges.map(badge => badge.box), ...edges.flatMap(edge => edge.arrows.map(arrow => arrow.box))], paintClearance);
|
|
351
|
+
for (const { box } of clearedLabels) {
|
|
352
|
+
const padded = { x: box.x - margin, y: box.y - margin, width: box.width + 2 * margin, height: box.height + 2 * margin };
|
|
353
|
+
bounds = bounds === undefined ? padded : rectUnion(bounds, padded);
|
|
354
|
+
}
|
|
355
|
+
if (bounds !== undefined) {
|
|
356
|
+
const x = Math.floor(bounds.x * 64) / 64, y = Math.floor(bounds.y * 64) / 64;
|
|
357
|
+
bounds = { x, y, width: Math.ceil((bounds.x + bounds.width) * 64) / 64 - x, height: Math.ceil((bounds.y + bounds.height) * 64) / 64 - y };
|
|
358
|
+
}
|
|
294
359
|
const nodeTargets = nodes.filter((node) => Object.values(flowEnds).some((ends) => ends.some((id) => id === node.occurrence || (flowAncestors[id] ?? []).includes(node.occurrence)))).map((node) => ({ occurrence: node.occurrence, entity: node.entity, anchor: { x: node.rect.x, y: node.rect.y } }));
|
|
295
360
|
return {
|
|
296
361
|
badges, flows, flowTargets, nodeTargets, flowEnds, flowRepresentatives, flowAncestors,
|
|
@@ -298,7 +363,8 @@ export function resolveScene(result, input) {
|
|
|
298
363
|
contentSize: sceneContentSize(result),
|
|
299
364
|
nodes,
|
|
300
365
|
edges,
|
|
301
|
-
labels,
|
|
366
|
+
labels: clearedLabels,
|
|
367
|
+
connectorCutouts,
|
|
302
368
|
...(bounds === undefined ? {} : { bounds }),
|
|
303
369
|
orientations: result.orientations ?? {},
|
|
304
370
|
};
|
|
@@ -312,14 +378,24 @@ export function sceneTextRows(scene, document) {
|
|
|
312
378
|
}
|
|
313
379
|
return rows;
|
|
314
380
|
}
|
|
315
|
-
/**
|
|
381
|
+
/**
|
|
382
|
+
* Quantum of the applied fit zoom, scale units. `sceneFit` floors every fit
|
|
383
|
+
* to it, so a fit does not follow sub-quantum changes of its bounds. The
|
|
384
|
+
* hosts print the resulting zoom as it is: one value for the world
|
|
385
|
+
* transform, the camera state, `data-zoom`, `--st-diagram-zoom`, the flow
|
|
386
|
+
* hit size and the painted-bounds read. Nothing is rounded again downstream.
|
|
387
|
+
*/
|
|
388
|
+
export const FIT_QUANTUM = 1e-3;
|
|
389
|
+
/** §5.1 / BF-2 fit of the scene bounds, as `cameraFit` computes it on the `FIT_QUANTUM` grid. */
|
|
316
390
|
export function sceneFit(input) {
|
|
317
391
|
return cameraFit({
|
|
318
392
|
...(input.bounds === undefined ? {} : { bounds: input.bounds }),
|
|
319
393
|
viewWidth: input.viewWidth,
|
|
320
394
|
windowHeight: input.windowHeight,
|
|
395
|
+
...(input.scrollbarHeight === undefined ? {} : { scrollbarHeight: input.scrollbarHeight }),
|
|
321
396
|
...(input.maxDisplayScale === undefined ? {} : { maxDisplayScale: input.maxDisplayScale }),
|
|
322
397
|
...(input.minReadableScale === undefined ? {} : { minReadableScale: input.minReadableScale }),
|
|
398
|
+
zoomQuantum: FIT_QUANTUM,
|
|
323
399
|
});
|
|
324
400
|
}
|
|
325
401
|
/**
|
|
@@ -339,13 +415,13 @@ export function scenePlacement(bounds, zoom, fit) {
|
|
|
339
415
|
if (bounds === undefined) {
|
|
340
416
|
return { contentWidth: 0, contentHeight: 0, translateX: 0, translateY: 0, overflowX: false, overflowY: false };
|
|
341
417
|
}
|
|
342
|
-
const contentWidth = bounds.width * zoom;
|
|
343
|
-
const contentHeight = bounds.height * zoom;
|
|
418
|
+
const contentWidth = bounds.width * zoom + 2 * FIT_PAINT_INSET;
|
|
419
|
+
const contentHeight = bounds.height * zoom + 2 * FIT_PAINT_INSET;
|
|
344
420
|
// Half a CSS px of tolerance: a fit computed by division must not scroll.
|
|
345
421
|
const overflowX = contentWidth > fit.fitWidth + 0.5;
|
|
346
422
|
const overflowY = contentHeight > fit.fitHeight + 0.5;
|
|
347
|
-
const startX = overflowX ?
|
|
348
|
-
const startY = overflowY ?
|
|
423
|
+
const startX = overflowX ? FIT_PAINT_INSET : (fit.fitWidth - bounds.width * zoom) / 2;
|
|
424
|
+
const startY = overflowY ? FIT_PAINT_INSET : (fit.fitHeight - bounds.height * zoom) / 2;
|
|
349
425
|
return {
|
|
350
426
|
contentWidth,
|
|
351
427
|
contentHeight,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { renderErdCardSvg } from './erd.js';
|
|
2
2
|
/** Standalone server-side SVG, consuming exactly the scene model used by the framework hosts. */
|
|
3
|
-
import { layoutScene, resolveScene, sceneFit } from './diagramSceneModel.js';
|
|
3
|
+
import { layoutScene, resolveScene, sceneConnectorMask, sceneFit, sceneNodesOnPlane } from './diagramSceneModel.js';
|
|
4
4
|
import { cardLineMeasurer, defaultCardMeasure, sceneCardMeasure, projectCardProps, projectZoneProps } from './diagramCardModel.js';
|
|
5
5
|
import { renderCardSvg } from './renderCardSvg.js';
|
|
6
6
|
import { renderZoneSvg } from './renderZoneSvg.js';
|
|
@@ -21,7 +21,7 @@ export function renderSceneSvg(input, options = {}) {
|
|
|
21
21
|
const viewportWidth = input.targetWidth ?? 1440;
|
|
22
22
|
const fit = sceneFit({ bounds: box, viewWidth: viewportWidth, windowHeight: 900 });
|
|
23
23
|
const edges = scene.edges.map(edge => `<polyline data-scene-edge="${escape(edge.occurrence)}" points="${edge.points.map(p => p.x + ',' + p.y).join(' ')}" fill="none" stroke="var(--st-component-diagramScene-edgeStroke, #64748b)" stroke-width="1.5" stroke-linejoin="round" ${edge.markers === undefined ? 'marker-end="url(#scene-arrow)"' : ''}/>${edge.markers ?? ''}`).join('');
|
|
24
|
-
const
|
|
24
|
+
const drawNode = (node) => {
|
|
25
25
|
const measure = measureFor(node.collapsed);
|
|
26
26
|
if (node.kind === 'erd' && node.erd !== undefined)
|
|
27
27
|
return `<g data-scene-occurrence="${escape(node.occurrence)}" transform="translate(${node.rect.x} ${node.rect.y})">${renderErdCardSvg(node.erd)}</g>`;
|
|
@@ -33,7 +33,15 @@ export function renderSceneSvg(input, options = {}) {
|
|
|
33
33
|
if (rendered.kind !== 'rendered')
|
|
34
34
|
throw new Error('Content must never refuse a component');
|
|
35
35
|
return `<g data-scene-occurrence="${escape(node.occurrence)}" transform="translate(${node.rect.x} ${node.rect.y})">${rendered.svg}</g>`;
|
|
36
|
-
}
|
|
36
|
+
};
|
|
37
|
+
// Expanded zone surfaces are opaque: they paint under the connectors, the boxes above them.
|
|
38
|
+
// The connector layer is masked over the footer text the frames paint themselves.
|
|
39
|
+
const mask = sceneConnectorMask(scene);
|
|
40
|
+
const box4 = (r) => `x="${r.x}" y="${r.y}" width="${r.width}" height="${r.height}"`;
|
|
41
|
+
const maskDef = mask === undefined ? '' : `<mask id="scene-connector-mask" maskUnits="userSpaceOnUse" ${box4(mask.region)}><rect ${box4(mask.region)} fill="#fff"/>${mask.cutouts.map(cutout => `<rect data-scene-cutout="" ${box4(cutout)} fill="#000"/>`).join('')}</mask>`;
|
|
42
|
+
const connectors = `<g data-scene-connectors=""${mask === undefined ? '' : ' mask="url(#scene-connector-mask)"'}>${edges}</g>`;
|
|
43
|
+
const frames = sceneNodesOnPlane(scene.nodes, 'frame').map(drawNode).join('');
|
|
44
|
+
const boxes = sceneNodesOnPlane(scene.nodes, 'box').map(drawNode).join('');
|
|
37
45
|
const labels = scene.labels.map(label => {
|
|
38
46
|
const b = label.box;
|
|
39
47
|
let text = '';
|
|
@@ -50,7 +58,7 @@ export function renderSceneSvg(input, options = {}) {
|
|
|
50
58
|
const leader = label.leader === undefined ? '' : `<polyline points="${label.leader.map(p => p.x + ',' + p.y).join(' ')}" fill="none" stroke="var(--st-component-diagramScene-edgeStroke, #64748b)"/>`;
|
|
51
59
|
return leader + `<g data-scene-label="${escape(label.occurrence)}" transform="translate(${b.x} ${b.y})" font-size="18" fill="var(--st-component-diagramScene-labelText, #172033)"><rect width="${b.width}" height="${b.height}" fill="var(--st-component-diagramScene-labelSurface, #ffffff)" stroke="var(--st-component-diagramScene-labelBorder, #cbd5e1)"/>${text}</g>`;
|
|
52
60
|
}).join('');
|
|
53
|
-
const svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="${box.x - 2} ${box.y - 2} ${box.width + 4} ${box.height + 4}" width="${(box.width + 4) * fit.zoom}" height="${(box.height + 4) * fit.zoom}" data-viewport-width="${viewportWidth}" data-display-scale="${fit.zoom}" role="img" aria-label="${escape(options.title ?? input.document.documentId)}" font-family="Inter, system-ui, sans-serif"><title>${escape(options.title ?? input.document.documentId)}</title><style>@font-face{font-family:Inter;src:local('Inter');font-weight:100 900}svg{--st-font-sans:Inter,system-ui,sans-serif;--st-semantic-surface-raised:#fff;--st-semantic-text-primary:#172033;--st-semantic-text-secondary:#475569;--st-semantic-border-strong:#64748b;--st-semantic-border-subtle:#cbd5e1;--st-semantic-feedback-info:#2563eb}</style><defs><marker id="scene-arrow" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0 0L10 5L0 10Z" fill="var(--st-component-diagramScene-edgeStroke, #64748b)"/></marker
|
|
61
|
+
const svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="${box.x - 2} ${box.y - 2} ${box.width + 4} ${box.height + 4}" width="${(box.width + 4) * fit.zoom}" height="${(box.height + 4) * fit.zoom}" data-viewport-width="${viewportWidth}" data-display-scale="${fit.zoom}" role="img" aria-label="${escape(options.title ?? input.document.documentId)}" font-family="Inter, system-ui, sans-serif"><title>${escape(options.title ?? input.document.documentId)}</title><style>@font-face{font-family:Inter;src:local('Inter');font-weight:100 900}svg{--st-font-sans:Inter,system-ui,sans-serif;--st-semantic-surface-raised:#fff;--st-semantic-text-primary:#172033;--st-semantic-text-secondary:#475569;--st-semantic-border-strong:#64748b;--st-semantic-border-subtle:#cbd5e1;--st-semantic-feedback-info:#2563eb}</style><defs><marker id="scene-arrow" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0 0L10 5L0 10Z" fill="var(--st-component-diagramScene-edgeStroke, #64748b)"/></marker>${maskDef}</defs>${frames}${connectors}${boxes}${labels}</svg>`;
|
|
54
62
|
return { svg, result, scene };
|
|
55
63
|
}
|
|
56
64
|
//# sourceMappingURL=renderSceneSvg.js.map
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Rect, Vec2 } from '../transform.js';
|
|
2
|
+
/** Keep measured label sizes and routes; move only labels whose paint would
|
|
3
|
+
* cover a connector. The same deterministic placement is used by both hosts. */
|
|
4
|
+
export declare function clearSceneLabels<T extends {
|
|
5
|
+
readonly box: Rect;
|
|
6
|
+
readonly leader?: readonly Vec2[];
|
|
7
|
+
}>(labels: readonly T[], routes: readonly (readonly Vec2[])[], shapes: readonly Rect[], clearance: number): T[];
|
|
8
|
+
//# sourceMappingURL=sceneLabelClearance.d.ts.map
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { normaliseRect } from '../geometry.js';
|
|
2
|
+
/** Keep measured label sizes and routes; move only labels whose paint would
|
|
3
|
+
* cover a connector. The same deterministic placement is used by both hosts. */
|
|
4
|
+
export function clearSceneLabels(labels, routes, shapes, clearance) {
|
|
5
|
+
const bounds = shapes.map(normaliseRect);
|
|
6
|
+
for (const route of routes)
|
|
7
|
+
for (let i = 1; i < route.length; i++) {
|
|
8
|
+
const a = route[i - 1], b = route[i];
|
|
9
|
+
bounds.push({ x: Math.min(a.x, b.x), y: Math.min(a.y, b.y), width: Math.abs(a.x - b.x), height: Math.abs(a.y - b.y) });
|
|
10
|
+
}
|
|
11
|
+
// Normalise each obstacle once, rather than allocating two rectangles for
|
|
12
|
+
// every candidate/obstacle pair during the search. Use these same bounds
|
|
13
|
+
// for candidate generation, including persisted negative extents.
|
|
14
|
+
return labels.map(label => {
|
|
15
|
+
const { box } = label;
|
|
16
|
+
const padded = (x, y) => ({ x: x - clearance, y: y - clearance, width: box.width + 2 * clearance, height: box.height + 2 * clearance });
|
|
17
|
+
const candidates = [{ x: box.x, y: box.y, cost: 0 }];
|
|
18
|
+
const compare = (a, b) => a.cost - b.cost || a.y - b.y || a.x - b.x;
|
|
19
|
+
const visited = new Set([`${box.x},${box.y}`]);
|
|
20
|
+
const add = (x, y) => {
|
|
21
|
+
const key = `${x},${y}`;
|
|
22
|
+
if (visited.has(key))
|
|
23
|
+
return;
|
|
24
|
+
visited.add(key);
|
|
25
|
+
const candidate = { x, y, cost: Math.abs(x - box.x) + Math.abs(y - box.y) };
|
|
26
|
+
let index = candidates.length;
|
|
27
|
+
candidates.push(candidate);
|
|
28
|
+
while (index > 0) {
|
|
29
|
+
const parent = (index - 1) >> 1;
|
|
30
|
+
if (compare(candidates[parent], candidate) <= 0)
|
|
31
|
+
break;
|
|
32
|
+
candidates[index] = candidates[parent];
|
|
33
|
+
index = parent;
|
|
34
|
+
}
|
|
35
|
+
candidates[index] = candidate;
|
|
36
|
+
};
|
|
37
|
+
// Expand only boundaries of obstacles actually hit. Coordinates come
|
|
38
|
+
// from a finite set of rectangle edges; no unbounded nudge loop or
|
|
39
|
+
// quadratic Cartesian grid is needed for dense scenes.
|
|
40
|
+
while (candidates.length > 0) {
|
|
41
|
+
// A min-heap preserves the exact cost/y/x order without sorting and
|
|
42
|
+
// shifting the entire frontier at every visited position.
|
|
43
|
+
const choice = candidates[0];
|
|
44
|
+
const last = candidates.pop();
|
|
45
|
+
if (candidates.length > 0) {
|
|
46
|
+
let index = 0;
|
|
47
|
+
while (2 * index + 1 < candidates.length) {
|
|
48
|
+
let child = 2 * index + 1;
|
|
49
|
+
if (child + 1 < candidates.length && compare(candidates[child + 1], candidates[child]) < 0)
|
|
50
|
+
child++;
|
|
51
|
+
if (compare(last, candidates[child]) <= 0)
|
|
52
|
+
break;
|
|
53
|
+
candidates[index] = candidates[child];
|
|
54
|
+
index = child;
|
|
55
|
+
}
|
|
56
|
+
candidates[index] = last;
|
|
57
|
+
}
|
|
58
|
+
const area = normaliseRect(padded(choice.x, choice.y));
|
|
59
|
+
const right = area.x + area.width, bottom = area.y + area.height;
|
|
60
|
+
const hitIndex = bounds.findIndex(obstacle => area.x < obstacle.x + obstacle.width && obstacle.x < right
|
|
61
|
+
&& area.y < obstacle.y + obstacle.height && obstacle.y < bottom);
|
|
62
|
+
const hit = bounds[hitIndex];
|
|
63
|
+
if (hit === undefined) {
|
|
64
|
+
const placed = { ...box, x: choice.x, y: choice.y };
|
|
65
|
+
bounds.push(normaliseRect(placed));
|
|
66
|
+
if (choice.cost === 0)
|
|
67
|
+
return label;
|
|
68
|
+
// Keep the route anchor and attach the last segment to the moved box.
|
|
69
|
+
const anchor = label.leader?.at(-2);
|
|
70
|
+
const leader = anchor === undefined ? undefined : [
|
|
71
|
+
...label.leader.slice(0, -1),
|
|
72
|
+
{ x: Math.max(placed.x, Math.min(anchor.x, placed.x + placed.width)),
|
|
73
|
+
y: Math.max(placed.y, Math.min(anchor.y, placed.y + placed.height)) },
|
|
74
|
+
];
|
|
75
|
+
return { ...label, box: placed, ...(leader === undefined ? {} : { leader }) };
|
|
76
|
+
}
|
|
77
|
+
add(hit.x - clearance - box.width, choice.y);
|
|
78
|
+
add(hit.x + hit.width + clearance, choice.y);
|
|
79
|
+
add(choice.x, hit.y - clearance - box.height);
|
|
80
|
+
add(choice.x, hit.y + hit.height + clearance);
|
|
81
|
+
}
|
|
82
|
+
return label;
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=sceneLabelClearance.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sentropic/diagram-canvas",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"description": "Headless diagram scenes, layout, hit testing and pure presentation models/SVG helpers. No DOM or framework.",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
"main": "./dist/index.js",
|
|
58
58
|
"sideEffects": false,
|
|
59
59
|
"dependencies": {
|
|
60
|
-
"@sentropic/diagram-core": "0.
|
|
60
|
+
"@sentropic/diagram-core": "0.2.0"
|
|
61
61
|
},
|
|
62
62
|
"scripts": {
|
|
63
63
|
"build": "node ../../scripts/sync-diagram-spacing.mjs && tsc -p tsconfig.json",
|