fix(types): type ComfyDialog and ComfyAsyncDialog properly

ComfyDialog:
- Type constructor buttons parameter as HTMLButtonElement[] | null
- Type show method parameter as string | HTMLElement | HTMLElement[]
- Use definite assignment for textElement

ComfyAsyncDialog:
- Make class generic with DialogAction<T> type
- Type resolve function, show/showModal return types
- Fix button creation with proper HTMLButtonElement cast
- Add generic static prompt method

ManageGroupDialog:
- Update show method signature to match base class
- Extract nodeType from parameter for string comparisons
This commit is contained in:
Johnpaul
2026-01-16 03:29:26 +01:00
parent 5bcf9e7be0
commit 7321c6e19e
3 changed files with 28 additions and 27 deletions

View File

@@ -340,7 +340,10 @@ export class ManageGroupDialog extends ComfyDialog<HTMLDialogElement> {
return !!outputs.length
}
override show(type?: string): void {
override show(groupNodeType?: string | HTMLElement | HTMLElement[]): void {
// Extract string type - this method repurposes the show signature
const nodeType =
typeof groupNodeType === 'string' ? groupNodeType : undefined
const groupNodes = Object.keys(app.rootGraph.extra?.groupNodes ?? {}).sort(
(a, b) => a.localeCompare(b)
)
@@ -392,7 +395,7 @@ export class ManageGroupDialog extends ComfyDialog<HTMLDialogElement> {
groupNodes.map((g) =>
$el('option', {
textContent: g,
selected: `${PREFIX}${SEPARATOR}${g}` === type,
selected: `${PREFIX}${SEPARATOR}${g}` === nodeType,
value: g
})
)
@@ -551,8 +554,8 @@ export class ManageGroupDialog extends ComfyDialog<HTMLDialogElement> {
this.element.replaceChildren(outer)
this.changeGroup(
type
? (groupNodes.find((g) => `${PREFIX}${SEPARATOR}${g}` === type) ??
nodeType
? (groupNodes.find((g) => `${PREFIX}${SEPARATOR}${g}` === nodeType) ??
groupNodes[0])
: groupNodes[0]
)

View File

@@ -1,28 +1,28 @@
import { $el } from '../../ui'
import { ComfyDialog } from '../dialog'
export class ComfyAsyncDialog extends ComfyDialog<HTMLDialogElement> {
// @ts-expect-error fixme ts strict error
#resolve: (value: any) => void
type DialogAction<T> = string | { value?: T; text: string }
constructor(actions?: Array<string | { value?: any; text: string }>) {
export class ComfyAsyncDialog<
T = string | null
> extends ComfyDialog<HTMLDialogElement> {
#resolve!: (value: T | null) => void
constructor(actions?: Array<DialogAction<T>>) {
super(
'dialog.comfy-dialog.comfyui-dialog',
// @ts-expect-error fixme ts strict error
actions?.map((opt) => {
if (typeof opt === 'string') {
opt = { text: opt }
}
const action = typeof opt === 'string' ? { text: opt } : opt
return $el('button.comfyui-button', {
type: 'button',
textContent: opt.text,
onclick: () => this.close(opt.value ?? opt.text)
})
textContent: action.text,
onclick: () => this.close((action.value ?? action.text) as T)
}) as HTMLButtonElement
})
)
}
override show(html: string | HTMLElement | HTMLElement[]) {
override show(html: string | HTMLElement | HTMLElement[]): Promise<T | null> {
this.element.addEventListener('close', () => {
this.close()
})
@@ -34,7 +34,7 @@ export class ComfyAsyncDialog extends ComfyDialog<HTMLDialogElement> {
})
}
showModal(html: string | HTMLElement | HTMLElement[]) {
showModal(html: string | HTMLElement | HTMLElement[]): Promise<T | null> {
this.element.addEventListener('close', () => {
this.close()
})
@@ -47,22 +47,22 @@ export class ComfyAsyncDialog extends ComfyDialog<HTMLDialogElement> {
})
}
override close(result = null) {
override close(result: T | null = null) {
this.#resolve(result)
this.element.close()
super.close()
}
static async prompt({
static async prompt<U = string>({
title = null,
message,
actions
}: {
title: string | null
message: string
actions: Array<string | { value?: any; text: string }>
}) {
const dialog = new ComfyAsyncDialog(actions)
actions: Array<DialogAction<U>>
}): Promise<U | null> {
const dialog = new ComfyAsyncDialog<U>(actions)
const content = [$el('span', message)]
if (title) {
content.unshift($el('h3', title))

View File

@@ -4,11 +4,10 @@ export class ComfyDialog<
T extends HTMLElement = HTMLElement
> extends EventTarget {
element: T
// @ts-expect-error fixme ts strict error
textElement: HTMLElement
textElement!: HTMLElement
#buttons: HTMLButtonElement[] | null
constructor(type = 'div', buttons = null) {
constructor(type = 'div', buttons: HTMLButtonElement[] | null = null) {
super()
this.#buttons = buttons
this.element = $el(type + '.comfy-modal', { parent: document.body }, [
@@ -35,8 +34,7 @@ export class ComfyDialog<
this.element.style.display = 'none'
}
// @ts-expect-error fixme ts strict error
show(html) {
show(html: string | HTMLElement | HTMLElement[]): void {
if (typeof html === 'string') {
this.textElement.innerHTML = html
} else {