-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Add sizeSlug to the image inserted from Inserter. #60138
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
base: trunk
Are you sure you want to change the base?
Changes from all commits
79efc8d
6ff6bf0
6ed6c44
4aacb3b
29b6484
d1a99fc
5259e2a
312160a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Original file line | Diff line number | Diff line change |
|---|---|---|---|
|
|
@@ -121,13 +121,14 @@ export function MediaPreview( { media, onClick, category } ) { | ||
| useState( false ); | useState( false ); | ||
| const [ isHovered, setIsHovered ] = useState( false ); | const [ isHovered, setIsHovered ] = useState( false ); | ||
| const [ isInserting, setIsInserting ] = useState( false ); | const [ isInserting, setIsInserting ] = useState( false ); | ||
| const { getSettings } = useSelect( blockEditorStore ); | |||
| const { imageDefaultSize } = getSettings(); | |||
|
Member
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. We shouldn't call the selector during the render. The actual setting value is only needed in the P.S. I've got a small PR that improves the |
|||
| const [ block, preview ] = useMemo( | const [ block, preview ] = useMemo( | ||
| () => getBlockAndPreviewFromMedia( media, category.mediaType ), | () => getBlockAndPreviewFromMedia( media, category.mediaType ), | ||
| [ media, category.mediaType ] | [ media, category.mediaType ] | ||
| ); | ); | ||
| const { createErrorNotice, createSuccessNotice } = | const { createErrorNotice, createSuccessNotice } = | ||
| useDispatch( noticesStore ); | useDispatch( noticesStore ); | ||
| const { getSettings } = useSelect( blockEditorStore ); | |||
|
|
|
||
| const onMediaInsert = useCallback( | const onMediaInsert = useCallback( | ||
| ( previewBlock ) => { | ( previewBlock ) => { | ||
|
|
@@ -169,6 +170,28 @@ export function MediaPreview( { media, onClick, category } ) { | ||
| if ( isBlobURL( img.url ) ) { | if ( isBlobURL( img.url ) ) { | ||
| return; | return; | ||
| } | } | ||
|
|
|||
| if ( | |||
|
Contributor
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. The check would be better with Something like that: Additionally and not related to this PR, we'll need to check how this behaves for other media types besides 'image'. It will most definitely need changes, for example the messages and adding the |
|||
| clonedBlock.name === 'core/image' && | |||
| img.media_type === 'image' | |||
| ) { | |||
| const sizeSlug = | |||
| img.sizes?.[ imageDefaultSize ] || | |||
| img.media_details?.sizes?.[ | |||
| imageDefaultSize | |||
| ] | |||
| ? imageDefaultSize | |||
| : 'full'; | |||
| onClick( { | |||
| ...clonedBlock, | |||
| attributes: { | |||
| ...clonedBlock.attributes, | |||
|
Contributor
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. Same handling (check media category and add size) should be done above, since I suggested to not set the size in the preview. The only difference is that we will only need to add the sizeSlug attribute. |
|||
| id: img.id, | |||
| url: img.url, | |||
| sizeSlug, | |||
| }, | |||
| } ); | |||
| } else { | |||
| onClick( { | onClick( { | ||
| ...clonedBlock, | ...clonedBlock, | ||
| attributes: { | attributes: { | ||
|
|
@@ -177,6 +200,8 @@ export function MediaPreview( { media, onClick, category } ) { | ||
| url: img.url, | url: img.url, | ||
| }, | }, | ||
| } ); | } ); | ||
| } | |||
|
|
|||
| createSuccessNotice( | createSuccessNotice( | ||
| __( 'Image uploaded and inserted.' ), | __( 'Image uploaded and inserted.' ), | ||
| { type: 'snackbar' } | { type: 'snackbar' } | ||
|
|
@@ -201,6 +226,7 @@ export function MediaPreview( { media, onClick, category } ) { | ||
| onClick, | onClick, | ||
| createSuccessNotice, | createSuccessNotice, | ||
| createErrorNotice, | createErrorNotice, | ||
| imageDefaultSize, | |||
| ] | ] | ||
| ); | ); | ||
|
|
|
||
|
|
|||
| Original file line number | Original file line | Diff line number | Diff line change |
|---|---|---|---|
| @@ -1,10 +1,12 @@ | |||
| /** | /** | ||
| * WordPress dependencies | * WordPress dependencies | ||
| */ | */ | ||
| import { __ } from '@wordpress/i18n'; | import { __, isRTL } from '@wordpress/i18n'; | ||
| import { useViewportMatch } from '@wordpress/compose'; | import { useViewportMatch } from '@wordpress/compose'; | ||
| import { Button } from '@wordpress/components'; | import { Button } from '@wordpress/components'; | ||
| import { useCallback, useMemo } from '@wordpress/element'; | import { useCallback, useMemo } from '@wordpress/element'; | ||
| import { useSelect } from '@wordpress/data'; | |||
| import { Icon, chevronRight, chevronLeft } from '@wordpress/icons'; | |||
|
|
|
||
| /** | /** | ||
| * Internal dependencies | * Internal dependencies | ||
|
|
@@ -15,8 +17,10 @@ import MediaUpload from '../../media-upload'; | ||
| import { useMediaCategories } from './hooks'; | import { useMediaCategories } from './hooks'; | ||
| import { getBlockAndPreviewFromMedia } from './utils'; | import { getBlockAndPreviewFromMedia } from './utils'; | ||
| import MobileTabNavigation from '../mobile-tab-navigation'; | import MobileTabNavigation from '../mobile-tab-navigation'; | ||
|
|
|||
| import CategoryTabs from '../category-tabs'; | import CategoryTabs from '../category-tabs'; | ||
| import InserterNoResults from '../no-results'; | import InserterNoResults from '../no-results'; | ||
| import { store as blockEditorStore } from '../../../store'; | |||
|
|
|
||
| const ALLOWED_MEDIA_TYPES = [ 'image', 'video', 'audio' ]; | const ALLOWED_MEDIA_TYPES = [ 'image', 'video', 'audio' ]; | ||
|
|
|
||
|
|
@@ -30,15 +34,21 @@ function MediaTab( { | ||
| const mediaCategories = useMediaCategories( rootClientId ); | const mediaCategories = useMediaCategories( rootClientId ); | ||
| const isMobile = useViewportMatch( 'medium', '<' ); | const isMobile = useViewportMatch( 'medium', '<' ); | ||
| const baseCssClass = 'block-editor-inserter__media-tabs'; | const baseCssClass = 'block-editor-inserter__media-tabs'; | ||
| const { getSettings } = useSelect( blockEditorStore ); | |||
| const { imageDefaultSize } = getSettings(); | |||
|
Member
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. Same here. Let's move the selector call inside the |
|||
| const onSelectMedia = useCallback( | const onSelectMedia = useCallback( | ||
| ( media ) => { | ( media ) => { | ||
| if ( ! media?.url ) { | if ( ! media?.url ) { | ||
| return; | return; | ||
| } | } | ||
| const [ block ] = getBlockAndPreviewFromMedia( media, media.type ); | const [ block ] = getBlockAndPreviewFromMedia( | ||
| media, | |||
| media.type, | |||
| imageDefaultSize | |||
| ); | |||
| onInsert( block ); | onInsert( block ); | ||
| }, | }, | ||
| [ onInsert ] | [ onInsert, imageDefaultSize ] | ||
| ); | ); | ||
| const categories = useMemo( | const categories = useMemo( | ||
| () => | () => | ||
|
|
|||
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.
It's good that you extract the selector like this, because we'll call it in a callback function. We could extract the size value from setting though only if we need it, which is when the media category is 'image'.