Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"id": "9a37f747-e96b-4304-9212-7abcaad7bdac",
"revision": 0,
"last_node_id": 5,
"last_link_id": 5,
"last_node_id": 4462758126524329,
"last_link_id": 7729209487955825,
"nodes": [
{
"id": 5,
Expand Down
40 changes: 40 additions & 0 deletions browser_tests/tests/copyPaste.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,46 @@ test.describe('Copy Paste', { tag: ['@screenshot', '@workflow'] }, () => {
})
})

test('Sparse clipboard widget values keep their original indices', async ({
comfyPage
}) => {
const originalNodes = await comfyPage.nodeOps.getNodeRefsByType('KSampler')
const originalIds = new Set(originalNodes.map(({ id }) => id))

await comfyPage.page.evaluate(() => {
const node = window
.app!.graph.serialize()
.nodes.find(({ type }) => type === 'KSampler')
if (!node) throw new Error('KSampler node not found')
const clipboardNode = {
...node,
widgets_values: { 0: 123, 2: 47, length: 3 }
}
const encoded = btoa(JSON.stringify({ nodes: [clipboardNode] }))
const dataTransfer = new DataTransfer()
dataTransfer.setData(
'text/html',
`<meta charset="utf-8"><div><span data-comfy-metadata="${encoded}"></span></div><span style="white-space:pre-wrap;">Text</span>`
)
document.dispatchEvent(
new ClipboardEvent('paste', {
clipboardData: dataTransfer,
bubbles: true,
cancelable: true
})
)
})

await expect
.poll(async () => {
const nodes = await comfyPage.nodeOps.getNodeRefsByType('KSampler')
const pasted = nodes.find(({ id }) => !originalIds.has(id))
if (!pasted) return undefined
return await (await pasted.getWidget(2)).getValue()
})
.toBe(47)
})

test('Can undo paste multiple nodes as single action', async ({
comfyPage
}) => {
Expand Down
5 changes: 0 additions & 5 deletions eslint-suppressions.json
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,6 @@
"count": 1
}
},
"src/lib/litegraph/src/subgraph/subgraphDeduplication.ts": {
"comfy/no-new-error-throw": {
"count": 1
}
},
"src/platform/assets/services/assetService.ts": {
"comfy/no-new-error-throw": {
"count": 21
Expand Down
2 changes: 1 addition & 1 deletion src/composables/useCopy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function selectDocumentText(selectedCharacters: number): void {
function readSerializedClipboardMetadata(dataTransfer: DataTransfer): string {
const match = dataTransfer
.getData('text/html')
.match(/data-metadata="([A-Za-z0-9+/=]+)"/)?.[1]
.match(/data-comfy-metadata="([A-Za-z0-9+/=]+)"/)?.[1]
expect(match).toBeDefined()
if (!match) throw new Error('Expected clipboard metadata to be written')

Expand Down
2 changes: 1 addition & 1 deletion src/composables/useCopy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const CANVAS_CLIPBOARD_KEY = 'litegrapheditor_clipboard'
const CANVAS_CLIPBOARD_ID_KEY = 'litegrapheditor_clipboard_id'

const clipboardHTMLWrapper = [
'<meta charset="utf-8"><div><span data-metadata="',
'<meta charset="utf-8"><div><span data-comfy-metadata="',
'"></span></div><span style="white-space:pre-wrap;">Text</span>'
]
const clipboardByteChunkSize = 0x8000
Expand Down
144 changes: 139 additions & 5 deletions src/composables/usePaste.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
LGraphNode
} from '@/lib/litegraph/src/litegraph'
import { app } from '@/scripts/app'
import { useToastStore } from '@/platform/updates/common/toastStore'
import { createMockLGraphNode } from '@/utils/__tests__/litegraphTestUtils'
import { createNode } from '@/utils/litegraphUtil'
import {
Expand Down Expand Up @@ -68,6 +69,11 @@ function createDataTransfer(files: File[] = []): DataTransfer {
return dataTransfer
}

function clipboardHtml(data: unknown, attribute = 'data-comfy-metadata') {
const encoded = btoa(JSON.stringify(data))
return `<meta charset="utf-8"><div><span ${attribute}="${encoded}"></span></div><span style="white-space:pre-wrap;">Text</span>`
}

const mockCanvas = {
current_node: null as LGraphNode | null,
graph: {
Expand Down Expand Up @@ -621,9 +627,14 @@ describe('usePaste', () => {
})

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>`
const data = {
nodes: [],
groups: [],
reroutes: [],
links: [],
subgraphs: []
}
const html = clipboardHtml(data)

usePaste()

Expand All @@ -641,6 +652,130 @@ describe('usePaste', () => {
})
})

it('accepts validated legacy data-metadata clipboard items', async () => {
const data = { nodes: [] }
const html = clipboardHtml(data, 'data-metadata')

usePaste()

const dataTransfer = new DataTransfer()
dataTransfer.setData('text/html', html)
dataTransfer.setData('text/plain', 'some text')

const event = new ClipboardEvent('paste', { clipboardData: dataTransfer })
document.dispatchEvent(event)

await vi.waitFor(() => {
expect(mockCanvas._deserializeItems).toHaveBeenCalledWith(
data,
expect.any(Object)
)
})
})

it('does not treat metadata embedded in arbitrary HTML as a Comfy clipboard', async () => {
const encoded = btoa(JSON.stringify({ nodes: [] }))
const html = `<article><span data-comfy-metadata="${encoded}"></span></article>`

usePaste()
const dataTransfer = new DataTransfer()
dataTransfer.setData('text/html', html)
document.dispatchEvent(
new ClipboardEvent('paste', { clipboardData: dataTransfer })
)

await vi.waitFor(() => {
expect(mockCanvas._deserializeItems).not.toHaveBeenCalled()
expect(mockCanvas.pasteFromClipboard).toHaveBeenCalled()
})
})

it.for([
{ name: 'null payload', data: null },
{ name: 'empty object', data: {} },
{ name: 'malformed node', data: { nodes: [{ type: 'KSampler' }] } },
{ name: 'malformed group', data: { groups: [{ id: 1 }] } },
{ name: 'malformed reroute', data: { reroutes: [{ id: 1 }] } },
{ name: 'malformed link', data: { links: [{ id: 1 }] } },
{ name: 'malformed subgraph', data: { subgraphs: [{ id: 'invalid' }] } }
])(
'rejects malformed Comfy metadata without stale fallback: $name',
async ({ data }) => {
const html = clipboardHtml(data)

usePaste()

const dataTransfer = new DataTransfer()
dataTransfer.setData('text/html', html)
dataTransfer.setData('text/plain', 'some text')

document.dispatchEvent(
new ClipboardEvent('paste', { clipboardData: dataTransfer })
)

await vi.waitFor(() => {
expect(mockCanvas._deserializeItems).not.toHaveBeenCalled()
expect(mockCanvas.pasteFromClipboard).not.toHaveBeenCalled()
expect(useToastStore().add).toHaveBeenCalledWith(
expect.objectContaining({ severity: 'error' })
)
})
}
)

it('rejects invalid Comfy JSON without stale fallback', async () => {
const html = `<meta charset="utf-8"><div><span data-comfy-metadata="${btoa('{')}"></span></div><span style="white-space:pre-wrap;">Text</span>`

usePaste()

const dataTransfer = new DataTransfer()
dataTransfer.setData('text/html', html)
dataTransfer.setData('text/plain', 'some text')

document.dispatchEvent(
new ClipboardEvent('paste', { clipboardData: dataTransfer })
)

await vi.waitFor(() => {
expect(mockCanvas._deserializeItems).not.toHaveBeenCalled()
expect(mockCanvas.pasteFromClipboard).not.toHaveBeenCalled()
expect(useToastStore().add).toHaveBeenCalledWith(
expect.objectContaining({ severity: 'error' })
)
})
})

it('should toast a deserialization error without falling back', async () => {
const deserializeError = new Error('Paste failed')
vi.mocked(mockCanvas._deserializeItems).mockImplementation(() => {
throw deserializeError
})
const data = {
nodes: [],
groups: [],
reroutes: [],
links: [],
subgraphs: []
}
const html = clipboardHtml(data)

usePaste()

const dataTransfer = new DataTransfer()
dataTransfer.setData('text/html', html)
dataTransfer.setData('text/plain', 'some text')

const event = new ClipboardEvent('paste', { clipboardData: dataTransfer })
document.dispatchEvent(event)

await vi.waitFor(() => {
expect(useToastStore().add).toHaveBeenCalledWith(
expect.objectContaining({ severity: 'error' })
)
expect(mockCanvas.pasteFromClipboard).not.toHaveBeenCalled()
})
})

it('should skip node metadata paste when a media node is selected', async () => {
const mockNode = createMockLGraphNode({
is_selected: true,
Expand All @@ -653,8 +788,7 @@ describe('usePaste', () => {
usePaste()

const nodeData = { nodes: [{ type: 'KSampler' }] }
const encoded = btoa(JSON.stringify(nodeData))
const html = `<div data-metadata="${encoded}"></div>`
const html = clipboardHtml(nodeData)

const dataTransfer = new DataTransfer()
dataTransfer.setData('text/html', html)
Expand Down
27 changes: 22 additions & 5 deletions src/composables/usePaste.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useEventListener } from '@vueuse/core'

import { useErrorHandling } from '@/composables/useErrorHandling'
import type { LGraphCanvas, LGraphNode } from '@/lib/litegraph/src/litegraph'
import { zClipboardItems } from '@/platform/workflow/validation/schemas/workflowSchema'
import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
import { app } from '@/scripts/app'
import { useWorkspaceStore } from '@/stores/workspaceStore'
Expand Down Expand Up @@ -42,19 +44,34 @@ export function cloneDataTransfer(original: DataTransfer): DataTransfer {

function pasteClipboardItems(data: DataTransfer): boolean {
const rawData = data.getData('text/html')
const match = rawData.match(/data-metadata="([A-Za-z0-9+/=]+)"/)?.[1]
const match = rawData.match(
/^<meta charset="utf-8"><div><span data-(?:comfy-)?metadata="([A-Za-z0-9+/=]+)"><\/span><\/div><span style="white-space:pre-wrap;">Text<\/span>$/
Comment thread
christian-byrne marked this conversation as resolved.
Resolved
)?.[1]
if (!match) return false

let parsed: unknown
try {
// Decode UTF-8 safe base64
const binaryString = atob(match)
const bytes = Uint8Array.from(binaryString, (c) => c.charCodeAt(0))
const decodedData = new TextDecoder().decode(bytes)
useCanvasStore().getCanvas()._deserializeItems(JSON.parse(decodedData), {})
parsed = JSON.parse(decodedData)
} catch (err) {
useErrorHandling().toastErrorHandler(err)
return true
}

const clipboardItems = zClipboardItems.safeParse(parsed)
if (!clipboardItems.success) {
useErrorHandling().toastErrorHandler(clipboardItems.error)
return true
}

try {
useCanvasStore().getCanvas()._deserializeItems(clipboardItems.data, {})
} catch (err) {
console.error(err)
useErrorHandling().toastErrorHandler(err)
}
return false
return true
}

function isWorkflow(
Expand Down
23 changes: 22 additions & 1 deletion src/extensions/core/groupNode.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fromAny, fromPartial } from '@total-typescript/shoehorn'
import { describe, expect, it, vi } from 'vitest'
import { describe, expect, it, test, vi } from 'vitest'

import { t } from '@/i18n'

Expand Down Expand Up @@ -134,6 +134,27 @@ describe('GroupNodeConfig.getLinks', () => {
const config = configFrom([], [[0, 1, 'IMAGE']])
expect(config.externalFrom).toEqual({ 0: { 1: 'IMAGE' } })
})

it('normalizes a missing primitive link type to null', () => {
const linkWithoutType = [0, 0, 1, 0, 1] satisfies GroupNodeLink
const config = configFrom([linkWithoutType])

expect(
config.getNodeDef({ index: 0, type: 'PrimitiveNode' })?.output
).toEqual([null])
})

test.for([{ unexpected: true }, ['IMAGE', 42]])(
Comment thread
christian-byrne marked this conversation as resolved.
Resolved
'normalizes an invalid primitive link type to null',
(invalidType) => {
const link = [0, 0, 1, 0, 1, invalidType] satisfies GroupNodeLink
const config = configFrom([link])

expect(
config.getNodeDef({ index: 0, type: 'PrimitiveNode' })?.output
).toEqual([null])
}
)
})

describe('findUnconsumedWidgetIndex', () => {
Expand Down
Loading
Loading