-
Notifications
You must be signed in to change notification settings - Fork 299
feat(loading): [loading] added type definition and interface of Loading component to optimize loading style processing #3253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -14,8 +14,29 @@ import { PopupManager } from '@opentiny/utils' | |||||||||||||||||||||||||||||||||||||||
import { getStyle, addClass } from '@opentiny/utils' | ||||||||||||||||||||||||||||||||||||||||
import { createComponent, hooks, appProperties } from '@opentiny/vue-common' | ||||||||||||||||||||||||||||||||||||||||
import Loading from './index' | ||||||||||||||||||||||||||||||||||||||||
import type { ILoadingState } from '@opentiny/vue-renderless/types/loading.type' | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
interface LoadingConfig { | ||||||||||||||||||||||||||||||||||||||||
text?: string | null | ||||||||||||||||||||||||||||||||||||||||
body?: boolean | ||||||||||||||||||||||||||||||||||||||||
lock?: boolean | ||||||||||||||||||||||||||||||||||||||||
customClass?: string | ||||||||||||||||||||||||||||||||||||||||
fullscreen?: boolean | ||||||||||||||||||||||||||||||||||||||||
iconSize?: string | ||||||||||||||||||||||||||||||||||||||||
target?: HTMLElement | string | ||||||||||||||||||||||||||||||||||||||||
size?: string | ||||||||||||||||||||||||||||||||||||||||
loadingImg?: string | ||||||||||||||||||||||||||||||||||||||||
tiny_mode?: string | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
interface LoadingInstance { | ||||||||||||||||||||||||||||||||||||||||
state: ILoadingState | ||||||||||||||||||||||||||||||||||||||||
$el: HTMLElement | ||||||||||||||||||||||||||||||||||||||||
originalPosition?: string | ||||||||||||||||||||||||||||||||||||||||
originalOverflow?: string | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
export const defaults = { | ||||||||||||||||||||||||||||||||||||||||
export const defaults: LoadingConfig = { | ||||||||||||||||||||||||||||||||||||||||
text: null, | ||||||||||||||||||||||||||||||||||||||||
body: false, | ||||||||||||||||||||||||||||||||||||||||
lock: false, | ||||||||||||||||||||||||||||||||||||||||
|
@@ -24,7 +45,7 @@ export const defaults = { | |||||||||||||||||||||||||||||||||||||||
iconSize: '' | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
let fullscreenLoading = null | ||||||||||||||||||||||||||||||||||||||||
let fullscreenLoading: LoadingInstance | null = null | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
export const constants = { | ||||||||||||||||||||||||||||||||||||||||
TEXT_ATTR: 'tiny-loading__text', | ||||||||||||||||||||||||||||||||||||||||
|
@@ -36,45 +57,48 @@ export const constants = { | |||||||||||||||||||||||||||||||||||||||
PARENT_RELATIVE_CLS: 'tiny-loading__parent-relative', | ||||||||||||||||||||||||||||||||||||||||
LOAD_ICON_TEXT: 'ui.load.dot' | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
const addStyle = (options, parent, instance) => { | ||||||||||||||||||||||||||||||||||||||||
let maskStyle = {} | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
const addStyle = (options: LoadingConfig, parent: HTMLElement, instance: LoadingInstance) => { | ||||||||||||||||||||||||||||||||||||||||
let maskStyle: Record<string, string> = {} | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
if (options.fullscreen) { | ||||||||||||||||||||||||||||||||||||||||
instance.originalPosition = getStyle(document.body, 'position') | ||||||||||||||||||||||||||||||||||||||||
instance.originalOverflow = getStyle(document.body, 'overflow') | ||||||||||||||||||||||||||||||||||||||||
maskStyle.zIndex = PopupManager.nextZIndex() | ||||||||||||||||||||||||||||||||||||||||
instance.originalPosition = getStyle(document.body, 'position') || '' | ||||||||||||||||||||||||||||||||||||||||
instance.originalOverflow = getStyle(document.body, 'overflow') || '' | ||||||||||||||||||||||||||||||||||||||||
maskStyle.zIndex = PopupManager.nextZIndex().toString() | ||||||||||||||||||||||||||||||||||||||||
} else if (options.body) { | ||||||||||||||||||||||||||||||||||||||||
const clientRect = options.target.getBoundingClientRect() | ||||||||||||||||||||||||||||||||||||||||
const target = options.target as HTMLElement | ||||||||||||||||||||||||||||||||||||||||
const clientRect = target?.getBoundingClientRect() | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
instance.originalPosition = getStyle(document.body, 'position') | ||||||||||||||||||||||||||||||||||||||||
instance.originalPosition = getStyle(document.body, 'position') || '' | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
const direction = ['top', 'left'] | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
direction.forEach((property) => { | ||||||||||||||||||||||||||||||||||||||||
let scroll = property === 'top' ? 'scrollTop' : 'scrollLeft' | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
maskStyle[property] = clientRect[property] + document.body[scroll] + document.documentElement[scroll] + 'px' | ||||||||||||||||||||||||||||||||||||||||
maskStyle[property] = | ||||||||||||||||||||||||||||||||||||||||
(clientRect?.[property] || 0) + document.body[scroll] + document.documentElement[scroll] + 'px' | ||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
const size = ['height', 'width'] | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
size.forEach((property) => { | ||||||||||||||||||||||||||||||||||||||||
maskStyle[property] = clientRect[property] + 'px' | ||||||||||||||||||||||||||||||||||||||||
maskStyle[property] = (clientRect?.[property] || 0) + 'px' | ||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||
instance.originalPosition = getStyle(parent, 'position') | ||||||||||||||||||||||||||||||||||||||||
instance.originalPosition = getStyle(parent, 'position') || '' | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
Object.keys(maskStyle).forEach((property) => { | ||||||||||||||||||||||||||||||||||||||||
instance.$el.style[property] = maskStyle[property] | ||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
export default (configs = {}) => { | ||||||||||||||||||||||||||||||||||||||||
export default (configs: LoadingConfig = {}) => { | ||||||||||||||||||||||||||||||||||||||||
configs = { ...defaults, ...configs } | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
if (typeof configs.target === 'string') { | ||||||||||||||||||||||||||||||||||||||||
configs.target = document.querySelector(configs.target) | ||||||||||||||||||||||||||||||||||||||||
configs.target = document.querySelector(configs.target) as HTMLElement | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
Comment on lines
+97
to
102
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Unsafe type assertion for querySelector result. The type assertion Consider a more defensive approach: - configs.target = document.querySelector(configs.target) as HTMLElement
+ const targetElement = document.querySelector(configs.target)
+ if (!targetElement) {
+ console.warn(`[Loading] Target element not found: ${configs.target}`)
+ }
+ configs.target = targetElement as HTMLElement 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
configs.target = configs.target || document.body | ||||||||||||||||||||||||||||||||||||||||
|
@@ -104,7 +128,7 @@ export default (configs = {}) => { | |||||||||||||||||||||||||||||||||||||||
tiny_mode: configs.tiny_mode || appProperties().tiny_mode?.value | ||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||
el: document.createElement('div') | ||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||
}) as LoadingInstance | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
for (const key in configs) { | ||||||||||||||||||||||||||||||||||||||||
if (Object.prototype.hasOwnProperty.call(configs, key)) { | ||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Duplicate interface definitions between files.
The LoadingConfig and LoadingInstance interfaces are defined both here and in index.ts. Consider moving these to a shared types file to avoid duplication and potential inconsistencies.
Then import these types in both files:
// In index.ts and service.ts +import type { LoadingConfig, LoadingInstance } from './types'