UNPKG

tdesign-vue-next

Version:
1 lines 9.23 kB
{"version":3,"file":"plugin.mjs","sources":["../../../components/notification/plugin.ts"],"sourcesContent":["import { App, Plugin, nextTick, AppContext, createVNode, render, VNode } from 'vue';\nimport NotificationList from './notification-list';\nimport { getAttach } from '@tdesign/shared-utils';\nimport {\n NotificationOptions,\n NotificationInstance,\n NotificationMethod,\n NotificationInfoMethod,\n NotificationWarningMethod,\n NotificationErrorMethod,\n NotificationSuccessMethod,\n NotificationCloseMethod,\n NotificationCloseAllMethod,\n} from './type';\nimport { AttachNodeReturnValue } from '../common';\nimport './style';\n\nlet seed = 0;\n// 存储不同 attach 和 不同 placement 消息列表实例\nconst instanceMap: Map<AttachNodeReturnValue, Record<string, VNode>> = new Map();\n\nconst NotificationFunction = (options: NotificationOptions, context?: AppContext): Promise<NotificationInstance> => {\n seed += 1;\n const hackOptions = {\n placement: 'top-right',\n zIndex: 6000,\n attach: 'body',\n id: seed,\n ...options,\n };\n hackOptions.content = options.content ? options.content : '';\n\n const attachEl = getAttach(hackOptions.attach);\n\n if (!instanceMap.get(attachEl)) {\n instanceMap.set(attachEl, {});\n }\n let tmpInstance = instanceMap.get(attachEl)[hackOptions.placement];\n if (!tmpInstance) {\n const wrapper = document.createElement('div');\n\n const instance = createVNode(NotificationList, {\n placement: hackOptions.placement,\n });\n\n // eslint-disable-next-line no-underscore-dangle\n if (context ?? NotificationPlugin._context) {\n // eslint-disable-next-line no-underscore-dangle\n instance.appContext = context ?? NotificationPlugin._context;\n }\n\n // 会遗留一个容器在 attach 中,需要找合适的时机回收\n attachEl.appendChild(wrapper);\n render(instance, wrapper);\n instance.component.exposed.add(hackOptions);\n instanceMap.get(attachEl)[hackOptions.placement] = instance;\n tmpInstance = instance;\n } else {\n tmpInstance.component.exposed.add(hackOptions);\n }\n\n return new Promise((resolve) => {\n const ins = instanceMap.get(attachEl)[hackOptions.placement];\n nextTick(() => {\n const notificationList: NotificationInstance[] = ins.component.exposed.notificationList.value ?? [];\n resolve(\n notificationList?.find((notify) => {\n return (notify as any).$?.vnode?.key === hackOptions.id;\n }),\n );\n });\n });\n};\n\nconst showThemeNotification: NotificationMethod = (theme, options, context) => {\n const hackOptions = { ...options, theme };\n return NotificationFunction(hackOptions, context);\n};\n\ninterface ExtraApi {\n info: NotificationInfoMethod;\n success: NotificationSuccessMethod;\n warning: NotificationWarningMethod;\n error: NotificationErrorMethod;\n close: NotificationCloseMethod;\n closeAll: NotificationCloseAllMethod;\n}\n\nconst extraApi: ExtraApi = {\n info: (options, context) => showThemeNotification('info', options, context),\n success: (options, context) => showThemeNotification('success', options, context),\n warning: (options, context) => showThemeNotification('warning', options, context),\n error: (options, context) => showThemeNotification('error', options, context),\n close: (promise) => {\n promise.then((instance) => instance.close());\n },\n closeAll: () => {\n instanceMap.forEach((attach) => {\n Object.keys(attach).forEach((placement) => {\n attach[placement].component.exposed.removeAll();\n });\n });\n },\n};\n\nexport type NotificationPluginType = Plugin &\n ExtraApi &\n NotificationMethod & {\n _context?: AppContext;\n };\n\nconst NotificationPlugin: NotificationPluginType = showThemeNotification as NotificationPluginType;\n\nNotificationPlugin.install = (app: App) => {\n app.config.globalProperties.$notify = showThemeNotification;\n Object.keys(extraApi).forEach((funcName: keyof ExtraApi) => {\n app.config.globalProperties.$notify[funcName] = extraApi[funcName];\n });\n // eslint-disable-next-line no-underscore-dangle\n NotificationPlugin._context = app._context;\n};\n\nObject.keys(extraApi).forEach((funcName: keyof ExtraApi) => {\n // @ts-ignore\n // TODO https://github.com/microsoft/TypeScript/issues/32693\n NotificationPlugin[funcName] = extraApi[funcName];\n});\n\nexport const NotifyPlugin = NotificationPlugin;\n\nexport default NotificationPlugin;\n"],"names":["seed","instanceMap","Map","NotificationFunction","options","context","hackOptions","_objectSpread","placement","zIndex","attach","id","content","attachEl","getAttach","get","set","tmpInstance","wrapper","document","createElement","instance","createVNode","NotificationList","NotificationPlugin","_context","appContext","appendChild","render","component","exposed","add","Promise","resolve","ins","nextTick","_ins$component$expose","notificationList","value","find","notify","_notify$$","$","vnode","key","showThemeNotification","theme","extraApi","info","success","warning","error","close","promise","then","closeAll","forEach","Object","keys","removeAll","install","app","config","globalProperties","$notify","funcName","NotifyPlugin"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiBA,IAAIA,IAAO,GAAA,CAAA,CAAA;AAEX,IAAMC,WAAA,sBAAqEC,GAAI,EAAA,CAAA;AAE/E,IAAMC,oBAAA,GAAuB,SAAvBA,oBAAAA,CAAwBC,OAAA,EAA8BC,OAAwD,EAAA;AAC1GL,EAAAA,IAAA,IAAA,CAAA,CAAA;EACR,IAAMM,WAAc,GAAAC,aAAA,CAAA;AAClBC,IAAAA,SAAW,EAAA,WAAA;AACXC,IAAAA,MAAQ,EAAA,GAAA;AACRC,IAAAA,MAAQ,EAAA,MAAA;AACRC,IAAAA,EAAI,EAAAX,IAAAA;AAAA,GAAA,EACDI,OAAA,CACL,CAAA;EACAE,WAAA,CAAYM,OAAU,GAAAR,OAAA,CAAQQ,OAAU,GAAAR,OAAA,CAAQQ,OAAU,GAAA,EAAA,CAAA;AAEpD,EAAA,IAAAC,QAAA,GAAWC,SAAU,CAAAR,WAAA,CAAYI,MAAM,CAAA,CAAA;AAE7C,EAAA,IAAI,CAACT,WAAA,CAAYc,GAAI,CAAAF,QAAQ,CAAG,EAAA;AAClBZ,IAAAA,WAAA,CAAAe,GAAA,CAAIH,QAAU,EAAA,EAAE,CAAA,CAAA;AAC9B,GAAA;AACA,EAAA,IAAII,WAAc,GAAAhB,WAAA,CAAYc,GAAI,CAAAF,QAAQ,EAAEP,WAAY,CAAAE,SAAA,CAAA,CAAA;EACxD,IAAI,CAACS,WAAa,EAAA;AACV,IAAA,IAAAC,OAAA,GAAUC,QAAS,CAAAC,aAAA,CAAc,KAAK,CAAA,CAAA;AAEtC,IAAA,IAAAC,QAAA,GAAWC,YAAYC,gBAAkB,EAAA;MAC7Cf,WAAWF,WAAY,CAAAE,SAAAA;AACzB,K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