22 changed files with 1043 additions and 196 deletions
@ -0,0 +1,103 @@ |
|||
import { ref } from 'vue'; |
|||
import { commonUpload } from '@/api/upload'; |
|||
import { QuillEditor } from '@vueup/vue-quill'; |
|||
|
|||
const quillEditor = ref<InstanceType<typeof QuillEditor> | null>(null); |
|||
|
|||
export const quillImageUploadCustom = (quill) => { |
|||
quillEditor.value = quill; |
|||
const toolbar = quillEditor.value?.getToolbar(); |
|||
|
|||
const formats = toolbar?.querySelectorAll('.ql-formats'); |
|||
if (!formats) return; |
|||
// 创建一个自定义按钮,使用
|
|||
const customButton = document.createElement('button'); |
|||
|
|||
const svgElement = createImageUploadSvg(); |
|||
|
|||
// 将img添加到button元素中
|
|||
customButton.appendChild(svgElement); |
|||
|
|||
// 给按钮添加点击事件
|
|||
customButton.onclick = (e) => { |
|||
e.stopPropagation(); |
|||
handleCustomImageUpload(); |
|||
}; |
|||
// 添加到工具栏尾部
|
|||
formats[formats.length - 1].appendChild(customButton); |
|||
}; |
|||
|
|||
// 自定义文件上传函数
|
|||
const handleCustomImageUpload = () => { |
|||
// 创建一个文件选择器
|
|||
const input = document.createElement('input'); |
|||
input.type = 'file'; |
|||
input.accept = 'image/*'; // 限制为图片文件
|
|||
input.click(); |
|||
|
|||
input.onchange = async (e: any) => { |
|||
const file = e?.target?.files[0]; |
|||
if (file) { |
|||
const formData = new FormData(); |
|||
formData.append('file', file); |
|||
|
|||
// 发送文件到服务器进行上传
|
|||
try { |
|||
const res = await commonUpload(formData); |
|||
console.log('res: ', res); |
|||
|
|||
if (res?.url) { |
|||
const fileUrl = res?.url; |
|||
insertImage(fileUrl); // 将图片插入到 Quill 编辑器
|
|||
} |
|||
} catch (error) { |
|||
console.error('文件上传失败', error); |
|||
} |
|||
} |
|||
}; |
|||
}; |
|||
|
|||
// 插入图片到 Quill 编辑器
|
|||
const insertImage = (url) => { |
|||
const quill = quillEditor.value?.getQuill(); |
|||
console.log('quill: ', quill); |
|||
// quillEditor.value?.setContents(html + `<img src="${url}" alt="Image" />`);
|
|||
const range = quill.getSelection(); |
|||
console.log('range: ', range); |
|||
let index = 0; |
|||
if (range) { |
|||
index = range.index; |
|||
} |
|||
quill.insertEmbed(index, 'image', url); |
|||
}; |
|||
|
|||
const createImageUploadSvg = () => { |
|||
// 创建 SVG 元素
|
|||
const svgElement = document.createElementNS('http://www.w3.org/2000/svg', 'svg'); |
|||
svgElement.setAttribute('viewBox', '0 0 1024 1024'); |
|||
svgElement.setAttribute('version', '1.1'); |
|||
svgElement.setAttribute('xmlns', 'http://www.w3.org/2000/svg'); |
|||
svgElement.setAttribute('width', '20'); |
|||
svgElement.setAttribute('height', '20'); |
|||
|
|||
// 创建第一个 path 元素
|
|||
const path1 = document.createElementNS('http://www.w3.org/2000/svg', 'path'); |
|||
path1.setAttribute( |
|||
'd', |
|||
'M585.6696875 876.7315625H199.9878125C140.96 876.7315625 92.9375 828.7090625 92.9375 769.6821875V229.038125c0-59.0278125 48.0225-107.0503125 107.0503125-107.0503125h625.5853125c59.0278125 0 107.0484375 48.0225 107.0484375 107.0503125v304.6509375c0 24.534375-19.9575 44.49375-44.4890625 44.49375-24.534375 0-44.4946875-19.959375-44.4946875-44.49375V419.121875L638.6328125 624.1259375c-20.2275 20.2303125-47.0953125 31.3725-75.65625 31.3725-16.479375 0-33.0065625-3.9075-47.79375-11.2996875l-169.693125-84.8475c-2.563125-1.2965625-5.2509375-1.9396875-8.0484375-1.9396875a17.589375 17.589375 0 0 0-12.6121875 5.2696875l-0.0675 0.0675-142.84875 142.6621875v64.27125c0 9.96375 8.1084375 18.07125 18.075 18.07125h385.681875c24.5325 0 44.49 19.959375 44.49 44.49375-0.0009375 24.52875-19.9584375 44.484375-44.49 44.484375zM199.9878125 210.963125c-9.9665625 0-18.075 8.1075-18.075 18.0740625V579.621875l79.8965625-79.8440625c20.2228125-20.2275 47.0915625-31.36875 75.6525-31.36875 16.48125 0 33.0103125 3.9084375 47.799375 11.3034375l169.689375 84.8475c2.5621875 1.2965625 5.2509375 1.93875 8.049375 1.93875 4.7803125 0 9.2625-1.87125 12.616875-5.2696875l268.0246875-268.03125v-64.160625c0-9.9665625-8.10375-18.0740625-18.0646875-18.0740625H199.9878125z', |
|||
); |
|||
path1.setAttribute('fill', '#4b5563'); |
|||
|
|||
// 创建第二个 path 元素
|
|||
const path2 = document.createElementNS('http://www.w3.org/2000/svg', 'path'); |
|||
path2.setAttribute( |
|||
'd', |
|||
'M788.009375 902.0121875c-20.7590625 0-37.648125-16.88625-37.648125-37.6434375v-153.75l-60.7565625 57.0909375c-7.03125 6.594375-16.1878125 10.2225-25.78875 10.2225-10.335 0-20.3221875-4.3275-27.3984375-11.874375-6.8840625-7.33875-10.5075-16.9040625-10.2-26.934375 0.3065625-10.0434375 4.516875-19.3640625 11.8565625-26.2490625L746.601875 610.925c10.2421875-12.3665625 25.2909375-19.4484375 41.3653125-19.4484375 16.155 0 31.2065625 7.081875 41.4478125 19.4484375l108.525 101.953125c7.340625 6.886875 11.551875 16.228125 11.855625 26.300625 0.3028125 10.05375-3.3253125 19.605-10.2159375 26.8959375-7.1203125 7.475625-17.1 11.758125-27.395625 11.7684375-9.5709375 0-18.718125-3.6309375-25.764375-10.2215625l-60.766875-57.045V864.36875c-0.0009375 20.7571875-16.888125 37.6434375-37.6434375 37.6434375zM354.411875 304.754375c-16.8346875 0-30.6075 13.77375-30.6075 30.6121875 0 16.8309375 13.7728125 30.6075 30.6075 30.6075 16.8384375 0 30.6121875-13.7765625 30.6121875-30.6075-0.0009375-16.843125-13.77375-30.6121875-30.6121875-30.6121875z m0 122.443125c-50.6034375 0-91.831875-41.2284375-91.831875-91.8309375 0-50.6071875 41.2284375-91.835625 91.831875-91.835625 50.6025 0 91.835625 41.2284375 91.835625 91.835625 0 50.6015625-41.233125 91.8309375-91.835625 91.8309375z', |
|||
); |
|||
path2.setAttribute('fill', '#4b5563'); |
|||
|
|||
// 将 path 元素添加到 SVG 元素中
|
|||
svgElement.appendChild(path1); |
|||
svgElement.appendChild(path2); |
|||
return svgElement; |
|||
}; |
|||
Loading…
Reference in new issue