Line data Source code
1 : import { computed } from 'vue'
2 : import type { ComputedRef } from 'vue'
3 :
4 : import type { ComfyCommandImpl } from '@comfyorg/keybindings/commandStore'
5 :
6 : type SubcategoryRule = {
7 : pattern: string | RegExp
8 : subcategory: string
9 : }
10 :
11 : type SubcategoryConfig = {
12 : defaultSubcategory: string
13 : rules: SubcategoryRule[]
14 : }
15 :
16 : /**
17 : * Composable for grouping commands by subcategory based on configurable rules
18 : */
19 22 : export function useCommandSubcategories(
20 : commands: ComputedRef<ComfyCommandImpl[]>,
21 : config: SubcategoryConfig
22 : ) {
23 : const subcategories = computed(() => {
24 22 : const result: Partial<Record<string, ComfyCommandImpl[]>> = {}
25 :
26 22 : for (const command of commands.value) {
27 400 : let subcategory = config.defaultSubcategory
28 :
29 : // Find the first matching rule
30 400 : for (const rule of config.rules) {
31 : const matches =
32 : typeof rule.pattern === 'string'
33 : ? command.id.includes(rule.pattern)
34 : : rule.pattern.test(command.id)
35 :
36 918 : if (matches) {
37 286 : subcategory = rule.subcategory
38 286 : break
39 : }
40 : }
41 :
42 : const commands = (result[subcategory] ||= [])
43 400 : commands.push(command)
44 : }
45 :
46 22 : return result as Record<string, ComfyCommandImpl[]>
47 : })
48 :
49 22 : return {
50 : subcategories
51 : }
52 : }
53 :
54 : /**
55 : * Predefined configuration for view controls subcategories
56 : */
57 2355 : export const VIEW_CONTROLS_CONFIG: SubcategoryConfig = {
58 : defaultSubcategory: 'view',
59 : rules: [
60 : { pattern: 'Zoom', subcategory: 'view' },
61 : { pattern: 'Fit', subcategory: 'view' },
62 : { pattern: 'Panel', subcategory: 'panel-controls' },
63 : { pattern: 'Sidebar', subcategory: 'panel-controls' }
64 : ]
65 : }
66 :
67 : /**
68 : * Predefined configuration for essentials subcategories
69 : */
70 2355 : export const ESSENTIALS_CONFIG: SubcategoryConfig = {
71 : defaultSubcategory: 'workflow',
72 : rules: [
73 : { pattern: 'Workflow', subcategory: 'workflow' },
74 : { pattern: 'Node', subcategory: 'node' },
75 : { pattern: 'Queue', subcategory: 'queue' }
76 : ]
77 : }
|