2020-12-25 06:44:47 +00:00
|
|
|
import { CSSProperties, VNodeTypes } from 'vue';
|
2019-01-12 03:33:27 +00:00
|
|
|
import Notification from '../vc-notification';
|
2020-03-24 08:58:53 +00:00
|
|
|
import LoadingOutlined from '@ant-design/icons-vue/LoadingOutlined';
|
|
|
|
import ExclamationCircleFilled from '@ant-design/icons-vue/ExclamationCircleFilled';
|
|
|
|
import CloseCircleFilled from '@ant-design/icons-vue/CloseCircleFilled';
|
|
|
|
import CheckCircleFilled from '@ant-design/icons-vue/CheckCircleFilled';
|
|
|
|
import InfoCircleFilled from '@ant-design/icons-vue/InfoCircleFilled';
|
2018-02-06 08:43:59 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
let defaultDuration = 3;
|
2020-11-19 14:10:51 +00:00
|
|
|
let defaultTop: string;
|
2020-10-14 03:40:45 +00:00
|
|
|
let messageInstance: any;
|
2019-01-12 03:33:27 +00:00
|
|
|
let key = 1;
|
|
|
|
let prefixCls = 'ant-message';
|
|
|
|
let transitionName = 'move-up';
|
|
|
|
let getContainer = () => document.body;
|
2020-10-14 03:40:45 +00:00
|
|
|
let maxCount: number;
|
2018-02-06 08:43:59 +00:00
|
|
|
|
2020-10-14 03:40:45 +00:00
|
|
|
function getMessageInstance(callback: (i: any) => void) {
|
2018-02-06 08:43:59 +00:00
|
|
|
if (messageInstance) {
|
2019-01-12 03:33:27 +00:00
|
|
|
callback(messageInstance);
|
|
|
|
return;
|
2018-02-06 08:43:59 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
Notification.newInstance(
|
|
|
|
{
|
|
|
|
prefixCls,
|
|
|
|
transitionName,
|
|
|
|
style: { top: defaultTop }, // 覆盖原来的样式
|
|
|
|
getContainer,
|
|
|
|
maxCount,
|
|
|
|
},
|
2020-10-14 03:40:45 +00:00
|
|
|
(instance: any) => {
|
2019-01-12 03:33:27 +00:00
|
|
|
if (messageInstance) {
|
|
|
|
callback(messageInstance);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
messageInstance = instance;
|
|
|
|
callback(instance);
|
|
|
|
},
|
|
|
|
);
|
2018-02-06 08:43:59 +00:00
|
|
|
}
|
|
|
|
|
2020-10-14 03:40:45 +00:00
|
|
|
type NoticeType = 'info' | 'success' | 'error' | 'warning' | 'loading';
|
|
|
|
|
|
|
|
export interface ThenableArgument {
|
|
|
|
(val: any): void;
|
|
|
|
}
|
|
|
|
|
2020-03-24 08:58:53 +00:00
|
|
|
const iconMap = {
|
|
|
|
info: InfoCircleFilled,
|
|
|
|
success: CheckCircleFilled,
|
|
|
|
error: CloseCircleFilled,
|
|
|
|
warning: ExclamationCircleFilled,
|
|
|
|
loading: LoadingOutlined,
|
|
|
|
};
|
2018-02-06 08:43:59 +00:00
|
|
|
|
2020-10-14 03:40:45 +00:00
|
|
|
export interface MessageType {
|
|
|
|
(): void;
|
|
|
|
then: (fill: ThenableArgument, reject: ThenableArgument) => Promise<void>;
|
|
|
|
promise: Promise<void>;
|
|
|
|
}
|
|
|
|
|
2021-06-23 13:47:53 +00:00
|
|
|
export interface MessageArgsProps {
|
2020-10-14 03:40:45 +00:00
|
|
|
content: VNodeTypes;
|
|
|
|
duration: number | null;
|
|
|
|
type: NoticeType;
|
|
|
|
onClose?: () => void;
|
|
|
|
icon?: VNodeTypes;
|
|
|
|
key?: string | number;
|
2020-12-25 06:44:47 +00:00
|
|
|
style?: CSSProperties;
|
|
|
|
class?: string;
|
2020-10-14 03:40:45 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 13:47:53 +00:00
|
|
|
function notice(args: MessageArgsProps): MessageType {
|
2019-01-12 03:33:27 +00:00
|
|
|
const duration = args.duration !== undefined ? args.duration : defaultDuration;
|
2020-03-24 08:58:53 +00:00
|
|
|
const Icon = iconMap[args.type];
|
2020-08-03 03:04:02 +00:00
|
|
|
const iconNode = Icon ? <Icon /> : '';
|
2018-02-06 08:43:59 +00:00
|
|
|
|
2020-03-07 11:45:13 +00:00
|
|
|
const target = args.key || key++;
|
2019-01-12 03:33:27 +00:00
|
|
|
const closePromise = new Promise(resolve => {
|
2018-09-05 13:28:54 +00:00
|
|
|
const callback = () => {
|
2018-12-09 10:37:41 +00:00
|
|
|
if (typeof args.onClose === 'function') {
|
2019-01-12 03:33:27 +00:00
|
|
|
args.onClose();
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
return resolve(true);
|
|
|
|
};
|
|
|
|
getMessageInstance(instance => {
|
2018-09-05 13:28:54 +00:00
|
|
|
instance.notice({
|
|
|
|
key: target,
|
|
|
|
duration,
|
2020-12-25 06:44:47 +00:00
|
|
|
style: args.style || {},
|
|
|
|
class: args.class,
|
2020-06-14 13:41:29 +00:00
|
|
|
content: () => {
|
2020-03-07 11:45:13 +00:00
|
|
|
return (
|
|
|
|
<div
|
|
|
|
class={`${prefixCls}-custom-content${args.type ? ` ${prefixCls}-${args.type}` : ''}`}
|
|
|
|
>
|
2020-10-14 03:40:45 +00:00
|
|
|
{args.icon || iconNode}
|
|
|
|
<span>{args.content}</span>
|
2020-03-07 11:45:13 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
2018-09-05 13:28:54 +00:00
|
|
|
onClose: callback,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-10-14 03:40:45 +00:00
|
|
|
const result: any = () => {
|
2018-02-06 08:43:59 +00:00
|
|
|
if (messageInstance) {
|
2019-01-12 03:33:27 +00:00
|
|
|
messageInstance.removeNotice(target);
|
2018-02-06 08:43:59 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2020-10-14 03:40:45 +00:00
|
|
|
result.then = (filled: ThenableArgument, rejected: ThenableArgument) =>
|
|
|
|
closePromise.then(filled, rejected);
|
2019-01-12 03:33:27 +00:00
|
|
|
result.promise = closePromise;
|
|
|
|
return result;
|
2018-02-06 08:43:59 +00:00
|
|
|
}
|
|
|
|
|
2020-10-14 03:40:45 +00:00
|
|
|
type ConfigDuration = number | (() => void);
|
2021-06-23 13:47:53 +00:00
|
|
|
type JointContent = VNodeTypes | MessageArgsProps;
|
2020-10-14 03:40:45 +00:00
|
|
|
export type ConfigOnClose = () => void;
|
2018-02-06 08:43:59 +00:00
|
|
|
|
2021-06-23 13:47:53 +00:00
|
|
|
function isArgsProps(content: JointContent): content is MessageArgsProps {
|
2020-10-14 03:40:45 +00:00
|
|
|
return (
|
|
|
|
Object.prototype.toString.call(content) === '[object Object]' &&
|
2021-06-23 13:47:53 +00:00
|
|
|
!!(content as MessageArgsProps).content
|
2020-10-14 03:40:45 +00:00
|
|
|
);
|
2020-03-07 11:45:13 +00:00
|
|
|
}
|
|
|
|
|
2020-10-14 03:40:45 +00:00
|
|
|
export interface ConfigOptions {
|
2020-11-19 14:10:51 +00:00
|
|
|
top?: string;
|
2020-10-14 03:40:45 +00:00
|
|
|
duration?: number;
|
|
|
|
prefixCls?: string;
|
|
|
|
getContainer?: () => HTMLElement;
|
|
|
|
transitionName?: string;
|
|
|
|
maxCount?: number;
|
|
|
|
}
|
2018-02-06 08:43:59 +00:00
|
|
|
|
2020-10-14 03:40:45 +00:00
|
|
|
const api: any = {
|
2018-12-09 10:37:41 +00:00
|
|
|
open: notice,
|
2020-10-14 03:40:45 +00:00
|
|
|
config(options: ConfigOptions) {
|
2018-02-06 08:43:59 +00:00
|
|
|
if (options.top !== undefined) {
|
2019-01-12 03:33:27 +00:00
|
|
|
defaultTop = options.top;
|
|
|
|
messageInstance = null; // delete messageInstance for new defaultTop
|
2018-02-06 08:43:59 +00:00
|
|
|
}
|
|
|
|
if (options.duration !== undefined) {
|
2019-01-12 03:33:27 +00:00
|
|
|
defaultDuration = options.duration;
|
2018-02-06 08:43:59 +00:00
|
|
|
}
|
|
|
|
if (options.prefixCls !== undefined) {
|
2019-01-12 03:33:27 +00:00
|
|
|
prefixCls = options.prefixCls;
|
2018-02-06 08:43:59 +00:00
|
|
|
}
|
|
|
|
if (options.getContainer !== undefined) {
|
2019-01-12 03:33:27 +00:00
|
|
|
getContainer = options.getContainer;
|
2018-02-06 08:43:59 +00:00
|
|
|
}
|
2018-04-07 06:29:59 +00:00
|
|
|
if (options.transitionName !== undefined) {
|
2019-01-12 03:33:27 +00:00
|
|
|
transitionName = options.transitionName;
|
|
|
|
messageInstance = null; // delete messageInstance for new transitionName
|
2018-04-07 06:29:59 +00:00
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
if (options.maxCount !== undefined) {
|
2019-01-12 03:33:27 +00:00
|
|
|
maxCount = options.maxCount;
|
|
|
|
messageInstance = null;
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
2018-02-06 08:43:59 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
destroy() {
|
2018-02-06 08:43:59 +00:00
|
|
|
if (messageInstance) {
|
2019-01-12 03:33:27 +00:00
|
|
|
messageInstance.destroy();
|
|
|
|
messageInstance = null;
|
2018-02-06 08:43:59 +00:00
|
|
|
}
|
|
|
|
},
|
2018-12-09 10:37:41 +00:00
|
|
|
};
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
['success', 'info', 'warning', 'error', 'loading'].forEach(type => {
|
2020-10-14 03:40:45 +00:00
|
|
|
api[type] = (content: JointContent, duration: ConfigDuration, onClose?: ConfigOnClose) => {
|
2020-03-07 11:45:13 +00:00
|
|
|
if (isArgsProps(content)) {
|
|
|
|
return api.open({ ...content, type });
|
|
|
|
}
|
2018-12-09 10:37:41 +00:00
|
|
|
if (typeof duration === 'function') {
|
2019-01-12 03:33:27 +00:00
|
|
|
onClose = duration;
|
|
|
|
duration = undefined;
|
2018-12-09 10:37:41 +00:00
|
|
|
}
|
2020-03-07 11:45:13 +00:00
|
|
|
return api.open({ content, duration, type, onClose });
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
|
|
|
});
|
2018-12-09 10:37:41 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
api.warn = api.warning;
|
2018-12-09 10:37:41 +00:00
|
|
|
|
2020-10-14 03:40:45 +00:00
|
|
|
export interface MessageApi {
|
|
|
|
info(content: JointContent, duration?: ConfigDuration, onClose?: ConfigOnClose): MessageType;
|
|
|
|
success(content: JointContent, duration?: ConfigDuration, onClose?: ConfigOnClose): MessageType;
|
|
|
|
error(content: JointContent, duration?: ConfigDuration, onClose?: ConfigOnClose): MessageType;
|
|
|
|
warn(content: JointContent, duration?: ConfigDuration, onClose?: ConfigOnClose): MessageType;
|
|
|
|
warning(content: JointContent, duration?: ConfigDuration, onClose?: ConfigOnClose): MessageType;
|
|
|
|
loading(content: JointContent, duration?: ConfigDuration, onClose?: ConfigOnClose): MessageType;
|
2021-06-23 13:47:53 +00:00
|
|
|
open(args: MessageArgsProps): MessageType;
|
2020-10-14 03:40:45 +00:00
|
|
|
config(options: ConfigOptions): void;
|
|
|
|
destroy(): void;
|
|
|
|
}
|
|
|
|
|
2020-11-14 06:10:10 +00:00
|
|
|
export default api as MessageApi;
|