Skip to content

Commit

Permalink
Media Library: Expose filters dropdown for individual images, such as…
Browse files Browse the repository at this point in the history
… with the Image block (#65965)

* Media Library: Expose filters dropdown for individual images, such as with the Image block

* Rename to be a little more generic

Unlinked contributors: cagivacode, tiagogon, kylemcph, Wadowad, yoanmarchal, RoqueCampos, venture89, spncr, simonPaulBergzeit, Ezokia, fylgjur, sbathompson-he, dnnsjsk.

Co-authored-by: andrewserong <andrewserong@git.wordpress.org>
Co-authored-by: draganescu <andraganescu@git.wordpress.org>
Co-authored-by: finalwebsites <finalwebsites@git.wordpress.org>
Co-authored-by: Drivingralle <drivingralle@git.wordpress.org>
Co-authored-by: johnbillion <johnbillion@git.wordpress.org>
Co-authored-by: torounit <toro_unit@git.wordpress.org>
Co-authored-by: paaljoachim <paaljoachim@git.wordpress.org>
Co-authored-by: Soean <soean@git.wordpress.org>
Co-authored-by: jamesmrobinson <imaginarymedia@git.wordpress.org>
  • Loading branch information
10 people authored Nov 1, 2024
1 parent 11e2d06 commit 0fa9ca0
Showing 1 changed file with 82 additions and 13 deletions.
95 changes: 82 additions & 13 deletions packages/media-utils/src/components/media-upload/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,46 @@ const getFeaturedImageMediaFrame = () => {
} );
};

/**
* Prepares the default frame for selecting a single media item.
*
* @return {window.wp.media.view.MediaFrame.Select} The default media workflow.
*/
const getSingleMediaFrame = () => {
const { wp } = window;

// Extend the default Select frame, and use the same `createStates` method as in core,
// but with the addition of `filterable: 'uploaded'` to the Library state, so that
// the user can filter the media library by uploaded media.
return wp.media.view.MediaFrame.Select.extend( {
/**
* Create the default states on the frame.
*/
createStates() {
const options = this.options;

if ( this.options.states ) {
return;
}

// Add the default states.
this.states.add( [
// Main states.
new wp.media.controller.Library( {
library: wp.media.query( options.library ),
multiple: options.multiple,
title: options.title,
priority: 20,
filterable: 'uploaded', // Allow filtering by uploaded images.
} ),
new wp.media.controller.EditImage( {
model: options.editImage,
} ),
] );
},
} );
};

/**
* Prepares the Gallery toolbars and frames.
*
Expand Down Expand Up @@ -324,6 +364,47 @@ class MediaUpload extends Component {
};
}

/**
* Initializes the Media Library requirements for the single image flow.
*
* @return {void}
*/
buildAndSetSingleMediaFrame() {
const { wp } = window;
const {
allowedTypes,
multiple = false,
title = __( 'Select or Upload Media' ),
value,
} = this.props;

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

// If a frame already exists, remove it.
if ( this.frame ) {
this.frame.remove();
}

const singleImageFrame = getSingleMediaFrame();
const attachments = getAttachmentsCollection( value );
const selection = new wp.media.model.Selection( attachments.models, {
props: attachments.props.toJSON(),
} );
this.frame = new singleImageFrame( {
mimeType: allowedTypes,
multiple,
selection,
...frameConfig,
} );
wp.media.frame = this.frame;
}

componentWillUnmount() {
this.frame?.remove();
}
Expand Down Expand Up @@ -424,27 +505,15 @@ class MediaUpload extends Component {

openModal() {
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 );
this.buildAndSetSingleMediaFrame();
}

if ( modalClass ) {
Expand Down

1 comment on commit 0fa9ca0

@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 0fa9ca0.
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/11624635563
📝 Reported issues:

Please sign in to comment.