Make WordPress Core

Changeset 24774


Ignore:
Timestamp:
07/23/2013 03:05:21 AM (11 years ago)
Author:
nacin
Message:

Autosave:

  • Remove editor.js as a dependency for autosave.js, as it was in 3.5.
  • Remove heartbeat.js as an implicit dependency.
  • Abstract out the serialization of title/content/excerpt for comparisons.

props azaozz.
fixes #24756.

Location:
trunk/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/js/autosave.js

    r24749 r24774  
    88                editor.onLoad.add( function() {
    99                    editor.save();
    10                     autosaveLast = ( $('#title').val() || '' ) + ( $('#content').val() || '' );
     10                    autosaveLast = wp.autosave.getCompareString();
    1111                });
    1212            }
    1313        });
    1414    } else {
    15         autosaveLast = ( $('#title').val() || '' ) + ( $('#content').val() || '' );
     15        autosaveLast = wp.autosave.getCompareString();
    1616    }
    1717
     
    4141
    4242    window.onbeforeunload = function(){
    43         var mce = typeof(tinymce) != 'undefined' ? tinymce.activeEditor : false, title, content;
    44 
    45         if ( mce && !mce.isHidden() ) {
    46             if ( mce.isDirty() )
     43        var editor = typeof(tinymce) != 'undefined' ? tinymce.activeEditor : false, compareString;
     44
     45        if ( editor && ! editor.isHidden() ) {
     46            if ( editor.isDirty() )
    4747                return autosaveL10n.saveAlert;
    4848        } else {
    4949            if ( fullscreen && fullscreen.settings.visible ) {
    50                 title = $('#wp-fullscreen-title').val() || '';
    51                 content = $("#wp_mce_fullscreen").val() || '';
     50                compareString = wp.autosave.getCompareString({
     51                    post_title: $('#wp-fullscreen-title').val() || '',
     52                    content: $('#wp_mce_fullscreen').val() || '',
     53                    excerpt: $('#excerpt').val() || ''
     54                });
    5255            } else {
    53                 title = $('#post #title').val() || '';
    54                 content = $('#post #content').val() || '';
    55             }
    56 
    57             if ( ( title || content ) && title + content != autosaveLast )
     56                compareString = wp.autosave.getCompareString();
     57            }
     58
     59            if ( compareString != autosaveLast )
    5860                return autosaveL10n.saveAlert;
    5961        }
     
    247249function autosave_enable_buttons() {
    248250    jQuery(document).trigger('autosave-enable-buttons');
    249     if ( ! wp.heartbeat.hasConnectionError() ) {
     251    if ( ! wp.heartbeat || ! wp.heartbeat.hasConnectionError() ) {
    250252        // delay that a bit to avoid some rare collisions while the DOM is being updated.
    251253        setTimeout(function(){
     
    274276autosave = function() {
    275277    var post_data = wp.autosave.getPostData(),
    276         doAutoSave = post_data.autosave,
     278        compareString,
    277279        successCallback;
    278280
    279281    blockSave = true;
     282
     283    // post_data.content cannot be retrieved at the moment
     284    if ( ! post_data.autosave )
     285        return false;
    280286
    281287    // No autosave while thickbox is open (media buttons)
    282288    if ( jQuery("#TB_window").css('display') == 'block' )
    283         doAutoSave = false;
     289        return false;
     290
     291    compareString = wp.autosave.getCompareString( post_data );
    284292
    285293    // 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 )
    294295        return false;
    295     }
     296
     297    autosaveLast = compareString;
     298    jQuery(document).triggerHandler('wpcountwords', [ post_data["content"] ]);
    296299
    297300    // Disable buttons until we know the save completed.
     
    306309    jQuery.ajax({
    307310        data: post_data,
    308         beforeSend: doAutoSave ? autosave_loading : null,
     311        beforeSend: autosave_loading,
    309312        type: "POST",
    310313        url: ajaxurl,
     
    383386
    384387    return data;
    385 }
     388};
     389
     390// Concatenate title, content and excerpt. Used to track changes when auto-saving.
     391wp.autosave.getCompareString = 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};
    386398
    387399wp.autosave.local = {
    388400
    389     lastsaveddata: '',
     401    lastSavedData: '',
    390402    blog_id: 0,
    391     ajaxurl: window.ajaxurl || 'wp-admin/admin-ajax.php',
    392403    hasStorage: false,
    393404
     
    493504     */
    494505    save: function( data ) {
    495         var result = false;
     506        var result = false, post_data, compareString;
    496507
    497508        if ( ! data ) {
     
    503514        }
    504515
    505         // If the content and title did not change since the last save, don't save again
    506         if ( post_data.post_title + ': ' + post_data.content == this.lastsaveddata )
     516        // Cannot get the post data at the moment
     517        if ( ! post_data.autosave )
    507518            return false;
    508519
    509         // Cannot get the post data at the moment
    510         if ( !post_data.autosave )
     520        compareString = wp.autosave.getCompareString( 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 )
    511524            return false;
    512525
     
    516529
    517530        if ( result )
    518             this.lastsaveddata = post_data.post_title + ': ' + post_data.content;
     531            this.lastSavedData = compareString;
    519532
    520533        return result;
     
    525538        var self = this;
    526539
    527         // Check if the browser supports sessionStorage and editor.js is loaded
    528         if ( ! this.checkStorage() || typeof switchEditors == 'undefined' )
     540        // Check if the browser supports sessionStorage and it's not disabled
     541        if ( ! this.checkStorage() )
    529542            return;
    530543
     
    539552            this.blog_id = typeof window.autosaveL10n != 'undefined' ? window.autosaveL10n.blog_id : 0;
    540553
    541         this.checkPost();
    542554        $(document).ready( function(){ self.run(); } );
    543555    },
     
    545557    // Run on DOM ready
    546558    run: function() {
    547         var self = this, post_data;
    548 
    549         // Set the comparison string
    550         if ( !this.lastsaveddata ) {
    551             post_data = wp.autosave.getPostData();
    552 
    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             else
    556                 this.lastsaveddata = post_data.post_title + ': ' + post_data.content;
    557         }
     559        var self = this;
     560
     561        // Check if the local post data is different than the loaded post data.
     562        this.checkPost();
    558563
    559564        // Set the schedule
     
    606611     */
    607612    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,
    609614            post_id = $('#post_ID').val() || 0, cookie = wpCookies.get( 'wp-saving-post-' + post_id );
    610615
     
    626631            return;
    627632
     633        content = $('#content').val() || '';
     634        post_title = $('#title').val() || '';
     635        excerpt = $('#excerpt').val() || '';
     636
     637        if ( $('#wp-content-wrap').hasClass('tmce-active') && typeof switchEditors != 'undefined' )
     638            content = switchEditors.pre_wpautop( content );
     639
    628640        // 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 );
    632 
    633             if ( $('#wp-content-wrap').hasClass('tmce-active') )
    634                 content = switchEditors.pre_wpautop( content );
    635 
    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;
     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;
    638643        }
    639644
    640645        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        };
    642651
    643652        notice = $('#local-storage-notice');
     
    667676        if ( post_data ) {
    668677            // Set the last saved data
    669             this.lastsaveddata = post_data.post_title + ': ' + post_data.content;
     678            this.lastSavedData = wp.autosave.getCompareString( post_data );
    670679
    671680            if ( $('#title').val() != post_data.post_title )
     
    675684            editor = typeof tinymce != 'undefined' && tinymce.get('content');
    676685
    677             if ( editor && ! editor.isHidden() ) {
     686            if ( editor && ! editor.isHidden() && typeof switchEditors != 'undefined' ) {
    678687                // Make sure there's an undo level in the editor
    679688                editor.undoManager.add();
     
    690699        return false;
    691700    }
    692 }
     701};
    693702
    694703wp.autosave.local.init();
  • trunk/wp-includes/script-loader.php

    r24755 r24774  
    104104    ) );
    105105
    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 );
    107107
    108108    $scripts->add( 'heartbeat', "/wp-includes/js/heartbeat$suffix.js", array('jquery'), false, 1 );
Note: See TracChangeset for help on using the changeset viewer.