Ben Lin
2024-06-18 ebbd788fbb2c0b45d4473798efc57eec8ba74a25
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
<template>
  <div :class="getClass" :style="getStyle">
    <div :class="`${prefixCls}-image-wrapper`" :style="getImageWrapperStyle" @click="openModal()">
      <div :class="`${prefixCls}-image-mask`" :style="getImageWrapperStyle">
        <Icon
          icon="ant-design:cloud-upload-outlined"
          :size="getIconWidth"
          :style="getImageWrapperStyle"
          color="#d6d6d6"
        />
      </div>
      <img :src="sourceValue" v-if="sourceValue" alt="avatar" />
    </div>
    <a-button
      :class="`${prefixCls}-upload-btn`"
      @click="openModal"
      v-if="showBtn"
      v-bind="btnProps"
    >
      {{ btnText ? btnText : t('component.cropper.selectImage') }}
    </a-button>
 
    <CropperModal
      @register="register"
      @upload-success="handleUploadSuccess"
      :uploadApi="uploadApi"
      :src="sourceValue"
      :size="size"
    />
  </div>
</template>
<script lang="ts" setup>
  import { computed, CSSProperties, unref, ref, watchEffect, watch, PropType } from 'vue';
  import CropperModal from './CropperModal.vue';
  import { useDesign } from '@/hooks/web/useDesign';
  import { useModal } from '@/components/Modal';
  import { useMessage } from '@/hooks/web/useMessage';
  import { useI18n } from '@/hooks/web/useI18n';
  import type { ButtonProps } from '@/components/Button';
  import Icon from '@/components/Icon/Icon.vue';
 
  defineOptions({ name: 'CropperAvatar' });
 
  const props = defineProps({
    width: { type: [String, Number], default: '200px' },
    value: { type: String },
    showBtn: { type: Boolean, default: true },
    btnProps: { type: Object as PropType<ButtonProps> },
    btnText: { type: String, default: '' },
    uploadApi: {
      type: Function as PropType<({ file, name }: { file: Blob; name: string }) => Promise<void>>,
    },
 
    size: { type: Number, default: 5 },
  });
 
  const emit = defineEmits(['update:value', 'change']);
 
  const sourceValue = ref(props.value || '');
  const { prefixCls } = useDesign('cropper-avatar');
  const [register, { openModal, closeModal }] = useModal();
  const { createMessage } = useMessage();
  const { t } = useI18n();
 
  const getClass = computed(() => [prefixCls]);
 
  const getWidth = computed(() => `${props.width}`.replace(/px/, '') + 'px');
 
  const getIconWidth = computed(() => parseInt(`${props.width}`.replace(/px/, '')) / 2 + 'px');
 
  const getStyle = computed((): CSSProperties => ({ width: unref(getWidth) }));
 
  const getImageWrapperStyle = computed(
    (): CSSProperties => ({ width: unref(getWidth), height: unref(getWidth) }),
  );
 
  watchEffect(() => {
    sourceValue.value = props.value || '';
  });
 
  watch(
    () => sourceValue.value,
    (v: string) => {
      emit('update:value', v);
    },
  );
 
  function handleUploadSuccess({ source, data }) {
    sourceValue.value = source;
    emit('change', { source, data });
    createMessage.success(t('component.cropper.uploadSuccess'));
  }
 
  defineExpose({ openModal: openModal.bind(null, true), closeModal });
</script>
 
<style lang="less" scoped>
  @prefix-cls: ~'@{namespace}-cropper-avatar';
 
  .@{prefix-cls} {
    display: inline-block;
    text-align: center;
 
    &-image-wrapper {
      overflow: hidden;
      border: 1px solid @border-color-base;
      border-radius: 50%;
      background: @component-background;
      cursor: pointer;
 
      img {
        width: 100%;
      }
    }
 
    &-image-mask {
      position: absolute;
      width: inherit;
      height: inherit;
      transition: opacity 0.4s;
      border: inherit;
      border-radius: inherit;
      opacity: 0;
      background: rgb(0 0 0 / 40%);
      cursor: pointer;
 
      ::v-deep(svg) {
        margin: auto;
      }
    }
 
    &-image-mask:hover {
      opacity: 40;
    }
 
    &-upload-btn {
      margin: 10px auto;
    }
  }
</style>