Skip to content

Commit b9d9ce7

Browse files
authored
[TS] Widget typing (#3804)
1 parent bb1ac32 commit b9d9ce7

File tree

12 files changed

+51
-65
lines changed

12 files changed

+51
-65
lines changed

src/components/load3d/controls/RecordingControls.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
</template>
7676

7777
<script setup lang="ts">
78-
import { IWidget, LGraphNode } from '@comfyorg/litegraph'
78+
import { LGraphNode } from '@comfyorg/litegraph'
7979
import { Tooltip } from 'primevue'
8080
import Button from 'primevue/button'
8181
@@ -101,13 +101,13 @@ const emit = defineEmits<{
101101
const resizeNodeMatchOutput = () => {
102102
console.log('resizeNodeMatchOutput')
103103
104-
const outputWidth = node.widgets?.find((w: IWidget) => w.name === 'width')
105-
const outputHeight = node.widgets?.find((w: IWidget) => w.name === 'height')
104+
const outputWidth = node.widgets?.find((w) => w.name === 'width')
105+
const outputHeight = node.widgets?.find((w) => w.name === 'height')
106106
107107
if (outputWidth && outputHeight && outputHeight.value && outputWidth.value) {
108108
const [oldWidth, oldHeight] = node.size
109109
110-
const scene = node.widgets?.find((w: IWidget) => w.name === 'image')
110+
const scene = node.widgets?.find((w) => w.name === 'image')
111111
112112
const sceneHeight = scene?.computedHeight
113113

src/composables/widgets/useFloatWidget.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ export const useFloatWidget = () => {
5050
Math.max(0, -Math.floor(Math.log10(step)))
5151
const enableRounding = !settingStore.get('Comfy.DisableFloatRounding')
5252

53-
const defaultValue = inputSpec.default ?? 0
53+
/** Assertion {@link inputSpec.default} */
54+
const defaultValue = (inputSpec.default as number | undefined) ?? 0
5455
return node.addWidget(
5556
widgetType,
5657
inputSpec.name,

src/composables/widgets/useIntWidget.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ export const useIntWidget = () => {
5555
: 'number'
5656

5757
const step = inputSpec.step ?? 1
58-
const defaultValue = inputSpec.default ?? 0
58+
/** Assertion {@link inputSpec.default} */
59+
const defaultValue = (inputSpec.default as number | undefined) ?? 0
5960
const widget = node.addWidget(
6061
widgetType,
6162
inputSpec.name,

src/extensions/core/contextMenuFilter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const ext = {
3636
w.type === 'combo' && w.options.values?.length === values.length
3737
)
3838
.find((w) =>
39-
// @ts-ignore Poorly typed; filter above "should" mitigate exceptions
39+
// @ts-expect-error Poorly typed; filter above "should" mitigate exceptions
4040
w.options.values?.every((v, i) => v === values[i])
4141
)?.value
4242

src/extensions/core/load3d.ts

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { IWidget } from '@comfyorg/litegraph'
2-
import { IStringWidget } from '@comfyorg/litegraph/dist/types/widgets'
1+
import type { IStringWidget } from '@comfyorg/litegraph/dist/types/widgets'
32
import { nextTick } from 'vue'
43

54
import Load3D from '@/components/load3d/Load3D.vue'
@@ -116,7 +115,7 @@ useExtensionService().registerExtension({
116115
fileInput.onchange = async () => {
117116
if (fileInput.files?.length) {
118117
const modelWidget = node.widgets?.find(
119-
(w: IWidget) => w.name === 'model_file'
118+
(w) => w.name === 'model_file'
120119
) as IStringWidget
121120

122121
node.properties['Texture'] = undefined
@@ -139,7 +138,7 @@ useExtensionService().registerExtension({
139138

140139
if (uploadPath && modelWidget) {
141140
if (!modelWidget.options?.values?.includes(uploadPath)) {
142-
// @ts-ignore Fails due to earlier type-assertion of IStringWidget
141+
// @ts-expect-error Fails due to earlier type-assertion of IStringWidget
143142
modelWidget.options?.values?.push(uploadPath)
144143
}
145144

@@ -155,9 +154,7 @@ useExtensionService().registerExtension({
155154
node.addWidget('button', 'clear', 'clear', () => {
156155
useLoad3dService().getLoad3d(node)?.clearModel()
157156

158-
const modelWidget = node.widgets?.find(
159-
(w: IWidget) => w.name === 'model_file'
160-
)
157+
const modelWidget = node.widgets?.find((w) => w.name === 'model_file')
161158
if (modelWidget) {
162159
modelWidget.value = ''
163160

@@ -203,12 +200,10 @@ useExtensionService().registerExtension({
203200

204201
const config = new Load3DConfiguration(load3d)
205202

206-
const modelWidget = node.widgets?.find(
207-
(w: IWidget) => w.name === 'model_file'
208-
)
209-
const width = node.widgets?.find((w: IWidget) => w.name === 'width')
210-
const height = node.widgets?.find((w: IWidget) => w.name === 'height')
211-
const sceneWidget = node.widgets?.find((w: IWidget) => w.name === 'image')
203+
const modelWidget = node.widgets?.find((w) => w.name === 'model_file')
204+
const width = node.widgets?.find((w) => w.name === 'width')
205+
const height = node.widgets?.find((w) => w.name === 'height')
206+
const sceneWidget = node.widgets?.find((w) => w.name === 'image')
212207

213208
if (modelWidget && width && height && sceneWidget) {
214209
config.configure('input', modelWidget, cameraState, width, height)
@@ -276,7 +271,7 @@ useExtensionService().registerExtension({
276271
fileInput.onchange = async () => {
277272
if (fileInput.files?.length) {
278273
const modelWidget = node.widgets?.find(
279-
(w: IWidget) => w.name === 'model_file'
274+
(w) => w.name === 'model_file'
280275
) as IStringWidget
281276

282277
const uploadPath = await Load3dUtils.uploadFile(
@@ -297,7 +292,7 @@ useExtensionService().registerExtension({
297292

298293
if (uploadPath && modelWidget) {
299294
if (!modelWidget.options?.values?.includes(uploadPath)) {
300-
// @ts-ignore Fails due to earlier type-assertion of IStringWidget
295+
// @ts-expect-error Fails due to earlier type-assertion of IStringWidget
301296
modelWidget.options?.values?.push(uploadPath)
302297
}
303298

@@ -313,9 +308,7 @@ useExtensionService().registerExtension({
313308
node.addWidget('button', 'clear', 'clear', () => {
314309
useLoad3dService().getLoad3d(node)?.clearModel()
315310

316-
const modelWidget = node.widgets?.find(
317-
(w: IWidget) => w.name === 'model_file'
318-
)
311+
const modelWidget = node.widgets?.find((w) => w.name === 'model_file')
319312
if (modelWidget) {
320313
modelWidget.value = ''
321314
}
@@ -352,18 +345,16 @@ useExtensionService().registerExtension({
352345

353346
await nextTick()
354347

355-
const sceneWidget = node.widgets?.find((w: IWidget) => w.name === 'image')
348+
const sceneWidget = node.widgets?.find((w) => w.name === 'image')
356349

357350
const load3d = useLoad3dService().getLoad3d(node) as Load3dAnimation
358351

359-
const modelWidget = node.widgets?.find(
360-
(w: IWidget) => w.name === 'model_file'
361-
)
352+
const modelWidget = node.widgets?.find((w) => w.name === 'model_file')
362353

363354
let cameraState = node.properties['Camera Info']
364355

365-
const width = node.widgets?.find((w: IWidget) => w.name === 'width')
366-
const height = node.widgets?.find((w: IWidget) => w.name === 'height')
356+
const width = node.widgets?.find((w) => w.name === 'width')
357+
const height = node.widgets?.find((w) => w.name === 'height')
367358

368359
if (modelWidget && width && height && sceneWidget && load3d) {
369360
const config = new Load3DConfiguration(load3d)
@@ -479,9 +470,7 @@ useExtensionService().registerExtension({
479470

480471
let cameraState = message.result[1]
481472

482-
const modelWidget = node.widgets?.find(
483-
(w: IWidget) => w.name === 'model_file'
484-
)
473+
const modelWidget = node.widgets?.find((w) => w.name === 'model_file')
485474

486475
if (load3d && modelWidget) {
487476
modelWidget.value = filePath.replaceAll('\\', '/')
@@ -555,9 +544,7 @@ useExtensionService().registerExtension({
555544

556545
const load3d = useLoad3dService().getLoad3d(node)
557546

558-
const modelWidget = node.widgets?.find(
559-
(w: IWidget) => w.name === 'model_file'
560-
)
547+
const modelWidget = node.widgets?.find((w) => w.name === 'model_file')
561548
if (load3d && modelWidget) {
562549
modelWidget.value = filePath.replaceAll('\\', '/')
563550

src/extensions/core/previewAny.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ Preview Any - original implement from
33
https://github.com/rgthree/rgthree-comfy/blob/main/py/display_any.py
44
upstream requested in https://github.com/Kosinkadink/rfcs/blob/main/rfcs/0000-corenodes.md#preview-nodes
55
*/
6-
import { IWidget } from '@comfyorg/litegraph'
7-
86
import { DOMWidget } from '@/scripts/domWidget'
97
import { ComfyWidgets } from '@/scripts/widgets'
108
import { useExtensionService } from '@/services/extensionService'
@@ -37,9 +35,7 @@ useExtensionService().registerExtension({
3735
? void 0
3836
: onExecuted.apply(this, [message])
3937

40-
const previewWidget = this.widgets?.find(
41-
(w: IWidget) => w.name === 'preview'
42-
)
38+
const previewWidget = this.widgets?.find((w) => w.name === 'preview')
4339

4440
if (previewWidget) {
4541
previewWidget.value = message.text[0]

src/extensions/core/saveMesh.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { IWidget } from '@comfyorg/litegraph'
21
import { nextTick } from 'vue'
32

43
import Load3D from '@/components/load3d/Load3D.vue'
@@ -61,7 +60,7 @@ useExtensionService().registerExtension({
6160

6261
const load3d = useLoad3dService().getLoad3d(node)
6362

64-
const modelWidget = node.widgets?.find((w: IWidget) => w.name === 'image')
63+
const modelWidget = node.widgets?.find((w) => w.name === 'image')
6564

6665
if (load3d && modelWidget) {
6766
const filePath = fileInfo['subfolder'] + '/' + fileInfo['filename']

src/extensions/core/uploadAudio.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { IWidget, LGraphNode } from '@comfyorg/litegraph'
1+
import type { LGraphNode } from '@comfyorg/litegraph'
22
import type { IStringWidget } from '@comfyorg/litegraph/dist/types/widgets'
33

44
import { useNodeDragAndDrop } from '@/composables/node/useNodeDragAndDrop'
@@ -164,11 +164,11 @@ app.registerExtension({
164164
// The widget that allows user to select file.
165165
// @ts-expect-error fixme ts strict error
166166
const audioWidget = node.widgets.find(
167-
(w: IWidget) => w.name === 'audio'
167+
(w) => w.name === 'audio'
168168
) as IStringWidget
169169
// @ts-expect-error fixme ts strict error
170170
const audioUIWidget = node.widgets.find(
171-
(w: IWidget) => w.name === 'audioUI'
171+
(w) => w.name === 'audioUI'
172172
) as unknown as DOMWidget<HTMLAudioElement, string>
173173

174174
const onAudioWidgetUpdate = () => {

src/schemas/nodeDefSchema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export const zNumericInputOptions = zBaseInputOptions.extend({
3636
min: z.number().optional(),
3737
max: z.number().optional(),
3838
step: z.number().optional(),
39-
// Note: Many node authors are using INT/FLOAT to pass list of INT/FLOAT.
39+
/** Note: Many node authors are using INT/FLOAT to pass list of INT/FLOAT. */
4040
default: z.union([z.number(), z.array(z.number())]).optional(),
4141
display: z.enum(['slider', 'number', 'knob']).optional()
4242
})

src/scripts/widgets.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export function addValueControlWidget(
7878
_values?: unknown,
7979
widgetName?: string,
8080
inputData?: InputSpec
81-
): IWidget {
81+
): IComboWidget {
8282
let name = inputData?.[1]?.control_after_generate
8383
if (typeof name !== 'string') {
8484
name = widgetName
@@ -102,7 +102,7 @@ export function addValueControlWidgets(
102102
defaultValue?: string,
103103
options?: Record<string, any>,
104104
inputData?: InputSpec
105-
): IWidget[] {
105+
): [IComboWidget, ...IStringWidget[]] {
106106
if (!defaultValue) defaultValue = 'randomize'
107107
if (!options) options = {}
108108

@@ -118,7 +118,6 @@ export function addValueControlWidgets(
118118
return name
119119
}
120120

121-
const widgets: IWidget[] = []
122121
const valueControl = node.addWidget(
123122
'combo',
124123
getName('control_after_generate', 'controlAfterGenerateName'),
@@ -135,12 +134,12 @@ export function addValueControlWidgets(
135134
// @ts-ignore index with symbol
136135
valueControl[IS_CONTROL_WIDGET] = true
137136
updateControlWidgetLabel(valueControl)
138-
widgets.push(valueControl)
137+
const widgets: [IComboWidget, ...IStringWidget[]] = [valueControl]
139138

140139
const isCombo = targetWidget.type === 'combo'
141140
let comboFilter: IStringWidget
142141
if (isCombo && valueControl.options.values) {
143-
// @ts-ignore Combo widget values may be a dictionary or legacy function type
142+
// @ts-expect-error Combo widget values may be a dictionary or legacy function type
144143
valueControl.options.values.push('increment-wrap')
145144
}
146145
if (isCombo && options.addFilterList !== false) {
@@ -184,7 +183,7 @@ export function addValueControlWidgets(
184183
const lower = filter.toLocaleLowerCase()
185184
check = (item: string) => item.toLocaleLowerCase().includes(lower)
186185
}
187-
// @ts-ignore Combo widget values may be a dictionary or legacy function type
186+
// @ts-expect-error Combo widget values may be a dictionary or legacy function type
188187
values = values.filter((item: string) => check(item))
189188
if (!values.length && targetWidget.options.values?.length) {
190189
console.warn(
@@ -211,17 +210,17 @@ export function addValueControlWidgets(
211210
current_index -= 1
212211
break
213212
case 'randomize':
214-
// @ts-ignore Combo widget values may be a dictionary or legacy function type
213+
// @ts-expect-error Combo widget values may be a dictionary or legacy function type
215214
current_index = Math.floor(Math.random() * current_length)
216215
break
217216
default:
218217
break
219218
}
220219
current_index = Math.max(0, current_index)
221-
// @ts-ignore Combo widget values may be a dictionary or legacy function type
220+
// @ts-expect-error Combo widget values may be a dictionary or legacy function type
222221
current_index = Math.min(current_length - 1, current_index)
223222
if (current_index >= 0) {
224-
// @ts-ignore Combo widget values may be a dictionary or legacy function type
223+
// @ts-expect-error Combo widget values may be a dictionary or legacy function type
225224
let value = values[current_index]
226225
targetWidget.value = value
227226
targetWidget.callback?.(value)

0 commit comments

Comments
 (0)