Skip to content

Instantly share code, notes, and snippets.

@gustav1105
Created February 16, 2020 12:54
Show Gist options
  • Save gustav1105/f0b972f3aaf4beb5d19117ac6db25bcb to your computer and use it in GitHub Desktop.
Save gustav1105/f0b972f3aaf4beb5d19117ac6db25bcb to your computer and use it in GitHub Desktop.
insert-image-to-editor
<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