Line data Source code
1 : import Fuse from 'fuse.js'
2 : import type { IFuseOptions } from 'fuse.js'
3 :
4 : import type { IBaseWidget } from '@comfyorg/graph-model/widgets'
5 : import type { NodeId } from '@comfyorg/graph-model/nodeId'
6 :
7 : import type { NodeWidgetsListList } from './widgetSections'
8 :
9 : interface WidgetSearchItem {
10 : index: number
11 : searchableLabel: string
12 : searchableName: string
13 : searchableType: string
14 : searchableValue: string
15 : }
16 :
17 : /** Returns the entries whose widget name, label, type, or value matches the query, in input order. */
18 233 : export function searchWidgets<T extends { widget: IBaseWidget }>(
19 : list: T[],
20 : query: string
21 : ): T[] {
22 233 : if (query.trim() === '') {
23 226 : return list
24 : }
25 :
26 7 : const searchableList: WidgetSearchItem[] = list.map((item, index) => {
27 : const searchableItem = {
28 : index,
29 : searchableLabel: item.widget.label?.toLowerCase() || '',
30 : searchableName: item.widget.name.toLowerCase(),
31 : searchableType:
32 : typeof item.widget.type === 'string'
33 : ? item.widget.type.toLowerCase()
34 : : '',
35 : searchableValue: item.widget.value?.toString().toLowerCase() || ''
36 : }
37 16 : return searchableItem
38 : })
39 :
40 : const fuseOptions: IFuseOptions<WidgetSearchItem> = {
41 : keys: [
42 : { name: 'searchableName', weight: 0.4 },
43 : { name: 'searchableLabel', weight: 0.3 },
44 : { name: 'searchableValue', weight: 0.3 },
45 : { name: 'searchableType', weight: 0.2 }
46 : ],
47 : threshold: 0.3
48 : }
49 :
50 : const fuse = new Fuse(searchableList, fuseOptions)
51 7 : const results = fuse.search(query.trim())
52 :
53 7 : const matchedItems = new Set(results.map((result) => list[result.item.index]))
54 :
55 7 : return list.filter((item) => matchedItems.has(item))
56 : }
57 :
58 : type NodeSearchItem = {
59 : nodeId: NodeId
60 : searchableTitle: string
61 : }
62 :
63 : /** Returns whole nodes whose title matches the query, and other nodes trimmed to their matching widgets, in input order. */
64 110 : export function searchWidgetsAndNodes(
65 : list: NodeWidgetsListList,
66 : query: string
67 : ): NodeWidgetsListList {
68 110 : if (query.trim() === '') {
69 108 : return list
70 : }
71 :
72 2 : const searchableList: NodeSearchItem[] = list.map((item) => ({
73 : nodeId: item.node.id,
74 : searchableTitle: (item.node.getTitle() ?? item.node.type).toLowerCase()
75 : }))
76 :
77 : const fuseOptions: IFuseOptions<NodeSearchItem> = {
78 : keys: [{ name: 'searchableTitle', weight: 1.0 }],
79 : threshold: 0.3
80 : }
81 :
82 : const fuse = new Fuse(searchableList, fuseOptions)
83 2 : const nodeMatches = fuse.search(query.trim())
84 2 : const matchedNodeIds = new Set(
85 : nodeMatches.map((result) => result.item.nodeId)
86 : )
87 :
88 2 : return list.flatMap((item) => {
89 6 : if (matchedNodeIds.has(item.node.id)) {
90 0 : return [item]
91 : }
92 :
93 6 : const widgets = searchWidgets(item.widgets, query)
94 6 : return widgets.length > 0 ? [{ ...item, widgets }] : []
95 : })
96 : }
|