Skip to content

Commit 35f700d

Browse files
committed
Add emitting 'ready' event on image loading
1 parent 611b082 commit 35f700d

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

src/Cropper.vue

+24-25
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { CropperWrapper } from './components/service';
88
import { ResizeEvent, MoveEvent } from './core/events';
99
import { isLocal, isCrossOriginURL, isUndefined, addTimestamp, getSettings, parseNumber } from './core/utils';
1010
import { arrayBufferToDataURL, getImageTransforms, getStyleTransforms, prepareSource, parseImage } from './core/image';
11-
import { ALL_DIRECTIONS, MINIMAL_PERCENT_SIZE, IMAGE_RESTRICTIONS } from './core/constants';
11+
import { ALL_DIRECTIONS, MINIMAL_PERCENT_SIZE, IMAGE_RESTRICTIONS, DEFAULT_COORDINATES } from './core/constants';
1212
import * as algorithms from './core/algorithms';
1313
1414
const cn = bem('vue-advanced-cropper');
@@ -169,17 +169,11 @@ export default {
169169
width: null,
170170
height: null,
171171
},
172-
coordinates: {
173-
left: 0,
174-
top: 0,
175-
width: 0,
176-
height: 0,
172+
coordinates: {
173+
...DEFAULT_COORDINATES
177174
},
178-
stencilCoordinates: {
179-
left: 0,
180-
top: 0,
181-
width: 0,
182-
height: 0,
175+
stencilCoordinates: {
176+
...DEFAULT_COORDINATES
183177
},
184178
frozenDirections: {
185179
width: false,
@@ -362,11 +356,7 @@ export default {
362356
window.addEventListener('resize', this.onResizeWindow);
363357
window.addEventListener('orientationchange', this.onResizeWindow);
364358
this.$refs.image.addEventListener('load', () => {
365-
// After loading image the current component can be unmounted
366-
// Therefore there is a workaround to prevent processing the following code
367-
if (this.$refs.image) {
368-
this.refreshImage().then(this.resetCoordinates);
369-
}
359+
this.onLoadImage();
370360
});
371361
this.onChangeImage();
372362
},
@@ -485,6 +475,17 @@ export default {
485475
this.clearImage();
486476
}
487477
},
478+
onLoadImage() {
479+
// After loading image the current component can be unmounted
480+
// Therefore there is a workaround to prevent processing the following code
481+
if (this.$refs.image && !this.imageLoaded) {
482+
this.imageLoaded = true;
483+
this.refreshImage().then(() => {
484+
this.resetCoordinates();
485+
this.$emit('ready');
486+
});
487+
}
488+
},
488489
onParseImage({ arrayBuffer, orientation }) {
489490
if (arrayBuffer && orientation && isLocal(this.src)) {
490491
this.imageAttributes.src = arrayBufferToDataURL(arrayBuffer);
@@ -495,7 +496,7 @@ export default {
495496
Vue.nextTick(() => {
496497
const image = this.$refs.image;
497498
if (image && image.complete) {
498-
this.refreshImage().then(this.resetCoordinates);
499+
this.onLoadImage();
499500
}
500501
});
501502
},
@@ -736,24 +737,22 @@ export default {
736737
if (this.delayedTransforms) {
737738
transforms.push(...Array.isArray(this.delayedTransforms) ? this.delayedTransforms : [this.delayedTransforms]);
738739
}
739-
740740
this.resetWorldTransforms();
741741
this.applyTransforms(transforms);
742742
this.delayedTransforms = null;
743-
this.imageLoaded = true;
744743
},
745744
clearImage() {
746745
const stretcher = this.$refs.stretcher;
747746
this.imageLoaded = false;
748-
this.onChangeCoordinates({
749-
left: 0,
750-
top: 0,
751-
width: 0,
752-
height: 0,
753-
}, false);
754747
setTimeout(() => {
755748
stretcher.style.height = 'auto';
756749
stretcher.style.width = 'auto';
750+
this.onChangeCoordinates({ ...DEFAULT_COORDINATES }, false);
751+
this.updateStencilCoordinates({ ...DEFAULT_COORDINATES });
752+
this.boundarySize = {
753+
width: 0,
754+
height: 0,
755+
};
757756
}, this.transitionTime);
758757
},
759758
refreshImage() {

src/core/constants.js

+7
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,10 @@ export const IMAGE_RESTRICTIONS = ['area', 'stencil', 'none'];
99
export const XHR_DONE = 4;
1010

1111
export const MINIMAL_PERCENT_SIZE = 0.1;
12+
13+
export const DEFAULT_COORDINATES = {
14+
left: 0,
15+
top: 0,
16+
width: 0,
17+
height: 0,
18+
};

0 commit comments

Comments
 (0)