sd-text-fix / ui /src /bridge.ts
Kazzze's picture
Update to current version
2a261b0 verified
Raw History Blame Contribute Delete
1.71 kB
import type { Look, Recipe } from './types'
/** What a ComfyUI node keeps: the selected lines with their texts, and the look. */
export interface NodeState {
items: { recipe: Recipe; text: string; erase?: boolean }[]
look: Partial<Look>
}
/** Messages from the host page: javascript/text_fix.js in the A1111 family, comfy_web/text_fix.js in ComfyUI. */
export type HostMessage = {
type: 'TF_INIT'
theme: 'dark' | 'light'
/** A1111 family: the image the gallery shows, and the tab it is in. */
image?: { url: string; name: string }
tab?: string
/** ComfyUI: the node the editor is open for, and what it keeps. */
node?: { id: string; state: NodeState }
}
/** Messages from this page to the host page. */
export type FrameMessage =
| { type: 'TF_READY' }
| { type: 'TF_CLOSE' }
| { type: 'TF_TO_GALLERY'; session: string }
| { type: 'TF_SEND'; session: string; target: 'img2img' | 'inpaint' }
| { type: 'TF_STATE'; state: NodeState }
export const inFrame = window.parent !== window
// The host serves this page itself: both sides have the same origin.
export function tellHost(message: FrameMessage) {
if (inFrame) window.parent.postMessage(message, window.location.origin)
}
export function onHostMessage(handler: (message: HostMessage) => void) {
const listener = (event: MessageEvent) => {
if (event.origin !== window.location.origin || event.source !== window.parent) return
const data = event.data as { type?: unknown } | null
if (data && data.type === 'TF_INIT') handler(event.data as HostMessage)
}
window.addEventListener('message', listener)
return () => window.removeEventListener('message', listener)
}