11'use strict' ;
22
3- var version = "0.4.2 " ;
3+ var version = "0.5.0 " ;
44
55const NAME = 'FormAPIEx' ;
66const VERSION = ( version . split ( '.' ) . map ( ( v ) => Number ( v ) ) ) ;
77const AUTHOR = 'student_2333 <lgc2333@126.com>' ;
88const LICENSE = 'Apache-2.0' ;
9+ const FormClose = Symbol ( `${ NAME } _FormClose` ) ;
910
1011/******************************************************************************
1112Copyright (c) Microsoft Corporation.
@@ -66,7 +67,7 @@ function deepClone(obj) {
6667}
6768function sendFormAsync ( player , form ) {
6869 return new Promise ( ( resolve ) => {
69- player . sendForm ( form , ( _ , data ) => setTimeout ( ( ) => resolve ( data ) , 0 ) ) ;
70+ player . sendForm ( form , ( _ , data ) => setTimeout ( ( ) => resolve ( data === null || data === undefined ? FormClose : data ) , 0 ) ) ;
7071 } ) ;
7172}
7273
@@ -288,12 +289,12 @@ class CustomFormEx {
288289 /**
289290 * 异步向玩家发送该表单
290291 * @param player 玩家对象
291- * @returns 返回结果,玩家关闭表单或发送失败返回 null
292+ * @returns 返回结果,玩家关闭表单或发送失败返回 FormClose
292293 */
293294 async sendAsync ( player ) {
294295 const data = await sendFormAsync ( player , buildCustomForm ( this . title , this . objects . map ( ( v ) => v [ 1 ] ) ) ) ;
295- if ( data === null || data === undefined )
296- return null ;
296+ if ( data === FormClose )
297+ return FormClose ;
297298 return this . parseReturn ( data ) ;
298299 }
299300}
@@ -306,11 +307,12 @@ _CustomFormEx_objects = new WeakMap();
306307 * @param content 表单内容
307308 * @param confirmButton 确认按钮标题
308309 * @param cancelButton 取消按钮标题
309- * @returns 玩家选择的按钮,发送失败返回 null
310+ * @returns 玩家选择的按钮
310311 */
311312function sendModalFormAsync ( player , title , content , confirmButton = '§a确认' , cancelButton = '§c取消' ) {
313+ // 不知道怎么回事按取消会返回 null / undefined,干脆直接转 boolean
312314 return new Promise ( ( resolve ) => {
313- player . sendModalForm ( title , content , confirmButton , cancelButton , ( _ , data ) => setTimeout ( ( ) => resolve ( data ) , 0 ) ) ;
315+ player . sendModalForm ( title , content , confirmButton , cancelButton , ( _ , data ) => setTimeout ( ( ) => resolve ( ! ! data ) , 0 ) ) ;
314316 } ) ;
315317}
316318
@@ -364,7 +366,7 @@ class SimpleFormAsync {
364366 /**
365367 * 异步向玩家发送该表单
366368 * @param player 玩家对象
367- * @returns 玩家选择的按钮序号,玩家关闭表单或发送失败返回 null 或 undefined
369+ * @returns 玩家选择的按钮序号,玩家关闭表单或发送失败返回 FormClose
368370 */
369371 sendAsync ( player ) {
370372 const form = mc
@@ -468,15 +470,15 @@ class SimpleFormEx {
468470 * 异步向玩家发送搜索表单
469471 * @param player 玩家对象
470472 * @param defaultVal 搜索框默认内容
471- * @returns 选择的搜索结果按钮参数。返回 null 为没搜到, false 为取消搜索
473+ * @returns 选择的搜索结果按钮参数。返回 null 为没搜到, FormClose 为取消搜索
472474 */
473475 async sendSearchForm ( player , defaultVal = '' ) {
474476 const form = new CustomFormEx ( this . title ) ;
475477 const res = await form
476478 . addInput ( 'param' , '请输入你要搜索的内容' , { default : defaultVal } )
477479 . sendAsync ( player ) ;
478- if ( ! res )
479- return false ;
480+ if ( res === FormClose )
481+ return FormClose ;
480482 const searched = this . searcher ( this . buttons , res . param ) ;
481483 if ( ! searched . length ) {
482484 await new SimpleFormAsync ( {
@@ -495,13 +497,13 @@ class SimpleFormEx {
495497 searchForm . maxPageNum = this . maxPageNum ;
496498 searchForm . hasSearchButton = false ;
497499 const selected = await searchForm . sendAsync ( player ) ;
498- return selected === null ? false : selected ;
500+ return selected === FormClose ? FormClose : selected ;
499501 }
500502 /**
501503 * 异步向玩家发送表单
502504 * @param player 玩家对象
503505 * @param page 页码
504- * @returns 给定的按钮参数,表单被玩家关闭或发送失败返回 null
506+ * @returns 给定的按钮参数,表单被玩家关闭或发送失败返回 FormClose
505507 */
506508 async sendAsync ( player , page = 1 ) {
507509 const buttons = this . canTurnPage ? this . getPage ( page ) : this . buttons ;
@@ -536,16 +538,15 @@ class SimpleFormEx {
536538 content : formatContent ( this . content ) ,
537539 buttons : formattedButtons ,
538540 } ) . sendAsync ( player ) ;
539- if ( resultIndex === null || resultIndex === undefined )
540- return null ;
541+ if ( resultIndex === FormClose )
542+ return FormClose ;
541543 let offset = 0 ;
542544 if ( this . hasSearchButton ) {
543545 if ( resultIndex === offset ) {
544546 const res = await this . sendSearchForm ( player ) ;
545- if ( res === false || res === null ) {
546- return this . sendAsync ( player , page ) ;
547- }
548- return res ;
547+ return res === null || res === FormClose
548+ ? this . sendAsync ( player , page )
549+ : res ;
549550 }
550551 offset += 1 ;
551552 }
@@ -556,7 +557,7 @@ class SimpleFormEx {
556557 default : page ,
557558 } )
558559 . sendAsync ( player ) ;
559- return this . sendAsync ( player , res ? res . num : page ) ;
560+ return this . sendAsync ( player , res === FormClose ? page : res . num ) ;
560561 }
561562 offset += 1 ;
562563 }
@@ -584,14 +585,15 @@ class SimpleFormOperational {
584585 const form = new SimpleFormEx ( this . buttons ) ;
585586 form . formatter = ( { text, image } ) => [ text , image ] ;
586587 const res = await form . sendAsync ( player ) ;
587- if ( ! res )
588- return res ;
588+ if ( res === FormClose )
589+ return FormClose ;
589590 return res . operation ( ) ;
590591 }
591592}
592593
593594exports . AUTHOR = AUTHOR ;
594595exports . CustomFormEx = CustomFormEx ;
596+ exports . FormClose = FormClose ;
595597exports . LICENSE = LICENSE ;
596598exports . NAME = NAME ;
597599exports . SimpleFormAsync = SimpleFormAsync ;
0 commit comments