Skip to content

Commit 37c00bf

Browse files
committed
0.5.0
1 parent cde0d03 commit 37c00bf

14 files changed

Lines changed: 115 additions & 89 deletions
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {
88
SimpleFormOperational,
99
sendModalFormAsync,
1010
wrapAsyncFunc,
11+
FormClose,
1112
} = require('./FormAPIEx');
1213

1314
const PLUGIN_NAME = 'FormAPIExExample';
@@ -85,7 +86,9 @@ mc.listen('onServerStarted', () => {
8586
}
8687
*/
8788
const res = await form.sendAsync(player);
88-
player.tell(res ? JSON.stringify(res, null, 2) : String(res));
89+
player.tell(
90+
res === FormClose ? String(res) : JSON.stringify(res, null, 2)
91+
);
8992
})();
9093

9194
return true;
@@ -138,7 +141,7 @@ mc.listen('onServerStarted', () => {
138141
{ text: '点我输出 114514', operation: () => player.tell('114514') },
139142
]
140143
);
141-
await form.sendAsync(player); // 返回值是执行后函数的返回值
144+
await form.sendAsync(player); // 返回值是执行后函数的返回值,或者 FormClose
142145
})();
143146

144147
return true;

lib/FormAPIEx.cjs

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
'use strict';
22

3-
var version = "0.4.2";
3+
var version = "0.5.0";
44

55
const NAME = 'FormAPIEx';
66
const VERSION = (version.split('.').map((v) => Number(v)));
77
const AUTHOR = 'student_2333 <lgc2333@126.com>';
88
const LICENSE = 'Apache-2.0';
9+
const FormClose = Symbol(`${NAME}_FormClose`);
910

1011
/******************************************************************************
1112
Copyright (c) Microsoft Corporation.
@@ -66,7 +67,7 @@ function deepClone(obj) {
6667
}
6768
function 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
*/
311312
function 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

593594
exports.AUTHOR = AUTHOR;
594595
exports.CustomFormEx = CustomFormEx;
596+
exports.FormClose = FormClose;
595597
exports.LICENSE = LICENSE;
596598
exports.NAME = NAME;
597599
exports.SimpleFormAsync = SimpleFormAsync;

lib/FormAPIEx.d.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ declare const NAME = "FormAPIEx";
33
declare const VERSION: [number, number, number];
44
declare const AUTHOR = "student_2333 <lgc2333@126.com>";
55
declare const LICENSE = "Apache-2.0";
6+
declare const FormClose: unique symbol;
7+
type FormClose = typeof FormClose;
68

79
interface CustomFormLabelObject {
810
type: 'label';
@@ -225,9 +227,9 @@ declare class CustomFormEx<T extends {
225227
/**
226228
* 异步向玩家发送该表单
227229
* @param player 玩家对象
228-
* @returns 返回结果,玩家关闭表单或发送失败返回 null
230+
* @returns 返回结果,玩家关闭表单或发送失败返回 FormClose
229231
*/
230-
sendAsync(player: Player): Promise<CustomFormReturn<T> | null>;
232+
sendAsync(player: Player): Promise<CustomFormReturn<T> | FormClose>;
231233
}
232234

233235
/**
@@ -237,9 +239,9 @@ declare class CustomFormEx<T extends {
237239
* @param content 表单内容
238240
* @param confirmButton 确认按钮标题
239241
* @param cancelButton 取消按钮标题
240-
* @returns 玩家选择的按钮,发送失败返回 null
242+
* @returns 玩家选择的按钮
241243
*/
242-
declare function sendModalFormAsync(player: Player, title: string, content: string, confirmButton?: string, cancelButton?: string): Promise<boolean | null | undefined>;
244+
declare function sendModalFormAsync(player: Player, title: string, content: string, confirmButton?: string, cancelButton?: string): Promise<boolean>;
243245

244246
interface SimpleFormAsyncOptions {
245247
/** 表单标题 */
@@ -282,9 +284,9 @@ declare class SimpleFormAsync {
282284
/**
283285
* 异步向玩家发送该表单
284286
* @param player 玩家对象
285-
* @returns 玩家选择的按钮序号,玩家关闭表单或发送失败返回 null 或 undefined
287+
* @returns 玩家选择的按钮序号,玩家关闭表单或发送失败返回 FormClose
286288
*/
287-
sendAsync(player: Player): Promise<number | null | undefined>;
289+
sendAsync(player: Player): Promise<number | FormClose>;
288290
}
289291

290292
declare class SimpleFormEx<T> {
@@ -348,16 +350,16 @@ declare class SimpleFormEx<T> {
348350
* 异步向玩家发送搜索表单
349351
* @param player 玩家对象
350352
* @param defaultVal 搜索框默认内容
351-
* @returns 选择的搜索结果按钮参数。返回 null 为没搜到, false 为取消搜索
353+
* @returns 选择的搜索结果按钮参数。返回 null 为没搜到, FormClose 为取消搜索
352354
*/
353-
sendSearchForm(player: Player, defaultVal?: string): Promise<T | null | false>;
355+
sendSearchForm(player: Player, defaultVal?: string): Promise<T | null | FormClose>;
354356
/**
355357
* 异步向玩家发送表单
356358
* @param player 玩家对象
357359
* @param page 页码
358-
* @returns 给定的按钮参数,表单被玩家关闭或发送失败返回 null
360+
* @returns 给定的按钮参数,表单被玩家关闭或发送失败返回 FormClose
359361
*/
360-
sendAsync(player: Player, page?: number): Promise<T | null>;
362+
sendAsync(player: Player, page?: number): Promise<T | FormClose>;
361363
}
362364

363365
interface SimpleFormOperationalButton<R> {
@@ -370,7 +372,7 @@ declare class SimpleFormOperational<R> {
370372
content: string;
371373
buttons: SimpleFormOperationalButton<R>[];
372374
constructor(title?: string, content?: string, buttons?: SimpleFormOperationalButton<R>[]);
373-
sendAsync(player: Player): Promise<R | null>;
375+
sendAsync(player: Player): Promise<R | FormClose>;
374376
}
375377

376378
/**
@@ -397,7 +399,7 @@ declare function deepClone<T>(obj: T): T;
397399
* @param form 表单对象
398400
* @returns 返回结果
399401
*/
400-
declare function sendFormAsync(player: Player, form: SimpleForm): Promise<number | null | undefined>;
401-
declare function sendFormAsync(player: Player, form: CustomForm): Promise<(string | boolean | number)[] | null | undefined>;
402+
declare function sendFormAsync(player: Player, form: SimpleForm): Promise<number | FormClose>;
403+
declare function sendFormAsync(player: Player, form: CustomForm): Promise<(string | boolean | number)[] | FormClose>;
402404

403-
export { AUTHOR, CustomFormDropdownObject, CustomFormEx, CustomFormInputObject, CustomFormInputOptions, CustomFormLabelObject, CustomFormObject, CustomFormObjectReturnType, CustomFormReturn, CustomFormSliderObject, CustomFormSliderOptions, CustomFormStepSliderObject, CustomFormSwitchObject, LICENSE, NAME, SimpleFormAsync, SimpleFormAsyncOptions, SimpleFormEx, SimpleFormOperational, SimpleFormOperationalButton, VERSION, buildCustomForm, deepClone, formatError, sendFormAsync, sendModalFormAsync, wrapAsyncFunc };
405+
export { AUTHOR, CustomFormDropdownObject, CustomFormEx, CustomFormInputObject, CustomFormInputOptions, CustomFormLabelObject, CustomFormObject, CustomFormObjectReturnType, CustomFormReturn, CustomFormSliderObject, CustomFormSliderOptions, CustomFormStepSliderObject, CustomFormSwitchObject, FormClose, LICENSE, NAME, SimpleFormAsync, SimpleFormAsyncOptions, SimpleFormEx, SimpleFormOperational, SimpleFormOperationalButton, VERSION, buildCustomForm, deepClone, formatError, sendFormAsync, sendModalFormAsync, wrapAsyncFunc };

0 commit comments

Comments
 (0)