Make WordPress Core

Changeset 49807 for trunk


Ignore:
Timestamp:
12/16/2020 03:23:35 PM (6 years ago)
Author:
azaozz
Message:

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.

Location:
trunk/src/js/_enqueues
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/js/_enqueues/admin/post.js

    r48650 r49807  
    489489         * show a confirmation dialog when there are unsaved changes.
    490490         */
    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.
    497505                        return __( 'The changes you made will be lost if you navigate away from this page.' );
    498506                }
  • trunk/src/js/_enqueues/wp/autosave.js

    r49065 r49807  
    3737        function autosave() {
    3838                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                }
    4157
    4258                /**
    … …  
    687703                         */
    688704                        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
    689731                                return getCompareString() !== initialCompareString;
    690732                        }
    … …  
    833875                 */
    834876                $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 ) {
    836879                                window.setTimeout( function() {
    837880                                        editor.save();
    838                                         initialCompareString = getCompareString();
     881                                        setInitialCompare();
    839882                                }, 1000 );
    840883                        }
    841884                }).ready( function() {
    842 
    843885                        // Set the initial compare string in case TinyMCE is not used or not loaded first.
    844                         initialCompareString = getCompareString();
     886                        setInitialCompare();
    845887                });
    846888
Note: See TracChangeset for help on using the changeset viewer.