-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathutil.ts
46 lines (40 loc) · 1.11 KB
/
util.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { getCurrentInstance, isRef, onMounted, onUnmounted, Ref } from 'vue'
export function def<T extends object>(obj: T, key: keyof T, val: any, enumerable?: boolean) {
Object.defineProperty(obj, key, {
value: val,
enumerable: !!enumerable,
writable: true,
configurable: true
})
}
export type Target = Ref<EventTarget | null> | EventTarget | string
export function getTarget(target: Target): EventTarget {
if (typeof target === 'string') {
const dom = document.querySelector(target)
if (!dom && process.env.NODE_ENV !== 'production') {
console.error('target is not found')
throw Error(`target of selector ${target} is not found`)
}
return dom!
}
if (isRef(target)) {
return target.value!
}
return target
}
export function tryOnMounted(cb: () => any): void {
const instance = getCurrentInstance()
if (instance) {
if (instance?.isMounted) {
cb()
} else {
onMounted(cb)
}
}
}
export function tryOnUnmounted(cb: () => any): void {
if (getCurrentInstance()) {
onUnmounted(cb)
}
}
export const isClient = typeof window !== 'undefined'