Skip to content

Fix displaying AMP validation error at block level in the Gutenberg editor#1293

Merged
westonruter merged 17 commits intodevelopfrom
fix/display-validation-error-at-block-level
Aug 1, 2018
Merged

Fix displaying AMP validation error at block level in the Gutenberg editor#1293
westonruter merged 17 commits intodevelopfrom
fix/display-validation-error-at-block-level

Conversation

@hellofromtonya
Copy link
Copy Markdown
Contributor

@hellofromtonya hellofromtonya commented Jul 30, 2018

This PR fixes the problems noted in #1292:

  • Changes the filter hook name to editor.BlockEdit. This filter was changed in Gutenberg 3.3 as noted here:

blocks.BlockEdit filter removed. Please use editor.BlockEdit instead.

  • Changes the block uid to ownProps.clientId. The id property was changed in Gutenberg 3.5.0 to .clientId as noted here:

All references to a block's uid have been replaced with equivalent props and selectors for clientId.

These two changes result in displaying the AMP validation error at the affected block upon saving or updating:

amp-issue-block-level-notification

  • Handling when blocks get out of sync.

  • Handling notices when duplicating blocks.

  • Reset warning notice after removing all affected blocks.

  • Display block-level notices in native mode or when force_sanitization is enabled.

  • Display block-level notices on first loading the post into the editor.
    When first opening a post in the editor, the validation notices are displayed. They update when saving.

  • Remove and disable notice handling when the AMP toggle is off.
    When the post's AMP toggle is off, the top and block level notices are removed. It then bails out. When the toggle is turned to on, the notices reappear after saving.

Closes #1292.

@hellofromtonya hellofromtonya added the Bug Something isn't working label Jul 30, 2018
@hellofromtonya hellofromtonya changed the title Fix displaying AMP validation error at block level in the Gutenberg editor [WIP] Fix displaying AMP validation error at block level in the Gutenberg editor Jul 30, 2018
@hellofromtonya hellofromtonya changed the title [WIP] Fix displaying AMP validation error at block level in the Gutenberg editor Fix displaying AMP validation error at block level in the Gutenberg editor Jul 30, 2018
@westonruter westonruter added this to the v1.0 milestone Jul 30, 2018
@hellofromtonya hellofromtonya changed the title Fix displaying AMP validation error at block level in the Gutenberg editor [WIP] Fix displaying AMP validation error at block level in the Gutenberg editor Jul 30, 2018
Tonya Mork added 4 commits July 31, 2018 20:21
On page load, the current post is not initially set up.  This commit waits until it is before letting `handleValidationErrorsStateChange()` continue.
Abstracted the "wait" conditional checks into a new method, thereby converting it into one guard clause.

Why? This approach improves readability of the `handleValidationErrorsStateChange()` as the intent is clear:  when true, wait by bailing out.  Then the details of the decision of whether to wait or not is handled elsewhere.
A few changes occurred with this commit:

1. Abstracted the checker.
2. Added the last state declaration on the module itself.
3. Compare the length of the last and current arrays. If not equal, then it changed. <- this one optimizes the checker to avoid running _.isEqual.
Blocks get out of sync in different scenarios.  For example, on page load, the block uids will change as the editor loads up and blocks are registered.

This commit compares the last known block order to the current block order.  If not equal, then it resets the last saved validation errors memory.  In doing so, if there are validation errors, then `handleValidationErrorsStateChange()` will continue processing instead of bailing out.
@hellofromtonya
Copy link
Copy Markdown
Contributor Author

hellofromtonya commented Aug 1, 2018

@westonruter The recent commits fixed 2 issues:

  1. Waiting for the editor to load up before handling the validation error state change.
  2. Handling when the blocks get out of sync.

For the last one, the blocks get out of sync. It happens every time when the editor is loading on page load. And I've seen it in other intermittent editing/saving workflow. With these changes, it now checks if the block order changed. If yes, it resets the last validation errors that were previously stored.

Can you test it on your machine?

@todos:
1. We still have an issue when duplicating a block.
2. Fix the formatting so Travis will be happy.

Gutenberg is removing `uid` and replacing it everywhere with `clientId`.  The change is included in 3.4.0 and `uid` will be removed in 3.5.0.

@see https://github.com/WordPress/gutenberg/blob/41fd116ca1f73a9b5c962c66ea0c4aae338ea35e/docs/reference/deprecated.md#350

We see the impact of this change as `getBlock()` requires the `clientId` now instead of the `uid`.

This commit replaces all references to `uid` with `clientId`.  In doing so, it fixes the issue with block-level notifications disappearing when duplicating a block and then saving.
Tonya Mork and others added 4 commits August 1, 2018 16:02
When there are no validation errors, this commit resets the warning notice and stores memory that the notice is reset.

This commit also abstracts the warning notice and block level notice resets into two separate methods.  This change prompts reuse and moves the details of how to reset into a methods that handle it.
The resetting of block level messages will not be reached when there are no validation errors.
@westonruter westonruter changed the title [WIP] Fix displaying AMP validation error at block level in the Gutenberg editor Fix displaying AMP validation error at block level in the Gutenberg editor Aug 1, 2018
} catch ( e ) {
// Clear out block validation errors in case the block sand errors cannot be aligned.
module.resetBLockNotices();
module.resetBlockNotices();
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch @westonruter. Thank you 👍

@kienstra
Copy link
Copy Markdown
Contributor

kienstra commented Aug 1, 2018

Notices Above Blocks

It looks like the Notices above blocks now appear as expected, at least on the blocks I tested:

notices-appear-above-blocks

notice-above

Also, a Notice appears above a duplicate block on updating the post:
duplicate-and-update

westonruter and others added 2 commits August 1, 2018 15:52
When a post's AMP toggle is off, the top and block level notices are removed.
@westonruter westonruter merged commit a963593 into develop Aug 1, 2018
@westonruter westonruter deleted the fix/display-validation-error-at-block-level branch August 1, 2018 23:46
@westonruter
Copy link
Copy Markdown
Member

Great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AMP validation error is not displaying at the block level in the Gutenberg editor.

3 participants