(props: BlockProps)
| 17 | */ |
| 18 | |
| 19 | export const ImageBlock = (props: BlockProps) => { |
| 20 | const { |
| 21 | blockId, |
| 22 | } = props; |
| 23 | const { blocks, updateBlock, selectedBlockId, setSelectedBlockId, removeBlock } = useBlocksContext(); |
| 24 | |
| 25 | const [source, setSource] = useState(blocks[blockId]?.properties.source || null); |
| 26 | const [aspectRatio, setAspectRatio] = useState(blocks[blockId]?.format?.block_aspect_ratio || null); |
| 27 | |
| 28 | const pickImage = async () => { |
| 29 | let result = await ImagePicker.launchImageLibraryAsync({ |
| 30 | mediaTypes: ['images'], |
| 31 | allowsEditing: false, |
| 32 | quality: 1, |
| 33 | }); |
| 34 | |
| 35 | if (!result.canceled) { |
| 36 | setSource(result.assets[0].uri); |
| 37 | setAspectRatio(result.assets[0].width / result.assets[0].height); |
| 38 | |
| 39 | const updatedBlock = updateBlockData(blocks[blockId], { |
| 40 | properties: { |
| 41 | source: result.assets[0].uri |
| 42 | }, |
| 43 | format: { |
| 44 | block_width: result.assets[0].width, |
| 45 | block_aspect_ratio: result.assets[0].width / result.assets[0].height |
| 46 | } |
| 47 | }); |
| 48 | |
| 49 | updateBlock(updatedBlock); |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | const handleRemoveBlock = () => { |
| 54 | setSelectedBlockId(null); |
| 55 | setTimeout(() => { |
| 56 | removeBlock(blockId); |
| 57 | }, 100); |
| 58 | }; |
| 59 | |
| 60 | const backgroundColor = source === null ? "#7d7a751d" : "transparent"; |
| 61 | |
| 62 | return ( |
| 63 | <DragProvider blockId={blockId}> |
| 64 | <Pressable |
| 65 | style={[styles.container, { backgroundColor: backgroundColor }]} |
| 66 | onPress={pickImage} |
| 67 | > |
| 68 | {source === null |
| 69 | ? ( |
| 70 | <View style={styles.row}> |
| 71 | <Ionicons name="image-outline" size={24} color="#7d7a75" /> |
| 72 | <Text style={styles.text}>Add an image</Text> |
| 73 | </View> |
| 74 | ) |
| 75 | : ( |
| 76 | <Image |
nothing calls this directly
no test coverage detected