Compare commits

...

1 Commits

Author SHA1 Message Date
f34b857cd2 feat(uploads): let users remove an uploaded image
Once uploaded, an asset could never be removed from the Uploads panel —
setUploads was only ever called with prepend-style updates, so a wrong
upload stuck around forever (cluttering the panel and the project file
with no way back). Adds a Remove button per upload tile, guarded by a
new isUploadReferenced() check so deleting an in-use upload is blocked
("Remove it from the screen first") instead of leaving a slide element
pointing at a source that no longer exists.

isUploadReferenced() also finally wires up collectAssetReferences()
(src/html-slide/assets.ts), which existed only in its own test file —
it's the only way to know an upload is still used by an AI-authored
HTML screen, since those screens reference uploads by id through
asset:<id>, not by src, so collectAssetSources() alone can't see that
usage.

Verified live end-to-end with a fresh bun run dev instance via the
browse headless-Chromium skill: uploaded a test image, removed it while
unused (succeeds, toast "Upload removed", tile disappears), placed it
on the canvas with "Add to canvas", attempted removal while in use
(blocked, toast "Remove it from the screen first", tile and canvas
element both stay), deleted the canvas element, removed the upload
again (succeeds). bun run typecheck/lint/structure/build clean; bun run
test 420/420 (4 new tests for isUploadReferenced, no regressions).
2026-10-29 12:01:41 +03:00
5 changed files with 66 additions and 5 deletions

View File

@ -296,6 +296,7 @@ export default function App() {
}}
onAddImage={actions.addImage}
onSetDeviceImage={actions.setDeviceImage}
onRemoveUpload={actions.removeUpload}
/>
)}
<EditorCanvas

View File

@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { collectAssetSources, mapAssetSources } from './asset-sources'
import { collectAssetSources, isUploadReferenced, mapAssetSources } from './asset-sources'
import type { Slide, UploadAsset } from './types'
const makeSlide = (overrides: Partial<Slide> = {}): Slide => ({
@ -73,3 +73,28 @@ describe('mapAssetSources', () => {
expect(partial.slides[0]?.elements[2]).toBe(project.slides[0]?.elements[2])
})
})
describe('isUploadReferenced', () => {
it('is true when the upload src is placed directly on a canvas element', () => {
const { slides } = projectFixture()
expect(isUploadReferenced(makeUpload('data:image/png;base64,AAA'), slides)).toBe(true)
})
it('is true when the upload src is used as a background image', () => {
const slides = [makeSlide({
background: { type: 'image', color1: '#000', color2: '#000', angle: 0, image: 'data:image/png;base64,BBB' },
})]
expect(isUploadReferenced(makeUpload('data:image/png;base64,BBB'), slides)).toBe(true)
})
it('is true when an AI-authored HTML screen references the upload by id, not src', () => {
const slides = [makeSlide({ html: '<img src="asset:upload-1">' })]
expect(isUploadReferenced(makeUpload('data:image/png;base64,AAA'), slides)).toBe(true)
})
it('is false for an upload that appears nowhere in any slide', () => {
const { slides } = projectFixture()
expect(isUploadReferenced(makeUpload('data:image/png;base64,UNUSED'), slides)).toBe(false)
expect(isUploadReferenced(makeUpload('data:image/png;base64,UNUSED'), [])).toBe(false)
})
})

View File

@ -1,3 +1,4 @@
import { collectAssetReferences } from './html-slide/assets'
import type { CanvasElement, Slide, UploadAsset } from './types'
/**
@ -63,3 +64,16 @@ export const mapAssetSources = <T extends ProjectAssetSources>(project: T, map:
const slides = project.slides.map((slide) => mapSlideSources(slide, map))
return { ...project, uploads, slides }
}
/**
* True when `upload` is placed somewhere in `slides` — directly on a canvas
* element/background, or by an `asset:<id>` reference inside an AI-authored
* HTML screen (those screens don't carry the image source itself, so
* `collectAssetSources` can't see this usage; only the upload's own id
* reveals it). Used to guard upload removal so deleting an upload never
* leaves a slide pointing at a source that no longer exists.
*/
export const isUploadReferenced = (upload: UploadAsset, slides: Slide[]): boolean => {
if (collectAssetSources({ slides, uploads: [] }).has(upload.src)) return true
return slides.some((slide) => slide.html !== undefined && collectAssetReferences(slide.html).includes(upload.id))
}

View File

@ -15,6 +15,7 @@ import {
Shapes,
Sparkles,
StartUp02,
Trash2,
Type,
Upload,
} from './icons'
@ -309,11 +310,12 @@ const BackgroundPanel = ({ background, uploads, onUpdate, onUploadBackground }:
)
}
const UploadsPanel = ({ uploads, onUpload, onAddImage, onSetDeviceImage }: {
const UploadsPanel = ({ uploads, onUpload, onAddImage, onSetDeviceImage, onRemoveUpload }: {
uploads: UploadAsset[]
onUpload: (files: FileList) => void
onAddImage: (asset: UploadAsset) => void
onSetDeviceImage: (asset: UploadAsset) => void
onRemoveUpload: (asset: UploadAsset) => void
}) => {
const inputRef = useRef<HTMLInputElement>(null)
const handleFiles = (event: ChangeEvent<HTMLInputElement>) => event.target.files && onUpload(event.target.files)
@ -328,7 +330,14 @@ const UploadsPanel = ({ uploads, onUpload, onAddImage, onSetDeviceImage }: {
{uploads.map((asset) => (
<div className="upload-asset" key={asset.id}>
<img src={asset.src} alt={asset.name} />
<div><strong>{asset.name}</strong><span><button onClick={() => onSetDeviceImage(asset)}>Use in mockup</button><button onClick={() => onAddImage(asset)}>Add to canvas</button></span></div>
<div>
<strong>{asset.name}</strong>
<span>
<button onClick={() => onSetDeviceImage(asset)}>Use in mockup</button>
<button onClick={() => onAddImage(asset)}>Add to canvas</button>
<button title="Remove upload" aria-label="Remove upload" onClick={() => onRemoveUpload(asset)}><Trash2 size={11} /></button>
</span>
</div>
</div>
))}
</div>
@ -352,9 +361,10 @@ type PropertiesPanelProps = {
onUploadBackground: (file: File) => void
onAddImage: (asset: UploadAsset) => void
onSetDeviceImage: (asset: UploadAsset) => void
onRemoveUpload: (asset: UploadAsset) => void
}
export const PropertiesPanel = ({ activeTool, activeSlide, uploads, onApplyTemplate, onAddText, onAddDevice, onAddShape, onAddIcon, onUpdateBackground, onUploadFiles, onUploadBackground, onAddImage, onSetDeviceImage }: PropertiesPanelProps) => (
export const PropertiesPanel = ({ activeTool, activeSlide, uploads, onApplyTemplate, onAddText, onAddDevice, onAddShape, onAddIcon, onUpdateBackground, onUploadFiles, onUploadBackground, onAddImage, onSetDeviceImage, onRemoveUpload }: PropertiesPanelProps) => (
<aside className="properties-panel" id="properties-panel">
<div className="panel-scroll">
{activeTool === 'templates' && <TemplatesPanel onApplyTemplate={onApplyTemplate} />}
@ -375,7 +385,7 @@ export const PropertiesPanel = ({ activeTool, activeSlide, uploads, onApplyTempl
)}
{activeTool === 'device' && <DevicePanel onAddDevice={onAddDevice} />}
{activeTool === 'background' && <BackgroundPanel background={activeSlide.background} uploads={uploads} onUpdate={onUpdateBackground} onUploadBackground={onUploadBackground} />}
{activeTool === 'uploads' && <UploadsPanel uploads={uploads} onUpload={onUploadFiles} onAddImage={onAddImage} onSetDeviceImage={onSetDeviceImage} />}
{activeTool === 'uploads' && <UploadsPanel uploads={uploads} onUpload={onUploadFiles} onAddImage={onAddImage} onSetDeviceImage={onSetDeviceImage} onRemoveUpload={onRemoveUpload} />}
</div>
<div className="panel-footer"><span><i /> Auto-save</span><small>Everything stays local</small></div>
</aside>

View File

@ -1,4 +1,5 @@
import { useCallback, type Dispatch, type SetStateAction } from 'react'
import { isUploadReferenced } from '../asset-sources'
import { importBlobAsset } from '../asset-store'
import { makeTemplate } from '../data'
import { getDevicePlacement } from '../mockups/catalog'
@ -267,6 +268,15 @@ export function useEditorActions({
: slide))
}, [activeSlide, activeSlideId, addElement, commit, selectedElement, setActiveTool, setSelectedElementId])
const removeUpload = useCallback((asset: UploadAsset) => {
if (isUploadReferenced(asset, slides)) {
setToast('Remove it from the screen first')
return
}
setUploads((current) => current.filter((item) => item.id !== asset.id))
setToast('Upload removed')
}, [setToast, setUploads, slides])
const uploadFiles = useCallback(async (files: FileList) => {
const accepted = Array.from(files).filter((file) => file.type.startsWith('image/'))
const assets = await Promise.all(accepted.map(async (file) => ({
@ -447,6 +457,7 @@ export function useEditorActions({
addDevice,
addImage,
setDeviceImage,
removeUpload,
uploadFiles,
uploadToSelectedDevice,
uploadBackgroundImage,