tinymce6 pasteuploadimage插件

這是由tinymce-pastewordimage改變而來,原版只能在粘貼word的圖片時圖片文件本地化,現(xiàn)加入了復(fù)制網(wǎng)頁內(nèi)容時圖片文件也本地化
tinymce-pastewordimage插件地址:hanghang2/tinymce-pastewordimage (github.com)
繞過防盜鏈參考網(wǎng)站:javascript - Referer和Referrer Policy及圖片防盜鏈 - 個人文章 - SegmentFault 思否

注意:該插件需要配置好images_upload_handler屬性
獲取htmlContent時,不同的框架獲取方式好像不一樣,我這里用的是vue3,請各自測試獲取方式

(function () {
    const global = window.tinymce.util.Tools.resolve('tinymce.PluginManager');
    const base64ToFile = (dataurl, filename = 'wps_office') => {
        const arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1], bstr = atob(arr[1]);
        let n = bstr.length;
        const u8arr = new Uint8Array(n);
        while (n--) {
            u8arr[n] = bstr.charCodeAt(n)
        }
        return new File([u8arr], filename, {type: mime})
    };
    const hexToBase64 = hexString => {
        return btoa(hexString.match(/\w{2}/g).map(char => {
            return String.fromCharCode(parseInt(char, 16))
        }).join(''))
    };

    function extractImageDataFromRtf(rtfData) {
        if (!rtfData) {
            return []
        }
        let regexPictureHeader = /{\\pict[\s\S]+?\\bliptag-?\d+(\\blipupi-?\d+)?({\\\*\\blipuid\s?[\da-fA-F]+)?[\s}]*?/;
        let regexPicture = new RegExp(`(?:(${regexPictureHeader.source}))([\\da-fA-F\\s]+)\\}`, 'g');
        let images = rtfData.match(regexPicture);
        const result = [];
        if (!images) {
            regexPictureHeader = /{\\pict[\s\S]+?(\\pngblip-?\d+)?(\\wmetafile8-?\d+)?{\\\*\\blipuid\s?[\da-fA-F]+[\s}]*?/;
            regexPicture = new RegExp(`(?:(${regexPictureHeader.source}))([\\da-fA-F\\s]+)\\}`, 'g');
            images = rtfData.match(regexPicture)
        }
        if (images) {
            for (const image of images) {
                let imageType = false;
                if (image.includes('\\pngblip')) {
                    imageType = 'image/png'
                } else if (image.includes('\\jpegblip')) {
                    imageType = 'image/jpeg'
                }
                if (imageType) {
                    const hex = image.replace(regexPictureHeader, '').replace(/[^\da-fA-F]/g, '');
                    const base64 = `data:${imageType};base64,${hexToBase64(hex)}`;
                    result.push({base64, file: base64ToFile(base64)})
                }
            }
        }
        return result
    }

    function getBase64Image(img, callback) {
        const image = new Image();
        image.crossOrigin = 'anonymous'; // 跨域
        image.referrerPolicy = 'no-referrer'; //繞過防盜鏈
        image.onload = () => {
            const canvas = document.createElement('canvas');
            canvas.useCORS = true;
            const ctx = canvas.getContext('2d');
            canvas.height = image.naturalHeight;
            canvas.width = image.naturalWidth;
            ctx.drawImage(image, 0, 0);
            const dataUrl = canvas.toDataURL();
            callback && callback(dataUrl)
        }
        image.onerror = (error) => {
            console.log(error)
            callback && callback()
        }
        image.src = img.src;
    }

    const getHtmlImgFiles = (e, editor) => {
        let htmlContent = e.clipboardData.getData('text/html');
        if (htmlContent) {
            let element = document.createElement('div');
            element.innerHTML = htmlContent;
            const imgElements = element.querySelectorAll('img');
            if (imgElements && imgElements.length > 0) {
                for (let i = 0; i < imgElements.length; i++) {
                    let imgEl = imgElements[i];
                    if (imgEl.src.startsWith('http') || imgEl.src.startsWith('ftp')) {
                        getBase64Image(imgEl, (base64) => {
                            if (base64) {
                                imgEl.setAttribute("src", base64);
                            }
                            if (i === imgElements.length - 1) {
                                editor.insertContent(element.innerHTML)
                            }
                        })
                    } else {
                        if (i === imgElements.length - 1) {
                            editor.insertContent(element.innerHTML)
                        }
                    }
                }
                e.preventDefault();
            }
        }
    }

    const getImgFiles = (e) => {
        if (e.clipboardData || e.originalEvent) {
            var clipboardData = (e.clipboardData || e.originalEvent.clipboardData);
            if (clipboardData.items) {
                var items = clipboardData.items, len = items.length;
                for (var i = 0; i < len; i++) {
                    if (items[i].type.indexOf('text/rtf') !== -1) {
                        return extractImageDataFromRtf(clipboardData.getData('text/rtf'))
                    }
                }
            }
        }
    };

    const Plugin = () => {
        global.add('pasteuploadimage', editor => {
            let imgs = [], index = 0;

            editor.on('paste', (e) => {
                imgs = [];
                index = 0;
                const blobInfo = getImgFiles(e);
                if (blobInfo && blobInfo.length) {
                    imgs = blobInfo
                }
                // 復(fù)制網(wǎng)頁內(nèi)容,圖片本地化
                getHtmlImgFiles(e, editor)
            });
            editor.on('init', () => {
                editor.parser.addNodeFilter('img', (nodes) => {
                    if (!nodes.some(node => node.attr('src').indexOf('file://') === 0)) {
                        return
                    }
                    for (let i = 0, l = nodes.length; i < l; i++) {
                        const node = nodes[i];
                        if (node.attr('src').indexOf('file://') === 0) {
                            if (imgs[index]?.base64) {
                                node.attr('src', imgs[index]?.base64);
                                index++
                            } else {
                                node.remove()
                            }
                        }
                        if (i + 1 === nodes.length) {
                            imgs = [];
                            index = 0
                        }
                    }
                })
            })
        })
    };
    Plugin()
})();

引用插件

 // 添加擴展插件
  external_plugins: {
    pasteuploadimage: '/tinymce/plugins/pasteuploadimage/plugin.min.js',
  },

插件位置

image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容