1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
| <template> <div class="quill-editor-wrapper"> <div ref="editorRef"></div> </div> </template>
<script setup lang="ts"> import { ref, onMounted, onBeforeUnmount } from 'vue'; import Quill from 'quill'; import 'quill/dist/quill.snow.css'; import { useGetUploadSign } from '@/composables/common'; import { UPLOAD_SCENES } from '@/constant/api'; import axios from 'axios'; import { ElMessage } from 'element-plus';
const BlockEmbed = Quill.import('blots/block/embed');
class ImageBlot extends BlockEmbed { static blotName = 'customImage'; static tagName = 'div'; static className = 'custom-image-wrapper';
static create(value: any) { const node = super.create(value) as HTMLElement;
node.setAttribute('data-url', value.url || ''); node.setAttribute('data-width', value.width || ''); node.setAttribute('data-height', value.height || ''); node.setAttribute('data-size', value.size || ''); node.setAttribute('data-name', value.name || ''); node.setAttribute('contenteditable', 'false');
const img = document.createElement('img'); img.setAttribute('src', value.url); if (value.width) img.style.width = value.width + 'px'; if (value.height) img.style.height = value.height + 'px';
node.appendChild(img); return node; }
static value(node: HTMLElement) { return { url: node.getAttribute('data-url') || '', width: node.getAttribute('data-width') || '', height: node.getAttribute('data-height') || '', size: node.getAttribute('data-size') || '', name: node.getAttribute('data-name') || '' }; } }
Quill.register(ImageBlot);
const editorRef = ref<HTMLElement>(); let quill: Quill | null = null;
const PLACEHOLDER_IMAGE = 'data:image/svg+xml,%3Csvg xmlns="http://www.w3.org/2000/svg" width="300" height="200"%3E%3Crect width="300" height="200" fill="%23f0f0f0"/%3E%3Ctext x="150" y="100" text-anchor="middle" dy=".3em" fill="%23999" font-size="16"%3E上传中...%3C/text%3E%3C/svg%3E';
const getImageDimensions = (file: File): Promise<{ width: number; height: number }> => { return new Promise((resolve) => { const img = new Image(); img.onload = () => { resolve({ width: img.width, height: img.height }); }; img.onerror = () => { resolve({ width: 0, height: 0 }); }; img.src = URL.createObjectURL(file); }); };
const imageHandler = async () => { const input = document.createElement('input'); input.setAttribute('type', 'file'); input.setAttribute('accept', 'image/*'); input.click();
input.onchange = async () => { const file = input.files?.[0]; if (!file) return;
if (!file.type.startsWith('image/')) { ElMessage.warning('请选择图片文件'); return; }
if (!quill) return;
const range = quill.getSelection(true);
const placeholderId = `placeholder-${Date.now()}`; quill.insertEmbed(range.index, 'customImage', { url: PLACEHOLDER_IMAGE, width: 300, height: 200, size: '', name: placeholderId }); quill.setSelection(range.index + 1, 0);
try { const dimensions = await getImageDimensions(file); const { sign, formData } = await useGetUploadSign(file, UPLOAD_SCENES.ACTIVITY_ENTRY); await axios.post(sign.host, formData);
const editor = quill.root; const placeholderNode = editor.querySelector(`[data-name="${placeholderId}"]`) as HTMLElement;
if (placeholderNode) { placeholderNode.setAttribute('data-url', sign.url); placeholderNode.setAttribute('data-width', dimensions.width.toString()); placeholderNode.setAttribute('data-height', dimensions.height.toString()); placeholderNode.setAttribute('data-size', file.size.toString()); placeholderNode.setAttribute('data-name', file.name);
const img = placeholderNode.querySelector('img'); if (img) { img.setAttribute('src', sign.url); img.style.width = ''; img.style.height = ''; } }
ElMessage.success('图片上传成功'); } catch (error) { ElMessage.error('图片上传失败'); console.error(error); } }; };
onMounted(() => { if (!editorRef.value) return;
quill = new Quill(editorRef.value, { theme: 'snow', modules: { toolbar: { container: [ [{ header: [1, 2, 3, false] }], ['bold', 'italic', 'underline', 'strike'], [{ list: 'ordered' }, { list: 'bullet' }], [{ color: [] }, { background: [] }], [{ align: [] }], ['link', 'image'], ['clean'] ], handlers: { image: imageHandler } } }, placeholder: '请输入内容...' }); });
onBeforeUnmount(() => { if (quill && quill.destroy) { quill.destroy(); } quill = null; });
const getHtml = (): string => { return quill?.root.innerHTML || ''; };
const setHtml = (html: string) => { if (quill) { quill.root.innerHTML = html; } };
defineExpose({ getHtml, setHtml }); </script>
<style scoped> .quill-editor-wrapper { min-height: 400px; }
.quill-editor-wrapper :deep(.ql-container) { min-height: 360px; }
.quill-editor-wrapper :deep(.custom-image-wrapper) { display: block; margin: 10px 0; text-align: center; }
.quill-editor-wrapper :deep(.custom-image-wrapper img) { max-width: 100%; height: auto; display: block; margin: 0 auto; } </style>
|