@@ -38,6 +38,7 @@ export function registerListeners(){
38
38
}
39
39
40
40
type MenuConfirmProps = {
41
+ /** This message is sent to the user (prefixed with /!\) if they cancel. */
41
42
cancelOutput ?: string ;
42
43
title ?: string ;
43
44
confirmText ?: string ;
@@ -69,7 +70,7 @@ export const Menu = {
69
70
} :{
70
71
optionStringifier ?:( opt :TOption ) => string ;
71
72
/**
72
- * Specifies the behavior when the player cancels the menu (by clicking Cancel, or by pressing Escape).
73
+ * Specifies the behavior when the player cancels the menu (by clicking Cancel, or by pressing Escape).
73
74
* @default "ignore"
74
75
*/
75
76
onCancel ?: TCancelBehavior ;
@@ -114,7 +115,13 @@ export const Menu = {
114
115
}
115
116
} } ) ;
116
117
117
- Call . menu ( target . con , registeredListeners . generic , title , description , arrangedOptions . map ( r => r . map ( optionStringifier ) ) ) ;
118
+ let i = 0 ;
119
+ const stringifiedOptions = arrangedOptions . map ( r => r . map ( item => {
120
+ if ( i === cancelOptionId ) return item as string ;
121
+ i ++ ;
122
+ return optionStringifier ( item ) ;
123
+ } ) ) ;
124
+ Call . menu ( target . con , registeredListeners . generic , title , description , stringifiedOptions ) ;
118
125
return promise ;
119
126
} ,
120
127
/** Displays a menu to a player, returning a Promise. Arranges options into a 2D array, and can add a Cancel option. */
@@ -133,7 +140,7 @@ export const Menu = {
133
140
const arrangedOptions = ( options . length == 0 && ! includeCancel ) ? [ ] : to2DArray ( options , columns ) ;
134
141
135
142
if ( includeCancel ) {
136
- arrangedOptions . push ( [ "Cancel" as never ] ) ;
143
+ arrangedOptions . push ( [ "[red] Cancel[] " as never ] ) ;
137
144
//This is safe because cancelOptionId is set,
138
145
//so the handler will never get called with "Cancel".
139
146
cancelOptionId = options . length ;
@@ -174,7 +181,7 @@ export const Menu = {
174
181
return Menu . raw ( title , description , options , target , {
175
182
...cfg ,
176
183
optionStringifier : o => o . text ,
177
- } ) . then ( o => o ?. data ) ;
184
+ } ) . then ( o => o ?. data as TButtonData | ( TCancelBehavior extends "null" ? null : never ) ) ;
178
185
} ,
179
186
pages < TOption extends unknown , TCancelBehavior extends MenuCancelOption > (
180
187
this :void , target :FishPlayer , title :string , description :string ,
@@ -209,7 +216,7 @@ export const Menu = {
209
216
showPage ( 0 ) ;
210
217
return promise ;
211
218
} ,
212
- pagedListButtons < TButtonData extends unknown , MenuCancelBehavior extends MenuCancelOption > (
219
+ pagedListButtons < TButtonData extends unknown , MenuCancelBehavior extends MenuCancelOption = "ignore" > (
213
220
this :void , target :FishPlayer , title :string , description :string ,
214
221
options :{ data : TButtonData ; text : string ; } [ ] ,
215
222
{ rowsPerPage = 10 , columns = 3 , ...cfg } : Pick < MenuOptions < TButtonData , MenuCancelBehavior > , "columns" | "onCancel" > & {
@@ -219,23 +226,22 @@ export const Menu = {
219
226
) {
220
227
//Generate pages
221
228
const pages = to2DArray ( to2DArray ( options , columns ) , rowsPerPage ) ;
222
- if ( pages . length == 1 ) return Menu . buttons ( target , title , description , pages [ 0 ] , cfg ) ;
229
+ if ( pages . length <= 1 ) return Menu . buttons ( target , title , description , pages [ 0 ] ?? [ ] , cfg ) ;
223
230
return Menu . pages ( target , title , description , pages , cfg ) ;
224
231
} ,
225
- pagedList < TButtonData extends unknown , MenuCancelBehavior extends MenuCancelOption > (
232
+ pagedList < TButtonData extends unknown , MenuCancelBehavior extends MenuCancelOption = "ignore" > (
226
233
this :void , target :FishPlayer , title :string , description :string ,
227
234
options :TButtonData [ ] ,
228
- { rowsPerPage = 10 , columns = 3 , optionStringifier, ...cfg } : Pick < MenuOptions < TButtonData , MenuCancelBehavior > , "columns" | "onCancel" | "optionStringifier" > & {
235
+ { rowsPerPage = 10 , columns = 3 , optionStringifier = String , ...cfg } : Pick < MenuOptions < TButtonData , MenuCancelBehavior > , "columns" | "onCancel" | "optionStringifier" > & {
229
236
/** @default 10 */
230
237
rowsPerPage ?:number ;
231
- optionStringifier : { } ;
232
- } ,
238
+ } = { } ,
233
239
) {
234
240
//Generate pages
235
241
const pages = to2DArray ( to2DArray ( options . map (
236
242
o => ( { data : o , get text ( ) { return optionStringifier ( o ) ; } } )
237
243
) , columns ) , rowsPerPage ) ;
238
- if ( pages . length == 1 ) return Menu . buttons ( target , title , description , pages [ 0 ] , cfg ) ;
244
+ if ( pages . length <= 1 ) return Menu . buttons ( target , title , description , pages [ 0 ] ?? [ ] , cfg ) ;
239
245
return Menu . pages ( target , title , description , pages , cfg ) ;
240
246
}
241
247
}
0 commit comments