Skip to content

Commit

Permalink
Code blocks: Add "Copy" button to copy code snippets (#79)
Browse files Browse the repository at this point in the history
* Function source: Add aria label to the main source code block

* Inject a "copy" button to each code block

* Style the copy button, remove unused CSS for older browsers
  • Loading branch information
ryelle authored Jun 6, 2022
1 parent ee1ce34 commit d112c00
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 14 deletions.
45 changes: 38 additions & 7 deletions source/wp-content/themes/wporg-developer/inc/user-content.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,45 @@ public static function comment_new_allowed_html( $commentdata ) {
*/
public static function scripts_and_styles() {
if ( is_singular() ) {
wp_enqueue_script( 'wporg-developer-function-reference', get_template_directory_uri() . '/js/function-reference.js', array( 'jquery' ), '20180724', true );
wp_enqueue_script(
'wporg-developer-function-reference',
get_template_directory_uri() . '/js/function-reference.js',
array( 'jquery', 'wp-a11y' ),
filemtime( dirname( __DIR__ ) . '/js/function-reference.js' ),
true
);
wp_localize_script(
'wporg-developer-function-reference',
'wporgFunctionReferenceI18n',
array(
'copy' => __( 'Copy', 'wporg' ),
'copied' => __( 'Code copied', 'wporg' ),
)
);

wp_enqueue_script( 'wporg-developer-user-notes', get_template_directory_uri() . '/js/user-notes.js', array( 'jquery', 'quicktags' ), '20200110', true );
wp_enqueue_script( 'wporg-developer-user-notes-feedback', get_template_directory_uri() . '/js/user-notes-feedback.js', array( 'jquery', 'quicktags' ), '20181023', true );
wp_localize_script( 'wporg-developer-user-notes-feedback', 'wporg_note_feedback', array(
'show' => __( 'Show Feedback', 'wporg' ),
'hide' => __( 'Hide Feedback', 'wporg' ),
) );
wp_enqueue_script(
'wporg-developer-user-notes',
get_template_directory_uri() . '/js/user-notes.js',
array( 'jquery', 'quicktags' ),
filemtime( dirname( __DIR__ ) . '/js/user-notes.js' ),
true
);

wp_enqueue_script(
'wporg-developer-user-notes-feedback',
get_template_directory_uri() . '/js/user-notes-feedback.js',
array( 'jquery', 'quicktags' ),
filemtime( dirname( __DIR__ ) . '/js/user-notes-feedback.js' ),
true
);
wp_localize_script(
'wporg-developer-user-notes-feedback',
'wporg_note_feedback',
array(
'show' => __( 'Show Feedback', 'wporg' ),
'hide' => __( 'Hide Feedback', 'wporg' ),
)
);
}
}

Expand Down
35 changes: 33 additions & 2 deletions source/wp-content/themes/wporg-developer/js/function-reference.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* @global jQuery */
/* global jQuery, wporgFunctionReferenceI18n */
/**
* function-reference.js
*
* Handles all interactivity on the single function page
*/

jQuery( function( $ ) {
jQuery( function ( $ ) {
let $usesList, $usedByList, $showMoreUses, $hideMoreUses, $showMoreUsedBy, $hideMoreUsedBy;

function toggleUsageListInit() {
Expand Down Expand Up @@ -47,4 +47,35 @@ jQuery( function( $ ) {
}

toggleUsageListInit();

// Inject the "copy" button into every code block.
$( '.wp-block-code' ).each( function ( i, element ) {
const $element = $( element );
let timeoutId;

const $button = $( document.createElement( 'button' ) );
$button.text( wporgFunctionReferenceI18n.copy );
$button.on( 'click', function () {
clearTimeout( timeoutId );
const code = $element.find( 'code' ).text();
if ( ! code ) {
return;
}

// This returns a promise which will resolve if the copy suceeded,
// and we can set the button text to tell the user it worked.
// We don't do anything if it fails.
window.navigator.clipboard.writeText( code ).then( function () {
$button.text( wporgFunctionReferenceI18n.copied );
wp.a11y.speak( wporgFunctionReferenceI18n.copied );

// After 5 seconds, reset the button text.
timeoutId = setTimeout( function () {
$button.text( wporgFunctionReferenceI18n.copy );
}, 5000 );
} );
} );

$element.prepend( $button );
} );
} );
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@
<?php
echo do_blocks(
sprintf(
'<!-- wp:code {"lineNumbers":true} --><pre class="wp-block-code" data-start="%s"><code lang="php" class="language-php line-numbers">%s</code></pre><!-- /wp:code -->',
'<!-- wp:code {"lineNumbers":true} --><pre class="wp-block-code" data-start="%1$s" aria-label="%2$s"><code lang="php" class="language-php line-numbers">%3$s</code></pre><!-- /wp:code -->',
esc_attr( get_post_meta( get_the_ID(), '_wp-parser_line_num', true ) ),
__( 'Function source code', 'wporg' ),
htmlentities( get_source_code() )
)
);
Expand Down
3 changes: 1 addition & 2 deletions source/wp-content/themes/wporg-developer/scss/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,7 @@
&:active {
border-color: #aaa #bbb #bbb #bbb;
background: #f0f0f0;
-webkit-box-shadow: inset 0 1px 0 rgba(120, 200, 230, 0.6), 1px 1px 2px rgba(0, 0, 0, 0.4);
box-shadow: inset 0 1px 0 rgba(120, 200, 230, 0.6), 1px 1px 2px rgba(0, 0, 0, 0.4);
box-shadow: 0 0 3px get-color(blue-40);
}

&.shiny-blue {
Expand Down
19 changes: 19 additions & 0 deletions source/wp-content/themes/wporg-developer/scss/prism.scss
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,22 @@ pre.line-numbers > code {
padding-right: 0.8em;
text-align: right;
}

/* Copy button */
pre.wp-block-code {
position: relative;

> button {
display: none;
position: absolute;
top: 1rem;
right: 1rem;
font-size: 1.4rem;
z-index: 2;
}

&:hover > button,
&:focus-within > button {
display: revert;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions source/wp-content/themes/wporg-developer/stylesheets/prism.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d112c00

Please sign in to comment.