-
Notifications
You must be signed in to change notification settings - Fork 461
Drag image to load image #7898
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JemiloII
wants to merge
18
commits into
Comfy-Org:main
Choose a base branch
from
JemiloII:drag-image-to-load-image
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+369
−34
Open
Drag image to load image #7898
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
e968078
Dragging non-workflow image will create a load image node
JemiloII 99da170
Merge branch 'main' into drag-image-to-load-image
JemiloII 2c84558
Merge branch 'main' into drag-image-to-load-image
JemiloII 677f4c6
Add tests for usePaste.ts
JemiloII d8b9b47
Updated logic so workflow images work as expected.
JemiloII 00b4e34
Use vi.waitFor()
JemiloII 5ae8ad9
Removed log
JemiloII 1cffc71
Code Rabbit Review Updates
JemiloII c313d44
Merge branch 'main' into drag-image-to-load-image
JemiloII e7e846d
Updating more types in the tests for Code Rabbit
JemiloII 35ba601
Merge branch 'main' into drag-image-to-load-image
JemiloII 692d8c7
Merge branch 'main' into drag-image-to-load-image
JemiloII 0c5052c
Merge branch 'main' into drag-image-to-load-image
JemiloII ef42683
Merge remote-tracking branch 'forked/drag-image-to-load-image' into d…
JemiloII 0a9ffe5
Updating types
JemiloII f08e3b7
Created factories, updated mp3 meme type, and vi.waitFor
JemiloII 3927221
Merge branch 'main' into drag-image-to-load-image
JemiloII 3b15007
Merge branch 'main' into drag-image-to-load-image
JemiloII File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,314 @@ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import type { | ||
| LGraphCanvas, | ||
| LGraph, | ||
| LGraphGroup, | ||
| LGraphNode | ||
| } from '@/lib/litegraph/src/litegraph' | ||
| import { LiteGraph } from '@/lib/litegraph/src/litegraph' | ||
| import { app } from '@/scripts/app' | ||
| import { isImageNode } from '@/utils/litegraphUtil' | ||
| import { pasteImageNode, usePaste } from './usePaste' | ||
|
|
||
| function createMockNode() { | ||
| return { | ||
| pos: [0, 0], | ||
| pasteFile: vi.fn(), | ||
| pasteFiles: vi.fn() | ||
| } | ||
| } | ||
|
|
||
| function createImageFile( | ||
| name: string = 'test.png', | ||
| type: string = 'image/png' | ||
| ): File { | ||
| return new File([''], name, { type }) | ||
| } | ||
|
|
||
| function createAudioFile( | ||
| name: string = 'test.mp3', | ||
| type: string = 'audio/mpeg' | ||
| ): File { | ||
| return new File([''], name, { type }) | ||
| } | ||
|
|
||
| function createDataTransfer(files: File[] = []): DataTransfer { | ||
| const dataTransfer = new DataTransfer() | ||
| files.forEach((file) => dataTransfer.items.add(file)) | ||
| return dataTransfer | ||
| } | ||
|
|
||
| const mockCanvas = { | ||
| current_node: null as LGraphNode | null, | ||
| graph: { | ||
| add: vi.fn(), | ||
| change: vi.fn() | ||
| } as Partial<LGraph> as LGraph, | ||
| graph_mouse: [100, 200], | ||
| pasteFromClipboard: vi.fn(), | ||
| _deserializeItems: vi.fn() | ||
| } as Partial<LGraphCanvas> as LGraphCanvas | ||
|
|
||
| const mockCanvasStore = { | ||
| canvas: mockCanvas, | ||
| getCanvas: vi.fn(() => mockCanvas) | ||
| } | ||
|
|
||
| const mockWorkspaceStore = { | ||
| shiftDown: false | ||
| } | ||
|
|
||
| vi.mock('@vueuse/core', () => ({ | ||
| useEventListener: vi.fn((target, event, handler) => { | ||
| target.addEventListener(event, handler) | ||
| return () => target.removeEventListener(event, handler) | ||
| }) | ||
| })) | ||
JemiloII marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| vi.mock('@/renderer/core/canvas/canvasStore', () => ({ | ||
| useCanvasStore: () => mockCanvasStore | ||
| })) | ||
|
|
||
| vi.mock('@/stores/workspaceStore', () => ({ | ||
| useWorkspaceStore: () => mockWorkspaceStore | ||
| })) | ||
|
|
||
| vi.mock('@/scripts/app', () => ({ | ||
| app: { | ||
| loadGraphData: vi.fn() | ||
| } | ||
| })) | ||
|
|
||
| vi.mock('@/lib/litegraph/src/litegraph', () => ({ | ||
| LiteGraph: { | ||
| createNode: vi.fn() | ||
| } | ||
| })) | ||
JemiloII marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| vi.mock('@/utils/litegraphUtil', () => ({ | ||
| isAudioNode: vi.fn(), | ||
| isImageNode: vi.fn(), | ||
| isVideoNode: vi.fn() | ||
| })) | ||
|
|
||
| vi.mock('@/workbench/eventHelpers', () => ({ | ||
| shouldIgnoreCopyPaste: vi.fn() | ||
| })) | ||
|
|
||
| describe('pasteImageNode', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| vi.mocked(mockCanvas.graph!.add).mockImplementation( | ||
| (node: LGraphNode | LGraphGroup) => node as LGraphNode | ||
| ) | ||
| }) | ||
|
|
||
| it('should create new LoadImage node when no image node provided', () => { | ||
| const mockNode = createMockNode() | ||
| vi.mocked(LiteGraph.createNode).mockReturnValue( | ||
| mockNode as unknown as LGraphNode | ||
| ) | ||
|
|
||
| const file = createImageFile() | ||
| const dataTransfer = createDataTransfer([file]) | ||
|
|
||
| pasteImageNode(mockCanvas as unknown as LGraphCanvas, dataTransfer.items) | ||
|
|
||
| expect(LiteGraph.createNode).toHaveBeenCalledWith('LoadImage') | ||
| expect(mockNode.pos).toEqual([100, 200]) | ||
| expect(mockCanvas.graph!.add).toHaveBeenCalledWith(mockNode) | ||
| expect(mockCanvas.graph!.change).toHaveBeenCalled() | ||
| expect(mockNode.pasteFile).toHaveBeenCalledWith(file) | ||
| }) | ||
JemiloII marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| it('should use existing image node when provided', () => { | ||
| const mockNode = createMockNode() | ||
| const file = createImageFile() | ||
| const dataTransfer = createDataTransfer([file]) | ||
|
|
||
| pasteImageNode( | ||
| mockCanvas as unknown as LGraphCanvas, | ||
| dataTransfer.items, | ||
| mockNode as unknown as LGraphNode | ||
| ) | ||
|
|
||
| expect(mockNode.pasteFile).toHaveBeenCalledWith(file) | ||
| expect(mockNode.pasteFiles).toHaveBeenCalledWith([file]) | ||
| }) | ||
|
|
||
| it('should handle multiple image files', () => { | ||
| const mockNode = createMockNode() | ||
| const file1 = createImageFile('test1.png') | ||
| const file2 = createImageFile('test2.jpg', 'image/jpeg') | ||
| const dataTransfer = createDataTransfer([file1, file2]) | ||
|
|
||
| pasteImageNode( | ||
| mockCanvas as unknown as LGraphCanvas, | ||
| dataTransfer.items, | ||
| mockNode as unknown as LGraphNode | ||
| ) | ||
|
|
||
| expect(mockNode.pasteFile).toHaveBeenCalledWith(file1) | ||
| expect(mockNode.pasteFiles).toHaveBeenCalledWith([file1, file2]) | ||
| }) | ||
|
|
||
| it('should do nothing when no image files present', () => { | ||
| const mockNode = createMockNode() | ||
| const dataTransfer = createDataTransfer() | ||
|
|
||
| pasteImageNode( | ||
| mockCanvas as unknown as LGraphCanvas, | ||
| dataTransfer.items, | ||
| mockNode as unknown as LGraphNode | ||
| ) | ||
|
|
||
| expect(mockNode.pasteFile).not.toHaveBeenCalled() | ||
| expect(mockNode.pasteFiles).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('should filter non-image items', () => { | ||
| const mockNode = createMockNode() | ||
| const imageFile = createImageFile() | ||
| const textFile = new File([''], 'test.txt', { type: 'text/plain' }) | ||
| const dataTransfer = createDataTransfer([textFile, imageFile]) | ||
|
|
||
| pasteImageNode( | ||
| mockCanvas as unknown as LGraphCanvas, | ||
| dataTransfer.items, | ||
| mockNode as unknown as LGraphNode | ||
| ) | ||
|
|
||
| expect(mockNode.pasteFile).toHaveBeenCalledWith(imageFile) | ||
| expect(mockNode.pasteFiles).toHaveBeenCalledWith([imageFile]) | ||
| }) | ||
| }) | ||
|
|
||
| describe('usePaste', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| mockCanvas.current_node = null | ||
| mockWorkspaceStore.shiftDown = false | ||
| vi.mocked(mockCanvas.graph!.add).mockImplementation( | ||
| (node: LGraphNode | LGraphGroup) => node as LGraphNode | ||
| ) | ||
| }) | ||
|
|
||
| it('should handle image paste', async () => { | ||
| const mockNode = createMockNode() | ||
| vi.mocked(LiteGraph.createNode).mockReturnValue( | ||
| mockNode as unknown as LGraphNode | ||
| ) | ||
|
|
||
| usePaste() | ||
|
|
||
| const file = createImageFile() | ||
| const dataTransfer = createDataTransfer([file]) | ||
| const event = new ClipboardEvent('paste', { clipboardData: dataTransfer }) | ||
| document.dispatchEvent(event) | ||
|
|
||
| await vi.waitFor(() => { | ||
| expect(LiteGraph.createNode).toHaveBeenCalledWith('LoadImage') | ||
| expect(mockNode.pasteFile).toHaveBeenCalledWith(file) | ||
| }) | ||
| }) | ||
|
|
||
| it('should handle audio paste', async () => { | ||
| const mockNode = createMockNode() | ||
| vi.mocked(LiteGraph.createNode).mockReturnValue( | ||
| mockNode as unknown as LGraphNode | ||
| ) | ||
|
|
||
| usePaste() | ||
|
|
||
| const file = createAudioFile() | ||
| const dataTransfer = createDataTransfer([file]) | ||
| const event = new ClipboardEvent('paste', { clipboardData: dataTransfer }) | ||
| document.dispatchEvent(event) | ||
|
|
||
| await vi.waitFor(() => { | ||
| expect(LiteGraph.createNode).toHaveBeenCalledWith('LoadAudio') | ||
| expect(mockNode.pasteFile).toHaveBeenCalledWith(file) | ||
| }) | ||
| }) | ||
|
|
||
| it('should handle workflow JSON paste', async () => { | ||
| const workflow = { version: '1.0', nodes: [], extra: {} } | ||
|
|
||
| usePaste() | ||
|
|
||
| const dataTransfer = new DataTransfer() | ||
| dataTransfer.setData('text/plain', JSON.stringify(workflow)) | ||
|
|
||
| const event = new ClipboardEvent('paste', { clipboardData: dataTransfer }) | ||
| document.dispatchEvent(event) | ||
|
|
||
| await vi.waitFor(() => { | ||
| expect(app.loadGraphData).toHaveBeenCalledWith(workflow) | ||
| }) | ||
| }) | ||
|
|
||
| it('should ignore paste when shift is down', () => { | ||
| mockWorkspaceStore.shiftDown = true | ||
|
|
||
| usePaste() | ||
|
|
||
| const file = createImageFile() | ||
| const dataTransfer = createDataTransfer([file]) | ||
| const event = new ClipboardEvent('paste', { clipboardData: dataTransfer }) | ||
| document.dispatchEvent(event) | ||
|
|
||
| expect(LiteGraph.createNode).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('should use existing image node when selected', () => { | ||
| const mockNode = { | ||
| is_selected: true, | ||
| pasteFile: vi.fn(), | ||
| pasteFiles: vi.fn() | ||
| } as unknown as Partial<LGraphNode> as LGraphNode | ||
| mockCanvas.current_node = mockNode | ||
| vi.mocked(isImageNode).mockReturnValue(true) | ||
|
|
||
| usePaste() | ||
|
|
||
| const file = createImageFile() | ||
| const dataTransfer = createDataTransfer([file]) | ||
| const event = new ClipboardEvent('paste', { clipboardData: dataTransfer }) | ||
| document.dispatchEvent(event) | ||
|
|
||
| expect(mockNode.pasteFile).toHaveBeenCalledWith(file) | ||
| }) | ||
JemiloII marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| it('should call canvas pasteFromClipboard for non-workflow text', () => { | ||
| usePaste() | ||
|
|
||
| const dataTransfer = new DataTransfer() | ||
| dataTransfer.setData('text/plain', 'just some text') | ||
|
|
||
| const event = new ClipboardEvent('paste', { clipboardData: dataTransfer }) | ||
| document.dispatchEvent(event) | ||
|
|
||
| expect(mockCanvas.pasteFromClipboard).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('should handle clipboard items with metadata', async () => { | ||
| const data = { test: 'data' } | ||
| const encoded = btoa(JSON.stringify(data)) | ||
| const html = `<div data-metadata="${encoded}"></div>` | ||
|
|
||
| usePaste() | ||
|
|
||
| const dataTransfer = new DataTransfer() | ||
| dataTransfer.setData('text/html', html) | ||
|
|
||
| const event = new ClipboardEvent('paste', { clipboardData: dataTransfer }) | ||
| document.dispatchEvent(event) | ||
|
|
||
| await vi.waitFor(() => { | ||
| expect(mockCanvas._deserializeItems).toHaveBeenCalledWith( | ||
| data, | ||
| expect.any(Object) | ||
| ) | ||
| }) | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.