UNPKG

vexip-ui

Version:

A Vue 3 UI library, Highly customizability, full TypeScript, performance pretty good

1 lines 12.7 kB
{"version":3,"file":"index.mjs","sources":["../../../components/message/index.ts"],"sourcesContent":["import { createApp, createVNode, markRaw, render } from 'vue'\n\nimport Component from './message.vue'\nimport { proxyExposed, unrefElement } from '@vexip-ui/hooks'\nimport { destroyObject, isClient, isNull, noop, toNumber } from '@vexip-ui/utils'\n\nimport type { App, MaybeRef } from 'vue'\nimport type { MaybeInstance } from '@vexip-ui/hooks'\nimport type {\n Key,\n MessageConfig,\n MessageInstance,\n MessageOptions,\n MessagePlacement,\n MessageType,\n} from './symbol'\n\nexport type { MessageConfig, MessageType, MessagePlacement, MessageOptions }\n\ntype FuzzyOptions = string | MessageOptions\ntype ManagerOptions = { duration?: number } & MessageConfig & Record<string, unknown>\n\ninterface AipMethod {\n (options: MessageOptions): () => void,\n (content: string, duration?: number): () => void,\n /** @internal */\n (options: FuzzyOptions, duration?: number): () => void,\n}\n\nconst placementWhiteList: MessagePlacement[] = ['top', 'bottom']\n\nlet count = 1\n\nfunction getKey() {\n return `message-${count++}`\n}\n\nexport class MessageManager {\n name: string\n defaults: Record<string, unknown>\n\n open: AipMethod\n primary: AipMethod\n info: AipMethod\n success: AipMethod\n warning: AipMethod\n error: AipMethod\n\n private _mountedApp: App<unknown> | null\n private _instance: MessageInstance | null\n private _innerApp: App<unknown> | null\n private _container: HTMLElement | null\n private _wrapper: HTMLElement | SVGElement | null\n private _mountedEl: HTMLElement | null\n private _installed: boolean\n private _configRecord: MessageConfig | null\n\n constructor(options: ManagerOptions = {}) {\n options = {\n ...options,\n duration: options.duration ? toNumber(options.duration) : 3000,\n }\n\n this._mountedApp = null\n this._instance = null\n this._innerApp = null\n this._container = null\n this._wrapper = null\n this._mountedEl = null\n this._installed = false\n this._configRecord = null\n this.name = 'Message'\n this.defaults = {}\n\n this.config(options)\n\n this.open = (content: FuzzyOptions, duration?: number) => {\n return this._open(null, content, duration)\n }\n\n this.primary = (content: FuzzyOptions, duration?: number) => {\n return this._open('primary', content, duration)\n }\n\n this.info = (content: FuzzyOptions, duration?: number) => {\n return this._open('info', content, duration)\n }\n\n this.success = (content: FuzzyOptions, duration?: number) => {\n return this._open('success', content, duration)\n }\n\n this.warning = (content: FuzzyOptions, duration?: number) => {\n return this._open('warning', content, duration)\n }\n\n this.error = (content: FuzzyOptions, duration?: number) => {\n return this._open('error', content, duration)\n }\n }\n\n judge(state: boolean, success: string, error: string, duration?: number): void\n judge(state: boolean, success: MessageOptions, error: string, duration?: number): void\n judge(state: boolean, success: string, error: MessageOptions, duration?: number): void\n judge(state: boolean, success: MessageOptions, error: MessageOptions): void\n judge(\n state: boolean,\n success: string | MessageOptions,\n error: string | MessageOptions,\n duration?: number,\n ) {\n if (state) {\n this.success(success, duration)\n } else {\n this.error(error, duration)\n }\n }\n\n close(key: Key) {\n if (isNull(key)) {\n this.clear()\n } else {\n this._getInstance()?.remove(key)\n }\n }\n\n config({ placement, startOffset, itemGap, ...others }: MessageConfig & MessageOptions) {\n this._configRecord = { placement, startOffset, itemGap }\n this.defaults = { ...this.defaults, ...others }\n\n if (this._installed) {\n const instance = this._getInstance()\n\n if (instance) {\n if (placement) {\n instance.config({\n placement: placementWhiteList.includes(placement) ? placement : placementWhiteList[0],\n })\n }\n\n instance.config({ startOffset, itemGap })\n }\n }\n }\n\n clone() {\n const manager = new MessageManager(this.defaults)\n\n manager._mountedApp = this._mountedApp\n manager._configRecord = this._configRecord\n manager._installed = this._installed\n\n return manager\n }\n\n clear() {\n this._getInstance()?.clear()\n }\n\n destroy() {\n this._mountedEl && this._wrapper?.removeChild(this._mountedEl)\n this._innerApp?.unmount()\n this._container && render(null, this._container)\n destroyObject(this)\n }\n\n isDestroyed() {\n return false\n }\n\n install(app: App, options: ManagerOptions & { property?: string } = {}) {\n const { property, ...others } = options\n\n this._mountedApp = app\n this._installed = true\n this.config(others)\n\n if (property || !app.config.globalProperties.$message) {\n app.config.globalProperties[property || '$message'] = this\n }\n }\n\n transferTo(target: MaybeRef<string | MaybeInstance>) {\n if (!isClient) return\n\n const el = unrefElement(target)\n\n if (el) {\n this._wrapper = el\n\n if (this._instance) {\n this._mountedEl && this._wrapper.appendChild(this._mountedEl)\n } else {\n this._getInstance()\n }\n }\n }\n\n private _getInstance() {\n if (!this._instance && isClient) {\n if (!this._mountedApp) {\n console.warn('[vexip-ui:Message]: App missing, the plugin maybe not installed.')\n\n this._container = document.createElement('div')\n this._innerApp = createApp(Component)\n this._instance = this._innerApp.mount(this._container) as MessageInstance\n } else {\n const vnode = createVNode(Component, null, null)\n\n this._container = document.createElement('div')\n vnode.appContext = this._mountedApp._context\n\n render(vnode, this._container)\n\n this._instance = proxyExposed<MessageInstance>(vnode)\n }\n\n this._mountedEl = this._container.firstElementChild as HTMLElement\n ;(this._wrapper || document.body).appendChild(this._mountedEl)\n }\n\n return this._instance\n }\n\n private _open(type: null | MessageType, content: FuzzyOptions, _duration?: number) {\n if (!isClient) {\n return noop\n }\n\n const options = typeof content === 'string' ? { content, duration: _duration } : content\n\n const key = options.key ?? getKey()\n const message = this._getInstance()!\n\n let timer: ReturnType<typeof setTimeout>\n\n const userCloseFn = options.onClose\n const onClose = () => {\n clearTimeout(timer)\n\n if (typeof userCloseFn === 'function') {\n return userCloseFn()\n }\n }\n\n const userEnterFn = options.onEnter\n const onEnter = () => {\n if (options.liveOnEnter) {\n clearTimeout(timer)\n }\n\n if (typeof userEnterFn === 'function') {\n return userEnterFn()\n }\n }\n\n const userLeaveFn = options.onLeave\n const onLeave = () => {\n if (options.liveOnEnter) {\n clearTimeout(timer)\n setDelayClose()\n }\n\n if (typeof userLeaveFn === 'function') {\n return userLeaveFn()\n }\n }\n\n const item: MessageOptions = {\n ...this.defaults,\n ...options,\n key,\n type: type ?? options.type,\n onClose,\n onEnter,\n onLeave,\n }\n\n if (item.icon && typeof item.icon !== 'function') {\n item.icon = markRaw(item.icon)\n }\n\n message.add(item)\n setDelayClose()\n\n function setDelayClose() {\n const duration = typeof item.duration === 'number' ? item.duration : 3000\n\n if (duration >= 500) {\n timer = setTimeout(() => {\n message.remove(key)\n }, duration)\n }\n }\n\n return () => {\n clearTimeout(timer)\n message.remove(key)\n }\n }\n}\n\nexport const Message = new MessageManager()\n"],"names":["placementWhiteList","count","getKey","MessageManager","options","__publicField","toNumber","content","duration","state","success","error","key","isNull","_a","placement","startOffset","itemGap","others","instance","manager","_b","render","destroyObject","app","property","target","isClient","el","unrefElement","createApp","Component","vnode","createVNode","proxyExposed","type","_duration","noop","message","timer","userCloseFn","onClose","userEnterFn","onEnter","userLeaveFn","onLeave","setDelayClose","item","markRaw","Message"],"mappings":";;;;;;;;AA6BA,MAAMA,IAAyC,CAAC,OAAO,QAAQ;AAE/D,IAAIC,IAAQ;AAEZ,SAASC,IAAS;AAChB,SAAO,WAAWD,GAAO;AAC3B;AAEO,MAAME,EAAe;AAAA,EAoB1B,YAAYC,IAA0B,IAAI;AAnB1C,IAAAC,EAAA;AACA,IAAAA,EAAA;AAEA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AAEQ,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AAGI,IAAAD,IAAA;AAAA,MACR,GAAGA;AAAA,MACH,UAAUA,EAAQ,WAAWE,EAASF,EAAQ,QAAQ,IAAI;AAAA,IAC5D,GAEA,KAAK,cAAc,MACnB,KAAK,YAAY,MACjB,KAAK,YAAY,MACjB,KAAK,aAAa,MAClB,KAAK,WAAW,MAChB,KAAK,aAAa,MAClB,KAAK,aAAa,IAClB,KAAK,gBAAgB,MACrB,KAAK,OAAO,WACZ,KAAK,WAAW,CAAC,GAEjB,KAAK,OAAOA,CAAO,GAEd,KAAA,OAAO,CAACG,GAAuBC,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