#Help with Quill Editor. I need help making the image button add a figure element instead of an img.
5 messages · Page 1 of 1 (latest)
Create a custom module that listens to the click event of the image button and use the File API or a file input element to get the image file from the user.
then show a prompt or a form to get the caption from the user.
Use the quill.getModule('formats').insertEmbed(range.index, 'figure', {'url': URL.createObjectURL(file), 'caption': caption}); method to insert the figure element with the image URL and caption into the editor at the current cursor position.
Thank for the reply, I am trying to implement the module like you said but not having any luck. This is my jsfiddle: https://jsfiddle.net/g04wLcme/
Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle code editor.
class ImageModule extends Module {
constructor(quill, options) {
super(quill, options);
this.quill = quill;
this.options = options;
this.handleClick = this.handleClick.bind(this);
}
attach(button) {
button.addEventListener('click', this.handleClick);
}
handleClick() {
const input = document.createElement('input');
input.setAttribute('type', 'file');
input.click();
input.onchange = async () => {
const file = input.files[0];
const caption = window.prompt('Enter Image Caption:');
const range = this.quill.getSelection();
this.quill.getModule('formats').insertEmbed(range.index, 'figure', {'url': URL.createObjectURL(file), 'caption': caption});
};
}
}
Quill.register('modules/image', ImageModule);