Skip to content
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 caption support to the Gallery block in <amp-carousel> and fix displaying Gallery block as carousel in WP 5.3 #3285

Merged
merged 16 commits into from
Oct 28, 2019

Conversation

kienstra
Copy link
Contributor

@kienstra kienstra commented Sep 17, 2019

Update: this has changed a lot, please see more recent comments

For images in a gallery that have a caption, this adds caption support when 'Display as carousel' is selected:
captions-in-carousel

Before

before-caption

After

caption-appears-now

Closes #2855

(Please do npm run build:css for the CSS here)

For images in a gallery that have a caption,
display them when in an <amp-carousel>.
@googlebot googlebot added the cla: yes Signed the Google CLA label Sep 17, 2019
@kienstra kienstra changed the title Add caption support to the Gallery shortcode and block in <amp-carousel> [WIP] Add caption support to the Gallery shortcode and block in <amp-carousel> Sep 17, 2019
@kienstra
Copy link
Contributor Author

There are probably more styling considerations with this. Sometimes the caption overlaps the <amp-img>.

Simply placing the caption on top of the <amp-img>, with a scrim behind it, could be an option. But the scrim doesn't look good with certain themes.

@westonruter westonruter added this to the v1.3.1 milestone Sep 18, 2019
@kienstra
Copy link
Contributor Author

kienstra commented Sep 20, 2019

Couldn't Get Styling To Work With Core Themes
Maybe a CSS specialist could help

I couldn't get the captions to look good on all of the Core themes, and having them look good on all themes would probably be even harder.

Maybe a CSS specialist could help here.

Also, I tried placing the caption above the <amp-img> and adding a scrim with a black gradient. But sometimes it was too wide:

scrim-wide

Here are the displays with the current state of the PR (without the scrim):

Twenty Fifteen

above-image

Twenty Sixteen

twenty-sixteen

Twenty Seventeen

twenty-seventeen-gallery

Twenty Nineteen

2019-gallery

Steps To Reproduce

  1. Create a new post
  2. Add a gallery block
  3. Upload 600 x 400 images
  4. Add captions to some of the images
  5. Select 'Display as carousel'
  6. Save the post, and go to the front-end

Of course, galleries could have any combination of image sizes, but the screenshots above all use 600 x 400 images to simplify this.

@kienstra
Copy link
Contributor Author

kienstra commented Sep 23, 2019

Before

The plugin output the <amp-carousel> like:

<amp-carousel type="slides" ...>
        <amp-img ...>
</amp-carousel>

After (with captions)

With this PR, the plugin outputs something like:

<amp-carousel type="slides" ...>
        <div class="slide">
                <amp-img ...>
                <div class="amp-wp-gallery-caption">
                        This is a caption
                </div>
        </div>
</amp-carousel>

(Though the AMP runtime adds a lot of tags, like <div class="i-amphtml-slide-item i-amphtml-slide-item-show">)

It looks like amp-carousels with captions have to have something like a <div class="slide"> wrapper for each slide.

But on the Core themes like Twenty Fifteen and Twenty Nineteen, I couldn't find a way to style the caption so it appears in the same place, or at least looks good.

And ensuring that captions look good on any theme would probably be much harder.

I'm not sure if there's a solution for this 😄

@kienstra
Copy link
Contributor Author

Also, please do npm run build:css to compile the CSS locally.

@austintude
Copy link

I've been unable, time wise, to look into this more deeply but the prospect of not only adding but modifying these is of great importance to several of my clients.
In an ideal world, I'd be able to change: font-size, margin, padding, alignment, and other layout typical styling options availble with most text based content.
While the multi-level-wrapping-text is clickable and expands up after being clicked, it is so low on the screen that this function is NOT obvious to the end user/site-visitor. /
Thank you for working on this. Keep up the good work.
Daniel

@westonruter
Copy link
Member

@kienstra I suppose this will then need UX support.

@kienstra
Copy link
Contributor Author

kienstra commented Oct 1, 2019

Hi @westonruter,
Yeah, that makes sense.

If it's alright, we've asked Bart Makos to work on this, as he's much better with CSS than me.

Though I'm not sure there is a good solution for captions, where they'll look good on any theme.

@austintude
Copy link

austintude commented Oct 2, 2019 via email

@kienstra
Copy link
Contributor Author

kienstra commented Oct 2, 2019

Haha, I wish I was great at CSS 😄

@westonruter
Copy link
Member

This may have some inspiration: Automattic/newspack-blocks#143

@kienstra
Copy link
Contributor Author

kienstra commented Oct 8, 2019

Nice, thanks for posting that! It could help.

@swissspidy swissspidy modified the milestones: v1.3.1, v1.4 Oct 17, 2019
It looks like this is working well with the Core themes
Twenty Fifteen through Twenty Nineteen.
Instead of modifying the ->textContent property,
create a new text node,
and append it to the <span> node.
@kienstra
Copy link
Contributor Author

Thanks to Weston's suggestion, captions look good so far. Though there's still some work left.

gallery-here

foreach ( $images as $image ) {
$amp_carousel->appendChild( $image );
$div = AMP_DOM_Utils::create_node(
Copy link
Contributor Author

@kienstra kienstra Oct 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This used to simply append the image to the <amp-carousel>. But now it creates a <div class="slide"> wrapper that includes the image the caption if one exists.

Then, it appends that <div class="slide"> to the <amp-carousel>.

It looks like that's needed in order to display a caption.

It's actually a slide in the <amp-carousel>
Also, add a comment, and remove an extra newline.
@kienstra
Copy link
Contributor Author

kienstra commented Oct 25, 2019

Gallery Blocks With amp-carousel Sometimes Look Different
Especially square images

Some Gallery blocks look different with this PR. But it looks like they better display the images at the width and height that the AMP_Img_Sanitizer produces.

Before

before-g

After

after-g

The <amp-img> above looks like a rectangle, as you'd expect from its width=“640” height=“400”.

This is from the new object-fit="cover" for the <amp-img>, which prevents the caption from overflowing the image:

scrim-wide

Steps to reproduce

  1. Create a new post
  2. Add a gallery block
  3. Upload few 800 x 800 images
  4. Select 'Display as carousel'
  5. Save the post, and go to the front-end of the AMP URL

@kienstra
Copy link
Contributor Author

kienstra commented Oct 25, 2019

Steps to test

  1. Create a new post with a Gallery block
  2. Upload few rectangular and square images to it
  3. Add captions to some, including very long captions
  4. Select 'Display as carousel'
  5. Save the post, and go to the front-end of the AMP URL
  6. Expected: the captions always appear over the image, no matter how long they are:

captions-gallery

  1. Ideally, test this with several themes

This is more important than
text-align: center.
Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The style of the captions overlaying the gallery looks very good now. Nevertheless, I had try to update the gallery block sanitizer to work with the new markup now coming in WordPress 5.3. See #3285 (comment)

I took an initial stab at adding compatibility, but some tests are broken now and need to be fixed. The changes need to be in WordPress 5.2 without Gutenberg active as well, to ensure the carousel is displaying as expected.

@kienstra
Copy link
Contributor Author

kienstra commented Oct 25, 2019

I took an initial stab at adding compatibility, but some tests are broken now and need to be fixed. The changes need to be in WordPress 5.2 without Gutenberg active as well, to ensure the carousel is displaying as expected.

Hi Weston, thanks for making those commits. Ah, I should have tested this with WordPress 5.3.

I'll work on the tests.

assets/css/src/amp-default.css
 76:37  ✖  Expected double quotes   string-quotes
Simply change the ' to ".
Copy link

@claudiulodro claudiulodro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed a couple small bugs in the implementation. In general, though, this works great with and without Gutenberg 6.5+!

' or ',
[
'( parent::figure[ contains( @class, "wp-block-gallery" ) and @data-amp-carousel = "true" ] )',
'( contains( @class, "wp-block-gallery" ) and @data-amp-carousel = "true" )',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you've lost some functionality with this change. Previously you could have the carousel "off" and lightbox "on" for the gallery, but this sanitizer will now ignore that setting combination.

* Element
*
* @var DOMElement $node
*/

$attributes = AMP_DOM_Utils::get_node_attributes_as_assoc_array( $node );
$is_amp_lightbox = isset( $attributes['data-amp-lightbox'] ) && true === filter_var( $attributes['data-amp-lightbox'], FILTER_VALIDATE_BOOLEAN );

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like $is_amp_carousel, $is_amp_lightbox also needs to check the parentNode to see if data-amp-lightbox is set (for Gutenberg 6.5+ compatibility). The simplest way of handling this situation for both $is_amp_carousel and $is_amp_lightbox might be something like this:

$attributes      = array_merge( AMP_DOM_Utils::get_node_attributes_as_assoc_array( $node ), AMP_DOM_Utils::get_node_attributes_as_assoc_array( $node->parentNode ) );

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @claudiulodro, thanks for pointing that out. I ended up doing something similar to that.

In that case, I think the sanitizer
should still run.
This could still use more testing.
Though maybe this isn't applied
as intended.
@kienstra
Copy link
Contributor Author

kienstra commented Oct 25, 2019

Hi @westonruter,
Thanks for your commits, and for testing this.

In my testing, the Gallery block is looking good with the following combinations:

Environment

  1. With Gutenberg active (6.7.0, latest stable)
  2. Without Gutenberg, running WP Core 5.2.4

Gallery Block Settings

  1. Only lightbox
  2. Only carousel
  3. Lightbox and carousel
  4. With and without captions

Though it looks like all Gallery blocks appear with a lightbox, whether it's selected or not. This issue also seems to appear on the develop branch and the last stable release (1.3.0 tag):

light-box-gallery

@kienstra
Copy link
Contributor Author

I noticed a couple small bugs in the implementation. In general, though, this works great with and without Gutenberg 6.5+!

Hi @claudiulodro,
Thanks a lot for testing this. If you have time, it'd be great if you could test this again. This should now work when the carousel is off and the lightbox is on.

@kienstra kienstra changed the title Add caption support to the Gallery shortcode and block in <amp-carousel> Add caption support to the Gallery block in <amp-carousel> Oct 25, 2019
@kienstra
Copy link
Contributor Author

kienstra commented Oct 25, 2019

One thing I missed is that this doesn't add captions to gallery shortcodes with 'Display as carousel' selected, like:

[gallery ids="4572, 3619"]

This PR now only addresses Gallery blocks.

Copy link

@claudiulodro claudiulodro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The latest version looks good. Thanks for making those revisions. 👍

@westonruter
Copy link
Member

One thing I missed is that this doesn't add captions to gallery shortcodes with 'Display as carousel' selected

@kienstra So the only outstanding thing is classic gallery shortcode instances will continue to be rendered without captions? If that's the case, then I think we can merge this and then follow-up with another issue/PR to do the rest.

@kienstra
Copy link
Contributor Author

Hi @westonruter,

@kienstra So the only outstanding thing is classic gallery shortcode instances will continue to be rendered without captions?

That's right, this PR doesn't affect the Gallery shortcode (or the Classic editor galleries, which use that shortcode).

@westonruter
Copy link
Member

OK great, so [gallery] shortcode use will appear no different for users, while at the same time this change fixes the Gutenberg gallery block integration to ensure that the Carousel setting actually has an effect:

Non-AMPAMP BeforeAMP After
Screen Shot 2019-10-28 at 14 08 12Screen Shot 2019-10-28 at 14 08 24Screen Shot 2019-10-28 at 14 08 48

@westonruter westonruter changed the title Add caption support to the Gallery block in <amp-carousel> Add caption support to the Gallery block in <amp-carousel>; fix displaying Gallery block as carousel in WP 5.3 Oct 28, 2019
@westonruter westonruter changed the title Add caption support to the Gallery block in <amp-carousel>; fix displaying Gallery block as carousel in WP 5.3 Add caption support to the Gallery block in <amp-carousel> and fix displaying Gallery block as carousel in WP 5.3 Oct 28, 2019
@westonruter westonruter merged commit 7cae80e into develop Oct 28, 2019
@westonruter westonruter deleted the add/gallery-caption branch October 28, 2019 21:17
@kienstra
Copy link
Contributor Author

@kienstra So the only outstanding thing is classic gallery shortcode instances will continue to be rendered without captions? If that's the case, then I think we can merge this and then follow-up with another issue/PR to do the rest.

Hi @westonruter,
Thanks a lot for reviewing this. Would it be alright if I opened an issue for [gallery] shortcode captions, then worked on a PR for that?

The support topic that prompted this PR's issue (#2855) looks to use a Gallery shortcode.

Thanks, Weston.

@austintude
Copy link

austintude commented Oct 28, 2019 via email

@westonruter
Copy link
Member

@kienstra por supuesto. 👍

@kienstra
Copy link
Contributor Author

Gracias!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Signed the Google CLA
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add image captions to the gallery carousel
6 participants