[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

/wp-includes/js/ -> autosave.js (source)

   1  /**
   2   * @output wp-includes/js/autosave.js
   3   */
   4  
   5  /* global tinymce, wpCookies, autosaveL10n, switchEditors */
   6  // Back-compat
   7  window.autosave = function() {
   8      return true;
   9  };
  10  
  11  /**
  12   * Adds autosave to the window object on dom ready.
  13   *
  14   * @since 3.9.0
  15   *
  16   * @param {jQuery} $ jQuery object.
  17   * @param {window} The window object.
  18   *
  19   */
  20  ( function( $, window ) {
  21      /**
  22       * Auto saves the post.
  23       *
  24       * @since 3.9.0
  25       *
  26       * @returns {Object}
  27       *     {{
  28       *         getPostData: getPostData,
  29       *         getCompareString: getCompareString,
  30       *         disableButtons: disableButtons,
  31       *         enableButtons: enableButtons,
  32       *         local: ({hasStorage, getSavedPostData, save, suspend, resume}|*),
  33       *         server: ({tempBlockSave, triggerSave, postChanged, suspend, resume}|*)}
  34       *     }
  35       *     The object with all functions for autosave.
  36       */
  37  	function autosave() {
  38          var initialCompareString,
  39              lastTriggerSave = 0,
  40              $document = $(document);
  41  
  42          /**
  43           * Returns the data saved in both local and remote autosave.
  44           *
  45           * @since 3.9.0
  46           *
  47           * @param {string} type The type of autosave either local or remote.
  48           *
  49           * @returns {Object} Object containing the post data.
  50           */
  51  		function getPostData( type ) {
  52              var post_name, parent_id, data,
  53                  time = ( new Date() ).getTime(),
  54                  cats = [],
  55                  editor = getEditor();
  56  
  57              // Don't run editor.save() more often than every 3 seconds.
  58              // It is resource intensive and might slow down typing in long posts on slow devices.
  59              if ( editor && editor.isDirty() && ! editor.isHidden() && time - 3000 > lastTriggerSave ) {
  60                  editor.save();
  61                  lastTriggerSave = time;
  62              }
  63  
  64              data = {
  65                  post_id: $( '#post_ID' ).val() || 0,
  66                  post_type: $( '#post_type' ).val() || '',
  67                  post_author: $( '#post_author' ).val() || '',
  68                  post_title: $( '#title' ).val() || '',
  69                  content: $( '#content' ).val() || '',
  70                  excerpt: $( '#excerpt' ).val() || ''
  71              };
  72  
  73              if ( type === 'local' ) {
  74                  return data;
  75              }
  76  
  77              $( 'input[id^="in-category-"]:checked' ).each( function() {
  78                  cats.push( this.value );
  79              });
  80              data.catslist = cats.join(',');
  81  
  82              if ( post_name = $( '#post_name' ).val() ) {
  83                  data.post_name = post_name;
  84              }
  85  
  86              if ( parent_id = $( '#parent_id' ).val() ) {
  87                  data.parent_id = parent_id;
  88              }
  89  
  90              if ( $( '#comment_status' ).prop( 'checked' ) ) {
  91                  data.comment_status = 'open';
  92              }
  93  
  94              if ( $( '#ping_status' ).prop( 'checked' ) ) {
  95                  data.ping_status = 'open';
  96              }
  97  
  98              if ( $( '#auto_draft' ).val() === '1' ) {
  99                  data.auto_draft = '1';
 100              }
 101  
 102              return data;
 103          }
 104  
 105          /**
 106           * Concatenates the title, content and excerpt. This is used to track changes
 107           * when auto-saving.
 108           *
 109           * @since 3.9.0
 110           *
 111           * @param {Object} postData The object containing the post data.
 112           *
 113           * @returns {string} A concatenated string with title, content and excerpt.
 114           */
 115  		function getCompareString( postData ) {
 116              if ( typeof postData === 'object' ) {
 117                  return ( postData.post_title || '' ) + '::' + ( postData.content || '' ) + '::' + ( postData.excerpt || '' );
 118              }
 119  
 120              return ( $('#title').val() || '' ) + '::' + ( $('#content').val() || '' ) + '::' + ( $('#excerpt').val() || '' );
 121          }
 122  
 123          /**
 124           * Disables save buttons.
 125           *
 126           * @since 3.9.0
 127           *
 128           * @returns {void}
 129           */
 130  		function disableButtons() {
 131              $document.trigger('autosave-disable-buttons');
 132  
 133              // Re-enable 5 sec later. Just gives autosave a head start to avoid collisions.
 134              setTimeout( enableButtons, 5000 );
 135          }
 136  
 137          /**
 138           * Enables save buttons.
 139           *
 140           * @since 3.9.0
 141           *
 142           * @returns {void}
 143           */
 144  		function enableButtons() {
 145              $document.trigger( 'autosave-enable-buttons' );
 146          }
 147  
 148          /**
 149           * Gets the content editor.
 150           *
 151           * @since 4.6.0
 152           *
 153           * @returns {boolean|*} Returns either false if the editor is undefined,
 154           *                         or the instance of the content editor.
 155           */
 156  		function getEditor() {
 157              return typeof tinymce !== 'undefined' && tinymce.get('content');
 158          }
 159  
 160          /**
 161           * Autosave in localStorage.
 162           *
 163           * @since 3.9.0
 164           *
 165           * @returns {
 166           * {
 167           *     hasStorage: *,
 168           *     getSavedPostData: getSavedPostData,
 169           *     save: save,
 170           *     suspend: suspend,
 171           *     resume: resume
 172           *     }
 173           * }
 174           * The object with all functions for local storage autosave.
 175           */
 176  		function autosaveLocal() {
 177              var blog_id, post_id, hasStorage, intervalTimer,
 178                  lastCompareString,
 179                  isSuspended = false;
 180  
 181              /**
 182               * Checks if the browser supports sessionStorage and it's not disabled.
 183               *
 184               * @since 3.9.0
 185               *
 186               * @returns {boolean} True if the sessionStorage is supported and enabled.
 187               */
 188  			function checkStorage() {
 189                  var test = Math.random().toString(),
 190                      result = false;
 191  
 192                  try {
 193                      window.sessionStorage.setItem( 'wp-test', test );
 194                      result = window.sessionStorage.getItem( 'wp-test' ) === test;
 195                      window.sessionStorage.removeItem( 'wp-test' );
 196                  } catch(e) {}
 197  
 198                  hasStorage = result;
 199                  return result;
 200              }
 201  
 202              /**
 203               * Initializes the local storage.
 204               *
 205               * @since 3.9.0
 206               *
 207               * @returns {boolean|Object} False if no sessionStorage in the browser or an Object
 208               *                           containing all postData for this blog.
 209               */
 210  			function getStorage() {
 211                  var stored_obj = false;
 212                  // Separate local storage containers for each blog_id
 213                  if ( hasStorage && blog_id ) {
 214                      stored_obj = sessionStorage.getItem( 'wp-autosave-' + blog_id );
 215  
 216                      if ( stored_obj ) {
 217                          stored_obj = JSON.parse( stored_obj );
 218                      } else {
 219                          stored_obj = {};
 220                      }
 221                  }
 222  
 223                  return stored_obj;
 224              }
 225  
 226              /**
 227               * Sets the storage for this blog. Confirms that the data was saved
 228               * successfully.
 229               *
 230               * @since 3.9.0
 231               *
 232               * @returns {boolean} True if the data was saved successfully, false if it wasn't saved.
 233               */
 234  			function setStorage( stored_obj ) {
 235                  var key;
 236  
 237                  if ( hasStorage && blog_id ) {
 238                      key = 'wp-autosave-' + blog_id;
 239                      sessionStorage.setItem( key, JSON.stringify( stored_obj ) );
 240                      return sessionStorage.getItem( key ) !== null;
 241                  }
 242  
 243                  return false;
 244              }
 245  
 246              /**
 247               * Gets the saved post data for the current post.
 248               *
 249               * @since 3.9.0
 250               *
 251               * @returns {boolean|Object} False if no storage or no data or the postData as an Object.
 252               */
 253  			function getSavedPostData() {
 254                  var stored = getStorage();
 255  
 256                  if ( ! stored || ! post_id ) {
 257                      return false;
 258                  }
 259  
 260                  return stored[ 'post_' + post_id ] || false;
 261              }
 262  
 263              /**
 264               * Sets (save or delete) post data in the storage.
 265               *
 266               * If stored_data evaluates to 'false' the storage key for the current post will be removed.
 267               *
 268               * @since 3.9.0
 269               *
 270               * @param {Object|boolean|null} stored_data The post data to store or null/false/empty to delete the key.
 271               *
 272               * @returns {boolean} True if data is stored, false if data was removed.
 273               */
 274  			function setData( stored_data ) {
 275                  var stored = getStorage();
 276  
 277                  if ( ! stored || ! post_id ) {
 278                      return false;
 279                  }
 280  
 281                  if ( stored_data ) {
 282                      stored[ 'post_' + post_id ] = stored_data;
 283                  } else if ( stored.hasOwnProperty( 'post_' + post_id ) ) {
 284                      delete stored[ 'post_' + post_id ];
 285                  } else {
 286                      return false;
 287                  }
 288  
 289                  return setStorage( stored );
 290              }
 291  
 292              /**
 293               * Sets isSuspended to true.
 294               *
 295               * @since 3.9.0
 296               *
 297               * @returns {void}
 298               */
 299  			function suspend() {
 300                  isSuspended = true;
 301              }
 302  
 303              /**
 304               * Sets isSuspended to false.
 305               *
 306               * @since 3.9.0
 307               *
 308               * @returns {void}
 309               */
 310  			function resume() {
 311                  isSuspended = false;
 312              }
 313  
 314              /**
 315               * Saves post data for the current post.
 316               *
 317               * Runs on a 15 sec. interval, saves when there are differences in the post title or content.
 318               * When the optional data is provided, updates the last saved post data.
 319               *
 320               * @since 3.9.0
 321               *
 322               * @param {Object} data The post data for saving, minimum 'post_title' and 'content'.
 323               *
 324               * @returns {boolean} Returns true when data has been saved, otherwise it returns false.
 325               */
 326  			function save( data ) {
 327                  var postData, compareString,
 328                      result = false;
 329  
 330                  if ( isSuspended || ! hasStorage ) {
 331                      return false;
 332                  }
 333  
 334                  if ( data ) {
 335                      postData = getSavedPostData() || {};
 336                      $.extend( postData, data );
 337                  } else {
 338                      postData = getPostData('local');
 339                  }
 340  
 341                  compareString = getCompareString( postData );
 342  
 343                  if ( typeof lastCompareString === 'undefined' ) {
 344                      lastCompareString = initialCompareString;
 345                  }
 346  
 347                  // If the content, title and excerpt did not change since the last save, don't save again.
 348                  if ( compareString === lastCompareString ) {
 349                      return false;
 350                  }
 351  
 352                  postData.save_time = ( new Date() ).getTime();
 353                  postData.status = $( '#post_status' ).val() || '';
 354                  result = setData( postData );
 355  
 356                  if ( result ) {
 357                      lastCompareString = compareString;
 358                  }
 359  
 360                  return result;
 361              }
 362  
 363              /**
 364               * Initializes the auto save function.
 365               *
 366               * Checks whether the editor is active or not to use the editor events
 367               * to autosave, or uses the values from the elements to autosave.
 368               *
 369               * Runs on DOM ready.
 370               *
 371               * @since 3.9.0
 372               *
 373               * @returns {void}
 374               */
 375  			function run() {
 376                  post_id = $('#post_ID').val() || 0;
 377  
 378                  // Check if the local post data is different than the loaded post data.
 379                  if ( $( '#wp-content-wrap' ).hasClass( 'tmce-active' ) ) {
 380  
 381                      // If TinyMCE loads first, check the post 1.5 sec. after it is ready.
 382                      // By this time the content has been loaded in the editor and 'saved' to the textarea.
 383                      // This prevents false positives.
 384                      $document.on( 'tinymce-editor-init.autosave', function() {
 385                          window.setTimeout( function() {
 386                              checkPost();
 387                          }, 1500 );
 388                      });
 389                  } else {
 390                      checkPost();
 391                  }
 392  
 393                  // Save every 15 sec.
 394                  intervalTimer = window.setInterval( save, 15000 );
 395  
 396                  $( 'form#post' ).on( 'submit.autosave-local', function() {
 397                      var editor = getEditor(),
 398                          post_id = $('#post_ID').val() || 0;
 399  
 400                      if ( editor && ! editor.isHidden() ) {
 401  
 402                          // Last onSubmit event in the editor, needs to run after the content has been moved to the textarea.
 403                          editor.on( 'submit', function() {
 404                              save({
 405                                  post_title: $( '#title' ).val() || '',
 406                                  content: $( '#content' ).val() || '',
 407                                  excerpt: $( '#excerpt' ).val() || ''
 408                              });
 409                          });
 410                      } else {
 411                          save({
 412                              post_title: $( '#title' ).val() || '',
 413                              content: $( '#content' ).val() || '',
 414                              excerpt: $( '#excerpt' ).val() || ''
 415                          });
 416                      }
 417  
 418                      var secure = ( 'https:' === window.location.protocol );
 419                      wpCookies.set( 'wp-saving-post', post_id + '-check', 24 * 60 * 60, false, false, secure );
 420                  });
 421              }
 422  
 423              /**
 424               * Compares 2 strings. Removes whitespaces in the strings before comparing them.
 425               *
 426               * @since 3.9.0
 427               *
 428               * @param {string} str1 The first string.
 429               * @param {string} str2 The second string.
 430               * @returns {boolean} True if the strings are the same.
 431               */
 432  			function compare( str1, str2 ) {
 433  				function removeSpaces( string ) {
 434                      return string.toString().replace(/[\x20\t\r\n\f]+/g, '');
 435                  }
 436  
 437                  return ( removeSpaces( str1 || '' ) === removeSpaces( str2 || '' ) );
 438              }
 439  
 440              /**
 441               * Checks if the saved data for the current post (if any) is different than the
 442               * loaded post data on the screen.
 443               *
 444               * Shows a standard message letting the user restore the post data if different.
 445               *
 446               * @since 3.9.0
 447               *
 448               * @returns {void}
 449               */
 450  			function checkPost() {
 451                  var content, post_title, excerpt, $notice,
 452                      postData = getSavedPostData(),
 453                      cookie = wpCookies.get( 'wp-saving-post' ),
 454                      $newerAutosaveNotice = $( '#has-newer-autosave' ).parent( '.notice' ),
 455                      $headerEnd = $( '.wp-header-end' );
 456  
 457                  if ( cookie === post_id + '-saved' ) {
 458                      wpCookies.remove( 'wp-saving-post' );
 459                      // The post was saved properly, remove old data and bail
 460                      setData( false );
 461                      return;
 462                  }
 463  
 464                  if ( ! postData ) {
 465                      return;
 466                  }
 467  
 468                  content = $( '#content' ).val() || '';
 469                  post_title = $( '#title' ).val() || '';
 470                  excerpt = $( '#excerpt' ).val() || '';
 471  
 472                  if ( compare( content, postData.content ) && compare( post_title, postData.post_title ) &&
 473                      compare( excerpt, postData.excerpt ) ) {
 474  
 475                      return;
 476                  }
 477  
 478                  /*
 479                   * If '.wp-header-end' is found, append the notices after it otherwise
 480                   * after the first h1 or h2 heading found within the main content.
 481                   */
 482                  if ( ! $headerEnd.length ) {
 483                      $headerEnd = $( '.wrap h1, .wrap h2' ).first();
 484                  }
 485  
 486                  $notice = $( '#local-storage-notice' )
 487                      .insertAfter( $headerEnd )
 488                      .addClass( 'notice-warning' );
 489  
 490                  if ( $newerAutosaveNotice.length ) {
 491  
 492                      // If there is a "server" autosave notice, hide it.
 493                      // The data in the session storage is either the same or newer.
 494                      $newerAutosaveNotice.slideUp( 150, function() {
 495                          $notice.slideDown( 150 );
 496                      });
 497                  } else {
 498                      $notice.slideDown( 200 );
 499                  }
 500  
 501                  $notice.find( '.restore-backup' ).on( 'click.autosave-local', function() {
 502                      restorePost( postData );
 503                      $notice.fadeTo( 250, 0, function() {
 504                          $notice.slideUp( 150 );
 505                      });
 506                  });
 507              }
 508  
 509              /**
 510               * Restores the current title, content and excerpt from postData.
 511               *
 512               * @since 3.9.0
 513               *
 514               * @param {Object} postData The object containing all post data.
 515               *
 516               * @returns {boolean} True if the post is restored.
 517               */
 518  			function restorePost( postData ) {
 519                  var editor;
 520  
 521                  if ( postData ) {
 522                      // Set the last saved data
 523                      lastCompareString = getCompareString( postData );
 524  
 525                      if ( $( '#title' ).val() !== postData.post_title ) {
 526                          $( '#title' ).focus().val( postData.post_title || '' );
 527                      }
 528  
 529                      $( '#excerpt' ).val( postData.excerpt || '' );
 530                      editor = getEditor();
 531  
 532                      if ( editor && ! editor.isHidden() && typeof switchEditors !== 'undefined' ) {
 533                          if ( editor.settings.wpautop && postData.content ) {
 534                              postData.content = switchEditors.wpautop( postData.content );
 535                          }
 536  
 537                          // Make sure there's an undo level in the editor
 538                          editor.undoManager.transact( function() {
 539                              editor.setContent( postData.content || '' );
 540                              editor.nodeChanged();
 541                          });
 542                      } else {
 543  
 544                          // Make sure the Text editor is selected
 545                          $( '#content-html' ).click();
 546                          $( '#content' ).focus();
 547  
 548                          // Using document.execCommand() will let the user undo.
 549                          document.execCommand( 'selectAll' );
 550                          document.execCommand( 'insertText', false, postData.content || '' );
 551                      }
 552  
 553                      return true;
 554                  }
 555  
 556                  return false;
 557              }
 558  
 559              blog_id = typeof window.autosaveL10n !== 'undefined' && window.autosaveL10n.blog_id;
 560  
 561              // Check if the browser supports sessionStorage and it's not disabled,
 562              // then initialize and run checkPost().
 563              // Don't run if the post type supports neither 'editor' (textarea#content) nor 'excerpt'.
 564              if ( checkStorage() && blog_id && ( $('#content').length || $('#excerpt').length ) ) {
 565                  $document.ready( run );
 566              }
 567  
 568              return {
 569                  hasStorage: hasStorage,
 570                  getSavedPostData: getSavedPostData,
 571                  save: save,
 572                  suspend: suspend,
 573                  resume: resume
 574              };
 575          }
 576  
 577          /**
 578           * Auto saves the post on the server.
 579           *
 580           * @since 3.9.0
 581           *
 582           * @returns {Object} {
 583           *     {
 584           *         tempBlockSave: tempBlockSave,
 585           *         triggerSave: triggerSave,
 586           *         postChanged: postChanged,
 587           *         suspend: suspend,
 588           *         resume: resume
 589           *         }
 590           *     } The object all functions for autosave.
 591           */
 592  		function autosaveServer() {
 593              var _blockSave, _blockSaveTimer, previousCompareString, lastCompareString,
 594                  nextRun = 0,
 595                  isSuspended = false;
 596  
 597  
 598              /**
 599               * Blocks saving for the next 10 seconds.
 600               *
 601               * @since 3.9.0
 602               *
 603               * @returns {void}
 604               */
 605  			function tempBlockSave() {
 606                  _blockSave = true;
 607                  window.clearTimeout( _blockSaveTimer );
 608  
 609                  _blockSaveTimer = window.setTimeout( function() {
 610                      _blockSave = false;
 611                  }, 10000 );
 612              }
 613  
 614              /**
 615               * Sets isSuspended to true.
 616               *
 617               * @since 3.9.0
 618               *
 619               * @returns {void}
 620               */
 621  			function suspend() {
 622                  isSuspended = true;
 623              }
 624  
 625              /**
 626               * Sets isSuspended to false.
 627               *
 628               * @since 3.9.0
 629               *
 630               * @returns {void}
 631               */
 632  			function resume() {
 633                  isSuspended = false;
 634              }
 635  
 636              /**
 637               * Triggers the autosave with the post data.
 638               *
 639               * @since 3.9.0
 640               *
 641               * @param {Object} data The post data.
 642               *
 643               * @returns {void}
 644               */
 645  			function response( data ) {
 646                  _schedule();
 647                  _blockSave = false;
 648                  lastCompareString = previousCompareString;
 649                  previousCompareString = '';
 650  
 651                  $document.trigger( 'after-autosave', [data] );
 652                  enableButtons();
 653  
 654                  if ( data.success ) {
 655                      // No longer an auto-draft
 656                      $( '#auto_draft' ).val('');
 657                  }
 658              }
 659  
 660              /**
 661               * Saves immediately.
 662               *
 663               * Resets the timing and tells heartbeat to connect now.
 664               *
 665               * @since 3.9.0
 666               *
 667               * @returns {void}
 668               */
 669  			function triggerSave() {
 670                  nextRun = 0;
 671                  wp.heartbeat.connectNow();
 672              }
 673  
 674              /**
 675               * Checks if the post content in the textarea has changed since page load.
 676               *
 677               * This also happens when TinyMCE is active and editor.save() is triggered by
 678               * wp.autosave.getPostData().
 679               *
 680               * @since 3.9.0
 681               *
 682               * @return {boolean} True if the post has been changed.
 683               */
 684  			function postChanged() {
 685                  return getCompareString() !== initialCompareString;
 686              }
 687  
 688              /**
 689               * Checks if the post can be saved or not.
 690               *
 691               * If the post hasn't changed or it cannot be updated,
 692               * because the autosave is blocked or suspended, the function returns false.
 693               *
 694               * @since 3.9.0
 695               *
 696               * @returns {Object} Returns the post data.
 697               */
 698  			function save() {
 699                  var postData, compareString;
 700  
 701                  // window.autosave() used for back-compat
 702                  if ( isSuspended || _blockSave || ! window.autosave() ) {
 703                      return false;
 704                  }
 705  
 706                  if ( ( new Date() ).getTime() < nextRun ) {
 707                      return false;
 708                  }
 709  
 710                  postData = getPostData();
 711                  compareString = getCompareString( postData );
 712  
 713                  // First check
 714                  if ( typeof lastCompareString === 'undefined' ) {
 715                      lastCompareString = initialCompareString;
 716                  }
 717  
 718                  // No change
 719                  if ( compareString === lastCompareString ) {
 720                      return false;
 721                  }
 722  
 723                  previousCompareString = compareString;
 724                  tempBlockSave();
 725                  disableButtons();
 726  
 727                  $document.trigger( 'wpcountwords', [ postData.content ] )
 728                      .trigger( 'before-autosave', [ postData ] );
 729  
 730                  postData._wpnonce = $( '#_wpnonce' ).val() || '';
 731  
 732                  return postData;
 733              }
 734  
 735              /**
 736               * Sets the next run, based on the autosave interval.
 737               *
 738               * @private
 739               *
 740               * @since 3.9.0
 741               *
 742               * @returns {void}
 743               */
 744  			function _schedule() {
 745                  nextRun = ( new Date() ).getTime() + ( autosaveL10n.autosaveInterval * 1000 ) || 60000;
 746              }
 747  
 748              /**
 749               * Sets the autosaveData on the autosave heartbeat.
 750               *
 751               * @since 3.9.0
 752               *
 753               * @returns {void}
 754               */
 755              $document.on( 'heartbeat-send.autosave', function( event, data ) {
 756                  var autosaveData = save();
 757  
 758                  if ( autosaveData ) {
 759                      data.wp_autosave = autosaveData;
 760                  }
 761  
 762                  /**
 763                   * Triggers the autosave of the post with the autosave data on the autosave
 764                   * heartbeat.
 765                   *
 766                   * @since 3.9.0
 767                   *
 768                   * @returns {void}
 769                   */
 770              }).on( 'heartbeat-tick.autosave', function( event, data ) {
 771                  if ( data.wp_autosave ) {
 772                      response( data.wp_autosave );
 773                  }
 774                  /**
 775                   * Disables buttons and throws a notice when the connection is lost.
 776                   *
 777                   * @since 3.9.0
 778                   *
 779                   * @returns {void}
 780                   */
 781              }).on( 'heartbeat-connection-lost.autosave', function( event, error, status ) {
 782  
 783                  // When connection is lost, keep user from submitting changes.
 784                  if ( 'timeout' === error || 603 === status ) {
 785                      var $notice = $('#lost-connection-notice');
 786  
 787                      if ( ! wp.autosave.local.hasStorage ) {
 788                          $notice.find('.hide-if-no-sessionstorage').hide();
 789                      }
 790  
 791                      $notice.show();
 792                      disableButtons();
 793                  }
 794  
 795                  /**
 796                   * Enables buttons when the connection is restored.
 797                   *
 798                   * @since 3.9.0
 799                   *
 800                   * @returns {void}
 801                   */
 802              }).on( 'heartbeat-connection-restored.autosave', function() {
 803                  $('#lost-connection-notice').hide();
 804                  enableButtons();
 805              }).ready( function() {
 806                  _schedule();
 807              });
 808  
 809              return {
 810                  tempBlockSave: tempBlockSave,
 811                  triggerSave: triggerSave,
 812                  postChanged: postChanged,
 813                  suspend: suspend,
 814                  resume: resume
 815              };
 816          }
 817  
 818          /**
 819           * Sets the autosave time out.
 820           *
 821           * Wait for TinyMCE to initialize plus 1 second. for any external css to finish loading,
 822           * then save to the textarea before setting initialCompareString.
 823           * This avoids any insignificant differences between the initial textarea content and the content
 824           * extracted from the editor.
 825           *
 826           * @since 3.9.0
 827           *
 828           * @returns {void}
 829           */
 830          $document.on( 'tinymce-editor-init.autosave', function( event, editor ) {
 831              if ( editor.id === 'content' ) {
 832                  window.setTimeout( function() {
 833                      editor.save();
 834                      initialCompareString = getCompareString();
 835                  }, 1000 );
 836              }
 837          }).ready( function() {
 838  
 839              // Set the initial compare string in case TinyMCE is not used or not loaded first
 840              initialCompareString = getCompareString();
 841          });
 842  
 843          return {
 844              getPostData: getPostData,
 845              getCompareString: getCompareString,
 846              disableButtons: disableButtons,
 847              enableButtons: enableButtons,
 848              local: autosaveLocal(),
 849              server: autosaveServer()
 850          };
 851      }
 852  
 853      /** @namespace wp */
 854      window.wp = window.wp || {};
 855      window.wp.autosave = autosave();
 856  
 857  }( jQuery, window ));


Generated: Sat Nov 23 20:47:33 2019 Cross-referenced by PHPXref 0.7