外观
交互组件.js
js
import { Dialog, Loading, UI } from '@kodet/content';
import { files, toast } from '@kodet/core';
/** 只使用公开 builder,不构造内部 dialog/callback/token 报文。
* 未设置标题则收起标题区域;未调用 cancel/confirm 不产生按钮,单按钮占满宽度。
* cancel()/confirm() 不传名称使用宿主默认文案,空字符串移除对应按钮。
* cancelable(默认true)独立控制返回键和外部点击关闭。
*/
export async function editName() {
const dialog = Dialog.create().title('编辑称呼').cancelable(true)
.blocks(b => { b.input({ id: 'name', text: '' }); })
.cancel('取消')
.confirm('保存', async () => {
const input = await dialog.ui('name').get();
await toast.show(String(input.text));
await dialog.close();
});
return await dialog.wait();
}
/** show 等待接受显示,wait 自动显示并等待实际关闭;重复 wait 复用同次显示。
* 无回调按钮自动关闭,有回调显式 close;wait 返回 confirm 或 cancel+reason,不携带表单值。
* onShown/onClosed 等完整签名见交互指南;所属页面销毁后 wait 报 ABORTED。
* 显示前完成 title/blocks/buttons 配置;运行时 button('confirm'|'cancel').update({enabled?,loading?})。
* 短内容自然高度,长内容受窗口约束可滚动;无按钮时专属间距收起,高度变化有过渡。
*/
export async function showWithoutActions() {
await Dialog.create().blocks(b => { b.text('点击外部或返回关闭'); }).wait();
}
/** 弹窗回调中的 UI(id) 默认定位该弹窗;更新原页要在打开前保存句柄。 */
export async function updatePageFromDialog() {
const title = UI('page-title');
const dialog = Dialog.create().confirm('更新', async () => {
await title.update({ text: '新的标题' });
await dialog.close();
});
await dialog.show();
}
/** Loading 无标题和进度参数,同一任务内创建与关闭;不跨点击保存句柄。 */
export async function readWithLoading() {
return await Loading.run(async () => await files.readText('@res/help.txt'));
}
export async function readWithManualLoading() {
const loading = await Loading.show();
try { return await files.readText('@res/help.txt'); }
finally { await loading.close(); }
}