Line data Source code
1 : import { defineStore } from 'pinia'
2 : import { ref } from 'vue'
3 :
4 : import type {
5 : MenuItem,
6 : MenuItemAction
7 : } from '@comfyorg/design-system/lib/menu/types'
8 : import { CORE_MENU_COMMANDS } from '@/constants/coreMenuCommands'
9 : import type { ComfyExtension } from '@/types/comfy'
10 :
11 : import { useCommandStore } from '@/platform/keybindings/commandStore'
12 :
13 : export interface CommandMenuItem extends MenuItemAction {
14 : commandId: string
15 : }
16 :
17 : export type AppMenuItem = MenuItem | CommandMenuItem
18 :
19 2356 : export const useMenuItemStore = defineStore('menuItem', () => {
20 2355 : const commandStore = useCommandStore()
21 2355 : const menuItems = ref<AppMenuItem[]>([])
22 :
23 2355 : const registerMenuGroup = (path: string[], items: AppMenuItem[]) => {
24 32972 : let currentLevel = menuItems.value
25 :
26 : // Traverse the path, creating nodes if necessary
27 32972 : for (let i = 0; i < path.length; i++) {
28 28262 : const segment = path[i]
29 28262 : const foundIndex = currentLevel.findIndex(
30 : (item) => item.label === segment
31 : )
32 28262 : let found = currentLevel[foundIndex]
33 :
34 28262 : if (foundIndex === -1) {
35 : // Create a new node if it doesn't exist
36 9422 : found = {
37 : label: segment,
38 : key: segment,
39 : items: []
40 : }
41 9422 : currentLevel.push(found)
42 : }
43 :
44 28262 : if (!found.items) {
45 0 : const { checked, command, radioGroup, separator, ...metadata } = found
46 0 : found = { ...metadata, items: [] }
47 0 : currentLevel[foundIndex] = found
48 : }
49 :
50 : // Move to the next level
51 28262 : currentLevel = found.items
52 : }
53 :
54 32972 : if (currentLevel.length > 0) {
55 18840 : currentLevel.push({
56 : separator: true
57 : })
58 : }
59 : // Add the new items to the last level
60 32972 : currentLevel.push(...items)
61 : }
62 73020 : function commandIdToMenuItem(commandId: string): CommandMenuItem {
63 73020 : const command = commandStore.getCommand(commandId)
64 73020 : return {
65 : command: () => commandStore.execute(command.id),
66 : label: command.menubarLabel,
67 : icon: command.id === 'Comfy.NewBlankWorkflow' ? undefined : command.icon,
68 : tooltip: command.tooltip,
69 : commandId: command.id,
70 : checked: command.active,
71 : shortcut: () => command.keybinding?.combo.toString(),
72 : pressAndHoldInterval:
73 : command.id === 'Comfy.Canvas.ZoomIn' ||
74 : command.id === 'Comfy.Canvas.ZoomOut'
75 : ? 50
76 : : undefined,
77 : trailingIcon:
78 : command.id === 'Comfy.NewBlankWorkflow' ? command.icon : undefined
79 : }
80 : }
81 :
82 2355 : const registerCommands = (path: string[], commandIds: string[]) => {
83 32972 : const items = commandIds.map(commandIdToMenuItem)
84 32972 : registerMenuGroup(path, items)
85 : }
86 :
87 2355 : const loadExtensionMenuCommands = (extension: ComfyExtension) => {
88 82789 : if (!extension.menuCommands) {
89 82786 : return
90 : }
91 :
92 82789 : const extensionCommandIds = new Set(
93 : extension.commands?.map((command) => command.id) ?? []
94 : )
95 82789 : extension.menuCommands.forEach((menuCommand) => {
96 3 : const commands = menuCommand.commands.filter((command) =>
97 : extensionCommandIds.has(command)
98 : )
99 3 : if (commands.length) {
100 2 : registerCommands(menuCommand.path, commands)
101 : }
102 : })
103 : }
104 :
105 2355 : const registerCoreMenuCommands = () => {
106 2355 : for (const [path, commands] of CORE_MENU_COMMANDS) {
107 28260 : registerCommands(path, commands)
108 : }
109 : }
110 :
111 2355 : return {
112 : menuItems,
113 : registerMenuGroup,
114 : registerCommands,
115 : loadExtensionMenuCommands,
116 : registerCoreMenuCommands,
117 : commandIdToMenuItem
118 : }
119 : })
|