Line data Source code
1 : import type { NodeBase, Transform } from './node'
2 : import type {
3 : CompactRect as Rect,
4 : Point2D as Vec2
5 : } from '@comfyorg/shared-frontend-utils/geometry'
6 : import type { Compositor, NodeTexture } from './compositor'
7 : import type { ContentStore } from './content'
8 : import type { Command } from './history'
9 :
10 : export interface RenderNodeCtx {
11 : compositor: Compositor
12 : content: ContentStore
13 :
14 : renderChild(node: NodeBase, region: Rect): NodeTexture | null
15 : placed(
16 : cacheKey: string,
17 : contentStamp: string,
18 : bitmap: HTMLCanvasElement | ImageBitmap | OffscreenCanvas,
19 : transform: Transform,
20 : linear?: boolean
21 : ): NodeTexture | null
22 : region: Rect
23 : devicePixelRatio: number
24 : }
25 :
26 : export interface NodeKind<T extends NodeBase = NodeBase> {
27 : kind: string
28 :
29 : create(init?: Partial<T>): T
30 :
31 : contentIds(node: T): string[]
32 :
33 : renderNode(node: T, ctx: RenderNodeCtx): NodeTexture | null
34 :
35 : bbox(node: T): Rect
36 :
37 : hitTest?(node: T, pt: Vec2): boolean
38 :
39 : onTransformCommitted?(
40 : node: T,
41 : before: Transform,
42 : deps: { content: ContentStore }
43 : ): Command | null
44 : }
45 :
46 2 : const registry = new Map<string, NodeKind>()
47 :
48 6 : export function registerNodeKind<T extends NodeBase>(kind: NodeKind<T>): void {
49 6 : registry.set(kind.kind, kind)
50 : }
51 :
52 74 : export function getNodeKind(kind: string): NodeKind {
53 74 : const k = registry.get(kind)
54 74 : if (!k) throw new Error(`Unknown node kind: ${kind}`)
55 74 : return k
56 : }
57 :
58 8 : export function hasNodeKind(kind: string): boolean {
59 8 : return registry.has(kind)
60 : }
|