|
| 1 | +<script setup lang="ts"> |
| 2 | +import { refDebounced, watchDebounced } from '@vueuse/core' |
| 3 | +import { |
| 4 | + computed, |
| 5 | + customRef, |
| 6 | + onBeforeUnmount, |
| 7 | + onMounted, |
| 8 | + ref, |
| 9 | + triggerRef |
| 10 | +} from 'vue' |
| 11 | +
|
| 12 | +import SearchBox from '@/components/common/SearchBox.vue' |
| 13 | +import SubgraphNodeWidget from '@/core/graph/subgraph/SubgraphNodeWidget.vue' |
| 14 | +import { |
| 15 | + type WidgetItem, |
| 16 | + demoteWidget, |
| 17 | + isRecommendedWidget, |
| 18 | + matchesPropertyItem, |
| 19 | + matchesWidgetItem, |
| 20 | + promoteWidget, |
| 21 | + widgetItemToProperty |
| 22 | +} from '@/core/graph/subgraph/proxyWidgetUtils' |
| 23 | +import { |
| 24 | + type ProxyWidgetsProperty, |
| 25 | + parseProxyWidgets |
| 26 | +} from '@/core/schemas/proxyWidget' |
| 27 | +import type { LGraphNode } from '@/lib/litegraph/src/litegraph' |
| 28 | +import { SubgraphNode } from '@/lib/litegraph/src/subgraph/SubgraphNode' |
| 29 | +import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' |
| 30 | +import { useCanvasStore } from '@/renderer/core/canvas/canvasStore' |
| 31 | +import { DraggableList } from '@/scripts/ui/draggableList' |
| 32 | +import { useLitegraphService } from '@/services/litegraphService' |
| 33 | +import { useDialogStore } from '@/stores/dialogStore' |
| 34 | +
|
| 35 | +const canvasStore = useCanvasStore() |
| 36 | +
|
| 37 | +const draggableList = ref<DraggableList | undefined>(undefined) |
| 38 | +const draggableItems = ref() |
| 39 | +const searchQuery = ref<string>('') |
| 40 | +const debouncedQuery = refDebounced(searchQuery, 200) |
| 41 | +const proxyWidgets = customRef<ProxyWidgetsProperty>((track, trigger) => ({ |
| 42 | + get() { |
| 43 | + track() |
| 44 | + const node = activeNode.value |
| 45 | + if (!node) return [] |
| 46 | + return parseProxyWidgets(node.properties.proxyWidgets) |
| 47 | + }, |
| 48 | + set(value?: ProxyWidgetsProperty) { |
| 49 | + trigger() |
| 50 | + const node = activeNode.value |
| 51 | + if (!value) return |
| 52 | + if (!node) { |
| 53 | + console.error('Attempted to toggle widgets with no node selected') |
| 54 | + return |
| 55 | + } |
| 56 | + node.properties.proxyWidgets = value |
| 57 | + } |
| 58 | +})) |
| 59 | +
|
| 60 | +const activeNode = computed(() => { |
| 61 | + const node = canvasStore.selectedItems[0] |
| 62 | + if (node instanceof SubgraphNode) return node |
| 63 | + useDialogStore().closeDialog() |
| 64 | + return undefined |
| 65 | +}) |
| 66 | +
|
| 67 | +const activeWidgets = computed<WidgetItem[]>({ |
| 68 | + get() { |
| 69 | + const node = activeNode.value |
| 70 | + if (!node) return [] |
| 71 | + return proxyWidgets.value.flatMap(([id, name]: [string, string]) => { |
| 72 | + const wNode = node.subgraph._nodes_by_id[id] |
| 73 | + if (!wNode?.widgets) return [] |
| 74 | + const w = wNode.widgets.find((w) => w.name === name) |
| 75 | + if (!w) return [] |
| 76 | + return [[wNode, w]] |
| 77 | + }) |
| 78 | + }, |
| 79 | + set(value: WidgetItem[]) { |
| 80 | + const node = activeNode.value |
| 81 | + if (!node) { |
| 82 | + console.error('Attempted to toggle widgets with no node selected') |
| 83 | + return |
| 84 | + } |
| 85 | + //map back to id/name |
| 86 | + const widgets: ProxyWidgetsProperty = value.map(widgetItemToProperty) |
| 87 | + proxyWidgets.value = widgets |
| 88 | + } |
| 89 | +}) |
| 90 | +
|
| 91 | +const interiorWidgets = computed<WidgetItem[]>(() => { |
| 92 | + const node = activeNode.value |
| 93 | + if (!node) return [] |
| 94 | + const { updatePreviews } = useLitegraphService() |
| 95 | + const interiorNodes = node.subgraph.nodes |
| 96 | + for (const node of interiorNodes) { |
| 97 | + node.updateComputedDisabled() |
| 98 | + updatePreviews(node) |
| 99 | + } |
| 100 | + return interiorNodes |
| 101 | + .flatMap(nodeWidgets) |
| 102 | + .filter(([_, w]: WidgetItem) => !w.computedDisabled) |
| 103 | +}) |
| 104 | +
|
| 105 | +const candidateWidgets = computed<WidgetItem[]>(() => { |
| 106 | + const node = activeNode.value |
| 107 | + if (!node) return [] |
| 108 | + const widgets = proxyWidgets.value |
| 109 | + return interiorWidgets.value.filter( |
| 110 | + (widgetItem: WidgetItem) => !widgets.some(matchesPropertyItem(widgetItem)) |
| 111 | + ) |
| 112 | +}) |
| 113 | +const filteredCandidates = computed<WidgetItem[]>(() => { |
| 114 | + const query = debouncedQuery.value.toLowerCase() |
| 115 | + if (!query) return candidateWidgets.value |
| 116 | + return candidateWidgets.value.filter( |
| 117 | + ([n, w]: WidgetItem) => |
| 118 | + n.title.toLowerCase().includes(query) || |
| 119 | + w.name.toLowerCase().includes(query) |
| 120 | + ) |
| 121 | +}) |
| 122 | +
|
| 123 | +const recommendedWidgets = computed(() => { |
| 124 | + const node = activeNode.value |
| 125 | + if (!node) return [] //Not reachable |
| 126 | + return filteredCandidates.value.filter(isRecommendedWidget) |
| 127 | +}) |
| 128 | +
|
| 129 | +const filteredActive = computed<WidgetItem[]>(() => { |
| 130 | + const query = debouncedQuery.value.toLowerCase() |
| 131 | + if (!query) return activeWidgets.value |
| 132 | + return activeWidgets.value.filter( |
| 133 | + ([n, w]: WidgetItem) => |
| 134 | + n.title.toLowerCase().includes(query) || |
| 135 | + w.name.toLowerCase().includes(query) |
| 136 | + ) |
| 137 | +}) |
| 138 | +
|
| 139 | +function toKey(item: WidgetItem) { |
| 140 | + return `${item[0].id}: ${item[1].name}` |
| 141 | +} |
| 142 | +function nodeWidgets(n: LGraphNode): WidgetItem[] { |
| 143 | + if (!n.widgets) return [] |
| 144 | + return n.widgets.map((w: IBaseWidget) => [n, w]) |
| 145 | +} |
| 146 | +function demote([node, widget]: WidgetItem) { |
| 147 | + const subgraphNode = activeNode.value |
| 148 | + if (!subgraphNode) return [] |
| 149 | + demoteWidget(node, widget, [subgraphNode]) |
| 150 | + triggerRef(proxyWidgets) |
| 151 | +} |
| 152 | +function promote([node, widget]: WidgetItem) { |
| 153 | + const subgraphNode = activeNode.value |
| 154 | + if (!subgraphNode) return [] |
| 155 | + promoteWidget(node, widget, [subgraphNode]) |
| 156 | + triggerRef(proxyWidgets) |
| 157 | +} |
| 158 | +function showAll() { |
| 159 | + const node = activeNode.value |
| 160 | + if (!node) return //Not reachable |
| 161 | + const widgets = proxyWidgets.value |
| 162 | + const toAdd: ProxyWidgetsProperty = |
| 163 | + filteredCandidates.value.map(widgetItemToProperty) |
| 164 | + widgets.push(...toAdd) |
| 165 | + proxyWidgets.value = widgets |
| 166 | +} |
| 167 | +function hideAll() { |
| 168 | + const node = activeNode.value |
| 169 | + if (!node) return //Not reachable |
| 170 | + //Not great from a nesting perspective, but path is cold |
| 171 | + //and it cleans up potential error states |
| 172 | + proxyWidgets.value = proxyWidgets.value.filter( |
| 173 | + (widgetItem) => !filteredActive.value.some(matchesWidgetItem(widgetItem)) |
| 174 | + ) |
| 175 | +} |
| 176 | +function showRecommended() { |
| 177 | + const node = activeNode.value |
| 178 | + if (!node) return //Not reachable |
| 179 | + const widgets = proxyWidgets.value |
| 180 | + const toAdd: ProxyWidgetsProperty = |
| 181 | + recommendedWidgets.value.map(widgetItemToProperty) |
| 182 | + //TODO: Add sort step here |
| 183 | + //Input should always be before output by default |
| 184 | + widgets.push(...toAdd) |
| 185 | + proxyWidgets.value = widgets |
| 186 | +} |
| 187 | +
|
| 188 | +function setDraggableState() { |
| 189 | + draggableList.value?.dispose() |
| 190 | + if (debouncedQuery.value || !draggableItems.value?.children?.length) return |
| 191 | + draggableList.value = new DraggableList( |
| 192 | + draggableItems.value, |
| 193 | + '.draggable-item' |
| 194 | + ) |
| 195 | + //Original implementation plays really poorly with vue, |
| 196 | + //It has been modified to not add/remove elements |
| 197 | + draggableList.value.applyNewItemsOrder = function () { |
| 198 | + const reorderedItems = [] |
| 199 | +
|
| 200 | + let oldPosition = -1 |
| 201 | + this.getAllItems().forEach((item, index) => { |
| 202 | + if (item === this.draggableItem) { |
| 203 | + oldPosition = index |
| 204 | + return |
| 205 | + } |
| 206 | + if (!this.isItemToggled(item)) { |
| 207 | + reorderedItems[index] = item |
| 208 | + return |
| 209 | + } |
| 210 | + const newIndex = this.isItemAbove(item) ? index + 1 : index - 1 |
| 211 | + reorderedItems[newIndex] = item |
| 212 | + }) |
| 213 | +
|
| 214 | + for (let index = 0; index < this.getAllItems().length; index++) { |
| 215 | + const item = reorderedItems[index] |
| 216 | + if (typeof item === 'undefined') { |
| 217 | + reorderedItems[index] = this.draggableItem |
| 218 | + } |
| 219 | + } |
| 220 | + const newPosition = reorderedItems.indexOf(this.draggableItem) |
| 221 | + const aw = activeWidgets.value |
| 222 | + const [w] = aw.splice(oldPosition, 1) |
| 223 | + aw.splice(newPosition, 0, w) |
| 224 | + activeWidgets.value = aw |
| 225 | + } |
| 226 | +} |
| 227 | +watchDebounced( |
| 228 | + filteredActive, |
| 229 | + () => { |
| 230 | + setDraggableState() |
| 231 | + }, |
| 232 | + { debounce: 100 } |
| 233 | +) |
| 234 | +onMounted(() => { |
| 235 | + setDraggableState() |
| 236 | +}) |
| 237 | +onBeforeUnmount(() => { |
| 238 | + draggableList.value?.dispose() |
| 239 | +}) |
| 240 | +</script> |
| 241 | +<template> |
| 242 | + <SearchBox |
| 243 | + v-model:model-value="searchQuery" |
| 244 | + class="p-2" |
| 245 | + :placeholder="$t('g.search') + '...'" |
| 246 | + /> |
| 247 | + <div |
| 248 | + v-if="filteredActive.length" |
| 249 | + class="pt-1 pb-4 border-b-1 border-sand-100 dark-theme:border-charcoal-600" |
| 250 | + > |
| 251 | + <div class="flex py-0 px-4 justify-between"> |
| 252 | + <div class="text-slate-100 text-[9px] font-semibold uppercase"> |
| 253 | + {{ $t('subgraphStore.shown') }} |
| 254 | + </div> |
| 255 | + <a |
| 256 | + class="cursor-pointer text-right text-blue-100 text-[11px] font-normal" |
| 257 | + @click.stop="hideAll" |
| 258 | + > |
| 259 | + {{ $t('subgraphStore.hideAll') }}</a |
| 260 | + > |
| 261 | + </div> |
| 262 | + <div ref="draggableItems"> |
| 263 | + <div |
| 264 | + v-for="[node, widget] in filteredActive" |
| 265 | + :key="toKey([node, widget])" |
| 266 | + class="w-full draggable-item" |
| 267 | + style="" |
| 268 | + > |
| 269 | + <SubgraphNodeWidget |
| 270 | + :node-title="node.title" |
| 271 | + :widget-name="widget.name" |
| 272 | + :is-shown="true" |
| 273 | + :is-draggable="!debouncedQuery" |
| 274 | + @toggle-visibility="demote([node, widget])" |
| 275 | + /> |
| 276 | + </div> |
| 277 | + </div> |
| 278 | + </div> |
| 279 | + <div v-if="filteredCandidates.length" class="pt-1 pb-4"> |
| 280 | + <div class="flex py-0 px-4 justify-between"> |
| 281 | + <div class="text-slate-100 text-[9px] font-semibold uppercase"> |
| 282 | + {{ $t('subgraphStore.hidden') }} |
| 283 | + </div> |
| 284 | + <a |
| 285 | + class="cursor-pointer text-right text-blue-100 text-[11px] font-normal" |
| 286 | + @click.stop="showAll" |
| 287 | + > |
| 288 | + {{ $t('subgraphStore.showAll') }}</a |
| 289 | + > |
| 290 | + </div> |
| 291 | + <div |
| 292 | + v-for="[node, widget] in filteredCandidates" |
| 293 | + :key="toKey([node, widget])" |
| 294 | + class="w-full" |
| 295 | + > |
| 296 | + <SubgraphNodeWidget |
| 297 | + :node-title="node.title" |
| 298 | + :widget-name="widget.name" |
| 299 | + @toggle-visibility="promote([node, widget])" |
| 300 | + /> |
| 301 | + </div> |
| 302 | + </div> |
| 303 | + <div |
| 304 | + v-if="recommendedWidgets.length" |
| 305 | + class="justify-center flex py-4 border-t-1 border-sand-100 dark-theme:border-charcoal-600" |
| 306 | + > |
| 307 | + <Button |
| 308 | + size="small" |
| 309 | + class="rounded border-none px-3 py-0.5" |
| 310 | + @click.stop="showRecommended" |
| 311 | + > |
| 312 | + {{ $t('subgraphStore.showRecommended') }} |
| 313 | + </Button> |
| 314 | + </div> |
| 315 | +</template> |
0 commit comments