@sentropic/diagram-canvas 0.2.0 → 0.2.2

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/camera.d.ts CHANGED
@@ -37,12 +37,14 @@ 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;
43
45
  }
44
46
  export interface CameraFit {
45
- /** The §5.1 fit box, CSS px. */
47
+ /** The usable §5.1 fit box, excluding scrollbars, CSS px. */
46
48
  readonly fitWidth: number;
47
49
  readonly fitHeight: number;
48
50
  /** Zoom framing the whole drawing on both axes, never above the maximum display scale. */
package/dist/camera.js CHANGED
@@ -36,7 +36,10 @@ export function fitBox(viewWidth, windowHeight) {
36
36
  export function cameraFit(input) {
37
37
  const maxZoom = Math.max(DEFAULT_MIN_READABLE_SCALE, input.maxDisplayScale ?? DEFAULT_MAX_DISPLAY_SCALE);
38
38
  const readableFloor = Math.min(maxZoom, Math.max(DEFAULT_MIN_READABLE_SCALE, input.minReadableScale ?? DEFAULT_MIN_READABLE_SCALE));
39
- const box = fitBox(input.viewWidth, input.windowHeight);
39
+ const frame = fitBox(input.viewWidth, input.windowHeight);
40
+ // Hosts add the scrollbar back to the outer height; fit and placement use
41
+ // only the drawable area, keeping the combined height inside the fit box.
42
+ const box = { width: frame.width, height: Math.max(0, frame.height - (input.scrollbarHeight ?? 0)) };
40
43
  const bounds = input.bounds;
41
44
  if (bounds === undefined || !(bounds.width > 0) || !(bounds.height > 0)) {
42
45
  // Empty scene: readout 100 %.
@@ -46,7 +49,7 @@ export function cameraFit(input) {
46
49
  // All hosts and camera inputs share this range. A small frame or a large
47
50
  // unfolded scene reaches the floor, then scrolls instead of shrinking text.
48
51
  const zoom = Math.max(readableFloor, zoomFit);
49
- const fitHeight = zoomFit === maxZoom ? Math.min(box.height, Math.ceil((bounds.height * zoom + 2 * FIT_PAINT_INSET) * 64) / 64) : box.height;
52
+ const fitHeight = Math.min(box.height, Math.ceil((bounds.height * zoom + 2 * FIT_PAINT_INSET) * 64) / 64);
50
53
  return { fitWidth: box.width, fitHeight, zoomFit, zoom, minZoom: zoom, maxZoom, pans: zoom > zoomFit };
51
54
  }
52
55
  /** Z-1/Z-7: any requested zoom lands inside `[minZoom, maxZoom]` of the current fit. */
@@ -194,6 +194,7 @@ export interface SceneCameraInput {
194
194
  readonly bounds?: Rect;
195
195
  readonly viewWidth: number;
196
196
  readonly windowHeight: number;
197
+ readonly scrollbarHeight?: number;
197
198
  readonly maxDisplayScale?: number;
198
199
  readonly minReadableScale?: number;
199
200
  }
@@ -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 { measureSceneBox } from "../layout/sceneMeasure.js";
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
@@ -167,7 +167,7 @@ export function resolveScene(result, input) {
167
167
  const measuredLabels = new Map((result.measurements?.slots ?? []).filter((slot) => slot.role === "edge-label").map((slot) => [slot.occurrence, slot.layout]));
168
168
  const headerHeights = new Map((result.measurements?.headers ?? []).map((header) => [header.occurrence, header.height]));
169
169
  const blockers = [];
170
- for (const node of result.labelBoxes === undefined || input.document.profileRefs.includes("erd@1") ? nodes : []) {
170
+ for (const node of nodes) {
171
171
  if (node.kind !== "zone" || node.collapsed) {
172
172
  blockers.push(node.rect);
173
173
  continue;
@@ -182,7 +182,10 @@ export function resolveScene(result, input) {
182
182
  width: node.rect.width,
183
183
  height: CONTENT_INSET + height + ZONE_GEOMETRY.ruleHeight / 2 + ruleClearance,
184
184
  });
185
- const reserve = measureSceneBox(node.presentation, node.occurrence, node.rect.width, false, input.measureLines, input.metrics?.containerPadding).footerReserve;
185
+ // A zone footer has one allocated line. Reserve its fixed chrome from
186
+ // the presentation instead of measuring the scene again during replay.
187
+ const reserve = node.presentation?.text.footer?.trim()
188
+ ? lineBoxHeight(ZONE_GEOMETRY.footer.size) + ZONE_GEOMETRY.ruleHeight + 2 * ZONE_GEOMETRY.ruleMargin + ZONE_GEOMETRY.footerBottomInset : 0;
186
189
  if (reserve > 0)
187
190
  blockers.push({ x: node.rect.x, y: node.rect.y + node.rect.height - reserve - ZONE_GEOMETRY.borderWidth - ruleClearance,
188
191
  width: node.rect.width, height: reserve + ZONE_GEOMETRY.borderWidth + ruleClearance });
@@ -273,7 +276,6 @@ export function resolveScene(result, input) {
273
276
  labels.push({ occurrence: id, lines: labelLines.get(id) ?? [], box: placement.box, ...(erd ? { erd: true } : {}), ...("leader" in placement ? { leader: placement.leader } : {}),
274
277
  ...(result.edgeBundles?.[id] === undefined ? {} : { title: result.edgeBundles[id].title }),
275
278
  ...(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
279
  }
278
280
  const badges = [];
279
281
  for (const step of flows.scenarios.flatMap((scenario) => scenario.steps)) {
@@ -291,6 +293,17 @@ export function resolveScene(result, input) {
291
293
  for (const arrows of Object.values(result.arrowBoxes ?? {}))
292
294
  for (const arrow of arrows)
293
295
  bounds = bounds === undefined ? arrow.box : rectUnion(bounds, arrow.box);
296
+ // Non-scaling connector paint is widest in world units at the readability
297
+ // floor. Reserve it as well as the label-padding token supplied by hosts.
298
+ const clearedLabels = clearSceneLabels(labels, edges.map(edge => edge.points), [...blockers, ...badges.map(badge => badge.box), ...edges.flatMap(edge => edge.arrows.map(arrow => arrow.box))], margin + ZONE_GEOMETRY.borderWidth / (2 * DEFAULT_MIN_READABLE_SCALE));
299
+ for (const { box } of clearedLabels) {
300
+ const padded = { x: box.x - margin, y: box.y - margin, width: box.width + 2 * margin, height: box.height + 2 * margin };
301
+ bounds = bounds === undefined ? padded : rectUnion(bounds, padded);
302
+ }
303
+ if (bounds !== undefined) {
304
+ const x = Math.floor(bounds.x * 64) / 64, y = Math.floor(bounds.y * 64) / 64;
305
+ bounds = { x, y, width: Math.ceil((bounds.x + bounds.width) * 64) / 64 - x, height: Math.ceil((bounds.y + bounds.height) * 64) / 64 - y };
306
+ }
294
307
  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
308
  return {
296
309
  badges, flows, flowTargets, nodeTargets, flowEnds, flowRepresentatives, flowAncestors,
@@ -298,7 +311,7 @@ export function resolveScene(result, input) {
298
311
  contentSize: sceneContentSize(result),
299
312
  nodes,
300
313
  edges,
301
- labels,
314
+ labels: clearedLabels,
302
315
  ...(bounds === undefined ? {} : { bounds }),
303
316
  orientations: result.orientations ?? {},
304
317
  };
@@ -318,6 +331,7 @@ export function sceneFit(input) {
318
331
  ...(input.bounds === undefined ? {} : { bounds: input.bounds }),
319
332
  viewWidth: input.viewWidth,
320
333
  windowHeight: input.windowHeight,
334
+ ...(input.scrollbarHeight === undefined ? {} : { scrollbarHeight: input.scrollbarHeight }),
321
335
  ...(input.maxDisplayScale === undefined ? {} : { maxDisplayScale: input.maxDisplayScale }),
322
336
  ...(input.minReadableScale === undefined ? {} : { minReadableScale: input.minReadableScale }),
323
337
  });
@@ -339,13 +353,13 @@ export function scenePlacement(bounds, zoom, fit) {
339
353
  if (bounds === undefined) {
340
354
  return { contentWidth: 0, contentHeight: 0, translateX: 0, translateY: 0, overflowX: false, overflowY: false };
341
355
  }
342
- const contentWidth = bounds.width * zoom;
343
- const contentHeight = bounds.height * zoom;
356
+ const contentWidth = bounds.width * zoom + 2 * FIT_PAINT_INSET;
357
+ const contentHeight = bounds.height * zoom + 2 * FIT_PAINT_INSET;
344
358
  // Half a CSS px of tolerance: a fit computed by division must not scroll.
345
359
  const overflowX = contentWidth > fit.fitWidth + 0.5;
346
360
  const overflowY = contentHeight > fit.fitHeight + 0.5;
347
- const startX = overflowX ? 0 : (fit.fitWidth - contentWidth) / 2;
348
- const startY = overflowY ? 0 : (fit.fitHeight - contentHeight) / 2;
361
+ const startX = overflowX ? FIT_PAINT_INSET : (fit.fitWidth - bounds.width * zoom) / 2;
362
+ const startY = overflowY ? FIT_PAINT_INSET : (fit.fitHeight - bounds.height * zoom) / 2;
349
363
  return {
350
364
  contentWidth,
351
365
  contentHeight,
@@ -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.0",
3
+ "version": "0.2.2",
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.1.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",