diff --git a/browser_tests/assets/widgets/image_crop_dynamic_inputs.json b/browser_tests/assets/widgets/image_crop_dynamic_inputs.json new file mode 100644 index 00000000000..15c34e01cb0 --- /dev/null +++ b/browser_tests/assets/widgets/image_crop_dynamic_inputs.json @@ -0,0 +1,148 @@ +{ + "id": "dadf4878-8f8f-41bc-bf4a-53714aaeede1", + "revision": 0, + "last_node_id": 2, + "last_link_id": 1, + "nodes": [ + { + "id": 1, + "type": "LoadImage", + "pos": [50, 80], + "size": [282.798828125, 102], + "flags": {}, + "order": 0, + "mode": 0, + "inputs": [ + { + "localized_name": "image", + "name": "image", + "type": "COMBO", + "widget": { + "name": "image" + }, + "link": null + }, + { + "localized_name": "choose file to upload", + "name": "upload", + "type": "IMAGEUPLOAD", + "widget": { + "name": "upload" + }, + "link": null + } + ], + "outputs": [ + { + "localized_name": "IMAGE", + "name": "IMAGE", + "type": "IMAGE", + "links": [1] + }, + { + "localized_name": "MASK", + "name": "MASK", + "type": "MASK", + "links": null + } + ], + "properties": { + "Node name for S&R": "LoadImage" + }, + "widgets_values": ["example.png", "image"], + "widgets_values_named": { + "image": "example.png", + "upload": "image" + } + }, + { + "id": 2, + "type": "DynamicInputImageCrop", + "pos": [450, 80], + "size": [400, 500], + "flags": {}, + "order": 1, + "mode": 0, + "inputs": [ + { + "localized_name": "image_1", + "name": "model.images.image_1", + "type": "IMAGE", + "link": 1 + }, + { + "localized_name": "image_2", + "name": "model.images.image_2", + "shape": 7, + "type": "IMAGE", + "link": null + }, + { + "localized_name": "region", + "name": "model.region", + "type": "BOUNDING_BOX", + "widget": { + "name": "model.region" + }, + "link": null + }, + { + "localized_name": "model", + "name": "model", + "type": "COMFY_DYNAMICCOMBO_V3", + "widget": { + "name": "model" + }, + "link": null + } + ], + "outputs": [ + { + "localized_name": "IMAGE", + "name": "IMAGE", + "type": "IMAGE", + "links": null + } + ], + "properties": { + "Node name for S&R": "DynamicInputImageCrop" + }, + "widgets_values": [ + "Crop", + { + "x": 0, + "y": 0, + "width": 512, + "height": 512 + }, + 0, + 0, + 512, + 512 + ], + "widgets_values_named": { + "model": "Crop", + "model.region": { + "x": 0, + "y": 0, + "width": 512, + "height": 512 + }, + "x": 0, + "y": 0, + "width": 512, + "height": 512 + } + } + ], + "links": [[1, 1, 0, 2, 0, "IMAGE"]], + "groups": [], + "config": {}, + "extra": { + "ds": { + "scale": 1, + "offset": [0, 0] + } + }, + "version": 0.4 +} diff --git a/browser_tests/tests/vueNodes/widgets/imageCropDynamicInputs.spec.ts b/browser_tests/tests/vueNodes/widgets/imageCropDynamicInputs.spec.ts new file mode 100644 index 00000000000..7e4835c7af3 --- /dev/null +++ b/browser_tests/tests/vueNodes/widgets/imageCropDynamicInputs.spec.ts @@ -0,0 +1,75 @@ +import { + comfyExpect as expect, + comfyPageFixture +} from '@e2e/fixtures/ComfyPage' +import type { ComfyNodeDef } from '@/schemas/nodeDefSchema' + +const test = comfyPageFixture.extend({ + page: async ({ page }, use) => { + await page.route('**/object_info', async (route) => { + const response = await route.fetch() + const definitions: Record = await response.json() + const cropNode: ComfyNodeDef = { + name: 'DynamicInputImageCrop', + display_name: 'Dynamic Input Image Crop', + category: 'testing', + python_module: 'nodes', + description: '', + input: { + required: { + model: [ + 'COMFY_DYNAMICCOMBO_V3', + { + options: [ + { + key: 'Crop', + inputs: { + required: { + images: [ + 'COMFY_AUTOGROW_V3', + { + template: { + input: { required: { image: ['IMAGE', {}] } }, + names: ['image_1', 'image_2'], + min: 1 + } + } + ], + region: ['BOUNDING_BOX', { component: 'ImageCrop' }] + } + } + } + ] + } + ] + } + }, + output: ['IMAGE'], + output_name: ['IMAGE'], + output_node: false + } + await route.fulfill({ + response, + json: { ...definitions, [cropNode.name]: cropNode } + }) + }) + await use(page) + } +}) + +test.describe( + 'Image Crop with dynamic inputs', + { tag: ['@widget', '@vue-nodes'] }, + () => { + test('shows the connected image after reopening a dynamic-input crop node', async ({ + comfyPage + }) => { + await comfyPage.workflow.loadWorkflow('widgets/image_crop_dynamic_inputs') + + const cropNode = comfyPage.vueNodes.getNodeLocator('2') + await expect(cropNode).toBeVisible() + await expect.soft(cropNode.getByTestId('crop-overlay')).toBeVisible() + await expect.soft(cropNode.getByTestId('crop-empty-state')).toBeHidden() + }) + } +) diff --git a/src/composables/useImageCrop.test.ts b/src/composables/useImageCrop.test.ts index 3ce672d1292..6fabaf75330 100644 --- a/src/composables/useImageCrop.test.ts +++ b/src/composables/useImageCrop.test.ts @@ -10,6 +10,10 @@ import { useCanvasStore } from '@/renderer/core/canvas/canvasStore' import WidgetImageCrop from '@/components/imagecrop/WidgetImageCrop.vue' import type { LGraphCanvas, LGraphNode } from '@/lib/litegraph/src/litegraph' +import { + createTestSubgraph, + createTestSubgraphNode +} from '@/lib/litegraph/src/subgraph/__fixtures__/subgraphHelpers' import { useLinkStore } from '@/stores/linkStore' import { graphScopeOf } from '@comfyorg/graph-model/graphScopeId' import { toLinkId } from '@comfyorg/graph-model/linkId' @@ -240,6 +244,7 @@ describe('useImageCrop', () => { }) cropNode = createMockLGraphNode({ id: 2, + inputs: [{ name: 'image', type: 'IMAGE' }], getInputNode: vi.fn(() => sourceNode), getInputLink: vi.fn(() => ({ origin_slot: 0 })), isSubgraphNode: () => false @@ -283,14 +288,11 @@ describe('useImageCrop', () => { }) } - /** - * Resolves the image input through the link store, the way - * `LGraphNode.getInputLink` does, so link edits drive the preview. - */ - function useLinkedCropNode() { + function useLinkedCropNode(inputs = [{ name: 'image', type: 'IMAGE' }]) { const linkStore = useLinkStore() cropNode = createMockLGraphNode({ id: CROP_NODE_ID, + inputs, isSubgraphNode: () => false, getInputLink: (slot: number) => linkStore.getInputSlotLink(GRAPH_SCOPE, CROP_NODE_ID, slot) ?? null, @@ -302,15 +304,15 @@ describe('useImageCrop', () => { mockResolveNode.mockReturnValue(cropNode) } - function connectImageInput() { + function connectImageInput(targetSlot = 0, type = 'IMAGE') { useLinkStore().registerLink(GRAPH_SCOPE, { id: toLinkId(1), graphId: GRAPH_SCOPE.owningGraphId, originNodeId: toNodeId(99), originSlot: 0, targetNodeId: CROP_NODE_ID, - targetSlot: 0, - type: 'IMAGE' + targetSlot, + type }) } @@ -336,6 +338,7 @@ describe('useImageCrop', () => { it('returns null image URL when there is no input node', async () => { const alone = createMockLGraphNode({ id: 2, + inputs: [{ name: 'image', type: 'IMAGE' }], getInputNode: vi.fn(() => null), getInputLink: vi.fn(), isSubgraphNode: () => false @@ -352,6 +355,7 @@ describe('useImageCrop', () => { }) const sgCrop = createMockLGraphNode({ id: 2, + inputs: [{ name: 'image', type: 'IMAGE' }], getInputNode: vi.fn(() => subgraphInput), getInputLink: vi.fn(() => null), isSubgraphNode: () => false @@ -373,6 +377,7 @@ describe('useImageCrop', () => { const sgCrop = createMockLGraphNode({ id: 2, + inputs: [{ name: 'image', type: 'IMAGE' }], getInputNode: vi.fn(() => subgraphInput), getInputLink: vi.fn(() => ({ origin_slot: 0 })), isSubgraphNode: () => false @@ -420,6 +425,7 @@ describe('useImageCrop', () => { mockResolveNode.mockReturnValue( createMockLGraphNode({ id: 2, + inputs: [{ name: 'image', type: 'IMAGE' }], getInputNode: vi.fn(() => reloadedSource), getInputLink: vi.fn(() => ({ origin_slot: 0 })), isSubgraphNode: () => false @@ -446,6 +452,72 @@ describe('useImageCrop', () => { expect(vm.imageUrl).toBe('https://example.com/a.png') }) + it('shows the image connected after mount to a first input not typed IMAGE', async () => { + useLinkedCropNode([{ name: 'image', type: '*' }]) + const vm = await mountHarness() + + connectImageInput(0) + await flushTicks() + + expect(vm.imageUrl).toBe('https://example.com/a.png') + }) + + it('shows the upstream image when the image input is not the first input, as after reopening a dynamic-input node', async () => { + useLinkedCropNode([ + { name: 'model', type: 'COMFY_DYNAMICCOMBO_V3' }, + { name: 'model.images.image_1', type: 'IMAGE' }, + { name: 'model.images.image_2', type: 'IMAGE' }, + { name: 'model.region', type: 'BOUNDING_BOX' } + ]) + connectImageInput(1) + + const vm = await mountHarness() + + expect(vm.imageUrl).toBe('https://example.com/a.png') + }) + + it('shows the image from a union input before a later IMAGE input', async () => { + useLinkedCropNode([ + { name: 'image', type: 'IMAGE,MASK' }, + { name: 'reference', type: 'IMAGE' } + ]) + connectImageInput(0) + + const vm = await mountHarness() + + expect(vm.imageUrl).toBe('https://example.com/a.png') + }) + + it('shows an empty preview on a subgraph host with a later image input', async () => { + const subgraph = createTestSubgraph({ + inputs: [ + { name: 'prompt', type: 'STRING' }, + { name: 'image', type: 'IMAGE' } + ], + outputs: [{ name: 'out', type: 'IMAGE' }] + }) + const host = createTestSubgraphNode(subgraph, { id: 2 }) + mockResolveNode.mockReturnValue(host) + + const vm = await mountHarness() + + expect(vm.imageUrl).toBeNull() + }) + + it('shows the image connected to a custom input type through the slot-0 fallback', async () => { + sourceNode = createMockLGraphNode({ + id: 99, + isSubgraphNode: () => false, + outputs: [{ name: 'image', type: 'CUSTOM_IMAGE' }] + }) + useLinkedCropNode([{ name: 'image', type: 'CUSTOM_IMAGE' }]) + connectImageInput(0, 'CUSTOM_IMAGE') + + const vm = await mountHarness() + + expect(vm.imageUrl).toBe('https://example.com/a.png') + }) + it('clears the preview when the image input is disconnected', async () => { useLinkedCropNode() connectImageInput() @@ -790,6 +862,7 @@ describe('WidgetImageCrop', () => { const source = createMockLGraphNode({ id: 99, isSubgraphNode: () => false }) const crop = createMockLGraphNode({ id: 2, + inputs: [{ name: 'image', type: 'IMAGE' }], getInputNode: vi.fn(() => source), getInputLink: vi.fn(), isSubgraphNode: () => false diff --git a/src/composables/useImageCrop.ts b/src/composables/useImageCrop.ts index 45beee1beda..9e468aa9a90 100644 --- a/src/composables/useImageCrop.ts +++ b/src/composables/useImageCrop.ts @@ -3,6 +3,8 @@ import type { Ref } from 'vue' import { computed, ref, watch } from 'vue' import { useLiveGraphNode } from '@/composables/useLiveGraphNode' +import type { LGraphNode } from '@/lib/litegraph/src/litegraph' +import { LiteGraph } from '@/lib/litegraph/src/litegraph' import type { Bounds } from '@comfyorg/shared-frontend-utils/geometry' import { useNodeOutputStore } from '@/stores/nodeOutputStore' import type { NodeId } from '@comfyorg/graph-model/nodeId' @@ -37,8 +39,6 @@ const CORNER_DOT_CLASS = /** Minimum crop width/height in source image pixel space. */ const MIN_CROP_SIZE = 16 const CROP_BOX_BORDER = 1 -/** Slot the cropped image arrives on. */ -const IMAGE_INPUT_SLOT = 0 /** * Next `isLoading` when `imageUrl` transitions. `null` means do not change @@ -69,6 +69,15 @@ interface UseImageCropOptions { onEditEnd?: () => void } +function imageInputSlot(node: LGraphNode): number { + if (node.isSubgraphNode()) return 0 + + const slot = node.inputs.findIndex((input) => + LiteGraph.isValidConnection('IMAGE', input.type) + ) + return slot === -1 ? 0 : slot +} + export function useImageCrop(nodeId: NodeId, options: UseImageCropOptions) { const { imageEl, containerEl, modelValue, onEditEnd } = options const nodeOutputStore = useNodeOutputStore() @@ -189,7 +198,10 @@ export function useImageCrop(nodeId: NodeId, options: UseImageCropOptions) { const cropNode = node.value if (!cropNode) return null - const sourceNode = resolveInputSourceNode(cropNode, IMAGE_INPUT_SLOT) + const sourceNode = resolveInputSourceNode( + cropNode, + imageInputSlot(cropNode) + ) if (!sourceNode) return null return nodeOutputStore.getNodeImageUrls(sourceNode)?.[0] ?? null