From d24eaa6f8ac81477f9585e6a5d986be2b8f05ece Mon Sep 17 00:00:00 2001 From: Sunil Prajapati <61308756+akasunil@users.noreply.github.com> Date: Thu, 29 Aug 2024 13:40:14 +0530 Subject: [PATCH] Add 'Reset' option to MediaReplaceFlow component (#64826) * Add reset option in MediaReplaceFlow component * Add media reset option in toolbar of media-text block * Refactor MediaReplaceFlow component use in cover, image and post-featured-image blocks * Refactor MediaReplaceFlow component use in site-logo block * Refactor BackgroundImageControls component under global styles * Add media reset option in video, audio and file blocks * Fix onReset argument value error on image block * Remove unused MenuItem component from blocks * Reset attribute on reset option click in file block * Fix issue of reset option in media-text block Co-authored-by: akasunil Co-authored-by: Mamaduka Co-authored-by: ramonjd Co-authored-by: andrewserong Co-authored-by: jasmussen --- .../global-styles/background-panel.js | 14 ++++---------- .../src/components/media-replace-flow/index.js | 13 ++++++++++++- packages/block-library/src/audio/edit.js | 1 + .../src/cover/edit/block-controls.js | 13 ++----------- packages/block-library/src/file/edit.js | 11 +++++++++++ packages/block-library/src/image/image.js | 8 ++------ .../src/media-text/media-container.js | 9 ++------- .../src/post-featured-image/edit.js | 8 ++------ packages/block-library/src/site-logo/edit.js | 18 +++--------------- packages/block-library/src/video/edit.js | 1 + 10 files changed, 40 insertions(+), 56 deletions(-) diff --git a/packages/block-editor/src/components/global-styles/background-panel.js b/packages/block-editor/src/components/global-styles/background-panel.js index 58703dcbc9929e..93fed5780e454d 100644 --- a/packages/block-editor/src/components/global-styles/background-panel.js +++ b/packages/block-editor/src/components/global-styles/background-panel.js @@ -432,6 +432,10 @@ function BackgroundImageControls( { } variant="secondary" onError={ onUploadError } + onReset={ () => { + closeAndFocus(); + onResetImage(); + } } > { canRemove && ( ) } - { hasValue && ( - { - closeAndFocus(); - onResetImage(); - } } - > - { __( 'Reset' ) } - - ) } ) } + { mediaURL && onReset && ( + { + onReset(); + onClose(); + } } + > + { __( 'Reset' ) } + + ) } { typeof children === 'function' ? children( { onClose } ) : children } diff --git a/packages/block-library/src/audio/edit.js b/packages/block-library/src/audio/edit.js index 3c9f9e11c3b24b..e3b4ca670f957d 100644 --- a/packages/block-library/src/audio/edit.js +++ b/packages/block-library/src/audio/edit.js @@ -155,6 +155,7 @@ function AudioEdit( { onSelect={ onSelectAudio } onSelectURL={ onSelectURL } onError={ onUploadError } + onReset={ () => onSelectAudio( undefined ) } /> ) } diff --git a/packages/block-library/src/cover/edit/block-controls.js b/packages/block-library/src/cover/edit/block-controls.js index 499aef5e9a8cf7..041566c0a51971 100644 --- a/packages/block-library/src/cover/edit/block-controls.js +++ b/packages/block-library/src/cover/edit/block-controls.js @@ -10,7 +10,6 @@ import { __experimentalBlockFullHeightAligmentControl as FullHeightAlignmentControl, privateApis as blockEditorPrivateApis, } from '@wordpress/block-editor'; -import { MenuItem } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; /** @@ -103,16 +102,8 @@ export default function CoverBlockControls( { onToggleFeaturedImage={ toggleUseFeaturedImage } useFeaturedImage={ useFeaturedImage } name={ ! url ? __( 'Add Media' ) : __( 'Replace' ) } - > - { !! url && ( - - { __( 'Reset' ) } - - ) } - + onReset={ onClearMedia } + /> ); diff --git a/packages/block-library/src/file/edit.js b/packages/block-library/src/file/edit.js index 448b6f5ec73d15..be061d357bffd1 100644 --- a/packages/block-library/src/file/edit.js +++ b/packages/block-library/src/file/edit.js @@ -109,6 +109,16 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) { function onSelectFile( newMedia ) { if ( ! newMedia || ! newMedia.url ) { + // Reset attributes. + setAttributes( { + href: undefined, + fileName: undefined, + textLinkHref: undefined, + id: undefined, + fileId: undefined, + displayPreview: undefined, + previewHeight: undefined, + } ); setTemporaryURL(); return; } @@ -230,6 +240,7 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) { accept="*" onSelect={ onSelectFile } onError={ onUploadError } + onReset={ () => onSelectFile( undefined ) } /> - onSelectImage( undefined ) }> - { __( 'Reset' ) } - - + onReset={ () => onSelectImage( undefined ) } + /> ) } { isSingleSelected && externalBlob && ( diff --git a/packages/block-library/src/media-text/media-container.js b/packages/block-library/src/media-text/media-container.js index 2d8556f9ebe327..986ecad6cc7e69 100644 --- a/packages/block-library/src/media-text/media-container.js +++ b/packages/block-library/src/media-text/media-container.js @@ -62,22 +62,18 @@ function ToolbarEditButton( { onSelectMedia, toggleUseFeaturedImage, useFeaturedImage, - featuredImageURL, } ) { return ( onSelectMedia( undefined ) } /> ); @@ -200,7 +196,6 @@ function MediaContainer( props, ref ) { } mediaId={ mediaId } toggleUseFeaturedImage={ toggleUseFeaturedImage } - useFeaturedImage={ useFeaturedImage } /> { ( mediaTypeRenderers[ mediaType ] || noop )() } { isTemporaryMedia && } diff --git a/packages/block-library/src/post-featured-image/edit.js b/packages/block-library/src/post-featured-image/edit.js index 3d54a7415f288b..ff777f707330df 100644 --- a/packages/block-library/src/post-featured-image/edit.js +++ b/packages/block-library/src/post-featured-image/edit.js @@ -10,7 +10,6 @@ import { isBlobURL } from '@wordpress/blob'; import { useEntityProp, store as coreStore } from '@wordpress/core-data'; import { useSelect, useDispatch } from '@wordpress/data'; import { - MenuItem, ToggleControl, PanelBody, Placeholder, @@ -368,11 +367,8 @@ export default function PostFeaturedImageEdit( { accept="image/*" onSelect={ onSelectImage } onError={ onUploadError } - > - setFeaturedImage( 0 ) }> - { __( 'Reset' ) } - - + onReset={ () => setFeaturedImage( 0 ) } + /> ) }
diff --git a/packages/block-library/src/site-logo/edit.js b/packages/block-library/src/site-logo/edit.js index e44147529af2b5..81217e6cb5135f 100644 --- a/packages/block-library/src/site-logo/edit.js +++ b/packages/block-library/src/site-logo/edit.js @@ -14,7 +14,6 @@ import { } from '@wordpress/element'; import { __, isRTL } from '@wordpress/i18n'; import { - MenuItem, PanelBody, RangeControl, ResizableBox, @@ -356,22 +355,11 @@ function SiteLogoReplaceFlow( { return ( - { ( { onClose } ) => - mediaURL && ( - { - onRemoveLogo(); - onClose(); - } } - > - { __( 'Reset' ) } - - ) - } - + onReset={ onRemoveLogo } + /> ); } diff --git a/packages/block-library/src/video/edit.js b/packages/block-library/src/video/edit.js index 63f633a41adf94..d72dd5463cde33 100644 --- a/packages/block-library/src/video/edit.js +++ b/packages/block-library/src/video/edit.js @@ -208,6 +208,7 @@ function VideoEdit( { onSelect={ onSelectVideo } onSelectURL={ onSelectURL } onError={ onUploadError } + onReset={ () => onSelectVideo( undefined ) } />