Editor: Fix improper triggering of the "Are you sure" prompt when navigating away from the old, "classic" Edit Post screen and there are no changes. Was triggered when there is an instance of TinyMCE in the Excerpt postbox.
Props rodrigosprimo, jonathanstegall, kevin940726, azaozz. Fixes #52038. Built from https://develop.svn.wordpress.org/trunk@49807 git-svn-id: http://core.svn.wordpress.org/trunk@49530 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
2ebc78cbf4
commit
71ae342050
|
@ -488,12 +488,20 @@ jQuery(document).ready( function($) {
|
||||||
* When the user is trying to load another page, or reloads current page
|
* When the user is trying to load another page, or reloads current page
|
||||||
* show a confirmation dialog when there are unsaved changes.
|
* show a confirmation dialog when there are unsaved changes.
|
||||||
*/
|
*/
|
||||||
$(window).on( 'beforeunload.edit-post', function() {
|
$( window ).on( 'beforeunload.edit-post', function( event ) {
|
||||||
var editor = typeof tinymce !== 'undefined' && tinymce.get('content');
|
var editor = window.tinymce && window.tinymce.get( 'content' );
|
||||||
|
var changed = false;
|
||||||
|
|
||||||
if ( ( editor && ! editor.isHidden() && editor.isDirty() ) ||
|
if ( wp.autosave ) {
|
||||||
( wp.autosave && wp.autosave.server.postChanged() ) ) {
|
changed = wp.autosave.server.postChanged();
|
||||||
|
} else if ( editor ) {
|
||||||
|
changed = ( ! editor.isHidden() && editor.isDirty() );
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( changed ) {
|
||||||
|
event.preventDefault();
|
||||||
|
// The return string is needed for browser compat.
|
||||||
|
// See https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event.
|
||||||
return __( 'The changes you made will be lost if you navigate away from this page.' );
|
return __( 'The changes you made will be lost if you navigate away from this page.' );
|
||||||
}
|
}
|
||||||
}).on( 'unload.edit-post', function( event ) {
|
}).on( 'unload.edit-post', function( event ) {
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -36,8 +36,24 @@ window.autosave = function() {
|
||||||
*/
|
*/
|
||||||
function autosave() {
|
function autosave() {
|
||||||
var initialCompareString,
|
var initialCompareString,
|
||||||
lastTriggerSave = 0,
|
initialCompareData = {},
|
||||||
$document = $(document);
|
lastTriggerSave = 0,
|
||||||
|
$document = $( document );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the initial compare data.
|
||||||
|
*
|
||||||
|
* @since 5.6.1
|
||||||
|
*/
|
||||||
|
function setInitialCompare() {
|
||||||
|
initialCompareData = {
|
||||||
|
post_title: $( '#title' ).val() || '',
|
||||||
|
content: $( '#content' ).val() || '',
|
||||||
|
excerpt: $( '#excerpt' ).val() || ''
|
||||||
|
};
|
||||||
|
|
||||||
|
initialCompareString = getCompareString( initialCompareData );
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the data saved in both local and remote autosave.
|
* Returns the data saved in both local and remote autosave.
|
||||||
|
@ -686,6 +702,32 @@ window.autosave = function() {
|
||||||
* @return {boolean} True if the post has been changed.
|
* @return {boolean} True if the post has been changed.
|
||||||
*/
|
*/
|
||||||
function postChanged() {
|
function postChanged() {
|
||||||
|
var changed = false;
|
||||||
|
|
||||||
|
// If there are TinyMCE instances, loop through them.
|
||||||
|
if ( window.tinymce ) {
|
||||||
|
window.tinymce.each( [ 'content', 'excerpt' ], function( field ) {
|
||||||
|
var editor = window.tinymce.get( field );
|
||||||
|
|
||||||
|
if ( ! editor || editor.isHidden() ) {
|
||||||
|
if ( $( '#' + field ).val() !== initialCompareData[ field ] ) {
|
||||||
|
changed = true;
|
||||||
|
// Break.
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else if ( editor.isDirty() ) {
|
||||||
|
changed = true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} );
|
||||||
|
|
||||||
|
if ( $( '#title' ).val() !== initialCompareData.post_title ) {
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return changed;
|
||||||
|
}
|
||||||
|
|
||||||
return getCompareString() !== initialCompareString;
|
return getCompareString() !== initialCompareString;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -832,16 +874,16 @@ window.autosave = function() {
|
||||||
* @return {void}
|
* @return {void}
|
||||||
*/
|
*/
|
||||||
$document.on( 'tinymce-editor-init.autosave', function( event, editor ) {
|
$document.on( 'tinymce-editor-init.autosave', function( event, editor ) {
|
||||||
if ( editor.id === 'content' ) {
|
// Reset the initialCompare data after the TinyMCE instances have been initialized.
|
||||||
|
if ( 'content' === editor.id || 'excerpt' === editor.id ) {
|
||||||
window.setTimeout( function() {
|
window.setTimeout( function() {
|
||||||
editor.save();
|
editor.save();
|
||||||
initialCompareString = getCompareString();
|
setInitialCompare();
|
||||||
}, 1000 );
|
}, 1000 );
|
||||||
}
|
}
|
||||||
}).ready( function() {
|
}).ready( function() {
|
||||||
|
|
||||||
// Set the initial compare string in case TinyMCE is not used or not loaded first.
|
// Set the initial compare string in case TinyMCE is not used or not loaded first.
|
||||||
initialCompareString = getCompareString();
|
setInitialCompare();
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -13,7 +13,7 @@
|
||||||
*
|
*
|
||||||
* @global string $wp_version
|
* @global string $wp_version
|
||||||
*/
|
*/
|
||||||
$wp_version = '5.7-alpha-49805';
|
$wp_version = '5.7-alpha-49807';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||||
|
|
Loading…
Reference in New Issue