Line data Source code
1 : import { pick, zip } from 'es-toolkit/compat'
2 :
3 : import { downloadFile, openFileInNewTab } from '@/base/common/downloadUtil'
4 : import { visibleCanvasViewport } from '@/composables/canvas/visibleCanvasViewport'
5 : import { useSelectedLiteGraphItems } from '@/composables/canvas/useSelectedLiteGraphItems'
6 : import { useSubgraphOperations } from '@/composables/graph/useSubgraphOperations'
7 : import { useNodeAnimatedImage } from '@/composables/node/useNodeAnimatedImage'
8 : import { useNodeCanvasImagePreview } from '@/composables/node/useNodeCanvasImagePreview'
9 : import { useNodeImage, useNodeVideo } from '@/composables/node/useNodeImage'
10 : import {
11 : addWidgetPromotionOptions,
12 : isPreviewPseudoWidget
13 : } from '@/core/graph/subgraph/promotionUtils'
14 : import { applyDynamicInputs } from '@/core/graph/widgets/dynamicWidgets'
15 : import { resolveNodeDefSlotText, st, t } from '@/i18n'
16 : import {
17 : LGraphCanvas,
18 : LGraphEventMode,
19 : LGraphNode,
20 : LiteGraph,
21 : RenderShape,
22 : SubgraphNode,
23 : createBounds,
24 : outputAsSerialisable
25 : } from '@/lib/litegraph/src/litegraph'
26 : import type {
27 : CreateNodeOptions,
28 : GraphAddOptions,
29 : IContextMenuValue,
30 : INodeInputSlot,
31 : Subgraph
32 : } from '@/lib/litegraph/src/litegraph'
33 : import type { PointTuple as Point } from '@comfyorg/shared-frontend-utils/geometry'
34 : import { GET_CONFIG } from '@comfyorg/graph-model/widgets'
35 : import type {
36 : ExportedSubgraphInstance,
37 : ISerialisableNodeInput,
38 : ISerialisableNodeOutput,
39 : ISerialisedNode
40 : } from '@comfyorg/graph-model/serialisation'
41 : import type { IBaseWidget } from '@comfyorg/graph-model/widgets'
42 : import { toConcreteWidget } from '@/lib/litegraph/src/widgets/widgetMap'
43 : import { useSettingStore } from '@/platform/settings/settingStore'
44 : import { useToast } from '@/components/ui/toast/toastStore'
45 : import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore'
46 : import { createPromotedMultilineWidget } from '@/renderer/extensions/vueNodes/widgets/utils/multilineTextarea'
47 : import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
48 : import { useDialogService } from '@/services/dialogService'
49 : import { resolveSubgraphPseudoWidgetCache } from '@/services/subgraphPseudoWidgetCache'
50 : import type { SubgraphPseudoWidgetCache } from '@/services/subgraphPseudoWidgetCache'
51 : import { transformInputSpecV2ToV1 } from '@comfyorg/object-info-parser/migration'
52 : import type {
53 : ComfyNodeDef as ComfyNodeDefV2,
54 : InputSpec,
55 : OutputSpec
56 : } from '@comfyorg/object-info-parser/schemas/nodeDefSchemaV2'
57 : import type {
58 : ComfyNodeDef as ComfyNodeDefV1,
59 : InputSpec as InputSpecV1
60 : } from '@comfyorg/object-info-parser'
61 : import {
62 : getInputSpecType,
63 : zDynamicGroupInputSpec
64 : } from '@comfyorg/object-info-parser'
65 : import { useApp } from '@/scripts/appInstance'
66 : import { provideLitegraphService } from '@/core/graph/litegraphServiceAccess'
67 : import {
68 : clipspace,
69 : copyToClipspace,
70 : pasteFromClipspace
71 : } from '@/scripts/clipspace'
72 : import { $el } from '@/scripts/ui/utils'
73 : import { useExecutionStore } from '@/stores/executionStore'
74 : import { useNodeOutputStore } from '@/stores/nodeOutputStore'
75 : import { ComfyNodeDefImpl } from '@/core/graph/nodeDef/ComfyNodeDefImpl'
76 : import {
77 : getPreviewExposureHostLocator,
78 : usePreviewExposureStore
79 : } from '@/stores/previewExposureStore'
80 : import { useSubgraphStore } from '@/stores/subgraphStore'
81 : import { useFavoritedWidgetsStore } from '@/renderer/extensions/propertiesPanel/favoritedWidgetsStore'
82 : import { usePropertiesPanelStore } from '@/renderer/extensions/propertiesPanel/propertiesPanelStore'
83 : import { useWidgetStore } from '@/stores/widgetStore'
84 : import { parseNodeId } from '@comfyorg/graph-model/nodeId'
85 : import type { SerializedNodeId } from '@comfyorg/graph-model/nodeId'
86 : import { isBlueprintType } from '@/utils/blueprintUtils'
87 : import { markCoreMediaMenuCallback } from '@/utils/coreMediaMenuActionUtils'
88 : import { fullResolutionSrc, getImageFileUrl } from '@/utils/hdrFormatUtil'
89 : import { getErrorMessage } from '@comfyorg/shared-frontend-utils/errorUtil'
90 : import type { WidgetId } from '@comfyorg/graph-model/widgetId'
91 : import { normalizeI18nKey } from '@/utils/formatUtil'
92 : import {
93 : isAnimatedOutput,
94 : isVideoNode,
95 : isVideoOutput,
96 : migrateWidgetsValues
97 : } from '@/utils/litegraphUtil'
98 : import { getOrderedInputSpecs } from '@/workbench/utils/nodeDefOrderingUtil'
99 :
100 : import { useExtensionService } from '@/platform/extensions/services/extensionService'
101 :
102 0 : async function reencodeAsPngBlob(blob: Blob): Promise<Blob> {
103 0 : let image: ImageBitmap | HTMLImageElement
104 0 : if (typeof window.createImageBitmap === 'undefined') {
105 0 : const img = new Image()
106 0 : const loaded = new Promise<void>((resolve, reject) => {
107 0 : img.onload = () => resolve()
108 0 : img.onerror = () => reject(new Error('Image load failed'))
109 : })
110 0 : img.src = URL.createObjectURL(blob)
111 0 : try {
112 0 : await loaded
113 : } finally {
114 0 : URL.revokeObjectURL(img.src)
115 : }
116 0 : image = img
117 : } else {
118 0 : image = await createImageBitmap(blob)
119 : }
120 :
121 0 : const canvas = $el('canvas', { width: image.width, height: image.height })
122 0 : try {
123 0 : const ctx = canvas.getContext('2d')
124 0 : if (!ctx) throw new Error('Could not get canvas context')
125 0 : ctx.drawImage(image, 0, 0)
126 : } finally {
127 0 : if ('close' in image && typeof image.close === 'function') {
128 0 : image.close()
129 : }
130 : }
131 :
132 0 : return new Promise<Blob>((resolve, reject) => {
133 0 : canvas.toBlob((result) => {
134 0 : if (result) resolve(result)
135 0 : else reject(new Error('PNG conversion failed'))
136 : }, 'image/png')
137 : })
138 : }
139 :
140 215 : export function getExtraOptionsForWidget(
141 : node: LGraphNode,
142 : widget: IBaseWidget
143 : ) {
144 215 : const options: IContextMenuValue[] = []
145 215 : const input = node.inputs.find((inp) => inp.widget?.name === widget.name)
146 :
147 215 : if (input) {
148 207 : options.unshift({
149 : content: `${t('contextMenu.RenameWidget')}: ${widget.label ?? widget.name}`,
150 : callback: async () => {
151 0 : const newLabel = await useDialogService().prompt({
152 : title: t('g.rename'),
153 : message: t('g.enterNewNamePrompt'),
154 : defaultValue: widget.label,
155 : placeholder: widget.name
156 : })
157 0 : if (newLabel === null) return
158 0 : widget.label = newLabel || undefined
159 0 : input.label = newLabel || undefined
160 0 : widget.callback?.(widget.value)
161 0 : useCanvasStore().canvas?.setDirty(true)
162 : }
163 : })
164 : }
165 :
166 215 : const favoritedWidgetsStore = useFavoritedWidgetsStore()
167 215 : const isFavorited = favoritedWidgetsStore.isFavorited(node, widget.name)
168 215 : options.unshift({
169 : content: isFavorited
170 : ? `${t('contextMenu.UnfavoriteWidget')}: ${widget.label ?? widget.name}`
171 : : `${t('contextMenu.FavoriteWidget')}: ${widget.label ?? widget.name}`,
172 : callback: () => {
173 0 : favoritedWidgetsStore.toggleFavorite(node, widget.name)
174 : }
175 : })
176 :
177 215 : if (node.graph && !node.graph.isRootGraph) {
178 117 : addWidgetPromotionOptions(options, widget, node)
179 : }
180 215 : return options
181 : }
182 :
183 67241 : function getMinSize(node: LGraphNode) {
184 67241 : node._initialMinSize ??= { width: 1, height: 1 }
185 67241 : return node._initialMinSize
186 : }
187 :
188 : /**
189 : * Service that augments litegraph with ComfyUI specific functionality.
190 : */
191 2356 : export const useLitegraphService = () => {
192 2450337 : const extensionService = useExtensionService()
193 2450337 : const toast = useToast()
194 2450337 : const widgetStore = useWidgetStore()
195 2450337 : const canvasStore = useCanvasStore()
196 2450337 : const { toggleSelectedNodesMode } = useSelectedLiteGraphItems()
197 2450337 : const subgraphPseudoWidgetCache = new WeakMap<
198 : SubgraphNode,
199 : SubgraphPseudoWidgetCache<LGraphNode, IBaseWidget>
200 : >()
201 :
202 1180 : function invalidateSubgraphPseudoWidgetCache(node: SubgraphNode) {
203 1180 : subgraphPseudoWidgetCache.delete(node)
204 : }
205 :
206 837 : function getPseudoWidgetPreviewTargets(node: SubgraphNode): LGraphNode[] {
207 837 : const hostLocator = getPreviewExposureHostLocator(node)
208 837 : if (!hostLocator) return []
209 837 : const promotions = usePreviewExposureStore().getExposuresAsPromotionShape(
210 : node.rootGraph.id,
211 : hostLocator
212 : )
213 837 : const resolved = resolveSubgraphPseudoWidgetCache({
214 : cache: subgraphPseudoWidgetCache.get(node) ?? null,
215 : promotions,
216 : getNodeById: (nodeId) => node.subgraph.getNodeById(nodeId) ?? undefined,
217 : isPreviewPseudoWidget
218 : })
219 837 : subgraphPseudoWidgetCache.set(node, resolved.cache)
220 837 : return resolved.nodes
221 : }
222 :
223 : /**
224 : * @internal The node definition name used to resolve the node's i18n text.
225 : */
226 121117 : function nodeDefName(node: LGraphNode): string {
227 121117 : return node.constructor.nodeData?.name ?? ''
228 : }
229 : /**
230 : * @internal Add input sockets to the node. (No widget)
231 : */
232 73932 : function addInputSocket(node: LGraphNode, inputSpec: InputSpec) {
233 73932 : const inputName = inputSpec.name
234 : const widgetConstructor = widgetStore.widgets.get(
235 : inputSpec.widgetType ?? inputSpec.type
236 : )
237 73932 : if (
238 : (widgetConstructor && !inputSpec.forceInput) ||
239 : applyDynamicInputs(node, inputSpec)
240 : )
241 44328 : return
242 :
243 73932 : node.addInput(inputName, inputSpec.type, {
244 : shape: inputSpec.isOptional ? RenderShape.HollowCircle : undefined,
245 : localized_name: resolveNodeDefSlotText(
246 : 'name',
247 : nodeDefName(node),
248 : inputName,
249 : inputSpec.display_name,
250 : inputName
251 : )
252 : })
253 : }
254 : /**
255 : * @internal Setup stroke styles for the node under various conditions.
256 : */
257 23115 : function setupStrokeStyles(node: LGraphNode) {
258 23115 : node.strokeStyles['running'] = function (this: LGraphNode) {
259 65747 : const nodeLocatorId = useWorkflowStore().nodeIdToNodeLocatorId(this.id)
260 : const state =
261 : useExecutionStore().nodeLocationProgressStates[nodeLocatorId]?.state
262 65747 : if (state === 'running') {
263 12 : return { color: '#0f0', lineWidth: 3 }
264 : }
265 : }
266 23115 : node.strokeStyles['dragOver'] = function (this: LGraphNode) {
267 65747 : if (useApp().dragOverNode?.id == this.id) {
268 0 : return { color: 'dodgerblue' }
269 : }
270 : }
271 23115 : node.strokeStyles['executionError'] = function (this: LGraphNode) {
272 65747 : if (useApp().lastExecutionError?.node_id == this.id) {
273 17 : return { color: LiteGraph.NODE_ERROR_COLOUR, lineWidth: 3 }
274 : }
275 : }
276 : }
277 :
278 : /**
279 : * Utility function. Implemented for use with dynamic widgets
280 : */
281 940 : function addNodeInput(node: LGraphNode, inputSpec: InputSpec) {
282 940 : addInputSocket(node, inputSpec)
283 940 : addInputWidget(node, inputSpec, { dynamic: true })
284 : }
285 :
286 40 : function validateDynamicGroupWidgets(inputData: InputSpecV1, name: string) {
287 40 : const { template } = zDynamicGroupInputSpec.parse(inputData)[1]
288 40 : const fields = { ...template.required, ...template.optional }
289 40 : for (const [field, spec] of Object.entries(fields)) {
290 120 : if (
291 : !widgetStore.widgets.has(spec[1]?.widgetType ?? getInputSpecType(spec))
292 : )
293 0 : throw new TypeError(
294 : `DynamicGroup field '${name}.${field}' requires a registered widget`
295 : )
296 : }
297 : }
298 :
299 : /**
300 : * @internal Add a widget to the node. For both primitive types and custom widgets
301 : * (unless `socketless`), an input socket is also added.
302 : */
303 73932 : function addInputWidget(
304 : node: LGraphNode,
305 : inputSpec: InputSpec,
306 : { dynamic }: { dynamic?: boolean } = {}
307 : ) {
308 73932 : const widgetInputSpec = { ...inputSpec }
309 73932 : if (inputSpec.widgetType) {
310 44 : widgetInputSpec.type = inputSpec.widgetType
311 : }
312 73932 : const inputName = inputSpec.name
313 73932 : const resolveLabel = (fallback: string) =>
314 : resolveNodeDefSlotText(
315 : 'name',
316 : nodeDefName(node),
317 : inputName,
318 : widgetInputSpec.display_name,
319 : fallback
320 : )
321 73932 : const widgetConstructor = widgetStore.widgets.get(widgetInputSpec.type)
322 73932 : if (!widgetConstructor || inputSpec.forceInput) return
323 :
324 44126 : const inputData = transformInputSpecV2ToV1(widgetInputSpec)
325 73930 : if (widgetInputSpec.type === 'COMFY_DYNAMICGROUP_V3')
326 40 : validateDynamicGroupWidgets(inputData, inputName)
327 73932 : const widgetsBefore = new Set(node.widgets ?? [])
328 73932 : const result = widgetConstructor(node, inputName, inputData, useApp())
329 : const wrappedResult = result && !('type' in result) ? result : undefined
330 73932 : const { minWidth = 1, minHeight = 1 } = wrappedResult ?? {}
331 73932 : const returnedWidget = result && 'type' in result ? result : result?.widget
332 73932 : if (returnedWidget) {
333 : // oxlint-disable-next-line typescript/no-unnecessary-condition -- extension widgets may omit options at runtime
334 44124 : returnedWidget.options ??= {}
335 : }
336 73932 : const widget =
337 : (node.widgets ?? []).find(
338 : (candidate) =>
339 : candidate === returnedWidget ||
340 : (!widgetsBefore.has(candidate) &&
341 : (!returnedWidget || candidate.name === returnedWidget.name))
342 : ) ?? (returnedWidget ? toConcreteWidget(returnedWidget, node) : undefined)
343 73932 : if (!widget) return
344 :
345 73932 : widget.label = resolveLabel(
346 : widget.label ?? widgetInputSpec.display_name ?? inputName
347 : )
348 73932 : Object.assign(widget.options, {
349 : advanced: inputSpec.advanced
350 : })
351 73932 : if (inputSpec.hidden !== undefined) widget.hidden = inputSpec.hidden
352 73929 : if (dynamic) widget.tooltip = inputSpec.tooltip
353 :
354 44280 : if (!widget.options.socketless) {
355 44085 : const inputSpecV1 = transformInputSpecV2ToV1(widgetInputSpec)
356 44085 : node.addInput(inputName, inputSpec.type, {
357 : shape: inputSpec.isOptional ? RenderShape.HollowCircle : undefined,
358 : localized_name: resolveLabel(inputName),
359 : widget: { name: inputName, [GET_CONFIG]: () => inputSpecV1 }
360 : })
361 : }
362 44126 : const minSize = getMinSize(node)
363 44126 : minSize.width = Math.max(minSize.width, minWidth)
364 44126 : minSize.height = Math.max(minSize.height, minHeight)
365 : }
366 :
367 : /**
368 : * @internal Add inputs to the node.
369 : */
370 23115 : function addInputs(node: LGraphNode, inputs: Record<string, InputSpec>) {
371 23115 : const orderedInputSpecs = getOrderedInputSpecs(
372 : node.constructor.nodeData ?? {},
373 : inputs
374 : )
375 :
376 23115 : for (const inputSpec of orderedInputSpecs) {
377 72992 : addInputSocket(node, inputSpec)
378 72992 : addInputWidget(node, inputSpec)
379 : }
380 : }
381 :
382 : /**
383 : * @internal Add outputs to the node.
384 : */
385 23115 : function addOutputs(node: LGraphNode, outputs: OutputSpec[]) {
386 23115 : for (const output of outputs) {
387 28966 : const { name, is_list } = output
388 : // TODO: Fix the typing at the node spec level
389 28966 : const type = output.type === 'COMFY_MATCHTYPE_V3' ? '*' : output.type
390 28966 : const shapeOptions = is_list ? { shape: LiteGraph.GRID_SHAPE } : {}
391 28966 : const typeKey = `dataTypes.${normalizeI18nKey(type)}`
392 28966 : const outputOptions = {
393 : ...shapeOptions,
394 : // If the output name is different from the output type, use the output name.
395 : // e.g.
396 : // - type ("INT"); name ("Positive") => translate name
397 : // - type ("FLOAT"); name ("FLOAT") => translate type
398 : localized_name:
399 : type !== name
400 : ? resolveNodeDefSlotText(
401 : 'name',
402 : nodeDefName(node),
403 : output.index,
404 : name
405 : )
406 : : st(typeKey, name)
407 : }
408 28966 : node.addOutput(name, type, outputOptions)
409 : }
410 : }
411 :
412 : /**
413 : * @internal Set the initial size of the node.
414 : */
415 23115 : function setInitialSize(node: LGraphNode) {
416 23115 : const s = node.computeSize()
417 : // Expand the width a little to fit widget values on screen.
418 23115 : const pad =
419 : node.widgets?.length &&
420 : !useSettingStore().get('LiteGraph.Node.DefaultPadding')
421 23115 : const minSize = getMinSize(node)
422 23115 : s[0] = Math.max(minSize.width, s[0] + (pad ? 60 : 0))
423 23115 : s[1] = Math.max(minSize.height, s[1])
424 23115 : node.setSize(s)
425 : }
426 :
427 1123 : function registerSubgraphNodeDef(
428 : nodeDefV1: ComfyNodeDefV1,
429 : subgraph: Subgraph,
430 : instanceData: ExportedSubgraphInstance
431 : ) {
432 1123 : const node = class ComfyNode extends SubgraphNode {
433 : static comfyClass: string
434 : static override title: string
435 : static override category: string
436 : static override nodeData: ComfyNodeDefV1 & ComfyNodeDefV2
437 :
438 : constructor() {
439 524 : super(useApp().rootGraph, subgraph, instanceData)
440 :
441 524 : subgraph.events.addEventListener('widget-promoted', () => {
442 1149 : invalidateSubgraphPseudoWidgetCache(this)
443 : })
444 :
445 524 : subgraph.events.addEventListener('widget-demoted', () => {
446 31 : invalidateSubgraphPseudoWidgetCache(this)
447 : })
448 :
449 524 : setupStrokeStyles(this)
450 524 : addInputs(this, ComfyNode.nodeData.inputs)
451 524 : addOutputs(this, ComfyNode.nodeData.outputs)
452 524 : setInitialSize(this)
453 524 : this.serialize_widgets = true
454 524 : void extensionService.invokeExtensionsAsync('nodeCreated', this)
455 : }
456 :
457 : protected override createPromotedHostWidget(
458 : input: INodeInputSlot,
459 : widgetId: WidgetId,
460 : sourceWidget: Readonly<IBaseWidget>
461 : ): IBaseWidget | undefined {
462 1106 : return createPromotedMultilineWidget({
463 : subgraphNode: this,
464 : input,
465 : widgetId,
466 : sourceWidget
467 : })
468 : }
469 :
470 : /**
471 : * Configure the node from a serialised node. Keep 'name', 'type', 'shape',
472 : * and 'localized_name' information from the original node definition.
473 : */
474 : override configure(data: ISerialisedNode): void {
475 985 : const RESERVED_KEYS = ['name', 'type', 'shape', 'localized_name']
476 :
477 : // Note: input name is unique in a node definition, so we can lookup
478 : // input by name.
479 985 : const inputByName = new Map<string, ISerialisableNodeInput>(
480 : data.inputs?.map((input) => [input.name, input]) ?? []
481 : )
482 : // Inputs defined by the node definition.
483 985 : const definedInputNames = new Set(
484 : this.inputs.map((input) => input.name)
485 : )
486 985 : const definedInputs = this.inputs.map((input) => {
487 971 : const inputData = inputByName.get(input.name)
488 971 : return inputData
489 : ? {
490 : ...inputData,
491 : // Whether the input has associated widget follows the
492 : // original node definition.
493 : ...pick(input, RESERVED_KEYS.concat('widget'))
494 : }
495 : : input
496 : })
497 : // Extra inputs that potentially dynamically added by custom js logic.
498 985 : const extraInputs = data.inputs?.filter(
499 : (input) => !definedInputNames.has(input.name)
500 : )
501 985 : data.inputs = [...definedInputs, ...(extraInputs ?? [])]
502 :
503 : // Note: output name is not unique, so we cannot lookup output by name.
504 : // Use index instead.
505 985 : data.outputs = zip(this.outputs, data.outputs ?? []).map(
506 : ([output, outputData], index) => {
507 : // If there are extra outputs in the serialised node, use them directly.
508 : // There are currently custom nodes that dynamically add outputs via
509 : // js logic.
510 476 : if (!output) return outputData as ISerialisableNodeOutput
511 :
512 476 : return outputData
513 : ? {
514 : ...outputData,
515 : ...pick(output, RESERVED_KEYS)
516 : }
517 : : outputAsSerialisable(output, this, index)
518 : }
519 : )
520 :
521 985 : data.widgets_values = migrateWidgetsValues(
522 : ComfyNode.nodeData.inputs,
523 : this.widgets,
524 : data.widgets_values ?? []
525 : )
526 :
527 985 : super.configure(data)
528 : }
529 : }
530 :
531 1123 : addNodeContextMenuHandler(node)
532 1123 : addDrawBackgroundHandler(node)
533 1123 : addNodeKeyHandler(node)
534 : // Note: Some extensions expects node.comfyClass to be set in
535 : // `beforeRegisterNodeDef`.
536 1123 : node.prototype.comfyClass = nodeDefV1.name
537 1123 : node.comfyClass = nodeDefV1.name
538 :
539 1123 : const nodeDef = new ComfyNodeDefImpl(nodeDefV1)
540 1123 : node.nodeData = nodeDef
541 1123 : LiteGraph.registerNodeType(subgraph.id, node)
542 : // Note: Do not following assignments before `LiteGraph.registerNodeType`
543 : // because `registerNodeType` will overwrite the assignments.
544 1123 : node.category = nodeDef.category
545 1123 : node.skip_list = true
546 1123 : node.title = nodeDef.display_name || nodeDef.name
547 : }
548 :
549 2440403 : async function registerNodeDef(nodeId: string, nodeDefV1: ComfyNodeDefV1) {
550 2440403 : const node = class ComfyNode extends LGraphNode {
551 : static comfyClass: string
552 : static override title: string
553 : static override category: string
554 : static override nodeData: ComfyNodeDefV1 & ComfyNodeDefV2
555 :
556 : constructor(title: string) {
557 22591 : super(title)
558 22591 : setupStrokeStyles(this)
559 22591 : addInputs(this, ComfyNode.nodeData.inputs)
560 22591 : addOutputs(this, ComfyNode.nodeData.outputs)
561 22591 : setInitialSize(this)
562 22591 : this.serialize_widgets = true
563 :
564 : // Mark API Nodes yellow by default to distinguish with other nodes.
565 22591 : if (ComfyNode.nodeData.api_node) {
566 35 : this.color = LGraphCanvas.node_colors.yellow.color
567 35 : this.bgcolor = LGraphCanvas.node_colors.yellow.bgcolor
568 : }
569 :
570 22591 : void extensionService.invokeExtensionsAsync('nodeCreated', this)
571 : }
572 :
573 : /**
574 : * Configure the node from a serialised node. Keep 'name', 'type', 'shape',
575 : * and 'localized_name' information from the original node definition.
576 : */
577 : override configure(data: ISerialisedNode): void {
578 22361 : const RESERVED_KEYS = ['name', 'type', 'shape', 'localized_name']
579 :
580 : // Note: input name is unique in a node definition, so we can lookup
581 : // input by name.
582 22361 : const inputByName = new Map<string, ISerialisableNodeInput>(
583 : data.inputs?.map((input) => [input.name, input]) ?? []
584 : )
585 : // Inputs defined by the node definition.
586 22361 : const definedInputNames = new Set(
587 : this.inputs.map((input) => input.name)
588 : )
589 22361 : const definedInputs = this.inputs.map((input) => {
590 72296 : const inputData = inputByName.get(input.name)
591 72296 : return inputData
592 : ? {
593 : ...inputData,
594 : // Whether the input has associated widget follows the
595 : // original node definition.
596 : ...pick(input, RESERVED_KEYS.concat('widget'))
597 : }
598 : : input
599 : })
600 : // Extra inputs that potentially dynamically added by custom js logic.
601 22361 : const extraInputs = data.inputs?.filter(
602 : (input) => !definedInputNames.has(input.name)
603 : )
604 22361 : data.inputs = [...definedInputs, ...(extraInputs ?? [])]
605 :
606 : // Note: output name is not unique, so we cannot lookup output by name.
607 : // Use index instead.
608 22361 : data.outputs = zip(this.outputs, data.outputs ?? []).map(
609 : ([output, outputData], index) => {
610 : // If there are extra outputs in the serialised node, use them directly.
611 : // There are currently custom nodes that dynamically add outputs via
612 : // js logic.
613 28704 : if (!output) return outputData as ISerialisableNodeOutput
614 :
615 28704 : return outputData
616 : ? {
617 : ...outputData,
618 : ...pick(output, RESERVED_KEYS)
619 : }
620 : : outputAsSerialisable(output, this, index)
621 : }
622 : )
623 :
624 22361 : data.widgets_values = migrateWidgetsValues(
625 : ComfyNode.nodeData.inputs,
626 : this.widgets ?? [],
627 : data.widgets_values ?? []
628 : )
629 :
630 22361 : super.configure(data)
631 : }
632 : }
633 :
634 2440403 : addNodeContextMenuHandler(node)
635 2440403 : addDrawBackgroundHandler(node)
636 2440403 : addNodeKeyHandler(node)
637 : // Note: Some extensions expects node.comfyClass to be set in
638 : // `beforeRegisterNodeDef`.
639 2440403 : node.prototype.comfyClass = nodeDefV1.name
640 2440403 : node.comfyClass = nodeDefV1.name
641 2440403 : await extensionService.invokeExtensionsAsync(
642 : 'beforeRegisterNodeDef',
643 : node,
644 : nodeDefV1 // Receives V1 NodeDef, and potentially make modifications to it
645 : )
646 :
647 2440403 : const nodeDef = new ComfyNodeDefImpl(nodeDefV1)
648 2440403 : node.nodeData = nodeDef
649 2440403 : LiteGraph.registerNodeType(nodeId, node)
650 : // Note: Do not following assignments before `LiteGraph.registerNodeType`
651 : // because `registerNodeType` will overwrite the assignments.
652 2440403 : node.category = nodeDef.category
653 2440403 : node.title = nodeDef.display_name || nodeDef.name
654 :
655 : // Set skip_list for dev-only nodes based on current DevMode setting
656 : // This ensures nodes registered after initial load respect the current setting
657 2440403 : if (nodeDef.dev_only) {
658 : const settingStore = useSettingStore()
659 7101 : node.skip_list = !settingStore.get('Comfy.DevMode')
660 : }
661 : }
662 :
663 : /**
664 : * Adds special context menu handling for nodes
665 : * e.g. this adds Open Image functionality for nodes that show images
666 : * @param {*} node The node to add the menu handler
667 : */
668 2441526 : function addNodeContextMenuHandler(node: typeof LGraphNode) {
669 166 : function getCopyImageOption(img: HTMLImageElement): IContextMenuValue[] {
670 166 : if (typeof window.ClipboardItem === 'undefined') return []
671 166 : return [
672 : {
673 : content: 'Copy Image',
674 : callback: markCoreMediaMenuCallback(async () => {
675 0 : const url = new URL(fullResolutionSrc(img), img.baseURI)
676 0 : url.searchParams.delete('preview')
677 :
678 0 : try {
679 : const data = await fetch(url)
680 0 : const blob = await data.blob()
681 0 : try {
682 0 : await navigator.clipboard.write([
683 : new ClipboardItem({ [blob.type]: blob })
684 : ])
685 : } catch (error) {
686 0 : if (blob.type !== 'image/png') {
687 0 : const pngBlob = await reencodeAsPngBlob(blob)
688 0 : await navigator.clipboard.write([
689 : new ClipboardItem({ 'image/png': pngBlob })
690 : ])
691 0 : return
692 : }
693 0 : throw error
694 : }
695 : } catch (error) {
696 0 : toast.warning(
697 : t('toastMessages.errorCopyImage', {
698 : error: getErrorMessage(error) ?? t('g.unknownError')
699 : })
700 : )
701 : }
702 : }, 'preview')
703 : }
704 : ]
705 : }
706 :
707 2441526 : node.prototype.getExtraMenuOptions = function (canvas, options) {
708 4630 : if (this.imgs) {
709 174 : let img
710 174 : if (this.imageIndex != null) {
711 166 : img = this.imgs[this.imageIndex]
712 8 : } else if (this.overIndex != null) {
713 0 : img = this.imgs[this.overIndex]
714 : }
715 174 : if (img) {
716 166 : options.unshift(
717 : {
718 : content: 'Open Image',
719 : callback: markCoreMediaMenuCallback(() => {
720 0 : const url = getImageFileUrl(img)
721 0 : void openFileInNewTab(url.toString())
722 : }, 'preview')
723 : },
724 : ...getCopyImageOption(img),
725 : {
726 : content: 'Save Image',
727 : callback: markCoreMediaMenuCallback(() => {
728 0 : const url = getImageFileUrl(img)
729 0 : const filename = new URLSearchParams(url.search).get('filename')
730 0 : downloadFile(url.toString(), filename ?? undefined)
731 : }, 'preview')
732 : }
733 : )
734 : }
735 : }
736 :
737 4630 : options.push({
738 : content: 'Bypass',
739 : callback: () => {
740 1 : toggleSelectedNodesMode(LGraphEventMode.BYPASS)
741 1 : canvas.setDirty(true, true)
742 : }
743 : })
744 :
745 4630 : if (!clipspace.returnNode) {
746 4630 : options.push({
747 : content: 'Copy (Clipspace)',
748 : callback: () => {
749 0 : copyToClipspace(this)
750 : }
751 : })
752 :
753 4630 : if (clipspace.current != null) {
754 0 : options.push({
755 : content: 'Paste (Clipspace)',
756 : callback: markCoreMediaMenuCallback(() => {
757 0 : pasteFromClipspace(this)
758 : }, 'input')
759 : })
760 : }
761 : }
762 4630 : if (this instanceof SubgraphNode) {
763 856 : options.unshift(
764 : {
765 : content: 'Edit Subgraph Widgets',
766 : callback: () => {
767 7 : usePropertiesPanelStore().openPanel('subgraph')
768 : }
769 : },
770 : {
771 : content: 'Unpack Subgraph',
772 : callback: () => {
773 1 : const { unpackSubgraph } = useSubgraphOperations()
774 1 : unpackSubgraph()
775 : }
776 : }
777 : )
778 : }
779 4630 : const [x, y] = canvas.graph_mouse
780 4630 : const overWidget = this.getWidgetOnPos(x, y, true)
781 4630 : if (overWidget)
782 80 : options.unshift(...getExtraOptionsForWidget(this, overWidget))
783 4630 : return []
784 : }
785 : }
786 66967 : function updatePreviews(node: LGraphNode, callback?: () => void) {
787 66967 : try {
788 66967 : unsafeUpdatePreviews.call(node, callback)
789 : } catch (error) {
790 0 : console.error('Error drawing node background', error)
791 : }
792 : }
793 66967 : function unsafeUpdatePreviews(this: LGraphNode, callback?: () => void) {
794 66967 : if (this.flags.collapsed) return
795 :
796 66916 : const nodeOutputStore = useNodeOutputStore()
797 66916 : const { showAnimatedPreview, removeAnimatedPreview } =
798 : useNodeAnimatedImage()
799 66916 : const { showCanvasImagePreview, removeCanvasImagePreview } =
800 : useNodeCanvasImagePreview()
801 :
802 66916 : const output = nodeOutputStore.getNodeOutputs(this)
803 66916 : const preview = nodeOutputStore.getNodePreviews(this)
804 :
805 66967 : const isNewOutput = output && this.images !== output.images
806 66967 : const isNewPreview = preview && this.preview !== preview
807 :
808 66967 : if (isNewPreview) this.preview = preview
809 66960 : if (isNewOutput) this.images = output.images
810 :
811 66967 : if (isNewOutput || isNewPreview) {
812 90 : this.animatedImages = isAnimatedOutput(output)
813 :
814 : const isVideo = isVideoOutput(output) || isVideoNode(this)
815 90 : if (isVideo) {
816 7 : useNodeVideo(this, callback).showPreview()
817 : } else {
818 83 : useNodeImage(this, callback).showPreview()
819 : }
820 : }
821 :
822 : // Nothing to do
823 66967 : if (!this.imgs?.length) return
824 :
825 1204 : if (this.animatedImages) {
826 3 : removeCanvasImagePreview(this)
827 3 : showAnimatedPreview(this)
828 : } else {
829 182 : removeAnimatedPreview(this)
830 182 : showCanvasImagePreview(this)
831 : }
832 : }
833 :
834 : /**
835 : * Adds Custom drawing logic for nodes
836 : * e.g. Draws images and handles thumbnail navigation on nodes that output images
837 : * @param {*} node The node to add the draw handler
838 : */
839 2441526 : function addDrawBackgroundHandler(node: typeof LGraphNode) {
840 : /**
841 : * @deprecated No longer needed as we use {@link useImagePreviewWidget}
842 : */
843 2441526 : node.prototype.setSizeForImage = function (this: LGraphNode) {
844 0 : console.warn(
845 : 'node.setSizeForImage is deprecated. Now it has no effect. Please remove the call to it.'
846 : )
847 : }
848 2441526 : node.prototype.onDrawBackground = function () {
849 65747 : updatePreviews(this)
850 :
851 65747 : if (this instanceof SubgraphNode) {
852 837 : const parentGraph = this.graph
853 837 : for (const interiorNode of getPseudoWidgetPreviewTargets(this)) {
854 539 : updatePreviews(interiorNode, () => {
855 0 : parentGraph?.setDirtyCanvas(true)
856 : })
857 : }
858 : }
859 : }
860 : }
861 :
862 2441526 : function addNodeKeyHandler(node: typeof LGraphNode) {
863 2441526 : const origNodeOnKeyDown = node.prototype.onKeyDown
864 :
865 2441526 : node.prototype.onKeyDown = function (e) {
866 : const originalResult: unknown = origNodeOnKeyDown?.call(this, e)
867 402 : if (originalResult === false) {
868 0 : return false
869 : }
870 :
871 402 : if (this.flags.collapsed || !this.imgs || this.imageIndex === null) {
872 393 : return
873 : }
874 :
875 9 : let handled = false
876 :
877 402 : if (e.key === 'ArrowLeft' || e.key === 'ArrowRight') {
878 0 : const imageIndex = this.imageIndex
879 0 : if (imageIndex === undefined) return
880 0 : if (e.key === 'ArrowLeft') {
881 0 : this.imageIndex = imageIndex - 1
882 : } else {
883 0 : this.imageIndex = imageIndex + 1
884 : }
885 0 : this.imageIndex %= this.imgs.length
886 :
887 0 : if (this.imageIndex < 0) {
888 0 : this.imageIndex = this.imgs.length + this.imageIndex
889 : }
890 0 : handled = true
891 9 : } else if (e.key === 'Escape') {
892 0 : this.imageIndex = null
893 0 : handled = true
894 : }
895 :
896 17 : if (handled) {
897 0 : e.preventDefault()
898 0 : e.stopImmediatePropagation()
899 0 : return false
900 : }
901 : }
902 : }
903 :
904 101 : function addNodeOnGraph(
905 : nodeDef: ComfyNodeDefV1 | ComfyNodeDefV2,
906 : options: CreateNodeOptions = {},
907 : addOptions?: GraphAddOptions
908 : ): LGraphNode | null {
909 101 : options.pos ??= getCanvasCenter()
910 :
911 101 : if (isBlueprintType(nodeDef.name)) {
912 3 : const canvas = canvasStore.getCanvas()
913 3 : const bp = useSubgraphStore().getBlueprint(nodeDef.name)
914 3 : const items: object = {
915 : nodes: bp.nodes,
916 : subgraphs: bp.definitions?.subgraphs
917 : }
918 3 : const results = canvas._deserializeItems(items, {
919 : position: options.pos
920 : })
921 3 : if (!results) throw new Error('Failed to add subgraph blueprint')
922 3 : const node = results.nodes.values().next().value
923 3 : if (!node)
924 0 : throw new Error(
925 : 'Subgraph blueprint was added, but failed to resolve a subgraph Node'
926 : )
927 3 : if (addOptions?.ghost) {
928 3 : node.flags.ghost = true
929 3 : canvas.graph?.trigger('node:property:changed', {
930 : nodeId: node.id,
931 : property: 'flags.ghost',
932 : oldValue: false,
933 : newValue: true
934 : })
935 3 : canvas.startGhostPlacement(node, addOptions.dragEvent)
936 : }
937 3 : return node
938 : }
939 :
940 98 : const node = LiteGraph.createNode(
941 : nodeDef.name,
942 : nodeDef.display_name,
943 : options
944 : )
945 :
946 101 : const graph = useWorkflowStore().activeSubgraph ?? useApp().graph
947 101 : if (!node) return null
948 :
949 98 : graph.add(node, addOptions)
950 98 : return node
951 : }
952 :
953 27 : function getCanvasCenter(): Point {
954 27 : if (!useApp().isGraphReady) return [0, 0]
955 : const visibleArea = useApp().canvas.ds.visible_area
956 : const [x, y, w, h] = visibleArea
957 27 : return [x + w / 2, y + h / 2]
958 : }
959 :
960 0 : function goToNode(nodeId: SerializedNodeId) {
961 0 : const parsedNodeId = parseNodeId(nodeId)
962 0 : if (!parsedNodeId) return
963 0 : const graphNode = useApp().canvas.graph?.getNodeById(parsedNodeId)
964 0 : if (!graphNode) return
965 0 : useApp().canvas.animateToBounds(graphNode.boundingRect)
966 : }
967 :
968 359 : function ensureBounds(nodes: LGraphNode[]) {
969 359 : for (const node of nodes) {
970 375 : if (!node.boundingRect.every((i) => i === 0)) continue
971 375 : node.updateArea()
972 : }
973 : }
974 :
975 : /**
976 : * Resets the canvas view to the default
977 : */
978 0 : function resetView() {
979 0 : const canvas = canvasStore.canvas
980 0 : if (!canvas) return
981 :
982 0 : canvas.ds.scale = 1
983 0 : canvas.ds.offset = [0, 0]
984 0 : canvas.setDirty(true, true)
985 : }
986 :
987 359 : function fitView() {
988 359 : const canvas = canvasStore.getCanvas()
989 359 : const nodes = canvas.graph?.nodes
990 359 : if (!nodes) return
991 359 : ensureBounds(nodes)
992 359 : const bounds = createBounds(nodes)
993 359 : if (!bounds) return
994 :
995 181 : canvas.ds.fitToBounds(bounds, {
996 : viewport: visibleCanvasViewport(canvas)
997 : })
998 181 : canvas.setDirty(true, true)
999 : }
1000 :
1001 2450337 : return {
1002 : registerNodeDef,
1003 : registerSubgraphNodeDef,
1004 : addNodeOnGraph,
1005 : addNodeInput,
1006 : getCanvasCenter,
1007 : getExtraOptionsForWidget,
1008 : goToNode,
1009 : resetView,
1010 : fitView,
1011 : updatePreviews
1012 : }
1013 : }
1014 :
1015 2356 : provideLitegraphService(useLitegraphService)
|