LCOV - code coverage report
Current view: top level - src/stores - dialogStore.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 90.5 % 63 57
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { defineStore } from 'pinia'
       2              : import { markRaw, ref } from 'vue'
       3              : import type { Component, Ref } from 'vue'
       4              : 
       5              : import type { DialogContentSize } from '@comfyorg/design-system/lib/dialog/dialog.variants'
       6              : import type { ComponentAttrs } from 'vue-component-type-helpers'
       7              : 
       8              : export interface DialogComponentProps {
       9              :   closable?: boolean
      10              :   dismissOnPointerDownOutside?: boolean
      11              :   /**
      12              :    * The header, body and footer components lay out and pad their own
      13              :    * sections, so the dialog frame adds no padding around them.
      14              :    */
      15              :   flush?: boolean
      16              :   /** The component renders the whole panel: its surface, size and close button. */
      17              :   headless?: boolean
      18              :   maximizable?: boolean
      19              :   maximized?: boolean
      20              :   onClose?: () => void
      21              :   /**
      22              :    * Guaranteed cleanup: fires exactly once when the dialog leaves the stack
      23              :    * for any reason — `closeDialog` (user or programmatic) or the
      24              :    * 10-dialog-cap eviction in `createDialog`. Wire promise settlement here.
      25              :    * Keep user-intent side effects (telemetry, "don't show again") in
      26              :    * `onClose`, which never fires on eviction.
      27              :    */
      28              :   onRemoved?: () => void
      29              :   /**
      30              :    * Hides the header close button while keeping `closable` dismissal paths
      31              :    * (Escape, programmatic close) available. Defaults to shown.
      32              :    */
      33              :   showCloseButton?: boolean
      34              :   size?: DialogContentSize
      35              : }
      36              : 
      37              : export interface DialogInstance {
      38              :   key: string
      39              :   visible: boolean
      40              :   title?: string
      41              :   headerComponent?: Component
      42              :   headerProps?: Record<string, unknown>
      43              :   component: Component
      44              :   contentProps: Record<string, unknown>
      45              :   footerComponent?: Component
      46              :   footerProps?: Record<string, unknown>
      47              :   dialogComponentProps: DialogComponentProps
      48              :   priority: number
      49              : }
      50              : 
      51              : export interface ShowDialogOptions<
      52              :   H extends Component = Component,
      53              :   B extends Component = Component,
      54              :   F extends Component = Component
      55              : > {
      56              :   key?: string
      57              :   title?: string
      58              :   headerComponent?: H
      59              :   footerComponent?: F
      60              :   component: B
      61              :   props?: ComponentAttrs<B>
      62              :   headerProps?: ComponentAttrs<H>
      63              :   footerProps?: ComponentAttrs<F>
      64              :   dialogComponentProps?: DialogComponentProps
      65              :   /**
      66              :    * Optional priority for dialog stacking.
      67              :    * A dialog will never be shown above a dialog with a higher priority.
      68              :    * @default 1
      69              :    */
      70              :   priority?: number
      71              : }
      72              : 
      73              : interface UpdateDialogOptions {
      74              :   key: string
      75              :   contentProps?: Partial<DialogInstance['contentProps']>
      76              :   dialogComponentProps?: Partial<DialogComponentProps>
      77              : }
      78              : 
      79          762 : function notifyRemoved(dialog: DialogInstance | undefined) {
      80          762 :   dialog?.dialogComponentProps.onRemoved?.()
      81              : }
      82              : 
      83         2356 : export const useDialogStore = defineStore('dialog', () => {
      84         2355 :   const dialogStack: Ref<DialogInstance[]> = ref([])
      85              : 
      86              :   /** The key `closeDialog()` closes when called without one. */
      87         2355 :   const activeKey = ref<string | null>(null)
      88              : 
      89         2355 :   const genDialogKey = () => `dialog-${Math.random().toString(36).slice(2, 9)}`
      90              : 
      91              :   /**
      92              :    * Inserts a dialog into the stack at the correct position based on priority.
      93              :    * Higher priority dialogs are placed before lower priority ones.
      94              :    */
      95          899 :   function insertDialogByPriority(dialog: DialogInstance) {
      96          899 :     const insertIndex = dialogStack.value.findIndex(
      97              :       (d) => d.priority <= dialog.priority
      98              :     )
      99          899 :     dialogStack.value.splice(
     100              :       insertIndex === -1 ? dialogStack.value.length : insertIndex,
     101              :       0,
     102              :       dialog
     103              :     )
     104              :   }
     105              : 
     106          393 :   function riseDialog(options: { key: string }) {
     107          393 :     const dialogKey = options.key
     108              : 
     109          393 :     const index = dialogStack.value.findIndex((d) => d.key === dialogKey)
     110          393 :     if (index !== -1) {
     111          393 :       const [dialog] = dialogStack.value.splice(index, 1)
     112          393 :       insertDialogByPriority(dialog)
     113          393 :       activeKey.value = dialogKey
     114              :     }
     115              :   }
     116              : 
     117          271 :   function closeDialog(options?: { key: string }) {
     118          271 :     const targetDialog = options
     119              :       ? dialogStack.value.find((d) => d.key === options.key)
     120              :       : dialogStack.value.find((d) => d.key === activeKey.value)
     121          271 :     if (!targetDialog) return
     122              : 
     123          271 :     targetDialog.dialogComponentProps.onClose?.()
     124              :     // Identity, not key: a reentrant onClose can evict targetDialog and open a
     125              :     // replacement under the same key. Whoever actually removes the dialog from
     126              :     // the stack fires onRemoved, so it fires exactly once.
     127          271 :     const index = dialogStack.value.findIndex((d) => d === targetDialog)
     128          271 :     const removed = index !== -1
     129          271 :     if (removed) dialogStack.value.splice(index, 1)
     130              : 
     131              :     // A reentrant callback may have already activated a dialog of its own;
     132              :     // only fall back to the stack tail when the active key named a dialog that
     133              :     // is now gone.
     134          256 :     if (!dialogStack.value.some((d) => d.key === activeKey.value)) {
     135          256 :       activeKey.value =
     136              :         dialogStack.value.length > 0
     137              :           ? dialogStack.value[dialogStack.value.length - 1].key
     138              :           : null
     139              :     }
     140              : 
     141          256 :     if (removed) notifyRemoved(targetDialog)
     142              :   }
     143              : 
     144          506 :   function createDialog<
     145              :     H extends Component = Component,
     146              :     B extends Component = Component,
     147              :     F extends Component = Component
     148              :   >(options: ShowDialogOptions<H, B, F> & { key: string }) {
     149          506 :     const evicted =
     150              :       dialogStack.value.length >= 10 ? dialogStack.value.shift() : undefined
     151              : 
     152          506 :     const dialog = {
     153              :       key: options.key,
     154              :       visible: true,
     155              :       title: options.title,
     156              :       headerComponent: options.headerComponent
     157              :         ? markRaw(options.headerComponent)
     158              :         : undefined,
     159              :       footerComponent: options.footerComponent
     160              :         ? markRaw(options.footerComponent)
     161              :         : undefined,
     162              :       component: markRaw(options.component),
     163              :       headerProps: { ...options.headerProps },
     164              :       contentProps: { ...options.props },
     165              :       footerProps: { ...options.footerProps },
     166              :       priority: options.priority ?? 1,
     167              :       dialogComponentProps: {
     168              :         maximizable: false,
     169              :         closable: true,
     170              :         dismissOnPointerDownOutside: true,
     171              :         ...options.dialogComponentProps,
     172              :         maximized: options.dialogComponentProps?.maximized ?? false
     173              :       }
     174              :     }
     175              : 
     176          506 :     insertDialogByPriority(dialog)
     177          506 :     activeKey.value = options.key
     178          506 :     notifyRemoved(evicted)
     179              : 
     180          506 :     return dialog
     181              :   }
     182              : 
     183          506 :   function showDialog<
     184              :     H extends Component = Component,
     185              :     B extends Component = Component,
     186              :     F extends Component = Component
     187              :   >(options: ShowDialogOptions<H, B, F>) {
     188          506 :     const dialogKey = options.key || genDialogKey()
     189              : 
     190          506 :     let dialog = dialogStack.value.find((d) => d.key === dialogKey)
     191              : 
     192          506 :     if (dialog) {
     193            2 :       dialog.visible = true
     194            2 :       riseDialog(dialog)
     195              :     } else {
     196          504 :       dialog = createDialog({ ...options, key: dialogKey })
     197              :     }
     198          506 :     return dialog
     199              :   }
     200              : 
     201              :   /**
     202              :    * Shows a dialog from a third party extension.
     203              :    * Explicitly keys extension dialogs with `extension-` prefix,
     204              :    * to avoid conflicts & prevent use of internal dialogs (available via `dialogService`).
     205              :    */
     206            2 :   function showExtensionDialog(options: ShowDialogOptions & { key: string }) {
     207            2 :     const { key } = options
     208            2 :     if (!key) {
     209            0 :       console.error('Extension dialog key is required')
     210            0 :       return
     211              :     }
     212              : 
     213            2 :     const extKey = key.startsWith('extension-') ? key : `extension-${key}`
     214              : 
     215            2 :     const dialog = dialogStack.value.find((d) => d.key === extKey)
     216            2 :     if (!dialog) return createDialog({ ...options, key: extKey })
     217              : 
     218            0 :     dialog.visible = true
     219            0 :     riseDialog(dialog)
     220            0 :     return dialog
     221              :   }
     222              : 
     223         8959 :   function isDialogOpen(key: string) {
     224         8959 :     return dialogStack.value.some((d) => d.key === key)
     225              :   }
     226              : 
     227            6 :   function updateDialog(options: UpdateDialogOptions): boolean {
     228            6 :     const dialog = dialogStack.value.find((d) => d.key === options.key)
     229            6 :     if (!dialog) return false
     230              : 
     231            2 :     if (options.contentProps) {
     232            2 :       dialog.contentProps = {
     233              :         ...dialog.contentProps,
     234              :         ...options.contentProps
     235              :       }
     236              :     }
     237              : 
     238            2 :     if (options.dialogComponentProps) {
     239            0 :       dialog.dialogComponentProps = {
     240              :         ...dialog.dialogComponentProps,
     241              :         ...options.dialogComponentProps
     242              :       }
     243              :     }
     244              : 
     245            2 :     return true
     246              :   }
     247              : 
     248         2355 :   return {
     249              :     dialogStack,
     250              :     riseDialog,
     251              :     showDialog,
     252              :     closeDialog,
     253              :     showExtensionDialog,
     254              :     isDialogOpen,
     255              :     updateDialog,
     256              :     activeKey
     257              :   }
     258              : })
        

Generated by: LCOV version 2.0-1