Skip to content

Commit 5a670fb

Browse files
authored
feat(loading): [loading] added type definition and interface of Loading component to optimize loading style processing (#3253)
1 parent 4b7ae8c commit 5a670fb

File tree

7 files changed

+112
-28
lines changed

7 files changed

+112
-28
lines changed

packages/modules.json

+12
Original file line numberDiff line numberDiff line change
@@ -2237,6 +2237,7 @@
22372237
"type": "component",
22382238
"exclude": false,
22392239
"mode": [
2240+
"mobile-first",
22402241
"pc"
22412242
]
22422243
},
@@ -2245,14 +2246,25 @@
22452246
"type": "component",
22462247
"exclude": false,
22472248
"mode": [
2249+
"mobile-first",
22482250
"pc"
22492251
]
22502252
},
2253+
"SkeletonItemMobileFirst": {
2254+
"path": "vue/src/skeleton-item/src/mobile-first.vue",
2255+
"type": "template",
2256+
"exclude": false
2257+
},
22512258
"SkeletonItemPc": {
22522259
"path": "vue/src/skeleton-item/src/pc.vue",
22532260
"type": "template",
22542261
"exclude": false
22552262
},
2263+
"SkeletonMobileFirst": {
2264+
"path": "vue/src/skeleton/src/mobile-first.vue",
2265+
"type": "template",
2266+
"exclude": false
2267+
},
22562268
"SkeletonPc": {
22572269
"path": "vue/src/skeleton/src/pc.vue",
22582270
"type": "template",

packages/vue-common/src/adapter/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ export type {
88
DefineComponent,
99
ComponentPublicInstance,
1010
SetupContext,
11-
ComputedRef
11+
ComputedRef,
12+
App
1213
} from 'virtual:common/adapter/vue'
1314

1415
export default vue

packages/vue-common/src/adapter/vue2/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,8 @@ export type {
353353
ExtractPropTypes,
354354
ComponentPublicInstance,
355355
SetupContext,
356-
ComputedRef
356+
ComputedRef,
357+
App
357358
} from '@vue/composition-api'
358359

359360
export type DefineComponent = typeof defineComponent

packages/vue-common/src/adapter/vue3/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -493,5 +493,6 @@ export type {
493493
DefineComponent,
494494
ComponentPublicInstance,
495495
SetupContext,
496-
ComputedRef
496+
ComputedRef,
497+
App
497498
} from 'vue'

packages/vue-common/src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,8 @@ export type {
377377
DefineComponent,
378378
ComponentPublicInstance,
379379
SetupContext,
380-
ComputedRef
380+
ComputedRef,
381+
App
381382
} from './adapter'
382383

383384
export {

packages/vue/src/loading/index.ts

+53-9
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,59 @@ import service from './src/service'
1313
import directive from './src/directive'
1414
import { setupComponent } from '@opentiny/vue-common'
1515
import { version } from './package.json'
16+
import type { App } from '@opentiny/vue-common'
17+
import type { ILoadingState } from '@opentiny/vue-renderless/types/loading.type'
1618

17-
const Loadings: any = {
18-
install(app) {
19+
// 定义LoadingConfig接口
20+
interface LoadingConfig {
21+
text?: string | null
22+
body?: boolean
23+
lock?: boolean
24+
customClass?: string
25+
fullscreen?: boolean
26+
iconSize?: string
27+
target?: HTMLElement | string
28+
size?: string
29+
loadingImg?: string
30+
tiny_mode?: string
31+
}
32+
33+
// 定义LoadingInstance接口
34+
interface LoadingInstance {
35+
state: ILoadingState
36+
$el: HTMLElement
37+
originalPosition?: string
38+
originalOverflow?: string
39+
}
40+
41+
// 定义Loading服务的类型
42+
type LoadingService = (config?: LoadingConfig) => LoadingInstance
43+
44+
// 定义LoadingPlugin接口
45+
interface LoadingPlugin {
46+
install: (app: App) => void
47+
service: LoadingService
48+
directive: any
49+
version: string
50+
}
51+
52+
// 定义Root接口
53+
interface Root {
54+
$apiPrefix?: string
55+
[key: string]: any
56+
}
57+
58+
// 向setupComponent添加TINYLoading属性
59+
// @ts-expect-error setupComponent上没有TINYLoading属性,但是在运行时会添加这个属性
60+
setupComponent.TINYLoading = {
61+
init(root: Root) {
62+
let prefix = root.$apiPrefix || '$'
63+
root[`${prefix}loading`] = service
64+
}
65+
}
66+
67+
const Loadings: LoadingPlugin = {
68+
install(app: App) {
1969
app.directive('loading', directive)
2070
},
2171
service,
@@ -26,15 +76,9 @@ const Loadings: any = {
2676
/* istanbul ignore next */
2777
if (process.env.BUILD_TARGET === 'runtime') {
2878
if (typeof window !== 'undefined' && window.Vue) {
79+
// @ts-expect-error window.Vue可能是Vue2或Vue3实例,类型兼容性在运行时处理
2980
Loadings.install(window.Vue)
3081
}
3182
}
3283

33-
setupComponent.TINYLoading = {
34-
init(root) {
35-
let prefix = root.$apiPrefix || '$'
36-
root[`${prefix}loading`] = service
37-
}
38-
}
39-
4084
export default Loadings

packages/vue/src/loading/src/service.ts

+39-15
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,29 @@ import { PopupManager } from '@opentiny/utils'
1414
import { getStyle, addClass } from '@opentiny/utils'
1515
import { createComponent, hooks, appProperties } from '@opentiny/vue-common'
1616
import Loading from './index'
17+
import type { ILoadingState } from '@opentiny/vue-renderless/types/loading.type'
18+
19+
interface LoadingConfig {
20+
text?: string | null
21+
body?: boolean
22+
lock?: boolean
23+
customClass?: string
24+
fullscreen?: boolean
25+
iconSize?: string
26+
target?: HTMLElement | string
27+
size?: string
28+
loadingImg?: string
29+
tiny_mode?: string
30+
}
31+
32+
interface LoadingInstance {
33+
state: ILoadingState
34+
$el: HTMLElement
35+
originalPosition?: string
36+
originalOverflow?: string
37+
}
1738

18-
export const defaults = {
39+
export const defaults: LoadingConfig = {
1940
text: null,
2041
body: false,
2142
lock: false,
@@ -24,7 +45,7 @@ export const defaults = {
2445
iconSize: ''
2546
}
2647

27-
let fullscreenLoading = null
48+
let fullscreenLoading: LoadingInstance | null = null
2849

2950
export const constants = {
3051
TEXT_ATTR: 'tiny-loading__text',
@@ -36,45 +57,48 @@ export const constants = {
3657
PARENT_RELATIVE_CLS: 'tiny-loading__parent-relative',
3758
LOAD_ICON_TEXT: 'ui.load.dot'
3859
}
39-
const addStyle = (options, parent, instance) => {
40-
let maskStyle = {}
60+
61+
const addStyle = (options: LoadingConfig, parent: HTMLElement, instance: LoadingInstance) => {
62+
let maskStyle: Record<string, string> = {}
4163

4264
if (options.fullscreen) {
43-
instance.originalPosition = getStyle(document.body, 'position')
44-
instance.originalOverflow = getStyle(document.body, 'overflow')
45-
maskStyle.zIndex = PopupManager.nextZIndex()
65+
instance.originalPosition = getStyle(document.body, 'position') || ''
66+
instance.originalOverflow = getStyle(document.body, 'overflow') || ''
67+
maskStyle.zIndex = PopupManager.nextZIndex().toString()
4668
} else if (options.body) {
47-
const clientRect = options.target.getBoundingClientRect()
69+
const target = options.target as HTMLElement
70+
const clientRect = target?.getBoundingClientRect()
4871

49-
instance.originalPosition = getStyle(document.body, 'position')
72+
instance.originalPosition = getStyle(document.body, 'position') || ''
5073

5174
const direction = ['top', 'left']
5275

5376
direction.forEach((property) => {
5477
let scroll = property === 'top' ? 'scrollTop' : 'scrollLeft'
5578

56-
maskStyle[property] = clientRect[property] + document.body[scroll] + document.documentElement[scroll] + 'px'
79+
maskStyle[property] =
80+
(clientRect?.[property] || 0) + document.body[scroll] + document.documentElement[scroll] + 'px'
5781
})
5882

5983
const size = ['height', 'width']
6084

6185
size.forEach((property) => {
62-
maskStyle[property] = clientRect[property] + 'px'
86+
maskStyle[property] = (clientRect?.[property] || 0) + 'px'
6387
})
6488
} else {
65-
instance.originalPosition = getStyle(parent, 'position')
89+
instance.originalPosition = getStyle(parent, 'position') || ''
6690
}
6791

6892
Object.keys(maskStyle).forEach((property) => {
6993
instance.$el.style[property] = maskStyle[property]
7094
})
7195
}
7296

73-
export default (configs = {}) => {
97+
export default (configs: LoadingConfig = {}) => {
7498
configs = { ...defaults, ...configs }
7599

76100
if (typeof configs.target === 'string') {
77-
configs.target = document.querySelector(configs.target)
101+
configs.target = document.querySelector(configs.target) as HTMLElement
78102
}
79103

80104
configs.target = configs.target || document.body
@@ -104,7 +128,7 @@ export default (configs = {}) => {
104128
tiny_mode: configs.tiny_mode || appProperties().tiny_mode?.value
105129
},
106130
el: document.createElement('div')
107-
})
131+
}) as LoadingInstance
108132

109133
for (const key in configs) {
110134
if (Object.prototype.hasOwnProperty.call(configs, key)) {

0 commit comments

Comments
 (0)