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 @@ -98,13 +98,6 @@ function useInserterMediaCategories() {
) {
return false;
}
// When a category has set `isExternalResource` to `true`, we
// don't need to check for allowed mime types, as they are used
// for restricting uploads for this media type and not for
// inserting media from external sources.
if ( category.isExternalResource ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now we upload the images from external sources, so we have to check the allowedMimeTypes setting.

return true;
}
return Object.values( allowedMimeTypes ).some( ( mimeType ) =>
mimeType.startsWith( `${ category.mediaType }/` )
);
Expand Down Expand Up @@ -156,7 +149,15 @@ export function useMediaCategories( rootClientId ) {
if ( category.isExternalResource ) {
return [ category.name, true ];
}
const results = await category.fetch( { per_page: 1 } );
let results = [];
try {
results = await category.fetch( {
per_page: 1,
} );
} catch ( e ) {
// If the request fails, we shallow the error and just don't show
// the category, in order to not break the media tab.
}
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
return [ category.name, !! results.length ];
} )
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,129 +1,16 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import {
__unstableComposite as Composite,
__unstableUseCompositeState as useCompositeState,
__unstableCompositeItem as CompositeItem,
Tooltip,
DropdownMenu,
MenuGroup,
MenuItem,
} from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';
import { useMemo, useCallback, useState } from '@wordpress/element';
import { cloneBlock } from '@wordpress/blocks';
import { moreVertical, external } from '@wordpress/icons';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import InserterDraggableBlocks from '../../inserter-draggable-blocks';
import { getBlockAndPreviewFromMedia } from './utils';

const MAXIMUM_TITLE_LENGTH = 25;
const MEDIA_OPTIONS_POPOVER_PROPS = {
position: 'bottom left',
className:
'block-editor-inserter__media-list__item-preview-options__popover',
};

function MediaPreviewOptions( { category, media } ) {
if ( ! category.getReportUrl ) {
return null;
}
const reportUrl = category.getReportUrl( media );
return (
<DropdownMenu
className="block-editor-inserter__media-list__item-preview-options"
label={ __( 'Options' ) }
popoverProps={ MEDIA_OPTIONS_POPOVER_PROPS }
icon={ moreVertical }
>
{ () => (
<MenuGroup>
<MenuItem
onClick={ () =>
window.open( reportUrl, '_blank' ).focus()
}
icon={ external }
>
{ sprintf(
/* translators: %s: The media type to report e.g: "image", "video", "audio" */
__( 'Report %s' ),
category.mediaType
) }
</MenuItem>
</MenuGroup>
) }
</DropdownMenu>
);
}

function MediaPreview( { media, onClick, composite, category } ) {
const [ isHovered, setIsHovered ] = useState( false );
const [ block, preview ] = useMemo(
() => getBlockAndPreviewFromMedia( media, category.mediaType ),
[ media, category.mediaType ]
);
const title = media.title?.rendered || media.title;
let truncatedTitle;
if ( title.length > MAXIMUM_TITLE_LENGTH ) {
const omission = '...';
truncatedTitle =
title.slice( 0, MAXIMUM_TITLE_LENGTH - omission.length ) + omission;
}
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
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={ () => onClick( block ) }
aria-label={ title }
>
<div className="block-editor-inserter__media-list__item-preview">
{ preview }
</div>
</CompositeItem>
<MediaPreviewOptions
category={ category }
media={ media }
/>
</div>
</Tooltip>
</div>
) }
</InserterDraggableBlocks>
);
}
import { MediaPreview } from './media-preview';

function MediaList( {
mediaList,
Expand All @@ -132,12 +19,6 @@ function MediaList( {
label = __( 'Media List' ),
} ) {
const composite = useCompositeState();
const onPreviewClick = useCallback(
( block ) => {
onClick( cloneBlock( block ) );
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I moved the cloneBlock call inside the MediaPreview callback.

},
[ onClick ]
);
return (
<Composite
{ ...composite }
Expand All @@ -150,7 +31,7 @@ function MediaList( {
key={ media.id || media.sourceId || index }
media={ media }
category={ category }
onClick={ onPreviewClick }
onClick={ onClick }
composite={ composite }
/>
) ) }
Expand Down
Loading