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

[Inserter - Media tab]: Upload Openverse images when inserted #48501

Merged
merged 8 commits into from
Feb 28, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import {
MenuGroup,
MenuItem,
Spinner,
Modal,
Flex,
FlexItem,
Button,
__experimentalVStack as VStack,
} from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';
import { useMemo, useCallback, useState } from '@wordpress/element';
Expand Down Expand Up @@ -69,7 +74,47 @@ function MediaPreviewOptions( { category, media } ) {
);
}

function InsertExternalImageModal( { onClose, onClick } ) {
return (
<Modal
title={ __( 'Insert external image' ) }
onRequestClose={ onClose }
className="block-editor-inserter-media-tab-media-preview-inserter-external-image-modal"
>
<VStack spacing={ 3 }>
<p>
{ __(
'This image cannot be uploaded to your Media Library, but it can still be inserted as an external image.'
) }
</p>
<p>
{ __(
'External images can be removed by the external provider without warning and could even have legal compliance issues related to GDPR.'
) }
</p>
</VStack>
<Flex
className="block-editor-block-lock-modal__actions"
justify="flex-end"
expanded={ false }
>
<FlexItem>
<Button variant="tertiary" onClick={ onClose }>
{ __( 'Cancel' ) }
</Button>
</FlexItem>
<FlexItem>
<Button variant="primary" onClick={ onClick }>
{ __( 'Insert' ) }
</Button>
</FlexItem>
</Flex>
</Modal>
);
}

export function MediaPreview( { media, onClick, composite, category } ) {
const [ showModal, setShowModal ] = useState( false );
const [ isHovered, setIsHovered ] = useState( false );
const [ isInserting, setIsInserting ] = useState( false );
const [ block, preview ] = useMemo(
Expand Down Expand Up @@ -134,13 +179,7 @@ export function MediaPreview( { media, onClick, composite, category } ) {
} );
} )
.catch( () => {
createErrorNotice(
__(
'The image cannot be uploaded to the media library. External images can be removed by the external provider without warning and could even have legal compliance issues related to GDPR.'
),
{ type: 'snackbar' }
);
onClick( clonedBlock );
setShowModal( true );
setIsInserting( false );
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
} );
},
Expand All @@ -162,53 +201,67 @@ export function MediaPreview( { media, onClick, composite, category } ) {
const onMouseEnter = useCallback( () => setIsHovered( true ), [] );
const onMouseLeave = useCallback( () => setIsHovered( false ), [] );
return (
<InserterDraggableBlocks isEnabled={ true } blocks={ [ block ] }>
{ ( { draggable, onDragStart, onDragEnd } ) => (
<div
className={ classnames(
'block-editor-inserter__media-list__list-item',
{
'is-hovered': isHovered,
}
) }
draggable={ draggable }
onDragStart={ onDragStart }
onDragEnd={ onDragEnd }
>
<Tooltip text={ truncatedTitle || title }>
{ /* Adding `is-hovered` class to the wrapper element is needed
<>
<InserterDraggableBlocks isEnabled={ true } blocks={ [ block ] }>
{ ( { draggable, onDragStart, onDragEnd } ) => (
<div
className={ classnames(
'block-editor-inserter__media-list__list-item',
{
'is-hovered': isHovered,
}
) }
draggable={ draggable }
onDragStart={ onDragStart }
onDragEnd={ onDragEnd }
>
<Tooltip text={ truncatedTitle || title }>
{ /* Adding `is-hovered` class to the wrapper element is needed
because the options Popover is rendered outside of this node. */ }
<div
onMouseEnter={ onMouseEnter }
onMouseLeave={ onMouseLeave }
>
<CompositeItem
role="option"
as="div"
{ ...composite }
className="block-editor-inserter__media-list__item"
onClick={ () => onMediaInsert( block ) }
aria-label={ title }
<div
onMouseEnter={ onMouseEnter }
onMouseLeave={ onMouseLeave }
>
<div className="block-editor-inserter__media-list__item-preview">
{ preview }
{ isInserting && (
<div className="block-editor-inserter__media-list__item-preview-spinner">
<Spinner />
</div>
) }
</div>
</CompositeItem>
{ ! isInserting && (
<MediaPreviewOptions
category={ category }
media={ media }
/>
) }
</div>
</Tooltip>
</div>
<CompositeItem
role="option"
as="div"
{ ...composite }
className="block-editor-inserter__media-list__item"
onClick={ () => onMediaInsert( block ) }
aria-label={ title }
>
<div className="block-editor-inserter__media-list__item-preview">
{ preview }
{ isInserting && (
<div className="block-editor-inserter__media-list__item-preview-spinner">
<Spinner />
</div>
) }
</div>
</CompositeItem>
{ ! isInserting && (
<MediaPreviewOptions
category={ category }
media={ media }
/>
) }
</div>
</Tooltip>
</div>
) }
</InserterDraggableBlocks>
{ showModal && (
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
<InsertExternalImageModal
onClose={ () => setShowModal( false ) }
onClick={ () => {
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
onClick( cloneBlock( block ) );
createSuccessNotice( __( 'Image inserted.' ), {
type: 'snackbar',
} );
setShowModal( false );
} }
/>
) }
</InserterDraggableBlocks>
</>
);
}
11 changes: 11 additions & 0 deletions packages/block-editor/src/components/inserter/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -697,3 +697,14 @@ $block-inserter-tabs-height: 44px;
height: 100%;
}
}


.block-editor-inserter-media-tab-media-preview-inserter-external-image-modal {
@include break-small() {
max-width: $break-mobile;
}

p {
margin: 0;
}
}