Skip to content

Commit 0b559c1

Browse files
committed
fix: 优化 callback 的类型
1 parent 02213bd commit 0b559c1

File tree

6 files changed

+41
-14
lines changed

6 files changed

+41
-14
lines changed

packages/fighting-components/image/src/PreviewList.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ export const Props = {
2828
previewRound: {
2929
type: String,
3030
default: (): string => ''
31+
},
32+
width: {
33+
type: String,
34+
default: (): string => ''
3135
}
3236
} as const
3337

packages/fighting-components/image/src/image.vue

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
v-if="caption"
2323
class="f-image-caption"
2424
:style="{
25+
width: width || `${captionWidth}px`,
2526
color: captionColor,
2627
borderBottomLeftRadius: round,
2728
borderBottomRightRadius: round
@@ -40,6 +41,7 @@
4041
:modalClose="modalClose"
4142
:showCloseBtn="showCloseBtn"
4243
:previewRound="previewRound"
44+
:width="width"
4345
@close="onClose"
4446
/>
4547
</div>
@@ -56,13 +58,17 @@
5658
import { onMounted, ref, defineAsyncComponent, computed } from 'vue'
5759
import { loadImage } from '@fighting-design/fighting-utils'
5860
import type { Ref, ComputedRef } from 'vue'
59-
import type { ordinaryFunctionInterface } from '@fighting-design/fighting-type'
61+
import type {
62+
ordinaryFunctionInterface,
63+
callbackInterface
64+
} from '@fighting-design/fighting-type'
6065
6166
const prop = defineProps(Props)
6267
const emit = defineEmits(Emits)
6368
6469
const isError: Ref<boolean> = ref<boolean>(true)
6570
const isPreviewListShow: Ref<boolean> = ref<boolean>(false)
71+
const captionWidth: Ref<number> = ref<number>(0)
6672
const FImageImg: Ref<HTMLImageElement | null> = ref<HTMLImageElement | null>(
6773
null
6874
)
@@ -88,9 +94,15 @@
8894
8995
onMounted((): void => {
9096
const node: HTMLImageElement = FImageImg.value as HTMLImageElement
91-
loadImage(node, prop, emit, (params: boolean): void => {
97+
const callback: callbackInterface = (
98+
params: boolean,
99+
width: number
100+
): void => {
92101
isError.value = params
93-
})
102+
captionWidth.value = width
103+
}
104+
105+
loadImage(node, prop, emit, callback)
94106
})
95107
</script>
96108

packages/fighting-type/image.d.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ export interface loadImageInterface {
88
node: HTMLImageElement,
99
prop: propsInterface,
1010
emit: Function,
11-
callback: Function
11+
callback: callbackInterface
1212
): void
1313
}
1414

1515
export interface LoadInterface {
1616
img: HTMLImageElement
1717
props: propsInterface
1818
emit: Function
19-
callback: Function
19+
callback: callbackInterface
2020
loadCreateImg(): void
2121
loadNextImg(): void
2222
onerror(evt: Event): void
@@ -59,3 +59,7 @@ export interface switchImageInterface {
5959
export interface optionClickInterface {
6060
(evt: PointerEvent): void
6161
}
62+
63+
export interface callbackInterface {
64+
(params: boolean, width: number): void
65+
}

packages/fighting-type/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ export type {
1010
callbackType,
1111
btnClickInterface,
1212
switchImageInterface,
13-
optionClickInterface
13+
optionClickInterface,
14+
callbackInterface
1415
} from './image'
1516
export type { installInterface, mainVNodeInterface } from './install'
1617
export type { onClickInterface, ordinaryFunctionInterface } from './auto'

packages/fighting-utils/loadImage.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import type {
22
LazyInterface,
33
LoadInterface,
44
loadImageInterface,
5-
propsInterface
5+
propsInterface,
6+
callbackInterface
67
} from '@fighting-design/fighting-type'
78

89
/**
@@ -12,12 +13,12 @@ class Load implements LoadInterface {
1213
img: HTMLImageElement
1314
props: propsInterface
1415
emit: Function
15-
callback: Function
16+
callback: callbackInterface
1617
constructor(
1718
img: HTMLImageElement,
1819
props: propsInterface,
1920
emit: Function,
20-
callback: Function
21+
callback: callbackInterface
2122
) {
2223
this.img = img
2324
this.props = props
@@ -40,10 +41,11 @@ class Load implements LoadInterface {
4041
return this.loadNextImg()
4142
}
4243
this.emit('error', evt)
43-
this.callback(false)
44+
this.callback(false, 0)
4445
}
4546
onload(evt: Event): void {
4647
this.emit('load', evt)
48+
this.callback(true, this.img.width)
4749
}
4850
// 如果加载 src 失败,则进入这里,加载 err-src 的图片地址
4951
loadNextImg(): void {
@@ -53,7 +55,7 @@ class Load implements LoadInterface {
5355
newImg.addEventListener('error', (evt: Event): void => {
5456
// 进入这里则说明 err-src 的图片也加载失败了
5557
this.emit('error', evt)
56-
this.callback(false)
58+
this.callback(false, 0)
5759
})
5860

5961
newImg.addEventListener('load', (evt: Event): void => {
@@ -72,7 +74,7 @@ class Lazy extends Load implements LazyInterface {
7274
img: HTMLImageElement,
7375
props: propsInterface,
7476
emit: Function,
75-
callback: Function
77+
callback: callbackInterface
7678
) {
7779
super(img, props, emit, callback)
7880
}
@@ -111,7 +113,7 @@ export const loadImage: loadImageInterface = (
111113
node: HTMLImageElement,
112114
prop: propsInterface,
113115
emit: Function,
114-
callback: Function
116+
callback: callbackInterface
115117
): void => {
116118
if (prop.lazy) {
117119
const lazy: Lazy = new Lazy(node, prop, emit, callback)

start/src/App.vue

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<template>
22
<div style="height: 1000px"></div>
33
<f-image
4-
width="200px"
54
root-margin="100px"
65
src="https://tianyuhao.cn/images/auto/1.png"
76
:draggable="false"
@@ -48,6 +47,11 @@
4847
</script>
4948

5049
<style>
50+
html,
51+
body {
52+
width: 100%;
53+
height: 100%;
54+
}
5155
.list {
5256
width: 300px;
5357
height: 200px;

0 commit comments

Comments
 (0)