| 1 |
If you'd like to integrate the dropzone with the [Pintura](https://pqina.nl/pintura/?ref=react-dropzone) image editor, you just need to pass either of the selected images to the `openDefaultEditor()` method exported by Pintura: |
| 2 |
|
| 3 |
```jsx static |
| 4 |
import React, { useState, useEffect } from 'react'; |
| 5 |
|
| 6 |
// React Dropzone |
| 7 |
import { useDropzone } from 'react-dropzone'; |
| 8 |
|
| 9 |
// Pintura Image Editor |
| 10 |
import 'pintura/pintura.css'; |
| 11 |
import { openDefaultEditor } from 'pintura'; |
| 12 |
|
| 13 |
// Based on the default React Dropzone image thumbnail example |
| 14 |
// The `thumbButton` style positions the edit button in the bottom right corner of the thumbnail |
| 15 |
const thumbsContainer = { |
| 16 |
display: 'flex', |
| 17 |
flexDirection: 'row', |
| 18 |
flexWrap: 'wrap', |
| 19 |
marginTop: 16, |
| 20 |
padding: 20, |
| 21 |
}; |
| 22 |
|
| 23 |
const thumb = { |
| 24 |
position: 'relative', |
| 25 |
display: 'inline-flex', |
| 26 |
borderRadius: 2, |
| 27 |
border: '1px solid #eaeaea', |
| 28 |
marginBottom: 8, |
| 29 |
marginRight: 8, |
| 30 |
width: 100, |
| 31 |
height: 100, |
| 32 |
padding: 4, |
| 33 |
boxSizing: 'border-box', |
| 34 |
}; |
| 35 |
|
| 36 |
const thumbInner = { |
| 37 |
display: 'flex', |
| 38 |
minWidth: 0, |
| 39 |
overflow: 'hidden', |
| 40 |
}; |
| 41 |
|
| 42 |
const img = { |
| 43 |
display: 'block', |
| 44 |
width: 'auto', |
| 45 |
height: '100%', |
| 46 |
}; |
| 47 |
|
| 48 |
const thumbButton = { |
| 49 |
position: 'absolute', |
| 50 |
right: 10, |
| 51 |
bottom: 10, |
| 52 |
}; |
| 53 |
|
| 54 |
// This function is called when the user taps the edit button. |
| 55 |
// It opens the editor and returns the modified file when done |
| 56 |
const editImage = (image, done) => { |
| 57 |
const imageFile = image.pintura ? image.pintura.file : image; |
| 58 |
const imageState = image.pintura ? image.pintura.data : {}; |
| 59 |
|
| 60 |
const editor = openDefaultEditor({ |
| 61 |
src: imageFile, |
| 62 |
imageState, |
| 63 |
}); |
| 64 |
|
| 65 |
editor.on('close', () => { |
| 66 |
// the user cancelled editing the image |
| 67 |
}); |
| 68 |
|
| 69 |
editor.on('process', ({ dest, imageState }) => { |
| 70 |
Object.assign(dest, { |
| 71 |
pintura: { file: imageFile, data: imageState }, |
| 72 |
}); |
| 73 |
done(dest); |
| 74 |
}); |
| 75 |
}; |
| 76 |
|
| 77 |
function App() { |
| 78 |
const [files, setFiles] = useState([]); |
| 79 |
const { getRootProps, getInputProps } = useDropzone({ |
| 80 |
accept: { |
| 81 |
'image/*': [], |
| 82 |
}, |
| 83 |
onDrop: (acceptedFiles) => { |
| 84 |
setFiles( |
| 85 |
acceptedFiles.map((file) => |
| 86 |
Object.assign(file, { |
| 87 |
preview: URL.createObjectURL(file), |
| 88 |
}) |
| 89 |
) |
| 90 |
); |
| 91 |
}, |
| 92 |
}); |
| 93 |
|
| 94 |
const thumbs = files.map((file, index) => ( |
| 95 |
<div style={thumb} key={file.name}> |
| 96 |
<div style={thumbInner}> |
| 97 |
<img src={file.preview} style={img} alt="" /> |
| 98 |
</div> |
| 99 |
<button |
| 100 |
style={thumbButton} |
| 101 |
onClick={() => |
| 102 |
editImage(file, (output) => { |
| 103 |
const updatedFiles = [...files]; |
| 104 |
|
| 105 |
// replace original image with new image |
| 106 |
updatedFiles[index] = output; |
| 107 |
|
| 108 |
// revoke preview URL for old image |
| 109 |
if (file.preview) URL.revokeObjectURL(file.preview); |
| 110 |
|
| 111 |
// set new preview URL |
| 112 |
Object.assign(output, { |
| 113 |
preview: URL.createObjectURL(output), |
| 114 |
}); |
| 115 |
|
| 116 |
// update view |
| 117 |
setFiles(updatedFiles); |
| 118 |
}) |
| 119 |
} |
| 120 |
> |
| 121 |
Edit |
| 122 |
</button> |
| 123 |
</div> |
| 124 |
)); |
| 125 |
|
| 126 |
useEffect( |
| 127 |
() => () => { |
| 128 |
// Make sure to revoke the Object URL to avoid memory leaks |
| 129 |
files.forEach((file) => URL.revokeObjectURL(file.preview)); |
| 130 |
}, |
| 131 |
[files] |
| 132 |
); |
| 133 |
|
| 134 |
return ( |
| 135 |
<section className="container"> |
| 136 |
<div {...getRootProps({ className: 'dropzone' })}> |
| 137 |
<input {...getInputProps()} /> |
| 138 |
<p>Drag 'n' drop some files here, or click to select files</p> |
| 139 |
</div> |
| 140 |
<aside style={thumbsContainer}>{thumbs}</aside> |
| 141 |
</section> |
| 142 |
); |
| 143 |
} |
| 144 |
|
| 145 |
export default App; |
| 146 |
``` |