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

Chrome: Adding the featured image panel #894

Merged
merged 3 commits into from
May 26, 2017
Merged

Conversation

youknowriad
Copy link
Contributor

closes #855

This PR adds the featured image panel. Its behaviour is similar to the current WordPress editor featured image panel.

This raises the question of async loading of WP entities (media). Should we store those in the state? Should we provide QueryComponents (ala Calypso). For now, to limit the scope of this PR, I'm just fetching the media inside the component.

@youknowriad youknowriad added the General Interface Parts of the UI which don't fall neatly under other labels. label May 24, 2017
@youknowriad youknowriad self-assigned this May 24, 2017
@jasmussen
Copy link
Contributor

I love it, works well, exactly how it should be. Zero concerns from my side. 👍 👍

@aduth aduth mentioned this pull request May 25, 2017
@@ -0,0 +1,3 @@
const spinner = <span className="spinner is-active" />;
Copy link
Member

Choose a reason for hiding this comment

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

Nice optimization 👍 (related)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have a good teacher :)

loading: true,
} );
const mediaIdToLoad = this.props.featuredImageId;
new wp.api.models.Media( { id: mediaIdToLoad } ).fetch()
Copy link
Member

@aduth aduth May 25, 2017

Choose a reason for hiding this comment

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

Backbone.Model#fetch returns a jqXHR (reference), which itself is a superset of XMLHttpRequest (reference), which makes available an abort method (reference).

Maybe rather than tracking this.mounted, we can store a reference to the request here and abort it if the component unmounts? I'm not sure what effect this has on promise resolution though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll try it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Works well in my testings

fetchMedia() {
if ( ! this.props.featuredImageId ) {
this.setState( {
media: null,
Copy link
Member

Choose a reason for hiding this comment

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

Maybe moving up the common media: null would make it more reasonable to turn setters into one-liners to trim a few lines and clarify the common behavior.

this.setState( { media: null } );
if ( ! this.props.featuredImageId ) {
	this.setState( { loading: false } );
	return;
}
this.setState( { loading: true } );

It's unclear from the documentation whether the current implementation would cause the render to occur twice by separate calls to setState, so maybe worth avoiding if it does.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried it and it doesn't render twice. updated

@youknowriad
Copy link
Contributor Author

Planning to merge this soon. Let me know if there's any concern.

@youknowriad youknowriad merged commit 65fff05 into master May 26, 2017
@youknowriad youknowriad deleted the add/featured-image branch May 26, 2017 13:06
type="image"
>
{ media &&
<img
Copy link
Member

Choose a reason for hiding this comment

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

One minor thing we might consider is using the media's dimensions to assign a height to the space that the image will occupy, to avoid the jarring appearance of content being pushed down only after the image has loaded.

Height is difficult to calculate unless we know the width of the container, which we probably don't want to assume. We could use a technique for proportionally scaled responsive containers though, using padding to dynamically fill height.

See:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure I understand. The height depends on the loaded image. So even if we know the width container, we don't know the ratio of the loaded image, how can we compute the height in this case?

Copy link
Member

Choose a reason for hiding this comment

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

To your point, more accurately there's two resources that need to be loaded:

  • The media entity REST API resource
  • The image itself

The idea is that after the first of these finishes loading, we know the dimensions of the image and the space we expect it to consume, even before the image itself has finished loading.

Practically speaking this is probably not very noticeable though, since either we've loaded a post with a featured image already assigned and Settings -> Featured Image is likely collapsed, or we've just assigned the image from the media library and the image is already cached by the browser.

Copy link
Member

@aduth aduth May 26, 2017

Choose a reason for hiding this comment

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

Here's maybe a clearer demonstration of the issue/solution and its particular relevance when we don't know the containing width:

https://codepen.io/aduth/pen/jmRGpV

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
General Interface Parts of the UI which don't fall neatly under other labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sidebar: Add Featured Image panel
3 participants