0

Im trying to implement upload image to server functionality in my react-quill component, and i get errors: quill.getSelection is not a function

My function to handle image uploads looks like this:

  const handleUploadImage = () => {
    const input = document.createElement("input");

    input.setAttribute("type", "file");
    input.setAttribute("accept", "image/*");
    input.click();

    input.onchange = async () => {
      const file = input.files[0];
      const formData = new FormData();

      formData.append("image", file);
      const range = quill.getSelection(true);
      quill.setSelection(range.index + 1);
      const res = await HttpClient().post("/api/blog/images", formData); // API post, returns image location as string e.g. 'http://www.example.com/images/foo.png'

      if (res.status === 201) {
        quill.deleteText(range.index, 1);
        quill.insertEmbed(range.index, "image", res.data);
      }
    };
  };

The problem is that quill, which is a ref, doesnt get the full api, when i console.log quill.current, only a few functions. How do I get the full API?

Mike
  • 23,542
  • 14
  • 76
  • 87
user14850280
  • 389
  • 3
  • 16

1 Answers1

1

I think you're looking for quil.current.getEditor():

getEditor() : Returns the Quill instance that backs the editor.

https://github.com/zenoamaro/react-quill#methods


So you can call getSelection on this Quil instance like this:

quil.current.getEditor().getSelection()

Simplified sandbox example

5eb
  • 14,798
  • 5
  • 21
  • 65