可以从 URL 将图像添加到 quill JS 编辑器中,但找不到像所有传统富文本编辑器那样从计算机添加图像的方法。
有什么方法可以达到这个目的吗?
最佳答案
您可以执行以下操作:
quill.getModule("toolbar").addHandler("image", () => {
this.selectLocalImage();
});
function selectLocalImage() {
var 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)) {
this.saveToServer(file, "image");
} else {
console.warn("Only images can be uploaded here.");
}
};
}
function saveToServer(file) {
// Upload file to server and get the uploaded file url in response then call insertToEditor(url) after getting the url from server
}
function insertToEditor(url) {
// push image url to editor.
const range = quill.getSelection();
quill.insertEmbed(range.index, "image", url);
}
关于javascript - 如何在 Quill JS 中添加来自计算机的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38006008/