Revisions: real URLs and preloading of the requested diff.

* Real URLs are being used now, using pushState. `?revision={id}` or `?from={from}&to={to}`.
* Drop the redundant `action=edit` from the URLs (this is the default).
* The initial comparison is preloaded, whether a single revision or a compare-two situation.

See #24425.

git-svn-id: http://core.svn.wordpress.org/trunk@24664 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Mark Jaquith 2013-07-11 22:56:48 +00:00
parent 25d9d478ac
commit c44ce3b6e6
4 changed files with 63 additions and 20 deletions

View File

@ -61,7 +61,7 @@ function wp_get_revision_ui_diff( $post, $compare_from, $compare_to ) {
return $return; return $return;
} }
function wp_prepare_revisions_for_js( $post, $selected_revision_id ) { function wp_prepare_revisions_for_js( $post, $selected_revision_id, $from = null ) {
$post = get_post( $post ); $post = get_post( $post );
$revisions = array(); $revisions = array();
$now_gmt = time(); $now_gmt = time();
@ -98,10 +98,27 @@ function wp_prepare_revisions_for_js( $post, $selected_revision_id ) {
); );
} }
// Now, grab the initial diff
if ( ! $from ) { // Single mode
$initial_revisions = array_reverse( array_keys( array_slice( $revisions, array_search( $selected_revision_id, array_keys( $revisions ) ), 2, true ) ) );
$compare_two_mode = false;
} else { // Compare two
$compare_two_mode = true;
$initial_revisions = array( $from, $selected_revision_id );
}
$diffs = array( array(
'id' => $initial_revisions[0] . ':' . $initial_revisions[1],
'fields' => wp_get_revision_ui_diff( $post->ID, $initial_revisions[0], $initial_revisions[1] ),
));
return array( return array(
'postId' => $post->ID, 'postId' => $post->ID,
'nonce' => wp_create_nonce( 'revisions-ajax-nonce' ), 'nonce' => wp_create_nonce( 'revisions-ajax-nonce' ),
'revisionData' => array_values( $revisions ), 'revisionData' => array_values( $revisions ),
'selectedRevision' => $selected_revision_id, 'to' => $selected_revision_id,
'from' => $from,
'diffData' => $diffs,
'baseUrl' => parse_url( admin_url( 'revision.php' ), PHP_URL_PATH ),
'compareTwoMode' => absint( $compare_two_mode ), // Apparently booleans are not allowed
); );
} }

View File

@ -17,9 +17,14 @@ window.wp = window.wp || {};
}; };
// wp_localize_script transforms top-level numbers into strings. Undo that. // wp_localize_script transforms top-level numbers into strings. Undo that.
if ( revisions.settings.selectedRevision ) if ( revisions.settings.to )
revisions.settings.selectedRevision = parseInt( revisions.settings.selectedRevision, 10 ); revisions.settings.to = parseInt( revisions.settings.to, 10 );
if ( revisions.settings.from )
revisions.settings.from = parseInt( revisions.settings.from, 10 );
// wp_localize_script does not allow for top-level booleans. Fix that.
if ( revisions.settings.compareTwoMode )
revisions.settings.compareTwoMode = revisions.settings.compareTwoMode === '1';
/** /**
* ======================================================================== * ========================================================================
@ -40,7 +45,7 @@ window.wp = window.wp || {};
this.revisions = options.revisions; this.revisions = options.revisions;
this.set({ this.set({
max: this.revisions.length - 1, max: this.revisions.length - 1,
value: this.revisions.indexOf( this.revisions.get( revisions.settings.selectedRevision ) ), value: this.revisions.indexOf( this.revisions.get( revisions.settings.to ) ),
compareTwoMode: this.frame.get('compareTwoMode') compareTwoMode: this.frame.get('compareTwoMode')
}); });
@ -314,6 +319,10 @@ window.wp = window.wp || {};
revisions.model.FrameState = Backbone.Model.extend({ revisions.model.FrameState = Backbone.Model.extend({
defaults: {
compareTwoMode: false
},
initialize: function( attributes, options ) { initialize: function( attributes, options ) {
var properties = {}; var properties = {};
@ -322,17 +331,22 @@ window.wp = window.wp || {};
this.revisions = options.revisions; this.revisions = options.revisions;
this.diffs = new revisions.model.Diffs( [], { revisions: this.revisions }); this.diffs = new revisions.model.Diffs( [], { revisions: this.revisions });
// Set the initial revision provided through the settings. // Set the initial diffs collection provided through the settings
properties.to = this.revisions.get( revisions.settings.selectedRevision ); this.diffs.set( revisions.settings.diffData );
properties.from = this.revisions.prev( properties.to );
properties.compareTwoMode = false; // Set the initial revisions, baseUrl, and mode as provided through settings
properties.to = this.revisions.get( revisions.settings.to );
properties.from = this.revisions.get( revisions.settings.from ) || this.revisions.prev( properties.to );
properties.compareTwoMode = revisions.settings.compareTwoMode;
properties.baseUrl = revisions.settings.baseUrl;
this.set( properties ); this.set( properties );
// Start the router. This will trigger a navigate event and ensure that // Start the router. This will trigger a navigate event and ensure that
// the `from` and `to` revisions accurately reflect the hash. // the `from` and `to` revisions accurately reflect the hash.
this.router = new revisions.Router({ model: this }); this.router = new revisions.Router({ model: this });
Backbone.history.start(); Backbone.history.start({ pushState: true });
// Set up internal listeners
this.listenTo( this, 'change:from', this.changeRevisionHandler ); this.listenTo( this, 'change:from', this.changeRevisionHandler );
this.listenTo( this, 'change:to', this.changeRevisionHandler ); this.listenTo( this, 'change:to', this.changeRevisionHandler );
this.listenTo( this, 'update:revisions', this.loadSurrounding ); this.listenTo( this, 'update:revisions', this.loadSurrounding );
@ -937,23 +951,30 @@ window.wp = window.wp || {};
revisions.Router = Backbone.Router.extend({ revisions.Router = Backbone.Router.extend({
initialize: function( options ) { initialize: function( options ) {
this.model = options.model; this.model = options.model;
this.routes = this.getRoutes();
// Maintain state history when dragging // Maintain state history when dragging
this.listenTo( this.model, 'update:diff', _.debounce( this.updateUrl, 250 ) ); this.listenTo( this.model, 'update:diff', _.debounce( this.updateUrl, 250 ) );
}, },
routes: { getRoutes: function() {
'from/:from/to/:to': 'handleRoute', var routes = {};
'at/:to': 'handleRoute' routes[this.baseUrl( '?from=:from&to=:to' )] = 'handleRoute';
routes[this.baseUrl( '?revision=:to' )] = 'handleRoute';
return routes;
},
baseUrl: function( url ) {
return this.model.get('baseUrl') + url;
}, },
updateUrl: function() { updateUrl: function() {
var from = this.model.has('from') ? this.model.get('from').id : 0; var from = this.model.has('from') ? this.model.get('from').id : 0;
var to = this.model.get('to').id; var to = this.model.get('to').id;
if ( this.model.get('compareTwoMode' ) ) if ( this.model.get('compareTwoMode' ) )
this.navigate( 'from/' + from + '/to/' + to ); this.navigate( this.baseUrl( '?from=' + from + '&to=' + to ) );
else else
this.navigate( 'at/' + to ); this.navigate( this.baseUrl( '?revision=' + to ) );
}, },
handleRoute: function( a, b ) { handleRoute: function( a, b ) {
@ -964,7 +985,7 @@ window.wp = window.wp || {};
b = this.model.revisions.get( a ); b = this.model.revisions.get( a );
a = this.model.revisions.prev( b ); a = this.model.revisions.prev( b );
b = b ? b.id : 0; b = b ? b.id : 0;
a = a ? a.id : 0 a = a ? a.id : 0;
compareTwo = false; compareTwo = false;
} else { } else {
compareTwo = true; compareTwo = true;
@ -984,7 +1005,7 @@ window.wp = window.wp || {};
from: selectedFromRevision from: selectedFromRevision
}); });
} }
revisions.settings.selectedRevision = to; revisions.settings.to = to;
} }
}); });

View File

@ -11,9 +11,12 @@ require_once('./admin.php');
require ABSPATH . 'wp-admin/includes/revision.php'; require ABSPATH . 'wp-admin/includes/revision.php';
wp_reset_vars( array( 'revision', 'action' ) ); wp_reset_vars( array( 'revision', 'action', 'from', 'to' ) );
$revision_id = absint( $revision ); $revision_id = absint( $revision );
$from = absint( $from );
if ( ! $revision_id )
$revision_id = absint( $to );
$redirect = 'edit.php'; $redirect = 'edit.php';
switch ( $action ) : switch ( $action ) :
@ -79,7 +82,7 @@ else
$parent_file = $submenu_file = 'edit.php'; $parent_file = $submenu_file = 'edit.php';
wp_enqueue_script( 'revisions' ); wp_enqueue_script( 'revisions' );
wp_localize_script( 'revisions', '_wpRevisionsSettings', wp_prepare_revisions_for_js( $post, $revision_id ) ); wp_localize_script( 'revisions', '_wpRevisionsSettings', wp_prepare_revisions_for_js( $post, $revision_id, $from ) );
/* Revisions Help Tab */ /* Revisions Help Tab */

View File

@ -897,7 +897,9 @@ function get_edit_post_link( $id = 0, $context = 'display' ) {
if ( ! $post = get_post( $id ) ) if ( ! $post = get_post( $id ) )
return; return;
if ( 'display' == $context ) if ( 'revision' === $post->post_type )
$action = '';
elseif ( 'display' == $context )
$action = '&action=edit'; $action = '&action=edit';
else else
$action = '&action=edit'; $action = '&action=edit';