ueditor的上传回调_编程_山海自留地

山海自留地

ueditor的上传回调

编程
2025-06-05 08:32:09

ueditor的上传回调

微信截图_20250605083116.png


ai代写 未验证

editor.ready(function () {
            // 统一处理图片属性
            const setImgAttributes = (img) => {
                // 跳过占位图
                if (img.src.includes('spacer.gif')) return;
                // 使用真实图片URL设置属性
                const realSrc = img.getAttribute('src') || img.src;
                img.layerSrc = realSrc;
                img.dataSrc = realSrc;
                if (!img.getAttribute('lay-src') || img.getAttribute('lay-src').includes('spacer.gif')) {
                    img.setAttribute('lay-src', realSrc);
                    img.setAttribute('data-src', realSrc);
                    img.setAttribute('v-lazy', realSrc);
                }
            };

            // 防抖函数实现
            function debounce(func, wait, immediate) {
                let timeout;
                return function () {
                    const context = this, args = arguments;
                    const later = function () {
                        timeout = null;
                        if (!immediate) func.apply(context, args);
                    };
                    const callNow = immediate && !timeout;
                    clearTimeout(timeout);
                    timeout = setTimeout(later, wait);
                    if (callNow) func.apply(context, args);
                };
            }

            // 防抖处理
            let debounceTimer;
            const handleContentChange = debounce(function () {
                const imgs = editor.document.querySelectorAll('img');
                imgs.length && imgs.forEach(setImgAttributes);
            }, 300);


            // 监听图片插入前的钩子
            editor.addListener('beforeinsertimage', function (t, images) {
                images.forEach(img => {
                    // 确保在插入前设置的是真实URL
                    if (img.src && !img.src.includes('spacer.gif')) {
                        setImgAttributes(img);
                    }
                });
                return true;
            });

            // 监听内容变化
            editor.addListener('contentChange', handleContentChange);

            // 额外监听图片上传完成事件
            editor.addListener('afterUpfile', function () {
                handleContentChange();
            });
        });