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

标签 javascript quill

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

相关文章:

javascript - 使用 jQuery 动态显示多个内容

angular - 羽毛笔编辑器中的光标位置

javascript - 识别 Quill.JS 中的新行

javascript - 什么、如何以及何时在自定义印迹上使用静态格式、格式和格式?

javascript - 如何在羽毛笔编辑器字体颜色菜单中添加我自己的颜色列表?

javascript - 单击按钮更改div

javascript - 当行为 :smooth 时,去抖不适用于 js scrollIntoView

angular - 带有 Angular 的 Quill 表,不适用于编辑模式

javascript - 避免 ontextchange 事件中的回传

javascript - 在 fetch 中添加 catch 时出错