Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Background image: add uploading state and restrict drag to one image. #64565

Merged
merged 6 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import {
__experimentalHStack as HStack,
__experimentalTruncate as Truncate,
Dropdown,
Placeholder,
Spinner,
__experimentalDropdownContentWrapper as DropdownContentWrapper,
} from '@wordpress/components';
import { __, _x, sprintf } from '@wordpress/i18n';
Expand Down Expand Up @@ -268,6 +270,14 @@ function BackgroundControlsPanel( {
);
}

function LoadingSpinner() {
return (
<Placeholder className="block-editor-global-styles-background-panel__loading">
<Spinner />
</Placeholder>
);
}

function BackgroundImageControls( {
onChange,
style,
Expand All @@ -277,6 +287,7 @@ function BackgroundImageControls( {
displayInPanel,
defaultValues,
} ) {
const [ isUploading, setIsUploading ] = useState( false );
const mediaUpload = useSelect(
( select ) => select( blockEditorStore ).getSettings().mediaUpload,
[]
Expand All @@ -289,6 +300,7 @@ function BackgroundImageControls( {
const { createErrorNotice } = useDispatch( noticesStore );
const onUploadError = ( message ) => {
createErrorNotice( message, { type: 'snackbar' } );
setIsUploading( false );
};

const resetBackgroundImage = () =>
Expand All @@ -303,10 +315,12 @@ function BackgroundImageControls( {
const onSelectMedia = ( media ) => {
if ( ! media || ! media.url ) {
resetBackgroundImage();
setIsUploading( false );
return;
}

if ( isBlobURL( media.url ) ) {
setIsUploading( true );
return;
}

Expand Down Expand Up @@ -349,16 +363,21 @@ function BackgroundImageControls( {
backgroundSize: sizeValue,
} )
);
setIsUploading( false );
};

// Drag and drop callback, restricting image to one.
const onFilesDrop = ( filesList ) => {
if ( filesList?.length > 1 ) {
onUploadError(
__( 'Only one image can be used as a background image.' )
);
return;
}
mediaUpload( {
allowedTypes: [ IMAGE_BACKGROUND_TYPE ],
filesList,
onFileChange( [ image ] ) {
if ( isBlobURL( image?.url ) ) {
return;
}
Comment on lines -359 to -361
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just double checking — this can be removed because it's already being handled early on in onSelectMedia, yeah?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I meant to write a note about that. Yeah, the same check is on onSelectMedia. 👍🏻

onSelectMedia( image );
},
onError: onUploadError,
Expand Down Expand Up @@ -393,6 +412,7 @@ function BackgroundImageControls( {
ref={ replaceContainerRef }
className="block-editor-global-styles-background-panel__image-tools-panel-item"
>
{ isUploading && <LoadingSpinner /> }
<MediaReplaceFlow
mediaId={ id }
mediaURL={ url }
Expand All @@ -414,6 +434,7 @@ function BackgroundImageControls( {
/>
}
variant="secondary"
onError={ onUploadError }
>
{ canRemove && (
<MenuItem
Expand Down
11 changes: 11 additions & 0 deletions packages/block-editor/src/components/global-styles/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,17 @@
box-shadow: inset 0 0 0 var(--wp-admin-border-width-focus) var(--wp-admin-theme-color);
}
}

.block-editor-global-styles-background-panel__loading {
height: 100%;
position: absolute;
z-index: 1;
width: 100%;
padding: 10px 0 0 0;
svg {
margin: 0;
}
}
}

.block-editor-global-styles-background-panel__image-preview-content,
Expand Down
Loading