Changeset 49807
- Timestamp:
- 12/16/2020 03:23:35 PM (4 years ago)
- Location:
- trunk/src/js/_enqueues
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/js/_enqueues/admin/post.js
r48650 r49807 489 489 * show a confirmation dialog when there are unsaved changes. 490 490 */ 491 $(window).on( 'beforeunload.edit-post', function() { 492 var editor = typeof tinymce !== 'undefined' && tinymce.get('content'); 493 494 if ( ( editor && ! editor.isHidden() && editor.isDirty() ) || 495 ( wp.autosave && wp.autosave.server.postChanged() ) ) { 496 491 $( window ).on( 'beforeunload.edit-post', function( event ) { 492 var editor = window.tinymce && window.tinymce.get( 'content' ); 493 var changed = false; 494 495 if ( wp.autosave ) { 496 changed = wp.autosave.server.postChanged(); 497 } else if ( editor ) { 498 changed = ( ! editor.isHidden() && editor.isDirty() ); 499 } 500 501 if ( changed ) { 502 event.preventDefault(); 503 // The return string is needed for browser compat. 504 // See https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event. 497 505 return __( 'The changes you made will be lost if you navigate away from this page.' ); 498 506 } -
trunk/src/js/_enqueues/wp/autosave.js
r49065 r49807 37 37 function autosave() { 38 38 var initialCompareString, 39 lastTriggerSave = 0, 40 $document = $(document); 39 initialCompareData = {}, 40 lastTriggerSave = 0, 41 $document = $( document ); 42 43 /** 44 * Sets the initial compare data. 45 * 46 * @since 5.6.1 47 */ 48 function setInitialCompare() { 49 initialCompareData = { 50 post_title: $( '#title' ).val() || '', 51 content: $( '#content' ).val() || '', 52 excerpt: $( '#excerpt' ).val() || '' 53 }; 54 55 initialCompareString = getCompareString( initialCompareData ); 56 } 41 57 42 58 /** … … 687 703 */ 688 704 function postChanged() { 705 var changed = false; 706 707 // If there are TinyMCE instances, loop through them. 708 if ( window.tinymce ) { 709 window.tinymce.each( [ 'content', 'excerpt' ], function( field ) { 710 var editor = window.tinymce.get( field ); 711 712 if ( ! editor || editor.isHidden() ) { 713 if ( $( '#' + field ).val() !== initialCompareData[ field ] ) { 714 changed = true; 715 // Break. 716 return false; 717 } 718 } else if ( editor.isDirty() ) { 719 changed = true; 720 return false; 721 } 722 } ); 723 724 if ( $( '#title' ).val() !== initialCompareData.post_title ) { 725 changed = true; 726 } 727 728 return changed; 729 } 730 689 731 return getCompareString() !== initialCompareString; 690 732 } … … 833 875 */ 834 876 $document.on( 'tinymce-editor-init.autosave', function( event, editor ) { 835 if ( editor.id === 'content' ) { 877 // Reset the initialCompare data after the TinyMCE instances have been initialized. 878 if ( 'content' === editor.id || 'excerpt' === editor.id ) { 836 879 window.setTimeout( function() { 837 880 editor.save(); 838 initialCompareString = getCompareString();881 setInitialCompare(); 839 882 }, 1000 ); 840 883 } 841 884 }).ready( function() { 842 843 885 // Set the initial compare string in case TinyMCE is not used or not loaded first. 844 initialCompareString = getCompareString();886 setInitialCompare(); 845 887 }); 846 888
Note: See TracChangeset
for help on using the changeset viewer.