Ticket #24756: 24756.2.diff
File 24756.2.diff, 9.9 KB (added by , 11 years ago) |
---|
-
wp-includes/js/autosave.js
7 7 if ( 'content' == editor.id ) { 8 8 editor.onLoad.add( function() { 9 9 editor.save(); 10 autosaveLast = ( $('#title').val() || '' ) + ( $('#content').val() || '');10 autosaveLast = wp.autosave.getLastSavedCompareString(); 11 11 }); 12 12 } 13 13 }); 14 14 } else { 15 autosaveLast = ( $('#title').val() || '' ) + ( $('#content').val() || '');15 autosaveLast = wp.autosave.getLastSavedCompareString(); 16 16 } 17 17 18 18 autosavePeriodical = $.schedule({time: autosaveL10n.autosaveInterval * 1000, func: function() { autosave(); }, repeat: true, protect: true}); … … 40 40 }); 41 41 42 42 window.onbeforeunload = function(){ 43 var mce = typeof(tinymce) != 'undefined' ? tinymce.activeEditor : false, title, content;43 var editor = typeof(tinymce) != 'undefined' ? tinymce.activeEditor : false, compareString; 44 44 45 if ( mce && !mce.isHidden() ) {46 if ( mce.isDirty() )45 if ( editor && ! editor.isHidden() ) { 46 if ( editor.isDirty() ) 47 47 return autosaveL10n.saveAlert; 48 48 } else { 49 49 if ( fullscreen && fullscreen.settings.visible ) { 50 title = $('#wp-fullscreen-title').val() || ''; 51 content = $("#wp_mce_fullscreen").val() || ''; 50 compareString = wp.autosave.getLastSavedCompareString({ 51 post_title: $('#wp-fullscreen-title').val() || '', 52 content: $('#wp_mce_fullscreen').val() || '', 53 excerpt: $('#excerpt').val() || '' 54 }); 52 55 } else { 53 title = $('#post #title').val() || ''; 54 content = $('#post #content').val() || ''; 56 compareString = wp.autosave.getLastSavedCompareString(); 55 57 } 56 58 57 if ( ( title || content ) && title + content!= autosaveLast )59 if ( compareString != autosaveLast ) 58 60 return autosaveL10n.saveAlert; 59 61 } 60 62 }; … … 246 248 247 249 function autosave_enable_buttons() { 248 250 jQuery(document).trigger('autosave-enable-buttons'); 249 if ( ! wp.heartbeat .hasConnectionError() ) {251 if ( ! wp.heartbeat || ! wp.heartbeat.hasConnectionError() ) { 250 252 // delay that a bit to avoid some rare collisions while the DOM is being updated. 251 253 setTimeout(function(){ 252 254 var parent = jQuery('#submitpost'); … … 273 275 274 276 autosave = function() { 275 277 var post_data = wp.autosave.getPostData(), 276 doAutoSave = post_data.autosave,278 compareString, 277 279 successCallback; 278 280 279 281 blockSave = true; 280 282 283 // post_data.content cannot be retrieved at the moment 284 if ( ! post_data.autosave ) 285 return false; 286 281 287 // No autosave while thickbox is open (media buttons) 282 288 if ( jQuery("#TB_window").css('display') == 'block' ) 283 doAutoSave =false;289 return false; 284 290 291 compareString = wp.autosave.getLastSavedCompareString( post_data ); 292 285 293 // Nothing to save or no change. 286 if ( ( post_data["post_title"].length == 0 && post_data["content"].length == 0 ) || post_data["post_title"] + post_data["content"] == autosaveLast ) { 287 doAutoSave = false; 288 } 289 290 if ( doAutoSave ) { 291 autosaveLast = post_data["post_title"] + post_data["content"]; 292 jQuery(document).triggerHandler('wpcountwords', [ post_data["content"] ]); 293 } else { 294 if ( compareString == autosaveLast ) 294 295 return false; 295 }296 296 297 autosaveLast = compareString; 298 jQuery(document).triggerHandler('wpcountwords', [ post_data["content"] ]); 299 297 300 // Disable buttons until we know the save completed. 298 301 autosave_disable_buttons(); 299 302 … … 305 308 306 309 jQuery.ajax({ 307 310 data: post_data, 308 beforeSend: doAutoSave ? autosave_loading : null,311 beforeSend: autosave_loading, 309 312 type: "POST", 310 313 url: ajaxurl, 311 314 success: successCallback … … 382 385 data['auto_draft'] = '1'; 383 386 384 387 return data; 385 } 388 }; 386 389 390 // Concatenate title, content and excerpt. Used to track changes when auto-saving. 391 wp.autosave.getLastSavedCompareString = function( post_data ) { 392 if ( typeof post_data === 'object' ) { 393 return ( post_data.post_title || '' ) + '::' + ( post_data.content || '' ) + '::' + ( post_data.excerpt || '' ); 394 } 395 396 return ( $('#title').val() || '' ) + '::' + ( $('#content').val() || '' ) + '::' + ( $('#excerpt').val() || '' ); 397 }; 398 387 399 wp.autosave.local = { 388 400 389 401 lastsaveddata: '', 390 402 blog_id: 0, 391 ajaxurl: window.ajaxurl || 'wp-admin/admin-ajax.php',392 403 hasStorage: false, 393 404 394 405 // Check if the browser supports sessionStorage and it's not disabled … … 492 503 * @return bool 493 504 */ 494 505 save: function( data ) { 495 var result = false ;506 var result = false, post_data, compareString; 496 507 497 508 if ( ! data ) { 498 509 post_data = wp.autosave.getPostData(); … … 502 513 post_data.autosave = true; 503 514 } 504 515 505 // If the content and title did not change since the last save, don't save again506 if ( post_data.post_title + ': ' + post_data.content == this.lastsaveddata)516 // Cannot get the post data at the moment 517 if ( ! post_data.autosave ) 507 518 return false; 508 519 509 // Cannot get the post data at the moment 510 if ( !post_data.autosave ) 520 compareString = wp.autosave.getLastSavedCompareString( post_data ); 521 522 // If the content, title and excerpt did not change since the last save, don't save again 523 if ( compareString == this.lastsaveddata ) 511 524 return false; 512 525 513 526 post_data['save_time'] = (new Date()).getTime(); … … 515 528 result = this.setData( post_data ); 516 529 517 530 if ( result ) 518 this.lastsaveddata = post_data.post_title + ': ' + post_data.content;531 this.lastsaveddata = compareString; 519 532 520 533 return result; 521 534 }, … … 524 537 init: function( settings ) { 525 538 var self = this; 526 539 527 // Check if the browser supports sessionStorage and editor.js is loaded528 if ( ! this.checkStorage() || typeof switchEditors == 'undefined')540 // Check if the browser supports sessionStorage and it's not disabled 541 if ( ! this.checkStorage() ) 529 542 return; 530 543 531 544 // Don't run if the post type supports neither 'editor' (textarea#content) nor 'excerpt'. … … 538 551 if ( !this.blog_id ) 539 552 this.blog_id = typeof window.autosaveL10n != 'undefined' ? window.autosaveL10n.blog_id : 0; 540 553 541 this.checkPost();542 554 $(document).ready( function(){ self.run(); } ); 543 555 }, 544 556 545 557 // Run on DOM ready 546 558 run: function() { 547 var self = this , post_data;559 var self = this; 548 560 549 // Set the comparison string 550 if ( !this.lastsaveddata ) { 551 post_data = wp.autosave.getPostData(); 561 // Check if the local post data is different than the loaded post data. 562 this.checkPost(); 552 563 553 if ( post_data.content && $('#wp-content-wrap').hasClass('tmce-active') )554 this.lastsaveddata = post_data.post_title + ': ' + switchEditors.pre_wpautop( post_data.content );555 else556 this.lastsaveddata = post_data.post_title + ': ' + post_data.content;557 }558 559 564 // Set the schedule 560 565 this.schedule = $.schedule({ 561 566 time: 15 * 1000, … … 605 610 * @return void 606 611 */ 607 612 checkPost: function() { 608 var self = this, post_data = this.getData(), content, check_data, strip_tags = false, notice,613 var self = this, post_data = this.getData(), content, post_title, excerpt, notice, 609 614 post_id = $('#post_ID').val() || 0, cookie = wpCookies.get( 'wp-saving-post-' + post_id ); 610 615 611 616 if ( ! post_data ) … … 625 630 if ( $('#has-newer-autosave').length ) 626 631 return; 627 632 628 // cookie == 'check' means the post was not saved properly, always show #local-storage-notice 629 if ( cookie != 'check' ) { 630 content = $('#content').val(); 631 check_data = $.extend( {}, post_data ); 633 content = $('#content').val() || ''; 634 post_title = $('#title').val() || ''; 635 excerpt = $('#excerpt').val() || ''; 632 636 633 if ( $('#wp-content-wrap').hasClass('tmce-active'))634 637 if ( $('#wp-content-wrap').hasClass('tmce-active') && typeof switchEditors != 'undefined' ) 638 content = switchEditors.pre_wpautop( content ); 635 639 636 if ( this.compare( content, check_data.content ) && this.compare( $('#title').val(), check_data.post_title ) && this.compare( $('#excerpt').val(), check_data.excerpt ) ) 637 return; 640 // cookie == 'check' means the post was not saved properly, always show #local-storage-notice 641 if ( cookie != 'check' && this.compare( content, post_data.content ) && this.compare( post_title, post_data.post_title ) && this.compare( excerpt, post_data.excerpt ) ) { 642 return; 638 643 } 639 644 640 645 this.restore_post_data = post_data; 641 this.undo_post_data = wp.autosave.getPostData(); 646 this.undo_post_data = { 647 content: content, 648 post_title: post_title, 649 excerpt: excerpt 650 }; 642 651 643 652 notice = $('#local-storage-notice'); 644 653 $('.wrap h2').first().after( notice.addClass('updated').show() ); … … 666 675 667 676 if ( post_data ) { 668 677 // Set the last saved data 669 this.lastsaveddata = post_data.post_title + ': ' + post_data.content;678 this.lastsaveddata = wp.autosave.getLastSavedCompareString( post_data ); 670 679 671 680 if ( $('#title').val() != post_data.post_title ) 672 681 $('#title').focus().val( post_data.post_title || '' ); … … 674 683 $('#excerpt').val( post_data.excerpt || '' ); 675 684 editor = typeof tinymce != 'undefined' && tinymce.get('content'); 676 685 677 if ( editor && ! editor.isHidden() ) {686 if ( editor && ! editor.isHidden() && typeof switchEditors != 'undefined' ) { 678 687 // Make sure there's an undo level in the editor 679 688 editor.undoManager.add(); 680 689 editor.setContent( post_data.content ? switchEditors.wpautop( post_data.content ) : '' ); … … 689 698 690 699 return false; 691 700 } 692 } 701 }; 693 702 694 703 wp.autosave.local.init(); 695 704 -
wp-includes/script-loader.php
103 103 'dismiss' => __('Dismiss'), 104 104 ) ); 105 105 106 $scripts->add( 'autosave', "/wp-includes/js/autosave$suffix.js", array('schedule', 'wp-ajax-response' , 'editor'), false, 1 );106 $scripts->add( 'autosave', "/wp-includes/js/autosave$suffix.js", array('schedule', 'wp-ajax-response'), false, 1 ); 107 107 108 108 $scripts->add( 'heartbeat', "/wp-includes/js/heartbeat$suffix.js", array('jquery'), false, 1 ); 109 109 did_action( 'init' ) && $scripts->localize( 'heartbeat', 'heartbeatSettings',