goview_vue/src/utils/utils.ts

186 lines
3.9 KiB
TypeScript
Raw Normal View History

2021-12-20 14:29:29 +08:00
import { h } from 'vue'
import { NIcon } from 'naive-ui'
import screenfull from 'screenfull'
2022-02-03 22:54:31 +08:00
import throttle from 'lodash/throttle'
import Image_404 from '../assets/images/exception/image-404.png'
2022-04-05 19:01:52 +08:00
import html2canvas from 'html2canvas'
import { downloadByA } from './file'
2021-12-20 14:29:29 +08:00
/**
* *
* @return { Boolean }
*/
export const isDev = () => {
return import.meta.env.DEV
}
2021-12-20 14:29:29 +08:00
/**
2022-01-16 22:17:34 +08:00
* * ID
2021-12-20 14:29:29 +08:00
* @param { Number } randomLength
*/
export const getUUID = (randomLength = 10) => {
2021-12-20 14:29:29 +08:00
return Number(
Math.random().toString().substr(2, randomLength) + Date.now()
).toString(36)
}
/**
* * render
* @param icon
* @param set
2021-12-20 14:29:29 +08:00
*/
export const renderIcon = (icon: any, set = {}) => {
return () => h(NIcon, set, { default: () => h(icon) })
}
/**
* * render
* @param lang
* @param set
* @param tag
*/
export const renderLang = (lang: string, set = {}, tag = 'span') => {
return () => h(tag, set, { default: () => window['$t'](lang) })
}
2021-12-20 14:29:29 +08:00
/**
* ! 使
2021-12-20 18:06:08 +08:00
* * vite 使 require utils
* @param path
2021-12-20 14:29:29 +08:00
* @param name
* @returns url
*/
// export const requireUrl = (path: string, name: string) => {
// return new URL(`${path}/${name}`, import.meta.url).href
// }
2021-12-21 10:06:03 +08:00
2021-12-20 18:06:08 +08:00
/**
* * 404
* @param path
* @param name
* @returns url
*/
export const requireErrorImg = () => {
return Image_404
2021-12-20 18:06:08 +08:00
}
2021-12-20 14:29:29 +08:00
/**
2022-01-20 21:25:35 +08:00
* *
* @param isFullscreen
* @param isEnabled
* @returns
*/
2021-12-20 14:29:29 +08:00
export const screenfullFn = (isFullscreen?: boolean, isEnabled?: boolean) => {
// 是否是全屏
if (isFullscreen) return screenfull.isFullscreen
// 是否支持全屏
if (isEnabled) return screenfull.isEnabled
if (screenfull.isEnabled) {
screenfull.toggle()
return
}
// TODO lang
window['$message'].warning('您的浏览器不支持全屏功能!')
}
2021-12-21 10:06:03 +08:00
2022-01-20 21:25:35 +08:00
/**
* *
* @param HTMLElement
2022-01-20 21:25:35 +08:00
* @param key
* @param value
*/
export const setDomAttribute = <
K extends keyof CSSStyleDeclaration,
V extends CSSStyleDeclaration[K]
>(
2022-01-20 21:25:35 +08:00
HTMLElement: HTMLElement,
2022-01-23 19:22:54 +08:00
key: K,
value: V
2022-01-20 21:25:35 +08:00
) => {
if (HTMLElement) {
HTMLElement.style[key] = value
}
}
2022-02-06 21:35:38 +08:00
2022-02-03 22:54:31 +08:00
/**
* * mac
* @returns boolean
*/
export const isMac = () => {
return /macintosh|mac os x/i.test(navigator.userAgent)
}
2022-02-06 21:35:38 +08:00
/**
* * file转url
*/
export const fileToUrl = (file: File): string => {
const Url = URL || window.URL || window.webkitURL
const ImageUrl = Url.createObjectURL(file)
return ImageUrl
}
/**
2022-02-06 21:35:38 +08:00
* * file转base64
*/
export const fileTobase64 = (file: File, callback: Function) => {
let reader = new FileReader()
reader.readAsDataURL(file)
2022-02-06 21:35:38 +08:00
reader.onload = function (e: ProgressEvent<FileReader>) {
if (e.target) {
let base64 = e.target.result
2022-02-06 21:35:38 +08:00
callback(base64)
}
}
}
2022-02-06 21:35:38 +08:00
2021-12-21 10:06:03 +08:00
/**
* *
*/
2022-02-03 22:54:31 +08:00
export const addEventListener = <K extends keyof WindowEventMap>(
target: HTMLElement | Document,
2021-12-21 10:06:03 +08:00
type: K,
listener: any,
2021-12-21 10:06:03 +08:00
options?: boolean | AddEventListenerOptions | undefined
) => {
if (!target) return
2021-12-21 10:06:03 +08:00
target.addEventListener(
type,
2022-02-03 22:54:31 +08:00
throttle(listener, 300, {
2021-12-21 10:06:03 +08:00
leading: true,
2022-04-05 19:01:52 +08:00
trailing: false,
2021-12-21 10:06:03 +08:00
}),
options
)
}
2021-12-21 10:06:03 +08:00
/**
* *
*/
2022-02-03 22:54:31 +08:00
export const removeEventListener = <K extends keyof WindowEventMap>(
target: HTMLElement | Document,
2021-12-21 10:06:03 +08:00
type: K,
2022-02-03 22:54:31 +08:00
listener: any
2021-12-21 10:06:03 +08:00
) => {
if (!target) return
2021-12-21 10:06:03 +08:00
target.removeEventListener(type, listener)
}
2022-04-05 19:01:52 +08:00
/**
* *
* @param html DOM
*/
export const canvasCut = (html: HTMLElement | null, callback?: Function) => {
if (!html) {
window['$message'].error('导出失败!')
if (callback) callback()
return
}
html2canvas(html).then((canvas: HTMLCanvasElement) => {
window['$message'].success('导出成功!')
downloadByA(canvas.toDataURL(), undefined, 'png')
if (callback) callback()
})
}