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

Fix: Image that is uploaded to an existing gallery does not appear in the edit gallery view #12435

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
68 changes: 52 additions & 16 deletions packages/edit-post/src/hooks/components/media-upload/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ class MediaUpload extends Component {
gallery = false,
title = __( 'Select or Upload Media' ),
modalClass,
value,
} ) {
super( ...arguments );
this.openModal = this.openModal.bind( this );
Expand All @@ -92,21 +91,7 @@ class MediaUpload extends Component {
this.onClose = this.onClose.bind( this );

if ( gallery ) {
const currentState = value ? 'gallery-edit' : 'gallery';
const GalleryDetailsMediaFrame = getGalleryDetailsMediaFrame();
const attachments = getAttachmentsCollection( value );
const selection = new wp.media.model.Selection( attachments.models, {
props: attachments.props.toJSON(),
multiple,
} );
this.frame = new GalleryDetailsMediaFrame( {
mimeType: allowedTypes,
state: currentState,
multiple,
selection,
editing: ( value ) ? true : false,
} );
wp.media.frame = this.frame;
this.buildAndSetGalleryFrame();
} else {
const frameConfig = {
title,
Expand All @@ -126,13 +111,56 @@ class MediaUpload extends Component {
this.frame.$el.addClass( modalClass );
}

this.initializeListeners();
}

initializeListeners() {
// When an image is selected in the media frame...
this.frame.on( 'select', this.onSelect );
this.frame.on( 'update', this.onUpdate );
this.frame.on( 'open', this.onOpen );
this.frame.on( 'close', this.onClose );
}

buildAndSetGalleryFrame() {
const {
allowedTypes,
multiple = false,
value = null,
} = this.props;
// If the value did not changed there is no need to rebuild the frame,
// we can continue to use the existing one.
if ( value === this.lastGalleryValue ) {
return;
}

this.lastGalleryValue = value;

// If a frame already existed remove it.
if ( this.frame ) {
this.frame.remove();
}
const currentState = value ? 'gallery-edit' : 'gallery';
if ( ! this.GalleryDetailsMediaFrame ) {
this.GalleryDetailsMediaFrame = getGalleryDetailsMediaFrame();
}

const attachments = getAttachmentsCollection( value );
const selection = new wp.media.model.Selection( attachments.models, {
props: attachments.props.toJSON(),
multiple,
} );
this.frame = new this.GalleryDetailsMediaFrame( {
mimeType: allowedTypes,
state: currentState,
multiple,
selection,
editing: ( value ) ? true : false,
} );
wp.media.frame = this.frame;
this.initializeListeners();
}

componentWillUnmount() {
this.frame.remove();
}
Expand Down Expand Up @@ -176,6 +204,7 @@ class MediaUpload extends Component {
selection.add( wp.media.attachment( id ) );
} );
}

// load the images so they are available in the media modal.
getAttachmentsCollection( castArray( this.props.value ) ).more();
}
Expand Down Expand Up @@ -205,6 +234,13 @@ class MediaUpload extends Component {
}

openModal() {
if (
this.props.gallery &&
this.props.value &&
this.props.value.length > 0
) {
this.buildAndSetGalleryFrame();
}
this.frame.open();
}

Expand Down