Skip to content

Commit

Permalink
Administration: Remove deprecated unload event handlers and use `pa…
Browse files Browse the repository at this point in the history
…gehide` (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.

git-svn-id: https://develop.svn.wordpress.org/trunk@56809 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
westonruter committed Oct 9, 2023
1 parent 9ec5646 commit fbe3732
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/js/_enqueues/admin/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ jQuery( function($) {
// 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.' );
}
}).on( 'unload.edit-post', function( event ) {
}).on( 'pagehide.edit-post', function( event ) {
if ( ! releaseLock ) {
return;
}
Expand Down
2 changes: 0 additions & 2 deletions src/js/_enqueues/lib/gallery.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ jQuery( function($) {
}
});

jQuery(window).on( 'unload', function () { window.tinymce = window.tinyMCE = window.wpgallery = null; } ); // Cleanup.

/* gallery settings */
window.tinymce = null;

Expand Down
41 changes: 37 additions & 4 deletions src/js/_enqueues/wp/heartbeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,16 +223,35 @@
settings.checkFocusTimer = window.setInterval( checkFocus, 10000 );
}

$(window).on( 'unload.wp-heartbeat', function() {
$(window).on( 'pagehide.wp-heartbeat', function() {
// Don't connect anymore.
settings.suspend = true;
suspend();

// Abort the last request if not completed.
if ( settings.xhr && settings.xhr.readyState !== 4 ) {
settings.xhr.abort();
}
});

$(window).on(
'pageshow.wp-heartbeat',
/**
* Handles pageshow event, specifically when page navigation is restored from back/forward cache.
*
* @param {jQuery.Event} event
* @param {PageTransitionEvent} event.originalEvent
*/
function ( event ) {
if ( event.originalEvent.persisted ) {
/*
* When page navigation is stored via bfcache (Back/Forward Cache), consider this the same as
* if the user had just switched to the tab since the behavior is similar.
*/
focused();
}
}
);

// Check for user activity every 30 seconds.
window.setInterval( checkUserActivity, 30000 );

Expand Down Expand Up @@ -541,14 +560,28 @@
settings.userActivity = time();

// Resume if suspended.
settings.suspend = false;
resume();

if ( ! settings.hasFocus ) {
settings.hasFocus = true;
scheduleNextTick();
}
}

/**
* Suspends connecting.
*/
function suspend() {
settings.suspend = true;
}

/**
* Resumes connecting.
*/
function resume() {
settings.suspend = false;
}

/**
* Runs when the user becomes active after a period of inactivity.
*
Expand Down Expand Up @@ -593,7 +626,7 @@
// Suspend after 10 minutes of inactivity when suspending is enabled.
// Always suspend after 60 minutes of inactivity. This will release the post lock, etc.
if ( ( settings.suspendEnabled && lastActive > 600000 ) || lastActive > 3600000 ) {
settings.suspend = true;
suspend();
}

if ( ! settings.userActivityEvents ) {
Expand Down
12 changes: 1 addition & 11 deletions src/js/_enqueues/wp/mce-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -643,16 +643,6 @@
}, 3000 );
}

function reload() {
if ( ! editor.isHidden() ) {
$( node ).data( 'rendered', null );

setTimeout( function() {
wp.mce.views.render();
} );
}
}

function addObserver() {
observer = new MutationObserver( _.debounce( resize, 100 ) );

Expand All @@ -663,7 +653,7 @@
} );
}

$( iframeWin ).on( 'load', resize ).on( 'unload', reload );
$( iframeWin ).on( 'load', resize );

MutationObserver = iframeWin.MutationObserver || iframeWin.WebKitMutationObserver || iframeWin.MozMutationObserver;

Expand Down
8 changes: 1 addition & 7 deletions src/js/_enqueues/wp/media/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
* @output wp-includes/js/media-models.js
*/

var $ = jQuery,
Attachment, Attachments, l10n, media;
var Attachment, Attachments, l10n, media;

/** @namespace wp */
window.wp = window.wp || {};
Expand Down Expand Up @@ -237,8 +236,3 @@ media.query = function( props ) {
props: _.extend( _.defaults( props || {}, { orderby: 'date' } ), { query: true } )
});
};

// Clean up. Prevents mobile browsers caching.
$(window).on('unload', function(){
window.wp = null;
});
2 changes: 1 addition & 1 deletion src/js/_enqueues/wp/widgets/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ wp.textWidgets = ( function( $ ) {
onInit = function() {

// When a widget is moved in the DOM the dynamically-created TinyMCE iframe will be destroyed and has to be re-built.
$( editor.getWin() ).on( 'unload', function() {
$( editor.getWin() ).on( 'pagehide', function() {
_.defer( buildEditor );
});

Expand Down
4 changes: 2 additions & 2 deletions src/wp-includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7585,7 +7585,7 @@ function wp_delete_file_from_directory( $file, $directory ) {
}

/**
* Outputs a small JS snippet on preview tabs/windows to remove `window.name` on unload.
* Outputs a small JS snippet on preview tabs/windows to remove `window.name` when a user is navigating to another page.
*
* This prevents reusing the same tab for a preview when the user has navigated away.
*
Expand Down Expand Up @@ -7614,7 +7614,7 @@ function wp_post_preview_js() {
}

if ( window.addEventListener ) {
window.addEventListener( 'unload', function() { window.name = ''; }, false );
window.addEventListener( 'pagehide', function() { window.name = ''; } );
}
}());
</script>
Expand Down

0 comments on commit fbe3732

Please sign in to comment.