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 / ImagePreview.jsx

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

104 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { addImageToBlock } from '@draft/api/WPApi';
2 import { downloadImage } from '@shared/api/wp';
3 import { store as blockEditorStore } from '@wordpress/block-editor';
4 import { Button, Spinner } from '@wordpress/components';
5 import { useDispatch, useSelect } from '@wordpress/data';
6 import { store as editPostStore } from '@wordpress/edit-post';
7 import { useState } from '@wordpress/element';
8 import { __ } from '@wordpress/i18n';
9 import { AnimatePresence, motion } from 'framer-motion';
10
11 export const ImagePreview = ({
12 prompt,
13 size,
14 isGenerating,
15 id,
16 src,
17 clearImageResponse,
18 }) => {
19 const { openGeneralSidebar } = useDispatch(editPostStore);
20 const { updateBlockAttributes } = useDispatch(blockEditorStore);
21 const [isInserting, setIsInserting] = useState(false);
22 const selectedBlock = useSelect(
23 (select) => select(blockEditorStore).getSelectedBlock(),
24 [],
25 );
26 const [imgWidth, imgHeight] = size.split('x');
27
28 const handleInsert = async (event) => {
29 event.preventDefault();
30 setIsInserting(true);
31 const image = await downloadImage(id, src, 'ai-generated');
32 if (!image) return;
33
34 await addImageToBlock(selectedBlock, image, updateBlockAttributes);
35 setIsInserting(false);
36 openGeneralSidebar('edit-post/block');
37 clearImageResponse();
38 };
39
40 if (src === '' && !isGenerating) return null;
41
42 return (
43 <div className="flex flex-col gap-5">
44 <AnimatePresence>
45 {isGenerating ? (
46 <motion.div
47 initial={{ opacity: 1 }}
48 exit={{ opacity: 0 }}
49 className="flex aspect-square w-full items-center justify-center"
50 style={{
51 background:
52 'linear-gradient(135deg, #E8E8E8 47.92%, #F3F3F3 60.42%, #E8E8E8 72.92%)',
53 }}
54 >
55 <Spinner style={{ height: '48px', width: '48px' }} />
56 </motion.div>
57 ) : (
58 <motion.div
59 initial={{ opacity: 0 }}
60 animate={{ opacity: 1 }}
61 className="bg-gray-100"
62 style={{ aspectRatio: Number(imgWidth) / Number(imgHeight) }}
63 >
64 <img
65 alt={prompt}
66 src={src}
67 className="block w-full"
68 style={{ aspectRatio: Number(imgWidth) / Number(imgHeight) }}
69 />
70 </motion.div>
71 )}
72 </AnimatePresence>
73 {isGenerating ? (
74 <p>
75 {__('Generating your image: ', 'extendify-local')}
76 <span className="font-bold">&quot;{prompt}&quot;</span>
77 </p>
78 ) : (
79 <form onSubmit={handleInsert} className="flex flex-col gap-5">
80 <Button
81 type="submit"
82 autoFocus
83 className="w-full justify-center"
84 variant="primary"
85 disabled={isInserting}
86 >
87 {isInserting
88 ? // translators: "Importing image" means the image is being added to the WordPress post editor
89 __('Importing image...', 'extendify-local')
90 : __('Use this image', 'extendify-local')}
91 </Button>
92 <Button
93 className="w-full justify-center bg-gray-200 text-gray-800 disabled:bg-gray-300 disabled:text-gray-700"
94 onClick={clearImageResponse}
95 disabled={isInserting}
96 >
97 {__('Delete image', 'extendify-local')}
98 </Button>
99 </form>
100 )}
101 </div>
102 );
103 };
104