2022-05-11 14:26:18 +08:00
|
|
|
import { render } from 'rc-util/lib/React/render';
|
2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
2023-03-10 14:12:49 +08:00
|
|
|
import ConfigProvider, { globalConfig, warnContext } from '../config-provider';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type {
|
2022-05-11 14:26:18 +08:00
|
|
|
ArgsProps,
|
|
|
|
ConfigOptions,
|
2022-06-13 17:55:00 +08:00
|
|
|
MessageInstance,
|
|
|
|
MessageType,
|
2022-05-11 14:26:18 +08:00
|
|
|
NoticeType,
|
|
|
|
TypeOpen,
|
|
|
|
} from './interface';
|
2022-06-07 23:29:12 +08:00
|
|
|
import PurePanel from './PurePanel';
|
2022-06-13 17:55:00 +08:00
|
|
|
import useMessage, { useInternalMessage } from './useMessage';
|
|
|
|
import { wrapPromiseFn } from './util';
|
2015-07-11 16:40:59 +08:00
|
|
|
|
2022-05-11 14:26:18 +08:00
|
|
|
export { ArgsProps };
|
|
|
|
|
|
|
|
let message: GlobalMessage | null = null;
|
|
|
|
|
2023-02-01 10:14:21 +08:00
|
|
|
let act: (callback: VoidFunction) => Promise<void> | void = (callback) => callback();
|
2022-05-11 14:26:18 +08:00
|
|
|
|
|
|
|
interface GlobalMessage {
|
|
|
|
fragment: DocumentFragment;
|
|
|
|
instance?: MessageInstance | null;
|
|
|
|
sync?: VoidFunction;
|
2020-07-15 19:51:56 +08:00
|
|
|
}
|
|
|
|
|
2022-05-11 14:26:18 +08:00
|
|
|
interface OpenTask {
|
|
|
|
type: 'open';
|
|
|
|
config: ArgsProps;
|
|
|
|
resolve: VoidFunction;
|
|
|
|
setCloseFn: (closeFn: VoidFunction) => void;
|
|
|
|
skipped?: boolean;
|
2020-07-15 19:51:56 +08:00
|
|
|
}
|
|
|
|
|
2022-05-11 14:26:18 +08:00
|
|
|
interface TypeTask {
|
|
|
|
type: NoticeType;
|
|
|
|
args: Parameters<TypeOpen>;
|
|
|
|
resolve: VoidFunction;
|
|
|
|
setCloseFn: (closeFn: VoidFunction) => void;
|
|
|
|
skipped?: boolean;
|
|
|
|
}
|
2021-02-09 21:49:15 +08:00
|
|
|
|
2022-05-11 14:26:18 +08:00
|
|
|
type Task =
|
|
|
|
| OpenTask
|
|
|
|
| TypeTask
|
|
|
|
| {
|
|
|
|
type: 'destroy';
|
|
|
|
key: React.Key;
|
|
|
|
skipped?: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
let taskQueue: Task[] = [];
|
|
|
|
|
|
|
|
let defaultGlobalConfig: ConfigOptions = {};
|
|
|
|
|
|
|
|
function getGlobalContext() {
|
|
|
|
const {
|
|
|
|
prefixCls: globalPrefixCls,
|
|
|
|
getContainer: globalGetContainer,
|
2023-01-17 11:15:14 +08:00
|
|
|
duration,
|
2022-05-11 14:26:18 +08:00
|
|
|
rtl,
|
|
|
|
maxCount,
|
|
|
|
top,
|
|
|
|
} = defaultGlobalConfig;
|
|
|
|
const mergedPrefixCls = globalPrefixCls ?? globalConfig().getPrefixCls('message');
|
|
|
|
const mergedContainer = globalGetContainer?.() || document.body;
|
|
|
|
|
|
|
|
return {
|
|
|
|
prefixCls: mergedPrefixCls,
|
|
|
|
container: mergedContainer,
|
2023-01-17 11:15:14 +08:00
|
|
|
duration,
|
2022-05-11 14:26:18 +08:00
|
|
|
rtl,
|
|
|
|
maxCount,
|
|
|
|
top,
|
|
|
|
};
|
2020-07-15 19:51:56 +08:00
|
|
|
}
|
|
|
|
|
2022-05-11 14:26:18 +08:00
|
|
|
interface GlobalHolderRef {
|
|
|
|
instance: MessageInstance;
|
|
|
|
sync: () => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
const GlobalHolder = React.forwardRef<GlobalHolderRef, {}>((_, ref) => {
|
2023-02-01 10:14:21 +08:00
|
|
|
const initializeMessageConfig: () => ConfigOptions = () => {
|
2023-01-17 11:15:14 +08:00
|
|
|
const { prefixCls, container, maxCount, duration, rtl, top } = getGlobalContext();
|
|
|
|
|
|
|
|
return {
|
|
|
|
prefixCls,
|
|
|
|
getContainer: () => container!,
|
|
|
|
maxCount,
|
|
|
|
duration,
|
|
|
|
rtl,
|
|
|
|
top,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-02-01 10:14:21 +08:00
|
|
|
const [messageConfig, setMessageConfig] = React.useState<ConfigOptions>(initializeMessageConfig);
|
2023-01-17 11:15:14 +08:00
|
|
|
|
2023-02-01 10:14:21 +08:00
|
|
|
const [api, holder] = useInternalMessage(messageConfig);
|
2022-05-11 14:26:18 +08:00
|
|
|
|
|
|
|
const global = globalConfig();
|
|
|
|
const rootPrefixCls = global.getRootPrefixCls();
|
|
|
|
const rootIconPrefixCls = global.getIconPrefixCls();
|
|
|
|
|
|
|
|
const sync = () => {
|
2023-02-01 10:14:21 +08:00
|
|
|
setMessageConfig(initializeMessageConfig);
|
2021-03-02 11:53:12 +08:00
|
|
|
};
|
|
|
|
|
2022-05-11 14:26:18 +08:00
|
|
|
React.useEffect(sync, []);
|
2021-03-02 11:53:12 +08:00
|
|
|
|
2022-05-11 14:26:18 +08:00
|
|
|
React.useImperativeHandle(ref, () => {
|
2023-02-23 16:38:06 +08:00
|
|
|
const instance: MessageInstance = { ...api };
|
2021-03-02 11:53:12 +08:00
|
|
|
|
2023-02-23 16:38:06 +08:00
|
|
|
Object.keys(instance).forEach((method: keyof MessageInstance) => {
|
2022-05-11 14:26:18 +08:00
|
|
|
instance[method] = (...args: any[]) => {
|
|
|
|
sync();
|
|
|
|
return (api as any)[method](...args);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
instance,
|
|
|
|
sync,
|
|
|
|
};
|
2021-03-02 11:53:12 +08:00
|
|
|
});
|
2015-07-23 11:59:56 +08:00
|
|
|
|
2022-05-11 14:26:18 +08:00
|
|
|
return (
|
|
|
|
<ConfigProvider prefixCls={rootPrefixCls} iconPrefixCls={rootIconPrefixCls}>
|
|
|
|
{holder}
|
|
|
|
</ConfigProvider>
|
|
|
|
);
|
|
|
|
});
|
2018-05-25 20:41:07 +08:00
|
|
|
|
2022-05-11 14:26:18 +08:00
|
|
|
function flushNotice() {
|
|
|
|
if (!message) {
|
|
|
|
const holderFragment = document.createDocumentFragment();
|
2018-05-25 20:41:07 +08:00
|
|
|
|
2022-05-11 14:26:18 +08:00
|
|
|
const newMessage: GlobalMessage = {
|
|
|
|
fragment: holderFragment,
|
|
|
|
};
|
2022-03-23 14:49:57 +08:00
|
|
|
|
2022-05-11 14:26:18 +08:00
|
|
|
message = newMessage;
|
|
|
|
|
|
|
|
// Delay render to avoid sync issue
|
|
|
|
act(() => {
|
|
|
|
render(
|
|
|
|
<GlobalHolder
|
2022-11-18 23:34:07 +08:00
|
|
|
ref={(node) => {
|
2022-05-11 14:26:18 +08:00
|
|
|
const { instance, sync } = node || {};
|
|
|
|
|
|
|
|
// React 18 test env will throw if call immediately in ref
|
|
|
|
Promise.resolve().then(() => {
|
|
|
|
if (!newMessage.instance && instance) {
|
|
|
|
newMessage.instance = instance;
|
|
|
|
newMessage.sync = sync;
|
|
|
|
flushNotice();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
/>,
|
|
|
|
holderFragment,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2018-08-31 12:28:09 +08:00
|
|
|
|
2022-05-11 14:26:18 +08:00
|
|
|
// Notification not ready
|
2022-11-18 23:34:07 +08:00
|
|
|
if (!message.instance) {
|
2022-05-11 14:26:18 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// >>> Execute task
|
2022-11-18 23:34:07 +08:00
|
|
|
taskQueue.forEach((task) => {
|
2022-05-11 14:26:18 +08:00
|
|
|
const { type, skipped } = task;
|
|
|
|
|
|
|
|
// Only `skipped` when user call notice but cancel it immediately
|
|
|
|
// and instance not ready
|
|
|
|
if (!skipped) {
|
|
|
|
switch (type) {
|
|
|
|
case 'open': {
|
|
|
|
act(() => {
|
|
|
|
const closeFn = message!.instance!.open({
|
|
|
|
...defaultGlobalConfig,
|
|
|
|
...task.config,
|
|
|
|
});
|
|
|
|
|
|
|
|
closeFn?.then(task.resolve);
|
|
|
|
task.setCloseFn(closeFn);
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'destroy':
|
|
|
|
act(() => {
|
|
|
|
message?.instance!.destroy(task.key);
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Other type open
|
|
|
|
default: {
|
|
|
|
act(() => {
|
|
|
|
const closeFn = message!.instance![type](...task.args);
|
|
|
|
|
|
|
|
closeFn?.then(task.resolve);
|
|
|
|
task.setCloseFn(closeFn);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-04 13:35:36 +08:00
|
|
|
});
|
2022-05-11 14:26:18 +08:00
|
|
|
|
|
|
|
// Clean up
|
|
|
|
taskQueue = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// ==============================================================================
|
|
|
|
// == Export ==
|
|
|
|
// ==============================================================================
|
|
|
|
|
|
|
|
function setMessageGlobalConfig(config: ConfigOptions) {
|
|
|
|
defaultGlobalConfig = {
|
|
|
|
...defaultGlobalConfig,
|
|
|
|
...config,
|
2020-07-15 19:51:56 +08:00
|
|
|
};
|
2022-05-11 14:26:18 +08:00
|
|
|
|
|
|
|
// Trigger sync for it
|
|
|
|
act(() => {
|
|
|
|
message?.sync?.();
|
|
|
|
});
|
2020-07-15 19:51:56 +08:00
|
|
|
}
|
2020-04-04 13:35:36 +08:00
|
|
|
|
2022-05-11 14:26:18 +08:00
|
|
|
function open(config: ArgsProps): MessageType {
|
2022-11-18 23:34:07 +08:00
|
|
|
const result = wrapPromiseFn((resolve) => {
|
2022-05-11 14:26:18 +08:00
|
|
|
let closeFn: VoidFunction;
|
|
|
|
|
|
|
|
const task: OpenTask = {
|
|
|
|
type: 'open',
|
|
|
|
config,
|
|
|
|
resolve,
|
2022-11-18 23:34:07 +08:00
|
|
|
setCloseFn: (fn) => {
|
2022-05-11 14:26:18 +08:00
|
|
|
closeFn = fn;
|
|
|
|
},
|
2018-05-25 20:41:07 +08:00
|
|
|
};
|
2022-05-11 14:26:18 +08:00
|
|
|
taskQueue.push(task);
|
2020-07-26 20:14:11 +08:00
|
|
|
|
2022-05-11 14:26:18 +08:00
|
|
|
return () => {
|
|
|
|
if (closeFn) {
|
|
|
|
act(() => {
|
|
|
|
closeFn();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
task.skipped = true;
|
|
|
|
}
|
|
|
|
};
|
2015-07-23 11:59:56 +08:00
|
|
|
});
|
2022-05-11 14:26:18 +08:00
|
|
|
|
|
|
|
flushNotice();
|
|
|
|
|
2018-05-25 20:41:07 +08:00
|
|
|
return result;
|
2015-07-23 11:59:56 +08:00
|
|
|
}
|
2015-07-11 16:40:59 +08:00
|
|
|
|
2022-05-11 14:26:18 +08:00
|
|
|
function typeOpen(type: NoticeType, args: Parameters<TypeOpen>): MessageType {
|
2023-03-10 14:12:49 +08:00
|
|
|
// Warning if exist theme
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
warnContext('message');
|
|
|
|
}
|
|
|
|
|
2022-11-18 23:34:07 +08:00
|
|
|
const result = wrapPromiseFn((resolve) => {
|
2022-05-11 14:26:18 +08:00
|
|
|
let closeFn: VoidFunction;
|
|
|
|
|
|
|
|
const task: TypeTask = {
|
|
|
|
type,
|
|
|
|
args,
|
|
|
|
resolve,
|
2022-11-18 23:34:07 +08:00
|
|
|
setCloseFn: (fn) => {
|
2022-05-11 14:26:18 +08:00
|
|
|
closeFn = fn;
|
|
|
|
},
|
|
|
|
};
|
2016-07-29 16:32:12 +08:00
|
|
|
|
2022-05-11 14:26:18 +08:00
|
|
|
taskQueue.push(task);
|
2019-09-05 10:15:14 +08:00
|
|
|
|
2022-05-11 14:26:18 +08:00
|
|
|
return () => {
|
|
|
|
if (closeFn) {
|
|
|
|
act(() => {
|
|
|
|
closeFn();
|
|
|
|
});
|
2020-08-06 13:34:18 +08:00
|
|
|
} else {
|
2022-05-11 14:26:18 +08:00
|
|
|
task.skipped = true;
|
2020-08-06 13:34:18 +08:00
|
|
|
}
|
2022-05-11 14:26:18 +08:00
|
|
|
};
|
|
|
|
});
|
2018-08-31 12:28:09 +08:00
|
|
|
|
2022-05-11 14:26:18 +08:00
|
|
|
flushNotice();
|
2019-09-05 10:15:14 +08:00
|
|
|
|
2022-05-11 14:26:18 +08:00
|
|
|
return result;
|
|
|
|
}
|
2019-09-05 10:15:14 +08:00
|
|
|
|
2022-05-11 14:26:18 +08:00
|
|
|
function destroy(key: React.Key) {
|
|
|
|
taskQueue.push({
|
|
|
|
type: 'destroy',
|
|
|
|
key,
|
|
|
|
});
|
|
|
|
flushNotice();
|
2020-07-15 19:51:56 +08:00
|
|
|
}
|
|
|
|
|
2023-02-22 10:41:43 +08:00
|
|
|
interface BaseMethods {
|
2022-05-11 14:26:18 +08:00
|
|
|
open: (config: ArgsProps) => MessageType;
|
|
|
|
destroy: (key?: React.Key) => void;
|
2022-11-19 15:24:28 +08:00
|
|
|
config: typeof setMessageGlobalConfig;
|
2022-05-11 14:26:18 +08:00
|
|
|
useMessage: typeof useMessage;
|
2022-06-07 23:29:12 +08:00
|
|
|
/** @private Internal Component. Do not use in your production. */
|
2022-06-13 17:55:00 +08:00
|
|
|
_InternalPanelDoNotUseOrYouWillBeFired: typeof PurePanel;
|
2023-02-22 10:41:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
interface MessageMethods {
|
|
|
|
info: TypeOpen;
|
|
|
|
success: TypeOpen;
|
|
|
|
error: TypeOpen;
|
|
|
|
warning: TypeOpen;
|
|
|
|
loading: TypeOpen;
|
|
|
|
}
|
|
|
|
|
|
|
|
const methods: (keyof MessageMethods)[] = ['success', 'info', 'warning', 'error', 'loading'];
|
|
|
|
|
|
|
|
const baseStaticMethods: BaseMethods = {
|
2022-05-11 14:26:18 +08:00
|
|
|
open,
|
|
|
|
destroy,
|
|
|
|
config: setMessageGlobalConfig,
|
|
|
|
useMessage,
|
2022-06-13 17:55:00 +08:00
|
|
|
_InternalPanelDoNotUseOrYouWillBeFired: PurePanel,
|
2022-05-11 14:26:18 +08:00
|
|
|
};
|
2018-08-31 12:28:09 +08:00
|
|
|
|
2023-02-22 10:41:43 +08:00
|
|
|
const staticMethods = baseStaticMethods as MessageMethods & BaseMethods;
|
2018-08-31 12:28:09 +08:00
|
|
|
|
2023-02-22 10:41:43 +08:00
|
|
|
methods.forEach((type: keyof MessageMethods) => {
|
2022-05-11 14:26:18 +08:00
|
|
|
staticMethods[type] = (...args: Parameters<TypeOpen>) => typeOpen(type, args);
|
|
|
|
});
|
|
|
|
|
|
|
|
// ==============================================================================
|
|
|
|
// == Test ==
|
|
|
|
// ==============================================================================
|
|
|
|
const noop = () => {};
|
2020-07-15 19:51:56 +08:00
|
|
|
|
2023-04-26 14:53:23 +08:00
|
|
|
/** @internal Only Work in test env */
|
2022-05-11 14:26:18 +08:00
|
|
|
// eslint-disable-next-line import/no-mutable-exports
|
|
|
|
export let actWrapper: (wrapper: any) => void = noop;
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV === 'test') {
|
2022-11-18 23:34:07 +08:00
|
|
|
actWrapper = (wrapper) => {
|
2022-05-11 14:26:18 +08:00
|
|
|
act = wrapper;
|
|
|
|
};
|
2018-08-31 12:28:09 +08:00
|
|
|
}
|
|
|
|
|
2023-04-26 14:53:23 +08:00
|
|
|
/** @internal Only Work in test env */
|
2022-05-11 14:26:18 +08:00
|
|
|
// eslint-disable-next-line import/no-mutable-exports
|
|
|
|
export let actDestroy = noop;
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV === 'test') {
|
|
|
|
actDestroy = () => {
|
|
|
|
message = null;
|
|
|
|
};
|
|
|
|
}
|
2020-09-28 00:41:16 +08:00
|
|
|
|
2022-05-11 14:26:18 +08:00
|
|
|
export default staticMethods;
|