-
Notifications
You must be signed in to change notification settings - Fork 384
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
Changes from 11 commits
0b8c793
b2b5c14
3cfad55
f73e574
fe35801
c4e1a64
61e571a
08d9237
0153b68
ddf5ec4
47fc7be
7e1d5a9
4bae05c
d2382e8
c83dbb8
e7dbb5d
54ecc46
23fbfed
82edbbd
e4abee8
f6bb8ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 👍. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
* External dependencies | ||
*/ | ||
import Clipboard from 'clipboard'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getURLValidationTableRows } from './get-url-validation-table-rows'; | ||
|
||
/** | ||
* Success handler, called when data is copied to the clipboard. | ||
* | ||
* @param {Object} event | ||
* @param {HTMLElement} event.trigger The element triggering the event. | ||
*/ | ||
function onSuccess( { trigger } ) { | ||
trigger.focus(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, looking at my code, that bug makes perfect sense 🤦 Fixing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
const newInnerText = __( 'Copied to clipboard', 'amp' ); | ||
|
||
// Exit if the user has already clicked the button and we are still within the | ||
// 4000ms before the setTimeout callback runs. | ||
if ( trigger.innerText === newInnerText ) { | ||
return; | ||
} | ||
|
||
const originalText = trigger.innerText; | ||
trigger.innerText = newInnerText; | ||
|
||
setTimeout( () => { | ||
if ( document.body.contains( trigger ) ) { | ||
trigger.innerText = originalText; | ||
} | ||
}, 4000 ); | ||
} | ||
|
||
/** | ||
* Sets up the "Copy to clipboard" buttons on the URL validation screen. | ||
*/ | ||
export function handleCopyToClipboardButtons() { | ||
const clipboards = []; | ||
|
||
// eslint-disable-next-line no-new | ||
clipboards.push( new Clipboard( 'button.single-url-detail-copy', { | ||
text: ( btn ) => { | ||
const json = JSON.parse( btn.getAttribute( 'data-error-json' ) ); | ||
const statusSelect = btn.closest( 'tr' ).querySelector( '.amp-validation-error-status' ); | ||
json.status = statusSelect.options[ statusSelect.selectedIndex ].text; | ||
|
||
return JSON.stringify( json, null, '\t' ); | ||
johnwatkins0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
} ) ); | ||
|
||
// eslint-disable-next-line no-new | ||
clipboards.push( new Clipboard( 'button.copy-all', { | ||
text: () => { | ||
const value = getURLValidationTableRows( { checkedOnly: true } ).map( ( row ) => { | ||
const copyButton = row.querySelector( '.single-url-detail-copy' ); | ||
if ( ! copyButton ) { | ||
return null; | ||
} | ||
|
||
const json = JSON.parse( copyButton.getAttribute( 'data-error-json' ) ); | ||
const statusSelect = row.querySelector( '.amp-validation-error-status' ); | ||
json.status = statusSelect.options[ statusSelect.selectedIndex ].text; | ||
|
||
return json; | ||
johnwatkins0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} ) | ||
.filter( ( item ) => item ); | ||
|
||
return JSON.stringify( value, null, '\t' ); | ||
}, | ||
} ) ); | ||
|
||
clipboards.forEach( ( clipboard ) => { | ||
clipboard.on( 'success', onSuccess ); | ||
} ); | ||
} |
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 ); | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1591,6 +1591,30 @@ public static function add_admin_notices() { | |||||
} | ||||||
} | ||||||
|
||||||
/** | ||||||
* Returns JSON-formatted error details for an error term. | ||||||
* | ||||||
* @param WP_Term $term The term. | ||||||
* @return string Encoded JSON. | ||||||
*/ | ||||||
public static function get_error_details_json( $term ) { | ||||||
$json = json_decode( $term->description, true ); | ||||||
|
||||||
// Convert the numeric constant value of the node_type to its constant name. | ||||||
$xml_reader_reflection_class = new ReflectionClass( 'XMLReader' ); | ||||||
$constants = $xml_reader_reflection_class->getConstants(); | ||||||
foreach ( $constants as $key => $value ) { | ||||||
if ( $json['node_type'] === $value ) { | ||||||
$json['node_type'] = $key; | ||||||
break; | ||||||
} | ||||||
} | ||||||
|
||||||
$json['status'] = (bool) ( (int) $term->term_group & self::ACCEPTED_VALIDATION_ERROR_BIT_MASK ) ? __( 'Removed', 'amp' ) : __( 'Kept', 'amp' ); | ||||||
johnwatkins0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
return wp_json_encode( $json, JSON_PRETTY_PRINT ); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
} | ||||||
|
||||||
/** | ||||||
* Add row actions. | ||||||
* | ||||||
|
@@ -1619,6 +1643,13 @@ public static function filter_tag_row_actions( $actions, WP_Term $tag ) { | |||||
esc_attr__( 'Toggle error details', 'amp' ), | ||||||
esc_html__( 'Details', '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( self::get_error_details_json( $term ) ), | ||||||
esc_html__( 'Copy to clipboard', 'amp' ) | ||||||
johnwatkins0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
); | ||||||
} elseif ( 'edit-tags.php' === $pagenow ) { | ||||||
$actions['details'] = sprintf( | ||||||
'<a href="%s">%s</a>', | ||||||
|
@@ -1646,7 +1677,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; | ||||||
} | ||||||
|
@@ -2251,7 +2282,6 @@ public static function render_single_url_error_details( $validation_error, $term | |||||
</dd> | ||||||
<?php endforeach; ?> | ||||||
</dl> | ||||||
|
||||||
<?php | ||||||
|
||||||
$output = ob_get_clean(); | ||||||
|
There was a problem hiding this comment.
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.