Skip to content

Commit ce4e0cc

Browse files
authored
[new feature] ImagePreview: add index slot (youzan#3157)
1 parent 8d1c02c commit ce4e0cc

6 files changed

Lines changed: 92 additions & 18 deletions

File tree

packages/image-preview/ImagePreview.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ const [sfc, bem] = use('image-preview');
88

99
function getDistance(touches) {
1010
return Math.sqrt(
11-
Math.abs((touches[0].clientX - touches[1].clientX) * (touches[0].clientY - touches[1].clientY))
11+
Math.abs(
12+
(touches[0].clientX - touches[1].clientX) *
13+
(touches[0].clientY - touches[1].clientY)
14+
)
1215
);
1316
}
1417

@@ -71,8 +74,8 @@ export default sfc({
7174
};
7275

7376
if (scale !== 1) {
74-
style.transform = `scale3d(${scale}, ${scale}, 1) translate(${this.moveX / scale}px, ${this
75-
.moveY / scale}px)`;
77+
style.transform = `scale3d(${scale}, ${scale}, 1) translate(${this.moveX /
78+
scale}px, ${this.moveY / scale}px)`;
7679
}
7780

7881
return style;
@@ -175,7 +178,11 @@ export default sfc({
175178
if (this.moving || this.zooming) {
176179
let stopPropagation = true;
177180

178-
if (this.moving && this.startMoveX === this.moveX && this.startMoveY === this.moveY) {
181+
if (
182+
this.moving &&
183+
this.startMoveX === this.moveX &&
184+
this.startMoveY === this.moveY
185+
) {
179186
stopPropagation = false;
180187
}
181188

@@ -219,7 +226,9 @@ export default sfc({
219226
const { active, images } = this;
220227

221228
const Index = this.showIndex && (
222-
<div class={bem('index')}>{`${active + 1}/${images.length}`}</div>
229+
<div class={bem('index')}>
230+
{this.slots('index') || `${active + 1}/${images.length}`}
231+
</div>
223232
);
224233

225234
const Images = (
@@ -244,7 +253,11 @@ export default sfc({
244253
};
245254
return (
246255
<SwipeItem>
247-
{this.lazyLoad ? <img vLazy={image} {...props} /> : <img src={image} {...props} />}
256+
{this.lazyLoad ? (
257+
<img vLazy={image} {...props} />
258+
) : (
259+
<img src={image} {...props} />
260+
)}
248261
</SwipeItem>
249262
);
250263
})}

packages/image-preview/demo/index.vue

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
<van-image-preview
1818
v-model="show"
1919
:images="images"
20-
/>
20+
@change="onChange"
21+
>
22+
<template v-slot:index>{{ $t('index', index) }}</template>
23+
</van-image-preview>
2124
</demo-block>
2225
</demo-section>
2326
</template>
@@ -38,20 +41,23 @@ export default {
3841
button1: '预览图片',
3942
button2: '指定初始位置',
4043
button3: '异步关闭',
41-
componentCall: '组件调用'
44+
componentCall: '组件调用',
45+
index: index => `${index + 1}`
4246
},
4347
'en-US': {
4448
button1: 'Show Images',
4549
button2: 'Custom Start Position',
4650
button3: 'Async Close',
47-
componentCall: 'Component Call'
51+
componentCall: 'Component Call',
52+
index: index => `Page: ${index}`
4853
}
4954
},
5055
5156
data() {
5257
return {
5358
show: false,
54-
images
59+
images,
60+
index: 0
5561
};
5662
},
5763
@@ -60,6 +66,10 @@ export default {
6066
this.show = true;
6167
},
6268
69+
onChange(index) {
70+
this.index = index;
71+
},
72+
6373
showImagePreview(position, timer) {
6474
const instance = ImagePreview({
6575
images,

packages/image-preview/en-US.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,29 @@ setTimeout(() => {
5656
<van-image-preview
5757
v-model="show"
5858
:images="images"
59-
/>
59+
@change="onChange"
60+
>
61+
<template v-slot:index>Page: { index }</template>
62+
</van-image-preview>
6063
```
6164

6265
```js
6366
export default {
6467
data() {
6568
return {
6669
show: false,
70+
index: 0,
6771
images: [
6872
'https://img.yzcdn.cn/1.jpg',
6973
'https://img.yzcdn.cn/2.jpg'
7074
]
7175
};
76+
},
77+
78+
methods: {
79+
onChange(index) {
80+
this.index = index;
81+
}
7282
}
7383
}
7484
```
@@ -111,6 +121,12 @@ export default {
111121
| close | Triggered when close | { index, url } |
112122
| change | Triggered when current image change | index: index of current image |
113123

124+
### Slot
125+
126+
| name | Description |
127+
|------|------|
128+
| index | Custom index |
129+
114130
### onClose Parematers
115131

116132
| Attribute | Description | Type |

packages/image-preview/test/__snapshots__/index.spec.js.snap

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`index slot 1`] = `
4+
<div class="van-image-preview" name="van-fade">
5+
<div class="van-image-preview__index">Custom Index</div>
6+
<div class="van-swipe">
7+
<div class="van-swipe__track" style="width: 0px; transition-duration: 0ms; transform: translateX(0px);"></div>
8+
</div>
9+
</div>
10+
`;
11+
312
exports[`render image 1`] = `
413
<div class="van-image-preview" name="van-fade">
514
<div class="van-image-preview__index">1/3</div>

packages/image-preview/test/index.spec.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
import Vue from 'vue';
22
import ImagePreview from '..';
33
import ImagePreviewVue from '../ImagePreview';
4-
import {
5-
mount,
6-
trigger,
7-
triggerDrag,
8-
transitionStub
9-
} from '../../../test/utils';
4+
import { mount, trigger, triggerDrag, transitionStub } from '../../../test/utils';
105

116
transitionStub();
127

@@ -109,3 +104,15 @@ test('zoom', async () => {
109104
expect(wrapper).toMatchSnapshot();
110105
Element.prototype.getBoundingClientRect = getBoundingClientRect;
111106
});
107+
108+
test('index slot', () => {
109+
const wrapper = mount({
110+
template: `
111+
<van-image-preview :value="true">
112+
<template v-slot:index>Custom Index</template>
113+
</van-image-preview>
114+
`
115+
});
116+
117+
expect(wrapper).toMatchSnapshot();
118+
});

packages/image-preview/zh-CN.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,29 @@ setTimeout(() => {
6666
<van-image-preview
6767
v-model="show"
6868
:images="images"
69-
/>
69+
@change="onChange"
70+
>
71+
<template v-slot:index>第{ index }页</template>
72+
</van-image-preview>
7073
```
7174

7275
```js
7376
export default {
7477
data() {
7578
return {
7679
show: false,
80+
index: 0,
7781
images: [
7882
'https://img.yzcdn.cn/1.jpg',
7983
'https://img.yzcdn.cn/2.jpg'
8084
]
8185
};
86+
},
87+
88+
methods: {
89+
onChange(index) {
90+
this.index = index;
91+
}
8292
}
8393
}
8494
```
@@ -127,6 +137,15 @@ export default {
127137
| close | 关闭时触发 | { index: 索引, url: 图片链接 } |
128138
| change | 切换当前图片时触发 | index, 当前图片的索引 |
129139

140+
### Slot
141+
142+
通过组件调用 `ImagePreview` 时,支持以下插槽:
143+
144+
| 名称 | 说明 |
145+
|------|------|
146+
| index | 自定义页码内容 |
147+
148+
130149
### onClose 回调参数
131150

132151
| 参数名 | 说明 | 类型 |

0 commit comments

Comments
 (0)