benwinding / quill-image-compress

A Quill rich text editor Module which compresses images uploaded to the editor
https://benwinding.github.io/quill-image-compress/src/demo.html
MIT License
123 stars 30 forks source link

Problem using this plugin with "quill-image-uploader" #28

Closed benwinding closed 2 years ago

benwinding commented 2 years ago

Seems to be an issue using the plugin with https://github.com/NoelOConnell/quill-image-uploader

Possibly a conflict with the configurations of each plugin.

CallumCM commented 2 years ago

I've had the same problem, when I use both plugins rather than uploading the image it just compresses the image and outputs base64. Thanks!

CallumCM commented 2 years ago

My solution was to replace the insertIntoEditor function in quill-image-compress with a custom image uploader, which I'll admit is a bit crude, but it works!

function b64toBlob(dataURI, type='image/webp') {
  const byteString = atob(dataURI.split(',')[1]);
  const ab = new ArrayBuffer(byteString.length);
  let ia = new Uint8Array(ab);
  for (var i = 0; i < byteString.length; i++) {
      ia[i] = byteString.charCodeAt(i);
  }
  return new Blob([ab], { type: 'image/webp' });
}

// Replace imageCompressor's script to just insert the base64 with a script to upload the image
imageCompressor.prototype.insertToEditor = (url) => {

  // data:image/png;base64,... is the beginning of the string, and is 22 characters long
  let file = b64toBlob(url.slice(22), url.slice(5).split(';')[0]);

  const formData = new FormData();
  formData.append("file", file);

  fetch("/upload", {method: "POST", body: formData})
    .then(response => response.text())
    .then(result => {
      const range = editor.getSelection();
      editor.insertEmbed(range.index, "image", `${result}`, "user");
    })
    .catch(error => {
      console.error("Error:", error);
    });
};
benwinding commented 2 years ago

@Turnip1234 nice work on finding a solution! 💯 Will happily merge a PR if you wanna make one for this change 👌

CallumCM commented 2 years ago

Absolutely!

benwinding commented 2 years ago

Fixed by PR #36