-
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
Add sizeSlug to the image inserted from Inserter. #60138
base: trunk
Are you sure you want to change the base?
Conversation
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @burnuser. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Size Change: +87 B (0%) Total Size: 1.77 MB
ℹ️ View Unchanged
|
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.
Thanks for the PR!
To ideally solve this problem, I think it is necessary to match the behavior when media is inserted via the Image block. In other words, I think we need to consider the following points.
- A newly inserted image may not always be at full size. We need to consider the value of the
imageDefaultSize
option (See). - If an image is inserted via a URL, it should always apply the size slug defined by the
imageDefaultSize
option (See).
The approach I think is to pass this option as the third parameter to the getBlockAndPreviewFromMedia()
function and derive the appropriate size slug inside the function. It would probably be written like this:
const { getSettings } = useSelect( blockEditorStore );
const { imageDefaultSize } = getSettings();
const [ block, preview ] = useMemo(
() => getBlockAndPreviewFromMedia( media, category.mediaType, imageDefaultSize ),
[ media, category.mediaType, imageDefaultSize ]
);
I added @ntsekouras, who implemented the media tab, as a reviewer 🙏 |
Thanks for the PR @torounit ! I agree with @t-hamano that we should match the image block behavior and in order to do that we need to handle Noting though that when the source is external and we select a media item, we try to upload it and only when the upload is done, we can check available sizes and set it properly(around here). We would also need some checks about the media type there too, even though in core we don't have other external sources of other media types(ex audio, video). |
055b3db
to
b7b20e1
Compare
@t-hamano @ntsekouras |
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.
Thank you again for the PR and the updates! I left some comments, but in general this is close to land.
export function getBlockAndPreviewFromMedia( | ||
media, | ||
mediaType, | ||
sizeSlug = 'full' |
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.
I think we shouldn't check the sizeSlug in the preview as its better to have the smaller one for performance. It also keeps things simpler without an extra param that's specific to a single media type.
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.
Agreed. The sizeSlug
attribute value isn't important for previews.
@@ -121,9 +121,16 @@ export function MediaPreview( { media, onClick, category } ) { | |||
useState( false ); | |||
const [ isHovered, setIsHovered ] = useState( false ); | |||
const [ isInserting, setIsInserting ] = useState( false ); | |||
const { getSettings } = useSelect( blockEditorStore ); |
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'.
}, | ||
} ); | ||
|
||
if ( |
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.
The check would be better with category.mediaType === 'image'
and we could simplify by including the extra attributes in an object and then have only one onClick
.
Something like that:
const extraAttributes = {
id: img.id,
url: img.url,
};
if (type === image) {
extraAttributes.sizeSlug = ....
}
onClick( {
...clonedBlock,
attributes: {
...clonedBlock.attributes,
...extraAttributes
},
} )
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 src
attribute instead of the url
. This happened because for a while this API wasn't public and we only had Openverse(images).
onClick( { | ||
...clonedBlock, | ||
attributes: { | ||
...clonedBlock.attributes, |
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.
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.
@@ -121,9 +121,16 @@ export function MediaPreview( { media, onClick, category } ) { | |||
useState( false ); | |||
const [ isHovered, setIsHovered ] = useState( false ); | |||
const [ isInserting, setIsInserting ] = useState( false ); | |||
const { getSettings } = useSelect( blockEditorStore ); | |||
const { imageDefaultSize } = getSettings(); |
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.
We shouldn't call the selector during the render. The actual setting value is only needed in the onMediaInsert
callback. The sizeSlug
does nothing for remote images.
P.S. I've got a small PR that improves the onMediaInsert
callback and uses the same technique for the mediaUpload
setting - #60983.
@@ -39,15 +41,21 @@ function MediaTab( { | |||
const mediaCategories = useMediaCategories( rootClientId ); | |||
const isMobile = useViewportMatch( 'medium', '<' ); | |||
const baseCssClass = 'block-editor-inserter__media-tabs'; | |||
const { getSettings } = useSelect( blockEditorStore ); | |||
const { imageDefaultSize } = getSettings(); |
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.
Same here. Let's move the selector call inside the onSelectMedia
.
5f760e5
to
5259e2a
Compare
What?
The image inserted from the block inserter is full size, so add
"sizeSlug": "full"
to the image block to be created.Why?
fix: #55027
How?
Add sizeSlug to the attributes of the block created by
getBlockAndPreviewFromMedia
.Testing Instructions
Testing Instructions for Keyboard
Screenshots or screencast
2024-03-23.5.17.15.mov