[ Index ]

PHP Cross Reference of WordPress

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       * @return {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           * @return {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           * @return {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           * @return {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           * @return {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           * @return {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           * @return {
 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               * @return {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               * @return {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               * @return {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               * @return {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               * @return {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               * @return {void}
 298               */
 299  			function suspend() {
 300                  isSuspended = true;
 301              }
 302  
 303              /**
 304               * Sets isSuspended to false.
 305               *
 306               * @since 3.9.0
 307               *
 308               * @return {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 seconds 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               * @return {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               * @return {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                      /*
 382                       * If TinyMCE loads first, check the post 1.5 seconds after it is ready.
 383                       * By this time the content has been loaded in the editor and 'saved' to the textarea.
 384                       * This prevents false positives.
 385                       */
 386                      $document.on( 'tinymce-editor-init.autosave', function() {
 387                          window.setTimeout( function() {
 388                              checkPost();
 389                          }, 1500 );
 390                      });
 391                  } else {
 392                      checkPost();
 393                  }
 394  
 395                  // Save every 15 seconds.
 396                  intervalTimer = window.setInterval( save, 15000 );
 397  
 398                  $( 'form#post' ).on( 'submit.autosave-local', function() {
 399                      var editor = getEditor(),
 400                          post_id = $('#post_ID').val() || 0;
 401  
 402                      if ( editor && ! editor.isHidden() ) {
 403  
 404                          // Last onSubmit event in the editor, needs to run after the content has been moved to the textarea.
 405                          editor.on( 'submit', function() {
 406                              save({
 407                                  post_title: $( '#title' ).val() || '',
 408                                  content: $( '#content' ).val() || '',
 409                                  excerpt: $( '#excerpt' ).val() || ''
 410                              });
 411                          });
 412                      } else {
 413                          save({
 414                              post_title: $( '#title' ).val() || '',
 415                              content: $( '#content' ).val() || '',
 416                              excerpt: $( '#excerpt' ).val() || ''
 417                          });
 418                      }
 419  
 420                      var secure = ( 'https:' === window.location.protocol );
 421                      wpCookies.set( 'wp-saving-post', post_id + '-check', 24 * 60 * 60, false, false, secure );
 422                  });
 423              }
 424  
 425              /**
 426               * Compares 2 strings. Removes whitespaces in the strings before comparing them.
 427               *
 428               * @since 3.9.0
 429               *
 430               * @param {string} str1 The first string.
 431               * @param {string} str2 The second string.
 432               * @return {boolean} True if the strings are the same.
 433               */
 434  			function compare( str1, str2 ) {
 435  				function removeSpaces( string ) {
 436                      return string.toString().replace(/[\x20\t\r\n\f]+/g, '');
 437                  }
 438  
 439                  return ( removeSpaces( str1 || '' ) === removeSpaces( str2 || '' ) );
 440              }
 441  
 442              /**
 443               * Checks if the saved data for the current post (if any) is different than the
 444               * loaded post data on the screen.
 445               *
 446               * Shows a standard message letting the user restore the post data if different.
 447               *
 448               * @since 3.9.0
 449               *
 450               * @return {void}
 451               */
 452  			function checkPost() {
 453                  var content, post_title, excerpt, $notice,
 454                      postData = getSavedPostData(),
 455                      cookie = wpCookies.get( 'wp-saving-post' ),
 456                      $newerAutosaveNotice = $( '#has-newer-autosave' ).parent( '.notice' ),
 457                      $headerEnd = $( '.wp-header-end' );
 458  
 459                  if ( cookie === post_id + '-saved' ) {
 460                      wpCookies.remove( 'wp-saving-post' );
 461                      // The post was saved properly, remove old data and bail.
 462                      setData( false );
 463                      return;
 464                  }
 465  
 466                  if ( ! postData ) {
 467                      return;
 468                  }
 469  
 470                  content = $( '#content' ).val() || '';
 471                  post_title = $( '#title' ).val() || '';
 472                  excerpt = $( '#excerpt' ).val() || '';
 473  
 474                  if ( compare( content, postData.content ) && compare( post_title, postData.post_title ) &&
 475                      compare( excerpt, postData.excerpt ) ) {
 476  
 477                      return;
 478                  }
 479  
 480                  /*
 481                   * If '.wp-header-end' is found, append the notices after it otherwise
 482                   * after the first h1 or h2 heading found within the main content.
 483                   */
 484                  if ( ! $headerEnd.length ) {
 485                      $headerEnd = $( '.wrap h1, .wrap h2' ).first();
 486                  }
 487  
 488                  $notice = $( '#local-storage-notice' )
 489                      .insertAfter( $headerEnd )
 490                      .addClass( 'notice-warning' );
 491  
 492                  if ( $newerAutosaveNotice.length ) {
 493  
 494                      // If there is a "server" autosave notice, hide it.
 495                      // The data in the session storage is either the same or newer.
 496                      $newerAutosaveNotice.slideUp( 150, function() {
 497                          $notice.slideDown( 150 );
 498                      });
 499                  } else {
 500                      $notice.slideDown( 200 );
 501                  }
 502  
 503                  $notice.find( '.restore-backup' ).on( 'click.autosave-local', function() {
 504                      restorePost( postData );
 505                      $notice.fadeTo( 250, 0, function() {
 506                          $notice.slideUp( 150 );
 507                      });
 508                  });
 509              }
 510  
 511              /**
 512               * Restores the current title, content and excerpt from postData.
 513               *
 514               * @since 3.9.0
 515               *
 516               * @param {Object} postData The object containing all post data.
 517               *
 518               * @return {boolean} True if the post is restored.
 519               */
 520  			function restorePost( postData ) {
 521                  var editor;
 522  
 523                  if ( postData ) {
 524                      // Set the last saved data.
 525                      lastCompareString = getCompareString( postData );
 526  
 527                      if ( $( '#title' ).val() !== postData.post_title ) {
 528                          $( '#title' ).focus().val( postData.post_title || '' );
 529                      }
 530  
 531                      $( '#excerpt' ).val( postData.excerpt || '' );
 532                      editor = getEditor();
 533  
 534                      if ( editor && ! editor.isHidden() && typeof switchEditors !== 'undefined' ) {
 535                          if ( editor.settings.wpautop && postData.content ) {
 536                              postData.content = switchEditors.wpautop( postData.content );
 537                          }
 538  
 539                          // Make sure there's an undo level in the editor.
 540                          editor.undoManager.transact( function() {
 541                              editor.setContent( postData.content || '' );
 542                              editor.nodeChanged();
 543                          });
 544                      } else {
 545  
 546                          // Make sure the Text editor is selected.
 547                          $( '#content-html' ).click();
 548                          $( '#content' ).focus();
 549  
 550                          // Using document.execCommand() will let the user undo.
 551                          document.execCommand( 'selectAll' );
 552                          document.execCommand( 'insertText', false, postData.content || '' );
 553                      }
 554  
 555                      return true;
 556                  }
 557  
 558                  return false;
 559              }
 560  
 561              blog_id = typeof window.autosaveL10n !== 'undefined' && window.autosaveL10n.blog_id;
 562  
 563              /*
 564               * Check if the browser supports sessionStorage and it's not disabled,
 565               * then initialize and run checkPost().
 566               * Don't run if the post type supports neither 'editor' (textarea#content) nor 'excerpt'.
 567               */
 568              if ( checkStorage() && blog_id && ( $('#content').length || $('#excerpt').length ) ) {
 569                  $document.ready( run );
 570              }
 571  
 572              return {
 573                  hasStorage: hasStorage,
 574                  getSavedPostData: getSavedPostData,
 575                  save: save,
 576                  suspend: suspend,
 577                  resume: resume
 578              };
 579          }
 580  
 581          /**
 582           * Auto saves the post on the server.
 583           *
 584           * @since 3.9.0
 585           *
 586           * @return {Object} {
 587           *     {
 588           *         tempBlockSave: tempBlockSave,
 589           *         triggerSave: triggerSave,
 590           *         postChanged: postChanged,
 591           *         suspend: suspend,
 592           *         resume: resume
 593           *         }
 594           *     } The object all functions for autosave.
 595           */
 596  		function autosaveServer() {
 597              var _blockSave, _blockSaveTimer, previousCompareString, lastCompareString,
 598                  nextRun = 0,
 599                  isSuspended = false;
 600  
 601  
 602              /**
 603               * Blocks saving for the next 10 seconds.
 604               *
 605               * @since 3.9.0
 606               *
 607               * @return {void}
 608               */
 609  			function tempBlockSave() {
 610                  _blockSave = true;
 611                  window.clearTimeout( _blockSaveTimer );
 612  
 613                  _blockSaveTimer = window.setTimeout( function() {
 614                      _blockSave = false;
 615                  }, 10000 );
 616              }
 617  
 618              /**
 619               * Sets isSuspended to true.
 620               *
 621               * @since 3.9.0
 622               *
 623               * @return {void}
 624               */
 625  			function suspend() {
 626                  isSuspended = true;
 627              }
 628  
 629              /**
 630               * Sets isSuspended to false.
 631               *
 632               * @since 3.9.0
 633               *
 634               * @return {void}
 635               */
 636  			function resume() {
 637                  isSuspended = false;
 638              }
 639  
 640              /**
 641               * Triggers the autosave with the post data.
 642               *
 643               * @since 3.9.0
 644               *
 645               * @param {Object} data The post data.
 646               *
 647               * @return {void}
 648               */
 649  			function response( data ) {
 650                  _schedule();
 651                  _blockSave = false;
 652                  lastCompareString = previousCompareString;
 653                  previousCompareString = '';
 654  
 655                  $document.trigger( 'after-autosave', [data] );
 656                  enableButtons();
 657  
 658                  if ( data.success ) {
 659                      // No longer an auto-draft.
 660                      $( '#auto_draft' ).val('');
 661                  }
 662              }
 663  
 664              /**
 665               * Saves immediately.
 666               *
 667               * Resets the timing and tells heartbeat to connect now.
 668               *
 669               * @since 3.9.0
 670               *
 671               * @return {void}
 672               */
 673  			function triggerSave() {
 674                  nextRun = 0;
 675                  wp.heartbeat.connectNow();
 676              }
 677  
 678              /**
 679               * Checks if the post content in the textarea has changed since page load.
 680               *
 681               * This also happens when TinyMCE is active and editor.save() is triggered by
 682               * wp.autosave.getPostData().
 683               *
 684               * @since 3.9.0
 685               *
 686               * @return {boolean} True if the post has been changed.
 687               */
 688  			function postChanged() {
 689                  return getCompareString() !== initialCompareString;
 690              }
 691  
 692              /**
 693               * Checks if the post can be saved or not.
 694               *
 695               * If the post hasn't changed or it cannot be updated,
 696               * because the autosave is blocked or suspended, the function returns false.
 697               *
 698               * @since 3.9.0
 699               *
 700               * @return {Object} Returns the post data.
 701               */
 702  			function save() {
 703                  var postData, compareString;
 704  
 705                  // window.autosave() used for back-compat.
 706                  if ( isSuspended || _blockSave || ! window.autosave() ) {
 707                      return false;
 708                  }
 709  
 710                  if ( ( new Date() ).getTime() < nextRun ) {
 711                      return false;
 712                  }
 713  
 714                  postData = getPostData();
 715                  compareString = getCompareString( postData );
 716  
 717                  // First check.
 718                  if ( typeof lastCompareString === 'undefined' ) {
 719                      lastCompareString = initialCompareString;
 720                  }
 721  
 722                  // No change.
 723                  if ( compareString === lastCompareString ) {
 724                      return false;
 725                  }
 726  
 727                  previousCompareString = compareString;
 728                  tempBlockSave();
 729                  disableButtons();
 730  
 731                  $document.trigger( 'wpcountwords', [ postData.content ] )
 732                      .trigger( 'before-autosave', [ postData ] );
 733  
 734                  postData._wpnonce = $( '#_wpnonce' ).val() || '';
 735  
 736                  return postData;
 737              }
 738  
 739              /**
 740               * Sets the next run, based on the autosave interval.
 741               *
 742               * @private
 743               *
 744               * @since 3.9.0
 745               *
 746               * @return {void}
 747               */
 748  			function _schedule() {
 749                  nextRun = ( new Date() ).getTime() + ( autosaveL10n.autosaveInterval * 1000 ) || 60000;
 750              }
 751  
 752              /**
 753               * Sets the autosaveData on the autosave heartbeat.
 754               *
 755               * @since 3.9.0
 756               *
 757               * @return {void}
 758               */
 759              $document.on( 'heartbeat-send.autosave', function( event, data ) {
 760                  var autosaveData = save();
 761  
 762                  if ( autosaveData ) {
 763                      data.wp_autosave = autosaveData;
 764                  }
 765  
 766                  /**
 767                   * Triggers the autosave of the post with the autosave data on the autosave
 768                   * heartbeat.
 769                   *
 770                   * @since 3.9.0
 771                   *
 772                   * @return {void}
 773                   */
 774              }).on( 'heartbeat-tick.autosave', function( event, data ) {
 775                  if ( data.wp_autosave ) {
 776                      response( data.wp_autosave );
 777                  }
 778                  /**
 779                   * Disables buttons and throws a notice when the connection is lost.
 780                   *
 781                   * @since 3.9.0
 782                   *
 783                   * @return {void}
 784                   */
 785              }).on( 'heartbeat-connection-lost.autosave', function( event, error, status ) {
 786  
 787                  // When connection is lost, keep user from submitting changes.
 788                  if ( 'timeout' === error || 603 === status ) {
 789                      var $notice = $('#lost-connection-notice');
 790  
 791                      if ( ! wp.autosave.local.hasStorage ) {
 792                          $notice.find('.hide-if-no-sessionstorage').hide();
 793                      }
 794  
 795                      $notice.show();
 796                      disableButtons();
 797                  }
 798  
 799                  /**
 800                   * Enables buttons when the connection is restored.
 801                   *
 802                   * @since 3.9.0
 803                   *
 804                   * @return {void}
 805                   */
 806              }).on( 'heartbeat-connection-restored.autosave', function() {
 807                  $('#lost-connection-notice').hide();
 808                  enableButtons();
 809              }).ready( function() {
 810                  _schedule();
 811              });
 812  
 813              return {
 814                  tempBlockSave: tempBlockSave,
 815                  triggerSave: triggerSave,
 816                  postChanged: postChanged,
 817                  suspend: suspend,
 818                  resume: resume
 819              };
 820          }
 821  
 822          /**
 823           * Sets the autosave time out.
 824           *
 825           * Wait for TinyMCE to initialize plus 1 second. for any external css to finish loading,
 826           * then save to the textarea before setting initialCompareString.
 827           * This avoids any insignificant differences between the initial textarea content and the content
 828           * extracted from the editor.
 829           *
 830           * @since 3.9.0
 831           *
 832           * @return {void}
 833           */
 834          $document.on( 'tinymce-editor-init.autosave', function( event, editor ) {
 835              if ( editor.id === 'content' ) {
 836                  window.setTimeout( function() {
 837                      editor.save();
 838                      initialCompareString = getCompareString();
 839                  }, 1000 );
 840              }
 841          }).ready( function() {
 842  
 843              // Set the initial compare string in case TinyMCE is not used or not loaded first.
 844              initialCompareString = getCompareString();
 845          });
 846  
 847          return {
 848              getPostData: getPostData,
 849              getCompareString: getCompareString,
 850              disableButtons: disableButtons,
 851              enableButtons: enableButtons,
 852              local: autosaveLocal(),
 853              server: autosaveServer()
 854          };
 855      }
 856  
 857      /** @namespace wp */
 858      window.wp = window.wp || {};
 859      window.wp.autosave = autosave();
 860  
 861  }( jQuery, window ));


Generated: Thu Oct 1 01:00:03 2020 Cross-referenced by PHPXref 0.7.1