Changeset 41265
- Timestamp:
- 08/18/2017 02:23:06 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/js/autosave.js
r38983 r41265 5 5 }; 6 6 7 /** 8 * @summary Adds autosave to the window object on dom ready. 9 * 10 * @since 3.9.0 11 * 12 * @param {jQuery} $ jQuery object. 13 * @param {window} The window object. 14 * 15 */ 7 16 ( function( $, window ) { 17 /** 18 * @summary Auto saves the post. 19 * 20 * @since 3.9.0 21 * 22 * @returns {Object} 23 * {{ 24 * getPostData: getPostData, 25 * getCompareString: getCompareString, 26 * disableButtons: disableButtons, 27 * enableButtons: enableButtons, 28 * local: ({hasStorage, getSavedPostData, save, suspend, resume}|*), 29 * server: ({tempBlockSave, triggerSave, postChanged, suspend, resume}|*)} 30 * } 31 * The object with all functions for autosave. 32 */ 8 33 function autosave() { 9 34 var initialCompareString, … … 12 37 13 38 /** 14 * Returns the data saved in both local and remote autosave 15 * 16 * @return object Object containing the post data 39 * @summary Returns the data saved in both local and remote autosave. 40 * 41 * @since 3.9.0 42 * 43 * @param {string} type The type of autosave either local or remote. 44 * 45 * @returns {Object} Object containing the post data. 17 46 */ 18 47 function getPostData( type ) { … … 22 51 editor = getEditor(); 23 52 24 // Don't run editor.save() more often than every 3 sec .53 // Don't run editor.save() more often than every 3 seconds. 25 54 // It is resource intensive and might slow down typing in long posts on slow devices. 26 55 if ( editor && editor.isDirty() && ! editor.isHidden() && time - 3000 > lastTriggerSave ) { … … 70 99 } 71 100 72 // Concatenate title, content and excerpt. Used to track changes when auto-saving. 101 /** 102 * @summary Concatenates the title, content and excerpt. 103 * 104 * This is used to track changes when auto-saving. 105 * 106 * @since 3.9.0 107 * 108 * @param {Object} postData The object containing the post data. 109 * 110 * @returns {string} A concatenated string with title, content and excerpt. 111 */ 73 112 function getCompareString( postData ) { 74 113 if ( typeof postData === 'object' ) { … … 79 118 } 80 119 120 /** 121 * @summary Disables save buttons. 122 * 123 * @since 3.9.0 124 * 125 * @returns {void} 126 */ 81 127 function disableButtons() { 82 128 $document.trigger('autosave-disable-buttons'); 129 83 130 // Re-enable 5 sec later. Just gives autosave a head start to avoid collisions. 84 131 setTimeout( enableButtons, 5000 ); 85 132 } 86 133 134 /** 135 * @summary Enables save buttons. 136 * 137 * @since 3.9.0 138 * 139 * @returns {void} 140 */ 87 141 function enableButtons() { 88 142 $document.trigger( 'autosave-enable-buttons' ); 89 143 } 90 144 145 /** 146 * @summary Gets the content editor. 147 * 148 * @since 4.6.0 149 * 150 * @returns {boolean|*} Returns either false if the editor is undefined, 151 * or the instance of the content editor. 152 */ 91 153 function getEditor() { 92 154 return typeof tinymce !== 'undefined' && tinymce.get('content'); 93 155 } 94 156 95 // Autosave in localStorage 157 /** 158 * @summary Autosave in localStorage. 159 * 160 * @since 3.9.0 161 * 162 * @returns { 163 * { 164 * hasStorage: *, 165 * getSavedPostData: getSavedPostData, 166 * save: save, 167 * suspend: suspend, 168 * resume: resume 169 * } 170 * } 171 * The object with all functions for local storage autosave. 172 */ 96 173 function autosaveLocal() { 97 174 var blog_id, post_id, hasStorage, intervalTimer, … … 99 176 isSuspended = false; 100 177 101 // Check if the browser supports sessionStorage and it's not disabled 178 /** 179 * @summary Checks if the browser supports sessionStorage and it's not disabled. 180 * 181 * @since 3.9.0 182 * 183 * @returns {boolean} True if the sessionStorage is supported and enabled. 184 */ 102 185 function checkStorage() { 103 186 var test = Math.random().toString(), … … 115 198 116 199 /** 117 * Initialize the local storage 118 * 119 * @return mixed False if no sessionStorage in the browser or an Object containing all postData for this blog 200 * @summary Initializes the local storage. 201 * 202 * @since 3.9.0 203 * 204 * @returns {boolean|Object} False if no sessionStorage in the browser or an Object 205 * containing all postData for this blog. 120 206 */ 121 207 function getStorage() { … … 136 222 137 223 /** 138 * Set the storage for this blog224 * @summary Sets the storage for this blog. 139 225 * 140 226 * Confirms that the data was saved successfully. 141 227 * 142 * @return bool 228 * @since 3.9.0 229 * 230 * @returns {boolean} True if the data was saved successfully, false if it wasn't saved. 143 231 */ 144 232 function setStorage( stored_obj ) { … … 155 243 156 244 /** 157 * Get the saved post data for the current post 158 * 159 * @return mixed False if no storage or no data or the postData as an Object 245 * @summary Gets the saved post data for the current post. 246 * 247 * @since 3.9.0 248 * 249 * @returns {boolean|Object} False if no storage or no data or the postData as an Object. 160 250 */ 161 251 function getSavedPostData() { … … 170 260 171 261 /** 172 * Set (save or delete) post data in the storage. 173 * 174 * If stored_data evaluates to 'false' the storage key for the current post will be removed 175 * 176 * $param stored_data The post data to store or null/false/empty to delete the key 177 * @return bool 262 * @summary Sets (save or delete) post data in the storage. 263 * 264 * If stored_data evaluates to 'false' the storage key for the current post will be removed. 265 * 266 * @since 3.9.0 267 * 268 * @param {Object|boolean|null} stored_data The post data to store or null/false/empty to delete the key. 269 * 270 * @returns {boolean} True if data is stored, false if data was removed. 178 271 */ 179 272 function setData( stored_data ) { … … 195 288 } 196 289 290 /** 291 * @summary Sets isSuspended to true. 292 * 293 * @since 3.9.0 294 * 295 * @returns {void} 296 */ 197 297 function suspend() { 198 298 isSuspended = true; 199 299 } 200 300 301 /** 302 * @summary Sets isSuspended to false. 303 * 304 * @since 3.9.0 305 * 306 * @returns {void} 307 */ 201 308 function resume() { 202 309 isSuspended = false; … … 204 311 205 312 /** 206 * Save post data for the current post313 * @summary Saves post data for the current post. 207 314 * 208 315 * Runs on a 15 sec. interval, saves when there are differences in the post title or content. 209 316 * When the optional data is provided, updates the last saved post data. 210 317 * 211 * $param data optional Object The post data for saving, minimum 'post_title' and 'content' 212 * @return bool 318 * @since 3.9.0 319 * 320 * @param {Object} data The post data for saving, minimum 'post_title' and 'content'. 321 * 322 * @returns {boolean} Returns true when data has been saved, otherwise it returns false. 213 323 */ 214 324 function save( data ) { … … 233 343 } 234 344 235 // If the content, title and excerpt did not change since the last save, don't save again 345 // If the content, title and excerpt did not change since the last save, don't save again. 236 346 if ( compareString === lastCompareString ) { 237 347 return false; … … 249 359 } 250 360 251 // Run on DOM ready 361 /** 362 * @summary Initializes the auto save function. 363 * 364 * Checks whether the editor is active or not to use the editor events 365 * to autosave, or uses the values from the elements to autosave. 366 * 367 * Runs on DOM ready. 368 * 369 * @since 3.9.0 370 * 371 * @returns {void} 372 */ 252 373 function run() { 253 374 post_id = $('#post_ID').val() || 0; … … 255 376 // Check if the local post data is different than the loaded post data. 256 377 if ( $( '#wp-content-wrap' ).hasClass( 'tmce-active' ) ) { 378 257 379 // If TinyMCE loads first, check the post 1.5 sec. after it is ready. 258 380 // By this time the content has been loaded in the editor and 'saved' to the textarea. … … 275 397 276 398 if ( editor && ! editor.isHidden() ) { 399 277 400 // Last onSubmit event in the editor, needs to run after the content has been moved to the textarea. 278 401 editor.on( 'submit', function() { … … 296 419 } 297 420 298 // Strip whitespace and compare two strings 421 /** 422 * @summary Compares 2 strings. 423 * 424 * 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 */ 299 432 function compare( str1, str2 ) { 300 433 function removeSpaces( string ) { … … 306 439 307 440 /** 308 * Check if the saved data for the current post (if any) is different than the loaded post data on the screen 441 * @summary Checks if the saved data for the current post (if any) is different 442 * than the loaded post data on the screen. 309 443 * 310 444 * Shows a standard message letting the user restore the post data if different. 311 445 * 312 * @return void 446 * @since 3.9.0 447 * 448 * @returns {void} 313 449 */ 314 450 function checkPost() { … … 353 489 354 490 if ( $newerAutosaveNotice.length ) { 491 355 492 // If there is a "server" autosave notice, hide it. 356 493 // The data in the session storage is either the same or newer. … … 370 507 } 371 508 372 // Restore the current title, content and excerpt from postData. 509 /** 510 * @summary 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 */ 373 518 function restorePost( postData ) { 374 519 var editor; … … 396 541 }); 397 542 } else { 543 398 544 // Make sure the Text editor is selected 399 545 $( '#content-html' ).click(); 400 546 $( '#content' ).focus(); 547 401 548 // Using document.execCommand() will let the user undo. 402 549 document.execCommand( 'selectAll' ); … … 428 575 } 429 576 430 // Autosave on the server 577 /** 578 * @summary 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 */ 431 592 function autosaveServer() { 432 593 var _blockSave, _blockSaveTimer, previousCompareString, lastCompareString, … … 434 595 isSuspended = false; 435 596 436 // Block saving for the next 10 sec. 597 598 /** 599 * @summary Blocks saving for the next 10 seconds. 600 * 601 * @since 3.9.0 602 * 603 * @returns {void} 604 */ 437 605 function tempBlockSave() { 438 606 _blockSave = true; … … 444 612 } 445 613 614 /** 615 * @summary Sets isSuspended to true. 616 * 617 * @since 3.9.0 618 * 619 * @returns {void} 620 */ 446 621 function suspend() { 447 622 isSuspended = true; 448 623 } 449 624 625 /** 626 * @summary Sets isSuspended to false. 627 * 628 * @since 3.9.0 629 * 630 * @returns {void} 631 */ 450 632 function resume() { 451 633 isSuspended = false; 452 634 } 453 635 454 // Runs on heartbeat-response 636 /** 637 * @summary 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 */ 455 645 function response( data ) { 456 646 _schedule(); … … 469 659 470 660 /** 471 * Save immediately 472 * 473 * Resets the timing and tells heartbeat to connect now 474 * 475 * @return void 661 * @summary Saves immediately. 662 * 663 * Resets the timing and tells heartbeat to connect now. 664 * 665 * @since 3.9.0 666 * 667 * @returns {void} 476 668 */ 477 669 function triggerSave() { … … 481 673 482 674 /** 483 * Checks if the post content in the textarea has changed since page load.675 * @summary Checks if the post content in the textarea has changed since page load. 484 676 * 485 677 * This also happens when TinyMCE is active and editor.save() is triggered by 486 678 * wp.autosave.getPostData(). 487 679 * 488 * @return bool 680 * @since 3.9.0 681 * 682 * @return {boolean} True if the post has been changed. 489 683 */ 490 684 function postChanged() { … … 492 686 } 493 687 494 // Runs on 'heartbeat-send' 688 /** 689 * @summary 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 */ 495 698 function save() { 496 699 var postData, compareString; … … 530 733 } 531 734 735 /** 736 * @summary Sets the next run, based on the autosave interval. 737 * 738 * @private 739 * 740 * @since 3.9.0 741 * 742 * @returns {void} 743 */ 532 744 function _schedule() { 533 745 nextRun = ( new Date() ).getTime() + ( autosaveL10n.autosaveInterval * 1000 ) || 60000; 534 746 } 535 747 748 /** 749 * @summary Sets the autosaveData on the autosave heartbeat. 750 * 751 * @since 3.9.0 752 * 753 * @returns {void} 754 */ 536 755 $document.on( 'heartbeat-send.autosave', function( event, data ) { 537 756 var autosaveData = save(); … … 540 759 data.wp_autosave = autosaveData; 541 760 } 761 762 /** 763 * @summary Triggers the autosave of the post with the autosave data 764 * on the autosave heartbeat. 765 * 766 * @since 3.9.0 767 * 768 * @returns {void} 769 */ 542 770 }).on( 'heartbeat-tick.autosave', function( event, data ) { 543 771 if ( data.wp_autosave ) { 544 772 response( data.wp_autosave ); 545 773 } 774 /** 775 * @summary Disables buttons and throws a notice when the connection is lost. 776 * 777 * @since 3.9.0 778 * 779 * @returns {void} 780 */ 546 781 }).on( 'heartbeat-connection-lost.autosave', function( event, error, status ) { 782 547 783 // When connection is lost, keep user from submitting changes. 548 784 if ( 'timeout' === error || 603 === status ) { … … 556 792 disableButtons(); 557 793 } 794 795 /** 796 * @summary Enables buttons when the connection is restored. 797 * 798 * @since 3.9.0 799 * 800 * @returns {void} 801 */ 558 802 }).on( 'heartbeat-connection-restored.autosave', function() { 559 803 $('#lost-connection-notice').hide(); … … 572 816 } 573 817 574 // Wait for TinyMCE to initialize plus 1 sec. for any external css to finish loading, 575 // then 'save' to the textarea before setting initialCompareString. 576 // This avoids any insignificant differences between the initial textarea content and the content 577 // extracted from the editor. 818 /** 819 * @summary 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 */ 578 830 $document.on( 'tinymce-editor-init.autosave', function( event, editor ) { 579 831 if ( editor.id === 'content' ) { … … 584 836 } 585 837 }).ready( function() { 838 586 839 // Set the initial compare string in case TinyMCE is not used or not loaded first 587 840 initialCompareString = getCompareString();
Note: See TracChangeset
for help on using the changeset viewer.