1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4 |
|
5 | var _typeof = require("@babel/runtime/helpers/typeof");
|
6 |
|
7 | Object.defineProperty(exports, "__esModule", {
|
8 | value: true
|
9 | });
|
10 | exports.getKeyThenIncreaseKey = getKeyThenIncreaseKey;
|
11 | exports.attachTypeApi = attachTypeApi;
|
12 | exports["default"] = exports.getInstance = void 0;
|
13 |
|
14 | var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
15 |
|
16 | var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
17 |
|
18 | var React = _interopRequireWildcard(require("react"));
|
19 |
|
20 | var _classnames = _interopRequireDefault(require("classnames"));
|
21 |
|
22 | var _rcNotification = _interopRequireDefault(require("rc-notification"));
|
23 |
|
24 | var _LoadingOutlined = _interopRequireDefault(require("@ant-design/icons/LoadingOutlined"));
|
25 |
|
26 | var _ExclamationCircleFilled = _interopRequireDefault(require("@ant-design/icons/ExclamationCircleFilled"));
|
27 |
|
28 | var _CloseCircleFilled = _interopRequireDefault(require("@ant-design/icons/CloseCircleFilled"));
|
29 |
|
30 | var _CheckCircleFilled = _interopRequireDefault(require("@ant-design/icons/CheckCircleFilled"));
|
31 |
|
32 | var _InfoCircleFilled = _interopRequireDefault(require("@ant-design/icons/InfoCircleFilled"));
|
33 |
|
34 | var _useMessage = _interopRequireDefault(require("./hooks/useMessage"));
|
35 |
|
36 | var _configProvider = require("../config-provider");
|
37 |
|
38 | function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
39 |
|
40 | function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
41 |
|
42 | var messageInstance;
|
43 | var defaultDuration = 3;
|
44 | var defaultTop;
|
45 | var key = 1;
|
46 | var localPrefixCls = '';
|
47 | var transitionName = 'move-up';
|
48 | var hasTransitionName = false;
|
49 | var getContainer;
|
50 | var maxCount;
|
51 | var rtl = false;
|
52 |
|
53 | function getKeyThenIncreaseKey() {
|
54 | return key++;
|
55 | }
|
56 |
|
57 | function setMessageConfig(options) {
|
58 | if (options.top !== undefined) {
|
59 | defaultTop = options.top;
|
60 | messageInstance = null;
|
61 | }
|
62 |
|
63 | if (options.duration !== undefined) {
|
64 | defaultDuration = options.duration;
|
65 | }
|
66 |
|
67 | if (options.prefixCls !== undefined) {
|
68 | localPrefixCls = options.prefixCls;
|
69 | }
|
70 |
|
71 | if (options.getContainer !== undefined) {
|
72 | getContainer = options.getContainer;
|
73 | }
|
74 |
|
75 | if (options.transitionName !== undefined) {
|
76 | transitionName = options.transitionName;
|
77 | messageInstance = null;
|
78 |
|
79 | hasTransitionName = true;
|
80 | }
|
81 |
|
82 | if (options.maxCount !== undefined) {
|
83 | maxCount = options.maxCount;
|
84 | messageInstance = null;
|
85 | }
|
86 |
|
87 | if (options.rtl !== undefined) {
|
88 | rtl = options.rtl;
|
89 | }
|
90 | }
|
91 |
|
92 | function getRCNotificationInstance(args, callback) {
|
93 | var customizePrefixCls = args.prefixCls;
|
94 |
|
95 | var _globalConfig = (0, _configProvider.globalConfig)(),
|
96 | getPrefixCls = _globalConfig.getPrefixCls,
|
97 | getRootPrefixCls = _globalConfig.getRootPrefixCls;
|
98 |
|
99 | var prefixCls = getPrefixCls('message', customizePrefixCls || localPrefixCls);
|
100 | var rootPrefixCls = getRootPrefixCls(args.rootPrefixCls, prefixCls);
|
101 |
|
102 | if (messageInstance) {
|
103 | callback({
|
104 | prefixCls: prefixCls,
|
105 | rootPrefixCls: rootPrefixCls,
|
106 | instance: messageInstance
|
107 | });
|
108 | return;
|
109 | }
|
110 |
|
111 | var instanceConfig = {
|
112 | prefixCls: prefixCls,
|
113 | transitionName: hasTransitionName ? transitionName : "".concat(rootPrefixCls, "-").concat(transitionName),
|
114 | style: {
|
115 | top: defaultTop
|
116 | },
|
117 | getContainer: getContainer,
|
118 | maxCount: maxCount
|
119 | };
|
120 |
|
121 | _rcNotification["default"].newInstance(instanceConfig, function (instance) {
|
122 | if (messageInstance) {
|
123 | callback({
|
124 | prefixCls: prefixCls,
|
125 | rootPrefixCls: rootPrefixCls,
|
126 | instance: messageInstance
|
127 | });
|
128 | return;
|
129 | }
|
130 |
|
131 | messageInstance = instance;
|
132 |
|
133 | if (process.env.NODE_ENV === 'test') {
|
134 | messageInstance.config = instanceConfig;
|
135 | }
|
136 |
|
137 | callback({
|
138 | prefixCls: prefixCls,
|
139 | rootPrefixCls: rootPrefixCls,
|
140 | instance: instance
|
141 | });
|
142 | });
|
143 | }
|
144 |
|
145 | var typeToIcon = {
|
146 | info: _InfoCircleFilled["default"],
|
147 | success: _CheckCircleFilled["default"],
|
148 | error: _CloseCircleFilled["default"],
|
149 | warning: _ExclamationCircleFilled["default"],
|
150 | loading: _LoadingOutlined["default"]
|
151 | };
|
152 |
|
153 | function getRCNoticeProps(args, prefixCls) {
|
154 | var _classNames;
|
155 |
|
156 | var duration = args.duration !== undefined ? args.duration : defaultDuration;
|
157 | var IconComponent = typeToIcon[args.type];
|
158 | var messageClass = (0, _classnames["default"])("".concat(prefixCls, "-custom-content"), (_classNames = {}, (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-").concat(args.type), args.type), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-rtl"), rtl === true), _classNames));
|
159 | return {
|
160 | key: args.key,
|
161 | duration: duration,
|
162 | style: args.style || {},
|
163 | className: args.className,
|
164 | content: React.createElement("div", {
|
165 | className: messageClass
|
166 | }, args.icon || IconComponent && React.createElement(IconComponent, null), React.createElement("span", null, args.content)),
|
167 | onClose: args.onClose,
|
168 | onClick: args.onClick
|
169 | };
|
170 | }
|
171 |
|
172 | function notice(args) {
|
173 | var target = args.key || key++;
|
174 | var closePromise = new Promise(function (resolve) {
|
175 | var callback = function callback() {
|
176 | if (typeof args.onClose === 'function') {
|
177 | args.onClose();
|
178 | }
|
179 |
|
180 | return resolve(true);
|
181 | };
|
182 |
|
183 | getRCNotificationInstance(args, function (_ref) {
|
184 | var prefixCls = _ref.prefixCls,
|
185 | instance = _ref.instance;
|
186 | instance.notice(getRCNoticeProps((0, _extends2["default"])((0, _extends2["default"])({}, args), {
|
187 | key: target,
|
188 | onClose: callback
|
189 | }), prefixCls));
|
190 | });
|
191 | });
|
192 |
|
193 | var result = function result() {
|
194 | if (messageInstance) {
|
195 | messageInstance.removeNotice(target);
|
196 | }
|
197 | };
|
198 |
|
199 | result.then = function (filled, rejected) {
|
200 | return closePromise.then(filled, rejected);
|
201 | };
|
202 |
|
203 | result.promise = closePromise;
|
204 | return result;
|
205 | }
|
206 |
|
207 | function isArgsProps(content) {
|
208 | return Object.prototype.toString.call(content) === '[object Object]' && !!content.content;
|
209 | }
|
210 |
|
211 | var api = {
|
212 | open: notice,
|
213 | config: setMessageConfig,
|
214 | destroy: function destroy(messageKey) {
|
215 | if (messageInstance) {
|
216 | if (messageKey) {
|
217 | var _messageInstance = messageInstance,
|
218 | removeNotice = _messageInstance.removeNotice;
|
219 | removeNotice(messageKey);
|
220 | } else {
|
221 | var _messageInstance2 = messageInstance,
|
222 | destroy = _messageInstance2.destroy;
|
223 | destroy();
|
224 | messageInstance = null;
|
225 | }
|
226 | }
|
227 | }
|
228 | };
|
229 |
|
230 | function attachTypeApi(originalApi, type) {
|
231 | originalApi[type] = function (content, duration, onClose) {
|
232 | if (isArgsProps(content)) {
|
233 | return originalApi.open((0, _extends2["default"])((0, _extends2["default"])({}, content), {
|
234 | type: type
|
235 | }));
|
236 | }
|
237 |
|
238 | if (typeof duration === 'function') {
|
239 | onClose = duration;
|
240 | duration = undefined;
|
241 | }
|
242 |
|
243 | return originalApi.open({
|
244 | content: content,
|
245 | duration: duration,
|
246 | type: type,
|
247 | onClose: onClose
|
248 | });
|
249 | };
|
250 | }
|
251 |
|
252 | ['success', 'info', 'warning', 'error', 'loading'].forEach(function (type) {
|
253 | return attachTypeApi(api, type);
|
254 | });
|
255 | api.warn = api.warning;
|
256 | api.useMessage = (0, _useMessage["default"])(getRCNotificationInstance, getRCNoticeProps);
|
257 |
|
258 |
|
259 | var getInstance = function getInstance() {
|
260 | return process.env.NODE_ENV === 'test' ? messageInstance : null;
|
261 | };
|
262 |
|
263 | exports.getInstance = getInstance;
|
264 | var _default = api;
|
265 | exports["default"] = _default; |
\ | No newline at end of file |