LCOV - code coverage report
Current view: top level - src/renderer/extensions/linearMode - OutputHistory.vue Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 71.2 % 125 89
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : <script setup lang="ts">
       2              : import {
       3              :   useEventListener,
       4              :   useInfiniteScroll,
       5              :   useResizeObserver
       6              : } from '@vueuse/core'
       7              : import { storeToRefs } from 'pinia'
       8              : import type { ComponentPublicInstance } from 'vue'
       9              : import {
      10              :   computed,
      11              :   nextTick,
      12              :   ref,
      13              :   toValue,
      14              :   useTemplateRef,
      15              :   watch,
      16              :   watchEffect
      17              : } from 'vue'
      18              : 
      19              : import { CanvasPointer } from '@/lib/litegraph/src/CanvasPointer'
      20              : import OutputHistoryActiveQueueItem from '@/renderer/extensions/linearMode/OutputHistoryActiveQueueItem.vue'
      21              : import OutputHistoryItem from '@/renderer/extensions/linearMode/OutputHistoryItem.vue'
      22              : import { useLinearOutputStore } from '@/renderer/extensions/linearMode/linearOutputStore'
      23              : import type {
      24              :   OutputSelection,
      25              :   SelectionValue
      26              : } from '@/renderer/extensions/linearMode/linearModeTypes'
      27              : import OutputPreviewItem from '@/renderer/extensions/linearMode/OutputPreviewItem.vue'
      28              : import { useOutputHistory } from '@/renderer/extensions/linearMode/useOutputHistory'
      29              : import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore'
      30              : import { useAppModeStore } from '@/stores/appModeStore'
      31              : import { useQueueStore } from '@/stores/queueStore'
      32              : import { cn } from '@comfyorg/tailwind-utils'
      33              : 
      34         2355 : const { outputs, allOutputs, selectFirstHistory, mayBeActiveWorkflowPending } =
      35              :   useOutputHistory()
      36           80 : const { hasOutputs } = storeToRefs(useAppModeStore())
      37           80 : const queueStore = useQueueStore()
      38           80 : const store = useLinearOutputStore()
      39           80 : const workflowStore = useWorkflowStore()
      40              : 
      41           80 : const emit = defineEmits<{
      42              :   updateSelection: [selection: OutputSelection]
      43              : }>()
      44              : 
      45           80 : const queueCount = computed(
      46              :   () => queueStore.runningTasks.length + queueStore.pendingTasks.length
      47              : )
      48              : 
      49           80 : const itemClass = cn(
      50              :   'shrink-0 cursor-pointer rounded-lg border-2 border-transparent p-1 outline-none',
      51              :   'relative data-[state=checked]:border-foreground'
      52              : )
      53              : 
      54           80 : const hasActiveContent = computed(
      55              :   () => store.activeWorkflowInProgressItems.length > 0
      56              : )
      57              : 
      58           80 : const visibleHistory = computed(() =>
      59              :   toValue(outputs.items).filter((a) => allOutputs(a).length > 0)
      60              : )
      61              : 
      62           80 : const selectableItems = computed(() => {
      63           51 :   const items: SelectionValue[] = []
      64           51 :   if (mayBeActiveWorkflowPending.value) {
      65            0 :     items.push({ id: 'slot:pending', kind: 'inProgress', itemId: 'pending' })
      66              :   }
      67           51 :   for (const item of store.activeWorkflowInProgressItems) {
      68          247 :     items.push({
      69              :       id: `slot:${item.id}`,
      70              :       kind: 'inProgress',
      71              :       itemId: item.id
      72              :     })
      73              :   }
      74           51 :   for (const asset of toValue(outputs.items)) {
      75            9 :     const outs = allOutputs(asset)
      76            9 :     for (let k = 0; k < outs.length; k++) {
      77          504 :       items.push({
      78              :         id: `history:${asset.id}:${k}`,
      79              :         kind: 'history',
      80              :         assetId: asset.id,
      81              :         key: k
      82              :       })
      83              :     }
      84              :   }
      85           51 :   return items
      86              : })
      87              : 
      88           80 : const selectionMap = computed(
      89              :   () => new Map(selectableItems.value.map((v) => [v.id, v]))
      90              : )
      91              : 
      92           80 : const selectedValue = computed(() => {
      93          134 :   if (!store.selectedId) return undefined
      94           52 :   return selectionMap.value.get(store.selectedId)
      95              : })
      96              : 
      97          761 : function itemAttrs(id: string) {
      98          761 :   const selected = store.selectedId === id
      99          761 :   return {
     100              :     'data-state': selected ? 'checked' : 'unchecked',
     101              :     tabindex: selected ? 0 : -1
     102              :   }
     103              : }
     104              : 
     105           80 : const selectedItemEl = ref<Element | null>(null)
     106              : 
     107          761 : function selectedRef(id: string) {
     108          761 :   return store.selectedId === id
     109              :     ? (el: Element | ComponentPublicInstance | null) => {
     110           80 :         selectedItemEl.value = el instanceof Element ? el : null
     111              :       }
     112              :     : undefined
     113              : }
     114              : 
     115          134 : function doEmit() {
     116          134 :   const sel = selectedValue.value
     117          134 :   if (!sel) {
     118           82 :     emit('updateSelection', { canShowPreview: true })
     119           82 :     return
     120              :   }
     121           56 :   if (sel.kind === 'inProgress') {
     122           44 :     const item = store.activeWorkflowInProgressItems.find(
     123              :       (i) => i.id === sel.itemId
     124              :     )
     125           44 :     if (!item || item.state === 'skeleton') {
     126           24 :       emit('updateSelection', { canShowPreview: true, showSkeleton: true })
     127           20 :     } else if (item.state === 'latent') {
     128            2 :       emit('updateSelection', {
     129              :         canShowPreview: true,
     130              :         latentPreviewUrl: item.latentPreviewUrl
     131              :       })
     132              :     } else {
     133           18 :       emit('updateSelection', {
     134              :         output: item.output,
     135              :         canShowPreview: true
     136              :       })
     137              :     }
     138           44 :     return
     139              :   }
     140            8 :   const asset = toValue(outputs.items).find((a) => a.id === sel.assetId)
     141          134 :   const output = asset ? allOutputs(asset)[sel.key] : undefined
     142          134 :   const isFirst = toValue(outputs.items)[0]?.id === sel.assetId
     143          134 :   emit('updateSelection', {
     144              :     asset,
     145              :     output,
     146              :     canShowPreview: isFirst
     147              :   })
     148              : }
     149              : 
     150           80 : watchEffect(doEmit)
     151              : 
     152              : // On load or workflow tab switch, select the most recent item.
     153              : // Prefer in-progress items for this workflow, then history, skipping
     154              : // the global pending slot which may belong to another workflow.
     155           80 : watch(
     156              :   () => workflowStore.activeWorkflow?.path,
     157              :   (path) => {
     158           81 :     if (!path) return
     159           81 :     const inProgress = store.activeWorkflowInProgressItems
     160           81 :     if (inProgress.length > 0) {
     161            1 :       store.selectAsLatest(`slot:${inProgress[0].id}`)
     162           80 :     } else if (hasOutputs.value) {
     163           72 :       selectFirstHistory()
     164              :     } else {
     165            8 :       store.selectAsLatest(null)
     166              :     }
     167              :   },
     168              :   { immediate: true }
     169              : )
     170              : 
     171              : // Keep history selection stable on media changes
     172           80 : watch(
     173              :   () => toValue(outputs.items),
     174              :   (newAssets, oldAssets) => {
     175           40 :     if (
     176              :       newAssets.length === oldAssets.length ||
     177              :       (oldAssets.length === 0 && newAssets.length !== 1)
     178              :     )
     179           37 :       return
     180              : 
     181           40 :     if (store.selectedId?.startsWith('slot:')) return
     182              : 
     183           40 :     const sv = store.selectedId
     184              :       ? selectionMap.value.get(store.selectedId)
     185              :       : undefined
     186              : 
     187           40 :     if (!sv || sv.kind !== 'history') {
     188            1 :       if (hasOutputs.value) selectFirstHistory()
     189            1 :       return
     190              :     }
     191              : 
     192              :     const wasFirst = sv.assetId === oldAssets[0]?.id
     193           40 :     if (wasFirst || !newAssets.some((a) => a.id === sv.assetId)) {
     194            0 :       if (hasOutputs.value) selectFirstHistory()
     195              :     }
     196              :   }
     197              : )
     198              : 
     199           80 : const outputsRef = useTemplateRef('outputsRef')
     200              : 
     201              : // Track scrollWidth so we can compensate when items are prepended on the left.
     202           80 : let lastScrollWidth = 0
     203           80 : useResizeObserver(outputsRef, () => {
     204           84 :   lastScrollWidth = outputsRef.value?.scrollWidth ?? 0
     205              : })
     206           80 : watch(
     207              :   () => visibleHistory.value[0]?.id,
     208              :   () => {
     209            3 :     const el = outputsRef.value
     210            3 :     if (!el || el.scrollLeft === 0) {
     211            3 :       lastScrollWidth = el?.scrollWidth ?? 0
     212            3 :       return
     213              :     }
     214            0 :     nextTick(() => {
     215            0 :       const delta = el.scrollWidth - lastScrollWidth
     216            0 :       if (delta !== 0) el.scrollLeft += delta
     217            0 :       lastScrollWidth = el.scrollWidth
     218              :     })
     219              :   }
     220              : )
     221              : 
     222           80 : useInfiniteScroll(
     223              :   outputsRef,
     224              :   async () => {
     225            0 :     await outputs.loadMore()
     226              :   },
     227              :   {
     228              :     canLoadMore: () => toValue(outputs.hasMore)
     229              :   }
     230              : )
     231              : 
     232            0 : function navigateToAdjacent(direction: 1 | -1) {
     233            0 :   const items = selectableItems.value
     234            0 :   if (items.length === 0) return
     235            0 :   const currentId = store.selectedId
     236            0 :   const idx = currentId ? items.findIndex((i) => i.id === currentId) : -1
     237            0 :   const nextIdx =
     238              :     idx === -1 ? 0 : Math.max(0, Math.min(items.length - 1, idx + direction))
     239            0 :   store.select(items[nextIdx].id)
     240            0 :   nextTick(() => {
     241            0 :     selectedItemEl.value?.scrollIntoView({
     242              :       block: 'nearest',
     243              :       inline: 'nearest'
     244              :     })
     245              :   })
     246              : }
     247              : 
     248           80 : const pointer = new CanvasPointer(document.body)
     249           80 : let scrollOffset = 0
     250           80 : useEventListener(
     251              :   document.body,
     252              :   'wheel',
     253              :   function (e: WheelEvent) {
     254            0 :     if (!e.ctrlKey && !e.metaKey) return
     255            0 :     e.preventDefault()
     256            0 :     e.stopPropagation()
     257              : 
     258            0 :     if (!pointer.isTrackpadGesture(e)) {
     259            0 :       if (e.deltaY > 0) navigateToAdjacent(1)
     260            0 :       else navigateToAdjacent(-1)
     261            0 :       return
     262              :     }
     263            0 :     scrollOffset += e.deltaY
     264            0 :     while (scrollOffset >= 60) {
     265            0 :       scrollOffset -= 60
     266            0 :       navigateToAdjacent(1)
     267              :     }
     268            0 :     while (scrollOffset <= -60) {
     269            0 :       scrollOffset += 60
     270            0 :       navigateToAdjacent(-1)
     271              :     }
     272              :   },
     273              :   { capture: true, passive: false }
     274              : )
     275              : 
     276           80 : useEventListener(
     277              :   outputsRef,
     278              :   'wheel',
     279              :   function (e: WheelEvent) {
     280            0 :     if (e.ctrlKey || e.metaKey || e.deltaY === 0) return
     281            0 :     e.preventDefault()
     282            0 :     if (outputsRef.value) outputsRef.value.scrollLeft += e.deltaY
     283              :   },
     284              :   { passive: false }
     285              : )
     286              : 
     287           80 : const keyHandlers: Record<string, 1 | -1> = {
     288              :   ArrowUp: -1,
     289              :   ArrowDown: 1,
     290              :   ArrowLeft: -1,
     291              :   ArrowRight: 1
     292              : }
     293           80 : useEventListener(document.body, 'keydown', (e: KeyboardEvent) => {
     294           15 :   if (
     295              :     !(e.key in keyHandlers) ||
     296              :     e.target instanceof HTMLTextAreaElement ||
     297              :     e.target instanceof HTMLInputElement
     298              :   )
     299           15 :     return
     300              : 
     301            0 :   e.preventDefault()
     302            0 :   e.stopPropagation()
     303            0 :   navigateToAdjacent(keyHandlers[e.key])
     304              : })
     305           80 : </script>
     306              : <template>
     307          168 :   <div
     308              :     role="group"
     309              :     class="flex h-21 min-w-0 items-start justify-center px-4 py-3 pb-4"
     310              :   >
     311              :     <div
     312              :       v-if="queueCount > 0 || hasActiveContent"
     313              :       class="flex h-15 shrink-0 items-start gap-0.5"
     314              :     >
     315              :       <OutputHistoryActiveQueueItem
     316              :         v-if="queueCount > 1 || queueStore.pendingTasks.length"
     317              :         class="mr-3"
     318              :         :queue-count="queueCount"
     319              :       />
     320              : 
     321              :       <div
     322              :         v-if="mayBeActiveWorkflowPending"
     323              :         :ref="selectedRef('slot:pending')"
     324              :         v-bind="itemAttrs('slot:pending')"
     325              :         :class="itemClass"
     326              :         @click="store.select('slot:pending')"
     327              :       >
     328              :         <OutputPreviewItem />
     329              :       </div>
     330              : 
     331          255 :       <div
     332              :         v-for="item in store.activeWorkflowInProgressItems"
     333              :         :key="`${item.id}-${item.state}`"
     334              :         :ref="selectedRef(`slot:${item.id}`)"
     335              :         v-bind="itemAttrs(`slot:${item.id}`)"
     336              :         data-testid="linear-in-progress-item"
     337              :         :class="itemClass"
     338              :         @click="store.select(`slot:${item.id}`)"
     339              :       >
     340              :         <OutputPreviewItem
     341              :           v-if="item.state !== 'image' || !item.output"
     342              :           :latent-preview="item.latentPreviewUrl"
     343              :         />
     344              :         <OutputHistoryItem v-else :output="item.output" />
     345              :       </div>
     346              : 
     347              :       <div
     348              :         v-if="hasActiveContent && visibleHistory.length > 0"
     349              :         class="mx-4 h-12 shrink-0 border-l"
     350              :       />
     351              :     </div>
     352              : 
     353              :     <article
     354              :       ref="outputsRef"
     355              :       data-testid="linear-outputs"
     356              :       class="min-w-0 overflow-x-auto overflow-y-clip"
     357              :     >
     358              :       <div class="flex h-15 w-fit items-start gap-0.5">
     359            9 :         <template v-for="(asset, aIdx) in visibleHistory" :key="asset.id">
     360              :           <div v-if="aIdx > 0" class="mx-4 h-12 shrink-0 border-l" />
     361          504 :           <div
     362              :             v-for="(output, key) in toValue(allOutputs(asset))"
     363              :             :key
     364              :             :ref="selectedRef(`history:${asset.id}:${key}`)"
     365              :             v-bind="itemAttrs(`history:${asset.id}:${key}`)"
     366              :             data-testid="linear-history-item"
     367              :             :class="itemClass"
     368              :             @click="store.select(`history:${asset.id}:${key}`)"
     369              :           >
     370              :             <OutputHistoryItem :output="output" />
     371              :           </div>
     372              :         </template>
     373              :       </div>
     374              :     </article>
     375              :   </div>
     376              : </template>
        

Generated by: LCOV version 2.0-1