@lew
To add an image from your computer in Quill JS, you can use the following steps:
Here is an example code snippet showing how you can achieve this:
HTML:
1 2 3 4 5 6 7 8 9 |
<div id="toolbar"> <button id="insertImage">Insert Image</button> </div> <div id="editor"> <p>Start typing here...</p> </div> <input type="file" id="imageInput" style="display: none"> |
JavaScript:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
var toolbar = document.getElementById('toolbar'); var editor = document.getElementById('editor'); var imageInput = document.getElementById('imageInput'); var quill = new Quill('#editor', { theme: 'snow' }); var insertImageButton = document.getElementById('insertImage'); insertImageButton.addEventListener('click', function() { imageInput.click(); }); imageInput.addEventListener('change', function() { var file = imageInput.files[0]; if (file) { var reader = new FileReader(); reader.onload = function(e) { var range = quill.getSelection(); quill.insertEmbed(range.index, 'image', e.target.result); } reader.readAsDataURL(file); } }); |
In this example, when the "Insert Image" button is clicked, it will trigger a click event on the hidden file input element. The user can then select an image from their computer, which will be inserted into the Quill editor at the current cursor position.