-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Support paste image #474
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support paste image #474
Conversation
Oh, there is a lint problem, I'll fix it later. |
This is pretty cool! People might paste in other media formats and other plugins might capture that. Can you remove the console.error statements and just return. |
I made it. |
@whitejava I looked into the current implementation of the image plugin and it wasn't even using Atomic blocks. I was refactoring it in a branch to Atomic blocks. I hope to push & merge this soon and then we can apply your change to the new stuff. |
I'm not sure what is Atomic blocks. |
@whitejava I think in you PR you are inserting atomic block which has block type 'atomic', but in you blockrenderFn you are checking block type 'block-image' |
how to do... |
# Conflicts: # draft-js-image-plugin/src/index.js
# Conflicts: # draft-js-image-plugin/src/index.js
when support paste image? thanks |
@whitejava we'd like to keep the image plugin functionality simple. But it'd be interesting to have a handle paste plugin which could handle use-cases like this, ping me on the draft-js slack if you want to talk about this! |
I don't know whether you have solved it. Here is my way to paste image to editor. At first add a paste event handler to document and let editor to record the focus state. import { pasteAndUploadImage } from './imageUtil';
...
document.addEventListener('paste', this.handleOnPaste, false);
handleOnPaste = e => {
if (this.isFocus) {
pasteAndUploadImage(e, this.handleOnPasteAndUploadImage)
this.props.onPaste(e);
}
};
...
<Editor onFocus={() => this.isFocus = true} onBlur={() => this.isFocus = false} /> Then use the methods from imageUtil to insert image. // imageUtil.js
import { EditorState, AtomicBlockUtils } from 'draft-js';
import axios from 'axios';
import * as EntityType from '../constants/entity';
const imageURLKeyMap = {};
export const addImage = (editorState, url, extraData) => {
const contentState = editorState.getCurrentContent();
const contentStateWithEntity = contentState.createEntity(
EntityType.IMAGE,
'IMMUTABLE',
{ ...extraData, src: url }
);
const entityKey = contentStateWithEntity.getLastCreatedEntityKey();
imageURLKeyMap[url] = entityKey;
const newEditorState = AtomicBlockUtils.insertAtomicBlock(editorState, entityKey, ' ');
return newEditorState;
};
export const updateImage = (editorState, toMergeData, localURL) => {
const contentState = editorState.getCurrentContent();
const entityKey = imageURLKeyMap[localURL];
const nextContentState = contentState.mergeEntityData(entityKey, toMergeData);
delete imageURLKeyMap[localURL];
return EditorState.push(editorState, nextContentState);
};
export const uploadImage = (url, file, config = {}) => {
const data = new FormData();
data.append('file', file);
return axios
.post(url, data, config)
.then(res => {
return res;
})
.catch(console.error);
};
// here is the paste method
export const pasteAndUploadImage = (event, onload) => {
const items = (event.clipboardData || event.originalEvent.clipboardData).items;
for (let item of items) {
if (item.kind === 'file' && item.type.includes('image/')) {
const blob = item.getAsFile();
const reader = new FileReader();
reader.onload = e => onload(blob, e);
reader.readAsDataURL(blob);
}
}
}; It works well with paste screen shots, however, when you copy a image file from your computer, it will at first paste the file name and then the image. That seems I need to spend some time to handle those file names. had somebody already solved this? Thx. |
Add my resolve problem anwser: first : source code position handlePastedFiles second: editor you can add customer handler see commit: hope to be able to help you. |
No description provided.