Make WordPress Core

Changeset 56809


Ignore:
Timestamp:
10/09/2023 09:29:42 PM (16 months ago)
Author:
westonruter
Message:

Administration: Remove deprecated unload event handlers and use pagehide (and pageshow) when appropriate.

Use pagehide event instead of unload in the following cases:

  • For classic editor to release the post lock.
  • In Text widget to rebuild editor after dragging widget to new location in classic widgets interface.
  • To clear out the window.name when navigating away from a post preview.
  • To suspend heartbeat, while also using pageshow event to resume as if it had been a focused tab in case page restored from bfcache.

Also:

  • Remove obsolete mobile cleanup code in js/_enqueues/lib/gallery.js (introduced in [9894]). Do same for src/js/_enqueues/wp/media/models.js (introduced in [22872]). See #22552.
  • Remove obsolete Firefox-specific workaround in js/_enqueues/wp/mce-view.js from [39282]. See #38511.

Fixes #55491.
Props spenserhale, westonruter, adamsilverstein, azaozz, shawfactor, peterwilsoncc, swissspidy.

Location:
trunk/src
Files:
7 edited

Legend:

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

    r55561 r56809  
    512512            return __( 'The changes you made will be lost if you navigate away from this page.' );
    513513        }
    514     }).on( 'unload.edit-post', function( event ) {
     514    }).on( 'pagehide.edit-post', function( event ) {
    515515        if ( ! releaseLock ) {
    516516            return;
  • trunk/src/js/_enqueues/lib/gallery.js

    r50547 r56809  
    8989});
    9090
    91 jQuery(window).on( 'unload', function () { window.tinymce = window.tinyMCE = window.wpgallery = null; } ); // Cleanup.
    92 
    9391/* gallery settings */
    9492window.tinymce = null;
  • trunk/src/js/_enqueues/wp/heartbeat.js

    r53226 r56809  
    224224            }
    225225
    226             $(window).on( 'unload.wp-heartbeat', function() {
     226            $(window).on( 'pagehide.wp-heartbeat', function() {
    227227                // Don't connect anymore.
    228                 settings.suspend = true;
     228                suspend();
    229229
    230230                // Abort the last request if not completed.
     
    233233                }
    234234            });
     235
     236            $(window).on(
     237                'pageshow.wp-heartbeat',
     238                /**
     239                 * Handles pageshow event, specifically when page navigation is restored from back/forward cache.
     240                 *
     241                 * @param {jQuery.Event} event
     242                 * @param {PageTransitionEvent} event.originalEvent
     243                 */
     244                function ( event ) {
     245                    if ( event.originalEvent.persisted ) {
     246                        /*
     247                         * When page navigation is stored via bfcache (Back/Forward Cache), consider this the same as
     248                         * if the user had just switched to the tab since the behavior is similar.
     249                         */
     250                        focused();
     251                    }
     252                }
     253            );
    235254
    236255            // Check for user activity every 30 seconds.
     
    542561
    543562            // Resume if suspended.
    544             settings.suspend = false;
     563            resume();
    545564
    546565            if ( ! settings.hasFocus ) {
     
    551570
    552571        /**
     572         * Suspends connecting.
     573         */
     574        function suspend() {
     575            settings.suspend = true;
     576        }
     577
     578        /**
     579         * Resumes connecting.
     580         */
     581        function resume() {
     582            settings.suspend = false;
     583        }
     584
     585        /**
    553586         * Runs when the user becomes active after a period of inactivity.
    554587         *
     
    594627            // Always suspend after 60 minutes of inactivity. This will release the post lock, etc.
    595628            if ( ( settings.suspendEnabled && lastActive > 600000 ) || lastActive > 3600000 ) {
    596                 settings.suspend = true;
     629                suspend();
    597630            }
    598631
  • trunk/src/js/_enqueues/wp/mce-view.js

    r48650 r56809  
    644644                }
    645645
    646                 function reload() {
    647                     if ( ! editor.isHidden() ) {
    648                         $( node ).data( 'rendered', null );
    649 
    650                         setTimeout( function() {
    651                             wp.mce.views.render();
    652                         } );
    653                     }
    654                 }
    655 
    656646                function addObserver() {
    657647                    observer = new MutationObserver( _.debounce( resize, 100 ) );
     
    664654                }
    665655
    666                 $( iframeWin ).on( 'load', resize ).on( 'unload', reload );
     656                $( iframeWin ).on( 'load', resize );
    667657
    668658                MutationObserver = iframeWin.MutationObserver || iframeWin.WebKitMutationObserver || iframeWin.MozMutationObserver;
  • trunk/src/js/_enqueues/wp/media/models.js

    r48650 r56809  
    33 */
    44
    5 var $ = jQuery,
    6     Attachment, Attachments, l10n, media;
     5var Attachment, Attachments, l10n, media;
    76
    87/** @namespace wp */
     
    238237    });
    239238};
    240 
    241 // Clean up. Prevents mobile browsers caching.
    242 $(window).on('unload', function(){
    243     window.wp = null;
    244 });
  • trunk/src/js/_enqueues/wp/widgets/text.js

    r51409 r56809  
    292292
    293293                    // When a widget is moved in the DOM the dynamically-created TinyMCE iframe will be destroyed and has to be re-built.
    294                     $( editor.getWin() ).on( 'unload', function() {
     294                    $( editor.getWin() ).on( 'pagehide', function() {
    295295                        _.defer( buildEditor );
    296296                    });
  • trunk/src/wp-includes/functions.php

    r56763 r56809  
    75867586
    75877587/**
    7588  * Outputs a small JS snippet on preview tabs/windows to remove `window.name` on unload.
     7588 * Outputs a small JS snippet on preview tabs/windows to remove `window.name` when a user is navigating to another page.
    75897589 *
    75907590 * This prevents reusing the same tab for a preview when the user has navigated away.
     
    76157615
    76167616        if ( window.addEventListener ) {
    7617             window.addEventListener( 'unload', function() { window.name = ''; }, false );
     7617            window.addEventListener( 'pagehide', function() { window.name = ''; } );
    76187618        }
    76197619    }());
Note: See TracChangeset for help on using the changeset viewer.