@@ -11,6 +11,7 @@ import type { MountProps } from '@components/base';
1111import { useOptions } from '@components/context' ;
1212import { type NotifyPosition , notifyPositions } from '@components/context/options/options' ;
1313import { Message , type MessageProps , type MessageType } from '@components/notify/message' ;
14+ import { createDeferred } from './promise' ;
1415import styles from './style.module.css' ;
1516
1617let notifyInst : typeof notify ;
@@ -45,48 +46,39 @@ export interface NotifyOptions {
4546 pos ?: NotifyPosition ;
4647
4748 /**
48- * 点击确认按钮时触发的回调
49+ * 显示的操作按钮
4950 *
50- * @remarks
51- * 只有指定该值,才会显示确定按钮。该操作会关闭整个消息框,但是返回 true 可以取消关闭通知框。
51+ * @reactive
5252 */
53- accept ?: MessageProps [ 'onAccept' ] ;
54-
55- /**
56- * 点击取消按钮时触发的回调
57- *
58- * @remarks
59- * 只有指定该值,才会显示取消按钮。该操作会关闭整个消息框,但是返回 true 可以取消关闭通知框。
60- */
61- cancel ?: MessageProps [ 'onCancel' ] ;
53+ actions ?: Array < 'accept' | 'cancel' > ;
6254}
6355
6456/**
6557 * {@link notify } 的快捷方式,用于显示成功信息。
6658 */
67- export async function success ( title : string , o ?: Omit < NotifyOptions , 'type' > ) : Promise < void > {
68- await notify ( title , o ? { ...o , type : 'success' } : { type : 'success' } ) ;
59+ export async function success ( title : string , o ?: Omit < NotifyOptions , 'type' > ) : Promise < boolean > {
60+ return await notify ( title , o ? { ...o , type : 'success' } : { type : 'success' } ) ;
6961}
7062
7163/**
7264 * {@link notify } 的快捷方式,用于显示普通信息。
7365 */
74- export async function info ( title : string , o ?: Omit < NotifyOptions , 'type' > ) : Promise < void > {
75- await notify ( title , o ? { ...o , type : 'info' } : { type : 'info' } ) ;
66+ export async function info ( title : string , o ?: Omit < NotifyOptions , 'type' > ) : Promise < boolean > {
67+ return await notify ( title , o ? { ...o , type : 'info' } : { type : 'info' } ) ;
7668}
7769
7870/**
7971 * {@link notify } 的快捷方式,用于显示警告信息。
8072 */
81- export async function warning ( title : string , o ?: Omit < NotifyOptions , 'type' > ) : Promise < void > {
82- await notify ( title , o ? { ...o , type : 'warning' } : { type : 'warning' } ) ;
73+ export async function warning ( title : string , o ?: Omit < NotifyOptions , 'type' > ) : Promise < boolean > {
74+ return await notify ( title , o ? { ...o , type : 'warning' } : { type : 'warning' } ) ;
8375}
8476
8577/**
8678 * {@link notify } 的快捷方式,用于显示错误信息。
8779 */
88- export async function error ( title : string , o ?: Omit < NotifyOptions , 'type' > ) : Promise < void > {
89- await notify ( title , o ? { ...o , type : 'error' } : { type : 'error' } ) ;
80+ export async function error ( title : string , o ?: Omit < NotifyOptions , 'type' > ) : Promise < boolean > {
81+ return await notify ( title , o ? { ...o , type : 'error' } : { type : 'error' } ) ;
9082}
9183
9284/**
@@ -95,8 +87,8 @@ export async function error(title: string, o?: Omit<NotifyOptions, 'type'>): Pro
9587 * @param title - 通知标题;
9688 * @param o - 其他选项;
9789 */
98- export async function notify ( title : string , o ?: NotifyOptions ) : Promise < void > {
99- await notifyInst ( title , o ) ;
90+ export async function notify ( title : string , o ?: NotifyOptions ) : Promise < boolean > {
91+ return await notifyInst ( title , o ) ;
10092}
10193
10294/**
@@ -134,24 +126,26 @@ function init(): JSX.Element {
134126 let bottomRef : HTMLDivElement ;
135127 const owner = getOwner ( ) ;
136128
137- notifyInst = async ( title : string , o ?: NotifyOptions ) : Promise < void > => {
129+ notifyInst = async ( title : string , o ?: NotifyOptions ) : Promise < boolean > => {
138130 if ( o ?. system && document . visibilityState === 'hidden' ) {
139131 const n = await system ( title , { body : o ?. body } ) ;
140132
141133 if ( o ?. duration && n ) {
142134 await sleep ( o . duration ) ;
143135 n . close ( ) ;
144136 }
145- return ;
137+ return new Promise < boolean > ( resolve => resolve ( false ) ) ;
146138 }
147139
140+ const [ p , resolve ] = createDeferred < boolean > ( ) ;
141+
148142 const props : MessageProps = {
149143 title : title ,
150144 body : o ?. body ,
151145 type : o ?. type ,
152146 duration : o ?. duration ?? opt . getStays ( ) ,
153- onAccept : o ?. accept ,
154- onCancel : o ?. cancel ,
147+ onAccept : o ?. actions ?. includes ( ' accept' ) ? async ( ) => resolve ( true ) : undefined ,
148+ onCancel : o ?. actions ?. includes ( ' cancel' ) ? async ( ) => resolve ( false ) : undefined ,
155149 } ;
156150
157151 const pos = o ?. pos ?? opt . getNotifyPosition ( ) ;
@@ -162,6 +156,8 @@ function init(): JSX.Element {
162156 case 'bottom' :
163157 runWithOwner ( owner , ( ) => render ( ( ) => < Message { ...props } /> , bottomRef ) ) ;
164158 }
159+
160+ return p ;
165161 } ;
166162
167163 systemInst = async ( title : string , o ?: NotificationOptions ) : Promise < Notification | undefined > => {
0 commit comments