From 09ae4b74af2bcb2988566b47701c9258a785f325 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Thu, 8 Oct 2026 12:39:21 +0300 Subject: [PATCH 1/2] Excerpt Generation: Move generation into its own sidebar panel with a review modal --- docs/experiments/excerpt-generation.md | 43 +- .../Excerpt_Generation/Excerpt_Generation.php | 1 + .../components/ExcerptGeneration.tsx | 72 ---- .../components/ExcerptGenerationModal.tsx | 176 ++++++++ .../components/ExcerptGenerationPanel.tsx | 102 +++++ .../components/ExcerptInlineButton.tsx | 60 --- .../components/ExcerptInlineWrapper.tsx | 157 ------- .../components/useExcerptGeneration.ts | 229 +++++----- src/experiments/excerpt-generation/index.scss | 32 ++ src/experiments/excerpt-generation/index.tsx | 56 ++- .../experiments/excerpt-generation.spec.js | 399 +++++++++++++++--- .../no-provider-degradation.spec.js | 40 +- 12 files changed, 847 insertions(+), 520 deletions(-) delete mode 100644 src/experiments/excerpt-generation/components/ExcerptGeneration.tsx create mode 100644 src/experiments/excerpt-generation/components/ExcerptGenerationModal.tsx create mode 100644 src/experiments/excerpt-generation/components/ExcerptGenerationPanel.tsx delete mode 100644 src/experiments/excerpt-generation/components/ExcerptInlineButton.tsx delete mode 100644 src/experiments/excerpt-generation/components/ExcerptInlineWrapper.tsx create mode 100644 src/experiments/excerpt-generation/index.scss diff --git a/docs/experiments/excerpt-generation.md b/docs/experiments/excerpt-generation.md index 23be8fb1d..fe567827e 100644 --- a/docs/experiments/excerpt-generation.md +++ b/docs/experiments/excerpt-generation.md @@ -2,21 +2,30 @@ ## Summary -The Excerpt Generation experiment adds AI-powered excerpt generation to the WordPress post editor. It provides a "Generate excerpt" button in the excerpt panel that uses AI to create concise, engaging summaries of post content. The experiment registers a WordPress Ability (`ai/excerpt-generation`) that can be used both through the admin UI and directly via REST API requests. +The Excerpt Generation experiment adds AI-powered excerpt generation to the WordPress post editor. It provides an "Excerpt generation" sidebar panel with a modal workflow for generating, editing, and applying excerpts. The experiment registers a WordPress Ability (`ai/excerpt-generation`) that can be used both through the admin UI and directly via REST API requests. ## Overview ### For End Users -When enabled, the Excerpt Generation experiment adds a "Generate excerpt" button to the excerpt panel in the WordPress post editor. Users can click this button to automatically generate an excerpt suggestion based on the current post content. The generated excerpt is approximately 55 words, optimized for clarity, engagement, and SEO, and suitable for archive views, RSS feeds, and search results. +When enabled, the Excerpt Generation experiment adds an "Excerpt generation" panel to the document sidebar of the WordPress post editor. Users can click its "Generate excerpt" button to open a modal that generates an excerpt from the post content, review or edit the suggestion, and apply it to the post. The generated excerpt is approximately 55 words, optimized for clarity, engagement, and SEO, and suitable for archive views, RSS feeds, and search results. **Key Features:** -- One-click excerpt generation from post content -- Automatically populates the excerpt field in the editor +- Generates an excerpt suggestion of roughly 55 words from the post content +- Editable textarea allows fine-tuning the suggestion before applying +- Copy to clipboard functionality for use outside the excerpt field - Works with any post type that supports excerpts -- Generates excerpts optimized for SEO and readability -- Can regenerate excerpts if you want a different suggestion +- Can regenerate if you want a different suggestion + +**Workflow:** + +1. Open or create a post in the editor +2. Find the "Excerpt generation" panel in the sidebar +3. Click "Generate excerpt" to open the modal +4. Review the AI-generated suggestion in the textarea and edit as needed +5. Click "Apply" to set it as the post's excerpt +6. Save/update the post as usual ### For Developers @@ -42,12 +51,12 @@ The ability can be called directly via REST API, making it useful for automation - `enabled`: Whether the experiment is enabled 2. **React Side:** - - The React entry point (`index.tsx`) registers a WordPress plugin that hooks into the excerpt panel using `__experimentalPluginPostExcerpt` - - `ExcerptGeneration` component renders a button that calls `useExcerptGeneration()` hook + - The React entry point (`index.tsx`) registers a `PluginDocumentSettingPanel` named `ai-excerpt-generation`, hidden when the core Excerpt panel is disabled + - `ExcerptGenerationPanel` renders the generate button; clicking it opens `ExcerptGenerationModal`, which shows the suggestion in an editable textarea with Apply, Regenerate, Copy and Cancel actions - `useExcerptGeneration` hook: - Gets current post ID and content from the editor store - - Calls the ability via `apiFetch` when the button is clicked - - Updates the editor store and DOM textarea with the generated excerpt + - Calls the ability when the modal opens or Regenerate is clicked + - Updates the editor store with the excerpt when it is applied - Handles loading states and error notifications 3. **Ability Execution:** @@ -303,14 +312,14 @@ When a post ID is provided, the ability uses `get_post_context()` to gather post You can extend the React components to add custom UI elements: 1. **Modify the button component:** - - Edit `src/experiments/excerpt-generation/components/ExcerptGeneration.tsx` + - Edit `src/experiments/excerpt-generation/components/ExcerptGenerationPanel.tsx` or `ExcerptGenerationModal.tsx` 2. **Add custom hooks:** - Create new hooks in `src/experiments/excerpt-generation/components/` - Import and use them in the main component -3. **Customize the excerpt panel:** - - The experiment uses `__experimentalPluginPostExcerpt` to inject into the excerpt panel +3. **Customize the sidebar panel:** + - The experiment uses `PluginDocumentSettingPanel` to render the panel - You can modify `src/experiments/excerpt-generation/index.tsx` to add additional UI ## Testing @@ -324,10 +333,12 @@ You can extend the React components to add custom UI elements: 2. **Test in the editor:** - Create or edit a post with content - - Scroll to the excerpt panel (or enable it in Screen Options) - - Click the "Generate excerpt" button - - Verify the excerpt is generated and populated in the field + - Open the "Excerpt generation" panel in the document sidebar + - Click "Generate excerpt" to open the modal + - Verify the suggestion populates the textarea, edit it if needed, and click "Apply" + - Verify the post's excerpt field is updated - Click "Regenerate excerpt" to test regeneration + - Test "Copy to clipboard" and verify the text is copied 3. **Test with different post types:** - The experiment only loads for post types that support excerpts diff --git a/includes/Experiments/Excerpt_Generation/Excerpt_Generation.php b/includes/Experiments/Excerpt_Generation/Excerpt_Generation.php index a5af2f77e..3a7bb26b7 100644 --- a/includes/Experiments/Excerpt_Generation/Excerpt_Generation.php +++ b/includes/Experiments/Excerpt_Generation/Excerpt_Generation.php @@ -94,6 +94,7 @@ public function enqueue_assets( string $hook_suffix ): void { } Asset_Loader::enqueue_script( 'excerpt_generation', 'experiments/excerpt-generation', array( 'include_core_abilities' => true ) ); + Asset_Loader::enqueue_style( 'excerpt_generation', 'experiments/excerpt-generation' ); Asset_Loader::localize_script( 'excerpt_generation', 'ExcerptGenerationData', diff --git a/src/experiments/excerpt-generation/components/ExcerptGeneration.tsx b/src/experiments/excerpt-generation/components/ExcerptGeneration.tsx deleted file mode 100644 index 05315bf5e..000000000 --- a/src/experiments/excerpt-generation/components/ExcerptGeneration.tsx +++ /dev/null @@ -1,72 +0,0 @@ -/** - * Excerpt generator component for the excerpt panel. - */ - -/** - * WordPress dependencies - */ -import { Button, Notice } from '@wordpress/components'; -import { update } from '@wordpress/icons'; -import { __ } from '@wordpress/i18n'; -import { Stack } from '@wordpress/ui'; - -/** - * Internal dependencies - */ -import { useExcerptGeneration } from './useExcerptGeneration'; - -const { aiExcerptGenerationData } = window as any; - -/** - * ExcerptGeneration component. - * - * Provides a button to generate an excerpt. - * - * @return {React.JSX.Element | null} The excerpt generation component. - */ -export default function ExcerptGeneration(): React.JSX.Element | null { - const { - isGenerating, - hasExcerpt, - isContentTooShort, - tooShortLabel, - handleGenerate, - } = useExcerptGeneration(); - - // Don't render if disabled. - if ( ! aiExcerptGenerationData?.enabled ) { - return null; - } - - let buttonLabel: string = __( 'Generate excerpt', 'ai' ); - - if ( isGenerating ) { - buttonLabel = __( 'Generating…', 'ai' ); - } else if ( hasExcerpt ) { - buttonLabel = __( 'Regenerate excerpt', 'ai' ); - } - - return ( - - { isContentTooShort && ( - - { tooShortLabel } - - ) } - - - - ); -} diff --git a/src/experiments/excerpt-generation/components/ExcerptGenerationModal.tsx b/src/experiments/excerpt-generation/components/ExcerptGenerationModal.tsx new file mode 100644 index 000000000..45b0c9d50 --- /dev/null +++ b/src/experiments/excerpt-generation/components/ExcerptGenerationModal.tsx @@ -0,0 +1,176 @@ +/** + * Modal component for generating and editing an excerpt suggestion. + */ + +/** + * WordPress dependencies + */ +import { useEffect } from '@wordpress/element'; +import { __ } from '@wordpress/i18n'; +import { Modal, Button, TextareaControl, Notice } from '@wordpress/components'; + +/** + * Internal dependencies + */ +import { useCopyToClipboardFeedback } from '../../../hooks/use-copy-to-clipboard-feedback'; + +interface ExcerptGenerationModalProps { + isGenerating: boolean; + suggestion: string | null; + editableText: string; + isContentTooShort: boolean; + tooShortLabel: string; + onEditableTextChange: ( text: string ) => void; + onGenerate: () => Promise< void >; + onApply: ( text: string ) => void; + onClose: () => void; +} + +function CopyButton( { + text, + disabled, +}: { + text: string; + disabled: boolean; +} ): JSX.Element { + const { ref, hasCopied } = useCopyToClipboardFeedback< HTMLButtonElement >( + { + text, + announcement: __( 'Excerpt copied to clipboard.', 'ai' ), + } + ); + + const isCopyDisabled = disabled || hasCopied; + + return ( + + ); +} + +/** + * Modal for generating and editing an excerpt. + * + * @param props Component props. + * @param props.isGenerating Whether generation is in progress. + * @param props.suggestion The generated suggestion. + * @param props.editableText The current editable text value. + * @param props.isContentTooShort Whether the content is too short to generate a suggestion. + * @param props.tooShortLabel Label to show when content is too short. + * @param props.onEditableTextChange Callback to update the editable text. + * @param props.onGenerate Callback to trigger generation. + * @param props.onApply Callback to apply the excerpt. + * @param props.onClose Callback to close the modal. + */ +export default function ExcerptGenerationModal( { + isGenerating, + suggestion, + editableText, + isContentTooShort, + tooShortLabel, + onEditableTextChange, + onGenerate, + onApply, + onClose, +}: ExcerptGenerationModalProps ): React.JSX.Element { + // Populate the textarea when a new suggestion arrives. + useEffect( () => { + if ( suggestion ) { + onEditableTextChange( suggestion ); + } + }, [ suggestion, onEditableTextChange ] ); + + let generateButtonLabel: string = suggestion + ? __( 'Regenerate', 'ai' ) + : __( 'Generate', 'ai' ); + + if ( isGenerating ) { + generateButtonLabel = __( 'Generating…', 'ai' ); + } + + return ( + +
+ { isContentTooShort && ( + + { tooShortLabel } + + ) } + + + +
+ + + + +
+
+
+ ); +} diff --git a/src/experiments/excerpt-generation/components/ExcerptGenerationPanel.tsx b/src/experiments/excerpt-generation/components/ExcerptGenerationPanel.tsx new file mode 100644 index 000000000..bf2b6a97f --- /dev/null +++ b/src/experiments/excerpt-generation/components/ExcerptGenerationPanel.tsx @@ -0,0 +1,102 @@ +/** + * Sidebar panel component for the excerpt generation experiment. + */ + +/** + * WordPress dependencies + */ +import { useState } from '@wordpress/element'; +import { __ } from '@wordpress/i18n'; +import { Button } from '@wordpress/components'; + +/** + * Internal dependencies + */ +import { useExcerptGeneration } from './useExcerptGeneration'; +import ExcerptGenerationModal from './ExcerptGenerationModal'; + +/** + * Panel with the generate button. Generation happens in a modal where the + * suggestion can be reviewed and edited before it is applied to the post. + */ +export default function ExcerptGenerationPanel(): React.JSX.Element { + const { + isGenerating, + suggestion, + currentExcerpt, + isContentTooShort, + tooShortLabel, + ensureProviderAvailable, + generateExcerpt, + cancelGeneration, + applyExcerpt, + clearSuggestion, + } = useExcerptGeneration(); + + const [ isModalOpen, setIsModalOpen ] = useState( false ); + const [ editableText, setEditableText ] = useState( '' ); + + const hasExcerpt = currentExcerpt.trim().length > 0; + + let buttonLabel: string = hasExcerpt + ? __( 'Regenerate excerpt', 'ai' ) + : __( 'Generate excerpt', 'ai' ); + + if ( isGenerating ) { + buttonLabel = __( 'Generating…', 'ai' ); + } + + const handleOpenModal = async () => { + if ( ! ensureProviderAvailable() ) { + return; + } + + setEditableText( currentExcerpt ); + setIsModalOpen( true ); + await generateExcerpt(); + }; + + return ( +
+ + + { isContentTooShort && ( +

+ { tooShortLabel } +

+ ) } + + { isModalOpen && ( + { + cancelGeneration(); + clearSuggestion(); + setIsModalOpen( false ); + } } + /> + ) } +
+ ); +} diff --git a/src/experiments/excerpt-generation/components/ExcerptInlineButton.tsx b/src/experiments/excerpt-generation/components/ExcerptInlineButton.tsx deleted file mode 100644 index 5f2c81ffe..000000000 --- a/src/experiments/excerpt-generation/components/ExcerptInlineButton.tsx +++ /dev/null @@ -1,60 +0,0 @@ -/** - * Inline button component for the excerpt link area. - */ - -/** - * WordPress dependencies - */ -import { Button } from '@wordpress/components'; -import { update } from '@wordpress/icons'; -import { __ } from '@wordpress/i18n'; - -/** - * Internal dependencies - */ -import { useExcerptGeneration } from './useExcerptGeneration'; - -const { aiExcerptGenerationData } = window as any; - -/** - * Inline button component for generating excerpts next to the excerpt link. - * - * @return {React.JSX.Element | null} The inline button component. - */ -export default function ExcerptInlineButton(): React.JSX.Element | null { - const { - isGenerating, - hasExcerpt, - isContentTooShort, - tooShortLabel, - handleGenerate, - } = useExcerptGeneration(); - - // Don't render if disabled. - if ( ! aiExcerptGenerationData?.enabled ) { - return null; - } - - let buttonLabel: string = __( 'Generate excerpt', 'ai' ); - - if ( isGenerating ) { - buttonLabel = __( 'Generating…', 'ai' ); - } else if ( hasExcerpt ) { - buttonLabel = __( 'Regenerate excerpt', 'ai' ); - } - - return ( -