Ticket #52038: 52038.diff
File 52038.diff, 3.6 KB (added by , 4 years ago) |
---|
-
src/js/_enqueues/admin/post.js
488 488 * When the user is trying to load another page, or reloads current page 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'); 491 $( window ).on( 'beforeunload.edit-post', function( event ) { 492 var editor = window.tinymce && window.tinymce.get( 'content' ); 493 var changed = false; 493 494 494 if ( ( editor && ! editor.isHidden() && editor.isDirty() ) || 495 ( wp.autosave && wp.autosave.server.postChanged() ) ) { 495 if ( wp.autosave ) { 496 changed = wp.autosave.server.postChanged(); 497 } else if ( editor ) { 498 changed = ( ! editor.isHidden() && editor.isDirty() ); 499 } 496 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 } 499 507 }).on( 'unload.edit-post', function( event ) { -
src/js/_enqueues/wp/autosave.js
36 36 */ 37 37 function autosave() { 38 38 var initialCompareString, 39 lastTriggerSave = 0, 40 $document = $(document); 39 initialCompareData = {}, 40 lastTriggerSave = 0, 41 $document = $( document ); 41 42 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 } 57 58 /** 43 59 * Returns the data saved in both local and remote autosave. 44 60 * 45 61 * @since 3.9.0 … … 686 702 * @return {boolean} True if the post has been changed. 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 719 return; 720 } 721 722 if ( editor.isDirty() ) { 723 changed = true; 724 return false; 725 } 726 } ); 727 728 if ( $( '#title' ).val() !== initialCompareData.post_title ) { 729 changed = true; 730 } 731 732 return changed; 733 } 734 689 735 return getCompareString() !== initialCompareString; 690 736 } 691 737 … … 832 878 * @return {void} 833 879 */ 834 880 $document.on( 'tinymce-editor-init.autosave', function( event, editor ) { 835 if ( editor.id === 'content' ) { 881 // Reset the initialCompare data after the TinyMCE instances have been initialized. 882 if ( 'content' === editor.id || 'excerpt' === editor.id ) { 836 883 window.setTimeout( function() { 837 884 editor.save(); 838 initialCompareString = getCompareString();885 setInitialCompare(); 839 886 }, 1000 ); 840 887 } 841 888 }).ready( function() { 842 843 889 // Set the initial compare string in case TinyMCE is not used or not loaded first. 844 initialCompareString = getCompareString();890 setInitialCompare(); 845 891 }); 846 892 847 893 return {