@tscomfy/layout 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +22 -2
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/layered.d.ts +32 -0
- package/dist/layered.d.ts.map +1 -0
- package/dist/layered.js +97 -0
- package/dist/layered.js.map +1 -0
- package/dist/layout.d.ts +15 -0
- package/dist/layout.d.ts.map +1 -0
- package/dist/layout.js +36 -0
- package/dist/layout.js.map +1 -0
- package/package.json +46 -4
- package/src/index.ts +9 -0
- package/src/layered.ts +125 -0
- package/src/layout.ts +40 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 the tscomfy authors
|
|
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,23 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @tscomfy/layout
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Automatic layout for tscomfy workflows written in the ComfyUI frontend's UI format: positions
|
|
4
|
+
for `toUi`, in columns by dependency depth, left to right. Pure, with no runtime dependencies.
|
|
5
|
+
|
|
6
|
+
```sh
|
|
7
|
+
npm install @tscomfy/layout @tscomfy/core
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
import { stringify, toUi } from "@tscomfy/core";
|
|
12
|
+
import { layout } from "@tscomfy/layout";
|
|
13
|
+
|
|
14
|
+
const { workflow: ui } = toUi(txt2img, { positions: layout(txt2img) });
|
|
15
|
+
const file = stringify(ui); // opens in the ComfyUI frontend
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Needs ES2024 (Node 24 or later, Bun 1.4 or later). See the [guides](https://github.com/TheeKia/tscomfy/tree/main/docs/guides), in particular
|
|
19
|
+
[the UI format](https://github.com/TheeKia/tscomfy/tree/main/docs/guides/ui-format.md).
|
|
20
|
+
|
|
21
|
+
## License
|
|
22
|
+
|
|
23
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @tscomfy/layout
|
|
3
|
+
*
|
|
4
|
+
* Automatic layout for workflows exported to the ComfyUI frontend's UI format:
|
|
5
|
+
* positions for `toUi`. Pure and runtime-agnostic, like core.
|
|
6
|
+
*/
|
|
7
|
+
export { layered, type LayoutBox, type LayoutSpacing, type Position } from "./layered.ts";
|
|
8
|
+
export { layout } from "./layout.ts";
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,OAAO,EAAE,KAAK,SAAS,EAAE,KAAK,aAAa,EAAE,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAC;AAC1F,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @tscomfy/layout
|
|
3
|
+
*
|
|
4
|
+
* Automatic layout for workflows exported to the ComfyUI frontend's UI format:
|
|
5
|
+
* positions for `toUi`. Pure and runtime-agnostic, like core.
|
|
6
|
+
*/
|
|
7
|
+
export { layered } from "./layered.js";
|
|
8
|
+
export { layout } from "./layout.js";
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,OAAO,EAAqD,MAAM,cAAc,CAAC;AAC1F,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A box to place: its size, and the boxes its inputs come from. Sizes and positions are in the
|
|
3
|
+
* same units, canvas units for `toUi` (the frontend's pixels at zoom 1).
|
|
4
|
+
*/
|
|
5
|
+
export interface LayoutBox {
|
|
6
|
+
/** Unique among the boxes laid out together. */
|
|
7
|
+
readonly id: string;
|
|
8
|
+
readonly width: number;
|
|
9
|
+
readonly height: number;
|
|
10
|
+
/** Ids of the boxes linked into this one; unknown ids and cycles are ignored. */
|
|
11
|
+
readonly sources: readonly string[];
|
|
12
|
+
}
|
|
13
|
+
/** Gaps between boxes, in the units of their sizes. */
|
|
14
|
+
export interface LayoutSpacing {
|
|
15
|
+
/** Between a column's widest box and the next column. */
|
|
16
|
+
readonly column: number;
|
|
17
|
+
/** Between one box and the next in a column. */
|
|
18
|
+
readonly row: number;
|
|
19
|
+
}
|
|
20
|
+
/** Where a box goes: its top-left corner. */
|
|
21
|
+
export type Position = readonly [x: number, y: number];
|
|
22
|
+
/**
|
|
23
|
+
* Positions for boxes that flow left to right: a box sits one column right of its rightmost
|
|
24
|
+
* source, boxes without sources in the first column; in each column boxes are ordered by the
|
|
25
|
+
* average row of their sources (ties by input order), and each column is as wide as its widest
|
|
26
|
+
* box. Boxes on a cycle go where the cycle is broken, in input order. The first column starts at
|
|
27
|
+
* x 0 and every column at y 0; `spacing` defaults to 80 between columns and 40 between rows.
|
|
28
|
+
*
|
|
29
|
+
* Throws a `TypeError` when two boxes share an id: their positions couldn't be told apart.
|
|
30
|
+
*/
|
|
31
|
+
export declare function layered(boxes: readonly LayoutBox[], spacing?: LayoutSpacing): ReadonlyMap<string, Position>;
|
|
32
|
+
//# sourceMappingURL=layered.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"layered.d.ts","sourceRoot":"","sources":["../src/layered.ts"],"names":[],"mappings":"AAIA;;;GAGG;AACH,MAAM,WAAW,SAAS;IACxB,gDAAgD;IAChD,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,iFAAiF;IACjF,QAAQ,CAAC,OAAO,EAAE,SAAS,MAAM,EAAE,CAAC;CACrC;AAED,uDAAuD;AACvD,MAAM,WAAW,aAAa;IAC5B,yDAAyD;IACzD,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,gDAAgD;IAChD,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;CACtB;AAKD,6CAA6C;AAC7C,MAAM,MAAM,QAAQ,GAAG,SAAS,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,CAAC,CAAC;AAEvD;;;;;;;;GAQG;AACH,wBAAgB,OAAO,CACrB,KAAK,EAAE,SAAS,SAAS,EAAE,EAC3B,OAAO,GAAE,aAA+B,GACvC,WAAW,CAAC,MAAM,EAAE,QAAQ,CAAC,CA4C/B"}
|
package/dist/layered.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// A layered layout (after Sugiyama et al., much simplified): columns by dependency depth, rows
|
|
2
|
+
// in each column ordered by where their sources sit, then packed from the top. Deterministic,
|
|
3
|
+
// and linear apart from one sort per column.
|
|
4
|
+
/** The spacing unless one is given. */
|
|
5
|
+
const DEFAULT_SPACING = { column: 80, row: 40 };
|
|
6
|
+
/**
|
|
7
|
+
* Positions for boxes that flow left to right: a box sits one column right of its rightmost
|
|
8
|
+
* source, boxes without sources in the first column; in each column boxes are ordered by the
|
|
9
|
+
* average row of their sources (ties by input order), and each column is as wide as its widest
|
|
10
|
+
* box. Boxes on a cycle go where the cycle is broken, in input order. The first column starts at
|
|
11
|
+
* x 0 and every column at y 0; `spacing` defaults to 80 between columns and 40 between rows.
|
|
12
|
+
*
|
|
13
|
+
* Throws a `TypeError` when two boxes share an id: their positions couldn't be told apart.
|
|
14
|
+
*/
|
|
15
|
+
export function layered(boxes, spacing = DEFAULT_SPACING) {
|
|
16
|
+
const index = new Map();
|
|
17
|
+
for (const [position, box] of boxes.entries()) {
|
|
18
|
+
if (index.has(box.id)) {
|
|
19
|
+
throw new TypeError(`layered: two boxes have the id ${JSON.stringify(box.id)}`);
|
|
20
|
+
}
|
|
21
|
+
index.set(box.id, position);
|
|
22
|
+
}
|
|
23
|
+
const columns = depths(boxes, index);
|
|
24
|
+
const byColumn = new Map();
|
|
25
|
+
for (const box of boxes) {
|
|
26
|
+
const column = columns.get(box.id) ?? 0;
|
|
27
|
+
const list = byColumn.get(column);
|
|
28
|
+
if (list === undefined)
|
|
29
|
+
byColumn.set(column, [box]);
|
|
30
|
+
else
|
|
31
|
+
list.push(box);
|
|
32
|
+
}
|
|
33
|
+
const rows = new Map();
|
|
34
|
+
const positions = new Map();
|
|
35
|
+
let x = 0;
|
|
36
|
+
for (const column of [...byColumn.keys()].toSorted((a, b) => a - b)) {
|
|
37
|
+
const members = byColumn.get(column) ?? [];
|
|
38
|
+
const order = (box) => {
|
|
39
|
+
const placed = box.sources.flatMap((source) => {
|
|
40
|
+
const row = rows.get(source);
|
|
41
|
+
return row === undefined ? [] : [row];
|
|
42
|
+
});
|
|
43
|
+
return placed.length === 0
|
|
44
|
+
? Number.POSITIVE_INFINITY
|
|
45
|
+
: placed.reduce((sum, row) => sum + row, 0) / placed.length;
|
|
46
|
+
};
|
|
47
|
+
const sorted = members
|
|
48
|
+
.map((box) => ({ box, key: order(box), at: index.get(box.id) ?? 0 }))
|
|
49
|
+
.toSorted((a, b) => (a.key === b.key ? a.at - b.at : a.key < b.key ? -1 : 1));
|
|
50
|
+
let y = 0;
|
|
51
|
+
let width = 0;
|
|
52
|
+
for (const [row, { box }] of sorted.entries()) {
|
|
53
|
+
rows.set(box.id, row);
|
|
54
|
+
positions.set(box.id, [x, y]);
|
|
55
|
+
y += box.height + spacing.row;
|
|
56
|
+
width = Math.max(width, box.width);
|
|
57
|
+
}
|
|
58
|
+
x += width + spacing.column;
|
|
59
|
+
}
|
|
60
|
+
return positions;
|
|
61
|
+
}
|
|
62
|
+
/** Each box's column: the longest path from a box without sources (iterative, cycle-safe). */
|
|
63
|
+
function depths(boxes, index) {
|
|
64
|
+
const depth = new Map();
|
|
65
|
+
const state = new Map();
|
|
66
|
+
for (const start of boxes) {
|
|
67
|
+
if (state.has(start.id))
|
|
68
|
+
continue;
|
|
69
|
+
const stack = [{ box: start, next: 0 }];
|
|
70
|
+
state.set(start.id, "open");
|
|
71
|
+
while (stack.length > 0) {
|
|
72
|
+
const frame = stack.at(-1);
|
|
73
|
+
if (frame === undefined)
|
|
74
|
+
break;
|
|
75
|
+
const source = frame.box.sources[frame.next++];
|
|
76
|
+
if (source !== undefined) {
|
|
77
|
+
const at = index.get(source);
|
|
78
|
+
const box = at === undefined ? undefined : boxes[at];
|
|
79
|
+
// Unknown sources, and back edges of a cycle, don't count.
|
|
80
|
+
if (box !== undefined && !state.has(box.id)) {
|
|
81
|
+
state.set(box.id, "open");
|
|
82
|
+
stack.push({ box, next: 0 });
|
|
83
|
+
}
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
stack.pop();
|
|
87
|
+
state.set(frame.box.id, "done");
|
|
88
|
+
const sources = frame.box.sources.flatMap((id) => {
|
|
89
|
+
const value = state.get(id) === "done" ? depth.get(id) : undefined;
|
|
90
|
+
return value === undefined ? [] : [value];
|
|
91
|
+
});
|
|
92
|
+
depth.set(frame.box.id, sources.length === 0 ? 0 : Math.max(...sources) + 1);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return depth;
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=layered.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"layered.js","sourceRoot":"","sources":["../src/layered.ts"],"names":[],"mappings":"AAAA,+FAA+F;AAC/F,8FAA8F;AAC9F,6CAA6C;AAuB7C,uCAAuC;AACvC,MAAM,eAAe,GAAkB,EAAE,MAAM,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,CAAC;AAK/D;;;;;;;;GAQG;AACH,MAAM,UAAU,OAAO,CACrB,KAA2B,EAC3B,OAAO,GAAkB,eAAe;IAExC,MAAM,KAAK,GAAG,IAAI,GAAG,EAAkB,CAAC;IACxC,KAAK,MAAM,CAAC,QAAQ,EAAE,GAAG,CAAC,IAAI,KAAK,CAAC,OAAO,EAAE,EAAE,CAAC;QAC9C,IAAI,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC;YACtB,MAAM,IAAI,SAAS,CAAC,kCAAkC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;QAClF,CA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,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC;oBAC5C,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;oBAC1B,KAAK,CAAC,IAAI,CAAC,EAAE,GAAG,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC,CAAC;gBAC/B,CAAC;gBACD,SAAS;YACX,CAAC;YACD,KAAK,CAAC,GAAG,EAAE,CAAC;YACZ,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;YAChC,MAAM,OAAO,GAAG,KAAK,CAAC,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,EAAE,EAAE,EAAE;gBAC/C,MAAM,KAAK,GAAG,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC,KAAK,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;gBACnE,OAAO,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;YAC5C,CAAC,CAAC,CAAC;YACH,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,EAAE,OAAO,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC;QAC/E,CAAC;IACH,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC"}
|
package/dist/layout.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type NodeId, type Workflow } from "@tscomfy/core";
|
|
2
|
+
import { type LayoutSpacing, type Position } from "./layered.ts";
|
|
3
|
+
/**
|
|
4
|
+
* Positions for `toUi`: the workflow laid out left to right in columns by
|
|
5
|
+
* dependency depth (see {@link layered}), each node the size `toUi` writes for it. A node's
|
|
6
|
+
* sources are the nodes its inputs link to, inside lists, Autogrow slots and dynamic-combo
|
|
7
|
+
* options too. `spacing` is in canvas units and defaults to 80 between columns and 40 between
|
|
8
|
+
* rows.
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* const { workflow: ui } = toUi(built, { positions: layout(built) });
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export declare function layout(workflow: Workflow, spacing?: LayoutSpacing): ReadonlyMap<NodeId, Position>;
|
|
15
|
+
//# sourceMappingURL=layout.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"layout.d.ts","sourceRoot":"","sources":["../src/layout.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,MAAM,EAEX,KAAK,QAAQ,EACd,MAAM,eAAe,CAAC;AACvB,OAAO,EAAW,KAAK,aAAa,EAAE,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAC;AAE1E;;;;;;;;;;GAUG;AACH,wBAAgB,MAAM,CAAC,QAAQ,EAAE,QAAQ,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,WAAW,CAAC,MAAM,EAAE,QAAQ,CAAC,CAOjG"}
|
package/dist/layout.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { isOutputRef, uiNodeSizes, } from "@tscomfy/core";
|
|
2
|
+
import { layered } from "./layered.js";
|
|
3
|
+
/**
|
|
4
|
+
* Positions for `toUi`: the workflow laid out left to right in columns by
|
|
5
|
+
* dependency depth (see {@link layered}), each node the size `toUi` writes for it. A node's
|
|
6
|
+
* sources are the nodes its inputs link to, inside lists, Autogrow slots and dynamic-combo
|
|
7
|
+
* options too. `spacing` is in canvas units and defaults to 80 between columns and 40 between
|
|
8
|
+
* rows.
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* const { workflow: ui } = toUi(built, { positions: layout(built) });
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export function layout(workflow, spacing) {
|
|
15
|
+
const sizes = uiNodeSizes(workflow);
|
|
16
|
+
const boxes = [...workflow.graph.nodes.values()].map((node) => {
|
|
17
|
+
const [width, height] = sizes.get(node.id) ?? [0, 0];
|
|
18
|
+
return { id: node.id, width, height, sources: sources(node.inputs) };
|
|
19
|
+
});
|
|
20
|
+
return layered(boxes, spacing);
|
|
21
|
+
}
|
|
22
|
+
/** The nodes a node's inputs link to. */
|
|
23
|
+
function sources(inputs) {
|
|
24
|
+
const found = new Set();
|
|
25
|
+
const pending = Object.values(inputs);
|
|
26
|
+
for (let value = pending.pop(); value !== undefined; value = pending.pop()) {
|
|
27
|
+
if (isOutputRef(value))
|
|
28
|
+
found.add(value.node);
|
|
29
|
+
else if (Array.isArray(value))
|
|
30
|
+
pending.push(...value);
|
|
31
|
+
else if (typeof value === "object" && value !== null)
|
|
32
|
+
pending.push(...Object.values(value));
|
|
33
|
+
}
|
|
34
|
+
return [...found];
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=layout.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"layout.js","sourceRoot":"","sources":["../src/layout.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EAGX,WAAW,GAEZ,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,OAAO,EAAqC,MAAM,cAAc,CAAC;AAE1E;;;;;;;;;;GAUG;AACH,MAAM,UAAU,MAAM,CAAC,QAAkB,EAAE,OAAuB;IAChE,MAAM,KAAK,GAAG,WAAW,CAAC,QAAQ,CAAC,CAAC;IACpC,MAAM,KAAK,GAAG,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE;QAC5D,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,GAAG,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;QACrD,OAAO,EAAE,EAAE,EAAE,IAAI,CAAC,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC;IACvE,CAAC,CAAC,CAAC;IACH,OAAO,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;AACjC,CAAC;AAED,yCAAyC;AACzC,SAAS,OAAO,CAAC,MAA+C;IAC9D,MAAM,KAAK,GAAG,IAAI,GAAG,EAAU,CAAC;IAChC,MAAM,OAAO,GAAiB,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IACpD,KAAK,IAAI,KAAK,GAAG,OAAO,CAAC,GAAG,EAAE,EAAE,KAAK,KAAK,SAAS,EAAE,KAAK,GAAG,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC;QAC3E,IAAI,WAAW,CAAC,KAAK,CAAC;YAAE,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;aACzC,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;YAAE,OAAO,CAAC,IAAI,CAAC,GAAI,KAA+B,CAAC,CAAC;aAC5E,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI;YAAE,OAAO,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;IAC9F,CAAC;IACD,OAAO,CAAC,GAAG,KAAK,CAAC,CAAC;AACpB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,48 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tscomfy/layout",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Automatic layout for tscomfy workflows written in the ComfyUI frontend's UI format.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"comfyui",
|
|
7
|
+
"graph",
|
|
8
|
+
"layout",
|
|
9
|
+
"typescript",
|
|
10
|
+
"workflow"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://github.com/TheeKia/tscomfy/tree/main/packages/layout#readme",
|
|
13
|
+
"bugs": "https://github.com/TheeKia/tscomfy/issues",
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/TheeKia/tscomfy.git",
|
|
18
|
+
"directory": "packages/layout"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist",
|
|
22
|
+
"!dist/**/*.tsbuildinfo",
|
|
23
|
+
"src"
|
|
24
|
+
],
|
|
25
|
+
"type": "module",
|
|
26
|
+
"sideEffects": false,
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"@tscomfy/source": "./src/index.ts",
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"default": "./dist/index.js"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"publishConfig": {
|
|
35
|
+
"access": "public"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@tscomfy/core": "0.1.0",
|
|
39
|
+
"@tscomfy/fixtures": "0.0.0"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"@tscomfy/core": "^0.1.0"
|
|
43
|
+
},
|
|
44
|
+
"engines": {
|
|
45
|
+
"bun": ">=1.4",
|
|
46
|
+
"node": ">=24"
|
|
47
|
+
}
|
|
48
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @tscomfy/layout
|
|
3
|
+
*
|
|
4
|
+
* Automatic layout for workflows exported to the ComfyUI frontend's UI format:
|
|
5
|
+
* positions for `toUi`. Pure and runtime-agnostic, like core.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export { layered, type LayoutBox, type LayoutSpacing, type Position } from "./layered.ts";
|
|
9
|
+
export { layout } from "./layout.ts";
|
package/src/layered.ts
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// A layered layout (after Sugiyama et al., much simplified): columns by dependency depth, rows
|
|
2
|
+
// in each column ordered by where their sources sit, then packed from the top. Deterministic,
|
|
3
|
+
// and linear apart from one sort per column.
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A box to place: its size, and the boxes its inputs come from. Sizes and positions are in the
|
|
7
|
+
* same units, canvas units for `toUi` (the frontend's pixels at zoom 1).
|
|
8
|
+
*/
|
|
9
|
+
export interface LayoutBox {
|
|
10
|
+
/** Unique among the boxes laid out together. */
|
|
11
|
+
readonly id: string;
|
|
12
|
+
readonly width: number;
|
|
13
|
+
readonly height: number;
|
|
14
|
+
/** Ids of the boxes linked into this one; unknown ids and cycles are ignored. */
|
|
15
|
+
readonly sources: readonly string[];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Gaps between boxes, in the units of their sizes. */
|
|
19
|
+
export interface LayoutSpacing {
|
|
20
|
+
/** Between a column's widest box and the next column. */
|
|
21
|
+
readonly column: number;
|
|
22
|
+
/** Between one box and the next in a column. */
|
|
23
|
+
readonly row: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** The spacing unless one is given. */
|
|
27
|
+
const DEFAULT_SPACING: LayoutSpacing = { column: 80, row: 40 };
|
|
28
|
+
|
|
29
|
+
/** Where a box goes: its top-left corner. */
|
|
30
|
+
export type Position = readonly [x: number, y: number];
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Positions for boxes that flow left to right: a box sits one column right of its rightmost
|
|
34
|
+
* source, boxes without sources in the first column; in each column boxes are ordered by the
|
|
35
|
+
* average row of their sources (ties by input order), and each column is as wide as its widest
|
|
36
|
+
* box. Boxes on a cycle go where the cycle is broken, in input order. The first column starts at
|
|
37
|
+
* x 0 and every column at y 0; `spacing` defaults to 80 between columns and 40 between rows.
|
|
38
|
+
*
|
|
39
|
+
* Throws a `TypeError` when two boxes share an id: their positions couldn't be told apart.
|
|
40
|
+
*/
|
|
41
|
+
export function layered(
|
|
42
|
+
boxes: readonly LayoutBox[],
|
|
43
|
+
spacing: LayoutSpacing = DEFAULT_SPACING,
|
|
44
|
+
): ReadonlyMap<string, Position> {
|
|
45
|
+
const index = new Map<string, number>();
|
|
46
|
+
for (const [position, box] of boxes.entries()) {
|
|
47
|
+
if (index.has(box.id)) {
|
|
48
|
+
throw new TypeError(`layered: two boxes have the id ${JSON.stringify(box.id)}`);
|
|
49
|
+
}
|
|
50
|
+
index.set(box.id, position);
|
|
51
|
+
}
|
|
52
|
+
const columns = depths(boxes, index);
|
|
53
|
+
const byColumn = new Map<number, LayoutBox[]>();
|
|
54
|
+
for (const box of boxes) {
|
|
55
|
+
const column = columns.get(box.id) ?? 0;
|
|
56
|
+
const list = byColumn.get(column);
|
|
57
|
+
if (list === undefined) byColumn.set(column, [box]);
|
|
58
|
+
else list.push(box);
|
|
59
|
+
}
|
|
60
|
+
const rows = new Map<string, number>();
|
|
61
|
+
const positions = new Map<string, Position>();
|
|
62
|
+
let x = 0;
|
|
63
|
+
for (const column of [...byColumn.keys()].toSorted((a, b) => a - b)) {
|
|
64
|
+
const members = byColumn.get(column) ?? [];
|
|
65
|
+
const order = (box: LayoutBox): number => {
|
|
66
|
+
const placed = box.sources.flatMap((source) => {
|
|
67
|
+
const row = rows.get(source);
|
|
68
|
+
return row === undefined ? [] : [row];
|
|
69
|
+
});
|
|
70
|
+
return placed.length === 0
|
|
71
|
+
? Number.POSITIVE_INFINITY
|
|
72
|
+
: placed.reduce((sum, row) => sum + row, 0) / placed.length;
|
|
73
|
+
};
|
|
74
|
+
const sorted = members
|
|
75
|
+
.map((box) => ({ box, key: order(box), at: index.get(box.id) ?? 0 }))
|
|
76
|
+
.toSorted((a, b) => (a.key === b.key ? a.at - b.at : a.key < b.key ? -1 : 1));
|
|
77
|
+
let y = 0;
|
|
78
|
+
let width = 0;
|
|
79
|
+
for (const [row, { box }] of sorted.entries()) {
|
|
80
|
+
rows.set(box.id, row);
|
|
81
|
+
positions.set(box.id, [x, y]);
|
|
82
|
+
y += box.height + spacing.row;
|
|
83
|
+
width = Math.max(width, box.width);
|
|
84
|
+
}
|
|
85
|
+
x += width + spacing.column;
|
|
86
|
+
}
|
|
87
|
+
return positions;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Each box's column: the longest path from a box without sources (iterative, cycle-safe). */
|
|
91
|
+
function depths(
|
|
92
|
+
boxes: readonly LayoutBox[],
|
|
93
|
+
index: ReadonlyMap<string, number>,
|
|
94
|
+
): ReadonlyMap<string, number> {
|
|
95
|
+
const depth = new Map<string, number>();
|
|
96
|
+
const state = new Map<string, "open" | "done">();
|
|
97
|
+
for (const start of boxes) {
|
|
98
|
+
if (state.has(start.id)) continue;
|
|
99
|
+
const stack: { readonly box: LayoutBox; next: number }[] = [{ box: start, next: 0 }];
|
|
100
|
+
state.set(start.id, "open");
|
|
101
|
+
while (stack.length > 0) {
|
|
102
|
+
const frame = stack.at(-1);
|
|
103
|
+
if (frame === undefined) break;
|
|
104
|
+
const source = frame.box.sources[frame.next++];
|
|
105
|
+
if (source !== undefined) {
|
|
106
|
+
const at = index.get(source);
|
|
107
|
+
const box = at === undefined ? undefined : boxes[at];
|
|
108
|
+
// Unknown sources, and back edges of a cycle, don't count.
|
|
109
|
+
if (box !== undefined && !state.has(box.id)) {
|
|
110
|
+
state.set(box.id, "open");
|
|
111
|
+
stack.push({ box, next: 0 });
|
|
112
|
+
}
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
stack.pop();
|
|
116
|
+
state.set(frame.box.id, "done");
|
|
117
|
+
const sources = frame.box.sources.flatMap((id) => {
|
|
118
|
+
const value = state.get(id) === "done" ? depth.get(id) : undefined;
|
|
119
|
+
return value === undefined ? [] : [value];
|
|
120
|
+
});
|
|
121
|
+
depth.set(frame.box.id, sources.length === 0 ? 0 : Math.max(...sources) + 1);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return depth;
|
|
125
|
+
}
|
package/src/layout.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isOutputRef,
|
|
3
|
+
type InputValue,
|
|
4
|
+
type NodeId,
|
|
5
|
+
uiNodeSizes,
|
|
6
|
+
type Workflow,
|
|
7
|
+
} from "@tscomfy/core";
|
|
8
|
+
import { layered, type LayoutSpacing, type Position } from "./layered.ts";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Positions for `toUi`: the workflow laid out left to right in columns by
|
|
12
|
+
* dependency depth (see {@link layered}), each node the size `toUi` writes for it. A node's
|
|
13
|
+
* sources are the nodes its inputs link to, inside lists, Autogrow slots and dynamic-combo
|
|
14
|
+
* options too. `spacing` is in canvas units and defaults to 80 between columns and 40 between
|
|
15
|
+
* rows.
|
|
16
|
+
*
|
|
17
|
+
* ```ts
|
|
18
|
+
* const { workflow: ui } = toUi(built, { positions: layout(built) });
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export function layout(workflow: Workflow, spacing?: LayoutSpacing): ReadonlyMap<NodeId, Position> {
|
|
22
|
+
const sizes = uiNodeSizes(workflow);
|
|
23
|
+
const boxes = [...workflow.graph.nodes.values()].map((node) => {
|
|
24
|
+
const [width, height] = sizes.get(node.id) ?? [0, 0];
|
|
25
|
+
return { id: node.id, width, height, sources: sources(node.inputs) };
|
|
26
|
+
});
|
|
27
|
+
return layered(boxes, spacing);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The nodes a node's inputs link to. */
|
|
31
|
+
function sources(inputs: { readonly [name: string]: InputValue }): string[] {
|
|
32
|
+
const found = new Set<string>();
|
|
33
|
+
const pending: InputValue[] = Object.values(inputs);
|
|
34
|
+
for (let value = pending.pop(); value !== undefined; value = pending.pop()) {
|
|
35
|
+
if (isOutputRef(value)) found.add(value.node);
|
|
36
|
+
else if (Array.isArray(value)) pending.push(...(value as readonly InputValue[]));
|
|
37
|
+
else if (typeof value === "object" && value !== null) pending.push(...Object.values(value));
|
|
38
|
+
}
|
|
39
|
+
return [...found];
|
|
40
|
+
}
|