Skip to content
This repository has been archived by the owner on May 24, 2021. It is now read-only.

How to insert image with custom text? #191

Open
lutaev opened this issue Dec 16, 2019 · 6 comments
Open

How to insert image with custom text? #191

lutaev opened this issue Dec 16, 2019 · 6 comments

Comments

@lutaev
Copy link

lutaev commented Dec 16, 2019

Hi there.
I'm working on extending of you amazing library. It would be pleasure if you help me with understanding how inserting images working. So, we have in addButton.js:

insertImage =(file)=> {
  if(!file)
    return

  let opts = {
    url: URL.createObjectURL(file),
  }

  // cleans input image value
  this.fileInput.value = ""

  return this.props.onChange(addNewBlock(this.props.editorState, 'image', opts))
}

I just want to add my custom text to be initialized with image, so, I have to add it to opts object:

let opts = {
  url: URL.createObjectURL(file),
  text: 'This is my caption text'
}

But if I do it this way, there is no text at all. I also checked image.js file and found this string rendering image caption:

<EditorBlock {...editorProps}/>

How should new EditorBlock be initialized to have text?
Thanks.

@michelson
Copy link
Owner

michelson commented Dec 16, 2019 via email

@lutaev
Copy link
Author

lutaev commented Dec 16, 2019

Thanks for answer. I already tried updateTextOfBlock but the result was the same - no caption text at all after rendering. I also tried another function - addNewBlockAt - with the same result.

@michelson
Copy link
Owner

michelson commented Dec 16, 2019 via email

@lutaev
Copy link
Author

lutaev commented Dec 17, 2019

Yes, I did it within the onChange method. Here is full code of method:

insertImage =(file)=> {
    if(!file)
      return

    let opts = {
      url: URL.createObjectURL(file),
    }

    // cleans input image value
    this.fileInput.value = ""

    this.props.onChange(addNewBlock(this.props.editorState, 'image', opts))
    setTimeout(() => {
      const { editorState } = this.props
      const currentBlock = getCurrentBlock(editorState)
      this.props.onChange(updateTextOfBlock(editorState, currentBlock, 'My caption'))
    }, 100)
  }

I see only image, but not text, editor is empty. According to draftjs, there is no leaves inside editor, possibly this fact will make things clearer.

Thanks again for your patience.

@michelson
Copy link
Owner

Hi @lutaev ,
I've added a branch in which I've implemented the custom caption on image added. As you noted the updateTextOfBlock does not work, I've updated the function with the latest api Modifier

check it out at, https://github.com/michelson/dante2/tree/default-image-caption

I guess it would be a good starting point

best

@lutaev
Copy link
Author

lutaev commented Dec 20, 2019

Thank you very much. I'll check it.

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

No branches or pull requests

2 participants