2024-06-03 07:33:20 -07:00
|
|
|
import type { ElMessageBoxOptions, Action, MessageBoxInputData } from 'element-plus';
|
2023-07-28 00:51:07 -07:00
|
|
|
import { ElMessageBox as MessageBox } from 'element-plus';
|
2023-04-18 03:41:55 -07:00
|
|
|
|
2023-08-16 00:29:11 -07:00
|
|
|
export type MessageBoxConfirmResult = 'confirm' | 'cancel';
|
|
|
|
|
2023-04-18 03:41:55 -07:00
|
|
|
export function useMessage() {
|
2024-06-03 07:33:20 -07:00
|
|
|
const handleCancelOrClose = (e: Action | Error): Action => {
|
2023-05-16 02:00:22 -07:00
|
|
|
if (e instanceof Error) throw e;
|
2024-06-03 07:33:20 -07:00
|
|
|
|
|
|
|
return e;
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleCancelOrClosePrompt = (e: Error | Action): MessageBoxInputData => {
|
|
|
|
if (e instanceof Error) throw e;
|
|
|
|
|
|
|
|
return { value: '', action: e };
|
2023-05-16 02:00:22 -07:00
|
|
|
};
|
|
|
|
|
2023-04-18 03:41:55 -07:00
|
|
|
async function alert(
|
2023-08-25 00:39:14 -07:00
|
|
|
message: ElMessageBoxOptions['message'],
|
2023-04-26 08:52:53 -07:00
|
|
|
configOrTitle?: string | ElMessageBoxOptions,
|
|
|
|
config?: ElMessageBoxOptions,
|
2023-04-18 03:41:55 -07:00
|
|
|
) {
|
|
|
|
const resolvedConfig = {
|
2024-06-03 07:33:20 -07:00
|
|
|
...(config ?? (typeof configOrTitle === 'object' ? configOrTitle : {})),
|
2023-04-18 03:41:55 -07:00
|
|
|
cancelButtonClass: 'btn--cancel',
|
|
|
|
confirmButtonClass: 'btn--confirm',
|
|
|
|
};
|
|
|
|
|
|
|
|
if (typeof configOrTitle === 'string') {
|
2024-01-18 01:28:01 -08:00
|
|
|
return await MessageBox.alert(message, configOrTitle, resolvedConfig).catch(
|
|
|
|
handleCancelOrClose,
|
|
|
|
);
|
2023-04-18 03:41:55 -07:00
|
|
|
}
|
2024-01-18 01:28:01 -08:00
|
|
|
return await MessageBox.alert(message, resolvedConfig).catch(handleCancelOrClose);
|
2023-04-18 03:41:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async function confirm(
|
2023-08-25 00:39:14 -07:00
|
|
|
message: ElMessageBoxOptions['message'],
|
2023-04-26 08:52:53 -07:00
|
|
|
configOrTitle?: string | ElMessageBoxOptions,
|
|
|
|
config?: ElMessageBoxOptions,
|
2024-06-03 07:33:20 -07:00
|
|
|
) {
|
2023-04-18 03:41:55 -07:00
|
|
|
const resolvedConfig = {
|
|
|
|
cancelButtonClass: 'btn--cancel',
|
|
|
|
confirmButtonClass: 'btn--confirm',
|
|
|
|
distinguishCancelAndClose: true,
|
2024-06-03 07:33:20 -07:00
|
|
|
showClose: config?.showClose ?? false,
|
2023-04-18 03:41:55 -07:00
|
|
|
closeOnClickModal: false,
|
2024-07-08 05:57:42 -07:00
|
|
|
...(config ?? (typeof configOrTitle === 'object' ? configOrTitle : {})),
|
2023-04-18 03:41:55 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
if (typeof configOrTitle === 'string') {
|
2024-06-03 07:33:20 -07:00
|
|
|
return await MessageBox.confirm(message, configOrTitle, resolvedConfig).catch(
|
2023-08-16 00:29:11 -07:00
|
|
|
handleCancelOrClose,
|
2024-06-03 07:33:20 -07:00
|
|
|
);
|
2023-04-18 03:41:55 -07:00
|
|
|
}
|
2024-06-03 07:33:20 -07:00
|
|
|
|
|
|
|
return await MessageBox.confirm(message, resolvedConfig).catch(handleCancelOrClose);
|
2023-04-18 03:41:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async function prompt(
|
2023-08-25 00:39:14 -07:00
|
|
|
message: ElMessageBoxOptions['message'],
|
2023-04-26 08:52:53 -07:00
|
|
|
configOrTitle?: string | ElMessageBoxOptions,
|
|
|
|
config?: ElMessageBoxOptions,
|
2023-04-18 03:41:55 -07:00
|
|
|
) {
|
|
|
|
const resolvedConfig = {
|
2024-06-03 07:33:20 -07:00
|
|
|
...(config ?? (typeof configOrTitle === 'object' ? configOrTitle : {})),
|
2023-04-18 03:41:55 -07:00
|
|
|
cancelButtonClass: 'btn--cancel',
|
|
|
|
confirmButtonClass: 'btn--confirm',
|
|
|
|
};
|
|
|
|
|
|
|
|
if (typeof configOrTitle === 'string') {
|
2024-01-18 01:28:01 -08:00
|
|
|
return await MessageBox.prompt(message, configOrTitle, resolvedConfig).catch(
|
2024-06-03 07:33:20 -07:00
|
|
|
handleCancelOrClosePrompt,
|
2024-01-18 01:28:01 -08:00
|
|
|
);
|
2023-04-18 03:41:55 -07:00
|
|
|
}
|
2024-06-03 07:33:20 -07:00
|
|
|
return await MessageBox.prompt(message, resolvedConfig).catch(handleCancelOrClosePrompt);
|
2023-04-18 03:41:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
alert,
|
|
|
|
confirm,
|
|
|
|
prompt,
|
2023-07-28 00:51:07 -07:00
|
|
|
message: MessageBox,
|
2023-04-18 03:41:55 -07:00
|
|
|
};
|
|
|
|
}
|