@opsydyn/foldkit-zloz-force 0.0.0-stage → 0.3.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 ADDED
@@ -0,0 +1,30 @@
1
+ # Changelog
2
+
3
+ ## [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
+
5
+ ### Miscellaneous Chores
6
+
7
+ - **foldkit-zloz-force:** Synchronize foldkit-zloz versions
8
+
9
+ ## [0.2.0](https://github.com/opsydyn/fold-kit-experiments/compare/foldkit-zloz-force-v0.1.1...foldkit-zloz-force-v0.2.0) (2026-11-09)
10
+
11
+ ### Miscellaneous Chores
12
+
13
+ - **foldkit-zloz-force:** Synchronize foldkit-zloz versions
14
+
15
+ ## [0.1.1](https://github.com/opsydyn/fold-kit-experiments/compare/foldkit-zloz-force-v0.1.0...foldkit-zloz-force-v0.1.1) (2026-11-09)
16
+
17
+ ### Bug Fixes
18
+
19
+ - **ci:** modernize actions and correct release workspace packing ([538c4ab](https://github.com/opsydyn/fold-kit-experiments/commit/538c4abe57213ebdfec8b9dc676422b3f61983a8))
20
+ - **ci:** update Node 24 actions and repair release packing ([337af66](https://github.com/opsydyn/fold-kit-experiments/commit/337af66382435266a6ce86cc9c2c60adbb6ca21f))
21
+
22
+ ## 0.1.0 (unreleased)
23
+
24
+ - Keep routing leads outside the conservative node envelope when the core uses a
25
+ curved outline, retaining canonical shaped/authored endpoints.
26
+
27
+ - Extend custom-anchor routing leads beyond the hinted node boundary.
28
+
29
+ - Extract the force layout adapter and automatic card-avoiding edge routing
30
+ from the Zloz workbench into a reusable package.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Alan P Currie
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,76 @@
1
- # Temporary Holding Version
1
+ # @opsydyn/foldkit-zloz-force
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Force layout and automatic orthogonal routing for Foldkit Zloz.
4
+ The adapter uses the FoldKit Viz simulation and requires Node.js 22 or newer
5
+ for headless use.
6
+
7
+ ## Usage
8
+
9
+ Install `@opsydyn/foldkit-zloz-force` with its peer `@opsydyn/foldkit-zloz`.
10
+ FoldKit Viz is a runtime dependency.
11
+
12
+ ```typescript
13
+ import { applyLayout, createEditor, layoutRequest } from '@opsydyn/foldkit-zloz';
14
+ import { createForceLayout } from '@opsydyn/foldkit-zloz-force';
15
+ import { supportsForceLayout } from '@opsydyn/foldkit-zloz-force/eligibility';
16
+
17
+ const editor = createEditor({
18
+ nodes: [
19
+ {
20
+ id: 'start',
21
+ label: 'Start',
22
+ position: { x: 0, y: 0 },
23
+ size: { width: 180, height: 100 },
24
+ ports: [],
25
+ },
26
+ ],
27
+ edges: [],
28
+ });
29
+
30
+ if (supportsForceLayout(editor.graph)) {
31
+ const request = layoutRequest(editor, 'force-1');
32
+ const result = await createForceLayout()(request);
33
+ const arranged = applyLayout(editor, request, result);
34
+ }
35
+ ```
36
+
37
+ The eligibility subpath lets a host enable its layout action without loading the
38
+ simulation. In a FoldKit app, run the adapter in a Command, keep the active
39
+ request in the Model, and pass that current request to `applyLayout` on completion.
40
+ That core operation rejects stale results and commits one undo step.
41
+
42
+ ## Behavior and limits
43
+
44
+ The initial adapter supports flat graphs of 1–100 nodes. It rejects invalid
45
+ graphs and graphs containing groups. Use the ELK adapter for compound layouts.
46
+
47
+ The simulation runs a bounded set of ticks. Card spacing uses a circle around
48
+ the largest measured node, and the adapter rejects results with overlapping
49
+ cards. Input graph records are preserved.
50
+
51
+ Automatic edges receive candidate right-angle routes that avoid cards and
52
+ penalize occupied corridors and crossings. Explicit edge path styles remain
53
+ host-authored. A blocked route falls back to the editor's normal automatic
54
+ edge path. Crossings and shared paths can still occur.
55
+
56
+ The adapter returns layout results and leaves application state ownership with
57
+ the caller. The Astro workbench consumes this package as an example.
58
+
59
+ ## Package checks
60
+
61
+ ```sh
62
+ bun run --filter @opsydyn/foldkit-zloz-force typecheck
63
+ bun run --filter @opsydyn/foldkit-zloz-force test
64
+ bun run --filter @opsydyn/foldkit-zloz-force pack:check
65
+ ```
66
+
67
+ The package ships ESM, TypeScript declarations, an MIT licence, and third-party
68
+ notices. Publish compatible Zloz core and Viz versions before releasing this
69
+ adapter through the repository's manual publish workflow.
70
+
71
+ Custom normalized port anchors use the core's canonical points. Routing leads
72
+ extend beyond the declared side of the node box before avoiding other cards;
73
+ returned routes start/end at the authored anchors. This does not infer custom
74
+ shape obstacles or arbitrary outward normals. With the core's built-in curved
75
+ outlines, endpoints stay on the outline and leads still clear the conservative
76
+ rectangular envelope before corridor search.
@@ -0,0 +1,5 @@
1
+ # Third-party notices
2
+
3
+ This package depends on `@opsydyn/foldkit-viz` for its force simulation.
4
+ That package is distributed separately with its own MIT licence and
5
+ third-party notices. This adapter's own source is MIT licensed.
@@ -0,0 +1,8 @@
1
+ import { ZlozGraph } from "@opsydyn/foldkit-zloz";
2
+
3
+ //#region src/eligibility.d.ts
4
+ /** Supports non-empty flat graphs of at most 100 nodes; groups require another layout adapter. */
5
+ declare function supportsForceLayout(graph: ZlozGraph): boolean;
6
+ //#endregion
7
+ export { supportsForceLayout };
8
+ //# sourceMappingURL=eligibility.d.mts.map
@@ -0,0 +1,9 @@
1
+ //#region src/eligibility.ts
2
+ /** Supports non-empty flat graphs of at most 100 nodes; groups require another layout adapter. */
3
+ function supportsForceLayout(graph) {
4
+ return graph.nodes.length > 0 && graph.nodes.length <= 100 && graph.nodes.every((node) => node.kind !== "group");
5
+ }
6
+ //#endregion
7
+ export { supportsForceLayout };
8
+
9
+ //# sourceMappingURL=eligibility.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"eligibility.mjs","names":[],"sources":["../src/eligibility.ts"],"sourcesContent":["/**\n * Lightweight eligibility checks without loading the force engine.\n * @module ForceEligibility\n */\nimport type { ZlozGraph } from '@opsydyn/foldkit-zloz';\n\n/** Supports non-empty flat graphs of at most 100 nodes; groups require another layout adapter. */\nexport function supportsForceLayout(graph: ZlozGraph): boolean {\n return (\n graph.nodes.length > 0 &&\n graph.nodes.length <= 100 &&\n graph.nodes.every((node) => node.kind !== 'group')\n );\n}\n"],"mappings":";;AAOA,SAAgB,oBAAoB,OAA2B;AAC7D,QACE,MAAM,MAAM,SAAS,KACrB,MAAM,MAAM,UAAU,OACtB,MAAM,MAAM,OAAO,SAAS,KAAK,SAAS,QAAQ"}
@@ -0,0 +1,9 @@
1
+ import { supportsForceLayout } from "./eligibility.mjs";
2
+ import { LayoutEngine } from "@opsydyn/foldkit-zloz";
3
+
4
+ //#region src/index.d.ts
5
+ /** Bounded, deterministic force layout using measured card sizes. */
6
+ declare function createForceLayout(): LayoutEngine;
7
+ //#endregion
8
+ export { createForceLayout, supportsForceLayout };
9
+ //# sourceMappingURL=index.d.mts.map
package/dist/index.mjs ADDED
@@ -0,0 +1,224 @@
1
+ import { supportsForceLayout } from "./eligibility.mjs";
2
+ import { runForceLayout } from "@opsydyn/foldkit-viz/simulation";
3
+ import { portBoundaryPosition, portPosition, validateGraph } from "@opsydyn/foldkit-zloz";
4
+ //#region src/routes.ts
5
+ const clearance = 8;
6
+ const lead = 16;
7
+ const directions = {
8
+ left: {
9
+ x: -1,
10
+ y: 0
11
+ },
12
+ right: {
13
+ x: 1,
14
+ y: 0
15
+ },
16
+ top: {
17
+ x: 0,
18
+ y: -1
19
+ },
20
+ bottom: {
21
+ x: 0,
22
+ y: 1
23
+ }
24
+ };
25
+ function obstacle(node) {
26
+ return {
27
+ id: node.id,
28
+ left: node.position.x - clearance,
29
+ right: node.position.x + node.size.width + clearance,
30
+ top: node.position.y - clearance,
31
+ bottom: node.position.y + node.size.height + clearance
32
+ };
33
+ }
34
+ function segmentClear(a, b, obstacles) {
35
+ return obstacles.every(({ left, right, top, bottom }) => {
36
+ if (a.x === b.x) return a.x <= left || a.x >= right || Math.max(a.y, b.y) <= top || Math.min(a.y, b.y) >= bottom;
37
+ return a.y <= top || a.y >= bottom || Math.max(a.x, b.x) <= left || Math.min(a.x, b.x) >= right;
38
+ });
39
+ }
40
+ function portLead(node, id) {
41
+ const port = node.ports.find((item) => item.id === id);
42
+ if (!port) throw new Error(`Missing port ${id} on ${node.id}.`);
43
+ const point = portPosition(node, id);
44
+ const boundary = portBoundaryPosition(node, id);
45
+ const direction = directions[port.side];
46
+ return {
47
+ point,
48
+ outside: {
49
+ x: direction.x < 0 ? node.position.x - lead : direction.x > 0 ? node.position.x + node.size.width + lead : boundary.x,
50
+ y: direction.y < 0 ? node.position.y - lead : direction.y > 0 ? node.position.y + node.size.height + lead : boundary.y
51
+ }
52
+ };
53
+ }
54
+ function corridorPenalty(from, to, occupied) {
55
+ if (from.x === to.x && from.y === to.y) return 0;
56
+ const vertical = from.x === to.x;
57
+ const axis = vertical ? "y" : "x";
58
+ const across = vertical ? "x" : "y";
59
+ const low = Math.min(from[axis], to[axis]);
60
+ const high = Math.max(from[axis], to[axis]);
61
+ let penalty = 0;
62
+ for (const segment of occupied) if (vertical === (segment.from.x === segment.to.x)) {
63
+ if (Math.abs(from[across] - segment.from[across]) >= 12) continue;
64
+ const overlap = Math.min(high, Math.max(segment.from[axis], segment.to[axis])) - Math.max(low, Math.min(segment.from[axis], segment.to[axis]));
65
+ penalty += Math.max(0, overlap) * 4;
66
+ } else if (segment.from[axis] > low && segment.from[axis] < high && from[across] > Math.min(segment.from[across], segment.to[across]) && from[across] < Math.max(segment.from[across], segment.to[across])) penalty += 48;
67
+ return penalty;
68
+ }
69
+ function routeForceEdges(nodes, edges) {
70
+ const byId = new Map(nodes.map((node) => [node.id, node]));
71
+ const obstacles = nodes.map(obstacle);
72
+ const columns = [...new Set(obstacles.flatMap(({ left, right }) => [left, right]))];
73
+ const rows = [...new Set(obstacles.flatMap(({ top, bottom }) => [top, bottom]))];
74
+ const occupied = [];
75
+ return edges.flatMap((edge) => {
76
+ const source = byId.get(edge.source.node);
77
+ const target = byId.get(edge.target.node);
78
+ if (!source || !target || edge.pathStyle !== void 0) return [];
79
+ const start = portLead(source, edge.source.port);
80
+ const end = portLead(target, edge.target.port);
81
+ const a = start.outside;
82
+ const b = end.outside;
83
+ if (!segmentClear(start.point, a, obstacles.filter((item) => item.id !== source.id)) || !segmentClear(b, end.point, obstacles.filter((item) => item.id !== target.id))) return [];
84
+ const laneColumns = occupied.flatMap(({ from }) => [from.x - 12, from.x + 12]);
85
+ const laneRows = occupied.flatMap(({ from }) => [from.y - 12, from.y + 12]);
86
+ const candidates = [...[...new Set([
87
+ a.x,
88
+ b.x,
89
+ (a.x + b.x) / 2,
90
+ ...columns,
91
+ ...laneColumns
92
+ ])].map((x) => [
93
+ a,
94
+ {
95
+ x,
96
+ y: a.y
97
+ },
98
+ {
99
+ x,
100
+ y: b.y
101
+ },
102
+ b
103
+ ]), ...[...new Set([
104
+ a.y,
105
+ b.y,
106
+ (a.y + b.y) / 2,
107
+ ...rows,
108
+ ...laneRows
109
+ ])].map((y) => [
110
+ a,
111
+ {
112
+ x: a.x,
113
+ y
114
+ },
115
+ {
116
+ x: b.x,
117
+ y
118
+ },
119
+ b
120
+ ])];
121
+ const directDistance = Math.abs(a.x - b.x) + Math.abs(a.y - b.y);
122
+ let shortest;
123
+ let distance = Infinity;
124
+ for (const points of candidates) {
125
+ let length = 0;
126
+ let clear = true;
127
+ for (let index = 1; index < points.length; index++) {
128
+ const from = points[index - 1];
129
+ const to = points[index];
130
+ if (!from || !to || !segmentClear(from, to, obstacles)) {
131
+ clear = false;
132
+ break;
133
+ }
134
+ length += Math.abs(to.x - from.x) + Math.abs(to.y - from.y) + corridorPenalty(from, to, occupied);
135
+ }
136
+ if (clear && length < distance) {
137
+ distance = length;
138
+ shortest = points;
139
+ if (length === directDistance) break;
140
+ }
141
+ }
142
+ if (!shortest) return [];
143
+ for (let index = 1; index < shortest.length; index++) {
144
+ const from = shortest[index - 1];
145
+ const to = shortest[index];
146
+ if (from && to && (from.x !== to.x || from.y !== to.y)) occupied.push({
147
+ from,
148
+ to
149
+ });
150
+ }
151
+ const points = [
152
+ start.point,
153
+ ...shortest,
154
+ end.point
155
+ ];
156
+ return [{
157
+ id: edge.id,
158
+ points: points.filter((point, index) => {
159
+ const previous = points[index - 1];
160
+ return previous?.x !== point.x || previous.y !== point.y;
161
+ })
162
+ }];
163
+ });
164
+ }
165
+ //#endregion
166
+ //#region src/layout.ts
167
+ function arrangeWithForce(request) {
168
+ const { graph, requestId, revision } = request;
169
+ validateGraph(graph);
170
+ if (!supportsForceLayout(graph)) throw new Error("Force layout requires a flat graph of 1–100 nodes.");
171
+ const radius = Math.max(...graph.nodes.map(({ size }) => Math.hypot(size.width, size.height) / 2)) + 12;
172
+ const layout = runForceLayout({
173
+ nodes: graph.nodes,
174
+ links: graph.edges.map(({ source, target }) => ({
175
+ source: source.node,
176
+ target: target.node
177
+ })),
178
+ collideRadius: radius,
179
+ linkDistance: 2 * radius + 24
180
+ });
181
+ const centers = new Map(layout.nodes.map((node) => [node.id, node]));
182
+ const cards = graph.nodes.map(({ id, size }) => {
183
+ const center = centers.get(id);
184
+ if (!center || !Number.isFinite(center.x) || !Number.isFinite(center.y)) throw new Error(`Force layout returned an invalid position for ${id}.`);
185
+ return {
186
+ id,
187
+ x: center.x - size.width / 2,
188
+ y: center.y - size.height / 2,
189
+ size
190
+ };
191
+ });
192
+ if (cards.some((card, index) => cards.slice(index + 1).some((other) => card.x < other.x + other.size.width && other.x < card.x + card.size.width && card.y < other.y + other.size.height && other.y < card.y + card.size.height))) throw new Error("Force layout left overlapping cards.");
193
+ const placed = graph.nodes.map((node, index) => {
194
+ const card = cards[index];
195
+ if (!card) throw new Error(`Missing force position for ${node.id}.`);
196
+ return {
197
+ ...node,
198
+ position: {
199
+ x: card.x,
200
+ y: card.y
201
+ }
202
+ };
203
+ });
204
+ return {
205
+ requestId,
206
+ revision,
207
+ positions: cards.map(({ id, x, y }) => ({
208
+ id,
209
+ x,
210
+ y
211
+ })),
212
+ routes: routeForceEdges(placed, graph.edges)
213
+ };
214
+ }
215
+ //#endregion
216
+ //#region src/index.ts
217
+ /** Bounded, deterministic force layout using measured card sizes. */
218
+ function createForceLayout() {
219
+ return async (request) => arrangeWithForce(request);
220
+ }
221
+ //#endregion
222
+ export { createForceLayout, supportsForceLayout };
223
+
224
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +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,KAAK,SAAS;EAC9C;;AAGH,SAAS,aAAa,GAAU,GAAU,WAAmD;AAC3F,QAAO,UAAU,OAAO,EAAE,MAAM,OAAO,KAAK,aAAa;AACvD,MAAI,EAAE,MAAM,EAAE,EACZ,QACE,EAAE,KAAK,QAAQ,EAAE,KAAK,SAAS,KAAK,IAAI,EAAE,GAAG,EAAE,EAAE,IAAI,OAAO,KAAK,IAAI,EAAE,GAAG,EAAE,EAAE,IAAI;AAEtF,SAAO,EAAE,KAAK,OAAO,EAAE,KAAK,UAAU,KAAK,IAAI,EAAE,GAAG,EAAE,EAAE,IAAI,QAAQ,KAAK,IAAI,EAAE,GAAG,EAAE,EAAE,IAAI;GAC1F;;AAGJ,SAA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AAO,EAAE,CACrE,OAAM,IAAI,MAAM,iDAAiD,GAAG,GAAG;AACzE,SAAO;GAAE;GAAI,GAAG,OAAO,IAAI,KAAK,QAAQ;GAAG,GAAG,OAAO,IAAI,KAAK,SAAS;GAAG;GAAM;GAChF;AACF,KACE,MAAM,MAAM,MAAM,UAChB,MACG,MAAM,QAAQ,EAAE,CAChB,MACE,UACC,KAAK,IAAI,MAAM,IAAI,MAAM,KAAK,SAC9B,MAAM,IAAI,KAAK,IAAI,KAAK,KAAK,SAC7B,KAAK,IAAI,MAAM,IAAI,MAAM,KAAK,UAC9B,MAAM,IAAI,KAAK,IAAI,KAAK,KAAK,OAChC,CACJ,CAED,OAAM,IAAI,MAAM,uCAAuC;CACzD,MAAM,SAAS,MAAM,MAAM,KAAK,MAAM,UAAU;EAC9C,MAAM,OAAO,MAAM;AACnB,MAAI,CAAC,KAAM,OAAM,IAAI,MAAM,8BAA8B,KAAK,GAAG,GAAG;AACpE,SAAO;GAAE,GAAG;GAAM,UAAU;IAAE,GAAG,KAAK;IAAG,GAAG,KAAK;IAAG;GAAE;GACtD;AACF,QAAO;EACL;EACA;EACA,WAAW,MAAM,KAAK,EAAE,IAAI,GAAG,SAAS;GAAE;GAAI;GAAG;GAAG,EAAE;EACtD,QAAQ,gBAAgB,QAAQ,MAAM,MAAM;EAC7C;;;;;ACzCH,SAAgB,oBAAkC;AAChD,QAAO,OAAO,YAAY,iBAAiB,QAAQ"}
package/package.json CHANGED
@@ -1,6 +1,73 @@
1
1
  {
2
2
  "name": "@opsydyn/foldkit-zloz-force",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.3.0",
4
+ "description": "Optional force layout and card-avoiding routing for Foldkit Zloz.",
5
+ "keywords": [
6
+ "diagram",
7
+ "flow",
8
+ "foldkit",
9
+ "graph",
10
+ "typescript"
11
+ ],
12
+ "homepage": "https://github.com/opsydyn/fold-kit-experiments/tree/main/packages/foldkit-zloz-force#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/opsydyn/fold-kit-experiments/issues"
15
+ },
16
+ "license": "MIT",
17
+ "author": "Alan P Currie",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/opsydyn/fold-kit-experiments.git",
21
+ "directory": "packages/foldkit-zloz-force"
22
+ },
23
+ "files": [
24
+ "dist",
25
+ "README.md",
26
+ "CHANGELOG.md",
27
+ "LICENSE",
28
+ "THIRD-PARTY-NOTICES.md",
29
+ "!dist/pack"
30
+ ],
31
+ "type": "module",
32
+ "sideEffects": false,
33
+ "main": "./dist/index.mjs",
34
+ "types": "./dist/index.d.mts",
35
+ "exports": {
36
+ ".": {
37
+ "types": "./dist/index.d.mts",
38
+ "import": "./dist/index.mjs"
39
+ },
40
+ "./package.json": "./package.json",
41
+ "./eligibility": {
42
+ "types": "./dist/eligibility.d.mts",
43
+ "import": "./dist/eligibility.mjs"
44
+ }
45
+ },
46
+ "publishConfig": {
47
+ "access": "public"
48
+ },
49
+ "scripts": {
50
+ "build": "bun run --filter @opsydyn/foldkit-zloz build && bun run --filter @opsydyn/foldkit-viz build && tsdown",
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",
53
+ "pack:check": "bun run build && publint && bun ../../tools/pack-zloz.ts foldkit-zloz-force"
54
+ },
55
+ "dependencies": {
56
+ "@opsydyn/foldkit-viz": "0.16.0"
57
+ },
58
+ "devDependencies": {
59
+ "@arethetypeswrong/cli": "0.18.5",
60
+ "@opsydyn/foldkit-zloz": "0.3.0",
61
+ "@types/node": "^22.0.0",
62
+ "bun-types": "^1.3.14",
63
+ "publint": "0.3.25",
64
+ "tsdown": "^0.21.8",
65
+ "typescript": "^6.0.3"
66
+ },
67
+ "peerDependencies": {
68
+ "@opsydyn/foldkit-zloz": "0.3.0"
69
+ },
70
+ "engines": {
71
+ "node": ">=22.0.0"
72
+ }
73
+ }