I'm using Quill editor on Livewire and trying to upload inserted images on editor with Livewire JavaScript Upload API. The problem is,
I can't insert temporary url to editor. If I use $image->temporaryUrl() or $url outside of editor, image shows. I can get image temporary url. But $image->temporaryUrl() and $url not working inside the editor. And images are still uploading to livewire-tmp directory. Just can't insert temporary url to editor.
My blade file:
<script>
var quill = null;
function selectLocalImage() {
const input = document.createElement('input');
input.setAttribute('type', 'file');
input.click();
// Listen upload local image and save to server
input.onchange = () => {
const file = input.files[0];
// file type is only image.
if (/^image\//.test(file.type)) {
imageHandler(file);
} else {
console.warn('You could only upload images.');
}
};
}
function imageHandler(image) {
var formData = new FormData();
var url = '{{ $url }}';
formData.append('image', image);
formData.append('_token', '{{ csrf_token() }}');
formData.append('pageId', '{{ $page->id }}');
//let file = document.querySelector('image').files[0];
@this.upload('image', image, (uploadedFilename) => {
insertToEditor(url, quill);
})
}
function insertToEditor(url, editor) {
// push image url to rich editor.
const range = editor.getSelection();
editor.insertEmbed(range.index, 'image', url);
}
$(document).ready(function() {
var options = {
modules: {
syntax: true,
toolbar: [
[{
'font': []
}, {
'size': []
}],
['bold', 'italic', 'underline', 'strike'],
[{
'color': []
}, {
'background': []
}],
[{
'script': 'super'
}, {
'script': 'sub'
}],
[{
'header': '1'
}, {
'header': '2'
}, 'blockquote', 'code-block'],
[{
'list': 'ordered'
}, {
'list': 'bullet'
}, {
'indent': '-1'
}, {
'indent': '+1'
}],
['direction', {
'align': []
}],
['link', 'image', 'video', 'formula'],
['clean']
]
},
placeholder: 'Content...',
theme: 'snow'
};
quill = new Quill('#editor', options);
quill.getModule('toolbar').addHandler('image', () => {
selectLocalImage();
});
quill.on('text-change', function() {
@this.set('pageBody', quill.root.innerHTML);
});
});
</script>
My component:
public $image;
public $url;
public function updatedImage()
{
if ($this->image) {
$this->url = $this->image->temporaryUrl();
// dd($this->url); <---- Showing temporary url
}
}
How can I solve this problem?