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
40 changes: 40 additions & 0 deletions src/composables/maskeditor/useMaskEditorSaver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,46 @@ describe('useMaskEditorSaver', () => {
)
})

it('replaces a stale clipspace image with the saved image', async () => {
mockNode.images = [
{
filename: 'pasted-before-edit.png',
subfolder: 'clipspace',
type: 'input'
}
]

await useMaskEditorSaver().save()

expect(mockNode.images).toEqual([
{
filename: 'clipspace-painted-masked-123.png',
subfolder: 'clipspace',
type: 'input'
}
])
})

it('does not write server references after the source node is detached', async () => {
vi.mocked(api.fetchApi).mockImplementation(async () => {
mockNode.graph = null
return {
ok: true,
json: () =>
Promise.resolve({
name: 'clipspace-painted-masked-123.png',
subfolder: 'clipspace',
type: 'input'
})
} as Response
})

await useMaskEditorSaver().save()

expect(useNodeOutputStore().nodeOutputs['42']).toBeUndefined()
expect(mockNode.widgets?.[0].value).toBe('original.png [input]')
})

it('omits subfolder from the upload FormData under the unified contract', async () => {
const fetchApiMock = vi.mocked(api.fetchApi)

Expand Down
15 changes: 9 additions & 6 deletions src/composables/maskeditor/useMaskEditorSaver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import type {
} from '@/stores/maskEditorDataStore'
import { api } from '@/scripts/api'
import { app } from '@/scripts/app'
import { createAnnotatedPath } from '@/utils/createAnnotatedPath'
import { encodeRgbaAsPng } from '@/utils/pngEncodeUtil'
import { isResultItemType } from '@/utils/typeGuardUtil'

Expand Down Expand Up @@ -278,6 +277,8 @@ export function useMaskEditorSaver() {
node: LGraphNode,
outputData: EditorOutputData
): void {
if (!node.graph) return

const mainRef = outputData.paintedMaskedImage.ref

writeImageWidgetValue(
Expand All @@ -286,11 +287,13 @@ export function useMaskEditorSaver() {
)

node.imgs = undefined
const annotatedPath = createAnnotatedPath(mainRef.filename, {
subfolder: mainRef.subfolder,
rootFolder: isResultItemType(mainRef.type) ? mainRef.type : undefined
})
nodeOutputStore.setNodeOutputs(node, annotatedPath, { folder: 'input' })
nodeOutputStore.replaceNodeOutputImages(node, [
{
filename: mainRef.filename,
subfolder: mainRef.subfolder,
type: isResultItemType(mainRef.type) ? mainRef.type : 'input'
}
])
node.graph?.setDirtyCanvas(true)
}

Expand Down
60 changes: 60 additions & 0 deletions src/stores/nodeOutputStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,66 @@ describe('nodeOutputStore legacy entry synchronization', () => {
})
})

describe('nodeOutputStore replaceNodeOutputImages', () => {
beforeEach(() => {
app.nodeOutputs = {}
app.nodePreviewImages = {}
})

it('drops the previous output metadata when replacing the images', () => {
const store = useNodeOutputStore()
const node = createMockNode({ id: 7 })
store.setOutputFromLegacy(
'7',
fromAny({
images: [{ filename: 'previous.webp' }],
animated: [true],
video: [{ filename: 'previous.mp4' }]
})
)

const images = [
{
filename: 'painted.png',
subfolder: 'clipspace',
type: 'input' as const
}
]
store.replaceNodeOutputImages(node, images)

expect(store.nodeOutputs['7']?.animated).toBeUndefined()
expect(store.nodeOutputs['7']?.video).toBeUndefined()
expect(store.nodeOutputs['7']?.images).toEqual(images)
})

it('ignores an empty replacement', () => {
const store = useNodeOutputStore()
const images = [{ filename: 'previous.png', type: 'input' as const }]
const node = createMockNode({ id: 7, images })
store.setOutputFromLegacy('7', { images })

store.replaceNodeOutputImages(node, [])

expect(store.nodeOutputs['7']?.images).toEqual(images)
expect(node.images).toEqual(images)
})

it('removes stale previews when replacing the images', () => {
const store = useNodeOutputStore()
const node = createMockNode({ id: 7 })
store.setNodePreviewsByLocatorId(createNodeLocatorId(null, node.id), [
'preview:stale'
])

store.replaceNodeOutputImages(node, [
{ filename: 'painted.png', type: 'input' }
])

expect(store.getNodePreviews(node)).toBeUndefined()
expect(app.nodePreviewImages['7']).toBeUndefined()
})
})

describe('nodeOutputStore restoreOutputs', () => {
beforeEach(() => {
app.nodeOutputs = {}
Expand Down
21 changes: 21 additions & 0 deletions src/stores/nodeOutputStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,26 @@ export const useNodeOutputStore = defineStore('nodeOutput', () => {
node.images = images
}

/**
* Replaces a node's output with exactly these images, dropping any media
* metadata the previous output carried. `animated`, `video` and friends are
* parallel to the images they were produced with, so an editor that rewrites
* the image list wholesale must not inherit them.
*/
function replaceNodeOutputImages(
node: LGraphNode,
images: NonNullable<ExecutedWsMessage['output']['images']>
) {
if (!images.length) return

const locatorId = nodeToNodeLocatorId(node)
if (!locatorId) return

revokePreviewsByLocatorId(locatorId)
setOutputsByLocatorId(locatorId, { images })
node.images = images
}

function setNodeOutputsByExecutionId(
executionId: NodeExecutionId,
outputs: ExecutedWsMessage['output'] | ResultItem,
Expand Down Expand Up @@ -508,6 +528,7 @@ export const useNodeOutputStore = defineStore('nodeOutput', () => {

setNodeOutputs,
setNodeOutputImages,
replaceNodeOutputImages,
setNodeOutputsByExecutionId,
setNodePreviewsByExecutionId,
setNodePreviewsByLocatorId,
Expand Down
Loading