Skip to content

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

Closed
wants to merge 7 commits into from
Closed

Conversation

whitejava
Copy link

No description provided.

@whitejava whitejava mentioned this pull request Oct 8, 2016
@whitejava
Copy link
Author

whitejava commented Oct 8, 2016

Oh, there is a lint problem, I'll fix it later.

@nikgraf
Copy link
Member

nikgraf commented Oct 9, 2016

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.

@whitejava
Copy link
Author

I made it.

@nikgraf
Copy link
Member

nikgraf commented Oct 14, 2016

@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.

@whitejava
Copy link
Author

I'm not sure what is Atomic blocks.
What is the problem?
What should I do next?

@mzbac
Copy link
Contributor

mzbac commented Oct 16, 2016

@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'

@whitejava
Copy link
Author

how to do...

caipeichao added 2 commits November 23, 2016 22:37
# Conflicts:
#	draft-js-image-plugin/src/index.js
# Conflicts:
#	draft-js-image-plugin/src/index.js
@the1sky
Copy link

the1sky commented Nov 28, 2016

when support paste image? thanks

@juliankrispel
Copy link
Member

@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!

@lcc19941214
Copy link

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.

@codingwesley
Copy link

Add my resolve problem anwser:

first : source code position handlePastedFiles

second: editor you can add customer handler

see commit:
codingwesley/qtext@585b78e#diff-93bc859637a9b7854dd52f54137379c9

hope to be able to help you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants