gpt4 book ai didi

javascript - 如何在 Quill JS 中添加来自计算机的图像?

转载 作者:行者123 更新时间:2023-11-28 18:31:03 25 4
gpt4 key购买 nike

可以从 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com