-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
ntsekouras
merged 8 commits into
trunk
from
fix/upload-external-openverse-images-on-insert
Feb 28, 2023
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ad8bbf2
[Inserter - Media tab]: Upload Openverse images when inserted
ntsekouras 6213ab2
extract to separate hook
ntsekouras 3ac74f2
Check all media categories against `allowedMimeTypes`
ntsekouras e9bb9de
add a spinner when is uploading image and extract Preview in separate…
ntsekouras cfe1680
Add extra safeguard agains category fetching request
ntsekouras fa60819
add modal if the image fails to upload
ntsekouras 15c2210
rename some props
ntsekouras 7784d6b
update copy
ntsekouras File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
|
@@ -132,12 +19,6 @@ function MediaList( { | |
label = __( 'Media List' ), | ||
} ) { | ||
const composite = useCompositeState(); | ||
const onPreviewClick = useCallback( | ||
( block ) => { | ||
onClick( cloneBlock( block ) ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved the |
||
}, | ||
[ onClick ] | ||
); | ||
return ( | ||
<Composite | ||
{ ...composite } | ||
|
@@ -150,7 +31,7 @@ function MediaList( { | |
key={ media.id || media.sourceId || index } | ||
media={ media } | ||
category={ category } | ||
onClick={ onPreviewClick } | ||
onClick={ onClick } | ||
composite={ composite } | ||
/> | ||
) ) } | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.