Skip to content

Commit e6093aa

Browse files
committed
feat(img-preview): add imgPreview componnt
1 parent 2f268ca commit e6093aa

File tree

7 files changed

+119
-138
lines changed

7 files changed

+119
-138
lines changed

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626
"lodash-es": "^4.17.15",
2727
"mockjs": "^1.1.0",
2828
"nprogress": "^0.2.0",
29-
"path-to-regexp": "^6.1.0",
29+
"path-to-regexp": "^6.2.0",
3030
"qrcode": "^1.4.4",
3131
"vue": "^3.0.0",
32-
"vue-i18n": "^9.0.0-beta.3",
33-
"vue-router": "^4.0.0-beta.12",
32+
"vue-i18n": "^9.0.0-beta.4",
33+
"vue-router": "^4.0.0-beta.13",
3434
"vuex": "^4.0.0-beta.4",
3535
"vuex-module-decorators": "^1.0.1",
3636
"zxcvbn": "^4.4.2"
@@ -51,8 +51,8 @@
5151
"@types/rollup-plugin-visualizer": "^2.6.0",
5252
"@types/shelljs": "^0.8.8",
5353
"@types/zxcvbn": "^4.4.0",
54-
"@typescript-eslint/eslint-plugin": "^4.2.0",
55-
"@typescript-eslint/parser": "^4.2.0",
54+
"@typescript-eslint/eslint-plugin": "^4.4.0",
55+
"@typescript-eslint/parser": "^4.4.0",
5656
"@vue/compiler-sfc": "^3.0.0",
5757
"autoprefixer": "^9.8.6",
5858
"babel-plugin-import": "^1.13.0",
@@ -63,7 +63,7 @@
6363
"eslint": "^7.10.0",
6464
"eslint-config-prettier": "^6.12.0",
6565
"eslint-plugin-prettier": "^3.1.4",
66-
"eslint-plugin-vue": "^7.0.0-beta.4",
66+
"eslint-plugin-vue": "^7.0.1",
6767
"fs-extra": "^9.0.1",
6868
"husky": "^4.3.0",
6969
"inquirer": "^7.3.3",

src/components/Drawer/src/index.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@import (reference) '../../../design/index.less';
2-
@header-height: 50px;
2+
@header-height: 40px;
33
@footer-height: 60px;
44

55
.basic-drawer {

src/components/Preview/src/index.tsx

Lines changed: 44 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { defineComponent, ref, unref, computed, reactive, watch } from 'vue';
2-
3-
import { FadeTransition } from '/@/components/Transition/index';
1+
import { defineComponent, ref, unref, computed, reactive, watchEffect } from 'vue';
42

53
import { basicProps } from './props';
64
import { Props } from './types';
@@ -11,9 +9,9 @@ import { CloseOutlined, LeftOutlined, RightOutlined } from '@ant-design/icons-vu
119
import resumeSvg from '/@/assets/svg/preview/resume.svg';
1210
import rotateSvg from '/@/assets/svg/preview/p-rotate.svg';
1311
import scaleSvg from '/@/assets/svg/preview/scale.svg';
14-
import unscaleSvg from '/@/assets/svg/preview/unscale.svg';
12+
import unScaleSvg from '/@/assets/svg/preview/unscale.svg';
1513
import loadingSvg from '/@/assets/images/loading.svg';
16-
import unrotateSvg from '/@/assets/svg/preview/unrotate.svg';
14+
import unRotateSvg from '/@/assets/svg/preview/unrotate.svg';
1715
enum StatueEnum {
1816
LOADING,
1917
DONE,
@@ -29,6 +27,7 @@ interface ImgState {
2927
status: StatueEnum;
3028
moveX: number;
3129
moveY: number;
30+
show: boolean;
3231
}
3332

3433
const prefixCls = 'img-preview';
@@ -46,7 +45,9 @@ export default defineComponent({
4645
currentIndex: 0,
4746
moveX: 0,
4847
moveY: 0,
48+
show: props.show,
4949
});
50+
5051
const wrapElRef = ref<HTMLDivElement | null>(null);
5152
const imgElRef = ref<HTMLImageElement | null>(null);
5253

@@ -133,16 +134,15 @@ export default defineComponent({
133134
}
134135

135136
// 关闭
136-
function handleClose() {
137-
const { instance } = props;
138-
if (instance) {
139-
instance.show = false;
140-
}
137+
function handleClose(e: MouseEvent) {
138+
e && e.stopPropagation();
139+
imgState.show = false;
141140
// 移除火狐浏览器下的鼠标滚动事件
142141
document.body.removeEventListener('DOMMouseScroll', scrollFunc);
143142
// 恢复火狐及Safari浏览器下的图片拖拽
144143
document.ondragstart = null;
145144
}
145+
146146
// 图片复原
147147
function resume() {
148148
initState();
@@ -202,26 +202,15 @@ export default defineComponent({
202202
const { imageList } = props;
203203
return imageList.length > 1;
204204
});
205-
watch(
206-
() => props.show,
207-
(show) => {
208-
if (show) {
209-
init();
210-
}
211-
},
212-
{
213-
immediate: true,
205+
206+
watchEffect(() => {
207+
if (props.show) {
208+
init();
214209
}
215-
);
216-
watch(
217-
() => props.imageList,
218-
() => {
210+
if (props.imageList) {
219211
initState();
220-
},
221-
{
222-
immediate: true,
223212
}
224-
);
213+
});
225214

226215
const renderClose = () => {
227216
return (
@@ -247,7 +236,7 @@ export default defineComponent({
247236
return (
248237
<div class={`${prefixCls}__controller`}>
249238
<div class={`${prefixCls}__controller-item`} onClick={() => scaleFunc(-0.15)}>
250-
<img src={unscaleSvg} />
239+
<img src={unScaleSvg} />
251240
</div>
252241
<div class={`${prefixCls}__controller-item`} onClick={() => scaleFunc(0.15)}>
253242
<img src={scaleSvg} />
@@ -256,7 +245,7 @@ export default defineComponent({
256245
<img src={resumeSvg} />
257246
</div>
258247
<div class={`${prefixCls}__controller-item`} onClick={() => rotateFunc(-90)}>
259-
<img src={unrotateSvg} />
248+
<img src={unRotateSvg} />
260249
</div>
261250
<div class={`${prefixCls}__controller-item`} onClick={() => rotateFunc(90)}>
262251
<img src={rotateSvg} />
@@ -277,41 +266,32 @@ export default defineComponent({
277266
};
278267
return () => {
279268
return (
280-
<FadeTransition>
281-
{() =>
282-
props.show && (
283-
<div class={prefixCls} ref={wrapElRef} onMouseup={handleMouseUp}>
284-
<div class={`${prefixCls}-content`}>
285-
<img
286-
width="32"
287-
src={loadingSvg}
288-
v-show={imgState.status === StatueEnum.LOADING}
289-
class={`${prefixCls}-image`}
290-
/>
291-
<img
292-
v-show={imgState.status === StatueEnum.DONE}
293-
style={unref(getImageStyle)}
294-
class={`${prefixCls}-image`}
295-
ref={imgElRef}
296-
src={imgState.currentUrl}
297-
onMousedown={handleAddMoveListener}
298-
/>
299-
<img
300-
width="32"
301-
src={loadingSvg}
302-
v-show={imgState.status === StatueEnum.LOADING}
303-
class={`${prefixCls}-image`}
304-
/>
305-
{renderClose()}
306-
{renderIndex()}
307-
{renderController()}
308-
{renderArrow('left')}
309-
{renderArrow('right')}
310-
</div>
311-
</div>
312-
)
313-
}
314-
</FadeTransition>
269+
imgState.show && (
270+
<div class={prefixCls} ref={wrapElRef} onMouseup={handleMouseUp}>
271+
<div class={`${prefixCls}-content`}>
272+
<img
273+
width="32"
274+
src={loadingSvg}
275+
class={[
276+
`${prefixCls}-image`,
277+
imgState.status === StatueEnum.LOADING ? '' : 'hidden',
278+
]}
279+
/>
280+
<img
281+
style={unref(getImageStyle)}
282+
class={[`${prefixCls}-image`, imgState.status === StatueEnum.DONE ? '' : 'hidden']}
283+
ref={imgElRef}
284+
src={imgState.currentUrl}
285+
onMousedown={handleAddMoveListener}
286+
/>
287+
{renderClose()}
288+
{renderIndex()}
289+
{renderController()}
290+
{renderArrow('left')}
291+
{renderArrow('right')}
292+
</div>
293+
</div>
294+
)
315295
);
316296
};
317297
},

src/components/Preview/src/props.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
import { PropType } from 'vue';
2-
import { Props } from './types';
32
export const basicProps = {
43
show: {
54
type: Boolean as PropType<boolean>,
65
default: false,
76
},
8-
instance: {
9-
type: Object as PropType<Props>,
10-
default: null,
11-
},
127
imageList: {
138
type: [Array] as PropType<string[]>,
149
default: null,

src/router/menus/modules/demo/feat.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ const menu: MenuModule = {
1313
path: '/context-menu',
1414
name: '右键菜单',
1515
},
16-
// {
17-
// path: '/img-preview',
18-
// name: '图片预览',
19-
// },
16+
{
17+
path: '/img-preview',
18+
name: '图片预览',
19+
},
2020
{
2121
path: '/i18n',
2222
name: '国际化',

src/views/demo/feat/img-preview/index.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
<div class="flex justify-center mt-4">
55
<img :src="img" v-for="img in imgList" :key="img" class="mr-2" @click="handleClick(img)" />
66
</div>
7+
<Alert message="无预览图" type="info" />
8+
<a-button @click="handlePreview" type="primary" class="mt-4">预览图片</a-button>
79
</div>
810
</template>
911
<script lang="ts">
@@ -21,7 +23,11 @@
2123
function handleClick(img: string) {
2224
createImgPreview({ imageList: [img] });
2325
}
24-
return { imgList, handleClick };
26+
27+
function handlePreview() {
28+
createImgPreview({ imageList: imgList });
29+
}
30+
return { imgList, handleClick, handlePreview };
2531
},
2632
});
2733
</script>

0 commit comments

Comments
 (0)