Skip to content

Commit

Permalink
feat: allow saving generated text in a media library from block edito…
Browse files Browse the repository at this point in the history
…r context
  • Loading branch information
rafaucau committed Aug 18, 2024
1 parent fd3f182 commit 6c228f3
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 12 deletions.
24 changes: 18 additions & 6 deletions src/components/BulkGenerateModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import generateAltText from '../utils/generateAltText';
import sleep from '../utils/sleep';
import BulkGenerationTable from './BulkGenerationTable';
import CustomPromptControl from './CustomPromptControl';
import SaveAltInMediaLibraryControl from './SaveAltInMediaLibraryControl';

export interface BulkGenerateModalProps {
attachmentIds: number[];
Expand All @@ -30,6 +31,9 @@ export default function BulkGenerateModal({
}: BulkGenerateModalProps) {
const [overwriteExisting, setOverwriteExisting] = useState(false);
const [customPrompt, setCustomPrompt] = useState('');
const [saveAltInMediaLibrary, setSaveAltInMediaLibrary] = useState(
context === 'mediaLibrary',
);
const [isGenerating, setIsGenerating] = useState(false);
const { attachments, hasResolved } = useAttachments(attachmentIds);
const [altGenerationMap, setAltGenerationMap] = useState<AltGenerationMap>(
Expand Down Expand Up @@ -100,7 +104,7 @@ export default function BulkGenerateModal({

const task = generateAltText(
id,
context === 'mediaLibrary',
saveAltInMediaLibrary,
customPrompt,
abortController.current.signal,
)
Expand Down Expand Up @@ -166,6 +170,12 @@ export default function BulkGenerateModal({
shouldCloseOnEsc={!isGenerating}
style={{ maxWidth: '48rem' }}
>
<CustomPromptControl
value={customPrompt}
onChange={setCustomPrompt}
disabled={isGenerating}
/>

<ToggleControl
label={__(
'Overwrite existing alternative texts',
Expand All @@ -187,11 +197,13 @@ export default function BulkGenerateModal({
disabled={isGenerating}
/>

<CustomPromptControl
value={customPrompt}
onChange={setCustomPrompt}
disabled={isGenerating}
/>
{context === 'editor' && (
<SaveAltInMediaLibraryControl
checked={saveAltInMediaLibrary}
onChange={setSaveAltInMediaLibrary}
disabled={isGenerating}
/>
)}

<BulkGenerationTable
loading={hasResolved}
Expand Down
42 changes: 42 additions & 0 deletions src/components/SaveAltInMediaLibraryControl.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { ToggleControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { ComponentProps } from 'react';

interface SaveAltInMediaLibraryControlProps {
label?: string;
checked: ComponentProps<typeof ToggleControl>['checked'];
onChange: ComponentProps<typeof ToggleControl>['onChange'];
disabled?: ComponentProps<typeof ToggleControl>['disabled'];
}

const DEFAULT_LABEL = __(
'Save alt text in media library',
'alt-text-generator-gpt-vision',
);

export default function SaveAltInMediaLibraryControl({
label = DEFAULT_LABEL,
checked,
onChange,
disabled = false,
}: SaveAltInMediaLibraryControlProps) {
return (
<ToggleControl
label={label}
checked={checked}
onChange={onChange}
help={
checked
? __(
'Alternative text will be saved in the WordPress media library, making it available for reuse across site.',
'alt-text-generator-gpt-vision',
)
: __(
'Alternative text will only be saved for the current editor block.',
'alt-text-generator-gpt-vision',
)
}
disabled={disabled}
/>
);
}
2 changes: 1 addition & 1 deletion src/editor/components/GalleryBlockInspectorControls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default ({ clientId }: GalleryBlockInspectorControlsProps) => {
<Panel>
<PanelBody
title={__(
'AI Alternative Text Generator',
'Alternative Text Generator',
'alt-text-generator-gpt-vision',
)}
>
Expand Down
10 changes: 7 additions & 3 deletions src/editor/components/GenerateAltButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ interface GenerateAltButtonProps {
currentAlt?: string;
onGenerate: (alt: string) => void;
customPrompt?: string;
saveAltGlobally?: boolean;
saveAltInMediaLibrary?: boolean;
}

export default ({
imgId,
currentAlt = '',
onGenerate,
customPrompt,
saveAltGlobally = false,
saveAltInMediaLibrary = false,
}: GenerateAltButtonProps) => {
const [isGenerating, setIsGenerating] = useState(false);
const { createSuccessNotice, createErrorNotice } = useDispatch(noticesStore);
Expand All @@ -39,7 +39,11 @@ export default ({
try {
setIsGenerating(true);

const alt = await generateAltText(imgId, saveAltGlobally, customPrompt);
const alt = await generateAltText(
imgId,
saveAltInMediaLibrary,
customPrompt,
);
onGenerate(alt);

await createSuccessNotice(
Expand Down
13 changes: 11 additions & 2 deletions src/editor/components/ImageBlockInspectorControls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Panel, PanelBody } from '@wordpress/components';
import { useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import CustomPromptControl from '../../components/CustomPromptControl';
import SaveAltInMediaLibraryControl from '../../components/SaveAltInMediaLibraryControl';
import GenerateAltButton from './GenerateAltButton';

/**
Expand All @@ -13,30 +14,38 @@ export default ({
setAttributes,
}: {
attributes: ImageBlockAttrs;
setAttributes: ImageBlockSetAttrs;
setAttributes: ImageBlockProps['setAttributes'];
}) => {
if (!attributes.id) return null;

const [customPrompt, setCustomPrompt] = useState('');
const [saveAltInMediaLibrary, setSaveAltInMediaLibrary] = useState(false);

return (
<InspectorControls>
<Panel>
<PanelBody
title={__(
'AI Alternative Text Generator',
'Alternative Text Generator',
'alt-text-generator-gpt-vision',
)}
>
<CustomPromptControl
value={customPrompt}
onChange={setCustomPrompt}
/>

<SaveAltInMediaLibraryControl
checked={saveAltInMediaLibrary}
onChange={setSaveAltInMediaLibrary}
/>

<GenerateAltButton
imgId={attributes.id}
currentAlt={attributes.alt}
customPrompt={customPrompt}
onGenerate={(alt) => setAttributes({ alt })}
saveAltInMediaLibrary={saveAltInMediaLibrary}
/>
</PanelBody>
</Panel>
Expand Down

0 comments on commit 6c228f3

Please sign in to comment.