mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-03-06 05:30:08 +00:00
Add Vue Combo (dropdown) widget (#4113)
This commit is contained in:
40
src/components/graph/widgets/DropdownComboWidget.vue
Normal file
40
src/components/graph/widgets/DropdownComboWidget.vue
Normal file
@@ -0,0 +1,40 @@
|
||||
<template>
|
||||
<div class="px-2">
|
||||
<Select
|
||||
v-model="selectedValue"
|
||||
:options="computedOptions"
|
||||
:placeholder="placeholder"
|
||||
class="w-full rounded-lg"
|
||||
:disabled="isLoading"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import Select from 'primevue/select'
|
||||
import { computed } from 'vue'
|
||||
|
||||
import type { ComboInputSpec } from '@/schemas/nodeDef/nodeDefSchemaV2'
|
||||
import type { ComponentWidget } from '@/scripts/domWidget'
|
||||
|
||||
const selectedValue = defineModel<string>()
|
||||
const { widget } = defineProps<{
|
||||
widget?: ComponentWidget<string>
|
||||
}>()
|
||||
|
||||
const inputSpec = (widget?.inputSpec ?? {}) as ComboInputSpec
|
||||
const placeholder = 'Select option'
|
||||
const isLoading = computed(() => selectedValue.value === 'Loading...')
|
||||
|
||||
// For remote widgets, we need to dynamically get options
|
||||
const computedOptions = computed(() => {
|
||||
if (inputSpec.remote) {
|
||||
// For remote widgets, the options may be dynamically updated
|
||||
// The useRemoteWidget will update the inputSpec.options
|
||||
return inputSpec.options ?? []
|
||||
}
|
||||
return inputSpec.options ?? []
|
||||
})
|
||||
|
||||
// Tooltip support is available via inputSpec.tooltip if needed in the future
|
||||
</script>
|
||||
@@ -1,5 +1,4 @@
|
||||
import type { LGraphNode } from '@comfyorg/litegraph'
|
||||
import type { IComboWidget } from '@comfyorg/litegraph/dist/types/widgets'
|
||||
import { ref } from 'vue'
|
||||
|
||||
import MultiSelectWidget from '@/components/graph/widgets/MultiSelectWidget.vue'
|
||||
@@ -15,14 +14,9 @@ import {
|
||||
} from '@/scripts/domWidget'
|
||||
import type { ComfyWidgetConstructorV2 } from '@/scripts/widgetTypes'
|
||||
|
||||
import { useRemoteWidget } from './useRemoteWidget'
|
||||
import { useDropdownComboWidget } from './useDropdownComboWidget'
|
||||
|
||||
const getDefaultValue = (inputSpec: ComboInputSpec) => {
|
||||
if (inputSpec.default) return inputSpec.default
|
||||
if (inputSpec.options?.length) return inputSpec.options[0]
|
||||
if (inputSpec.remote) return 'Loading...'
|
||||
return undefined
|
||||
}
|
||||
// Default value logic is now handled in useDropdownComboWidget
|
||||
|
||||
const addMultiSelectWidget = (node: LGraphNode, inputSpec: ComboInputSpec) => {
|
||||
const widgetValue = ref<string[]>([])
|
||||
@@ -45,53 +39,9 @@ const addMultiSelectWidget = (node: LGraphNode, inputSpec: ComboInputSpec) => {
|
||||
}
|
||||
|
||||
const addComboWidget = (node: LGraphNode, inputSpec: ComboInputSpec) => {
|
||||
const defaultValue = getDefaultValue(inputSpec)
|
||||
const comboOptions = inputSpec.options ?? []
|
||||
const widget = node.addWidget(
|
||||
'combo',
|
||||
inputSpec.name,
|
||||
defaultValue,
|
||||
() => {},
|
||||
{
|
||||
values: comboOptions
|
||||
}
|
||||
) as IComboWidget
|
||||
|
||||
if (inputSpec.remote) {
|
||||
const remoteWidget = useRemoteWidget({
|
||||
remoteConfig: inputSpec.remote,
|
||||
defaultValue,
|
||||
node,
|
||||
widget
|
||||
})
|
||||
if (inputSpec.remote.refresh_button) remoteWidget.addRefreshButton()
|
||||
|
||||
const origOptions = widget.options
|
||||
widget.options = new Proxy(origOptions, {
|
||||
get(target, prop) {
|
||||
// Assertion: Proxy handler passthrough
|
||||
return prop !== 'values'
|
||||
? target[prop as keyof typeof target]
|
||||
: remoteWidget.getValue()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (inputSpec.control_after_generate) {
|
||||
// TODO: Re-implement control widget functionality without circular dependency
|
||||
console.warn(
|
||||
'Control widget functionality temporarily disabled for combo widgets due to circular dependency'
|
||||
)
|
||||
// widget.linkedWidgets = addValueControlWidgets(
|
||||
// node,
|
||||
// widget,
|
||||
// undefined,
|
||||
// undefined,
|
||||
// transformInputSpecV2ToV1(inputSpec)
|
||||
// )
|
||||
}
|
||||
|
||||
return widget
|
||||
// Use the new dropdown combo widget for single-selection combo widgets
|
||||
const dropdownWidget = useDropdownComboWidget()
|
||||
return dropdownWidget(node, inputSpec)
|
||||
}
|
||||
|
||||
export const useComboWidget = () => {
|
||||
|
||||
98
src/composables/widgets/useDropdownComboWidget.ts
Normal file
98
src/composables/widgets/useDropdownComboWidget.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
import type { LGraphNode } from '@comfyorg/litegraph'
|
||||
import { ref } from 'vue'
|
||||
|
||||
import DropdownComboWidget from '@/components/graph/widgets/DropdownComboWidget.vue'
|
||||
import { transformInputSpecV2ToV1 } from '@/schemas/nodeDef/migration'
|
||||
import type {
|
||||
ComboInputSpec,
|
||||
InputSpec
|
||||
} from '@/schemas/nodeDef/nodeDefSchemaV2'
|
||||
import { ComponentWidgetImpl, addWidget } from '@/scripts/domWidget'
|
||||
import type { ComfyWidgetConstructorV2 } from '@/scripts/widgetTypes'
|
||||
import { addValueControlWidgets } from '@/scripts/widgets'
|
||||
|
||||
import { useRemoteWidget } from './useRemoteWidget'
|
||||
|
||||
const PADDING = 8
|
||||
|
||||
const getDefaultValue = (inputSpec: ComboInputSpec) => {
|
||||
if (inputSpec.default) return inputSpec.default
|
||||
if (inputSpec.options?.length) return inputSpec.options[0]
|
||||
if (inputSpec.remote) return 'Loading...'
|
||||
return ''
|
||||
}
|
||||
|
||||
export const useDropdownComboWidget = (
|
||||
options: { defaultValue?: string } = {}
|
||||
) => {
|
||||
const widgetConstructor: ComfyWidgetConstructorV2 = (
|
||||
node: LGraphNode,
|
||||
inputSpec: InputSpec
|
||||
) => {
|
||||
// Type assertion to ComboInputSpec since this is specifically for combo widgets
|
||||
const comboInputSpec = inputSpec as ComboInputSpec
|
||||
|
||||
// Initialize widget value
|
||||
const defaultValue = options.defaultValue ?? getDefaultValue(comboInputSpec)
|
||||
const widgetValue = ref<string>(defaultValue)
|
||||
|
||||
// Create the widget instance
|
||||
const widget = new ComponentWidgetImpl<string>({
|
||||
node,
|
||||
name: inputSpec.name,
|
||||
component: DropdownComboWidget,
|
||||
inputSpec,
|
||||
options: {
|
||||
// Required: getter for widget value
|
||||
getValue: () => widgetValue.value,
|
||||
|
||||
// Required: setter for widget value
|
||||
setValue: (value: string) => {
|
||||
widgetValue.value = value
|
||||
},
|
||||
|
||||
// Optional: minimum height for the widget (dropdown needs some height)
|
||||
getMinHeight: () => 42 + PADDING,
|
||||
|
||||
// Optional: whether to serialize this widget's value
|
||||
serialize: true
|
||||
}
|
||||
})
|
||||
|
||||
// Handle remote widget functionality
|
||||
if (comboInputSpec.remote) {
|
||||
const remoteWidget = useRemoteWidget({
|
||||
remoteConfig: comboInputSpec.remote,
|
||||
defaultValue,
|
||||
node,
|
||||
widget: widget as any // Cast to be compatible with the remote widget interface
|
||||
})
|
||||
if (comboInputSpec.remote.refresh_button) {
|
||||
remoteWidget.addRefreshButton()
|
||||
}
|
||||
|
||||
// Update the widget to use remote data
|
||||
// Note: The remote widget will handle updating the options through the inputSpec
|
||||
}
|
||||
|
||||
// Handle control_after_generate widgets
|
||||
if (comboInputSpec.control_after_generate) {
|
||||
const linkedWidgets = addValueControlWidgets(
|
||||
node,
|
||||
widget as any, // Cast to be compatible with legacy widget interface
|
||||
undefined,
|
||||
undefined,
|
||||
transformInputSpecV2ToV1(comboInputSpec)
|
||||
)
|
||||
// Store reference to linked widgets (mimicking original behavior)
|
||||
;(widget as any).linkedWidgets = linkedWidgets
|
||||
}
|
||||
|
||||
// Register the widget with the node
|
||||
addWidget(node, widget as any)
|
||||
|
||||
return widget
|
||||
}
|
||||
|
||||
return widgetConstructor
|
||||
}
|
||||
Reference in New Issue
Block a user