PluginProbe
Extendify / 3.0.4
Extendify v3.0.4
3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 All 127 releases
extendify / src / Draft / components / image-generation / GenerateForm.jsx

GenerateForm.jsx in Extendify 3.0.4, at src/Draft/components/image-generation/GenerateForm.jsx

131 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { CreditCounter } from '@draft/components/image-generation/CreditCounter';
2 import { useImageGenerationStore } from '@shared/state/generate-images';
3 import {
4 Button,
5 TextareaControl,
6 __experimentalToggleGroupControl as ToggleGroupControl,
7 __experimentalToggleGroupControlOptionIcon as ToggleGroupControlOptionIcon,
8 } from '@wordpress/components';
9 import { useEffect, useState } from '@wordpress/element';
10 import { __ } from '@wordpress/i18n';
11
12 export const GenerateForm = ({ isGenerating, errorMessage }) => {
13 const { imageCredits, resetImageCredits, aiImageOptions, setAiImageOption } =
14 useImageGenerationStore();
15 const usedCredits = imageCredits.total - imageCredits.remaining;
16 const [refreshCheck, setRefreshCheck] = useState(0);
17 const { size, prompt } = aiImageOptions;
18
19 useEffect(() => {
20 const handle = () => {
21 setRefreshCheck((prev) => prev + 1);
22 if (!imageCredits.refresh) return;
23 if (new Date(Number(imageCredits.refresh)) > new Date()) return;
24 resetImageCredits();
25 };
26 if (refreshCheck === 0) handle(); // First run
27 const id = setTimeout(handle, 1000);
28 return () => clearTimeout(id);
29 }, [imageCredits, resetImageCredits, refreshCheck]);
30
31 return (
32 <>
33 {isGenerating ? null : (
34 <div>
35 <TextareaControl
36 id="draft-ai-image-textarea"
37 autoFocus
38 placeholder={__(
39 'Tell AI about the image you would like to create',
40 'extendify-local',
41 )}
42 label={__('Image Prompt', 'extendify-local')}
43 hideLabelFromVision
44 rows="7"
45 value={prompt}
46 onChange={(prompt) => setAiImageOption('prompt', prompt)}
47 />
48
49 <ToggleGroupControl
50 isBlock
51 label={__('Aspect Ratio', 'extendify-local')}
52 onChange={(size) => setAiImageOption('size', size)}
53 value={size}
54 >
55 <ToggleGroupControlOptionIcon
56 className="m-auto"
57 type="button"
58 icon={AspectRatioSquare}
59 label={__('Square: 1:1', 'extendify-local')}
60 value="1024x1024"
61 />
62 <ToggleGroupControlOptionIcon
63 className="m-auto"
64 type="button"
65 icon={AspectRatioLandscape}
66 label={__('Landscape: 4:3', 'extendify-local')}
67 value="1536x1024"
68 />
69 <ToggleGroupControlOptionIcon
70 className="m-auto"
71 type="button"
72 icon={AspectRatioPortrait}
73 label={__('Portrait: 3:4', 'extendify-local')}
74 value="1024x1536"
75 />
76 </ToggleGroupControl>
77 </div>
78 )}
79 {errorMessage.length > 0 && (
80 <p className="mb-0 text-red-500">{errorMessage}</p>
81 )}
82 <Button
83 type="submit"
84 className="w-full justify-center"
85 variant="primary"
86 __next40pxDefaultSize
87 disabled={isGenerating || !prompt || usedCredits >= imageCredits.total}
88 >
89 {isGenerating
90 ? __('Generating image...', 'extendify-local')
91 : __('Generate image', 'extendify-local')}
92 </Button>
93 {isGenerating ? null : (
94 <CreditCounter usedCredits={usedCredits} total={imageCredits.total} />
95 )}
96 </>
97 );
98 };
99
100 const AspectRatioLandscape = (
101 <svg xmlns="http://www.w3.org/2000/svg" style={{ padding: '7px 4px' }}>
102 <title>{__('Landscape: 4:3', 'extendify-local')}</title>
103 <path
104 fillRule="evenodd"
105 d="M0 1c0-.552285.447715-1 1-1h14c.5523 0 1 .447715 1 1v8c0 .55228-.4477 1-1 1H1c-.552285 0-1-.44772-1-1V1Z"
106 clipRule="evenodd"
107 />
108 </svg>
109 );
110
111 const AspectRatioPortrait = (
112 <svg xmlns="http://www.w3.org/2000/svg" style={{ padding: '4px 6px' }}>
113 <title>{__('Portrait: 3:4', 'extendify-local')}</title>
114 <path
115 fillRule="evenodd"
116 d="M9.66669 3.5e-7C10.219 3.7e-7 10.6667.447716 10.6667 1v14c0 .5523-.4477 1-1.00001 1h-8c-.55229 0-1.000003-.4477-1.000003-1L.666688 1C.666688.447715 1.1144-2e-8 1.66669 0l8 3.5e-7Z"
117 clipRule="evenodd"
118 />
119 </svg>
120 );
121 const AspectRatioSquare = (
122 <svg xmlns="http://www.w3.org/2000/svg" style={{ padding: '6px' }}>
123 <title>{__('Square: 1:1', 'extendify-local')}</title>
124 <path
125 fillRule="evenodd"
126 d="M11.3333-4e-8c.5523 2e-8 1 .44771504 1 1.00000004v10c0 .5523-.4477 1-1 1H1.33333c-.552283 0-.999998-.4477-.999998-1V.999999C.333332.447715.781047-5e-7 1.33333-4.8e-7L11.3333-4e-8Z"
127 clipRule="evenodd"
128 />
129 </svg>
130 );
131