Line data Source code
1 : import type { LGraph } from '@comfyorg/graph-model/entityRegistry'
2 : import type { NodeId } from '@comfyorg/graph-model/nodeId'
3 : import type { UUID } from '@/utils/uuid'
4 :
5 : import { layoutStore } from '@/renderer/core/layout/store/layoutStore'
6 : import type { LayoutSource } from '@/renderer/core/layout/types'
7 : import type { Point2D as Point } from '@comfyorg/shared-frontend-utils/geometry'
8 :
9 26971 : export function useLayoutMutations(source: LayoutSource) {
10 26971 : const moveNode = (
11 : rootGraphId: UUID,
12 : nodeId: NodeId,
13 : position: Point
14 : ): void => {
15 : const existing = layoutStore.getNodeLayout(rootGraphId, nodeId)
16 0 : if (!existing) return
17 :
18 0 : layoutStore.applyOperation({
19 : type: 'moveNode',
20 : graphId: rootGraphId,
21 : nodeId,
22 : position,
23 : timestamp: Date.now(),
24 : source
25 : })
26 : }
27 :
28 308 : function batchMoveNodes(
29 : rootGraphId: UUID,
30 : updates: Array<{ nodeId: NodeId; position: Point }>
31 : ): void {
32 308 : if (updates.length === 0) return
33 :
34 308 : const nodeBoundsUpdates = updates.flatMap(({ nodeId, position }) => {
35 353 : const existing = layoutStore.getNodeLayout(rootGraphId, nodeId)
36 353 : if (!existing) return []
37 :
38 353 : return [
39 : {
40 : nodeId,
41 : bounds: {
42 : x: position.x,
43 : y: position.y,
44 : width: existing.size.width,
45 : height: existing.size.height
46 : }
47 : }
48 : ]
49 : })
50 :
51 308 : if (nodeBoundsUpdates.length === 0) return
52 308 : layoutStore.batchUpdateNodeBounds(rootGraphId, nodeBoundsUpdates, {
53 : source
54 : })
55 : }
56 :
57 26971 : const setNodeZIndex = (
58 : rootGraphId: UUID,
59 : nodeId: NodeId,
60 : zIndex: number
61 : ): void => {
62 : const existing = layoutStore.getNodeLayout(rootGraphId, nodeId)
63 1559 : if (!existing) return
64 :
65 1559 : layoutStore.applyOperation({
66 : type: 'setNodeZIndex',
67 : graphId: rootGraphId,
68 : nodeId,
69 : zIndex,
70 : timestamp: Date.now(),
71 : source
72 : })
73 : }
74 :
75 26971 : const bringNodeToFront = (rootGraphId: UUID, nodeId: NodeId): void => {
76 0 : setNodeZIndex(rootGraphId, nodeId, layoutStore.allocateZIndex())
77 : }
78 :
79 26971 : const setNodeOrder = (
80 : graph: LGraph,
81 : nodeId: NodeId,
82 : order: 'front' | 'back'
83 : ): void => {
84 1469 : const rootGraphId = graph.rootGraph.id
85 1469 : if (!graph._nodes.some((node) => node.id === nodeId)) return
86 1469 : if (!layoutStore.getNodeLayout(rootGraphId, nodeId)) return
87 :
88 1469 : const zIndex =
89 : order === 'front'
90 : ? layoutStore.allocateZIndex()
91 : : graph._nodes.reduce(
92 : (lowest, node) =>
93 : Math.min(
94 : lowest,
95 : layoutStore.getNodeLayout(rootGraphId, node.id)?.zIndex ?? 0
96 : ),
97 : 0
98 : ) - 1
99 1469 : setNodeZIndex(rootGraphId, nodeId, zIndex)
100 : }
101 :
102 26971 : return {
103 : moveNode,
104 : batchMoveNodes,
105 : setNodeZIndex,
106 : bringNodeToFront,
107 : setNodeOrder
108 : }
109 : }
|