|
| 1 | +<script setup lang="ts"> |
| 2 | +import { computed, onBeforeUnmount, ref, useTemplateRef, watch } from 'vue' |
| 3 | +import { ClickOutside as vClickOutside, ElMessage } from 'element-plus' |
| 4 | +import { Plus } from '@element-plus/icons-vue' |
| 5 | +
|
| 6 | +defineOptions({ name: 'MkEditAvatar' }) |
| 7 | +
|
| 8 | +const props = withDefaults(defineProps<{ defaultIcon: string; editable?: boolean; size?: number }>(), { |
| 9 | + editable: true, |
| 10 | + size: 32, |
| 11 | +}) |
| 12 | +const avatar = defineModel<string>({ default: '' }) |
| 13 | +const emit = defineEmits<{ change: [icon: string, file: File | null] }>() |
| 14 | +defineSlots<{ default?(props: { icon: string; size: number }): unknown }>() |
| 15 | +
|
| 16 | +// 头像编辑草稿:确认前不修改外部值。 |
| 17 | +const visible = ref(false) |
| 18 | +const logoMode = ref<'default' | 'custom'>('default') |
| 19 | +const draftIcon = ref('') |
| 20 | +const draftFile = ref<File | null>(null) |
| 21 | +const reading = ref(false) |
| 22 | +const referenceRef = useTemplateRef<HTMLElement>('referenceRef') |
| 23 | +const fileInputRef = useTemplateRef<HTMLInputElement>('fileInputRef') |
| 24 | +const currentIcon = computed(() => avatar.value || props.defaultIcon) |
| 25 | +let fileReader: FileReader | undefined |
| 26 | +
|
| 27 | +function open() { |
| 28 | + if (!props.editable || visible.value) return |
| 29 | + logoMode.value = avatar.value && avatar.value !== props.defaultIcon ? 'custom' : 'default' |
| 30 | + draftIcon.value = logoMode.value === 'custom' ? avatar.value : '' |
| 31 | + draftFile.value = null |
| 32 | + visible.value = true |
| 33 | +} |
| 34 | +
|
| 35 | +function close() { |
| 36 | + visible.value = false |
| 37 | + fileReader?.abort() |
| 38 | + reading.value = false |
| 39 | +} |
| 40 | +
|
| 41 | +function clearDraft() { |
| 42 | + if (visible.value) return |
| 43 | + draftIcon.value = '' |
| 44 | + draftFile.value = null |
| 45 | + if (fileInputRef.value) fileInputRef.value.value = '' |
| 46 | +} |
| 47 | +
|
| 48 | +function closeOutside(event: MouseEvent) { |
| 49 | + if (event.target instanceof Node && referenceRef.value?.contains(event.target)) return |
| 50 | + close() |
| 51 | +} |
| 52 | +
|
| 53 | +function confirm() { |
| 54 | + if (reading.value || (logoMode.value === 'custom' && !draftIcon.value)) return |
| 55 | + const icon = logoMode.value === 'default' ? '' : draftIcon.value |
| 56 | + const file = logoMode.value === 'custom' ? draftFile.value : null |
| 57 | + avatar.value = icon |
| 58 | + emit('change', icon, file) |
| 59 | + close() |
| 60 | +} |
| 61 | +
|
| 62 | +// 本地图片校验与预览,文件由调用方上传。 |
| 63 | +function selectImage(event: Event) { |
| 64 | + const input = event.target as HTMLInputElement |
| 65 | + const file = input.files?.[0] |
| 66 | + input.value = '' |
| 67 | + if (!file) return |
| 68 | + if (!['image/jpeg', 'image/png', 'image/gif'].includes(file.type)) { |
| 69 | + ElMessage.warning('仅支持 JPG、PNG、GIF 格式的图片') |
| 70 | + return |
| 71 | + } |
| 72 | + if (file.size > 10 * 1024 * 1024) { |
| 73 | + ElMessage.warning('文件大小不能超过 10MB') |
| 74 | + return |
| 75 | + } |
| 76 | + fileReader?.abort() |
| 77 | + const reader = new FileReader() |
| 78 | + fileReader = reader |
| 79 | + reading.value = true |
| 80 | + reader.onload = () => { |
| 81 | + draftIcon.value = String(reader.result) |
| 82 | + draftFile.value = file |
| 83 | + reading.value = false |
| 84 | + } |
| 85 | + reader.onerror = () => { |
| 86 | + reading.value = false |
| 87 | + ElMessage.error('图片读取失败,请重新选择') |
| 88 | + } |
| 89 | + reader.readAsDataURL(file) |
| 90 | +} |
| 91 | +
|
| 92 | +watch( |
| 93 | + () => props.editable, |
| 94 | + (editable) => { |
| 95 | + if (!editable) close() |
| 96 | + }, |
| 97 | +) |
| 98 | +onBeforeUnmount(() => fileReader?.abort()) |
| 99 | +</script> |
| 100 | + |
| 101 | +<template> |
| 102 | + <!-- // TODO: 优化头像预览效果 --> |
| 103 | + <el-popover :visible="visible" :width="414" placement="bottom-start" @after-leave="clearDraft"> |
| 104 | + <template #reference> |
| 105 | + <!-- 悬停或点击修改头像 --> |
| 106 | + <button |
| 107 | + ref="referenceRef" |
| 108 | + type="button" |
| 109 | + class="inline-flex shrink-0 overflow-hidden rounded-md border-0 bg-transparent p-0 align-middle" |
| 110 | + :class="editable ? 'cursor-pointer' : 'cursor-default'" |
| 111 | + :disabled="!editable" |
| 112 | + :title="editable ? '修改头像' : '头像'" |
| 113 | + @mouseenter="open" |
| 114 | + @focus="open" |
| 115 | + @click.stop="open" |
| 116 | + @keydown.esc.stop="close" |
| 117 | + > |
| 118 | + <slot :icon="avatar" :size="size"> |
| 119 | + <img :src="currentIcon" alt="头像" class="object-contain" :style="{ width: `${size}px`, height: `${size}px` }" /> |
| 120 | + </slot> |
| 121 | + </button> |
| 122 | + </template> |
| 123 | + |
| 124 | + <div v-click-outside="closeOutside" class="text-N900" @keydown.esc.stop="close" @click.stop> |
| 125 | + <div class="mb-1">Logo 设置<span class="ml-0.5 text-danger">*</span></div> |
| 126 | + <el-radio-group v-model="logoMode" :disabled="reading" class="mb-2"> |
| 127 | + <el-radio value="default">默认 Logo</el-radio> |
| 128 | + <el-radio value="custom">自定义上传</el-radio> |
| 129 | + </el-radio-group> |
| 130 | + |
| 131 | + <img v-if="logoMode === 'default'" :src="defaultIcon" alt="默认 Logo" class="h-[106px] w-[106px] rounded-md object-contain" /> |
| 132 | + <div v-else> |
| 133 | + <input ref="fileInputRef" type="file" accept="image/jpeg,image/png,image/gif" class="hidden" @change="selectImage" /> |
| 134 | + <!-- 选择或更换头像图片 --> |
| 135 | + <button |
| 136 | + type="button" |
| 137 | + class="flex-center h-20 w-20 cursor-pointer overflow-hidden rounded-md border border-dashed bg-N100 p-0 hover:border-primary" |
| 138 | + :disabled="reading" |
| 139 | + :title="draftIcon ? '更换头像图片' : '上传头像图片'" |
| 140 | + @click="fileInputRef?.click()" |
| 141 | + > |
| 142 | + <img v-if="draftIcon" :src="draftIcon" alt="自定义头像预览" class="h-full w-full object-contain" /> |
| 143 | + <MkIcon v-else :icon="Plus" :size="28" /> |
| 144 | + </button> |
| 145 | + <p class="mt-1 text-N600">支持 JPG、PNG、GIF,文件大小不超过 10MB</p> |
| 146 | + </div> |
| 147 | + |
| 148 | + <div class="mt-4 flex justify-end gap-3"> |
| 149 | + <!-- 取消头像修改 --> |
| 150 | + <el-button class="min-w-20" @click="close">取消</el-button> |
| 151 | + <!-- 确认头像修改 --> |
| 152 | + <el-button class="ml-0! min-w-20" type="primary" :loading="reading" :disabled="logoMode === 'custom' && !draftIcon" @click="confirm"> |
| 153 | + 确定 |
| 154 | + </el-button> |
| 155 | + </div> |
| 156 | + </div> |
| 157 | + </el-popover> |
| 158 | +</template> |
0 commit comments