@@ -205,6 +205,44 @@ export function useMediaUpload() {
205205 const pendingImetaRef = React . useRef ( pendingImeta ) ;
206206 pendingImetaRef . current = pendingImeta ;
207207
208+ /**
209+ * Pre-edit originals of annotated attachments, keyed by the annotated
210+ * attachment's URL. Powers "revert to original" in the composer lightbox.
211+ * In-memory only — cleared implicitly when the attachment leaves the
212+ * composer (send, remove, draft switch).
213+ */
214+ const [ originalsByUrl , setOriginalsByUrl ] = React . useState <
215+ Map < string , BlobDescriptor >
216+ > ( ( ) => new Map ( ) ) ;
217+ const originalsByUrlRef = React . useRef ( originalsByUrl ) ;
218+ originalsByUrlRef . current = originalsByUrl ;
219+
220+ /** Annotated URL → original URL (derived; handy for stable list keys). */
221+ const originalUrlByUrl = React . useMemo ( ( ) => {
222+ const map = new Map < string , string > ( ) ;
223+ for ( const [ url , original ] of originalsByUrl ) map . set ( url , original . url ) ;
224+ return map ;
225+ } , [ originalsByUrl ] ) ;
226+
227+ // Prune originals whose annotated attachment is no longer pending —
228+ // covers remove, cancel, send-clear, and draft restore in one place.
229+ React . useEffect ( ( ) => {
230+ setOriginalsByUrl ( ( prev ) => {
231+ if ( prev . size === 0 ) return prev ;
232+ const liveUrls = new Set ( pendingImeta . map ( ( d ) => d . url ) ) ;
233+ let changed = false ;
234+ const next = new Map < string , BlobDescriptor > ( ) ;
235+ for ( const [ url , original ] of prev ) {
236+ if ( liveUrls . has ( url ) ) {
237+ next . set ( url , original ) ;
238+ } else {
239+ changed = true ;
240+ }
241+ }
242+ return changed ? next : prev ;
243+ } ) ;
244+ } , [ pendingImeta ] ) ;
245+
208246 /** Monotonic slot counter — ensures each batch gets unique indices even
209247 * before React flushes the state update. */
210248 const nextSlotRef = React . useRef ( 0 ) ;
@@ -511,6 +549,80 @@ export function useMediaUpload() {
511549 [ isUploadCanceled , onUploaded , onUploadError , reserveUploadingPreview ] ,
512550 ) ;
513551
552+ /**
553+ * Upload an annotated replacement for an existing image attachment and
554+ * swap it into the same slot (attachment order is preserved). The pre-edit
555+ * descriptor is remembered in `originalsByUrl` so the edit can be reverted;
556+ * chained edits keep the earliest original as the single revert point.
557+ *
558+ * Returns the new descriptor, or null if `oldUrl` is no longer pending.
559+ * Rejects on upload failure (after surfacing the standard error banner) so
560+ * callers can keep their editing UI open.
561+ */
562+ const uploadEditedAttachment = React . useCallback (
563+ async (
564+ oldUrl : string ,
565+ bytes : Uint8Array ,
566+ ) : Promise < BlobDescriptor | null > => {
567+ const oldDescriptor = pendingImetaRef . current . find (
568+ ( d ) => d . url === oldUrl ,
569+ ) ;
570+ if ( ! oldDescriptor ) return null ;
571+
572+ // The annotated output is always PNG — swap the extension accordingly.
573+ const stem = ( oldDescriptor . filename ?? "image" ) . replace ( / \. [ ^ . ] + $ / , "" ) ;
574+ const filename = `${ stem } .png` ;
575+
576+ const previewId = reserveUploadingPreview ( ) ;
577+ setUploadingCount ( ( c ) => c + 1 ) ;
578+ try {
579+ const descriptor = await uploadMediaBytes (
580+ [ ...bytes ] ,
581+ filename ,
582+ uploadProgressId ( previewId ) ,
583+ ) ;
584+ if ( isUploadCanceled ( previewId ) ) return null ;
585+ finishUpload ( previewId ) ;
586+ setImetaSlots ( ( prev ) =>
587+ prev . map ( ( d ) => ( d ?. url === oldUrl ? descriptor : d ) ) ,
588+ ) ;
589+ setOriginalsByUrl ( ( prev ) => {
590+ const next = new Map ( prev ) ;
591+ // Re-editing an annotated image keeps the earliest original.
592+ const original = prev . get ( oldUrl ) ?? oldDescriptor ;
593+ next . delete ( oldUrl ) ;
594+ next . set ( descriptor . url , original ) ;
595+ return next ;
596+ } ) ;
597+ return descriptor ;
598+ } catch ( err ) {
599+ onUploadError ( err , previewId ) ;
600+ throw err ;
601+ }
602+ } ,
603+ [ finishUpload , isUploadCanceled , onUploadError , reserveUploadingPreview ] ,
604+ ) ;
605+
606+ /**
607+ * Swap an annotated attachment back to its pre-edit original (same slot)
608+ * and forget the stored original. Returns the restored descriptor, or null
609+ * if the URL has no recorded original.
610+ */
611+ const revertAttachment = React . useCallback (
612+ ( url : string ) : BlobDescriptor | null => {
613+ const original = originalsByUrlRef . current . get ( url ) ;
614+ if ( ! original ) return null ;
615+ setImetaSlots ( ( prev ) => prev . map ( ( d ) => ( d ?. url === url ? original : d ) ) ) ;
616+ setOriginalsByUrl ( ( prev ) => {
617+ const next = new Map ( prev ) ;
618+ next . delete ( url ) ;
619+ return next ;
620+ } ) ;
621+ return original ;
622+ } ,
623+ [ ] ,
624+ ) ;
625+
514626 const removeAttachment = React . useCallback ( ( url : string ) => {
515627 setImetaSlots ( ( prev ) => prev . map ( ( d ) => ( d ?. url === url ? null : d ) ) ) ;
516628 } , [ ] ) ;
@@ -541,11 +653,14 @@ export function useMediaUpload() {
541653 handlePaste,
542654 isDragOver,
543655 isUploading,
656+ originalUrlByUrl,
544657 pendingImeta,
545658 pendingImetaRef,
546659 removeAttachment,
660+ revertAttachment,
547661 setPendingImeta,
548662 setUploadState,
663+ uploadEditedAttachment,
549664 uploadFile,
550665 uploadingCount,
551666 uploadingPreviews,
@@ -561,9 +676,12 @@ export function useMediaUpload() {
561676 handlePaste ,
562677 isDragOver ,
563678 isUploading ,
679+ originalUrlByUrl ,
564680 pendingImeta ,
565681 removeAttachment ,
682+ revertAttachment ,
566683 setPendingImeta ,
684+ uploadEditedAttachment ,
567685 uploadFile ,
568686 uploadingCount ,
569687 uploadingPreviews ,
0 commit comments