Accessibility: Media: Add a "Copy URL" button to the attachment File URL fields.

For a number of years, various screens in the WordPress admin provided users with a readonly input field to copy the attachment file URL. Manually copying from a readonly field is an annoying task at best even for mouser users. It's a usability and accessibility issue at the same time. 
These fields now have a new "Copy URL" button that is easy to use and accessible to everyone.

Props theolg, markdubois, vabrashev, sajjad67, xkon, nrqsnchz, melchoyce, audrasjb, afercia.
See #41612, #50322, #50335.
Fixes #48463.


git-svn-id: https://develop.svn.wordpress.org/trunk@48232 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Andrea Fercia
2020-06-30 13:14:05 +00:00
parent cc4767cec0
commit 36a39ff333
8 changed files with 153 additions and 12 deletions
+37 -3
View File
@@ -4,7 +4,7 @@
* @output wp-admin/js/post.js
*/
/* global postL10n, ajaxurl, wpAjax, setPostThumbnailL10n, postboxes, pagenow, tinymce, alert, deleteUserSetting */
/* global postL10n, ajaxurl, wpAjax, setPostThumbnailL10n, postboxes, pagenow, tinymce, alert, deleteUserSetting, ClipboardJS */
/* global theList:true, theExtraList:true, getUserSetting, setUserSetting, commentReply, commentsBox */
/* global WPSetThumbnailHTML, wptitlehint */
@@ -297,7 +297,10 @@ jQuery(document).ready( function($) {
$postVisibilitySelect = $('#post-visibility-select'),
$timestampdiv = $('#timestampdiv'),
$postStatusSelect = $('#post-status-select'),
isMac = window.navigator.platform ? window.navigator.platform.indexOf( 'Mac' ) !== -1 : false;
isMac = window.navigator.platform ? window.navigator.platform.indexOf( 'Mac' ) !== -1 : false,
copyAttachmentURLClipboard = new ClipboardJS( '.copy-attachment-url.edit-media' ),
copyAttachmentURLSuccessTimeout,
__ = wp.i18n.__;
postboxes.add_postbox_toggles(pagenow);
@@ -1217,7 +1220,38 @@ jQuery(document).ready( function($) {
window.history.replaceState( null, null, location );
});
}
});
/**
* Copies the attachment URL in the Edit Media page to the clipboard.
*
* @since 5.5.0
*
* @param {MouseEvent} event A click event.
*
* @returns {void}
*/
copyAttachmentURLClipboard.on( 'success', function( event ) {
var triggerElement = $( event.trigger ),
successElement = $( '.success', triggerElement.closest( '.copy-to-clipboard-container' ) );
// Clear the selection and move focus back to the trigger.
event.clearSelection();
// Handle ClipboardJS focus bug, see https://github.com/zenorocha/clipboard.js/issues/680
triggerElement.focus();
// Show success visual feedback.
clearTimeout( copyAttachmentURLSuccessTimeout );
successElement.removeClass( 'hidden' );
// Hide success visual feedback after 3 seconds since last success.
copyAttachmentURLSuccessTimeout = setTimeout( function() {
successElement.addClass( 'hidden' );
}, 3000 );
// Handle success audible feedback.
wp.a11y.speak( __( 'The file URL has been copied to your clipboard' ) );
} );
} );
/**
* TinyMCE word count display
+41 -1
View File
@@ -1,7 +1,9 @@
/* global ClipboardJS */
var Attachment = wp.media.view.Attachment,
l10n = wp.media.view.l10n,
$ = jQuery,
Details;
Details,
__ = wp.i18n.__;
Details = Attachment.extend(/** @lends wp.media.view.Attachment.Details.prototype */{
tagName: 'div',
@@ -26,6 +28,42 @@ Details = Attachment.extend(/** @lends wp.media.view.Attachment.Details.prototyp
'keydown': 'toggleSelectionHandler'
},
/**
* Copies the attachment URL to the clipboard.
*
* @since 5.5.0
*
* @param {MouseEvent} event A click event.
*
* @returns {void}
*/
copyAttachmentDetailsURLClipboard: function() {
var clipboard = new ClipboardJS( '.copy-attachment-url' ),
successTimeout;
clipboard.on( 'success', function( event ) {
var triggerElement = $( event.trigger ),
successElement = $( '.success', triggerElement.closest( '.copy-to-clipboard-container' ) );
// Clear the selection and move focus back to the trigger.
event.clearSelection();
// Handle ClipboardJS focus bug, see https://github.com/zenorocha/clipboard.js/issues/680
triggerElement.focus();
// Show success visual feedback.
clearTimeout( successTimeout );
successElement.removeClass( 'hidden' );
// Hide success visual feedback after 3 seconds since last success.
successTimeout = setTimeout( function() {
successElement.addClass( 'hidden' );
}, 3000 );
// Handle success audible feedback.
wp.a11y.speak( __( 'The file URL has been copied to your clipboard' ) );
} );
},
/**
* Shows the details of an attachment.
*
@@ -43,6 +81,8 @@ Details = Attachment.extend(/** @lends wp.media.view.Attachment.Details.prototyp
// Call 'initialize' directly on the parent class.
Attachment.prototype.initialize.apply( this, arguments );
this.copyAttachmentDetailsURLClipboard();
},
/**