-
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
Update/image block #11377
Update/image block #11377
Conversation
- Add `srcset` and `sizes` inside the editor. - Add `data-wp-attachment-id` and `ata-wp-percent-width` to the img tag. Will be used on the front-end to properly set `srcset` and `sizes`, and also img `width` and `height` where missing. - Improve setting/resetting the image dimensions.
Add `srcset` to the attachment data from the API and to the data in the media modal.
…d warnings after ignore!)
Most functionality works as expected. Still todo: image dimensions by entering width or height. These need to override the `scale` attribute.
- Don't save srcset and sizes in block props. - Better names? - Clean up few eslint-disable-*.
Tweak some names, clean up few things, simplify the php parts, fall back to the original block html if some image data is not there.
- Better handling of existing (old) image blocks and just uploaded images. - Add support for scaled external images.
@@ -181,6 +181,8 @@ module.exports = { | |||
}; | |||
} ), | |||
} ], | |||
// TODO: removeme, maybe? Without this linting fails horribly on Windows, many thousands of false positives :) | |||
// 'linebreak-style': 'off', |
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.
Is this supposed to be part of this PR?
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.
Not really. Still work-in-progress though, will do the TODO
:)
), | ||
'align' => array( | ||
'type' => 'string', | ||
'enum' => array( 'center', 'left', 'right', 'wide', '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.
From my testing in my own PRs, the empty ''
is unnecessary.
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.
Yeah, probably. However it makes that a bit more readable, i.e. all "possible values" are listed (so somebody looking at that place in couple of years won't think it's weird/buggy) :)
This was actually copied from https://github.com/WordPress/gutenberg/blob/master/packages/block-library/src/latest-comments/index.php#L177.
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.
Technically, ''
isn't actually a value that the attribute ever gets in practice. It technically gets null
when no value is set, right? I mean, due to JavaScript type coercion, those 2 values end up being treated the same, but if you're going to put it there to make things clearer, I think putting null
there would make more sense.
But by that logic, almost every instance of enum
should include null
or ''
when the attribute is optional for the block. I don't think it makes the code more readable; I think it actually createst confusion, because it implies that you can disallow an empty string or null
value by removing it from the enum, which is not the case. Essentially, it's dead code that has no purpose, but implies that it does by existing.
I actually made #9832 to remove the ''
from the Latest Comments block a while back.
Yeah, that's pretty weird though, not sure where these come from as I'm on Windows, perhaps some git "internals"? Looking there, do we really want 755 on .js files? Thought (source) files should not have the exec bit set, i.e. these files actually need 644? Hmmm, these permission changes seem to -sometimes- happen when I |
This (experimental) PR introduces quite a few changes. Been thinking how we can split it to make it a bit more manageable :) The larger changes are:
As far as I see 1, 3 and 4 are "blockers". To implement 3 and 4 we will need 5. Once we have 1, 3, 4, and 5 in place the rest would be pretty easy to add. |
Discussing how to best handle this (thanks @catehstn), perhaps we can do 1 and 5 in parallel in separate PRs, then add 3 and 4 as they are essential for continuing to support responsive images on the front-end. Then 2, 6 and 8 can be added easily with everything else in place, and I'll make separate issue for 7. @mtias does that sound ok? :) |
I recommend fast-tracking the components that make theme integration possible so we can get it baked into Twenty Nineteen and tested at scale. Once we know it works, the functionality needs to be communicated to all theme developers to ensure themes are handling responsive images properly. This is especially the case for any theme claiming to be "Gutenberg ready". I have time set aside to submit PRs for Twenty Nineteen to get this sorted and to write an exhaustive article / documentation on how to get this right on the front end to avoid massive performance hits across the web because of a WordPress update. |
@mor10 sure, think we can handle that on #5650. The "example" code is: https://github.com/WordPress/gutenberg/pull/11377/files#diff-ad1f24a74f64a1c137bfb28da9349bd1R30 but instead of a global better to use a function or two, and "normalize". So themes could do
and we will store that "somewhere". Then have a Of course this will also need some JS and CSS to set these widths globally in the editor. |
@azaozz I'm not clear on how this would work for the |
@azaozz thanks for breaking this down, it's very helpful, and I agree with you that 1, 3, and 4 are blockers. I'm not sure that 5 is necessary, because the responsive filters on I have some time to dedicate towards moving these pieces forward. Are there any that you would prefer I focus on? |
Re item 5, #11523 might be an entry point to being able to filter the image block's output without making it explicitly a dynamic block. |
@chrisvanpatten yep, good catch, thanks for linking that. If block attributes are also passed to the filter proposed in #11523, there will be no need to make the image block dynamic. @joemcgill # 5 is needed if we want # 3. Also, we need to pass some more data from the editor to the front-end and the best way to do that is in block attributes (block props). Currently this PR makes the image block dynamic so that mechanism works, but if the filter from #11523 is implemented (and gives access to block attributes), we won't need that.
That's great! IMHO the most important thing that we need to get right is how to best calculate the Currently we always look at the image width in pixels (as the image file dimensions are in pixels). This lets us calculate a "compatible" Additionally (in this PR) the image tag What
|
Could this PR be milestoned 4.4? |
@azaozz re:
The intention for the default I think the best way for themes to handle this would be for us to calculate |
linkDestination: { | ||
type: 'string', | ||
default: 'none', | ||
}, | ||
srcSet: { |
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.
"for the editor's sake" is not something I think we should want as a reason for an attribute to exist. As I see it, the attributes should contain the minimal normalized data to be able to reconstruct the image in any context. fileWidth
, fileHeight
, srcSet
, sizes
are redundant if the id
is already known.
function _gutenberg_cache_images_meta( $content ) { | ||
// Need to find all image blocks and get the attachment IDs from them BEFORE the parser is run | ||
// so we can get the image attachments meta all at once from the DB. | ||
if ( preg_match_all( '/^<!-- wp:image {.*"id":(\d+),.*} -->$/m', $content, $matches ) ) { |
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.
Is this something where being able to hook to the parse (#10108) would prevent us from needing to try to match via a regular expression?
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.
To cache the attachments meta we will need to know all attachments IDs before the meta is needed. The point is to do one call to the DB for all the meta data, not multiple calls. We would need to process all "image" blocks, all "media and text" (when an image is used), "gallery", and "cover".
I know it's somewhat hacky to use a regex, but not sure we can get all these IDs before callbacks have started/blocks are processed.
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.
Maybe then in addition to block_pre_render
it would be useful to have a filter which occurs immediately following the do_blocks
parse, before the loop to render each individual block.
cc @dmsnell
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.
fileWidth, fileHeight, srcSet, sizes are redundant if the id is already known.
Well, you'd think so but images (and image meta) is "the land of thousand edge cases" :) Example: the website was "exported" and then imported into another site. Now all attachment IDs stored in post_content are "wrong". (Well in this case WP will also fail to generate the srcset).
It would be beneficial to have some more info/meta about the image from the moment it was added to the post. Also, that somewhat helps with external images.
The srcSet
and sizes
would be pretty nice to have inside the editor but not essential for now. I'm almost ready with the next chunk split off this PR, there they are not used. However it has fileWidth
, fileHeight
, userWidth
, userHeight
for when the user set these manually (can reuse width and height and just add one prop indicating this), and editWidth
that lets the theme scale images according to the width of the block in the editor when the post was created. Of course, none of these are set in stone, lets move the discussion to that pr (I'll ping you there).
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.
in addition to block_pre_render it would be useful to have a filter which occurs immediately following the do_blocks parse, before the loop to render each individual block.
this is how I imagined we would do things like what's being discussed here. if we want to do post-wide operations or side-effecting transformations then we can filter each block and hold state in the filter function with a static variable or class property, then a closing operation at the end. I hadn't thought about placing that last filter though in this context, so thanks for pointing it out - I'll add it to #10108
Trying to digest this and #6177, there's a lot said about what should be shown on the front-end, what should be shown in the editor. I don't see so much about what's actually saved to disk (the content in the database). It's significant because the front-end and editor both have means to reconstruct what should be shown (by And if the primary concern is what's shown on the front-end, could we focus on that and disregard what's shown in the editor for now? |
Right. As image meta can be somewhat inconsistent and even change after the post is published, the idea is to store some data about the image while the post is created. Ideally that will be enough data so the theme could "calculate" a more precise |
This didn't get to WordPress 5.0 and 5.1. What are the next steps planned, should it be refreshed? @youknowriad and @mapk any thoughts? |
Hey @azaozz and others. Can we get a status update to how this PR is doing? Let us know how we can help to move it onward. Thanks. |
Any updates on this one? Noticing some strange issues with the image block and the crops/image sizes that are being used |
Trying to triage PRs today. It seems that this PR is stale. I'm going to close it, for now. Thanks all for your efforts. We should consider opening smaller targetted PRs to fix the existing image issues. |
Fixes and updates to handling images in the image block.
There is only one small UI change: the "Image Size" drop-down in the inspector is reduced to only show "Default" and "Thumbnail" sizes, plus any cropped sizes added by themes and plugins. The reason is that selecting an image by file size is a thing from the past. All available sizes will be in the
srcset
attribute in the<img>
tag, so the browser is not constrained to a specific file.Another change is that the 25, 50, 75, 100% buttons work properly (visually) now. Before they were calculated from the image file dimensions. As that was usually a huge image/photo changing them had no effect at all. Now they are based on the actual block width, so setting several different images to 50% width will properly make them the same size.
Another somewhat visible change is that images inside the editor support high-density screens as there are
srcset
andsizes
attributes.The under-the-hood changes are a lot more:
width
andheight
attributes on the front.$content_width
or the new$block_width
).srcset
andsizes
attributes are properly calculated for all images (after the width and height are determined.$block_attributes
array to thewp_calculate_image_srcset
andwp_calculate_image_sizes
filters allowing generation of much better values by the themes.See #6177.