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
Changes from 1 commit
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
35 changes: 32 additions & 3 deletions assets/src/amp-validation/copy-to-clipboard-buttons.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,53 @@
*/
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();
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

const originalText = trigger.innerText;
trigger.innerText = __( 'Copied to clipboard', 'amp' );

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
new Clipboard( 'button.single-url-detail-copy', {
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' );
},
} );
} ) );

// eslint-disable-next-line no-new
new Clipboard( 'button.copy-all', {
clipboards.push( new Clipboard( 'button.copy-all', {
text: () => {
const value = getURLValidationTableRows( { checkedOnly: true } ).map( ( row ) => {
const copyButton = row.querySelector( '.single-url-detail-copy' );
Expand All @@ -42,5 +67,9 @@ export function handleCopyToClipboardButtons() {

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

clipboards.forEach( ( clipboard ) => {
clipboard.on( 'success', onSuccess );
} );
}