@statelyai/layout 0.1.3 → 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.
Files changed (85) hide show
  1. package/NOTICE.md +43 -0
  2. package/README.md +153 -6
  3. package/dist/elkjs/index.cjs +9 -0
  4. package/dist/elkjs/index.d.cts +155 -0
  5. package/dist/elkjs/index.d.mts +51 -39
  6. package/dist/elkjs/index.mjs +3 -1484
  7. package/dist/elkjs/main.cjs +17 -0
  8. package/dist/elkjs/main.d.cts +10 -0
  9. package/dist/elkjs/main.d.mts +11 -0
  10. package/dist/elkjs/main.mjs +17 -0
  11. package/dist/elkjs/worker-api.cjs +3 -0
  12. package/dist/elkjs/worker-api.d.cts +3 -0
  13. package/dist/elkjs/worker-api.d.mts +3 -0
  14. package/dist/elkjs/worker-api.mjs +3 -0
  15. package/dist/elkjs/worker.cjs +86 -0
  16. package/dist/elkjs/worker.d.cts +17 -0
  17. package/dist/elkjs/worker.d.mts +17 -0
  18. package/dist/elkjs/worker.mjs +86 -0
  19. package/dist/elkjs-DaHMISnQ.mjs +1677 -0
  20. package/dist/elkjs-dMghiiWW.cjs +1706 -0
  21. package/dist/index-BEA_pzeT.d.cts +72 -0
  22. package/dist/{index-8xYkohbz.d.mts → index-D9dhxrRl.d.cts} +111 -5
  23. package/dist/index-DNTLRsDl.d.mts +72 -0
  24. package/dist/index-DNixtE3h.d.mts +1318 -0
  25. package/dist/index.cjs +215 -0
  26. package/dist/index.d.cts +78 -0
  27. package/dist/index.d.mts +4 -3
  28. package/dist/index.mjs +25 -25
  29. package/dist/layered/index.cjs +38 -0
  30. package/dist/layered/index.d.cts +3 -0
  31. package/dist/layered/index.d.mts +3 -2
  32. package/dist/layered/index.mjs +1 -1
  33. package/dist/layered-B2buJqMf.cjs +12009 -0
  34. package/dist/{layered-CoeH9LRO.mjs → layered-DGFTeQSl.mjs} +2323 -149
  35. package/dist/{spore-BtTSZbb6.mjs → layout-engine-Cs5tatA6.mjs} +159 -5
  36. package/dist/layout-engine-Dn1PiFwP.cjs +712 -0
  37. package/dist/public-types-9NCPoqbp.d.mts +109 -0
  38. package/dist/public-types-BcRZoJE3.d.cts +109 -0
  39. package/dist/routing/index.cjs +28 -0
  40. package/dist/routing/index.d.cts +3 -0
  41. package/dist/routing/index.d.mts +3 -0
  42. package/dist/routing/index.mjs +3 -0
  43. package/dist/routing-A2hrK9MP.mjs +2396 -0
  44. package/dist/routing-ujEP7Qh6.cjs +2606 -0
  45. package/dist/types-CT2akdK0.d.mts +133 -0
  46. package/dist/types-uxPlGRof.d.cts +133 -0
  47. package/dist/worker-client-BNjwT1AJ.mjs +92 -0
  48. package/dist/worker-client-BwGGRD5y.d.mts +19 -0
  49. package/dist/worker-client-D4oEvhAl.cjs +98 -0
  50. package/dist/worker-client-D4zNyPEI.d.cts +19 -0
  51. package/package.json +45 -10
  52. package/src/authoring/coordinates.ts +114 -0
  53. package/src/authoring/partial.ts +584 -0
  54. package/src/authoring/routing.ts +211 -0
  55. package/src/box.ts +22 -0
  56. package/src/constraints.ts +222 -0
  57. package/src/elkjs/index.ts +104 -39
  58. package/src/elkjs/main.ts +19 -0
  59. package/src/elkjs/public-types.ts +125 -0
  60. package/src/elkjs/routes.ts +147 -0
  61. package/src/elkjs/worker-api.ts +29 -0
  62. package/src/elkjs/worker-client.ts +138 -0
  63. package/src/elkjs/worker.ts +117 -0
  64. package/src/index.ts +19 -0
  65. package/src/internal/elkjs-compatibility.ts +17 -0
  66. package/src/internal/layout-engine.ts +126 -0
  67. package/src/layered/compound.ts +417 -0
  68. package/src/layered/index.ts +58 -105
  69. package/src/layered/types.ts +21 -0
  70. package/src/layout.ts +35 -28
  71. package/src/packing.ts +22 -0
  72. package/src/random.ts +75 -3
  73. package/src/routing/adapters.ts +111 -0
  74. package/src/routing/algorithms.ts +676 -0
  75. package/src/routing/coordination.ts +86 -0
  76. package/src/routing/engine.ts +473 -0
  77. package/src/routing/index.ts +68 -0
  78. package/src/routing/layout-cache.ts +65 -0
  79. package/src/routing/model.ts +218 -0
  80. package/src/routing/path.ts +324 -0
  81. package/src/routing/persistent.ts +117 -0
  82. package/src/routing/search.ts +276 -0
  83. package/src/routing/spatial.ts +78 -0
  84. package/src/routing/types.ts +146 -0
  85. package/src/types.ts +17 -2
@@ -0,0 +1,138 @@
1
+ import type {
2
+ ELK as ElkInterface,
3
+ ELKConstructorArguments,
4
+ ElkLayoutAlgorithmDescription,
5
+ ElkLayoutArguments,
6
+ ElkLayoutCategoryDescription,
7
+ ElkLayoutOptionDescription,
8
+ ElkNode,
9
+ LaidOutElkNode,
10
+ LayoutOptions,
11
+ } from "./public-types";
12
+
13
+ interface WorkerRequest {
14
+ id?: number;
15
+ cmd: "register" | "layout" | "algorithms" | "options" | "categories";
16
+ algorithms?: string[];
17
+ graph?: ElkNode;
18
+ layoutOptions?: LayoutOptions;
19
+ options?: Pick<ElkLayoutArguments, "logging" | "measureExecutionTime">;
20
+ }
21
+
22
+ interface WorkerAnswer {
23
+ id: number;
24
+ data?: unknown;
25
+ error?: unknown;
26
+ }
27
+
28
+ interface WorkerLike {
29
+ onmessage: ((event: MessageEvent<WorkerAnswer>) => void) | null;
30
+ onerror: ((event: ErrorEvent) => void) | null;
31
+ onmessageerror: ((event: MessageEvent) => void) | null;
32
+ postMessage(message: WorkerRequest): void;
33
+ terminate(): void;
34
+ }
35
+
36
+ class PromisedWorker {
37
+ readonly #worker: WorkerLike;
38
+ readonly #resolvers = new Map<
39
+ number,
40
+ { resolve(value: unknown): void; reject(reason?: unknown): void }
41
+ >();
42
+ #id = 0;
43
+ #failure: Error | undefined;
44
+
45
+ constructor(worker: WorkerLike) {
46
+ this.#worker = worker;
47
+ worker.onmessage = (event) => {
48
+ const resolver = this.#resolvers.get(event.data.id);
49
+ if (!resolver) return;
50
+ this.#resolvers.delete(event.data.id);
51
+ if (event.data.error !== undefined) resolver.reject(event.data.error);
52
+ else resolver.resolve(event.data.data);
53
+ };
54
+ worker.onerror = (event) => this.#rejectAll(event.error ?? event.message);
55
+ worker.onmessageerror = (event) => this.#rejectAll(event.data);
56
+ }
57
+
58
+ postMessage<T>(message: WorkerRequest): Promise<T> {
59
+ if (this.#failure) return Promise.reject(this.#failure);
60
+ const id = this.#id++;
61
+ return new Promise<T>((resolve, reject) => {
62
+ this.#resolvers.set(id, { resolve: resolve as (value: unknown) => void, reject });
63
+ this.#worker.postMessage({ ...message, id });
64
+ });
65
+ }
66
+
67
+ terminate(): void {
68
+ this.#worker.terminate();
69
+ }
70
+
71
+ #rejectAll(reason: unknown): void {
72
+ const error = reason instanceof Error ? reason : new Error(String(reason ?? "Worker failed"));
73
+ this.#failure = error;
74
+ for (const resolver of this.#resolvers.values()) resolver.reject(error);
75
+ this.#resolvers.clear();
76
+ }
77
+ }
78
+
79
+ export class WorkerELK implements ElkInterface {
80
+ readonly #defaultLayoutOptions: LayoutOptions;
81
+ readonly #worker: PromisedWorker;
82
+ readonly #ready: Promise<unknown>;
83
+
84
+ constructor({
85
+ defaultLayoutOptions = {},
86
+ algorithms,
87
+ workerFactory,
88
+ workerUrl,
89
+ }: ELKConstructorArguments = {}) {
90
+ if (workerUrl === undefined && workerFactory === undefined) {
91
+ throw new Error("Cannot construct an ELK without both 'workerUrl' and 'workerFactory'.");
92
+ }
93
+ const factory = workerFactory ?? ((url?: string) => new globalThis.Worker(url!));
94
+ const worker = factory(workerUrl) as WorkerLike;
95
+ if (!worker || typeof worker.postMessage !== "function") {
96
+ throw new TypeError("Created worker does not provide the required 'postMessage' function.");
97
+ }
98
+ this.#defaultLayoutOptions = defaultLayoutOptions;
99
+ this.#worker = new PromisedWorker(worker);
100
+ this.#ready = this.#worker.postMessage({ cmd: "register", algorithms });
101
+ }
102
+
103
+ async layout<T extends ElkNode>(
104
+ graph: T,
105
+ args: ElkLayoutArguments = {},
106
+ ): Promise<LaidOutElkNode<T>> {
107
+ if (!graph) throw new Error("Missing mandatory parameter 'graph'.");
108
+ await this.#ready;
109
+ return this.#worker.postMessage({
110
+ cmd: "layout",
111
+ graph,
112
+ layoutOptions: { ...this.#defaultLayoutOptions, ...args.layoutOptions },
113
+ options: {
114
+ logging: args.logging ?? false,
115
+ measureExecutionTime: args.measureExecutionTime ?? false,
116
+ },
117
+ });
118
+ }
119
+
120
+ async knownLayoutAlgorithms(): Promise<ElkLayoutAlgorithmDescription[]> {
121
+ await this.#ready;
122
+ return this.#worker.postMessage({ cmd: "algorithms" });
123
+ }
124
+
125
+ async knownLayoutOptions(): Promise<ElkLayoutOptionDescription[]> {
126
+ await this.#ready;
127
+ return this.#worker.postMessage({ cmd: "options" });
128
+ }
129
+
130
+ async knownLayoutCategories(): Promise<ElkLayoutCategoryDescription[]> {
131
+ await this.#ready;
132
+ return this.#worker.postMessage({ cmd: "categories" });
133
+ }
134
+
135
+ terminateWorker(): void {
136
+ this.#worker.terminate();
137
+ }
138
+ }
@@ -0,0 +1,117 @@
1
+ /*******************************************************************************
2
+ * Copyright (c) 2019 Kiel University and others.
3
+ *
4
+ * Adapted from elkjs 0.11.1 lib/elk-worker.js.
5
+ * SPDX-License-Identifier: EPL-2.0
6
+ *******************************************************************************/
7
+ import ELK from "./index";
8
+ import type { ElkLayoutArguments, ElkNode } from "./types";
9
+
10
+ type WorkerCommand =
11
+ | { id: number; cmd: "register"; algorithms?: string[] }
12
+ | {
13
+ id: number;
14
+ cmd: "layout";
15
+ graph: ElkNode;
16
+ layoutOptions?: Record<string, unknown>;
17
+ options?: ElkLayoutArguments;
18
+ }
19
+ | { id: number; cmd: "algorithms" | "options" | "categories" };
20
+
21
+ type WorkerAnswer = { id: number; data?: unknown; error?: unknown };
22
+
23
+ /** In-process implementation of the message interface shipped by elkjs workers. */
24
+ export class Worker implements globalThis.Worker {
25
+ onerror: ((this: AbstractWorker, event: ErrorEvent) => unknown) | null = null;
26
+ onmessage: ((this: globalThis.Worker, event: MessageEvent) => unknown) | null = null;
27
+ onmessageerror: ((this: globalThis.Worker, event: MessageEvent) => unknown) | null = null;
28
+
29
+ readonly #listeners = new Map<string, Set<EventListenerOrEventListenerObject>>();
30
+ #elk = new ELK();
31
+ #terminated = false;
32
+
33
+ constructor(_url?: string) {}
34
+
35
+ postMessage(message: unknown, _transfer: Transferable[]): void;
36
+ postMessage(message: unknown, _options?: StructuredSerializeOptions): void;
37
+ postMessage(message: unknown): void {
38
+ if (this.#terminated) return;
39
+ queueMicrotask(() => void this.#handle(message as WorkerCommand));
40
+ }
41
+
42
+ terminate(): void {
43
+ this.#terminated = true;
44
+ }
45
+
46
+ addEventListener<K extends keyof WorkerEventMap>(
47
+ type: K,
48
+ listener: (this: globalThis.Worker, event: WorkerEventMap[K]) => unknown,
49
+ options?: boolean | AddEventListenerOptions,
50
+ ): void;
51
+ addEventListener(
52
+ type: string,
53
+ listener: EventListenerOrEventListenerObject,
54
+ _options?: boolean | AddEventListenerOptions,
55
+ ): void {
56
+ const listeners = this.#listeners.get(type) ?? new Set();
57
+ listeners.add(listener);
58
+ this.#listeners.set(type, listeners);
59
+ }
60
+
61
+ removeEventListener<K extends keyof WorkerEventMap>(
62
+ type: K,
63
+ listener: (this: globalThis.Worker, event: WorkerEventMap[K]) => unknown,
64
+ options?: boolean | EventListenerOptions,
65
+ ): void;
66
+ removeEventListener(
67
+ type: string,
68
+ listener: EventListenerOrEventListenerObject,
69
+ _options?: boolean | EventListenerOptions,
70
+ ): void {
71
+ this.#listeners.get(type)?.delete(listener);
72
+ }
73
+
74
+ dispatchEvent(event: Event): boolean {
75
+ for (const listener of this.#listeners.get(event.type) ?? []) {
76
+ if (typeof listener === "function") listener.call(this, event);
77
+ else listener.handleEvent(event);
78
+ }
79
+ return !event.defaultPrevented;
80
+ }
81
+
82
+ async #handle(message: WorkerCommand): Promise<void> {
83
+ try {
84
+ let data: unknown;
85
+ switch (message.cmd) {
86
+ case "register":
87
+ this.#elk = new ELK({ algorithms: message.algorithms });
88
+ break;
89
+ case "layout":
90
+ data = await this.#elk.layout(message.graph, {
91
+ ...message.options,
92
+ layoutOptions: message.layoutOptions,
93
+ });
94
+ break;
95
+ case "algorithms":
96
+ data = await this.#elk.knownLayoutAlgorithms();
97
+ break;
98
+ case "options":
99
+ data = await this.#elk.knownLayoutOptions();
100
+ break;
101
+ case "categories":
102
+ data = await this.#elk.knownLayoutCategories();
103
+ break;
104
+ }
105
+ this.#answer({ id: message.id, data });
106
+ } catch (error) {
107
+ this.#answer({ id: message.id, error });
108
+ }
109
+ }
110
+
111
+ #answer(answer: WorkerAnswer): void {
112
+ if (this.#terminated) return;
113
+ const event = { data: answer, type: "message" } as MessageEvent<WorkerAnswer>;
114
+ this.onmessage?.call(this, event);
115
+ this.dispatchEvent(event);
116
+ }
117
+ }
package/src/index.ts CHANGED
@@ -71,6 +71,10 @@ export type {
71
71
  EdgeRoutes,
72
72
  LayerAssigner,
73
73
  LayerAssignment,
74
+ CompoundVisualGraph,
75
+ CompoundLayoutOptions,
76
+ CompoundLayoutGeometry,
77
+ CompoundEdgeAttachment,
74
78
  LayeredLayoutOptions,
75
79
  LayeredAdvancedOptions,
76
80
  ElkLayeredOptionId,
@@ -90,3 +94,18 @@ export type {
90
94
  NodePlacer,
91
95
  NodeSize,
92
96
  } from "./layered";
97
+
98
+ export { c } from "./constraints";
99
+ export type {
100
+ LayoutConstraint,
101
+ GeometryReference,
102
+ GeometryAttribute,
103
+ ConstraintStrength,
104
+ AlignConstraint,
105
+ DistributeConstraint,
106
+ PinConstraint,
107
+ LinearConstraint,
108
+ WaypointConstraint,
109
+ } from "./constraints";
110
+
111
+ export * from "./routing";
@@ -0,0 +1,17 @@
1
+ import type { VisualGraph } from "@statelyai/graph";
2
+
3
+ export interface Elkjs0111ResultPolicy {
4
+ providerBounds?: { width: number; height: number };
5
+ preserveEdgeSections?: boolean;
6
+ skipBoundsNormalization?: boolean;
7
+ }
8
+
9
+ export const elkjs0111ResultPolicy = Symbol("elkjs0111ResultPolicy");
10
+
11
+ export function setElkjs0111ResultPolicy<N, E, G, P>(
12
+ graph: VisualGraph<N, E, G, P>,
13
+ policy: Elkjs0111ResultPolicy,
14
+ ): VisualGraph<N, E, G, P> {
15
+ Object.defineProperty(graph, elkjs0111ResultPolicy, { value: policy });
16
+ return graph;
17
+ }
@@ -0,0 +1,126 @@
1
+ import type { Graph, VisualGraph } from "@statelyai/graph";
2
+ import { boxAlgorithm, elkjs0111BoxAlgorithm, type BoxLayoutOptions } from "../box";
3
+ import { fixedAlgorithm, type FixedLayoutOptions } from "../fixed";
4
+ import { layeredAlgorithm, type LayeredLayoutOptions } from "../layered";
5
+ import {
6
+ elkjs0111RectanglePackingAlgorithm,
7
+ rectanglePackingAlgorithm,
8
+ type RectanglePackingLayoutOptions,
9
+ } from "../packing";
10
+ import { elkjs0111RandomAlgorithm, randomAlgorithm, type RandomLayoutOptions } from "../random";
11
+ import {
12
+ sporeCompactionAlgorithm,
13
+ sporeOverlapRemovalAlgorithm,
14
+ type SporeLayoutOptions,
15
+ } from "../spore";
16
+ import type { LayoutAlgorithm, LayoutExecutionContext } from "../types";
17
+
18
+ export interface BuiltInLayoutOptionsById {
19
+ box: BoxLayoutOptions;
20
+ fixed: FixedLayoutOptions;
21
+ layered: LayeredLayoutOptions;
22
+ random: RandomLayoutOptions;
23
+ rectpacking: RectanglePackingLayoutOptions;
24
+ sporeCompaction: SporeLayoutOptions;
25
+ sporeOverlap: SporeLayoutOptions;
26
+ }
27
+
28
+ export type BuiltInLayoutAlgorithmId = keyof BuiltInLayoutOptionsById;
29
+
30
+ export const builtInLayoutAlgorithms = {
31
+ box: boxAlgorithm,
32
+ fixed: fixedAlgorithm,
33
+ layered: layeredAlgorithm,
34
+ random: randomAlgorithm,
35
+ rectpacking: rectanglePackingAlgorithm,
36
+ sporeCompaction: sporeCompactionAlgorithm,
37
+ sporeOverlap: sporeOverlapRemovalAlgorithm,
38
+ } satisfies {
39
+ [Id in BuiltInLayoutAlgorithmId]: LayoutAlgorithm<BuiltInLayoutOptionsById[Id]>;
40
+ };
41
+
42
+ const elkjs0111LayoutAlgorithms = {
43
+ ...builtInLayoutAlgorithms,
44
+ box: elkjs0111BoxAlgorithm,
45
+ random: elkjs0111RandomAlgorithm,
46
+ rectpacking: elkjs0111RectanglePackingAlgorithm,
47
+ } satisfies {
48
+ [Id in BuiltInLayoutAlgorithmId]: LayoutAlgorithm<BuiltInLayoutOptionsById[Id]>;
49
+ };
50
+
51
+ export function getBuiltInLayoutAlgorithm(id: string): LayoutAlgorithm<unknown> | undefined {
52
+ if (!Object.hasOwn(builtInLayoutAlgorithms, id)) return undefined;
53
+ return builtInLayoutAlgorithms[id as BuiltInLayoutAlgorithmId] as
54
+ | LayoutAlgorithm<unknown>
55
+ | undefined;
56
+ }
57
+
58
+ interface ExecuteLayoutAlgorithmRequest<N, E, G, P, O> {
59
+ algorithm: LayoutAlgorithm<O>;
60
+ graph: Graph<N, E, G, P> | VisualGraph<N, E, G, P>;
61
+ options: O;
62
+ context: LayoutExecutionContext;
63
+ }
64
+
65
+ /** Execute one algorithm through the shared engine seam. */
66
+ export function executeLayoutAlgorithm<N, E, G, P, O>({
67
+ algorithm,
68
+ graph,
69
+ options,
70
+ context,
71
+ }: ExecuteLayoutAlgorithmRequest<N, E, G, P, O>):
72
+ | VisualGraph<N, E, G, P>
73
+ | Promise<VisualGraph<N, E, G, P>> {
74
+ return algorithm.layout(graph, options, context);
75
+ }
76
+
77
+ function createDirectExecutionContext(): LayoutExecutionContext {
78
+ return {
79
+ scope: { mode: "full" },
80
+ diagnostics: [],
81
+ measurePhase(_id, run) {
82
+ return run();
83
+ },
84
+ throwIfAborted() {},
85
+ };
86
+ }
87
+
88
+ interface ExecuteBuiltInLayoutRequest<N, E, G, P, Id extends BuiltInLayoutAlgorithmId> {
89
+ algorithm: Id;
90
+ graph: Graph<N, E, G, P> | VisualGraph<N, E, G, P>;
91
+ options: BuiltInLayoutOptionsById[Id];
92
+ }
93
+
94
+ /** Execute a built-in algorithm through the shared engine seam. */
95
+ export function executeBuiltInLayout<N, E, G, P, Id extends BuiltInLayoutAlgorithmId>({
96
+ algorithm,
97
+ graph,
98
+ options,
99
+ }: ExecuteBuiltInLayoutRequest<N, E, G, P, Id>):
100
+ | VisualGraph<N, E, G, P>
101
+ | Promise<VisualGraph<N, E, G, P>> {
102
+ return executeLayoutAlgorithm({
103
+ algorithm: builtInLayoutAlgorithms[algorithm] as LayoutAlgorithm<BuiltInLayoutOptionsById[Id]>,
104
+ graph,
105
+ options,
106
+ context: createDirectExecutionContext(),
107
+ });
108
+ }
109
+
110
+ /** Execute through the pinned elkjs 0.11.1 compatibility policy. */
111
+ export function executeElkjs0111Layout<N, E, G, P, Id extends BuiltInLayoutAlgorithmId>({
112
+ algorithm,
113
+ graph,
114
+ options,
115
+ }: ExecuteBuiltInLayoutRequest<N, E, G, P, Id>):
116
+ | VisualGraph<N, E, G, P>
117
+ | Promise<VisualGraph<N, E, G, P>> {
118
+ return executeLayoutAlgorithm({
119
+ algorithm: elkjs0111LayoutAlgorithms[algorithm] as LayoutAlgorithm<
120
+ BuiltInLayoutOptionsById[Id]
121
+ >,
122
+ graph,
123
+ options,
124
+ context: createDirectExecutionContext(),
125
+ });
126
+ }