Privacy: Add an indication when the Copy action in Privacy Policy Guide is complete.

This adds a "Copied!" text near the "Copy this section to clipboard" button to provide direct feedback that the action was completed.

Props garrett-eclipse, nickylimjj, xkon, desrosj, birgire.
Fixes #44588.

git-svn-id: https://develop.svn.wordpress.org/trunk@47572 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2020-04-12 14:24:38 +00:00
parent 63ac227863
commit d5df031e6f
4 changed files with 50 additions and 23 deletions

View File

@ -259,10 +259,14 @@ jQuery( document ).ready( function( $ ) {
doNextErasure( 1, 1 ); doNextErasure( 1, 1 );
}); });
// Privacy policy page, copy button. // Privacy Policy page, copy action.
$( document ).on( 'click', function( event ) { $( document ).on( 'click', function( event ) {
var $target = $( event.target ); var $parent,
var $parent, $container, range; $container,
range,
__ = wp.i18n.__,
$target = $( event.target ),
copiedNotice = $target.siblings( '.success' );
if ( $target.is( 'button.privacy-text-copy' ) ) { if ( $target.is( 'button.privacy-text-copy' ) ) {
$parent = $target.parent().parent(); $parent = $target.parent().parent();
@ -277,22 +281,35 @@ jQuery( document ).ready( function( $ ) {
var documentPosition = document.documentElement.scrollTop, var documentPosition = document.documentElement.scrollTop,
bodyPosition = document.body.scrollTop; bodyPosition = document.body.scrollTop;
// Setup copy.
window.getSelection().removeAllRanges(); window.getSelection().removeAllRanges();
// Hide tutorial content to remove from copied content.
range = document.createRange(); range = document.createRange();
$container.addClass( 'hide-privacy-policy-tutorial' ); $container.addClass( 'hide-privacy-policy-tutorial' );
// Copy action.
range.selectNodeContents( $container[0] ); range.selectNodeContents( $container[0] );
window.getSelection().addRange( range ); window.getSelection().addRange( range );
document.execCommand( 'copy' ); document.execCommand( 'copy' );
// Reset section.
$container.removeClass( 'hide-privacy-policy-tutorial' ); $container.removeClass( 'hide-privacy-policy-tutorial' );
window.getSelection().removeAllRanges(); window.getSelection().removeAllRanges();
// Return scroll position - see #49540.
if ( documentPosition > 0 && documentPosition !== document.documentElement.scrollTop ) { if ( documentPosition > 0 && documentPosition !== document.documentElement.scrollTop ) {
document.documentElement.scrollTop = documentPosition; document.documentElement.scrollTop = documentPosition;
} else if ( bodyPosition > 0 && bodyPosition !== document.body.scrollTop ) { } else if ( bodyPosition > 0 && bodyPosition !== document.body.scrollTop ) {
document.body.scrollTop = bodyPosition; document.body.scrollTop = bodyPosition;
} }
// Display and speak notice to indicate action complete.
copiedNotice.addClass( 'visible' );
wp.a11y.speak( __( 'The section has been copied to your clipboard.' ) );
// Delay notice dismissal.
setTimeout( function(){ copiedNotice.removeClass( 'visible' ); }, 3000 );
} catch ( er ) {} } catch ( er ) {}
} }
} }

View File

@ -728,6 +728,18 @@ form#tags-filter {
padding-bottom: 6px; padding-bottom: 6px;
} }
.privacy-text-actions .success {
display: none;
color: #40860a;
float: right;
padding-right: 1em;
}
.privacy-text-actions .success.visible {
display: inline-block;
height: 32px;
}
.wp-privacy-policy-guide .policy-text h2 { .wp-privacy-policy-guide .policy-text h2 {
margin: 1.2em 0 1em; margin: 1.2em 0 1em;
padding: 0; padding: 0;

View File

@ -372,12 +372,9 @@ final class WP_Privacy_Policy_Content {
public static function privacy_policy_guide() { public static function privacy_policy_guide() {
$content_array = self::get_suggested_policy_text(); $content_array = self::get_suggested_policy_text();
$content = ''; $content = '';
$toc = array( '<li><a href="#wp-privacy-policy-guide-introduction">' . __( 'Introduction' ) . '</a></li>' ); $toc = array( '<li><a href="#wp-privacy-policy-guide-introduction">' . __( 'Introduction' ) . '</a></li>' );
$date_format = __( 'F j, Y' ); $date_format = __( 'F j, Y' );
$copy = __( 'Copy this section to clipboard' );
$return_to_top = '<a href="#" class="return-to-top">' . __( '&uarr; Return to Top' ) . '</a>';
foreach ( $content_array as $section ) { foreach ( $content_array as $section ) {
$class = ''; $class = '';
@ -415,21 +412,22 @@ final class WP_Privacy_Policy_Content {
$content .= $removed; $content .= $removed;
$content .= '<div class="policy-text">' . $section['policy_text'] . '</div>'; $content .= '<div class="policy-text">' . $section['policy_text'] . '</div>';
$content .= $return_to_top; $content .= '<a href="#" class="return-to-top">' . __( '&uarr; Return to Top' ) . '</a>';
if ( empty( $section['removed'] ) ) { if ( empty( $section['removed'] ) ) {
$content .= '<div class="privacy-text-actions">'; $content .= '<div class="privacy-text-actions">';
$content .= '<button type="button" class="privacy-text-copy button">'; $content .= '<button type="button" class="privacy-text-copy button">';
$content .= $copy; $content .= __( 'Copy this section to clipboard' );
$content .= '<span class="screen-reader-text">'; $content .= '<span class="screen-reader-text">';
/* translators: %s: Plugin name. */ /* translators: %s: Plugin name. */
$content .= sprintf( __( 'Copy suggested policy text from %s.' ), $plugin_name ); $content .= sprintf( __( 'Copy suggested policy text from %s.' ), $plugin_name );
$content .= '</span>'; $content .= '</span>';
$content .= '</button>'; $content .= '</button>';
$content .= '<span class="success" aria-hidden="true">' . __( 'Copied!' ) . '</span>';
$content .= '</div>'; $content .= '</div>';
} }
$content .= "</div>\n"; // End of .privacy-text-section. $content .= '</div>'; // End of .privacy-text-section.
} }
if ( count( $toc ) > 2 ) { if ( count( $toc ) > 2 ) {
@ -437,7 +435,7 @@ final class WP_Privacy_Policy_Content {
<div class="privacy-text-box-toc"> <div class="privacy-text-box-toc">
<p><?php _e( 'Table of Contents' ); ?></p> <p><?php _e( 'Table of Contents' ); ?></p>
<ol> <ol>
<?php echo implode( "\n", $toc ); ?> <?php echo implode( $toc ); ?>
</ol> </ol>
</div> </div>
<?php <?php

View File

@ -1417,7 +1417,7 @@ function wp_default_scripts( $scripts ) {
$scripts->add( 'site-health', "/wp-admin/js/site-health$suffix.js", array( 'clipboard', 'jquery', 'wp-util', 'wp-a11y', 'wp-i18n' ), false, 1 ); $scripts->add( 'site-health', "/wp-admin/js/site-health$suffix.js", array( 'clipboard', 'jquery', 'wp-util', 'wp-a11y', 'wp-i18n' ), false, 1 );
$scripts->set_translations( 'site-health' ); $scripts->set_translations( 'site-health' );
$scripts->add( 'privacy-tools', "/wp-admin/js/privacy-tools$suffix.js", array( 'jquery' ), false, 1 ); $scripts->add( 'privacy-tools', "/wp-admin/js/privacy-tools$suffix.js", array( 'jquery', 'wp-a11y', 'wp-i18n' ), false, 1 );
did_action( 'init' ) && $scripts->localize( did_action( 'init' ) && $scripts->localize(
'privacy-tools', 'privacy-tools',
'privacyToolsL10n', 'privacyToolsL10n',