{"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      offset: hackOptions.offset,\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","offset","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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