PluginProbe
Extendify / 2.2.0
Extendify v2.2.0
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 2.2.0, at src/Draft/components/image-generation/ImagePreview.jsx

100 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { store as blockEditorStore } from '@wordpress/block-editor';
2 import { Button, Spinner } from '@wordpress/components';
3 import { useSelect, useDispatch } from '@wordpress/data';
4 import { store as editPostStore } from '@wordpress/edit-post';
5 import { useState } from '@wordpress/element';
6 import { __ } from '@wordpress/i18n';
7 import { downloadImage } from '@shared/api/wp';
8 import { AnimatePresence, motion } from 'framer-motion';
9 import { addImageToBlock } from '@draft/api/WPApi';
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 <Spinner style={{ height: '48px', width: '48px' }} />
55 </motion.div>
56 ) : (
57 <motion.div
58 initial={{ opacity: 0 }}
59 animate={{ opacity: 1 }}
60 className="bg-gray-100"
61 style={{ aspectRatio: Number(imgWidth) / Number(imgHeight) }}>
62 <img
63 alt={prompt}
64 src={src}
65 className="block w-full"
66 style={{ aspectRatio: Number(imgWidth) / Number(imgHeight) }}
67 />
68 </motion.div>
69 )}
70 </AnimatePresence>
71 {isGenerating ? (
72 <p>
73 {__('Generating your image: ', 'extendify-local')}
74 <span className="font-bold">&quot;{prompt}&quot;</span>
75 </p>
76 ) : (
77 <form onSubmit={handleInsert} className="flex flex-col gap-5">
78 <Button
79 type="submit"
80 autoFocus
81 className="w-full justify-center"
82 variant="primary"
83 disabled={isInserting}>
84 {isInserting
85 ? // translators: "Importing image" means the image is being added to the WordPress post editor
86 __('Importing image...', 'extendify-local')
87 : __('Use this image', 'extendify-local')}
88 </Button>
89 <Button
90 className="w-full justify-center bg-gray-200 text-gray-800 disabled:bg-gray-300 disabled:text-gray-700"
91 onClick={clearImageResponse}
92 disabled={isInserting}>
93 {__('Delete image', 'extendify-local')}
94 </Button>
95 </form>
96 )}
97 </div>
98 );
99 };
100