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

Add buttons to copy error details to clipboard #5500

Merged
merged 21 commits into from
Oct 29, 2020

Conversation

johnwatkins0
Copy link
Contributor

@johnwatkins0 johnwatkins0 commented Oct 12, 2020

Summary

This modifies the single AMP Validated URL screen by making it easier to copy error details to the user's clipboard in pretty-printed JSON format. Users can take action in two ways:

  1. By clicking this "Copy to clipboard" button in the actions for a single error (a single JSON object is copied):

Screen Shot 2020-10-12 at 4 57 03 PM

  1. By clicking this "Copy error details to clipboard" button in the bulk actions row (an array of JSON objects is copied):

Screen Shot 2020-10-12 at 4 58 09 PM

These buttons are to be cleaned up as part of #5431.

QA steps are in #5209

Fixes #5209

Checklist

  • My pull request is addressing an open issue (please create one otherwise).
  • My code is tested and passes existing tests.
  • My code follows the Engineering Guidelines (updates are often made to the guidelines, check it out periodically).

@google-cla google-cla bot added the cla: yes Signed the Google CLA label Oct 12, 2020
@johnwatkins0 johnwatkins0 marked this pull request as ready for review October 12, 2020 22:00
@github-actions
Copy link
Contributor

github-actions bot commented Oct 12, 2020

Plugin builds for 47fc7be are ready 🛎️!

row.classList.toggle( 'new', ! markingAsReviewed );
addBeforeUnloadPrompt();
}
getURLValidationTableRows( { checkedOnly: true } ).forEach( ( row ) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a tangential change. I found myself copying pasting this code (which is from another PR I worked on recently) and instead created the getURLValidationTableRows helper function.

@johnwatkins0 johnwatkins0 self-assigned this Oct 12, 2020
@westonruter westonruter added the WS:Core Work stream for Plugin core label Oct 15, 2020
@@ -0,0 +1,61 @@
/**
Copy link
Contributor

Choose a reason for hiding this comment

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

Alternatively you could use clipboard.js (which comes bundled with WordPress), but this approach is also fine 👍.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good suggestion. I didn't think of this. I've refactored out the wheel-reinvention in 61e571a

@@ -1619,6 +1619,16 @@ public static function filter_tag_row_actions( $actions, WP_Term $tag ) {
esc_attr__( 'Toggle error details', 'amp' ),
esc_html__( 'Details', 'amp' )
);

$json = json_decode( $term->description, true );
Copy link
Contributor

Choose a reason for hiding this comment

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

So for a single error the JSON would look like:

{
  "code": "DISALLOWED_TAG",
  "node_attributes": {
    "src": "https:\/\/wordpressdev.lndo.site\/content\/themes\/twentytwentyone\/assets\/js\/primary-navigation.js?ver=__normalized__",
    "id": "twenty-twenty-one-primary-navigation-script-js"
  },
  "node_name": "script",
  "node_type": 1,
  "parent_name": "body",
  "type": "js_error",
  "status": "Removed"
}

For the node_type field, I'm wondering if we should get the XML constant name from the value and use that instead. By itself, the constant value wouldn't be of much use and would require the person having to lookup the constants themselves. For reference, here's the list:

image

Copy link
Contributor

Choose a reason for hiding this comment

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

We could reflect the XMLReader class and obtain the constants from there

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in fe35801

}

const value = getURLValidationTableRows( { checkedOnly: true } ).map( ( row ) => {
const copyButton = row.querySelector( '.single-url-detail-copy' );
Copy link
Contributor

Choose a reason for hiding this comment

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

I noticed that if the user changes any of the row's values (without updating the post), the JSON is not updated. For example, updating a validation error to be kept and then copying the JSON for the row does not reflect the updated field value.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in c4e1a64

@johnwatkins0
Copy link
Contributor Author

Thanks for the suggestions, @pierlon. I've updated and am re-requesting now.

@johnwatkins0 johnwatkins0 requested a review from pierlon October 21, 2020 14:59
@CLAassistant
Copy link

CLAassistant commented Oct 22, 2020

CLA assistant check
All committers have signed the CLA.

@pierlon
Copy link
Contributor

pierlon commented Oct 27, 2020

The only issue I have with this currently is that the "Copy error details to clipboard" button nor the "Copy to clipboard" buttons stay focused when clicked. Also, should we provide a visual feedback to indicate that the JSON has successfully been copied?

@johnwatkins0
Copy link
Contributor Author

The only issue I have with this currently is that the "Copy error details to clipboard" button nor the "Copy to clipboard" buttons stay focused when clicked. Also, should we provide a visual feedback to indicate that the JSON has successfully been copied?

Good call, I will update.

@johnwatkins0
Copy link
Contributor Author

@pierlon I've addressed your suggestions in ddf5ec4

  1. The button is focused on success.
  2. The button text is updated for four seconds. (I referred to this Gutenberg component for inspiration.)

Let me know what you think.

* @param {HTMLElement} event.trigger The element triggering the event.
*/
function onSuccess( { trigger } ) {
trigger.focus();
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor bug:
Clicking on the button while it's showing the updated text permanently sets it to said text:

deepin-screen-recorder_Select area_20201028143033

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, looking at my code, that bug makes perfect sense 🤦 Fixing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the catch, @pierlon. Fixed in 47fc7be

Copy link
Contributor

@pierlon pierlon left a comment

Choose a reason for hiding this comment

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

Well done!

Co-authored-by: Weston Ruter <westonruter@google.com>

$json['status'] = (bool) ( (int) $term->term_group & self::ACCEPTED_VALIDATION_ERROR_BIT_MASK ) ? __( 'Removed', 'amp' ) : __( 'Kept', 'amp' );

return wp_json_encode( $json, JSON_PRETTY_PRINT );
Copy link
Member

Choose a reason for hiding this comment

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

Since the slashes aren't needed in this context (and they impair readability):

Suggested change
return wp_json_encode( $json, JSON_PRETTY_PRINT );
return wp_json_encode( $json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES );

Copy link
Member

Choose a reason for hiding this comment

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

I suppose this is not needed if the JSON is re-parsed and re-serialized. If that's the case, then JSON_PRETTY_PRINT isn't needed either.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated in c83dbb8 and 54ecc46

johnwatkins0 and others added 2 commits October 29, 2020 14:09
Co-authored-by: Weston Ruter <westonruter@google.com>
…iewed field

Co-authored-by: Weston Ruter <westonruter@google.com>
@westonruter
Copy link
Member

@johnwatkins0 Do you want to rebase this onto develop? That should fix the build errors.

@johnwatkins0
Copy link
Contributor Author

@westonruter I've addressed all feedback and merged in the latest develop, but as you can see, there is still a composer issue in the actions building the zip files. I saw this in another PR earlier.

…209-clipboard-friendly-errors

* 'develop' of github.com:ampproject/amp-wp: (174 commits)
  Temporarily force of Composer v1 in CI (#5543)
  Fix typo in Optimizer readme (#5535)
  Include file.php separately from class-wp-upgrader.php (#5539)
  Add PhpIncludeInspection annotation
  Add PhpUnusedParameterInspection annotation
  Capture the original block render_callbacks to detect originating plugin
  Fix Gutenberg 9.2.1 compat due to gutenberg_current_parsed_block_tracking()
  Ensure full coverage for test_get_plugin_dir
  Skip Facebook tests earlier when external-http; update skip message
  Fix CSSList splicing and eliminate reliance on buggy replace/splice methods
  Add more removed rules that cause splice issue
  Add charset rule to allowed_at_rules_retained test data
  Ensure -moz-document rule is removed after replaced
  Skip FB test once testing external-http suite
  Add test for PluginRegistry::get_plugin_dir()
  Ensure css_list is sanitized after -moz-document transformation
  Transform -moz-document at-rule Firefox hack into supports hack
  Fix lint error
  Manually bootstrap WP CLI
  Skip test if Facebook oEmbed endpoint is down
  ...
…mpproject/amp-wp into feature/5209-clipboard-friendly-errors

* 'feature/5209-clipboard-friendly-errors' of github.com:ampproject/amp-wp:
  Remove unnecessary aria-label
  Parse and re-stringify JSON data to prettify
  No need to pretty-print error details on the backend
@westonruter
Copy link
Member

there is still a composer issue in the actions building the zip files. I saw this in another PR earlier.

Isn't this addressed by #5543?

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.

Love this functionality 👍

@westonruter westonruter added this to the v2.1 milestone Oct 29, 2020
@pierlon
Copy link
Contributor

pierlon commented Oct 29, 2020

there is still a composer issue in the actions building the zip files. I saw this in another PR earlier.

Isn't this addressed by #5543?

@johnwatkins0 is referring to the current GHA workflow used to build the plugin; it's also now using composer v2 which is causing the jobs to fail.

@westonruter
Copy link
Member

I've run the tests locally and they pass (aside from the oEmbed handlers that . So I'm going ahead and merging.

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 WS:Core Work stream for Plugin core
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Request: clipboard-friendly validation errors
4 participants