Autosave: improve the notice when the sessionStorage autosave is different than the content.
- Make it higher priority than the server autosave. - Change it so the editors undo and redo can be used. - Replace the restore link with a button. - Add better explanation/help. See #37025. Built from https://develop.svn.wordpress.org/trunk@37737 git-svn-id: http://core.svn.wordpress.org/trunk@37702 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
d242446e59
commit
c8d203e1a3
|
@ -2003,14 +2003,13 @@ function convert_to_screen( $hook_name ) {
|
||||||
*/
|
*/
|
||||||
function _local_storage_notice() {
|
function _local_storage_notice() {
|
||||||
?>
|
?>
|
||||||
<div id="local-storage-notice" class="hidden notice">
|
<div id="local-storage-notice" class="hidden notice is-dismissible">
|
||||||
<p class="local-restore">
|
<p class="local-restore">
|
||||||
<?php _e( 'The backup of this post in your browser is different from the version below.' ); ?>
|
<?php _e( 'The backup of this post in your browser is different from the version below.' ); ?>
|
||||||
<a class="restore-backup" href="#"><?php _e('Restore the backup.'); ?></a>
|
<button type="button" class="button restore-backup"><?php _e('Restore the backup'); ?></button>
|
||||||
</p>
|
</p>
|
||||||
<p class="undo-restore hidden">
|
<p class="help">
|
||||||
<?php _e('Post restored successfully.'); ?>
|
<?php _e( 'This will replace the current editor content with the last backup version. You can use undo and redo in the editor to get the old content back or to return to the restored version.' ); ?>
|
||||||
<a class="undo-restore-backup" href="#"><?php _e('Undo.'); ?></a>
|
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<?php
|
<?php
|
||||||
|
|
|
@ -19,11 +19,11 @@ window.autosave = function() {
|
||||||
var post_name, parent_id, data,
|
var post_name, parent_id, data,
|
||||||
time = ( new Date() ).getTime(),
|
time = ( new Date() ).getTime(),
|
||||||
cats = [],
|
cats = [],
|
||||||
editor = typeof tinymce !== 'undefined' && tinymce.get('content');
|
editor = getEditor();
|
||||||
|
|
||||||
// Don't run editor.save() more often than every 3 sec.
|
// Don't run editor.save() more often than every 3 sec.
|
||||||
// It is resource intensive and might slow down typing in long posts on slow devices.
|
// It is resource intensive and might slow down typing in long posts on slow devices.
|
||||||
if ( editor && ! editor.isHidden() && time - 3000 > lastTriggerSave ) {
|
if ( editor && editor.isDirty() && ! editor.isHidden() && time - 3000 > lastTriggerSave ) {
|
||||||
editor.save();
|
editor.save();
|
||||||
lastTriggerSave = time;
|
lastTriggerSave = time;
|
||||||
}
|
}
|
||||||
|
@ -88,9 +88,13 @@ window.autosave = function() {
|
||||||
$document.trigger( 'autosave-enable-buttons' );
|
$document.trigger( 'autosave-enable-buttons' );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getEditor() {
|
||||||
|
return typeof tinymce !== 'undefined' && tinymce.get('content');
|
||||||
|
}
|
||||||
|
|
||||||
// Autosave in localStorage
|
// Autosave in localStorage
|
||||||
function autosaveLocal() {
|
function autosaveLocal() {
|
||||||
var restorePostData, undoPostData, blog_id, post_id, hasStorage, intervalTimer,
|
var blog_id, post_id, hasStorage, intervalTimer,
|
||||||
lastCompareString,
|
lastCompareString,
|
||||||
isSuspended = false;
|
isSuspended = false;
|
||||||
|
|
||||||
|
@ -266,7 +270,7 @@ window.autosave = function() {
|
||||||
intervalTimer = window.setInterval( save, 15000 );
|
intervalTimer = window.setInterval( save, 15000 );
|
||||||
|
|
||||||
$( 'form#post' ).on( 'submit.autosave-local', function() {
|
$( 'form#post' ).on( 'submit.autosave-local', function() {
|
||||||
var editor = typeof tinymce !== 'undefined' && tinymce.get('content'),
|
var editor = getEditor(),
|
||||||
post_id = $('#post_ID').val() || 0;
|
post_id = $('#post_ID').val() || 0;
|
||||||
|
|
||||||
if ( editor && ! editor.isHidden() ) {
|
if ( editor && ! editor.isHidden() ) {
|
||||||
|
@ -310,7 +314,8 @@ window.autosave = function() {
|
||||||
function checkPost() {
|
function checkPost() {
|
||||||
var content, post_title, excerpt, $notice,
|
var content, post_title, excerpt, $notice,
|
||||||
postData = getSavedPostData(),
|
postData = getSavedPostData(),
|
||||||
cookie = wpCookies.get( 'wp-saving-post' );
|
cookie = wpCookies.get( 'wp-saving-post' ),
|
||||||
|
$newerAutosaveNotice = $( '#has-newer-autosave' ).parent( '.notice' );
|
||||||
|
|
||||||
if ( cookie === post_id + '-saved' ) {
|
if ( cookie === post_id + '-saved' ) {
|
||||||
wpCookies.remove( 'wp-saving-post' );
|
wpCookies.remove( 'wp-saving-post' );
|
||||||
|
@ -323,11 +328,6 @@ window.autosave = function() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// There is a newer autosave. Don't show two "restore" notices at the same time.
|
|
||||||
if ( $( '#has-newer-autosave' ).length ) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
content = $( '#content' ).val() || '';
|
content = $( '#content' ).val() || '';
|
||||||
post_title = $( '#title' ).val() || '';
|
post_title = $( '#title' ).val() || '';
|
||||||
excerpt = $( '#excerpt' ).val() || '';
|
excerpt = $( '#excerpt' ).val() || '';
|
||||||
|
@ -338,34 +338,25 @@ window.autosave = function() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
restorePostData = postData;
|
|
||||||
undoPostData = {
|
|
||||||
content: content,
|
|
||||||
post_title: post_title,
|
|
||||||
excerpt: excerpt
|
|
||||||
};
|
|
||||||
|
|
||||||
$notice = $( '#local-storage-notice' )
|
$notice = $( '#local-storage-notice' )
|
||||||
.insertAfter( $( '.wrap h1, .wrap h2' ).first() )
|
.insertAfter( $( '.wrap h1, .wrap h2' ).first() )
|
||||||
.addClass( 'notice-warning' )
|
.addClass( 'notice-warning' );
|
||||||
.show();
|
|
||||||
|
|
||||||
$notice.on( 'click.autosave-local', function( event ) {
|
if ( $newerAutosaveNotice.length ) {
|
||||||
var $target = $( event.target );
|
// If there is a "server" autosave notice, hide it.
|
||||||
|
// The data in the session storage is either the same or newer.
|
||||||
if ( $target.hasClass( 'restore-backup' ) ) {
|
$newerAutosaveNotice.slideUp( 150, function() {
|
||||||
restorePost( restorePostData );
|
$notice.slideDown( 150 );
|
||||||
$target.parent().hide();
|
});
|
||||||
$(this).find( 'p.undo-restore' ).show();
|
} else {
|
||||||
$notice.removeClass( 'notice-warning' ).addClass( 'notice-success' );
|
$notice.slideDown( 200 );
|
||||||
} else if ( $target.hasClass( 'undo-restore-backup' ) ) {
|
|
||||||
restorePost( undoPostData );
|
|
||||||
$target.parent().hide();
|
|
||||||
$(this).find( 'p.local-restore' ).show();
|
|
||||||
$notice.removeClass( 'notice-success' ).addClass( 'notice-warning' );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
event.preventDefault();
|
$notice.find( '.restore-backup' ).on( 'click.autosave-local', function() {
|
||||||
|
restorePost( postData );
|
||||||
|
$notice.fadeTo( 250, 0, function() {
|
||||||
|
$notice.slideUp( 150 );
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -382,16 +373,25 @@ window.autosave = function() {
|
||||||
}
|
}
|
||||||
|
|
||||||
$( '#excerpt' ).val( postData.excerpt || '' );
|
$( '#excerpt' ).val( postData.excerpt || '' );
|
||||||
editor = typeof tinymce !== 'undefined' && tinymce.get('content');
|
editor = getEditor();
|
||||||
|
|
||||||
if ( editor && ! editor.isHidden() && typeof switchEditors !== 'undefined' ) {
|
if ( editor && ! editor.isHidden() && typeof switchEditors !== 'undefined' ) {
|
||||||
|
if ( editor.settings.wpautop && postData.content ) {
|
||||||
|
postData.content = switchEditors.wpautop( postData.content );
|
||||||
|
}
|
||||||
|
|
||||||
// Make sure there's an undo level in the editor
|
// Make sure there's an undo level in the editor
|
||||||
editor.undoManager.add();
|
editor.undoManager.transact( function() {
|
||||||
editor.setContent( postData.content ? switchEditors.wpautop( postData.content ) : '' );
|
editor.setContent( postData.content || '' );
|
||||||
|
editor.nodeChanged();
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
// Make sure the Text editor is selected
|
// Make sure the Text editor is selected
|
||||||
$( '#content-html' ).click();
|
$( '#content-html' ).click();
|
||||||
$( '#content' ).val( postData.content );
|
$( '#content' ).focus();
|
||||||
|
// Using document.execCommand() will let the user undo.
|
||||||
|
document.execCommand( 'selectAll' );
|
||||||
|
document.execCommand( 'insertText', false, postData.content || '' );
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -4,7 +4,7 @@
|
||||||
*
|
*
|
||||||
* @global string $wp_version
|
* @global string $wp_version
|
||||||
*/
|
*/
|
||||||
$wp_version = '4.6-alpha-37736';
|
$wp_version = '4.6-alpha-37737';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||||
|
|
Loading…
Reference in New Issue