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

Feature : Add "Copy" button to Revision Viewer #8079

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
30 changes: 28 additions & 2 deletions src/js/_enqueues/wp/revisions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1122,11 +1122,37 @@ window.wp = window.wp || {};
revisions.view.Diff = wp.Backbone.View.extend({
className: 'revisions-diff',
template: wp.template('revisions-diff'),

events: {
'click .button-copy': 'copyContent',
},
// Generate the options to be passed to the template.
prepare: function() {
return _.extend({ fields: this.model.fields.toJSON() }, this.options );
}
},
copyContent: function(e) {
const button = e.currentTarget;
const side = button.getAttribute('data-side');
const fields = this.model.fields.toJSON();
if (side === 'left') {
navigator.clipboard.writeText(fields[1].from.post_content).then(() => {
button.value = wp.i18n.__('Copied');
button.disabled = true;
setTimeout( () => {
button.value = wp.i18n.__('Copy');
button.disabled = false;
}, 5000 );
});
} else if (side === 'right') {
navigator.clipboard.writeText(fields[1].to.post_content).then(() => {
button.value = wp.i18n.__('Copied');
button.disabled = true;
setTimeout( () => {
button.value = wp.i18n.__('Copy');
button.disabled = false;
}, 5000 );
});
}
},
});

// The revisions router.
Expand Down
4 changes: 4 additions & 0 deletions src/wp-admin/css/revisions.css
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ table.diff th {
word-wrap: break-word;
}

table.diff thead tr.diff-sub-title th input[type="button"] {
vertical-align: middle;
}

table.diff td h1,
table.diff td h2,
table.diff td h3,
Expand Down
22 changes: 16 additions & 6 deletions src/wp-admin/includes/revision.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ function wp_get_revision_ui_diff( $post, $compare_from, $compare_to ) {
*/
$args = apply_filters( 'revision_text_diff_options', $args, $field, $compare_from, $compare_to );

$diff = wp_text_diff( $content_from, $content_to, $args );
$diff = wp_text_diff( $content_from, $content_to, $args, true );

if ( ! $diff && 'post_title' === $field ) {
/*
Expand All @@ -143,11 +143,21 @@ function wp_get_revision_ui_diff( $post, $compare_from, $compare_to ) {
}

if ( $diff ) {
$return[] = array(
'id' => $field,
'name' => $name,
'diff' => $diff,
);
if ( 'post_content' === $field ) {
$return[] = array(
'id' => $field,
'name' => $name,
'diff' => $diff,
'from' => $compare_from,
'to' => $compare_to,
);
} else {
$return[] = array(
'id' => $field,
'name' => $name,
'diff' => $diff,
);
}
}
}

Expand Down
15 changes: 11 additions & 4 deletions src/wp-includes/pluggable.php
Original file line number Diff line number Diff line change
Expand Up @@ -3124,7 +3124,7 @@ function get_avatar( $id_or_email, $size = 96, $default_value = '', $alt = '', $
* }
* @return string Empty string if strings are equivalent or HTML with differences.
*/
function wp_text_diff( $left_string, $right_string, $args = null ) {
function wp_text_diff( $left_string, $right_string, $args = null, $enable_copy_button = false ) {
$defaults = array(
'title' => '',
'title_left' => '',
Expand Down Expand Up @@ -3168,9 +3168,16 @@ function wp_text_diff( $left_string, $right_string, $args = null ) {
$th_or_td_right = empty( $args['title_right'] ) ? 'td' : 'th';

$r .= "<tr class='diff-sub-title'>\n";
$r .= "\t<$th_or_td_left>$args[title_left]</$th_or_td_left>\n";
if ( $is_split_view ) {
$r .= "\t<$th_or_td_right>$args[title_right]</$th_or_td_right>\n";
if ( $enable_copy_button ) {
$r .= "\t<$th_or_td_left>$args[title_left] <input class='button button-primary button-copy' data-side='left' type='button' value='" . __( 'Copy' ) . "' /></$th_or_td_left>\n";
if ( $is_split_view ) {
$r .= "\t<$th_or_td_right>$args[title_right] <input class='button button-primary button-copy' data-side='right' type='button' value='" . __( 'Copy' ) . "' /> </$th_or_td_right>\n";
}
} else {
$r .= "\t<$th_or_td_left>$args[title_left]</$th_or_td_left>\n";
if ( $is_split_view ) {
$r .= "\t<$th_or_td_right>$args[title_right]</$th_or_td_right>\n";
}
}
$r .= "</tr>\n";
}
Expand Down
Loading