Line data Source code
1 : <script setup lang="ts">
2 : import { storeToRefs } from 'pinia'
3 : import { computed, nextTick, ref, shallowRef, useTemplateRef, watch } from 'vue'
4 : import { useI18n } from 'vue-i18n'
5 :
6 : import { promotedInputWidgets } from '@/core/graph/subgraph/promotedInputWidget'
7 : import {
8 : isWidgetPromotedOnSubgraphNode,
9 : reorderSubgraphInputsByWidgetOrder
10 : } from '@/core/graph/subgraph/promotionUtils'
11 : import type { SubgraphNode } from '@/lib/litegraph/src/subgraph/SubgraphNode'
12 : import type { IBaseWidget } from '@comfyorg/graph-model/widgets'
13 : import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
14 : import CollapseToggleButton from '@comfyorg/design-system/accordion/CollapseToggleButton'
15 : import { useWidgetValueStore } from '@/stores/widgetValueStore'
16 : import { usePropertiesPanelStore } from '@/renderer/extensions/propertiesPanel/propertiesPanelStore'
17 : import {
18 : deriveWidgetVisibility,
19 : isWidgetVisibleOnSurface
20 : } from '@/types/widgetVisibility'
21 :
22 : import { searchWidgets } from './widgetSearch'
23 : import type { NodeWidgetsList } from './widgetSections'
24 : import PanelSearchHeader from './PanelSearchHeader.vue'
25 : import SectionWidgets from './SectionWidgets.vue'
26 :
27 : const { node } = defineProps<{
28 : node: SubgraphNode
29 : }>()
30 :
31 2355 : const { t } = useI18n()
32 27 : const canvasStore = useCanvasStore()
33 27 : const propertiesPanelStore = usePropertiesPanelStore()
34 27 : const widgetValueStore = useWidgetValueStore()
35 27 : const { focusedSection } = storeToRefs(propertiesPanelStore)
36 27 : const searchQuery = ref('')
37 :
38 27 : const advancedInputsCollapsed = ref(true)
39 27 : const firstSectionCollapsed = ref(false)
40 27 : const isAllCollapsed = computed({
41 : get() {
42 : const hasAdvanced = advancedInputsWidgets.value.length > 0
43 28 : return hasAdvanced
44 : ? firstSectionCollapsed.value && advancedInputsCollapsed.value
45 : : firstSectionCollapsed.value
46 : },
47 : set(collapse: boolean) {
48 0 : firstSectionCollapsed.value = collapse
49 0 : advancedInputsCollapsed.value = collapse
50 : }
51 : })
52 27 : const advancedInputsSectionRef = useTemplateRef('advancedInputsSectionRef')
53 :
54 27 : watch(
55 : focusedSection,
56 : async (section) => {
57 27 : if (section === 'advanced-inputs') {
58 0 : advancedInputsCollapsed.value = false
59 0 : propertiesPanelStore.clearFocusedSection()
60 :
61 0 : await nextTick()
62 :
63 0 : await new Promise((resolve) => setTimeout(resolve, 300))
64 :
65 : const sectionComponent = advancedInputsSectionRef.value
66 0 : const sectionElement = sectionComponent?.rootElement
67 0 : if (sectionElement) {
68 0 : sectionElement.scrollIntoView({ behavior: 'smooth', block: 'start' })
69 : }
70 : }
71 : },
72 : { immediate: true }
73 : )
74 :
75 31 : function isPromotedWidgetVisibleInPanel(widget: IBaseWidget): boolean {
76 31 : const visibility = widget.widgetId
77 : ? widgetValueStore.getWidgetVisibility(widget.widgetId)
78 : : undefined
79 31 : return (
80 : !visibility ||
81 : isWidgetVisibleOnSurface(visibility, 'panel', { showAdvanced: true })
82 : )
83 : }
84 :
85 27 : const widgetsList = computed((): NodeWidgetsList => {
86 28 : return promotedInputWidgets(node)
87 : .filter(isPromotedWidgetVisibleInPanel)
88 : .map((widget) => ({ node, widget }))
89 : })
90 :
91 27 : const advancedInputsWidgets = computed((): NodeWidgetsList => {
92 : const interiorNodes = node.subgraph.nodes
93 :
94 : const allInteriorWidgets = interiorNodes.flatMap((interiorNode) => {
95 38 : const { widgets = [] } = interiorNode
96 38 : return widgets
97 : .filter((widget) =>
98 : isWidgetVisibleOnSurface(
99 : widget.visibility ?? deriveWidgetVisibility(widget),
100 : 'panel',
101 : { showAdvanced: true }
102 : )
103 : )
104 : .map((widget) => ({ node: interiorNode, widget }))
105 : })
106 :
107 28 : return allInteriorWidgets.filter(
108 : ({ node: interiorNode, widget }) =>
109 : !isWidgetPromotedOnSubgraphNode(node, {
110 : sourceNodeId: interiorNode.id,
111 : sourceWidgetName: widget.name
112 : })
113 : )
114 : })
115 :
116 27 : const searchedWidgetsList = shallowRef<NodeWidgetsList>(widgetsList.value)
117 27 : const isSearching = ref(false)
118 :
119 28 : async function searcher(query: string) {
120 28 : isSearching.value = query.trim() !== ''
121 28 : searchedWidgetsList.value = searchWidgets(widgetsList.value, query)
122 : }
123 :
124 1 : function handleReorder({
125 : fromIndex,
126 : toIndex
127 : }: {
128 : fromIndex: number
129 : toIndex: number
130 : }) {
131 1 : const widgets = searchedWidgetsList.value.map((row) => row.widget)
132 1 : const [moved] = widgets.splice(fromIndex, 1)
133 1 : if (!moved) return
134 1 : widgets.splice(toIndex, 0, moved)
135 :
136 1 : reorderSubgraphInputsByWidgetOrder(node, widgets)
137 1 : canvasStore.canvas?.setDirty(true, true)
138 : }
139 :
140 27 : const label = computed(() => {
141 28 : return searchedWidgetsList.value.length !== 0
142 : ? t('rightSidePanel.inputs')
143 : : t('rightSidePanel.inputsNone')
144 : })
145 27 : </script>
146 :
147 : <template>
148 : <PanelSearchHeader v-model="searchQuery" :searcher :update-key="widgetsList">
149 : <CollapseToggleButton
150 : v-model="isAllCollapsed"
151 : :expand-label="$t('g.expandAll')"
152 : :collapse-label="$t('g.collapseAll')"
153 : :show="!isSearching && advancedInputsWidgets.length > 0"
154 : />
155 : </PanelSearchHeader>
156 : <SectionWidgets
157 : :collapse="firstSectionCollapsed && !isSearching"
158 : :node
159 : :label
160 : :host="node"
161 : :widgets="searchedWidgetsList"
162 : :is-draggable="!isSearching"
163 : :enable-empty-state="isSearching"
164 : :tooltip="
165 : isSearching || searchedWidgetsList.length
166 : ? ''
167 : : t('rightSidePanel.inputsNoneTooltip')
168 : "
169 : class="border-b"
170 : @update:collapse="(v) => (firstSectionCollapsed = v)"
171 : @reorder="handleReorder"
172 : >
173 : <template #empty>
174 : <div class="px-4 pt-5 pb-15 text-center text-sm text-muted-foreground">
175 : {{ t('rightSidePanel.noneSearchDesc') }}
176 : </div>
177 : </template>
178 : </SectionWidgets>
179 : <SectionWidgets
180 : v-if="advancedInputsWidgets.length > 0"
181 : ref="advancedInputsSectionRef"
182 : v-model:collapse="advancedInputsCollapsed"
183 : :label="t('rightSidePanel.advancedInputs')"
184 : :host="node"
185 : :widgets="advancedInputsWidgets"
186 : show-node-name
187 : class="border-b"
188 : />
189 : </template>
|