外观
main.js
js
import { storage, files, html, toast, links } from '@kodet/core';
import { defineApp, Page, Host, Dialog, UI } from '@kodet/content';
import { reading } from './reading-modules.js';
import { registerSelectionSearch } from './selection-search.js';
const app = defineApp();
registerSelectionSearch(app);
const bookDetail = app.api('book', reading.loadBookDetail);
// 观察错误,不在诊断任务内操作页面;局部恢复仍使用 try/catch。
app.onError(({ error }) => { console.error(error.code, error.message); });
app.onInit(async () => {
if (await storage.get('name') === undefined) await storage.set('name', '读者');
});
// 平台打开入口,与 JS 模块 export 无关。route 是本应用公开的功能名。
app.onOpen(async request => {
if (request.kind === 'route' && request.route === 'detail' || request.kind === 'link') {
await Host.openPage('opening', { request }).run();
}
});
app.page('opening', '打开参数').load(ctx => Page.create().next(null).blocks(blocks => {
blocks.text(JSON.stringify(ctx.params.request, null, 2));
blocks.entry({ name: '进入演示首页', onClick: async () => { await links.openApp(app.manifest.id); } });
}));
app.home('home', '演示').initial(true).load(async () => {
const stored = await storage.get('name');
const name = typeof stored === 'string' ? stored : '读者';
return Page.create().next(null).blocks(blocks => {
blocks.text(`你好,${name}`, { id: 'greeting' });
blocks.entry({ name: '我的空间 · 历史', onClick: Host.openMySpace() });
blocks.entry({ name: '我的空间 · 书架', onClick: async () => { await Host.openMySpace('bookshelf').run(); } });
blocks.entry({ name: '我的空间 · 收藏', onClick: Host.openMySpace('favorites') });
blocks.entry({ name: '无标题和按钮的对话框', onClick: async () => {
await Dialog.create().blocks(b => b.text('仅显示正文;返回或点击外侧关闭。')).show();
} });
blocks.entry({ name: '单按钮自然高度对话框', onClick: async () => {
await Dialog.create().blocks(b => b.text('短内容')).confirm().show();
} });
blocks.entry({ name: '修改称呼', desc: '弹窗保存后更新当前页面', onClick: async () => {
// 在打开弹窗前获取页面句柄,确认按钮中仍然指向原页面。
const greeting = UI('greeting');
const currentName = await storage.get('name');
const dialog = Dialog.create().title('修改称呼').cancel('取消')
.blocks(b => { b.input({ id: 'name', hint: '输入称呼', text: typeof currentName === 'string' ? currentName : name }); })
.confirm('保存', async () => {
const input = await dialog.ui('name').get();
const value = typeof input.text === 'string' ? input.text.trim() : '';
if (!value) { await toast.show('请先输入称呼'); return; }
await storage.set('name', value);
await greeting.update({ text: `你好,${value}` });
await dialog.close();
});
await dialog.show();
} });
blocks.entry({ name: '打开小说详情', desc: '两章包内文本,可离线阅读',
onClick: Host.openDetail(bookDetail, { name: '山间来信' }) });
blocks.entry({ name: '直接进入阅读器', desc: '由原生页面加载详情和章节',
onClick: Host.openReader(bookDetail, { name: '山间来信' }) });
blocks.entry({ name: '通用 SDK 离线演示', desc: '命名空间、文件流、摘要与批量 HTML 提取', onClick: Host.openPage('sdk') });
blocks.entry({ name: '查看说明', onClick: Host.openPage('about') });
blocks.entry({ name: '多选与搜索', desc: '离线筛选数组、内联多选和搜索范围', onClick: Host.openPage('selection') });
});
});
app.page('about', '示例说明').load(() => Page.create().next(null).blocks(blocks => {
blocks.text('通用能力从 @kodet/core 导入,内容模型从 @kodet/content 导入。');
blocks.text('本示例不发起网络请求。network.js 提供可复用的请求与流式读取写法。');
}));
app.page('sdk', '通用 SDK').load(async () => {
const demo = storage.namespace('offline-demo');
await demo.batch([{ type: 'set', key: 'visited', value: true }]);
const source = files.resource('@res/letter.txt');
const copy = files.temp.file('letter-copy.txt');
await source.readable().pipeTo(copy.writable());
const bytes = await copy.readBytes();
const digest = new Uint8Array(await crypto.subtle.digest('SHA-256', bytes)).toHex();
const document = await html.parse('<article><h2>离线数据</h2><a href="/help">说明</a></article>', { baseURL: 'https://example.test/' });
try {
const rows = await document.select('article').extract({ title: 'h2', url: { selector: 'a', value: 'url', attribute: 'href' } });
return Page.create().next(null).blocks(blocks => {
blocks.text(`文件:${bytes.length} 字节`);
blocks.text(`SHA-256:${digest}`);
blocks.text(`批量解析:${JSON.stringify(rows)}`);
});
} finally {
await document.close();
await copy.remove();
}
});