Skip to content

Commit

Permalink
MediaUtils: move window access to function body (#46970)
Browse files Browse the repository at this point in the history
  • Loading branch information
flootr authored Jan 16, 2023
1 parent 07cd1ba commit 8f7bd86
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
9 changes: 2 additions & 7 deletions packages/blob/src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* Browser dependencies
*/
const { createObjectURL, revokeObjectURL } = window.URL;

/**
* @type {Record<string, File|undefined>}
*/
Expand All @@ -16,7 +11,7 @@ const cache = {};
* @return {string} The blob URL.
*/
export function createBlobURL( file ) {
const url = createObjectURL( file );
const url = window.URL.createObjectURL( file );

cache[ url ] = file;

Expand Down Expand Up @@ -56,7 +51,7 @@ export function getBlobTypeByURL( url ) {
*/
export function revokeBlobURL( url ) {
if ( cache[ url ] ) {
revokeObjectURL( url );
window.URL.revokeObjectURL( url );
}

delete cache[ url ];
Expand Down
17 changes: 13 additions & 4 deletions packages/media-utils/src/components/media-upload/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
import { Component } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

const { wp } = window;

const DEFAULT_EMPTY_GALLERY = [];

/**
* Prepares the Featured Image toolbars and frames.
*
* @return {wp.media.view.MediaFrame.Select} The default media workflow.
* @return {window.wp.media.view.MediaFrame.Select} The default media workflow.
*/
const getFeaturedImageMediaFrame = () => {
const { wp } = window;

return wp.media.view.MediaFrame.Select.extend( {
/**
* Enables the Set Featured Image Button.
Expand Down Expand Up @@ -73,9 +73,10 @@ const getFeaturedImageMediaFrame = () => {
/**
* Prepares the Gallery toolbars and frames.
*
* @return {wp.media.view.MediaFrame.Post} The default media workflow.
* @return {window.wp.media.view.MediaFrame.Post} The default media workflow.
*/
const getGalleryDetailsMediaFrame = () => {
const { wp } = window;
/**
* Custom gallery details frame.
*
Expand Down Expand Up @@ -210,6 +211,8 @@ const slimImageObject = ( img ) => {
};

const getAttachmentsCollection = ( ids ) => {
const { wp } = window;

return wp.media.query( {
order: 'ASC',
orderby: 'post__in',
Expand All @@ -236,6 +239,8 @@ class MediaUpload extends Component {
this.onUpdate = this.onUpdate.bind( this );
this.onClose = this.onClose.bind( this );

const { wp } = window;

if ( gallery ) {
this.buildAndSetGalleryFrame();
} else {
Expand Down Expand Up @@ -287,6 +292,8 @@ class MediaUpload extends Component {
return;
}

const { wp } = window;

this.lastGalleryValue = value;

// If a frame already existed remove it.
Expand Down Expand Up @@ -324,6 +331,7 @@ class MediaUpload extends Component {
* @return {void}
*/
buildAndSetFeatureImageFrame() {
const { wp } = window;
const featuredImageFrame = getFeaturedImageMediaFrame();
const attachments = getAttachmentsCollection( this.props.value );
const selection = new wp.media.model.Selection( attachments.models, {
Expand Down Expand Up @@ -371,6 +379,7 @@ class MediaUpload extends Component {
}

onOpen() {
const { wp } = window;
const { value } = this.props;
this.updateCollection();

Expand Down

1 comment on commit 8f7bd86

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 8f7bd86.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/3928267689
📝 Reported issues:

Please sign in to comment.