Line data Source code
1 : import { isComboWidget } from '@/lib/litegraph/src/litegraph'
2 : import type { IBaseWidget, IComboWidget } from '@comfyorg/graph-model/widgets'
3 : import { findComboValueIndex } from '@/lib/litegraph/src/utils/widget'
4 : import type { ControlOptions } from '@/types/simplifiedWidget'
5 : import { isControlOption } from '@/types/simplifiedWidget'
6 :
7 : import { IS_CONTROL_WIDGET } from './controlWidgetMarker'
8 :
9 : type ValueControlMode = ControlOptions | 'increment-wrap'
10 :
11 200 : export function isValueControlMode(value: unknown): value is ValueControlMode {
12 200 : return isControlOption(value) || value === 'increment-wrap'
13 : }
14 :
15 65 : export function nextValueForLinkedTarget(params: {
16 : target: IBaseWidget
17 : linkedWidgets: IBaseWidget[] | undefined
18 : nodeId: unknown
19 : }): IBaseWidget['value'] | undefined {
20 65 : const linked = params.linkedWidgets
21 65 : if (!linked) return undefined
22 :
23 65 : const controlWidget = linked.find(isValueControlWidget)
24 65 : if (!controlWidget) return undefined
25 :
26 65 : const comboFilter = linked.find(
27 : (w) => w !== controlWidget && w.type === 'string'
28 : )
29 65 : const filterValue =
30 : typeof comboFilter?.value === 'string' ? comboFilter.value : undefined
31 :
32 65 : const mode = controlWidget.value as ValueControlMode
33 65 : return computeNextControlledValue(params.target, mode, {
34 : comboFilter: filterValue,
35 : nodeId: params.nodeId
36 : })
37 : }
38 :
39 2356 : const SAFE_INTEGER_MAX = 1125899906842624
40 2356 : const SAFE_INTEGER_MIN = -1125899906842624
41 :
42 69 : export function isValueControlWidget(widget: IBaseWidget): boolean {
43 69 : return (
44 : (widget as Record<symbol, unknown>)[IS_CONTROL_WIDGET] === true &&
45 : typeof widget.beforeQueued === 'function' &&
46 : typeof widget.afterQueued === 'function'
47 : )
48 : }
49 :
50 0 : function buildComboFilter(
51 : filter: string | undefined,
52 : nodeId?: unknown
53 : ): ((item: string) => boolean) | undefined {
54 0 : if (!filter) return undefined
55 :
56 0 : if (filter.startsWith('/') && filter.endsWith('/')) {
57 0 : try {
58 0 : const regex = new RegExp(filter.substring(1, filter.length - 1))
59 0 : return (item: string) => regex.test(item)
60 : } catch (error) {
61 0 : console.error(
62 : 'Error constructing RegExp filter for node ' + String(nodeId),
63 : filter,
64 : error
65 : )
66 : }
67 : }
68 :
69 0 : const lower = filter.toLocaleLowerCase()
70 0 : return (item: string) => item.toLocaleLowerCase().includes(lower)
71 : }
72 :
73 65 : export function computeNextControlledValue(
74 : target: IBaseWidget,
75 : mode: ValueControlMode,
76 : options: { comboFilter?: string; nodeId?: unknown } = {}
77 : ): IBaseWidget['value'] | undefined {
78 65 : if (mode === 'fixed') return undefined
79 :
80 51 : if (isComboWidget(target)) {
81 0 : return computeNextComboValue(target, mode, options)
82 : }
83 :
84 51 : return computeNextNumberValue(target, mode)
85 : }
86 :
87 0 : function computeNextComboValue(
88 : target: IComboWidget,
89 : mode: ValueControlMode,
90 : { comboFilter, nodeId }: { comboFilter?: string; nodeId?: unknown }
91 : ): IBaseWidget['value'] | undefined {
92 0 : const rawValues = target.options.values
93 0 : if (!Array.isArray(rawValues)) return undefined
94 :
95 0 : const allValues = rawValues.filter(
96 : (value): value is string | number =>
97 : typeof value === 'string' || typeof value === 'number'
98 : )
99 0 : const check = buildComboFilter(comboFilter, nodeId)
100 0 : const values = check
101 : ? allValues.filter((value) => check(String(value)))
102 : : allValues
103 :
104 0 : if (!values.length) {
105 0 : if (allValues.length) {
106 0 : console.warn(
107 : 'Filter for node ' + String(nodeId) + ' has filtered out all items',
108 : comboFilter
109 : )
110 : }
111 0 : return undefined
112 : }
113 :
114 0 : let currentIndex = findComboValueIndex(values, target.value)
115 0 : const length = values.length
116 :
117 0 : switch (mode) {
118 : case 'increment':
119 0 : currentIndex += 1
120 0 : break
121 : case 'increment-wrap':
122 0 : currentIndex += 1
123 0 : if (currentIndex >= length) currentIndex = 0
124 0 : break
125 : case 'decrement':
126 0 : currentIndex -= 1
127 0 : break
128 : case 'randomize':
129 0 : currentIndex = Math.floor(Math.random() * length)
130 : break
131 : }
132 :
133 0 : currentIndex = Math.max(0, Math.min(length - 1, currentIndex))
134 0 : return values[currentIndex]
135 : }
136 :
137 51 : function computeNextNumberValue(
138 : target: IBaseWidget,
139 : mode: ValueControlMode
140 : ): number | undefined {
141 51 : if (typeof target.value !== 'number') return undefined
142 :
143 51 : const { min: rawMin = 0, max: rawMax = 1, step2 = 1 } = target.options
144 51 : const max = Math.min(SAFE_INTEGER_MAX, rawMax)
145 51 : const min = Math.max(SAFE_INTEGER_MIN, rawMin)
146 51 : const range = (max - min) / step2
147 :
148 51 : let next = target.value
149 51 : switch (mode) {
150 : case 'increment':
151 : case 'increment-wrap':
152 5 : next += step2
153 5 : break
154 : case 'decrement':
155 0 : next -= step2
156 0 : break
157 : case 'randomize':
158 46 : next = Math.floor(Math.random() * range) * step2 + min
159 : break
160 : }
161 :
162 51 : return Math.min(Math.max(next, min), max)
163 : }
|