diff --git a/package-lock.json b/package-lock.json index 706ceaa6987a52..4ea4170f45c63b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -59804,6 +59804,7 @@ "version": "0.31.0", "license": "GPL-2.0-or-later", "dependencies": { + "@react-spring/web": "^9.4.5", "@wordpress/api-fetch": "file:../api-fetch", "@wordpress/base-styles": "file:../base-styles", "@wordpress/blob": "file:../blob", diff --git a/packages/fields/package.json b/packages/fields/package.json index d37ed3de2997c9..722520d6085789 100644 --- a/packages/fields/package.json +++ b/packages/fields/package.json @@ -47,6 +47,7 @@ "src/**/*.scss" ], "dependencies": { + "@react-spring/web": "^9.4.5", "@wordpress/api-fetch": "file:../api-fetch", "@wordpress/base-styles": "file:../base-styles", "@wordpress/blob": "file:../blob", diff --git a/packages/fields/src/components/media-edit/index.tsx b/packages/fields/src/components/media-edit/index.tsx index 862eac92b74407..e73e8788df4397 100644 --- a/packages/fields/src/components/media-edit/index.tsx +++ b/packages/fields/src/components/media-edit/index.tsx @@ -14,6 +14,7 @@ import { __experimentalText as Text, __experimentalTruncate as Truncate, __experimentalVStack as VStack, + __experimentalHStack as HStack, BaseControl, Tooltip, VisuallyHidden, @@ -28,7 +29,7 @@ import { useRef, useState, } from '@wordpress/element'; -import { __ } from '@wordpress/i18n'; +import { __, sprintf } from '@wordpress/i18n'; import { archive, audio, @@ -36,6 +37,10 @@ import { file, closeSmall, error as errorIcon, + chevronUp, + chevronDown, + chevronLeft, + chevronRight, } from '@wordpress/icons'; import { MediaUpload, @@ -49,9 +54,27 @@ import { store as noticesStore } from '@wordpress/notices'; */ import { unlock } from '../../lock-unlock'; import type { MediaEditProps } from '../../types'; +import useMovingAnimation from './use-moving-animation'; const { MediaUploadModal } = unlock( mediaUtilsPrivateApis ); +function AnimatedMediaItem( { + children, + index, + className, +}: { + children: React.ReactNode; + index: number; + className?: string; +} ) { + const ref = useMovingAnimation( index ); + return ( +
+ { children } +
+ ); +} + type BlobItem = { id: string; source_url: string; @@ -59,6 +82,13 @@ type BlobItem = { alt_text?: string; }; +function normalizeValue( value: number | number[] | undefined ): number[] { + if ( Array.isArray( value ) ) { + return value; + } + return value ? [ value ] : []; +} + /** * Conditional Media component that uses MediaUploadModal when experiment is enabled, * otherwise falls back to media-utils MediaUpload. @@ -122,7 +152,9 @@ function MediaPickerButton( { const isBlob = attachment && isBlobURL( attachment.source_url ); const mediaPickerButton = (
{ @@ -160,7 +192,11 @@ function MediaPickerButton( { if ( ! showTooltip ) { return mediaPickerButton; } - return { mediaPickerButton }; + return ( + + { mediaPickerButton } + + ); } const archiveMimeTypes = [ @@ -195,6 +231,54 @@ function MediaEditPlaceholder( props: { ); } +function MoveButtons( { + itemId, + index, + totalItems, + isUploading, + moveItem, + orientation = 'vertical', +}: { + itemId: number; + index: number; + totalItems: number; + isUploading: boolean; + moveItem: ( id: number, direction: 'up' | 'down' ) => void; + orientation?: 'vertical' | 'horizontal'; +} ) { + const isHorizontal = orientation === 'horizontal'; + return ( + <> +
) } - + ); } ) } { ( multiple || ! allItems?.length ) && ( { + setTargetItemId( undefined ); + open(); + } } label={ addButtonLabel } onFilesDrop={ onFilesDrop } isUploading={ isUploading } @@ -335,68 +440,110 @@ function CompactMediaEditAttachments( { addButtonLabel, multiple, removeItem, + moveItem, open, onFilesDrop, isUploading, + setTargetItemId, }: MediaEditAttachmentsProps ) { return ( <> { !! allItems?.length && ( - - { allItems.map( ( attachment ) => { - const isBlob = isBlobURL( attachment.source_url ); - return ( -
- + + { allItems.map( ( attachment, index ) => { + const isBlob = isBlobURL( attachment.source_url ); + const showMoveButtons = + multiple && allItems.length > 1; + const attachmentNumericId = attachment.id as number; + return ( + - <> - - { ! isBlob && ( - - } + { + setTargetItemId( + attachmentNumericId + ); + open(); + } } + label={ __( 'Replace' ) } + showTooltip + onFilesDrop={ onFilesDrop } + attachment={ attachment } + isUploading={ isUploading } + > + <> + - ) } - - -
- ); - } ) } -
+ { ! isBlob && ( + + } + /> + ) } + + + { ! isBlob && ( + + { showMoveButtons && ( + + ) } +