19
19
var post_name, parent_id, data,
20
20
time = ( new Date() ).getTime(),
22
editor = typeof tinymce !== 'undefined' && tinymce.get('content');
24
24
// Don't run editor.save() more often than every 3 sec.
25
25
// It is resource intensive and might slow down typing in long posts on slow devices.
26
if ( editor && ! editor.isHidden() && time - 3000 > lastTriggerSave ) {
26
if ( editor && editor.isDirty() && ! editor.isHidden() && time - 3000 > lastTriggerSave ) {
28
28
lastTriggerSave = time;
88
88
$document.trigger( 'autosave-enable-buttons' );
91
function getEditor() {
92
return typeof tinymce !== 'undefined' && tinymce.get('content');
91
95
// Autosave in localStorage
92
96
function autosaveLocal() {
93
var restorePostData, undoPostData, blog_id, post_id, hasStorage, intervalTimer,
97
var blog_id, post_id, hasStorage, intervalTimer,
95
99
isSuspended = false;
266
270
intervalTimer = window.setInterval( save, 15000 );
268
272
$( 'form#post' ).on( 'submit.autosave-local', function() {
269
var editor = typeof tinymce !== 'undefined' && tinymce.get('content'),
273
var editor = getEditor(),
270
274
post_id = $('#post_ID').val() || 0;
272
276
if ( editor && ! editor.isHidden() ) {
310
314
function checkPost() {
311
315
var content, post_title, excerpt, $notice,
312
316
postData = getSavedPostData(),
313
cookie = wpCookies.get( 'wp-saving-post' );
317
cookie = wpCookies.get( 'wp-saving-post' ),
318
$newerAutosaveNotice = $( '#has-newer-autosave' ).parent( '.notice' );
315
320
if ( cookie === post_id + '-saved' ) {
316
321
wpCookies.remove( 'wp-saving-post' );
326
// There is a newer autosave. Don't show two "restore" notices at the same time.
327
if ( $( '#has-newer-autosave' ).length ) {
331
331
content = $( '#content' ).val() || '';
332
332
post_title = $( '#title' ).val() || '';
333
333
excerpt = $( '#excerpt' ).val() || '';
341
restorePostData = postData;
344
post_title: post_title,
348
341
$notice = $( '#local-storage-notice' )
349
342
.insertAfter( $( '.wrap h1, .wrap h2' ).first() )
350
.addClass( 'notice-warning' )
353
$notice.on( 'click.autosave-local', function( event ) {
354
var $target = $( event.target );
356
if ( $target.hasClass( 'restore-backup' ) ) {
357
restorePost( restorePostData );
358
$target.parent().hide();
359
$(this).find( 'p.undo-restore' ).show();
360
$notice.removeClass( 'notice-warning' ).addClass( 'notice-success' );
361
} else if ( $target.hasClass( 'undo-restore-backup' ) ) {
362
restorePost( undoPostData );
363
$target.parent().hide();
364
$(this).find( 'p.local-restore' ).show();
365
$notice.removeClass( 'notice-success' ).addClass( 'notice-warning' );
368
event.preventDefault();
343
.addClass( 'notice-warning' );
345
if ( $newerAutosaveNotice.length ) {
346
// If there is a "server" autosave notice, hide it.
347
// The data in the session storage is either the same or newer.
348
$newerAutosaveNotice.slideUp( 150, function() {
349
$notice.slideDown( 150 );
352
$notice.slideDown( 200 );
355
$notice.find( '.restore-backup' ).on( 'click.autosave-local', function() {
356
restorePost( postData );
357
$notice.fadeTo( 250, 0, function() {
358
$notice.slideUp( 150 );
384
375
$( '#excerpt' ).val( postData.excerpt || '' );
385
editor = typeof tinymce !== 'undefined' && tinymce.get('content');
376
editor = getEditor();
387
378
if ( editor && ! editor.isHidden() && typeof switchEditors !== 'undefined' ) {
379
if ( editor.settings.wpautop && postData.content ) {
380
postData.content = switchEditors.wpautop( postData.content );
388
383
// Make sure there's an undo level in the editor
389
editor.undoManager.add();
390
editor.setContent( postData.content ? switchEditors.wpautop( postData.content ) : '' );
384
editor.undoManager.transact( function() {
385
editor.setContent( postData.content || '' );
386
editor.nodeChanged();
392
389
// Make sure the Text editor is selected
393
390
$( '#content-html' ).click();
394
$( '#content' ).val( postData.content );
391
$( '#content' ).focus();
392
// Using document.execCommand() will let the user undo.
393
document.execCommand( 'selectAll' );
394
document.execCommand( 'insertText', false, postData.content || '' );