@statelyai/layout 0.0.2 → 0.0.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@statelyai/layout",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "Extensible, graph-native layout algorithms for @statelyai/graph",
5
5
  "license": "EPL-2.0 OR GPL-3.0-or-later",
6
6
  "repository": {
@@ -27,6 +27,10 @@
27
27
  "types": "./src/elkjs/index.ts",
28
28
  "default": "./dist/elkjs/index.mjs"
29
29
  },
30
+ "./lib/elk.bundled.js": {
31
+ "types": "./src/elkjs/index.ts",
32
+ "default": "./dist/elkjs/index.mjs"
33
+ },
30
34
  "./layered": {
31
35
  "types": "./src/layered/index.ts",
32
36
  "default": "./dist/layered/index.mjs"
@@ -14,6 +14,7 @@ import type {
14
14
  ElkConstructorArguments,
15
15
  ElkLayoutAlgorithmDescription,
16
16
  ElkEdge,
17
+ ElkLabel,
17
18
  ElkLayoutArguments,
18
19
  ElkLayoutCategoryDescription,
19
20
  ElkLayoutOptionDescription,
@@ -25,6 +26,13 @@ import type {
25
26
  LaidOutElkNode,
26
27
  } from "./types";
27
28
 
29
+ function isLayoutEdgeLabel(label: ElkLabel): boolean {
30
+ return (
31
+ getBooleanOption(label.layoutOptions ?? {}, "noLayout") !== true &&
32
+ (Boolean(label.text) || label.width !== undefined || label.height !== undefined)
33
+ );
34
+ }
35
+
28
36
  export type {
29
37
  ElkConstructorArguments,
30
38
  ElkCommonDescription,
@@ -824,8 +832,11 @@ function applyNodeMicroLayout(
824
832
  constraints.includes("PORT_LABELS") &&
825
833
  !constraints.includes("NODE_LABELS") &&
826
834
  !constraints.includes("MINIMUM_SIZE");
827
- let width = effectivelyFixedPortLabelSize ? (node.width ?? 0) : 0;
828
- let height = effectivelyFixedPortLabelSize ? (node.height ?? 0) : 0;
835
+ const preserveComputedCompoundSize = (node.children?.length ?? 0) > 0;
836
+ let width =
837
+ effectivelyFixedPortLabelSize || preserveComputedCompoundSize ? (node.width ?? 0) : 0;
838
+ let height =
839
+ effectivelyFixedPortLabelSize || preserveComputedCompoundSize ? (node.height ?? 0) : 0;
829
840
  let insideHorizontalInset = 0;
830
841
  let insideVerticalInset = 0;
831
842
  const insideLabelCells = new Map<string, { width: number; height: number }>();
@@ -845,8 +856,12 @@ function applyNodeMicroLayout(
845
856
  }
846
857
  width = Math.max(width, (Math.max(sideCounts.NORTH, sideCounts.SOUTH) + 1) * spacing);
847
858
  height = Math.max(height, (Math.max(sideCounts.EAST, sideCounts.WEST) + 1) * spacing);
848
- if (sideCounts.NORTH === 0 && sideCounts.SOUTH === 0) width = 0;
849
- if (sideCounts.EAST === 0 && sideCounts.WEST === 0) height = 0;
859
+ if (!preserveComputedCompoundSize && sideCounts.NORTH === 0 && sideCounts.SOUTH === 0) {
860
+ width = 0;
861
+ }
862
+ if (!preserveComputedCompoundSize && sideCounts.EAST === 0 && sideCounts.WEST === 0) {
863
+ height = 0;
864
+ }
850
865
  }
851
866
  if (constraints.includes("NODE_LABELS")) {
852
867
  for (const label of node.labels ?? []) {
@@ -1257,10 +1272,7 @@ function toGraph(root: ElkNode, globalOptions: Readonly<Record<string, unknown>>
1257
1272
  if (isInsideSelfLoop(root, edge)) return [];
1258
1273
  const source = endpoint(edge.sources?.[0] ?? edge.source, portOwnerById);
1259
1274
  const target = endpoint(edge.targets?.[0] ?? edge.target, portOwnerById);
1260
- const labels = (edge.labels ?? []).filter(
1261
- (label) =>
1262
- Boolean(label.text) && getBooleanOption(label.layoutOptions ?? {}, "noLayout") !== true,
1263
- );
1275
+ const labels = (edge.labels ?? []).filter(isLayoutEdgeLabel);
1264
1276
  const labelLabelSpacing = getNumberOption(globalOptions, "spacing.labelLabel") ?? 0;
1265
1277
  const labelWidth = Math.max(0, ...labels.map((label) => label.width ?? 0));
1266
1278
  const labelHeight =
@@ -1380,7 +1392,7 @@ function applyLayout(
1380
1392
  label.y ??= 0;
1381
1393
  continue;
1382
1394
  }
1383
- if (!label.text) {
1395
+ if (!isLayoutEdgeLabel(label)) {
1384
1396
  label.x ??= 0;
1385
1397
  label.y ??= 0;
1386
1398
  continue;
@@ -1436,10 +1448,7 @@ function normalizeElkGraphBounds(
1436
1448
  );
1437
1449
  }
1438
1450
  for (const edge of root.edges ?? []) {
1439
- const laidOutLabels = (edge.labels ?? []).filter(
1440
- (label) =>
1441
- Boolean(label.text) && getBooleanOption(label.layoutOptions ?? {}, "noLayout") !== true,
1442
- );
1451
+ const laidOutLabels = (edge.labels ?? []).filter(isLayoutEdgeLabel);
1443
1452
  minimumX = Math.min(minimumX, ...laidOutLabels.map((label) => label.x ?? 0));
1444
1453
  minimumY = Math.min(minimumY, ...laidOutLabels.map((label) => label.y ?? 0));
1445
1454
  if (getBooleanOption(edge.layoutOptions ?? {}, "noLayout") !== true) {
@@ -1466,7 +1475,7 @@ function normalizeElkGraphBounds(
1466
1475
  point.y += shiftY;
1467
1476
  }
1468
1477
  }
1469
- for (const label of (edge.labels ?? []).filter((candidate) => Boolean(candidate.text))) {
1478
+ for (const label of (edge.labels ?? []).filter(isLayoutEdgeLabel)) {
1470
1479
  label.x = (label.x ?? 0) + shiftX;
1471
1480
  label.y = (label.y ?? 0) + shiftY;
1472
1481
  }
@@ -1571,11 +1580,7 @@ function normalizeElkGraphBounds(
1571
1580
  ]),
1572
1581
  ...(root.edges ?? []).flatMap((edge) =>
1573
1582
  (edge.labels ?? [])
1574
- .filter(
1575
- (label) =>
1576
- Boolean(label.text) &&
1577
- getBooleanOption(label.layoutOptions ?? {}, "noLayout") !== true,
1578
- )
1583
+ .filter(isLayoutEdgeLabel)
1579
1584
  .map((label) => (label.x ?? 0) + (label.width ?? 0)),
1580
1585
  ),
1581
1586
  ...(root.edges ?? []).flatMap((edge) =>
@@ -1610,11 +1615,7 @@ function normalizeElkGraphBounds(
1610
1615
  ]),
1611
1616
  ...(root.edges ?? []).flatMap((edge) =>
1612
1617
  (edge.labels ?? [])
1613
- .filter(
1614
- (label) =>
1615
- Boolean(label.text) &&
1616
- getBooleanOption(label.layoutOptions ?? {}, "noLayout") !== true,
1617
- )
1618
+ .filter(isLayoutEdgeLabel)
1618
1619
  .map(
1619
1620
  (label) =>
1620
1621
  (label.y ?? 0) +