Files
ComfyUI_frontend/src/stores/widgetStore.ts
2025-04-06 11:50:21 -04:00

37 lines
1.1 KiB
TypeScript

import { defineStore } from 'pinia'
import { computed, ref } from 'vue'
import type { InputSpec as InputSpecV2 } from '@/schemas/nodeDef/nodeDefSchemaV2'
import {
type InputSpec as InputSpecV1,
getInputSpecType
} from '@/schemas/nodeDefSchema'
import { ComfyWidgetConstructor, ComfyWidgets } from '@/scripts/widgets'
export const useWidgetStore = defineStore('widget', () => {
const coreWidgets = ComfyWidgets
const customWidgets = ref<Map<string, ComfyWidgetConstructor>>(new Map())
const widgets = computed<Map<string, ComfyWidgetConstructor>>(
() => new Map([...customWidgets.value, ...Object.entries(coreWidgets)])
)
function inputIsWidget(spec: InputSpecV2 | InputSpecV1) {
const type = Array.isArray(spec) ? getInputSpecType(spec) : spec.type
return widgets.value.has(type)
}
function registerCustomWidgets(
newWidgets: Record<string, ComfyWidgetConstructor>
) {
for (const [type, widget] of Object.entries(newWidgets)) {
customWidgets.value.set(type, widget)
}
}
return {
widgets,
inputIsWidget,
registerCustomWidgets
}
})