@opsydyn/foldkit-zloz-force 0.3.0 → 0.5.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/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # Changelog
2
2
 
3
+ ## Unreleased
4
+
5
+ - Validate explicit request connection mode through core; preserve loose ordered endpoints without rewriting ports.
6
+
7
+ ## [0.5.0](https://github.com/opsydyn/fold-kit-experiments/compare/foldkit-zloz-force-v0.4.0...foldkit-zloz-force-v0.5.0) (2026-11-15)
8
+
9
+ ### Miscellaneous Chores
10
+
11
+ - **foldkit-zloz-force:** Synchronize foldkit-zloz versions
12
+
13
+ ## [0.4.0](https://github.com/opsydyn/fold-kit-experiments/compare/foldkit-zloz-force-v0.3.0...foldkit-zloz-force-v0.4.0) (2026-11-15)
14
+
15
+ ### Features
16
+
17
+ - **zloz:** qualify v2 portable adapter boundaries ([e19efa7](https://github.com/opsydyn/fold-kit-experiments/commit/e19efa782bcd22424d62ea545fafa8770120a49c))
18
+
3
19
  ## [0.3.0](https://github.com/opsydyn/fold-kit-experiments/compare/foldkit-zloz-force-v0.2.0...foldkit-zloz-force-v0.3.0) (2026-11-10)
4
20
 
5
21
  ### Miscellaneous Chores
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { supportsForceLayout } from "./eligibility.mjs";
2
2
  import { runForceLayout } from "@opsydyn/foldkit-viz/simulation";
3
- import { portBoundaryPosition, portPosition, validateGraph } from "@opsydyn/foldkit-zloz";
3
+ import { portBoundaryPosition, portPosition, validateLayoutRequest } from "@opsydyn/foldkit-zloz";
4
4
  //#region src/routes.ts
5
5
  const clearance = 8;
6
6
  const lead = 16;
@@ -166,7 +166,7 @@ function routeForceEdges(nodes, edges) {
166
166
  //#region src/layout.ts
167
167
  function arrangeWithForce(request) {
168
168
  const { graph, requestId, revision } = request;
169
- validateGraph(graph);
169
+ validateLayoutRequest(request);
170
170
  if (!supportsForceLayout(graph)) throw new Error("Force layout requires a flat graph of 1–100 nodes.");
171
171
  const radius = Math.max(...graph.nodes.map(({ size }) => Math.hypot(size.width, size.height) / 2)) + 12;
172
172
  const layout = runForceLayout({
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":[],"sources":["../src/routes.ts","../src/layout.ts","../src/index.ts"],"sourcesContent":["import {\n portPosition,\n portBoundaryPosition,\n type EdgeRoute,\n type ZlozEdge,\n type ZlozNode,\n type Point,\n} from '@opsydyn/foldkit-zloz';\n\nconst clearance = 8;\nconst lead = 16;\nconst directions = {\n left: { x: -1, y: 0 },\n right: { x: 1, y: 0 },\n top: { x: 0, y: -1 },\n bottom: { x: 0, y: 1 },\n};\n\nfunction obstacle(node: ZlozNode) {\n return {\n id: node.id,\n left: node.position.x - clearance,\n right: node.position.x + node.size.width + clearance,\n top: node.position.y - clearance,\n bottom: node.position.y + node.size.height + clearance,\n };\n}\n\nfunction segmentClear(a: Point, b: Point, obstacles: readonly ReturnType<typeof obstacle>[]) {\n return obstacles.every(({ left, right, top, bottom }) => {\n if (a.x === b.x)\n return (\n a.x <= left || a.x >= right || Math.max(a.y, b.y) <= top || Math.min(a.y, b.y) >= bottom\n );\n return a.y <= top || a.y >= bottom || Math.max(a.x, b.x) <= left || Math.min(a.x, b.x) >= right;\n });\n}\n\nfunction portLead(node: ZlozNode, id: string) {\n const port = node.ports.find((item) => item.id === id);\n if (!port) throw new Error(`Missing port ${id} on ${node.id}.`);\n const point = portPosition(node, id);\n const boundary = portBoundaryPosition(node, id);\n const direction = directions[port.side];\n return {\n point,\n // Curved-outline intersections can sit inside the conservative rectangular obstacle.\n // Leave that envelope before searching corridors, retaining the authored endpoint.\n outside: {\n x:\n direction.x < 0\n ? node.position.x - lead\n : direction.x > 0\n ? node.position.x + node.size.width + lead\n : boundary.x,\n y:\n direction.y < 0\n ? node.position.y - lead\n : direction.y > 0\n ? node.position.y + node.size.height + lead\n : boundary.y,\n },\n };\n}\n\ntype Segment = Readonly<{ from: Point; to: Point }>;\n\nfunction corridorPenalty(from: Point, to: Point, occupied: readonly Segment[]): number {\n if (from.x === to.x && from.y === to.y) return 0;\n const vertical = from.x === to.x;\n const axis = vertical ? 'y' : 'x';\n const across = vertical ? 'x' : 'y';\n const low = Math.min(from[axis], to[axis]);\n const high = Math.max(from[axis], to[axis]);\n let penalty = 0;\n for (const segment of occupied) {\n const otherVertical = segment.from.x === segment.to.x;\n if (vertical === otherVertical) {\n if (Math.abs(from[across] - segment.from[across]) >= 12) continue;\n const overlap =\n Math.min(high, Math.max(segment.from[axis], segment.to[axis])) -\n Math.max(low, Math.min(segment.from[axis], segment.to[axis]));\n penalty += Math.max(0, overlap) * 4;\n } else if (\n segment.from[axis] > low &&\n segment.from[axis] < high &&\n from[across] > Math.min(segment.from[across], segment.to[across]) &&\n from[across] < Math.max(segment.from[across], segment.to[across])\n )\n penalty += 48;\n }\n return penalty;\n}\n\nexport function routeForceEdges(\n nodes: readonly ZlozNode[],\n edges: readonly ZlozEdge[],\n): EdgeRoute[] {\n const byId = new Map(nodes.map((node) => [node.id, node]));\n const obstacles = nodes.map(obstacle);\n const columns = [...new Set(obstacles.flatMap(({ left, right }) => [left, right]))];\n const rows = [...new Set(obstacles.flatMap(({ top, bottom }) => [top, bottom]))];\n const occupied: Segment[] = [];\n return edges.flatMap((edge) => {\n const source = byId.get(edge.source.node);\n const target = byId.get(edge.target.node);\n if (!source || !target || edge.pathStyle !== undefined) return [];\n const start = portLead(source, edge.source.port);\n const end = portLead(target, edge.target.port);\n const a = start.outside;\n const b = end.outside;\n if (\n !segmentClear(\n start.point,\n a,\n obstacles.filter((item) => item.id !== source.id),\n ) ||\n !segmentClear(\n b,\n end.point,\n obstacles.filter((item) => item.id !== target.id),\n )\n )\n return [];\n\n const laneColumns = occupied.flatMap(({ from }) => [from.x - 12, from.x + 12]);\n const laneRows = occupied.flatMap(({ from }) => [from.y - 12, from.y + 12]);\n const candidates = [\n ...[...new Set([a.x, b.x, (a.x + b.x) / 2, ...columns, ...laneColumns])].map((x) => [\n a,\n { x, y: a.y },\n { x, y: b.y },\n b,\n ]),\n ...[...new Set([a.y, b.y, (a.y + b.y) / 2, ...rows, ...laneRows])].map((y) => [\n a,\n { x: a.x, y },\n { x: b.x, y },\n b,\n ]),\n ];\n const directDistance = Math.abs(a.x - b.x) + Math.abs(a.y - b.y);\n let shortest: Point[] | undefined;\n let distance = Infinity;\n for (const points of candidates) {\n let length = 0;\n let clear = true;\n for (let index = 1; index < points.length; index++) {\n const from = points[index - 1];\n const to = points[index];\n if (!from || !to || !segmentClear(from, to, obstacles)) {\n clear = false;\n break;\n }\n length +=\n Math.abs(to.x - from.x) + Math.abs(to.y - from.y) + corridorPenalty(from, to, occupied);\n }\n if (clear && length < distance) {\n distance = length;\n shortest = points;\n if (length === directDistance) break;\n }\n }\n if (!shortest) return [];\n for (let index = 1; index < shortest.length; index++) {\n const from = shortest[index - 1];\n const to = shortest[index];\n if (from && to && (from.x !== to.x || from.y !== to.y)) occupied.push({ from, to });\n }\n const points = [start.point, ...shortest, end.point];\n return [\n {\n id: edge.id,\n points: points.filter((point, index) => {\n const previous = points[index - 1];\n return previous?.x !== point.x || previous.y !== point.y;\n }),\n },\n ];\n });\n}\n","import { runForceLayout } from '@opsydyn/foldkit-viz/simulation';\nimport { validateGraph, type LayoutRequest, type LayoutResult } from '@opsydyn/foldkit-zloz';\n\nimport { supportsForceLayout } from './eligibility';\nimport { routeForceEdges } from './routes';\n\nexport function arrangeWithForce(request: LayoutRequest): LayoutResult {\n const { graph, requestId, revision } = request;\n validateGraph(graph);\n if (!supportsForceLayout(graph))\n throw new Error('Force layout requires a flat graph of 1–100 nodes.');\n\n // Use a circle that encloses the largest card, with room around its corners.\n const radius =\n Math.max(...graph.nodes.map(({ size }) => Math.hypot(size.width, size.height) / 2)) + 12;\n const layout = runForceLayout({\n nodes: graph.nodes,\n links: graph.edges.map(({ source, target }) => ({ source: source.node, target: target.node })),\n collideRadius: radius,\n linkDistance: 2 * radius + 24,\n });\n const centers = new Map(layout.nodes.map((node) => [node.id, node]));\n const cards = graph.nodes.map(({ id, size }) => {\n const center = centers.get(id);\n if (!center || !Number.isFinite(center.x) || !Number.isFinite(center.y))\n throw new Error(`Force layout returned an invalid position for ${id}.`);\n return { id, x: center.x - size.width / 2, y: center.y - size.height / 2, size };\n });\n if (\n cards.some((card, index) =>\n cards\n .slice(index + 1)\n .some(\n (other) =>\n card.x < other.x + other.size.width &&\n other.x < card.x + card.size.width &&\n card.y < other.y + other.size.height &&\n other.y < card.y + card.size.height,\n ),\n )\n )\n throw new Error('Force layout left overlapping cards.');\n const placed = graph.nodes.map((node, index) => {\n const card = cards[index];\n if (!card) throw new Error(`Missing force position for ${node.id}.`);\n return { ...node, position: { x: card.x, y: card.y } };\n });\n return {\n requestId,\n revision,\n positions: cards.map(({ id, x, y }) => ({ id, x, y })),\n routes: routeForceEdges(placed, graph.edges),\n };\n}\n","/**\n * Optional bounded force layout adapter for flat graphs.\n * @module Force\n */\nimport type { LayoutEngine } from '@opsydyn/foldkit-zloz';\n\nimport { arrangeWithForce } from './layout';\n\nexport { supportsForceLayout } from './eligibility';\n\n/** Bounded, deterministic force layout using measured card sizes. */\nexport function createForceLayout(): LayoutEngine {\n return async (request) => arrangeWithForce(request);\n}\n"],"mappings":";;;;AASA,MAAM,YAAY;AAClB,MAAM,OAAO;AACb,MAAM,aAAa;CACjB,MAAM;EAAE,GAAG;EAAI,GAAG;EAAG;CACrB,OAAO;EAAE,GAAG;EAAG,GAAG;EAAG;CACrB,KAAK;EAAE,GAAG;EAAG,GAAG;EAAI;CACpB,QAAQ;EAAE,GAAG;EAAG,GAAG;EAAG;CACvB;AAED,SAAS,SAAS,MAAgB;AAChC,QAAO;EACL,IAAI,KAAK;EACT,MAAM,KAAK,SAAS,IAAI;EACxB,OAAO,KAAK,SAAS,IAAI,KAAK,KAAK,QAAQ;EAC3C,KAAK,KAAK,SAAS,IAAI;EACvB,QAAQ,KAAK,SAAS,IAAI,KAAK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,gBAAgB,QAAQ,MAAM,MAAM;EAC7C;;;;;ACzCH,SAAgB,oBAAkC;AAChD,QAAO,OAAO,YAAY,iBAAiB,QAAQ"}
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/routes.ts","../src/layout.ts","../src/index.ts"],"sourcesContent":["import {\n portPosition,\n portBoundaryPosition,\n type EdgeRoute,\n type ZlozEdge,\n type ZlozNode,\n type Point,\n} from '@opsydyn/foldkit-zloz';\n\nconst clearance = 8;\nconst lead = 16;\nconst directions = {\n left: { x: -1, y: 0 },\n right: { x: 1, y: 0 },\n top: { x: 0, y: -1 },\n bottom: { x: 0, y: 1 },\n};\n\nfunction obstacle(node: ZlozNode) {\n return {\n id: node.id,\n left: node.position.x - clearance,\n right: node.position.x + node.size.width + clearance,\n top: node.position.y - clearance,\n bottom: node.position.y + node.size.height + clearance,\n };\n}\n\nfunction segmentClear(a: Point, b: Point, obstacles: readonly ReturnType<typeof obstacle>[]) {\n return obstacles.every(({ left, right, top, bottom }) => {\n if (a.x === b.x)\n return (\n a.x <= left || a.x >= right || Math.max(a.y, b.y) <= top || Math.min(a.y, b.y) >= bottom\n );\n return a.y <= top || a.y >= bottom || Math.max(a.x, b.x) <= left || Math.min(a.x, b.x) >= right;\n });\n}\n\nfunction portLead(node: ZlozNode, id: string) {\n const port = node.ports.find((item) => item.id === id);\n if (!port) throw new Error(`Missing port ${id} on ${node.id}.`);\n const point = portPosition(node, id);\n const boundary = portBoundaryPosition(node, id);\n const direction = directions[port.side];\n return {\n point,\n // Curved-outline intersections can sit inside the conservative rectangular obstacle.\n // Leave that envelope before searching corridors, retaining the authored endpoint.\n outside: {\n x:\n direction.x < 0\n ? node.position.x - lead\n : direction.x > 0\n ? node.position.x + node.size.width + lead\n : boundary.x,\n y:\n direction.y < 0\n ? node.position.y - lead\n : direction.y > 0\n ? node.position.y + node.size.height + lead\n : boundary.y,\n },\n };\n}\n\ntype Segment = Readonly<{ from: Point; to: Point }>;\n\nfunction corridorPenalty(from: Point, to: Point, occupied: readonly Segment[]): number {\n if (from.x === to.x && from.y === to.y) return 0;\n const vertical = from.x === to.x;\n const axis = vertical ? 'y' : 'x';\n const across = vertical ? 'x' : 'y';\n const low = Math.min(from[axis], to[axis]);\n const high = Math.max(from[axis], to[axis]);\n let penalty = 0;\n for (const segment of occupied) {\n const otherVertical = segment.from.x === segment.to.x;\n if (vertical === otherVertical) {\n if (Math.abs(from[across] - segment.from[across]) >= 12) continue;\n const overlap =\n Math.min(high, Math.max(segment.from[axis], segment.to[axis])) -\n Math.max(low, Math.min(segment.from[axis], segment.to[axis]));\n penalty += Math.max(0, overlap) * 4;\n } else if (\n segment.from[axis] > low &&\n segment.from[axis] < high &&\n from[across] > Math.min(segment.from[across], segment.to[across]) &&\n from[across] < Math.max(segment.from[across], segment.to[across])\n )\n penalty += 48;\n }\n return penalty;\n}\n\nexport function routeForceEdges(\n nodes: readonly ZlozNode[],\n edges: readonly ZlozEdge[],\n): EdgeRoute[] {\n const byId = new Map(nodes.map((node) => [node.id, node]));\n const obstacles = nodes.map(obstacle);\n const columns = [...new Set(obstacles.flatMap(({ left, right }) => [left, right]))];\n const rows = [...new Set(obstacles.flatMap(({ top, bottom }) => [top, bottom]))];\n const occupied: Segment[] = [];\n return edges.flatMap((edge) => {\n const source = byId.get(edge.source.node);\n const target = byId.get(edge.target.node);\n if (!source || !target || edge.pathStyle !== undefined) return [];\n const start = portLead(source, edge.source.port);\n const end = portLead(target, edge.target.port);\n const a = start.outside;\n const b = end.outside;\n if (\n !segmentClear(\n start.point,\n a,\n obstacles.filter((item) => item.id !== source.id),\n ) ||\n !segmentClear(\n b,\n end.point,\n obstacles.filter((item) => item.id !== target.id),\n )\n )\n return [];\n\n const laneColumns = occupied.flatMap(({ from }) => [from.x - 12, from.x + 12]);\n const laneRows = occupied.flatMap(({ from }) => [from.y - 12, from.y + 12]);\n const candidates = [\n ...[...new Set([a.x, b.x, (a.x + b.x) / 2, ...columns, ...laneColumns])].map((x) => [\n a,\n { x, y: a.y },\n { x, y: b.y },\n b,\n ]),\n ...[...new Set([a.y, b.y, (a.y + b.y) / 2, ...rows, ...laneRows])].map((y) => [\n a,\n { x: a.x, y },\n { x: b.x, y },\n b,\n ]),\n ];\n const directDistance = Math.abs(a.x - b.x) + Math.abs(a.y - b.y);\n let shortest: Point[] | undefined;\n let distance = Infinity;\n for (const points of candidates) {\n let length = 0;\n let clear = true;\n for (let index = 1; index < points.length; index++) {\n const from = points[index - 1];\n const to = points[index];\n if (!from || !to || !segmentClear(from, to, obstacles)) {\n clear = false;\n break;\n }\n length +=\n Math.abs(to.x - from.x) + Math.abs(to.y - from.y) + corridorPenalty(from, to, occupied);\n }\n if (clear && length < distance) {\n distance = length;\n shortest = points;\n if (length === directDistance) break;\n }\n }\n if (!shortest) return [];\n for (let index = 1; index < shortest.length; index++) {\n const from = shortest[index - 1];\n const to = shortest[index];\n if (from && to && (from.x !== to.x || from.y !== to.y)) occupied.push({ from, to });\n }\n const points = [start.point, ...shortest, end.point];\n return [\n {\n id: edge.id,\n points: points.filter((point, index) => {\n const previous = points[index - 1];\n return previous?.x !== point.x || previous.y !== point.y;\n }),\n },\n ];\n });\n}\n","import { runForceLayout } from '@opsydyn/foldkit-viz/simulation';\nimport {\n validateLayoutRequest,\n type LayoutRequest,\n type LayoutResult,\n} from '@opsydyn/foldkit-zloz';\n\nimport { supportsForceLayout } from './eligibility';\nimport { routeForceEdges } from './routes';\n\nexport function arrangeWithForce(request: LayoutRequest): LayoutResult {\n const { graph, requestId, revision } = request;\n validateLayoutRequest(request);\n if (!supportsForceLayout(graph))\n throw new Error('Force layout requires a flat graph of 1–100 nodes.');\n\n // Use a circle that encloses the largest card, with room around its corners.\n const radius =\n Math.max(...graph.nodes.map(({ size }) => Math.hypot(size.width, size.height) / 2)) + 12;\n const layout = runForceLayout({\n nodes: graph.nodes,\n links: graph.edges.map(({ source, target }) => ({ source: source.node, target: target.node })),\n collideRadius: radius,\n linkDistance: 2 * radius + 24,\n });\n const centers = new Map(layout.nodes.map((node) => [node.id, node]));\n const cards = graph.nodes.map(({ id, size }) => {\n const center = centers.get(id);\n if (!center || !Number.isFinite(center.x) || !Number.isFinite(center.y))\n throw new Error(`Force layout returned an invalid position for ${id}.`);\n return { id, x: center.x - size.width / 2, y: center.y - size.height / 2, size };\n });\n if (\n cards.some((card, index) =>\n cards\n .slice(index + 1)\n .some(\n (other) =>\n card.x < other.x + other.size.width &&\n other.x < card.x + card.size.width &&\n card.y < other.y + other.size.height &&\n other.y < card.y + card.size.height,\n ),\n )\n )\n throw new Error('Force layout left overlapping cards.');\n const placed = graph.nodes.map((node, index) => {\n const card = cards[index];\n if (!card) throw new Error(`Missing force position for ${node.id}.`);\n return { ...node, position: { x: card.x, y: card.y } };\n });\n return {\n requestId,\n revision,\n positions: cards.map(({ id, x, y }) => ({ id, x, y })),\n routes: routeForceEdges(placed, graph.edges),\n };\n}\n","/**\n * Optional bounded force layout adapter for flat graphs.\n * @module Force\n */\nimport type { LayoutEngine } from '@opsydyn/foldkit-zloz';\n\nimport { arrangeWithForce } from './layout';\n\nexport { supportsForceLayout } from './eligibility';\n\n/** Bounded, deterministic force layout using measured card sizes. */\nexport function createForceLayout(): LayoutEngine {\n return async (request) => arrangeWithForce(request);\n}\n"],"mappings":";;;;AASA,MAAM,YAAY;AAClB,MAAM,OAAO;AACb,MAAM,aAAa;CACjB,MAAM;EAAE,GAAG;EAAI,GAAG;EAAG;CACrB,OAAO;EAAE,GAAG;EAAG,GAAG;EAAG;CACrB,KAAK;EAAE,GAAG;EAAG,GAAG;EAAI;CACpB,QAAQ;EAAE,GAAG;EAAG,GAAG;EAAG;CACvB;AAED,SAAS,SAAS,MAAgB;AAChC,QAAO;EACL,IAAI,KAAK;EACT,MAAM,KAAK,SAAS,IAAI;EACxB,OAAO,KAAK,SAAS,IAAI,KAAK,KAAK,QAAQ;EAC3C,KAAK,KAAK,SAAS,IAAI;EACvB,QAAQ,KAAK,SAAS,IAAI,KAAK,KAAK,SAAS;EAC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AQ,MAAM,MAAM;EAC7C;;;;;AC7CH,SAAgB,oBAAkC;AAChD,QAAO,OAAO,YAAY,iBAAiB,QAAQ"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opsydyn/foldkit-zloz-force",
3
- "version": "0.3.0",
3
+ "version": "0.5.0",
4
4
  "description": "Optional force layout and card-avoiding routing for Foldkit Zloz.",
5
5
  "keywords": [
6
6
  "diagram",
@@ -49,15 +49,17 @@
49
49
  "scripts": {
50
50
  "build": "bun run --filter @opsydyn/foldkit-zloz build && bun run --filter @opsydyn/foldkit-viz build && tsdown",
51
51
  "typecheck": "bun run --filter @opsydyn/foldkit-zloz build && bun run --filter @opsydyn/foldkit-viz build && tsc --noEmit",
52
- "test": "bun run build && bun test ./test",
52
+ "test": "bun run test:unit && bun run test:package",
53
+ "test:unit": "bun run build && bun test ./test --path-ignore-patterns='**/package.test.ts'",
54
+ "test:package": "bun test ./test/package.test.ts",
53
55
  "pack:check": "bun run build && publint && bun ../../tools/pack-zloz.ts foldkit-zloz-force"
54
56
  },
55
57
  "dependencies": {
56
- "@opsydyn/foldkit-viz": "0.16.0"
58
+ "@opsydyn/foldkit-viz": "0.18.0"
57
59
  },
58
60
  "devDependencies": {
59
61
  "@arethetypeswrong/cli": "0.18.5",
60
- "@opsydyn/foldkit-zloz": "0.3.0",
62
+ "@opsydyn/foldkit-zloz": "0.5.0",
61
63
  "@types/node": "^22.0.0",
62
64
  "bun-types": "^1.3.14",
63
65
  "publint": "0.3.25",
@@ -65,7 +67,7 @@
65
67
  "typescript": "^6.0.3"
66
68
  },
67
69
  "peerDependencies": {
68
- "@opsydyn/foldkit-zloz": "0.3.0"
70
+ "@opsydyn/foldkit-zloz": "0.5.0"
69
71
  },
70
72
  "engines": {
71
73
  "node": ">=22.0.0"