Skip to content

Commit

Permalink
Media upload component: lazy mount (#56958)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix authored Dec 11, 2023
1 parent d617e5c commit 5ea1a8f
Showing 1 changed file with 32 additions and 35 deletions.
67 changes: 32 additions & 35 deletions packages/media-utils/src/components/media-upload/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,45 +224,13 @@ const getAttachmentsCollection = ( ids ) => {
};

class MediaUpload extends Component {
constructor( {
allowedTypes,
gallery = false,
unstableFeaturedImageFlow = false,
modalClass,
multiple = false,
title = __( 'Select or Upload Media' ),
} ) {
constructor() {
super( ...arguments );
this.openModal = this.openModal.bind( this );
this.onOpen = this.onOpen.bind( this );
this.onSelect = this.onSelect.bind( this );
this.onUpdate = this.onUpdate.bind( this );
this.onClose = this.onClose.bind( this );

const { wp } = window;

if ( gallery ) {
this.buildAndSetGalleryFrame();
} else {
const frameConfig = {
title,
multiple,
};
if ( !! allowedTypes ) {
frameConfig.library = { type: allowedTypes };
}

this.frame = wp.media( frameConfig );
}

if ( modalClass ) {
this.frame.$el.addClass( modalClass );
}

if ( unstableFeaturedImageFlow ) {
this.buildAndSetFeatureImageFrame();
}
this.initializeListeners();
}

initializeListeners() {
Expand Down Expand Up @@ -348,7 +316,7 @@ class MediaUpload extends Component {
}

componentWillUnmount() {
this.frame.remove();
this.frame?.remove();
}

onUpdate( selections ) {
Expand Down Expand Up @@ -444,9 +412,38 @@ class MediaUpload extends Component {
}

openModal() {
if ( this.props.gallery ) {
const {
allowedTypes,
gallery = false,
unstableFeaturedImageFlow = false,
modalClass,
multiple = false,
title = __( 'Select or Upload Media' ),
} = this.props;
const { wp } = window;

if ( gallery ) {
this.buildAndSetGalleryFrame();
} else {
const frameConfig = {
title,
multiple,
};
if ( !! allowedTypes ) {
frameConfig.library = { type: allowedTypes };
}

this.frame = wp.media( frameConfig );
}

if ( modalClass ) {
this.frame.$el.addClass( modalClass );
}

if ( unstableFeaturedImageFlow ) {
this.buildAndSetFeatureImageFrame();
}
this.initializeListeners();
this.frame.open();
}

Expand Down

1 comment on commit 5ea1a8f

@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 5ea1a8f.
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/7174362902
📝 Reported issues:

Please sign in to comment.