2023-07-28 00:51:07 -07:00
|
|
|
import type { ElMessageBoxOptions } from 'element-plus';
|
|
|
|
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() {
|
2023-05-16 02:00:22 -07:00
|
|
|
const handleCancelOrClose = (e: unknown) => {
|
|
|
|
if (e instanceof Error) throw e;
|
|
|
|
else return e;
|
|
|
|
};
|
|
|
|
|
2023-04-18 03:41:55 -07:00
|
|
|
async function alert(
|
|
|
|
message: string,
|
2023-04-26 08:52:53 -07:00
|
|
|
configOrTitle?: string | ElMessageBoxOptions,
|
|
|
|
config?: ElMessageBoxOptions,
|
2023-04-18 03:41:55 -07:00
|
|
|
) {
|
|
|
|
const resolvedConfig = {
|
|
|
|
...(config || (typeof configOrTitle === 'object' ? configOrTitle : {})),
|
|
|
|
cancelButtonClass: 'btn--cancel',
|
|
|
|
confirmButtonClass: 'btn--confirm',
|
|
|
|
};
|
|
|
|
|
|
|
|
if (typeof configOrTitle === 'string') {
|
2023-05-16 02:00:22 -07:00
|
|
|
return MessageBox.alert(message, configOrTitle, resolvedConfig).catch(handleCancelOrClose);
|
2023-04-18 03:41:55 -07:00
|
|
|
}
|
2023-05-16 02:00:22 -07:00
|
|
|
return MessageBox.alert(message, resolvedConfig).catch(handleCancelOrClose);
|
2023-04-18 03:41:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async function confirm(
|
|
|
|
message: string,
|
2023-04-26 08:52:53 -07:00
|
|
|
configOrTitle?: string | ElMessageBoxOptions,
|
|
|
|
config?: ElMessageBoxOptions,
|
2023-08-16 00:29:11 -07:00
|
|
|
): Promise<MessageBoxConfirmResult> {
|
2023-04-18 03:41:55 -07:00
|
|
|
const resolvedConfig = {
|
|
|
|
...(config || (typeof configOrTitle === 'object' ? configOrTitle : {})),
|
|
|
|
cancelButtonClass: 'btn--cancel',
|
|
|
|
confirmButtonClass: 'btn--confirm',
|
|
|
|
distinguishCancelAndClose: true,
|
|
|
|
showClose: config?.showClose || false,
|
|
|
|
closeOnClickModal: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (typeof configOrTitle === 'string') {
|
2023-08-16 00:29:11 -07:00
|
|
|
return MessageBox.confirm(message, configOrTitle, resolvedConfig).catch(
|
|
|
|
handleCancelOrClose,
|
|
|
|
) as unknown as Promise<MessageBoxConfirmResult>;
|
2023-04-18 03:41:55 -07:00
|
|
|
}
|
2023-08-16 00:29:11 -07:00
|
|
|
return MessageBox.confirm(message, resolvedConfig).catch(
|
|
|
|
handleCancelOrClose,
|
|
|
|
) as unknown as Promise<MessageBoxConfirmResult>;
|
2023-04-18 03:41:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async function prompt(
|
|
|
|
message: string,
|
2023-04-26 08:52:53 -07:00
|
|
|
configOrTitle?: string | ElMessageBoxOptions,
|
|
|
|
config?: ElMessageBoxOptions,
|
2023-04-18 03:41:55 -07:00
|
|
|
) {
|
|
|
|
const resolvedConfig = {
|
|
|
|
...(config || (typeof configOrTitle === 'object' ? configOrTitle : {})),
|
|
|
|
cancelButtonClass: 'btn--cancel',
|
|
|
|
confirmButtonClass: 'btn--confirm',
|
|
|
|
};
|
|
|
|
|
|
|
|
if (typeof configOrTitle === 'string') {
|
2023-05-16 02:00:22 -07:00
|
|
|
return MessageBox.prompt(message, configOrTitle, resolvedConfig).catch(handleCancelOrClose);
|
2023-04-18 03:41:55 -07:00
|
|
|
}
|
2023-05-16 02:00:22 -07:00
|
|
|
return MessageBox.prompt(message, resolvedConfig).catch(handleCancelOrClose);
|
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
|
|
|
};
|
|
|
|
}
|