Line data Source code
1 : import { useI18n } from 'vue-i18n'
2 :
3 : import type { RectSide as Direction } from '@comfyorg/shared-frontend-utils/geometry'
4 : import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
5 : import { alignNodes, distributeNodes } from '@/lib/litegraph/src/utils/arrange'
6 : import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
7 : import { isLGraphNode } from '@/utils/litegraphUtil'
8 :
9 : import { useCanvasRefresh } from './useCanvasRefresh'
10 :
11 : interface AlignOption {
12 : name: string
13 : localizedName: string
14 : value: Direction
15 : icon: string
16 : }
17 :
18 : interface DistributeOption {
19 : name: string
20 : localizedName: string
21 : value: boolean // true for horizontal, false for vertical
22 : icon: string
23 : }
24 :
25 : /**
26 : * Composable for handling node alignment and distribution
27 : */
28 2355 : export function useNodeArrangement() {
29 2355 : const { t } = useI18n()
30 2355 : const canvasStore = useCanvasStore()
31 2355 : const canvasRefresh = useCanvasRefresh()
32 2355 : const alignOptions: AlignOption[] = [
33 : {
34 : name: 'top',
35 : localizedName: t('contextMenu.Top'),
36 : value: 'top',
37 : icon: 'icon-[lucide--align-start-horizontal]'
38 : },
39 : {
40 : name: 'bottom',
41 : localizedName: t('contextMenu.Bottom'),
42 : value: 'bottom',
43 : icon: 'icon-[lucide--align-end-horizontal]'
44 : },
45 : {
46 : name: 'left',
47 : localizedName: t('contextMenu.Left'),
48 : value: 'left',
49 : icon: 'icon-[lucide--align-start-vertical]'
50 : },
51 : {
52 : name: 'right',
53 : localizedName: t('contextMenu.Right'),
54 : value: 'right',
55 : icon: 'icon-[lucide--align-end-vertical]'
56 : }
57 : ]
58 :
59 2355 : const distributeOptions: DistributeOption[] = [
60 : {
61 : name: 'horizontal',
62 : localizedName: t('contextMenu.Horizontal'),
63 : value: true,
64 : icon: 'icon-[lucide--align-center-horizontal]'
65 : },
66 : {
67 : name: 'vertical',
68 : localizedName: t('contextMenu.Vertical'),
69 : value: false,
70 : icon: 'icon-[lucide--align-center-vertical]'
71 : }
72 : ]
73 :
74 2355 : const applyAlign = (alignOption: AlignOption, alignTo?: LGraphNode) => {
75 1 : const selectedNodes = Array.from(canvasStore.selectedItems).filter((item) =>
76 : isLGraphNode(item)
77 : )
78 :
79 1 : if (selectedNodes.length === 0) {
80 0 : return
81 : }
82 :
83 1 : const newPositions = alignNodes(selectedNodes, alignOption.value, alignTo)
84 1 : canvasStore.canvas?.applyNodePositions(newPositions)
85 :
86 1 : canvasRefresh.refreshCanvas()
87 : }
88 :
89 2355 : const applyDistribute = (distributeOption: DistributeOption) => {
90 1 : const selectedNodes = Array.from(canvasStore.selectedItems).filter((item) =>
91 : isLGraphNode(item)
92 : )
93 :
94 1 : if (selectedNodes.length < 2) {
95 0 : return
96 : }
97 :
98 1 : const newPositions = distributeNodes(selectedNodes, distributeOption.value)
99 1 : canvasStore.canvas?.applyNodePositions(newPositions)
100 1 : canvasRefresh.refreshCanvas()
101 : }
102 :
103 2355 : return {
104 : alignOptions,
105 : distributeOptions,
106 : applyAlign,
107 : applyDistribute
108 : }
109 : }
|