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

PLANET-4810: Convert Media Block to WYSIWYG #376

Merged
merged 11 commits into from
Nov 6, 2020
Merged

Conversation

pablocubico
Copy link
Contributor

@pablocubico pablocubico commented Sep 2, 2020

Ref: https://jira.greenpeace.org/browse/PLANET-4810

I'm testing a "canonical save" approach, it uses the save() method as is intended by Wordpress.

There is a caveat to it: the save() method needs to be a pure function with no side effects or hooks, static content is saved in editing time, so we can't change the styles of a block saved using this method in the future. For example, any changes to MediaVideoElement will cause a validation error in the block editor.

So we have 3 options when rendering blocks:

  1. Render in the client side using our frontendRendered, which stores a static DOM node as a placeholder, in which we inject the React component at run time on the frontend.
  2. Render on the server side using Twig and ServerSideRender.
  3. Save the content as static content using save() and a "pure" component.

Currently we are leaning towards (1), this makes perfect sense for heavily interactive blocks as the Spreadsheet. Pros: we can do a lot using React in the frontend. Cons: it may have some penalizations for SEO and no-script situations or JS errors.

This block uses (3), which is the "WP endorsed" way of dealing with static blocks. Pros: ideally, the content is stored in its "pure" form, with no side effects or after-render actions. But for example, in the case of MP4 files, this block adds some DOM manipulations after the initial render. This should only be used in cases where the content is absolutely static. Cons: we can't change the block once its saved. Any changes to the code generating the output will cause a validation error in the editor. We do those kind of changes sometimes, so maybe we don't what that resilience to change.

Method (2), the Twig templates: maybe this is not a bad option for some blocks, as it allows for server-side rendering dynamic blocks and integrates well with some WP flows. Pros: it's rendered on the server side and we have full control of what happens before the output, preventing any FOUC. We can change the way a block is rendered over time and it won't hurt the block in the editor. We can make retroactive changes to blocks inside content. Cons: we have to replicate the block in React to make it WYSIWYG in the Editor, duplicating the markup in JSX and Twig versions.

@pablocubico pablocubico requested a review from a team September 2, 2020 10:21
@pablocubico pablocubico self-assigned this Sep 2, 2020
@pablocubico pablocubico requested review from sagarsdeshmukh and mleray and removed request for a team September 2, 2020 10:21
Copy link
Contributor

@mleray mleray left a comment

Choose a reason for hiding this comment

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

I think there is still some old code that needs to be removed:

  • in test-shortcode-converter.php: here and here
  • in class-shortcode-to-gutenberg.php: here

@pablocubico pablocubico force-pushed the planet-4810 branch 2 times, most recently from 4b30292 to f2dbb02 Compare September 9, 2020 03:13
Copy link
Contributor

@mleray mleray left a comment

Choose a reason for hiding this comment

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

Looks pretty good 👍 I've added a few comments, mostly for small code clean-ups & improvements! Also the failing tests need to be fixed, by removing the old acceptance tests (+ create follow-up ticket to add JS acceptance tests) and fix some php spacing 🙂

assets/src/blocks/Media/MediaEditor.js Outdated Show resolved Hide resolved
assets/src/blocks/Media/MediaEditor.js Outdated Show resolved Hide resolved
assets/src/blocks/Media/MediaEditor.js Outdated Show resolved Hide resolved
assets/src/blocks/Media/MediaEditor.js Outdated Show resolved Hide resolved
assets/src/blocks/Media/MediaEditor.js Outdated Show resolved Hide resolved
assets/src/blocks/Media/MediaEditor.js Outdated Show resolved Hide resolved
assets/src/blocks/Media/MediaEditor.js Outdated Show resolved Hide resolved
assets/src/blocks/Media/MediaEditor.js Outdated Show resolved Hide resolved
assets/src/blocks/Media/MediaFrontend.js Outdated Show resolved Hide resolved
assets/src/blocks/Media/MediaEmbedPreview.js Outdated Show resolved Hide resolved
@pablocubico
Copy link
Contributor Author

@mleray comments addressed and rebased to master!

Copy link
Contributor

@mleray mleray left a comment

Choose a reason for hiding this comment

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

After testing this on local, it looks like existing blocks are not rendered at first and need to be re-saved! We could fix that by adding a render_callback as we did for other blocks 🙂

@pablocubico pablocubico force-pushed the planet-4810 branch 3 times, most recently from 290f8aa to f522bda Compare September 17, 2020 12:37
@pablocubico
Copy link
Contributor Author

After testing this on local, it looks like existing blocks are not rendered at first and need to be re-saved! We could fix that by adding a render_callback as we did for other blocks 🙂

@mleray So I added the render_callback as in the other blocks but I just realized this is a static block, the output of save() is stored as is and the block is rendered statically, so it doesn't need it... I need to figure out what to do with existing content, hold on! 🤔

@pablocubico pablocubico force-pushed the planet-4810 branch 3 times, most recently from 89da4d9 to c33208b Compare September 22, 2020 20:37
@pablocubico pablocubico force-pushed the planet-4810 branch 2 times, most recently from f98153c to 6710883 Compare September 25, 2020 22:48
Copy link
Contributor

@Inwerpsel Inwerpsel left a comment

Choose a reason for hiding this comment

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

Having the frontend wait for php, which in turns waits for youtube's (or other) oembed endpoint response so that it finally can start performing requests for the embedded content, seems like something we probably want to avoid unless there is no other way, as it will be really slow.

We can have a look at the different embed types we offer and whether any of them has content that changes over time. If none of them do then saving it statically seems to make a lot more sense.

assets/src/blocks/Media/MediaBlock.js Outdated Show resolved Hide resolved
assets/src/blocks/Media/MediaEditor.js Outdated Show resolved Hide resolved
assets/src/blocks/Media/MediaEditor.js Outdated Show resolved Hide resolved
assets/src/blocks/Media/MediaEditor.js Outdated Show resolved Hide resolved
assets/src/blocks/Media/MediaEditor.js Outdated Show resolved Hide resolved
assets/src/blocks/Media/MediaEditor.js Outdated Show resolved Hide resolved
assets/src/blocks/Media/MediaElementPlugin.js Outdated Show resolved Hide resolved
assets/src/blocks/Media/MediaFrontend.js Outdated Show resolved Hide resolved
assets/src/blocks/Media/MediaFrontend.js Outdated Show resolved Hide resolved
@pablocubico
Copy link
Contributor Author

Having the frontend wait for php, which in turns waits for youtube's (or other) oembed endpoint response so that it finally can start performing requests for the embedded content, seems like something we probably want to avoid unless there is no other way, as it will be really slow.

We can have a look at the different embed types we offer and whether any of them has content that changes over time. If none of them do then saving it statically seems to make a lot more sense.

As said in one of the comments, the endpoint is only for old content, new content is statically saved and rendered directly here: https://github.com/greenpeace/planet4-plugin-gutenberg-blocks/pull/376/files#diff-ea623fe817268e0262e2bec08bdf6098R39

This is just to support old content without a migration, we can remove it if we migrate old Media blocks to static blocks in the near future.

@pablocubico pablocubico force-pushed the planet-4810 branch 5 times, most recently from 2632c32 to 8327daa Compare October 24, 2020 15:15
@pablocubico
Copy link
Contributor Author

@lithrel Indeed, I moved the video poster selector to the sidebar.
@Inwerpsel youtube_id deprecated & migrated, the MediaElementJS plugin is now loaded via useScript & useStylesheet.

Copy link
Contributor

@Inwerpsel Inwerpsel left a comment

Choose a reason for hiding this comment

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

Currently turning the youtube url into a nocookie one will not work anymore, definitely one to address. Also some things we can simplify a lot.

assets/src/blocks/Media/MediaBlock.js Outdated Show resolved Hide resolved
assets/src/blocks/Media/MediaEditor.js Outdated Show resolved Hide resolved
assets/src/blocks/Media/MediaEditor.js Outdated Show resolved Hide resolved
assets/src/blocks/Media/MediaEditor.js Outdated Show resolved Hide resolved
assets/src/blocks/Media/MediaEditor.js Outdated Show resolved Hide resolved
assets/src/blocks/Media/MediaEditor.js Outdated Show resolved Hide resolved
assets/src/blocks/Media/MediaElementVideo.js Outdated Show resolved Hide resolved
assets/src/blocks/Media/MediaElementVideo.js Outdated Show resolved Hide resolved
assets/src/blocks/Media/MediaFrontend.js Show resolved Hide resolved
@pablocubico pablocubico force-pushed the planet-4810 branch 2 times, most recently from b54d7e3 to b886f94 Compare October 27, 2020 19:35
This squashed:
* Showing a nice error message and using debounce() on the URL input.
* Remove deprecated shortcodes code.
* Address feedback from review. Use a proper JSX tag instead of renderEdit.
* Remove Tooltip coming from the Articles block
* Store new posterURL and embedHtml static properties on top of the old attributes
* Responsive fixes for MediaElementJS.
* Addressing feedback (removing endpoint and the StaticMediaFrontend, tidying up)
* Media block WYSIWYG implementation using static save().
* Moved video poster image picker to the sidebar
* Deprecate youtube_id
* Add addScript and addLinkTag methods to be used in or out of hooks
* Remove nested ternary, address review feedback.
@pablocubico
Copy link
Contributor Author

@Inwerpsel picking up from our latest conversations (you probably saw my comments via Toast notifications):

  • undo/redo: it wasn't because of the state, it's happening on other blocks, I'll create a ticket for that
  • wrapEmbedHTML: happening on the MediaFrontend block, not at save()-time nor in the PHP side
  • other minor comments addressed

Copy link
Contributor

@Inwerpsel Inwerpsel left a comment

Choose a reason for hiding this comment

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

I'll finish the review tomorrow. Looks good so far, left some suggestions.

If we fallback with JS we probably want to run a migration soon as the difference with static html will probably be noticeable (assumption, still need to confirm the difference).

Thinking of that migration, it's actually going to be a bit tricky as we need to run JS to get the new block state, probably some setup involved in having that run on all posts 🤔

assets/src/blocks/Media/MediaBlock.js Show resolved Hide resolved
assets/src/blocks/Media/MediaEditor.js Outdated Show resolved Hide resolved
assets/src/blocks/Media/MediaFrontend.js Show resolved Hide resolved
assets/src/blocks/Media/MediaBlock.js Outdated Show resolved Hide resolved
Copy link
Contributor

@Inwerpsel Inwerpsel left a comment

Choose a reason for hiding this comment

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

Looks good 👍 👍 👍

I recommend still making useScript and useStylesheet not retrying on error, it could result in the resource being added and removed on each render if the error persists. But probably not an issue for the usage in this PR 🤷

assets/src/components/useStyleSheet/addLinkTag.js Outdated Show resolved Hide resolved
classes/blocks/class-media.php Show resolved Hide resolved
Copy link
Contributor

@mleray mleray left a comment

Choose a reason for hiding this comment

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

We need to remember to create a follow-up ticket to rewrite the acceptance tests in JS, but apart from that looks good to me 👍

@pablocubico pablocubico merged commit 03f7d39 into master Nov 6, 2020
@pablocubico pablocubico deleted the planet-4810 branch November 6, 2020 04:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants