-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add 'Generate image' button to media upload components
- Loading branch information
1 parent
a33ac3a
commit 9ff6271
Showing
4 changed files
with
69 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { Button } from '@wordpress/components'; | ||
import { _nx } from '@wordpress/i18n'; | ||
import { useBlockProps } from '@wordpress/block-editor'; | ||
|
||
/** | ||
* Adds a `Generate image` button to the media-related blocks. | ||
* @see {@link https://github.com/10up/classifai/issues/724} | ||
* | ||
* @param {React.ReactNode} Component The Wrapped Media upload component. | ||
* @return {React.ReactNode} The transformed Media upload component. | ||
*/ | ||
function addImageGenerationLink( Component ) { | ||
return function ( props ) { | ||
const { render, mode, ...rest } = props; | ||
let blockProps; | ||
|
||
try { | ||
blockProps = useBlockProps(); | ||
} catch ( e ) { | ||
return <Component { ...props } />; | ||
} | ||
|
||
const { 'data-type': blockName } = blockProps; | ||
|
||
let isSingle = 1; | ||
|
||
if ( blockName && 'core/gallery' === blockName ) { | ||
isSingle = Infinity; | ||
} | ||
|
||
return ( | ||
<> | ||
<Component | ||
{ ...rest } | ||
mode="generate" | ||
render={ ( { open } ) => ( | ||
<Button variant="tertiary" onClick={ open }> | ||
{ _nx( | ||
'Generate image', | ||
'Generate images', | ||
isSingle, | ||
'Image or gallery upload', | ||
'classifai' | ||
) } | ||
</Button> | ||
) } | ||
/> | ||
<Component { ...props } /> | ||
</> | ||
); | ||
}; | ||
} | ||
|
||
wp.hooks.addFilter( | ||
'editor.MediaUpload', | ||
'classifai/image-generation-link', | ||
addImageGenerationLink | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters