Skip to content
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 @@ -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
Copy Markdown
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.
}
Comment thread
ntsekouras marked this conversation as resolved.
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
Copy Markdown
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