-
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 all 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,76 @@ | ||
/** | ||
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 ) => { | ||
return JSON.stringify( JSON.parse( btn.getAttribute( 'data-error-json' ) ), null, '\t' ); | ||
}, | ||
} ) ); | ||
|
||
// 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; | ||
} | ||
|
||
return JSON.parse( copyButton.getAttribute( 'data-error-json' ) ); | ||
} ) | ||
.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 ); | ||
} |
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.