Line data Source code
1 : <template>
2 13402 : <Menu
3 : v-bind="menuProps"
4 : :items="menuItems"
5 : :reference="menuReference"
6 : update-position-strategy="always"
7 : >
8 : <template #item="{ item, hasSubmenu }">
9 : <div class="flex w-full items-center gap-2">
10 : <span
11 : v-if="getItemColor(item)"
12 : class="size-5 rounded-full border"
13 : :style="{ backgroundColor: getItemColor(item) }"
14 : />
15 : <i v-else-if="item.icon" :class="item.icon" />
16 : <span class="flex-1">{{ item.label }}</span>
17 : <span
18 : v-if="item.shortcut"
19 : class="ml-auto rounded-sm border bg-secondary p-1 text-xs text-nowrap text-muted-foreground"
20 : >
21 : {{ item.shortcut }}
22 : </span>
23 : <i v-if="hasSubmenu" class="icon-[lucide--chevron-right] opacity-60" />
24 : </div>
25 : </template>
26 : </Menu>
27 : </template>
28 :
29 : <script setup lang="ts">
30 : import { computed, onMounted, onUnmounted, ref } from 'vue'
31 :
32 : import Menu from '@comfyorg/design-system/menu/Menu'
33 : import { getMenuAnchorPosition } from '@comfyorg/design-system/lib/menu/menuAnchor'
34 : import { usePointMenu } from '@comfyorg/design-system/lib/menu/usePointMenu'
35 : import type {
36 : MenuItem,
37 : MenuItemAction,
38 : MenuItemRadioGroup,
39 : MenuItemSeparator,
40 : MenuItemSubmenu
41 : } from '@comfyorg/design-system/lib/menu/types'
42 : import {
43 : registerNodeOptionsInstance,
44 : useMoreOptionsMenu
45 : } from '@/composables/graph/useMoreOptionsMenu'
46 : import type { MenuOption } from '@/composables/graph/menuOption'
47 : import { useNodeCustomization } from '@/composables/graph/useNodeCustomization'
48 : import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
49 :
50 2355 : const { menuOptions, bump } = useMoreOptionsMenu()
51 2355 : const { getCurrentShape } = useNodeCustomization()
52 2355 : const canvasStore = useCanvasStore()
53 :
54 : interface NodeMenuMetadata {
55 : color?: string
56 : }
57 :
58 : type NodeMenuAction = MenuItemAction & NodeMenuMetadata
59 :
60 : type NodeMenuSubmenu = Omit<MenuItemSubmenu, 'items'> &
61 : NodeMenuMetadata & {
62 : items: NodeMenuItem[]
63 : }
64 :
65 : type NodeMenuItem =
66 : | MenuItemSeparator
67 : | NodeMenuAction
68 : | NodeMenuSubmenu
69 : | MenuItemRadioGroup
70 :
71 12595 : function getItemColor(item: MenuItem): string | undefined {
72 12595 : return 'color' in item && typeof item.color === 'string'
73 : ? item.color
74 : : undefined
75 : }
76 :
77 2355 : const worldPosition = ref({ x: 0, y: 0 })
78 :
79 2355 : const lgCanvas = canvasStore.getCanvas()
80 2355 : const menuReference = {
81 : contextElement: lgCanvas.canvas,
82 : getBoundingClientRect() {
83 2260 : const { scale, offset } = lgCanvas.ds
84 2260 : const { left, top } = lgCanvas.canvas.getBoundingClientRect()
85 2260 : return new DOMRect(
86 : (worldPosition.value.x + offset[0]) * scale + left,
87 : (worldPosition.value.y + offset[1]) * scale + top,
88 : 1,
89 : 1
90 : )
91 : }
92 : }
93 :
94 209237 : function convertToMenuItem(option: MenuOption): NodeMenuItem {
95 209237 : if (option.type === 'divider') return { separator: true }
96 :
97 209237 : if (option.isShapePicker && option.submenu) {
98 13334 : return {
99 : label: option.label,
100 : icon: option.icon,
101 : radioGroup: {
102 : value: getCurrentShape()?.localizedName ?? '',
103 : options: option.submenu.map((sub) => ({
104 : value: sub.label,
105 : label: sub.label,
106 : command: sub.action
107 : }))
108 : }
109 : }
110 : }
111 :
112 209237 : if (option.hasSubmenu && option.submenu) {
113 13848 : return {
114 : label: option.label,
115 : icon: option.icon,
116 : disabled: option.disabled,
117 : shortcut: option.shortcut,
118 : items: option.submenu.map((sub) => ({
119 : label: sub.label,
120 : icon: sub.icon,
121 : color: sub.color,
122 : disabled: sub.disabled,
123 : command: sub.action
124 : }))
125 : }
126 : }
127 :
128 114533 : return {
129 : label: option.label,
130 : icon: option.icon,
131 : disabled: option.disabled,
132 : shortcut: option.shortcut,
133 : command: option.action
134 : }
135 : }
136 :
137 2355 : const menuItems = computed<MenuItem[]>(() =>
138 : menuOptions.value.map(convertToMenuItem)
139 : )
140 :
141 172 : function placeAtEvent(event: Event) {
142 172 : if (event.type === 'contextmenu') event.preventDefault()
143 172 : bump()
144 :
145 172 : const { x, y } = getMenuAnchorPosition(event)
146 172 : const { left, top } = lgCanvas.canvas.getBoundingClientRect()
147 172 : const { scale, offset } = lgCanvas.ds
148 172 : worldPosition.value = {
149 : x: (x - left) / scale - offset[0],
150 : y: (y - top) / scale - offset[1]
151 : }
152 : }
153 :
154 2355 : const {
155 : open: isOpen,
156 : menuProps,
157 : show,
158 : hide,
159 : toggle
160 : } = usePointMenu(placeAtEvent)
161 :
162 2355 : onMounted(() => {
163 2355 : registerNodeOptionsInstance({ toggle, show, hide, isOpen })
164 : })
165 :
166 2355 : onUnmounted(() => {
167 0 : registerNodeOptionsInstance(null)
168 : })
169 : </script>
|