-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #749 from 10up/feat/724
feat/724: Adds "Generate image" button to media upload components
- Loading branch information
Showing
4 changed files
with
80 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,69 @@ | ||
import { Button } from '@wordpress/components'; | ||
import { _nx } from '@wordpress/i18n'; | ||
import { useBlockProps } from '@wordpress/block-editor'; | ||
|
||
const allowedCoreBlocks = [ | ||
'core/image', | ||
'core/gallery', | ||
'core/media-text', | ||
'core/cover', | ||
]; | ||
|
||
/** | ||
* 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, ...rest } = props; | ||
let blockProps; | ||
|
||
try { | ||
blockProps = useBlockProps(); | ||
} catch ( e ) { | ||
return <Component { ...props } />; | ||
} | ||
|
||
const { 'data-type': blockName } = blockProps; | ||
|
||
if ( ! allowedCoreBlocks.includes( blockName ) ) { | ||
return <Component { ...props } />; | ||
} | ||
|
||
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