Skip to content

Commit d7839b9

Browse files
committed
Fix(avatar-upload): make AvatarUpload component an wrapper for vue-image-crop-upload
* Fix the bug that the background still showing when closed * Add more options for AvatarUpload component * Add docs and links for AvatarUpload component
1 parent 82b1a8e commit d7839b9

File tree

4 files changed

+121
-120
lines changed

4 files changed

+121
-120
lines changed

src/components/AvatarUpload/index.vue

Lines changed: 51 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,77 @@
11
<template>
2-
<div
3-
v-show="value"
4-
class="vue-image-crop-upload"
5-
>
6-
<div class="vicp-wrap">
7-
<img-upload
8-
v-model="show"
9-
field="img"
10-
:width="300"
11-
:height="300"
12-
url="https://httpbin.org/post"
13-
:params="params"
14-
:headers="headers"
15-
img-format="png"
16-
@crop-success="cropSuccess"
17-
@crop-upload-success="cropUploadSuccess"
18-
@crop-upload-fail="cropUploadFail"
19-
/>
20-
</div>
21-
</div>
2+
<image-crop-upload
3+
v-model="show"
4+
:field="field"
5+
:url="url"
6+
:width="width"
7+
:height="height"
8+
:params="params"
9+
:headers="headers"
10+
:lang-type="language"
11+
:with-credentials="true"
12+
img-format="png"
13+
@src-file-set="srcFileSet"
14+
@crop-success="cropSuccess"
15+
@crop-upload-success="cropUploadSuccess"
16+
@crop-upload-fail="cropUploadFail"
17+
/>
2218
</template>
2319

2420
<script lang="ts">
21+
import ImageCropUpload from 'vue-image-crop-upload'
2522
import { Component, Prop, Vue } from 'vue-property-decorator'
26-
import imgUpload from 'vue-image-crop-upload'
23+
import { AppModule } from '@/store/modules/app'
2724
2825
@Component({
2926
name: 'AvatarUpload',
3027
components: {
31-
imgUpload
28+
ImageCropUpload
3229
}
3330
})
3431
export default class extends Vue {
35-
@Prop({ default: true }) private value!: boolean
32+
// You can add more Prop, see: https://github.com/dai-siki/vue-image-crop-upload#usage
33+
@Prop({ required: true }) private value!: boolean
34+
@Prop({ required: true }) private url!: string
35+
@Prop({ required: true }) private field!: string
36+
@Prop({ default: 300 }) private width!: number
37+
@Prop({ default: 300 }) private height!: number
38+
@Prop({ default: () => {} }) private params!: object
39+
@Prop({ default: () => {} }) private headers!: object
3640
37-
private customStyle!: Object
38-
private show = true
39-
private params = { token: '123456798', name: 'avatar' }
40-
private headers = { smail: '*_~' }
41-
private imgDataUrl = ''
41+
// https://github.com/dai-siki/vue-image-crop-upload#language-package
42+
private languageTypeList: { [key: string]: string } = {
43+
'en': 'en',
44+
'zh': 'zh',
45+
'es': 'es-MX',
46+
'ja': 'ja'
47+
}
48+
49+
get show() {
50+
return this.value
51+
}
52+
53+
set show(value) {
54+
this.$emit('input', value)
55+
}
56+
57+
get language() {
58+
return this.languageTypeList[AppModule.language]
59+
}
4260
43-
private toggleShow() {
44-
this.show = !this.show
61+
private srcFileSet(fileName: string, fileType: string, fileSize: number) {
62+
this.$emit('src-file-set', fileName, fileType, fileSize)
4563
}
4664
4765
private cropSuccess(imgDataUrl: string, field: string) {
48-
console.log('-------- crop success --------')
49-
this.imgDataUrl = imgDataUrl
66+
this.$emit('crop-success', imgDataUrl, field)
5067
}
5168
5269
private cropUploadSuccess(jsonData: any, field: string) {
53-
console.log('-------- upload success --------')
54-
console.log(jsonData)
55-
this.$emit('crop-upload-success', jsonData.files.img)
56-
console.log('field: ' + field)
70+
this.$emit('crop-upload-success', jsonData, field)
5771
}
5872
5973
private cropUploadFail(status: boolean, field: string) {
60-
console.log('-------- upload fail --------')
61-
console.log(status)
62-
console.log('field: ' + field)
74+
this.$emit('crop-upload-fail', status, field)
6375
}
6476
}
6577
</script>

src/components/BackToTop/index.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export default class extends Vue {
3737
}
3838
}
3939
})
40-
private customStyle!: Object
40+
private customStyle!: object
4141
4242
private visible = false
4343
private isMoving = false

src/views/components-demo/avatar-upload.vue

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@
1414
type="primary"
1515
icon="upload"
1616
tyle="position: absolute;bottom: 15px;margin-left: 40px;"
17-
@click="imagecropperShow=true"
17+
@click="toggleShow"
1818
>
1919
Change Avatar
2020
</el-button>
21-
2221
<avatar-upload
23-
v-show="imagecropperShow"
24-
:key="imagecropperKey"
22+
v-model="showImageUpload"
23+
field="avatar"
2524
:width="300"
2625
:height="300"
26+
:params="params"
27+
:headers="headers"
2728
url="https://httpbin.org/post"
28-
lang-type="en"
29-
@close="close"
30-
@crop-upload-success="cropSuccess"
29+
@close="onClose"
30+
@crop-upload-success="onCropUploadSuccess"
3131
/>
3232
</div>
3333
</template>
@@ -45,23 +45,27 @@ import PanThumb from '@/components/PanThumb/index.vue'
4545
}
4646
})
4747
export default class extends Vue {
48-
private imagecropperShow = false
49-
private imagecropperKey = 0
48+
private showImageUpload = false
5049
private image = 'https://wpimg.wallstcn.com/577965b9-bb9e-4e02-9f0c-095b41417191'
50+
private params = { some_params: 'your_params_goes_here' }
51+
private headers = { smail: '*_~' }
52+
53+
private toggleShow() {
54+
this.showImageUpload = !this.showImageUpload
55+
}
5156
52-
private cropSuccess(resData: string) {
53-
this.imagecropperShow = false
54-
this.imagecropperKey = this.imagecropperKey + 1
55-
this.image = resData
57+
private onCropUploadSuccess(jsonData: any, field: string) {
58+
this.showImageUpload = false
59+
this.image = jsonData.files[field]
5660
}
5761
58-
private close() {
59-
this.imagecropperShow = false
62+
private onClose() {
63+
this.showImageUpload = false
6064
}
6165
}
6266
</script>
6367

64-
<style scoped>
68+
<style lang="scss" scoped>
6569
.avatar {
6670
width: 200px;
6771
height: 200px;

0 commit comments

Comments
 (0)