Skip to content

Commit

Permalink
Merge pull request #536 from 10up/feature/commands
Browse files Browse the repository at this point in the history
Initial integration into the new Command Palette
  • Loading branch information
dkotter authored Jul 17, 2023
2 parents 800638e + 6ebe3e3 commit d3d85c4
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
10 changes: 10 additions & 0 deletions includes/Classifai/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,16 @@ public function enqueue_admin_assets() {
'ajax_nonce' => wp_create_nonce( 'classifai' ),
]
);

if ( wp_script_is( 'wp-commands', 'registered' ) ) {
wp_enqueue_script(
'classifai-commands',
CLASSIFAI_PLUGIN_URL . 'dist/commands.js',
get_asset_info( 'commands', 'dependencies' ),
get_asset_info( 'commands', 'version' ),
true
);
}
}

/**
Expand Down
86 changes: 86 additions & 0 deletions src/js/gutenberg-plugins/commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { useCommandLoader } from '@wordpress/commands';
import { edit, image, settings } from '@wordpress/icons';
import { __ } from '@wordpress/i18n';
import { registerPlugin } from '@wordpress/plugins';

const Commands = () => {
const getCommandLoader = () => {
const commands = [];
const excerptButton = document.querySelector(
'.editor-post-excerpt button.classifai-post-excerpt'
);
const titleButton = document.querySelector(
'.classifai-post-status button.title'
);

// Command to open the ClassifAI settings page.
commands.push( {
name: 'classifai/settings',
label: __( 'ClassifAI settings', 'classifai' ),
icon: settings,
callback: () => {
document.location.href = 'tools.php?page=classifai';
},
} );

// Command to generate an excerpt.
if ( excerptButton ) {
commands.push( {
name: 'classifai/generate-excerpt',
label: __( 'ClassifAI: Generate excerpt', 'classifai' ),
icon: edit,
callback: ( { close } ) => {
close();

excerptButton.scrollIntoView( {
block: 'center',
} );
excerptButton.click();
},
} );
}

// Command to generate titles.
if ( titleButton ) {
commands.push( {
name: 'classifai/generate-titles',
label: __( 'ClassifAI: Generate titles', 'classifai' ),
icon: edit,
callback: ( { close } ) => {
close();

titleButton.scrollIntoView( {
block: 'center',
} );
titleButton.click();
},
} );
}

// Command to go to the image generation page.
if ( typeof classifaiDalleData !== 'undefined' ) {
commands.push( {
name: 'classifai/generate-image',
label: __( 'ClassifAI: Generate image', 'classifai' ),
icon: image,
callback: () => {
document.location.href =
'upload.php?action=classifai-generate-image';
},
} );
}

return { commands };
};

useCommandLoader( {
name: 'classifai',
hook: getCommandLoader,
} );

return null;
};

if ( 'function' === typeof useCommandLoader ) {
registerPlugin( 'classifai-commands', { render: Commands } );
}
1 change: 1 addition & 0 deletions src/js/post-excerpt/panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function PostExcerpt( { excerpt, onUpdateExcerpt } ) {
</ExternalLink>
) }
<Button
className="classifai-post-excerpt"
variant={ 'secondary' }
data-id={ postId }
onClick={ ( e ) =>
Expand Down
1 change: 1 addition & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module.exports = {
'generate-title-classic': [
'./src/js/openai/classic-editor-title-generator.js',
],
commands: [ './src/js/gutenberg-plugins/commands.js' ],
'generate-image-media-upload': [
'./src/js/media-modal/views/generate-image-media-upload.js',
],
Expand Down

0 comments on commit d3d85c4

Please sign in to comment.