From c8d08e50f6c6380da08446d406486e719f30b569 Mon Sep 17 00:00:00 2001 From: Chen Fengyuan Date: Sat, 23 Jun 2018 21:52:31 +0800 Subject: [PATCH] fix: avoid to create new `data` object --- src/app.vue | 4 ++-- src/components/editor.vue | 29 ++++++++++------------------- src/components/loader.vue | 16 ++++++++-------- src/components/navbar.vue | 10 +++++----- 4 files changed, 25 insertions(+), 34 deletions(-) diff --git a/src/app.vue b/src/app.vue index 8dd34bd..9db65f5 100644 --- a/src/app.vue +++ b/src/app.vue @@ -5,8 +5,8 @@
- - + +
diff --git a/src/components/editor.vue b/src/components/editor.vue index 9c7d9d5..330ee78 100644 --- a/src/components/editor.vue +++ b/src/components/editor.vue @@ -20,11 +20,6 @@ import Cropper from 'cropperjs'; export default { - model: { - event: 'change', - prop: 'data', - }, - props: { data: { type: Object, @@ -203,6 +198,7 @@ autoCrop: false, dragMode: 'move', background: false, + ready: () => { if (this.croppedData) { this.cropper @@ -216,9 +212,10 @@ this.cropBoxData = null; } }, + crop: ({ detail }) => { if (detail.width > 0 && detail.height > 0 && !data.cropping) { - this.triggerChange({ + this.update({ cropping: true, }); } @@ -230,9 +227,6 @@ if (this.cropper) { this.cropper.destroy(); this.cropper = null; - this.triggerChange({ - cropping: false, - }); } }, @@ -243,7 +237,7 @@ this.croppedData = cropper.getData(); this.canvasData = cropper.getCanvasData(); this.cropBoxData = cropper.getCropBoxData(); - this.triggerChange({ + this.update({ cropped: true, cropping: false, previousUrl: data.url, @@ -258,7 +252,7 @@ clear() { if (this.data.cropping) { this.cropper.clear(); - this.triggerChange({ + this.update({ cropping: false, }); } @@ -266,8 +260,8 @@ restore() { if (this.data.cropped) { - this.triggerChange({ - cropping: false, + this.update({ + cropped: false, previousUrl: '', url: this.data.previousUrl, }); @@ -276,7 +270,7 @@ reset() { this.stop(); - this.triggerChange({ + this.update({ cropped: false, cropping: false, loaded: false, @@ -287,11 +281,8 @@ }); }, - triggerChange(data) { - this.$emit('change', { - ...this.data, - ...data, - }, this.data); + update(data) { + Object.assign(this.data, data); }, }, diff --git a/src/components/loader.vue b/src/components/loader.vue index 47577dd..3d5cb6d 100644 --- a/src/components/loader.vue +++ b/src/components/loader.vue @@ -12,11 +12,6 @@ const URL = window.URL || window.webkitURL; export default { - model: { - event: 'change', - prop: 'data', - }, - props: { data: { type: Object, @@ -36,12 +31,12 @@ if (/^image\/\w+$/.test(file.type)) { if (URL) { - this.$emit('change', { + resolve({ loaded: true, name: file.name, type: file.type, url: URL.createObjectURL(file), - }, this.data); + }); } else { reject(new Error('Your browser is not supported.')); } @@ -52,8 +47,9 @@ }, change({ target }) { - this.read(target.files).then(() => { + this.read(target.files).then((data) => { target.value = ''; + this.update(data); }).catch((e) => { target.value = ''; this.alert(e); @@ -72,6 +68,10 @@ alert(e) { window.alert(e && e.message ? e.message : e); }, + + update(data) { + Object.assign(this.data, data); + }, }, }; diff --git a/src/components/navbar.vue b/src/components/navbar.vue index f1810a2..cd206ce 100644 --- a/src/components/navbar.vue +++ b/src/components/navbar.vue @@ -1,11 +1,11 @@