diff --git a/src/composables/maskeditor/useMaskEditorSaver.test.ts b/src/composables/maskeditor/useMaskEditorSaver.test.ts index cc7c626dad6..4057473a134 100644 --- a/src/composables/maskeditor/useMaskEditorSaver.test.ts +++ b/src/composables/maskeditor/useMaskEditorSaver.test.ts @@ -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) diff --git a/src/composables/maskeditor/useMaskEditorSaver.ts b/src/composables/maskeditor/useMaskEditorSaver.ts index 5133af59f0b..89189389c08 100644 --- a/src/composables/maskeditor/useMaskEditorSaver.ts +++ b/src/composables/maskeditor/useMaskEditorSaver.ts @@ -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' @@ -278,6 +277,8 @@ export function useMaskEditorSaver() { node: LGraphNode, outputData: EditorOutputData ): void { + if (!node.graph) return + const mainRef = outputData.paintedMaskedImage.ref writeImageWidgetValue( @@ -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) } diff --git a/src/stores/nodeOutputStore.test.ts b/src/stores/nodeOutputStore.test.ts index 76391d6bd48..d3328742c69 100644 --- a/src/stores/nodeOutputStore.test.ts +++ b/src/stores/nodeOutputStore.test.ts @@ -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 = {} diff --git a/src/stores/nodeOutputStore.ts b/src/stores/nodeOutputStore.ts index bc3c37c5ebd..caf26fd5c5a 100644 --- a/src/stores/nodeOutputStore.ts +++ b/src/stores/nodeOutputStore.ts @@ -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 + ) { + 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, @@ -508,6 +528,7 @@ export const useNodeOutputStore = defineStore('nodeOutput', () => { setNodeOutputs, setNodeOutputImages, + replaceNodeOutputImages, setNodeOutputsByExecutionId, setNodePreviewsByExecutionId, setNodePreviewsByLocatorId,