Skip to content

Commit

Permalink
feat: add 'Generate image' button to media upload components
Browse files Browse the repository at this point in the history
  • Loading branch information
Sidsector9 committed Mar 15, 2024
1 parent a33ac3a commit 9ff6271
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"_": "readonly",
"File": "readonly",
"Headers": "readonly",
"requestAnimationFrame": "readonly"
"requestAnimationFrame": "readonly",
"React": "readonly"
},
"extends": ["plugin:@wordpress/eslint-plugin/recommended"],
"ignorePatterns": ["*.json"]
Expand Down
8 changes: 8 additions & 0 deletions includes/Classifai/Features/ImageGeneration.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,14 @@ public function enqueue_admin_scripts( string $hook_suffix = '' ) {
true
);

wp_enqueue_script(
'classifai-extend-image-blocks',
CLASSIFAI_PLUGIN_URL . 'dist/extend-image-blocks.js',
get_asset_info( 'extend-image-blocks', 'dependencies' ),
get_asset_info( 'extend-image-blocks', 'version' ),
true
);

/**
* Filter the default attribution added to generated images.
*
Expand Down
58 changes: 58 additions & 0 deletions src/js/extend-image-block-generate-image.js
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
);
1 change: 1 addition & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ module.exports = {
'generate-image-media-upload': [
'./src/js/media-modal/views/generate-image-media-upload.js',
],
'extend-image-blocks': './src/js/extend-image-block-generate-image.js',
},
module: {
rules: [
Expand Down

0 comments on commit 9ff6271

Please sign in to comment.