cortico-world-fun 0.2.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/PUBLISHING.md +57 -205
- package/README.md +59 -5
- package/describe.json +171 -10
- package/dist/artwork.js +64 -0
- package/dist/stories.js +395 -0
- package/dist/webui.js +383 -74
- package/package.json +5 -5
- package/src/ENV_PROMPT.md +2 -1
- package/src/branches.js +62 -0
- package/src/definition.js +16 -4
- package/src/exploration.js +268 -0
- package/src/journeys.js +65 -0
- package/src/keepsakes.js +28 -6
- package/src/recommendations.js +109 -0
- package/src/state.js +71 -39
- package/src/story-examples.js +111 -0
- package/src/story-library.js +149 -0
- package/src/story-packs.js +428 -0
- package/src/world.js +425 -124
package/dist/stories.js
ADDED
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
/** Venue story pack authoring for Coo WebUI. */
|
|
2
|
+
const CSS = `
|
|
3
|
+
.fun-stories{--fs-line:var(--border,#e3e4e3);--fs-ink:var(--foreground,#1b1a1e);--fs-dim:var(--muted-foreground,#5c5c60);--fs-card:var(--surface,#fbfbfb);--fs-soft:color-mix(in srgb,var(--fs-card) 93%,var(--fs-ink));--fs-accent:var(--accent,#00a870);--fs-error:var(--destructive,#b23b35);font:14px/1.65 var(--sans,-apple-system,"Segoe UI","PingFang SC","Microsoft YaHei",sans-serif);color:var(--fs-ink);padding:8px 4px 24px;overflow-wrap:anywhere}
|
|
4
|
+
.fun-stories *{box-sizing:border-box}
|
|
5
|
+
.fun-stories h2,.fun-stories h3,.fun-stories h4,.fun-stories p{margin:0}
|
|
6
|
+
.fun-stories h2{font-size:20px}.fun-stories h3{font-size:15px}.fun-stories h4{font-size:14px}
|
|
7
|
+
.fun-story-heading{margin-bottom:14px;padding:4px 0 12px;border-bottom:1px solid var(--fs-line)}
|
|
8
|
+
.fun-story-note,.fun-story-field,.fun-story-feedback{color:var(--fs-dim);font-size:12px}
|
|
9
|
+
.fun-story-editor,.fun-story-pack{border:1px solid var(--fs-line);border-radius:12px;background:var(--fs-card);padding:12px;margin-bottom:9px}
|
|
10
|
+
.fun-story-editor{background:var(--fs-soft);margin-bottom:16px}
|
|
11
|
+
.fun-story-editor>summary{cursor:pointer;font-weight:600}
|
|
12
|
+
.fun-story-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(min(160px,100%),1fr));gap:6px 10px;margin:8px 0}
|
|
13
|
+
.fun-story-grid :is(input,select){display:block;width:100%;min-width:0;margin-top:3px}
|
|
14
|
+
.fun-story-branches{grid-template-columns:repeat(auto-fit,minmax(min(235px,100%),1fr))}
|
|
15
|
+
.fun-story-branch{margin:0;padding:6px 9px;min-width:0;border:1px solid var(--fs-line);border-radius:var(--radius-sm,6px)}
|
|
16
|
+
.fun-story-branch>.fun-story-field{margin:5px 0}
|
|
17
|
+
.fun-story-row{display:flex;align-items:center;gap:6px 8px;flex-wrap:wrap;margin:6px 0}
|
|
18
|
+
.fun-story-row>h3,.fun-story-row>h4{margin-right:auto}
|
|
19
|
+
.fun-story-field{display:block;font-weight:600}
|
|
20
|
+
.fun-story-row label{display:inline-flex;align-items:center;gap:5px}
|
|
21
|
+
.fun-stories button,.fun-stories select,.fun-stories textarea,.fun-story-grid input{font:inherit;color:inherit;background:var(--fs-card);border:1px solid var(--fs-line);border-radius:var(--radius-sm,6px);padding:4px 9px}
|
|
22
|
+
.fun-stories button,.fun-stories select{min-height:31px;max-width:100%}
|
|
23
|
+
.fun-stories button{cursor:pointer}
|
|
24
|
+
.fun-stories button:hover{border-color:var(--fs-accent)}
|
|
25
|
+
.fun-stories button[aria-disabled=true]{opacity:.55;cursor:default}
|
|
26
|
+
.fun-stories :is(button,select,textarea,input):focus-visible{outline:2px solid var(--fs-accent);outline-offset:2px}
|
|
27
|
+
.fun-stories .fun-story-primary{border-color:var(--fs-accent);background:var(--fs-accent);color:var(--accent-foreground,#fff);font-weight:600}
|
|
28
|
+
.fun-stories .fun-story-danger,.fun-story-errors{color:var(--fs-error)}
|
|
29
|
+
.fun-stories textarea{display:block;width:100%;margin:4px 0;resize:vertical;font:12px/1.55 ui-monospace,Consolas,monospace}
|
|
30
|
+
.fun-story-badge{font-size:11.5px;color:var(--fs-dim);border:1px solid var(--fs-line);border-radius:999px;padding:0 7px;background:var(--fs-soft)}
|
|
31
|
+
.fun-story-unavailable{border-style:dashed}
|
|
32
|
+
.fun-stories ul,.fun-stories ol{margin:3px 0;padding-inline-start:19px}
|
|
33
|
+
.fun-story-feedback>:empty,.fun-story-result:empty,.fun-story-confirm:empty{display:none}
|
|
34
|
+
.fun-story-confirm,.fun-story-result{margin-top:8px}
|
|
35
|
+
.fun-story-node{padding:5px 0;border-bottom:1px solid var(--fs-line)}
|
|
36
|
+
.fun-story-node:last-child{border-bottom:0}
|
|
37
|
+
.fun-story-node p{white-space:pre-wrap}
|
|
38
|
+
@media(max-width:420px){.fun-stories{padding-inline:0}.fun-story-editor,.fun-story-pack{padding:10px}}
|
|
39
|
+
`;
|
|
40
|
+
|
|
41
|
+
function el(tag, attrs = {}, ...children) {
|
|
42
|
+
const node = document.createElement(tag);
|
|
43
|
+
for (const [key, value] of Object.entries(attrs)) node.setAttribute(key, value);
|
|
44
|
+
for (const child of children.flat()) {
|
|
45
|
+
if (child == null) continue;
|
|
46
|
+
node.appendChild(typeof child === 'string' ? document.createTextNode(child) : child);
|
|
47
|
+
}
|
|
48
|
+
return node;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function messageList(messages) {
|
|
52
|
+
return messages?.length ? el('ul', {}, messages.map((message) => el('li', {}, String(message)))) : null;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function feedback() {
|
|
56
|
+
const status = el('div', { role: 'status' });
|
|
57
|
+
const alert = el('div', { class: 'fun-story-errors', role: 'alert' });
|
|
58
|
+
return {
|
|
59
|
+
node: el('div', { class: 'fun-story-feedback' }, status, alert),
|
|
60
|
+
set(text = '', result) {
|
|
61
|
+
const warnings = messageList(result?.warnings);
|
|
62
|
+
status.replaceChildren(...[text ? document.createTextNode(text) : null, warnings].filter(Boolean));
|
|
63
|
+
const errors = [...new Set([result?.error, ...(result?.errors ?? [])].filter(Boolean))];
|
|
64
|
+
if (result?.ok === false && !errors.length) errors.push('操作失败,请重试。');
|
|
65
|
+
alert.replaceChildren(...(errors.length ? [messageList(errors)] : []));
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function previewGraph(result) {
|
|
71
|
+
const nodes = (result.nodes ?? []).map((node) => {
|
|
72
|
+
const when = node.when && Object.keys(node.when).length
|
|
73
|
+
? `条件:${typeof node.when === 'string' ? node.when : JSON.stringify(node.when)}` : '';
|
|
74
|
+
const choices = (node.choices ?? []).map((choice) => el('li', {}, `${choice.label || choice.id} → ${choice.next || '结束'}`));
|
|
75
|
+
return el('li', { class: 'fun-story-node' },
|
|
76
|
+
el('strong', {}, `${node.id}${node.area ? ` · ${node.area}` : ''}`),
|
|
77
|
+
node.event ? el('p', {}, node.event) : null,
|
|
78
|
+
when ? el('p', { class: 'fun-story-note' }, when) : null,
|
|
79
|
+
node.next ? el('p', { class: 'fun-story-note' }, `下一节点 → ${node.next}`) : null,
|
|
80
|
+
choices.length ? el('ul', {}, choices) : null,
|
|
81
|
+
!node.next && !choices.length ? el('p', { class: 'fun-story-note' }, '结束') : null);
|
|
82
|
+
});
|
|
83
|
+
return el('section', { class: 'fun-story-graph', 'aria-label': '只读节点图' },
|
|
84
|
+
el('h4', {}, result.title || '节点图'),
|
|
85
|
+
el('p', { class: 'fun-story-note' }, result.eligible ? '当前预览条件匹配' : '当前预览条件未匹配'),
|
|
86
|
+
el('ol', {}, nodes));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function label(text, control) {
|
|
90
|
+
return el('label', { class: 'fun-story-field' }, text, control);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function fillSelect(select, items, placeholder) {
|
|
94
|
+
const selected = select.value;
|
|
95
|
+
select.replaceChildren(el('option', { value: '' }, placeholder),
|
|
96
|
+
...items.map(({ value, label }) => el('option', { value }, label)));
|
|
97
|
+
select.value = items.some((item) => item.value === selected) ? selected : '';
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function simpleEditor(button, save) {
|
|
101
|
+
const select = (key) => el('select', { class: `fun-story-author-${key}`, 'data-story-field': key });
|
|
102
|
+
const input = (key, max, type = 'text') => el('input', { class: `fun-story-author-${key}`, type, 'data-story-field': key, ...(max ? { maxlength: String(max) } : {}) });
|
|
103
|
+
const place = select('place');
|
|
104
|
+
const area = select('area');
|
|
105
|
+
const id = input('id', 24);
|
|
106
|
+
id.value = `user-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 7)}`;
|
|
107
|
+
const title = input('title', 80);
|
|
108
|
+
const event = input('event', 120);
|
|
109
|
+
const prompt = input('prompt', 160);
|
|
110
|
+
const from = input('from', null, 'time');
|
|
111
|
+
const to = input('to', null, 'time');
|
|
112
|
+
const branches = ['a', 'b'].map((key, index) => {
|
|
113
|
+
const choice = input(`${key}-label`, 160);
|
|
114
|
+
const result = input(`${key}-result`, 160);
|
|
115
|
+
const event = input(`${key}-event`, 120);
|
|
116
|
+
const area = select(`${key}-area`);
|
|
117
|
+
const node = el('fieldset', { class: 'fun-story-branch' }, el('legend', { class: 'fun-story-field' }, `选项 ${index + 1}`),
|
|
118
|
+
label('选项文字', choice), label('选择结果', result), label('后续事件', event), label('后续区域', area));
|
|
119
|
+
return { key, choice, result, event, area, node };
|
|
120
|
+
});
|
|
121
|
+
let places = [];
|
|
122
|
+
const report = feedback();
|
|
123
|
+
const updateAreas = () => {
|
|
124
|
+
const areas = (places.find((item) => item.id === place.value)?.areas ?? []).map((name) => ({ value: name, label: name }));
|
|
125
|
+
fillSelect(area, areas, '选择起始区域');
|
|
126
|
+
for (const branch of branches) fillSelect(branch.area, areas, '同起始区域');
|
|
127
|
+
};
|
|
128
|
+
place.addEventListener('change', () => {
|
|
129
|
+
area.value = '';
|
|
130
|
+
for (const branch of branches) branch.area.value = '';
|
|
131
|
+
updateAreas();
|
|
132
|
+
});
|
|
133
|
+
const saveButton = button('保存新故事包', () => {
|
|
134
|
+
const fields = [place, area, id, title, event, prompt, ...branches.flatMap((branch) => [branch.choice, branch.result, branch.event])];
|
|
135
|
+
const missing = fields.find((field) => !field.value.trim());
|
|
136
|
+
if (missing) {
|
|
137
|
+
report.set('', { ok: false, error: '请填写地点、起始区域和故事内容。' });
|
|
138
|
+
missing.focus();
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
if (!/^user-[a-z0-9_-]{1,19}$/.test(id.value.trim())) {
|
|
142
|
+
report.set('', { ok: false, error: '标识须以 user- 开头,最多 24 字符,只能含小写字母、数字、短横线或下划线。' });
|
|
143
|
+
id.focus();
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
if (Boolean(from.value) !== Boolean(to.value) || (from.value && from.value === to.value)) {
|
|
147
|
+
report.set('', { ok: false, error: '请填写不同的开始和结束时间,或将两项都留空。' });
|
|
148
|
+
(from.value && !to.value ? to : from).focus();
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
const start = { id: 'start', area: area.value, event: event.value.trim(), prompt: prompt.value.trim(),
|
|
152
|
+
choices: branches.map((branch) => ({ id: branch.key, label: branch.choice.value.trim(), result: branch.result.value.trim(), next: `end-${branch.key}` })) };
|
|
153
|
+
if (from.value) start.when = { from: from.value, to: to.value };
|
|
154
|
+
const nodes = [start, ...branches.map((branch) => ({ id: `end-${branch.key}`, area: branch.area.value || area.value, event: branch.event.value.trim() }))];
|
|
155
|
+
save(report, JSON.stringify({ version: 1, id: id.value.trim(), title: title.value.trim(), placeId: place.value, start: start.id, nodes }));
|
|
156
|
+
}, { class: 'fun-story-action fun-story-primary' });
|
|
157
|
+
const node = el('section', { class: 'fun-story-editor', 'aria-label': '编写故事' },
|
|
158
|
+
el('h3', {}, '编写故事'),
|
|
159
|
+
el('div', { class: 'fun-story-grid' }, label('地点', place), label('起始区域', area), label('故事标题', title), label('故事标识', id)),
|
|
160
|
+
el('div', { class: 'fun-story-grid' }, label('起始事件', event), label('选择提问', prompt)),
|
|
161
|
+
el('div', { class: 'fun-story-grid fun-story-branches' }, branches.map((branch) => branch.node)),
|
|
162
|
+
el('details', {}, el('summary', {}, '触发时段(可选)'),
|
|
163
|
+
el('div', { class: 'fun-story-grid' }, label('开始时间', from), label('结束时间', to))),
|
|
164
|
+
el('div', { class: 'fun-story-row' }, saveButton), report.node);
|
|
165
|
+
function update(items) {
|
|
166
|
+
places = items;
|
|
167
|
+
fillSelect(place, items.map((item) => ({ value: item.id, label: item.name || item.id })), '选择地点');
|
|
168
|
+
updateAreas();
|
|
169
|
+
}
|
|
170
|
+
update([]);
|
|
171
|
+
return { node, update };
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export function mountStories(ctx) {
|
|
175
|
+
const { root } = ctx;
|
|
176
|
+
const box = el('div', { class: 'fun-stories' });
|
|
177
|
+
root.replaceChildren(el('style', {}, CSS), box);
|
|
178
|
+
let busy = false;
|
|
179
|
+
let listRequest = 0;
|
|
180
|
+
let lastPacks;
|
|
181
|
+
let draftEdits = 0;
|
|
182
|
+
|
|
183
|
+
function button(text, handler, attrs = {}) {
|
|
184
|
+
const node = el('button', { type: 'button', ...attrs }, text);
|
|
185
|
+
node.addEventListener('click', () => {
|
|
186
|
+
if (node.getAttribute('aria-disabled') !== 'true' && !ctx.signal?.aborted) handler();
|
|
187
|
+
});
|
|
188
|
+
return node;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const errorResult = (error) => ({ ok: false, error: error?.message ?? String(error) });
|
|
192
|
+
function setBusy(value) {
|
|
193
|
+
busy = value;
|
|
194
|
+
for (const node of box.querySelectorAll('.fun-story-action')) node.setAttribute('aria-disabled', String(value));
|
|
195
|
+
}
|
|
196
|
+
async function run(report, pending, method, args, render) {
|
|
197
|
+
if (busy) return;
|
|
198
|
+
setBusy(true);
|
|
199
|
+
report.set(pending);
|
|
200
|
+
try {
|
|
201
|
+
const result = await ctx.invoke(method, [args]);
|
|
202
|
+
if (!ctx.signal?.aborted) await render(result ?? { ok: false, error: '返回结果为空。' });
|
|
203
|
+
} catch (error) {
|
|
204
|
+
if (!ctx.signal?.aborted) report.set('', errorResult(error));
|
|
205
|
+
} finally {
|
|
206
|
+
if (!ctx.signal?.aborted) setBusy(false);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const place = el('select', { class: 'fun-story-place' });
|
|
211
|
+
const defaultPlace = () => el('option', { value: '' }, '默认地点');
|
|
212
|
+
place.appendChild(defaultPlace());
|
|
213
|
+
const draft = el('textarea', { class: 'fun-story-json', rows: '8', spellcheck: 'false', placeholder: '粘贴故事包 JSON…' });
|
|
214
|
+
const overwrite = el('input', { type: 'checkbox', class: 'fun-story-overwrite' });
|
|
215
|
+
const editorReport = feedback();
|
|
216
|
+
const editorResult = el('div', { class: 'fun-story-result' });
|
|
217
|
+
// List renders and pending requests never replace the editable control.
|
|
218
|
+
draft.addEventListener('input', () => {
|
|
219
|
+
draftEdits += 1;
|
|
220
|
+
if (!busy) editorReport.set();
|
|
221
|
+
editorResult.replaceChildren();
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
function editAction(text, method, pending) {
|
|
225
|
+
return button(text, () => {
|
|
226
|
+
const json = draft.value;
|
|
227
|
+
const edits = draftEdits;
|
|
228
|
+
const placeId = place.value;
|
|
229
|
+
const args = method === 'template' ? (placeId ? { placeId } : {}) : { json };
|
|
230
|
+
if (method === 'import' && overwrite.checked) args.replace = true;
|
|
231
|
+
editorResult.replaceChildren();
|
|
232
|
+
void run(editorReport, pending, method, args, async (result) => {
|
|
233
|
+
const changed = draftEdits !== edits || draft.value !== json;
|
|
234
|
+
const previous = changed ? '上次提交:' : '';
|
|
235
|
+
if (method === 'template') {
|
|
236
|
+
if (result.ok && !changed) {
|
|
237
|
+
if (typeof result.json !== 'string') throw new Error('模板没有返回 JSON。');
|
|
238
|
+
draft.value = result.json;
|
|
239
|
+
overwrite.checked = false;
|
|
240
|
+
}
|
|
241
|
+
editorReport.set(result.ok ? (changed ? '草稿已修改,保留当前内容。' : '模板已填入。') : '', result);
|
|
242
|
+
} else if (method === 'validate') {
|
|
243
|
+
editorReport.set(previous + (result.ok ? '校验通过。' : '校验未通过。'), result);
|
|
244
|
+
} else if (method === 'preview') {
|
|
245
|
+
editorReport.set(previous + (result.ok ? '只读预览已生成。' : '预览失败。'), result);
|
|
246
|
+
if (result.ok) editorResult.appendChild(previewGraph(result));
|
|
247
|
+
} else {
|
|
248
|
+
editorReport.set(previous + (result.ok ? `已导入${result.id ? ` · ${result.id}` : ''}。` : '导入失败。'), result);
|
|
249
|
+
if (result.ok) {
|
|
250
|
+
overwrite.checked = false;
|
|
251
|
+
await load();
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
}, { class: `fun-story-action${method === 'import' ? ' fun-story-primary' : ''}` });
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const loadReport = feedback();
|
|
259
|
+
const libraryReport = feedback();
|
|
260
|
+
const packList = el('div', { class: 'fun-story-list' });
|
|
261
|
+
const refreshButton = button('刷新列表', () => { void load(); });
|
|
262
|
+
const simple = simpleEditor(button, (report, json) => {
|
|
263
|
+
void run(report, '正在保存新故事包…', 'import', { json }, async (result) => {
|
|
264
|
+
report.set(result.ok ? `已保存新故事包${result.id ? ` · ${result.id}` : ''}。` : '保存失败。', result);
|
|
265
|
+
if (result.ok) await load();
|
|
266
|
+
});
|
|
267
|
+
});
|
|
268
|
+
box.replaceChildren(
|
|
269
|
+
el('header', { class: 'fun-story-heading' }, el('h2', {}, '故事包'),
|
|
270
|
+
el('p', { class: 'fun-story-note' }, '为地点编写事件节点与分支。草稿仅在导入时保存。')),
|
|
271
|
+
simple.node,
|
|
272
|
+
el('details', { class: 'fun-story-advanced' },
|
|
273
|
+
el('summary', {}, '高级编辑 · 查看或粘贴完整 JSON'),
|
|
274
|
+
el('section', { class: 'fun-story-editor', 'aria-label': '高级 JSON 编辑器' },
|
|
275
|
+
el('h3', {}, '完整 JSON'),
|
|
276
|
+
el('div', { class: 'fun-story-row' }, el('label', { class: 'fun-story-field' }, '模板地点', place),
|
|
277
|
+
editAction('填入模板', 'template', '正在取模板…')),
|
|
278
|
+
el('label', { class: 'fun-story-field' }, '故事包 JSON', draft),
|
|
279
|
+
el('div', { class: 'fun-story-row' }, editAction('校验 JSON', 'validate', '正在校验…'),
|
|
280
|
+
editAction('预览草稿', 'preview', '正在预览…'), editAction('导入故事包', 'import', '正在导入…'),
|
|
281
|
+
el('label', { class: 'fun-story-field' }, overwrite, '覆盖同 ID 故事包')),
|
|
282
|
+
editorReport.node, editorResult)),
|
|
283
|
+
el('section', { 'aria-label': '故事包列表' },
|
|
284
|
+
el('div', { class: 'fun-story-row' }, el('h3', {}, '已收录故事包'), refreshButton),
|
|
285
|
+
loadReport.node, libraryReport.node, packList));
|
|
286
|
+
|
|
287
|
+
function packCard(pack) {
|
|
288
|
+
const title = pack.title || pack.id;
|
|
289
|
+
const report = feedback();
|
|
290
|
+
const resultBox = el('div', { class: 'fun-story-result' });
|
|
291
|
+
const confirm = el('div', { class: 'fun-story-confirm' });
|
|
292
|
+
const packAction = (text, key, handler, css = '') => button(text, handler, {
|
|
293
|
+
class: `fun-story-action ${css}`, 'aria-disabled': String(busy),
|
|
294
|
+
'aria-label': `${text} ${title}`, 'data-story-focus': `${pack.id}:${key}`,
|
|
295
|
+
});
|
|
296
|
+
const read = (method, label) => {
|
|
297
|
+
const opener = packAction(label, method, () => {
|
|
298
|
+
confirm.replaceChildren();
|
|
299
|
+
resultBox.replaceChildren();
|
|
300
|
+
void run(report, method === 'preview' ? '正在预览…' : '正在导出…', method, { id: pack.id }, (result) => {
|
|
301
|
+
report.set(result.ok ? (method === 'preview' ? '只读预览已生成。' : '已导出,可复制 JSON。') : '', result);
|
|
302
|
+
if (!result.ok) return;
|
|
303
|
+
if (method === 'preview') {
|
|
304
|
+
resultBox.appendChild(previewGraph(result));
|
|
305
|
+
} else {
|
|
306
|
+
if (typeof result.json !== 'string') throw new Error('导出没有返回 JSON。');
|
|
307
|
+
const output = el('textarea', { class: 'fun-story-export', rows: '6', readonly: '', spellcheck: 'false' });
|
|
308
|
+
output.value = result.json;
|
|
309
|
+
resultBox.appendChild(el('label', { class: 'fun-story-field' }, `导出的 JSON · ${title}`, output));
|
|
310
|
+
}
|
|
311
|
+
resultBox.appendChild(button('收起', () => {
|
|
312
|
+
resultBox.replaceChildren();
|
|
313
|
+
report.set();
|
|
314
|
+
opener.focus();
|
|
315
|
+
}));
|
|
316
|
+
});
|
|
317
|
+
});
|
|
318
|
+
return opener;
|
|
319
|
+
};
|
|
320
|
+
const mutate = (method, label) => {
|
|
321
|
+
void run(report, `正在${label}…`, method, { id: pack.id }, async (result) => {
|
|
322
|
+
libraryReport.set();
|
|
323
|
+
report.set('', result);
|
|
324
|
+
if (!result.ok) return;
|
|
325
|
+
libraryReport.set(`已${label}「${title}」。`, result);
|
|
326
|
+
await load();
|
|
327
|
+
confirm.replaceChildren();
|
|
328
|
+
});
|
|
329
|
+
};
|
|
330
|
+
const toggle = pack.enabled ? ['disable', '停用'] : ['enable', '启用'];
|
|
331
|
+
const remove = pack.builtin ? null : packAction('移除', 'remove', () => {
|
|
332
|
+
const cancel = button('取消', () => { confirm.replaceChildren(); remove.focus(); });
|
|
333
|
+
confirm.replaceChildren(el('span', {}, `移除「${title}」?`),
|
|
334
|
+
el('div', { class: 'fun-story-row' }, cancel, packAction('确认移除', 'confirm', () => mutate('remove', '移除'), 'fun-story-danger')));
|
|
335
|
+
cancel.focus();
|
|
336
|
+
}, 'fun-story-danger');
|
|
337
|
+
const warnings = pack.warnings?.length ? pack.warnings : pack.available === false ? ['绑定地点或区域当前不匹配。'] : [];
|
|
338
|
+
return el('article', { class: 'fun-story-pack', 'data-pack-id': pack.id },
|
|
339
|
+
el('div', { class: 'fun-story-row' }, el('h4', {}, title),
|
|
340
|
+
el('span', { class: 'fun-story-badge' }, pack.builtin ? '内置' : '自定义'),
|
|
341
|
+
el('span', { class: 'fun-story-badge' }, pack.enabled ? '已启用' : '已停用'),
|
|
342
|
+
pack.available === false ? el('span', { class: 'fun-story-badge fun-story-unavailable' }, '暂不可用') : null),
|
|
343
|
+
el('p', { class: 'fun-story-note' }, `${pack.placeName || pack.placeId || '未绑定地点'} · ${pack.nodeCount} 个节点 · 版本 ${pack.revision}`),
|
|
344
|
+
pack.description ? el('p', {}, pack.description) : null,
|
|
345
|
+
warnings.length ? el('div', { class: 'fun-story-note' }, messageList(warnings)) : null,
|
|
346
|
+
el('div', { class: 'fun-story-row' }, read('preview', '预览'), read('export', '导出 JSON'),
|
|
347
|
+
packAction(toggle[1], 'toggle', () => mutate(...toggle)), remove), confirm, report.node, resultBox);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
function renderList(data) {
|
|
351
|
+
const selected = place.value;
|
|
352
|
+
place.replaceChildren(defaultPlace(), ...data.places.map((item) => el('option', { value: item.id }, item.name || item.id)));
|
|
353
|
+
place.value = data.places.some((item) => item.id === selected) ? selected : '';
|
|
354
|
+
simple.update(data.places);
|
|
355
|
+
const signature = JSON.stringify(data.packs);
|
|
356
|
+
if (signature === lastPacks) return;
|
|
357
|
+
const focused = root.activeElement ?? document.activeElement;
|
|
358
|
+
const buttonWasFocused = [...packList.querySelectorAll('button')].includes(focused);
|
|
359
|
+
const focusKey = focused?.getAttribute?.('data-story-focus');
|
|
360
|
+
const cards = data.packs.map(packCard);
|
|
361
|
+
packList.replaceChildren(...(cards.length ? cards : [el('p', { class: 'fun-story-note' }, '还没有故事包。')]));
|
|
362
|
+
lastPacks = signature;
|
|
363
|
+
if (buttonWasFocused) {
|
|
364
|
+
const replacement = [...packList.querySelectorAll('button')].find((node) => focusKey && node.getAttribute('data-story-focus') === focusKey);
|
|
365
|
+
(replacement || refreshButton).focus();
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
async function load() {
|
|
370
|
+
if (ctx.signal?.aborted) return;
|
|
371
|
+
const sequence = ++listRequest;
|
|
372
|
+
refreshButton.setAttribute('aria-disabled', 'true');
|
|
373
|
+
loadReport.set('正在读取故事包…');
|
|
374
|
+
try {
|
|
375
|
+
const data = await ctx.invoke('list', []);
|
|
376
|
+
if (sequence !== listRequest || ctx.signal?.aborted) return;
|
|
377
|
+
if (!data?.ok) {
|
|
378
|
+
loadReport.set('列表加载失败。', data ?? { ok: false });
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
renderList(data);
|
|
382
|
+
loadReport.set('', data);
|
|
383
|
+
} catch (error) {
|
|
384
|
+
if (sequence === listRequest && !ctx.signal?.aborted) loadReport.set('列表加载失败。', errorResult(error));
|
|
385
|
+
} finally {
|
|
386
|
+
if (sequence === listRequest && !ctx.signal?.aborted) {
|
|
387
|
+
refreshButton.setAttribute('aria-disabled', 'false');
|
|
388
|
+
refreshButton.textContent = loadReport.node.querySelector('.fun-story-errors').textContent ? '重试' : '刷新列表';
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
void load();
|
|
394
|
+
return { refresh: load };
|
|
395
|
+
}
|