[new feature] ImagePreview: add index slot (#3157)

This commit is contained in:
neverland 2019-04-15 20:59:00 +08:00 committed by GitHub
parent 8d1c02cfbd
commit ce4e0cc3e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 92 additions and 18 deletions

View File

@ -8,7 +8,10 @@ const [sfc, bem] = use('image-preview');
function getDistance(touches) { function getDistance(touches) {
return Math.sqrt( return Math.sqrt(
Math.abs((touches[0].clientX - touches[1].clientX) * (touches[0].clientY - touches[1].clientY)) Math.abs(
(touches[0].clientX - touches[1].clientX) *
(touches[0].clientY - touches[1].clientY)
)
); );
} }
@ -71,8 +74,8 @@ export default sfc({
}; };
if (scale !== 1) { if (scale !== 1) {
style.transform = `scale3d(${scale}, ${scale}, 1) translate(${this.moveX / scale}px, ${this style.transform = `scale3d(${scale}, ${scale}, 1) translate(${this.moveX /
.moveY / scale}px)`; scale}px, ${this.moveY / scale}px)`;
} }
return style; return style;
@ -175,7 +178,11 @@ export default sfc({
if (this.moving || this.zooming) { if (this.moving || this.zooming) {
let stopPropagation = true; let stopPropagation = true;
if (this.moving && this.startMoveX === this.moveX && this.startMoveY === this.moveY) { if (
this.moving &&
this.startMoveX === this.moveX &&
this.startMoveY === this.moveY
) {
stopPropagation = false; stopPropagation = false;
} }
@ -219,7 +226,9 @@ export default sfc({
const { active, images } = this; const { active, images } = this;
const Index = this.showIndex && ( const Index = this.showIndex && (
<div class={bem('index')}>{`${active + 1}/${images.length}`}</div> <div class={bem('index')}>
{this.slots('index') || `${active + 1}/${images.length}`}
</div>
); );
const Images = ( const Images = (
@ -244,7 +253,11 @@ export default sfc({
}; };
return ( return (
<SwipeItem> <SwipeItem>
{this.lazyLoad ? <img vLazy={image} {...props} /> : <img src={image} {...props} />} {this.lazyLoad ? (
<img vLazy={image} {...props} />
) : (
<img src={image} {...props} />
)}
</SwipeItem> </SwipeItem>
); );
})} })}

View File

@ -17,7 +17,10 @@
<van-image-preview <van-image-preview
v-model="show" v-model="show"
:images="images" :images="images"
/> @change="onChange"
>
<template v-slot:index>{{ $t('index', index) }}</template>
</van-image-preview>
</demo-block> </demo-block>
</demo-section> </demo-section>
</template> </template>
@ -38,20 +41,23 @@ export default {
button1: '预览图片', button1: '预览图片',
button2: '指定初始位置', button2: '指定初始位置',
button3: '异步关闭', button3: '异步关闭',
componentCall: '组件调用' componentCall: '组件调用',
index: index => `${index + 1}`
}, },
'en-US': { 'en-US': {
button1: 'Show Images', button1: 'Show Images',
button2: 'Custom Start Position', button2: 'Custom Start Position',
button3: 'Async Close', button3: 'Async Close',
componentCall: 'Component Call' componentCall: 'Component Call',
index: index => `Page: ${index}`
} }
}, },
data() { data() {
return { return {
show: false, show: false,
images images,
index: 0
}; };
}, },
@ -60,6 +66,10 @@ export default {
this.show = true; this.show = true;
}, },
onChange(index) {
this.index = index;
},
showImagePreview(position, timer) { showImagePreview(position, timer) {
const instance = ImagePreview({ const instance = ImagePreview({
images, images,

View File

@ -56,7 +56,10 @@ setTimeout(() => {
<van-image-preview <van-image-preview
v-model="show" v-model="show"
:images="images" :images="images"
/> @change="onChange"
>
<template v-slot:index>Page: { index }</template>
</van-image-preview>
``` ```
```js ```js
@ -64,11 +67,18 @@ export default {
data() { data() {
return { return {
show: false, show: false,
index: 0,
images: [ images: [
'https://img.yzcdn.cn/1.jpg', 'https://img.yzcdn.cn/1.jpg',
'https://img.yzcdn.cn/2.jpg' 'https://img.yzcdn.cn/2.jpg'
] ]
}; };
},
methods: {
onChange(index) {
this.index = index;
}
} }
} }
``` ```
@ -111,6 +121,12 @@ export default {
| close | Triggered when close | { index, url } | | close | Triggered when close | { index, url } |
| change | Triggered when current image change | index: index of current image | | change | Triggered when current image change | index: index of current image |
### Slot
| name | Description |
|------|------|
| index | Custom index |
### onClose Parematers ### onClose Parematers
| Attribute | Description | Type | | Attribute | Description | Type |

View File

@ -1,5 +1,14 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP // Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`index slot 1`] = `
<div class="van-image-preview" name="van-fade">
<div class="van-image-preview__index">Custom Index</div>
<div class="van-swipe">
<div class="van-swipe__track" style="width: 0px; transition-duration: 0ms; transform: translateX(0px);"></div>
</div>
</div>
`;
exports[`render image 1`] = ` exports[`render image 1`] = `
<div class="van-image-preview" name="van-fade"> <div class="van-image-preview" name="van-fade">
<div class="van-image-preview__index">1/3</div> <div class="van-image-preview__index">1/3</div>

View File

@ -1,12 +1,7 @@
import Vue from 'vue'; import Vue from 'vue';
import ImagePreview from '..'; import ImagePreview from '..';
import ImagePreviewVue from '../ImagePreview'; import ImagePreviewVue from '../ImagePreview';
import { import { mount, trigger, triggerDrag, transitionStub } from '../../../test/utils';
mount,
trigger,
triggerDrag,
transitionStub
} from '../../../test/utils';
transitionStub(); transitionStub();
@ -109,3 +104,15 @@ test('zoom', async () => {
expect(wrapper).toMatchSnapshot(); expect(wrapper).toMatchSnapshot();
Element.prototype.getBoundingClientRect = getBoundingClientRect; Element.prototype.getBoundingClientRect = getBoundingClientRect;
}); });
test('index slot', () => {
const wrapper = mount({
template: `
<van-image-preview :value="true">
<template v-slot:index>Custom Index</template>
</van-image-preview>
`
});
expect(wrapper).toMatchSnapshot();
});

View File

@ -66,7 +66,10 @@ setTimeout(() => {
<van-image-preview <van-image-preview
v-model="show" v-model="show"
:images="images" :images="images"
/> @change="onChange"
>
<template v-slot:index>第{ index }页</template>
</van-image-preview>
``` ```
```js ```js
@ -74,11 +77,18 @@ export default {
data() { data() {
return { return {
show: false, show: false,
index: 0,
images: [ images: [
'https://img.yzcdn.cn/1.jpg', 'https://img.yzcdn.cn/1.jpg',
'https://img.yzcdn.cn/2.jpg' 'https://img.yzcdn.cn/2.jpg'
] ]
}; };
},
methods: {
onChange(index) {
this.index = index;
}
} }
} }
``` ```
@ -127,6 +137,15 @@ export default {
| close | 关闭时触发 | { index: 索引, url: 图片链接 } | | close | 关闭时触发 | { index: 索引, url: 图片链接 } |
| change | 切换当前图片时触发 | index, 当前图片的索引 | | change | 切换当前图片时触发 | index, 当前图片的索引 |
### Slot
通过组件调用 `ImagePreview` 时,支持以下插槽:
| 名称 | 说明 |
|------|------|
| index | 自定义页码内容 |
### onClose 回调参数 ### onClose 回调参数
| 参数名 | 说明 | 类型 | | 参数名 | 说明 | 类型 |