-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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 to gallery images #4199
Conversation
aef1927
to
9fede69
Compare
blocks/library/gallery/block.js
Outdated
this.setState( { | ||
selectedImage: index, | ||
} ); | ||
this.props.setFocus( focusValue ); |
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 wonder how this works. the setFocus
should be passed an object. Maybe something like this { index }
to keep track of the focused caption.
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.
Hi @youknowriad, normally we pass an object in the format { index: val, ... focusValue }, but in this case, we already had a mechanism to track the active image so we pass only focus value and we make use of the existing mechanism focus={ this.state.selectedImage === index && focus ? focus : undefined }. I think adding a new property to the focus value to track the active image would be redundant in this case given that we already have a mechanism for that.
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.
Ok, so this means, if you edit the caption of the image, it's selected. (not against this) but I can't test for now as the TinyMCE CDN is broken.
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.
Yes, that's right when we try to edit a caption we automatically select that image.
9fede69
to
145252a
Compare
Somehow I was expecting all caption fields to pop up as soon as the block is selected, and empty ones to disappear again if they are empty. Currently I have to selected the gallery block (nothing happens), then select an an image (focus immediately jump to the caption, which does not happen in a normal image block), and moving selection to another block does not hide the empty caption field. I'm also seeing the black background overlapping with the blue border (Chrome): |
145252a
to
7736ebf
Compare
blocks/library/gallery/block.js
Outdated
@@ -56,13 +57,23 @@ class GalleryBlock extends Component { | |||
} | |||
|
|||
onSelectImage( index ) { | |||
return () => { | |||
return ( event ) => { | |||
// ignore clicks in the editable caption |
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.
Why?
If it's the case that we want to avoid calling setState
to the already-selected image, should we not do this more generically to test whether the index
is any different than state.selectedImage
?
In either case, a more detailed comment would be helpful.
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.
Hi @aduth, I will add a more detailed comment 👍 The reason of this check is that without it, making changes in the editable selects/un-selects the image, e.g when we try to select text to make it bold we would be selecting/unselecting many times during this process.
7736ebf
to
f8b95d2
Compare
The points raised were addressed 👍 |
There are a few visual issues we should work through before approving.
Can we add more space to the toolbar and put higher, above the content?
As a point to consider, maybe we should not show all captions by default. What about showing on just the first or showing only when you click on an image. Right now the wall of the same text visually incredibly distracting. |
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 have requested changes in comments.
f8b95d2
to
1319c46
Compare
Hi @karmatosed thank you for your design review.
|
55d454f
to
4c125b9
Compare
Hi @karmatosed, maybe it was some caching issue or other strange intermittent problem. But with the merge of the last changes to gallery markup to render images in a list this had to suffer considerable changes in the code. If in your tests the design does not appear like that, please let me know and I will try to debug further. |
blocks/library/gallery/block.js
Outdated
@@ -74,7 +92,8 @@ class GalleryBlock extends Component { | |||
} | |||
|
|||
onSelectImages( imgs ) { | |||
this.props.setAttributes( { images: imgs } ); | |||
const images = imgs.map( img => img.caption ? { ...img, caption: [ img.caption ] } : img ); | |||
this.props.setAttributes( { images } ); |
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.
Minor: I would probably use something more descriptive like:
onSelectImages( images ) {
this.props.setAttributes( {
images: images.map( ( attributes ) => ( {
...attributes,
caption: attributes.caption || [],
} ),
} );
}
Additionally, why [ img.caption ]
? Shouldn't img.caption
be an array?
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 followed your suggestion and used most of your code, thank you :)
Regarding this part:
Shouldn't img.caption be an array?
This function executes when the images are selected from the media library, and we receive a string from it. That's why we wrap in an array so it is ready to be used in the editable.
return ( event ) => { | ||
// ignore clicks in the editable caption. | ||
// Without this logic, text operations like selection, select / unselects the images. | ||
if ( event.target.tagName === 'FIGCAPTION' ) { |
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.
What if deselecting a gallery item works similarly to selecting and deselecting blocks? So a click inside the block would not deselect the block, only a click outside it or in another block would.
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 was thinking something like:
onSelectImage( index ) {
return ( event ) => {
this.setState( ( state ) => ( {
selectedImage: index,
} ) );
// unfocus currently focus editable
this.props.setFocus( { ...this.props.focus, editableIndex: undefined } );
};
}
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.
What if deselecting a gallery item works similarly to selecting and deselecting blocks? So a click inside the block would not deselect the block, only a click outside it or in another block would.
That's a nice suggestion, and I think it may be a good idea, but maybe we can do it in a different PR so we can discuss this change and receive more feedback if this is something we should pursue or no.
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.
Well, I think this change would make the image and gallery block behave consistently and at the same time simplify the code.
It works really nicely now! |
@jorgefilipecosta cache could have been the issue yes - it seems to work now thanks. |
4c125b9
to
8374158
Compare
8374158
to
6a6996b
Compare
6a6996b
to
0dbe86e
Compare
Hi @iseulde, thank you for your feedback, this PR was updated and multiple line support was added. |
I'm seeing a glitch now. When I put select an image, the caption field "jumps" for a millisecond. |
4a71b77
to
13388dd
Compare
Hi @iseulde it looks like the blink in the caption was caused by a CSS rule, I used an equivalent one and things look fine now.
They should immediately select when clicking on them even if the block was not selected like in master. I tried to find a cause for that not happening in your tests. And it looks like if developer tools are open that does not happen (also in master). Maybe a bug a in the browser. Thank you for your tests! |
blocks/library/gallery/editor.scss
Outdated
} | ||
|
||
.blocks-rich-text figcaption:last-of-type { | ||
position: relative; |
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.
What does this do?
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 see the reason now, too bad about the rich text wrapper. Still not understanding the last-of-type
though.
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 want position relative, to figcaption elements in the editor except when we have a placeholder. In this cases, RichText creates 2 figcaption the first should use absolute and second relative like in the other cases. In order to not overwrite the property of the first figcaption when placeholder is visible, this rule was used. Relying on attributes like data-is-placeholder-visible caused blinks because the attributes are not immediately added to the dom. So I updated the rule and in this version, it seems there are no blinks.
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.
Could you leave a comment?
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.
Comment added.
blocks/library/gallery/editor.scss
Outdated
@@ -13,6 +12,26 @@ | |||
&.is-transient img { | |||
@include loading_fade; | |||
} | |||
|
|||
.blocks-rich-text__inline-toolbar { |
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.
Should be .block-rich-text__inline-toolbar
I think.
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.
Well, it's a typo in the RichText
component it seems. :)
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.
(But this rule won't do anything.)
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.
Yes the name was updated 👍 The spacing was also updated because right our css rules changed and we don't require a big margin.
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 it still needed at all? As long as the toolbar appears outside the black area, it's good. I didn't see it appearing inside the black area before.
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.
Yes in this layout the toolbar is not bad even without the rule. So I removed it.
blocks/library/gallery/editor.scss
Outdated
@@ -13,6 +12,26 @@ | |||
&.is-transient img { | |||
@include loading_fade; | |||
} | |||
|
|||
.blocks-rich-text__inline-toolbar { | |||
margin-top: -1.8em; |
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.
Why change this?
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.
Without this change, the inline toolbar is very close to the caption text this creates a space so things look better.
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 doesn't look like the rule is applied though. See above.
I think it has something to do with the block scrolling into view. Anyway, if it's present in master we can open a new issue. :) |
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.
Works nicely. Would be nice to address the comments.
8f7c54a
to
d9d337f
Compare
Squashed commits: [f08184b] Made gallery to image and image to gallery transformations take into consideration caption attributes. [0cf5d9e] Implemented caption editing and styles in the gallery block. [ebca968] Added caption to the attributes of images in the gallery and to the saving logic. [465b2c7] Added caption slimImageObject set of MediaUploadButton. This allows us to read to image caption from the media library in the gallery block.
d9d337f
to
c854709
Compare
Description
This PR adds caption to individual images in the gallery block addressing #1443
How Has This Been Tested?
Add gallery block, insert images from the media library, some with captions some without a caption. Verify that for the ones with the caption, the caption editable is pre-populated, verify it is possible to change captions.
Save the post preview and checks things look identical to what appeared in the editor.
Screenshots (jpeg or gifs if applicable):