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

Encourage lighter images and videos #2509

Merged
merged 35 commits into from
Jun 12, 2019
Merged

Conversation

kienstra
Copy link
Contributor

@kienstra kienstra commented Jun 5, 2019

  • As Weston suggested, this replaces the 'full' image size with a custom size of 99999px (unlimited) by 1440px

replace-previous-size-here

Closes #2332

kienstra added 5 commits June 1, 2019 16:16
Do this on the componentDidUpdate method,
as it seems that this is where it is updated.
Also, revert a change to webpack.config.js
that I accidentally staged before.
This is only used on one place,
so it is probably better in that class.
@googlebot googlebot added the cla: yes Signed the Google CLA label Jun 5, 2019
@westonruter
Copy link
Member

Make sure this new image size is used for background image.

@kienstra
Copy link
Contributor Author

kienstra commented Jun 5, 2019

Sure, I'll work on that now.

Use the newly added 'amp_story_page' image size
for the AMP Story background image.
const sizesWithoutFullSize = initialSizes.filter( ( size ) => 'full' !== size.slug );

// If the AMP Story slug isn't present, add it.
if ( ! sizesWithoutFullSize.filter( ( size ) => MAX_IMAGE_SIZE_SLUG === size.slug ).length ) {
Copy link
Contributor Author

@kienstra kienstra Jun 5, 2019

Choose a reason for hiding this comment

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

It'd be nice if this logic to add the image size didn't have to run so often. For example, if it ran only on wp.domReady().

But it looks like the settings update, so these settings will be overwritten if this logic only runs on wp.domReady().

kienstra added 4 commits June 5, 2019 00:26
…ably need a better solution

There was an error:
447:16  error  'select' is already declared in the upper scope  no-shadow
Use the 'image_size_names_choose' filter
to make the custom image size available
in the media const in amp-story-page/edit.js
This prevents making a second request for it.
Also, this enables changing the paramter
ownSelect to select.
This is the first iteration, and it might
need more information in the notice.
@kienstra
Copy link
Contributor Author

kienstra commented Jun 6, 2019

Current Display For Large Video Sizes

bd8d73b adds a notice on selecting a video as the Background Media if it has more than 1 MB per second, like more than 5 MB for a 5-second video.

  1. This might need better styling.
  2. Maybe this could use more information, like the size of the video and its length.

large-video-file-size

To test this, you could download this image, which is 33 seconds and 252 MB. This should display the notice.

@westonruter
Copy link
Member

@kienstra I'm getting an error:

image

kienstra added 6 commits June 6, 2019 23:49
Instead of an initial let assignemnt and an if block,
this simply assigns it all in one line.
Also, simplify reduceRight to reduce.
The order doesn't seem to matter,
so it doesn't have to be from the right.
As Weston mentioned,
it will still work with a large video.
As Weston suggested, the megabytesInBytes constnt
can also be used in the admin notice.
As Weston suggested, it's a little less repetetive
not having 'AMP' in the name.
If the 'full' size image is never needed in AMP stories,
this could be much simpler.
It's removed whenever the 'post_type' is 'amp_story'.
This might need more thought, though.
@kienstra
Copy link
Contributor Author

kienstra commented Jun 7, 2019

@kienstra I'm getting an error:

Thanks for letting me know. With 236e226 removing the class in with-replaced-full-size-image.js, that shouldn't be an issue anymore.

kienstra added 2 commits June 7, 2019 01:35
Also, on clicking 'Remove Video,'
set that attribute back to undefined.
add_new_max_image_size() actually applies
to core Image blocks also.
@kienstra kienstra changed the title [WIP] Encourage lighter images and videos Encourage lighter images and videos Jun 7, 2019
Use this image size if it's available,
otherwise simply default to the full size image.
@swissspidy swissspidy requested a review from westonruter June 10, 2019 19:25
kienstra added 6 commits June 10, 2019 23:34
Before, this didn't work with just the filter
for 'image_size_names_choose'
so get the correct image size after
the actual image is chosen.
The filter didn't have access to the post type,
so it would run on posts and pages,
needlessly adding the 'amp_story_page' custom size.
This approach isn't ideal either,

but it won't run outside of AMP stories.
It was defined in the entire module,
though also used as a parameter of withSelect().
This isn't ideal, but select() is needed
to get the image size.
This seems to help with an issue
where getMedia() didn't get the background image before.
I couldn't figure out how to enable a custom
size for the background image with purely JS.
This won't apply to Image blocks in a non-story
editor, like for posts.
@kienstra
Copy link
Contributor Author

Hi @westonruter,
Thanks for your patience here.

0bd76bc#diff-37ed1ba86eaee9e72c2ced620309452bR1545 adds a conditional in the PHP filter to check that 'query-attachments' === $_POST['action'].

This allows it to apply to the background image, but not to the Image block's 'Image Size' <select> element (or an Image block in a non-story editor).

I also brought back the JS filter, like earlier in this PR, which replaces the Image block's Full size with the amp_story_page size.

This time, it didn't see the Uncaught TypeError: Cannot call class as a function error that you found earlier. But it'd be interesting to know if you see that error.

@kienstra
Copy link
Contributor Author

Hi @swissspidy,
Thanks a lot for your help on #2389.

Would you be available to review this again? Thanks, Pascal!

…ide-lighter-media

* 'develop' of github.com:ampproject/amp-wp:
  Update dependency css-loader to v3 (#2563)
  Consistently import components from block-editor package (#2561)
  Refactor AMP modes (#2550)
  Update dependency webpack-cli to v3.3.4 (#2558)
@westonruter
Copy link
Member

@kienstra I'm getting a JS error upon loading the editor:

image

@kienstra
Copy link
Contributor Author

@kienstra I'm getting a JS error upon loading the editor:

OK, thanks. The JS filter is probably responsible for that.

…ide-lighter-media

* 'develop' of github.com:ampproject/amp-wp:
  Fix resizing for formatting blocks (#2562)
  Prevent block invalidation due to missing width/height (#2557)
@kienstra
Copy link
Contributor Author

Nice, great idea: fb29ca8

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 error is happening here:

image

Where Component is the extended BlockEdit class:

image

Based on my limited knowledge of React, I was thinking something like this is needed:

--- a/assets/src/stories-editor/components/higher-order/with-replaced-full-size-image.js
+++ b/assets/src/stories-editor/components/higher-order/with-replaced-full-size-image.js
@@ -19,7 +19,7 @@ const BLOCK_EDITOR_STORE = 'core/block-editor';
 
 export default createHigherOrderComponent(
 	( BlockEdit ) => {
-		return class extends BlockEdit {
+		class BlockEditWithReplacedFullImageSize extends BlockEdit {
 			/**
 			 * The class constructor.
 			 */
@@ -67,7 +67,9 @@ export default createHigherOrderComponent(
 					dispatch( BLOCK_EDITOR_STORE ).updateSettings( { imageSizes: sizesWithoutFullSize } );
 				}
 			}
-		};
+		}
+
+		return ( props ) => <BlockEditWithReplacedFullImageSize { ...props } />;
 	},
 	'withReplacedFullSizeImage'
 );

But that's not making any change. Perhaps its just syntactic sugar for the exact same thing.

}

if ( super.componentDidUpdate ) {
super.componentDidUpdate();
Copy link
Member

Choose a reason for hiding this comment

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

I'm no React expert, but shouldn't this be passing the args passed into componentDidUpdate? In other words:

super.componentDidUpdate( ...arguments );

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, good point.

@westonruter
Copy link
Member

I don't see any other example in the codebase of BlockEdit extended, so perhaps this is not the right approach. Composition should be used instead?

@kienstra
Copy link
Contributor Author

I don't see any other example in the codebase of BlockEdit extended, so perhaps this is not the right approach. Composition should be used instead?

Good point, inheritance isn't a best practice in React, it seems. The only issue is that this needs to 'hook' into the Image edit component's componentDidUpdate().

But if this extended it with a higher-order-component, the HOC probably wouldn't call its componentDidUpdate method when the Image component's componentDidUpdate method runs.

Maybe there's another approach, though.

@kienstra
Copy link
Contributor Author

I'll keep looking at this.

The JS filter produced a console error.
So instead, use the existing PHP filter
that Weston had suggested.
@kienstra
Copy link
Contributor Author

Hi @westonruter,
Thanks for your help here.

Does c330ce7 work for you? It uses the PHP filter that you had suggested originally. It works for me locally.

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.

This does work!

{
sprintf(
/* translators: %d: the number of recommended megabytes per second */
__( 'A video size of less than %d MB per second is recommended.', 'amp' ),
Copy link
Member

Choose a reason for hiding this comment

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

Minor thing but it would be useful to know how many MB/second the selected video is, to know how far from the target the video is.

@westonruter westonruter merged commit 7409759 into develop Jun 12, 2019
@kienstra
Copy link
Contributor Author

@westonruter, thanks a lot for your help and patience!

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.

Guide users to not use heavy assets when creating stories
4 participants