linkmore-design
Version:
π πlmη»δ»ΆεΊγπ
221 lines (219 loc) β’ 7.49 kB
JavaScript
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import CheckCircleFilled from '@ant-design/icons/CheckCircleFilled';
import CloseCircleFilled from '@ant-design/icons/CloseCircleFilled';
import ExclamationCircleFilled from '@ant-design/icons/ExclamationCircleFilled';
import InfoCircleFilled from '@ant-design/icons/InfoCircleFilled';
import LoadingOutlined from '@ant-design/icons/LoadingOutlined';
import classNames from 'classnames';
import RCNotification from 'rc-notification';
import * as React from 'react';
import ConfigProvider, { globalConfig } from "../config-provider";
import createUseMessage from "./hooks/useMessage";
var messageInstance;
var defaultDuration = 3;
var defaultTop;
var key = 1;
var localPrefixCls = '';
var transitionName = 'move-up';
var hasTransitionName = false;
var getContainer;
var maxCount;
var rtl = false;
export function getKeyThenIncreaseKey() {
return key++;
}
function setMessageConfig(options) {
if (options.top !== undefined) {
defaultTop = options.top;
messageInstance = null; // delete messageInstance for new defaultTop
}
if (options.duration !== undefined) {
defaultDuration = options.duration;
}
if (options.prefixCls !== undefined) {
localPrefixCls = options.prefixCls;
}
if (options.getContainer !== undefined) {
getContainer = options.getContainer;
messageInstance = null; // delete messageInstance for new getContainer
}
if (options.transitionName !== undefined) {
transitionName = options.transitionName;
messageInstance = null; // delete messageInstance for new transitionName
hasTransitionName = true;
}
if (options.maxCount !== undefined) {
maxCount = options.maxCount;
messageInstance = null;
}
if (options.rtl !== undefined) {
rtl = options.rtl;
}
}
function getRCNotificationInstance(args, callback) {
var customizePrefixCls = args.prefixCls,
getContextPopupContainer = args.getPopupContainer;
var _globalConfig = globalConfig(),
getPrefixCls = _globalConfig.getPrefixCls,
getRootPrefixCls = _globalConfig.getRootPrefixCls,
getIconPrefixCls = _globalConfig.getIconPrefixCls;
var prefixCls = getPrefixCls('message', customizePrefixCls || localPrefixCls);
var rootPrefixCls = getRootPrefixCls(args.rootPrefixCls, prefixCls);
var iconPrefixCls = getIconPrefixCls();
if (messageInstance) {
callback({
prefixCls: prefixCls,
rootPrefixCls: rootPrefixCls,
iconPrefixCls: iconPrefixCls,
instance: messageInstance
});
return;
}
var instanceConfig = {
prefixCls: prefixCls,
transitionName: hasTransitionName ? transitionName : "".concat(rootPrefixCls, "-").concat(transitionName),
style: {
top: defaultTop
},
// θ¦ηεζ₯ηζ ·εΌ
getContainer: getContainer || getContextPopupContainer,
maxCount: maxCount
};
RCNotification.newInstance(instanceConfig, function (instance) {
if (messageInstance) {
callback({
prefixCls: prefixCls,
rootPrefixCls: rootPrefixCls,
iconPrefixCls: iconPrefixCls,
instance: messageInstance
});
return;
}
messageInstance = instance;
if (process.env.NODE_ENV === 'test') {
messageInstance.config = instanceConfig;
}
callback({
prefixCls: prefixCls,
rootPrefixCls: rootPrefixCls,
iconPrefixCls: iconPrefixCls,
instance: instance
});
});
}
var typeToIcon = {
info: InfoCircleFilled,
success: CheckCircleFilled,
error: CloseCircleFilled,
warning: ExclamationCircleFilled,
loading: LoadingOutlined
};
export var typeList = Object.keys(typeToIcon);
function getRCNoticeProps(args, prefixCls, iconPrefixCls) {
var _classNames;
var duration = args.duration !== undefined ? args.duration : defaultDuration;
var progress = args.progress === false ? false : true;
var IconComponent = typeToIcon[args.type];
var messageClass = classNames("".concat(prefixCls, "-custom-content"), (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-").concat(args.type), args.type), _defineProperty(_classNames, "".concat(prefixCls, "-rtl"), rtl === true), _classNames));
return {
key: args.key,
duration: duration,
style: args.style || {},
className: classNames(args.className, 'lm_message'),
content: /*#__PURE__*/React.createElement(ConfigProvider, {
iconPrefixCls: iconPrefixCls
}, /*#__PURE__*/React.createElement("div", {
className: messageClass
}, args.icon || IconComponent && /*#__PURE__*/React.createElement(IconComponent, null), /*#__PURE__*/React.createElement("span", null, args.content), progress && duration > 0 && /*#__PURE__*/React.createElement("div", {
className: "message_progress",
style: {
animationDuration: "".concat(duration, "s")
}
}))),
onClose: args.onClose,
onClick: args.onClick
};
}
function notice(args) {
var target = args.key || getKeyThenIncreaseKey();
var closePromise = new Promise(function (resolve) {
var callback = function callback() {
if (typeof args.onClose === 'function') {
args.onClose();
}
return resolve(true);
};
getRCNotificationInstance(args, function (_ref) {
var prefixCls = _ref.prefixCls,
iconPrefixCls = _ref.iconPrefixCls,
instance = _ref.instance;
instance.notice(getRCNoticeProps(_objectSpread(_objectSpread({}, args), {}, {
key: target,
onClose: callback
}), prefixCls, iconPrefixCls));
});
});
var result = function result() {
if (messageInstance) {
var _args$onClose;
messageInstance.removeNotice(target);
(_args$onClose = args.onClose) === null || _args$onClose === void 0 ? void 0 : _args$onClose.call(args);
}
};
result.then = function (filled, rejected) {
return closePromise.then(filled, rejected);
};
result.promise = closePromise;
return result;
}
function isArgsProps(content) {
return Object.prototype.toString.call(content) === '[object Object]' && !!content.content;
}
var api = {
open: notice,
config: setMessageConfig,
destroy: function destroy(messageKey) {
if (messageInstance) {
if (messageKey) {
var _messageInstance = messageInstance,
removeNotice = _messageInstance.removeNotice;
removeNotice(messageKey);
} else {
var _messageInstance2 = messageInstance,
_destroy = _messageInstance2.destroy;
_destroy();
messageInstance = null;
}
}
}
};
export function attachTypeApi(originalApi, type) {
originalApi[type] = function (content, duration, onClose) {
if (isArgsProps(content)) {
return originalApi.open(_objectSpread(_objectSpread({}, content), {}, {
type: type
}));
}
if (typeof duration === 'function') {
onClose = duration;
duration = undefined;
}
return originalApi.open({
content: content,
duration: duration,
type: type,
onClose: onClose
});
};
}
typeList.forEach(function (type) {
return attachTypeApi(api, type);
});
api.warn = api.warning;
api.useMessage = createUseMessage(getRCNotificationInstance, getRCNoticeProps);
/** @internal test Only function. Not work on production */
export var getInstance = function getInstance() {
return process.env.NODE_ENV === 'test' ? messageInstance : null;
};
export default api;