fix(ImagePreview): avoid Vue 2.6 event bubble issues

This commit is contained in:
陈嘉涵 2019-12-31 14:07:51 +08:00 committed by neverland
parent b844a038fa
commit b66597621c
2 changed files with 31 additions and 9 deletions

View File

@ -1,6 +1,6 @@
import { createNamespace } from '../utils'; import { createNamespace } from '../utils';
import { range } from '../utils/format/number'; import { range } from '../utils/format/number';
import { preventDefault } from '../utils/dom/event'; import { on, preventDefault } from '../utils/dom/event';
import { PopupMixin } from '../mixins/popup'; import { PopupMixin } from '../mixins/popup';
import { TouchMixin } from '../mixins/touch'; import { TouchMixin } from '../mixins/touch';
import Image from '../image'; import Image from '../image';
@ -99,10 +99,7 @@ export default createComponent({
watch: { watch: {
value(val) { value(val) {
if (val) { if (val) {
this.setActive(this.startPosition); this.onShow();
this.$nextTick(() => {
this.$refs.swipe.swipeTo(this.startPosition, { immediate: true });
});
} else { } else {
this.$emit('close', { this.$emit('close', {
index: this.active, index: this.active,
@ -116,7 +113,34 @@ export default createComponent({
} }
}, },
mounted() {
if (this.value) {
this.bindEvent();
}
},
methods: { methods: {
onShow() {
this.bindEvent();
this.setActive(this.startPosition);
this.$nextTick(() => {
this.$refs.swipe.swipeTo(this.startPosition, { immediate: true });
});
},
bindEvent() {
if (!this.eventBinded) {
this.eventBinded = true;
this.$nextTick(() => {
const swipe = this.$refs.swipe.$el;
on(swipe, 'touchstart', this.onWrapperTouchStart);
on(swipe, 'touchmove', preventDefault);
on(swipe, 'touchend', this.onWrapperTouchEnd);
on(swipe, 'touchcancel', this.onWrapperTouchEnd);
});
}
},
onWrapperTouchStart() { onWrapperTouchStart() {
this.touchStartTime = new Date(); this.touchStartTime = new Date();
}, },
@ -286,10 +310,6 @@ export default createComponent({
initialSwipe={this.startPosition} initialSwipe={this.startPosition}
showIndicators={this.showIndicators} showIndicators={this.showIndicators}
onChange={this.setActive} onChange={this.setActive}
nativeOnTouchstart={this.onWrapperTouchStart}
nativeOnTouchMove={preventDefault}
nativeOnTouchend={this.onWrapperTouchEnd}
nativeOnTouchcancel={this.onWrapperTouchEnd}
> >
{this.images.map((image, index) => ( {this.images.map((image, index) => (
<SwipeItem> <SwipeItem>

View File

@ -25,6 +25,8 @@ test('render image', async () => {
expect(wrapper).toMatchSnapshot(); expect(wrapper).toMatchSnapshot();
await later();
const swipe = wrapper.find('.van-swipe__track'); const swipe = wrapper.find('.van-swipe__track');
triggerDrag(swipe, 500, 0); triggerDrag(swipe, 500, 0);
expect(wrapper.emitted('input')).toBeFalsy(); expect(wrapper.emitted('input')).toBeFalsy();