-
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 4 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,61 @@ | ||
/** | ||
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 |
||
* 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' ); | ||
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 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. 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. 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 ); | ||
} |
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 |
---|---|---|
|
@@ -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 ); | ||
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. 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 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. We could reflect the 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. 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>', | ||
|
@@ -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; | ||
} | ||
|
@@ -2251,7 +2261,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.