Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Media upload component: lazy mount #56958

Merged
merged 2 commits into from
Dec 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading