Created
February 16, 2020 12:54
-
-
Save gustav1105/f0b972f3aaf4beb5d19117ac6db25bcb to your computer and use it in GitHub Desktop.
insert-image-to-editor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<input | |
id="fileInput" | |
style={{ display: "none" }} | |
type="file" | |
accept="image/png,image/jpeg,image/jpg,image/gif" | |
onChange={event => { | |
const reader = new FileReader(); | |
reader.addEventListener( | |
"load", | |
function() { | |
const contentStateWithEntity = editorState | |
.getCurrentContent() | |
.createEntity("IMAGE", "IMMUTABLE", { src: reader.result }); | |
setEditorState( | |
AtomicBlockUtils.insertAtomicBlock( | |
EditorState.set(editorState, { | |
currentContent: contentStateWithEntity | |
}), | |
contentStateWithEntity.getLastCreatedEntityKey(), | |
" " | |
) | |
); | |
}, | |
false | |
); | |
if (event.target.files) { | |
reader.readAsDataURL( | |
Array.prototype.slice.call(event.target.files)[0] | |
); | |
} | |
}} | |
/> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment