Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor: move node.imgs sync from component to store
Move syncNodeImgs logic from ImagePreview.vue to imagePreviewStore.ts
for proper separation of concerns. Store handles backwards compatibility
sync when setting node outputs.
  • Loading branch information
christian-byrne committed Dec 13, 2025
commit 9a9b3f286bd92f8677b174a8ed8c17c090e8c51e
13 changes: 0 additions & 13 deletions src/renderer/extensions/vueNodes/components/ImagePreview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ import { computed, ref, watch } from 'vue'
import { useI18n } from 'vue-i18n'

import { downloadFile } from '@/base/common/downloadUtil'
import { app } from '@/scripts/app'
import { useCommandStore } from '@/stores/commandStore'
import { useNodeOutputStore } from '@/stores/imagePreviewStore'

Expand Down Expand Up @@ -188,17 +187,6 @@ watch(
{ deep: true, immediate: true }
)

/**
* Sync node.imgs for backwards compatibility with legacy systems (e.g., Copy Image).
*/
const syncNodeImgs = () => {
if (!props.nodeId || !currentImageEl.value) return
const node = app.rootGraph?.getNodeById(props.nodeId)
if (!node) return
node.imgs = [currentImageEl.value]
node.imageIndex = currentIndex.value
}

// Event handlers
const handleImageLoad = (event: Event) => {
if (!event.target || !(event.target instanceof HTMLImageElement)) return
Expand All @@ -209,7 +197,6 @@ const handleImageLoad = (event: Event) => {
if (img.naturalWidth && img.naturalHeight) {
actualDimensions.value = `${img.naturalWidth} x ${img.naturalHeight}`
}
syncNodeImgs()
}

const handleImageError = () => {
Expand Down
23 changes: 22 additions & 1 deletion src/stores/imagePreviewStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ interface SetOutputOptions {
}

export const useNodeOutputStore = defineStore('nodeOutput', () => {
const { nodeIdToNodeLocatorId, nodeToNodeLocatorId } = useWorkflowStore()
const { nodeIdToNodeLocatorId, nodeToNodeLocatorId, nodeLocatorIdToNodeId } =
useWorkflowStore()
const { executionIdToNodeLocatorId } = useExecutionStore()
const scheduledRevoke: Record<NodeLocatorId, { stop: () => void }> = {}
const latestOutput = ref<string[]>([])
Expand Down Expand Up @@ -156,6 +157,26 @@ export const useNodeOutputStore = defineStore('nodeOutput', () => {
}) ?? []
app.nodeOutputs[nodeLocatorId] = outputs
nodeOutputs.value[nodeLocatorId] = outputs

syncNodeImgs(nodeLocatorId, latestOutput.value)
}

/**
* Sync node.imgs for backwards compatibility with legacy systems (e.g., Copy Image).
*/
function syncNodeImgs(nodeLocatorId: NodeLocatorId, imageUrls: string[]) {
if (!imageUrls.length) return
const nodeId = nodeLocatorIdToNodeId(nodeLocatorId)
if (nodeId === null) return
const node = app.canvas?.graph?.getNodeById(nodeId)
if (!node) return

const img = new Image()
img.onload = () => {
node.imgs = [img]
node.imageIndex = 0
}
img.src = imageUrls[0]
}

function setNodeOutputs(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createTestingPinia } from '@pinia/testing'
import type { VueWrapper } from '@vue/test-utils'
import { mount } from '@vue/test-utils'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { describe, expect, it, vi } from 'vitest'
import { nextTick } from 'vue'
import { createI18n } from 'vue-i18n'

Expand All @@ -13,22 +13,6 @@ vi.mock('@/base/common/downloadUtil', () => ({
downloadFile: vi.fn()
}))

// Mock app for node.imgs sync tests
const mockNode: {
imgs?: HTMLImageElement[] | undefined
imageIndex?: number | null
} = {}
vi.mock('@/scripts/app', () => ({
app: {
rootGraph: {
getNodeById: vi.fn(() => mockNode)
},
canvas: {
select: vi.fn()
}
}
}))

const i18n = createI18n({
legacy: false,
locale: 'en',
Expand Down Expand Up @@ -60,11 +44,6 @@ describe('ImagePreview', () => {
}
const wrapperRegistry = new Set<VueWrapper>()

beforeEach(() => {
delete mockNode.imgs
delete mockNode.imageIndex
})

const mountImagePreview = (props = {}) => {
const wrapper = mount(ImagePreview, {
props: { ...defaultProps, ...props },
Expand Down Expand Up @@ -329,21 +308,4 @@ describe('ImagePreview', () => {
expect(imgElement.exists()).toBe(true)
expect(imgElement.attributes('alt')).toBe('Node output 2')
})

it('syncs node.imgs on image load for legacy compatibility', async () => {
const wrapper = mountImagePreview({
imageUrls: [defaultProps.imageUrls[0]],
nodeId: 'test-node-123'
})

const img = wrapper.find('img')
expect(img.exists()).toBe(true)

await img.trigger('load')
await nextTick()

expect(mockNode.imgs).toHaveLength(1)
expect(mockNode.imgs?.[0]).toBe(img.element)
expect(mockNode.imageIndex).toBe(0)
})
})
Loading