index.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import _extends from "@babel/runtime/helpers/esm/extends";
  2. import { createVNode as _createVNode } from "vue";
  3. import Notification from '../vc-notification';
  4. import LoadingOutlined from "@ant-design/icons-vue/es/icons/LoadingOutlined";
  5. import ExclamationCircleFilled from "@ant-design/icons-vue/es/icons/ExclamationCircleFilled";
  6. import CloseCircleFilled from "@ant-design/icons-vue/es/icons/CloseCircleFilled";
  7. import CheckCircleFilled from "@ant-design/icons-vue/es/icons/CheckCircleFilled";
  8. import InfoCircleFilled from "@ant-design/icons-vue/es/icons/InfoCircleFilled";
  9. import classNames from '../_util/classNames';
  10. import useStyle from './style';
  11. import useMessage from './useMessage';
  12. let defaultDuration = 3;
  13. let defaultTop;
  14. let messageInstance;
  15. let key = 1;
  16. let localPrefixCls = '';
  17. let transitionName = 'move-up';
  18. let hasTransitionName = false;
  19. let getContainer = () => document.body;
  20. let maxCount;
  21. let rtl = false;
  22. export function getKeyThenIncreaseKey() {
  23. return key++;
  24. }
  25. function setMessageConfig(options) {
  26. if (options.top !== undefined) {
  27. defaultTop = options.top;
  28. messageInstance = null; // delete messageInstance for new defaultTop
  29. }
  30. if (options.duration !== undefined) {
  31. defaultDuration = options.duration;
  32. }
  33. if (options.prefixCls !== undefined) {
  34. localPrefixCls = options.prefixCls;
  35. }
  36. if (options.getContainer !== undefined) {
  37. getContainer = options.getContainer;
  38. messageInstance = null; // delete messageInstance for new getContainer
  39. }
  40. if (options.transitionName !== undefined) {
  41. transitionName = options.transitionName;
  42. messageInstance = null; // delete messageInstance for new transitionName
  43. hasTransitionName = true;
  44. }
  45. if (options.maxCount !== undefined) {
  46. maxCount = options.maxCount;
  47. messageInstance = null;
  48. }
  49. if (options.rtl !== undefined) {
  50. rtl = options.rtl;
  51. }
  52. }
  53. function getMessageInstance(args, callback) {
  54. if (messageInstance) {
  55. callback(messageInstance);
  56. return;
  57. }
  58. Notification.newInstance({
  59. appContext: args.appContext,
  60. prefixCls: args.prefixCls || localPrefixCls,
  61. rootPrefixCls: args.rootPrefixCls,
  62. transitionName,
  63. hasTransitionName,
  64. style: {
  65. top: defaultTop
  66. },
  67. getContainer: getContainer || args.getPopupContainer,
  68. maxCount,
  69. name: 'message',
  70. useStyle
  71. }, instance => {
  72. if (messageInstance) {
  73. callback(messageInstance);
  74. return;
  75. }
  76. messageInstance = instance;
  77. callback(instance);
  78. });
  79. }
  80. const typeToIcon = {
  81. info: InfoCircleFilled,
  82. success: CheckCircleFilled,
  83. error: CloseCircleFilled,
  84. warning: ExclamationCircleFilled,
  85. loading: LoadingOutlined
  86. };
  87. export const typeList = Object.keys(typeToIcon);
  88. function notice(args) {
  89. const duration = args.duration !== undefined ? args.duration : defaultDuration;
  90. const target = args.key || getKeyThenIncreaseKey();
  91. const closePromise = new Promise(resolve => {
  92. const callback = () => {
  93. if (typeof args.onClose === 'function') {
  94. args.onClose();
  95. }
  96. return resolve(true);
  97. };
  98. getMessageInstance(args, instance => {
  99. instance.notice({
  100. key: target,
  101. duration,
  102. style: args.style || {},
  103. class: args.class,
  104. content: _ref => {
  105. let {
  106. prefixCls
  107. } = _ref;
  108. const Icon = typeToIcon[args.type];
  109. const iconNode = Icon ? _createVNode(Icon, null, null) : '';
  110. const messageClass = classNames(`${prefixCls}-custom-content`, {
  111. [`${prefixCls}-${args.type}`]: args.type,
  112. [`${prefixCls}-rtl`]: rtl === true
  113. });
  114. return _createVNode("div", {
  115. "class": messageClass
  116. }, [typeof args.icon === 'function' ? args.icon() : args.icon || iconNode, _createVNode("span", null, [typeof args.content === 'function' ? args.content() : args.content])]);
  117. },
  118. onClose: callback,
  119. onClick: args.onClick
  120. });
  121. });
  122. });
  123. const result = () => {
  124. if (messageInstance) {
  125. messageInstance.removeNotice(target);
  126. }
  127. };
  128. result.then = (filled, rejected) => closePromise.then(filled, rejected);
  129. result.promise = closePromise;
  130. return result;
  131. }
  132. function isArgsProps(content) {
  133. return Object.prototype.toString.call(content) === '[object Object]' && !!content.content;
  134. }
  135. const api = {
  136. open: notice,
  137. config: setMessageConfig,
  138. destroy(messageKey) {
  139. if (messageInstance) {
  140. if (messageKey) {
  141. const {
  142. removeNotice
  143. } = messageInstance;
  144. removeNotice(messageKey);
  145. } else {
  146. const {
  147. destroy
  148. } = messageInstance;
  149. destroy();
  150. messageInstance = null;
  151. }
  152. }
  153. }
  154. };
  155. export function attachTypeApi(originalApi, type) {
  156. originalApi[type] = (content, duration, onClose) => {
  157. if (isArgsProps(content)) {
  158. return originalApi.open(_extends(_extends({}, content), {
  159. type
  160. }));
  161. }
  162. if (typeof duration === 'function') {
  163. onClose = duration;
  164. duration = undefined;
  165. }
  166. return originalApi.open({
  167. content,
  168. duration,
  169. type,
  170. onClose
  171. });
  172. };
  173. }
  174. typeList.forEach(type => attachTypeApi(api, type));
  175. api.warn = api.warning;
  176. api.useMessage = useMessage;
  177. /** @private test Only function. Not work on production */
  178. export const getInstance = () => process.env.NODE_ENV === 'test' ? messageInstance : null;
  179. export default api;