Add sizeSlug to the image inserted from Inserter.#60138
Add sizeSlug to the image inserted from Inserter.#60138
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: +77 B (0%) Total Size: 3.09 MB
ℹ️ View Unchanged
|
t-hamano
left a comment
There was a problem hiding this comment.
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
imageDefaultSizeoption (See). - If an image is inserted via a URL, it should always apply the size slug defined by the
imageDefaultSizeoption (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 |
ntsekouras
left a comment
There was a problem hiding this comment.
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.
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.
Agreed. The sizeSlug attribute value isn't important for previews.
| useState( false ); | ||
| const [ isHovered, setIsHovered ] = useState( false ); | ||
| const [ isInserting, setIsInserting ] = useState( false ); | ||
| const { getSettings } = useSelect( blockEditorStore ); |
There was a problem hiding this comment.
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.
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.
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.
packages/block-editor/src/components/inserter/media-tab/media-preview.js
Outdated
Show resolved
Hide resolved
packages/block-editor/src/components/inserter/media-tab/media-tab.js
Outdated
Show resolved
Hide resolved
5f760e5 to
5259e2a
Compare
|
Hi, @torounit Do you have time to refresh the branch and address the feedback? P.S. I also don't mind doing it myself if you're busy. |
|
Thanks for the reminder! I’ll update it. |
312160a to
bce1d7b
Compare
bce1d7b to
3741bac
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