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
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
0b8c793
Add buttons to copy error details to clipboard
johnwatkins0 Oct 12, 2020
b2b5c14
Move clipboard functions into own module and prettyprint JSON
johnwatkins0 Oct 12, 2020
3cfad55
Resolve failing test
johnwatkins0 Oct 12, 2020
f73e574
Add tests for clipboard-friendly error button
johnwatkins0 Oct 12, 2020
fe35801
Convert error node_type to constant name in JSON
johnwatkins0 Oct 21, 2020
c4e1a64
Update markup status in JSON data in case it has changed
johnwatkins0 Oct 21, 2020
61e571a
Use clipboard.js package for copying to clipboard
johnwatkins0 Oct 21, 2020
08d9237
Disable eslint rule per line
johnwatkins0 Oct 21, 2020
0153b68
Remove unneeded helper function
johnwatkins0 Oct 21, 2020
ddf5ec4
Copy to clipboard: restore focus state and show 'copied' text
johnwatkins0 Oct 28, 2020
47fc7be
Copy to clipboard: handle case where clicks button again immediately
johnwatkins0 Oct 28, 2020
7e1d5a9
Make 'copy to clipboard' text consistent
johnwatkins0 Oct 29, 2020
4bae05c
Clipboard-friendly errors: use status from DB instead of from UI
johnwatkins0 Oct 29, 2020
d2382e8
Clipboard-friendly errors: convert status to removed bool and add rev…
johnwatkins0 Oct 29, 2020
c83dbb8
No need to pretty-print error details on the backend
johnwatkins0 Oct 29, 2020
e7dbb5d
Merge remote-tracking branch 'origin/develop' into feature/5209-clipb…
johnwatkins0 Oct 29, 2020
54ecc46
Parse and re-stringify JSON data to prettify
johnwatkins0 Oct 29, 2020
23fbfed
Remove unnecessary aria-label
johnwatkins0 Oct 29, 2020
82edbbd
Merge branch 'develop' of github.com:ampproject/amp-wp into feature/5…
westonruter Oct 29, 2020
e4abee8
Merge branch 'feature/5209-clipboard-friendly-errors' of github.com:a…
westonruter Oct 29, 2020
f6bb8ad
Fix test_get_error_details_json
westonruter Oct 29, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions assets/src/amp-validation/amp-validated-url-post-edit-screen.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { __, _n, sprintf } from '@wordpress/i18n';
* Internal dependencies
*/
import setValidationErrorRowsSeenClass from './set-validation-error-rows-seen-class';
import { handleCopyToClipboardButtons } from './copy-to-clipboard-buttons';
import { getURLValidationTableRows } from './get-url-validation-table-rows';

/**
* The id for the 'Showing x of y errors' notice.
Expand All @@ -32,6 +34,7 @@ domReady( () => {
handleBulkActions();
watchForUnsavedChanges();
setupStylesheetsMetabox();
handleCopyToClipboardButtons();
} );

let beforeUnloadPromptAdded = false;
Expand Down Expand Up @@ -389,13 +392,10 @@ const handleBulkActions = () => {

const markingAsReviewed = target.classList.contains( 'reviewed' );

[ ...document.querySelectorAll( 'select.amp-validation-error-status' ) ].forEach( ( select ) => {
const row = select.closest( 'tr' );
if ( row.querySelector( '.check-column input[type=checkbox]' ).checked ) {
row.querySelector( 'input[type=checkbox].amp-validation-error-status-review' ).checked = markingAsReviewed;
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.

row.querySelector( 'input[type=checkbox].amp-validation-error-status-review' ).checked = markingAsReviewed;
row.classList.toggle( 'new', ! markingAsReviewed );
addBeforeUnloadPrompt();
} );
} );
};
Expand Down
61 changes: 61 additions & 0 deletions assets/src/amp-validation/copy-to-clipboard-buttons.js
Original file line number Diff line number Diff line change
@@ -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

* Internal dependencies
*/
import { getURLValidationTableRows } from './get-url-validation-table-rows';

/**
* Copies any string to the clipboard.
*
* Note: This only works within a callback responding to a user action.
*
* @param {string} value Any string value.
*/
function copyToClipboard( value ) {
const textareaElement = document.createElement( 'textarea' );
textareaElement.value = value;
textareaElement.style.position = 'absolute';
textareaElement.style.left = '-1000%';
document.body.appendChild( textareaElement );
textareaElement.select();
document.execCommand( 'copy' );
document.body.removeChild( textareaElement );
}

/**
* Callback when a user clicks a button to copy error details to a clipboard.
*
* @param {Event} event Click event.
*/
function handleCopyToClipboardClick( event ) {
// Handle a single error detail button.
if ( event.target.classList.contains( 'single-url-detail-copy' ) ) {
copyToClipboard( event.target.getAttribute( 'data-error-json' ) );
return;
}

// Handle a click on the bulk action button.
if ( ! event.target.classList.contains( 'copy-all' ) ) {
return;
}

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

if ( ! copyButton ) {
return null;
}

const data = JSON.parse( copyButton.getAttribute( 'data-error-json' ) );

return data;
} )
.filter( ( item ) => item );

copyToClipboard( JSON.stringify( value, null, '\t' ) );
}

/**
* Sets up the "Copy to clipboard" buttons on the URL validation screen.
*/
export function handleCopyToClipboardButtons() {
global.addEventListener( 'click', handleCopyToClipboardClick );
}
16 changes: 16 additions & 0 deletions assets/src/amp-validation/get-url-validation-table-rows.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Gets the table rows on a single URL validation screen.
*
* @param {Object} options
* @param {boolean} options.checkedOnly Whether to return only checked rows.
*/
export function getURLValidationTableRows( options = {} ) {
const rows = [ ...document.querySelectorAll( 'select.amp-validation-error-status' ) ]
.map( ( select ) => select.closest( 'tr' ) );

if ( true !== options.checkedOnly ) {
return rows;
}

return rows.filter( ( row ) => row.querySelector( '.check-column input[type=checkbox]' ).checked );
}
1 change: 1 addition & 0 deletions includes/validation/class-amp-validated-url-post-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -2796,6 +2796,7 @@ public static function render_single_url_list_table( $post ) {
<button type="button" class="button action keep"><?php esc_html_e( 'Keep', 'amp' ); ?></button>
<button type="button" class="button action reviewed-toggle reviewed"><?php esc_html_e( 'Mark reviewed', 'amp' ); ?></button>
<button type="button" class="button action reviewed-toggle unreviewed"><?php esc_html_e( 'Mark unreviewed', 'amp' ); ?></button>
<button type="button" class="button action copy-all"><?php esc_html_e( 'Copy error details to clipboard', 'amp' ); ?></button>
<div id="vertical-divider"></div>
</div>
<div id="url-post-filter" class="alignleft actions">
Expand Down
13 changes: 11 additions & 2 deletions includes/validation/class-amp-validation-error-taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

$actions['copy'] = sprintf(
'<button type="button" aria-label="%s" class="single-url-detail-copy button-link" data-error-json="%s">%s</button>',
esc_attr__( 'Copy to clipboard', 'amp' ),
esc_attr( wp_json_encode( $json, JSON_PRETTY_PRINT ) ),
esc_html__( 'Copy to clipboard', 'amp' )
);
} elseif ( 'edit-tags.php' === $pagenow ) {
$actions['details'] = sprintf(
'<a href="%s">%s</a>',
Expand Down Expand Up @@ -1646,7 +1656,7 @@ public static function filter_tag_row_actions( $actions, WP_Term $tag ) {
}
}

$actions = wp_array_slice_assoc( $actions, [ 'details', 'delete' ] );
$actions = wp_array_slice_assoc( $actions, [ 'details', 'delete', 'copy' ] );

return $actions;
}
Expand Down Expand Up @@ -2251,7 +2261,6 @@ public static function render_single_url_error_details( $validation_error, $term
</dd>
<?php endforeach; ?>
</dl>

<?php

$output = ob_get_clean();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,12 @@ function ( $actions ) {
[ 'details', 'delete' ],
array_keys( $filtered_actions )
);

$pagenow = 'post.php';
$actions = AMP_Validation_Error_Taxonomy::filter_tag_row_actions( $initial_actions, get_term( $term_this_taxonomy ) );
$this->assertTrue( array_key_exists( 'copy', $actions ) );
$this->assertStringContains( 'Copy to clipboard', $actions['copy'] );

}

/**
Expand Down