@joplo-app/core 0.1.2 → 0.2.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/dist/compile.js CHANGED
@@ -8,11 +8,11 @@
8
8
  */
9
9
  import { isBind, isNode, bindPath, toCondition } from '@joplo-app/kit/node';
10
10
  import { BUILTIN_THEMES, COMPONENT_TYPES, DIRECTOR_LAYER, INTERACTIVE_TYPES, PAGE_LAYER, matchChoice, nodeLabel } from "./catalog.js";
11
- import { choicesOf } from "./player.js";
11
+ import { choicesOf, commonPrefix } from "./player.js";
12
12
  import { Project, SCHEMA_VERSION } from "./schema.js";
13
13
  const KNOWN = new Set(COMPONENT_TYPES);
14
14
  const OVERLAY = 'overlay';
15
- const MOCK_LABEL = { success: '模拟成功', failure: '模拟失败', denied: '模拟无权限', other: '模拟' };
15
+ const MOCK_LABEL = { success: 'Mock success', failure: 'Mock failure', denied: 'Mock permission denied', other: 'Mock' };
16
16
  export function compile(src, mode) {
17
17
  const diags = [];
18
18
  const rel = (s) => (s ? relSource(src.root, s) : undefined);
@@ -45,10 +45,10 @@ export function compile(src, mode) {
45
45
  if (o)
46
46
  picked.set(fid, o);
47
47
  else
48
- err('UNKNOWN_PICK', `功能 ${fid} 的 pick「${pick}」不是 options/ 下的方案`, f.def.source);
48
+ err('UNKNOWN_PICK', `Feature ${fid} pick "${pick}" is not an option under options/`, f.def.source);
49
49
  }
50
50
  else if (mode === 'publish')
51
- warn('NOT_PICKED', `功能「${f.def.name}」还没有选定方案,不会发布`, f.def.source);
51
+ warn('NOT_PICKED', `Feature "${f.def.name}" has no picked option and will not be published`, f.def.source);
52
52
  }
53
53
  const included = mode === 'dev' ? allOptions : [...picked.values()];
54
54
  const baseRoutes = new Map();
@@ -58,9 +58,9 @@ export function compile(src, mode) {
58
58
  for (const o of allOptions) {
59
59
  for (const [path, p] of Object.entries(o.def.routes ?? {})) {
60
60
  if (!path.startsWith('/'))
61
- err('BAD_ROUTE', `路径必须以 / 开头:${path}`, o.def.source);
61
+ err('BAD_ROUTE', `Route must start with /: ${path}`, o.def.source);
62
62
  if (baseRoutes.has(path))
63
- err('ROUTE_EXISTS', `方案 ${o.qid} 新增的路径 ${path} 已存在于 routes/,请改为堆叠(on)`, p.source ?? o.def.source);
63
+ err('ROUTE_EXISTS', `Option ${o.qid} adds route ${path} which already exists in routes/; use on to add elements instead`, p.source ?? o.def.source);
64
64
  addedRoutes.set(`${o.qid}|${path}`, { path, page: { ...p, file: o.def.file }, addedBy: o });
65
65
  }
66
66
  }
@@ -80,7 +80,7 @@ export function compile(src, mode) {
80
80
  for (const r of [...baseRoutes.values(), ...[...addedRoutes.values()].filter((r) => included.some((o) => o.qid === r.addedBy.qid))]) {
81
81
  const lid = layoutOf(r.page);
82
82
  if (lid && !src.layouts[lid])
83
- err('UNKNOWN_LAYOUT', `页面 ${r.path} 使用了不存在的布局「${lid}」`, r.page.source);
83
+ err('UNKNOWN_LAYOUT', `Page ${r.path} uses an unknown layout "${lid}"`, r.page.source);
84
84
  if (out.routes[r.path])
85
85
  continue;
86
86
  const slots = new Set();
@@ -106,7 +106,7 @@ export function compile(src, mode) {
106
106
  id: fid,
107
107
  name: fdef.name,
108
108
  goal: fdef.goal,
109
- roles: fdef.roles ?? [],
109
+ roles: rolesOf(fdef.roles),
110
110
  constraints: Object.entries(fdef.constraints ?? {}).map(([id, c]) => (typeof c === 'string' ? { id, text: c, hard: true } : { id, text: c.text, hard: c.hard ?? true })),
111
111
  caseIds: mode === 'publish' ? [] : caseIds,
112
112
  optionIds: included.filter((o) => o.fid === fid).map((o) => o.qid),
@@ -158,7 +158,7 @@ export function compile(src, mode) {
158
158
  if (!used.has(slot)) {
159
159
  const from = contribs.get(slot)[0].from;
160
160
  const o = allOptions.find((x) => x.qid === from);
161
- err('UNKNOWN_SLOT', `方案 ${from} 往路径 ${route.path} 的插槽「${slot}」堆叠元素,但该页面和布局中没有 <Slot name="${slot}" />`, o?.def.source, {
161
+ err('UNKNOWN_SLOT', `Option ${from} adds elements to route ${route.path} slot "${slot}", but the page and layout have no <Slot name="${slot}" />`, o?.def.source, {
162
162
  kind: 'option',
163
163
  id: from,
164
164
  });
@@ -175,7 +175,7 @@ export function compile(src, mode) {
175
175
  const t = src.config.theme ?? 'sketch';
176
176
  const authorThemes = Object.keys(src.themes ?? {});
177
177
  if (!BUILTIN_THEMES.includes(t) && !authorThemes.includes(t))
178
- err('THEME_UNKNOWN', `config({ theme: '${t}' }) 不是内置主题(${BUILTIN_THEMES.join('、')}),项目里也没有 themes/${t}.css${authorThemes.length ? `(作者主题:${authorThemes.join('、')})` : ''}`, src.config.source ?? `${src.config.file}:1:1`);
178
+ err('THEME_UNKNOWN', `config({ theme: '${t}' }) is not a built-in theme (${BUILTIN_THEMES.join(', ')}), and the project has no themes/${t}.css${authorThemes.length ? ` (author themes: ${authorThemes.join(', ')})` : ''}`, src.config.source ?? `${src.config.file}:1:1`);
179
179
  }
180
180
  const sceneFor = new Map();
181
181
  const addScene = (s) => {
@@ -196,7 +196,7 @@ export function compile(src, mode) {
196
196
  const touched = [...Object.keys(def.on ?? {}), ...Object.keys(def.routes ?? {})];
197
197
  for (const p of Object.keys(def.on ?? {}))
198
198
  if (!ctxRoutes.has(p))
199
- err('UNKNOWN_ROUTE', `方案 ${qid} 堆叠到不存在的路径 ${p}`, def.source);
199
+ err('UNKNOWN_ROUTE', `Option ${qid} adds elements to an unknown route ${p}`, def.source);
200
200
  const sceneOf = (path) => (mode === 'dev' ? `${qid}@${path}` : `@${path}`);
201
201
  if (mode === 'dev') {
202
202
  for (const [k, r] of Object.entries(def.rules ?? {}))
@@ -209,9 +209,11 @@ export function compile(src, mode) {
209
209
  return `${qid}.${key}`;
210
210
  if (fdef.rules?.[key])
211
211
  return `${fid}.${key}`;
212
- err('UNKNOWN_RULE', `uses("${key}") 引用了不存在的规则(应为 option.rules 或 feature.rules 的键)`, at);
212
+ err('UNKNOWN_RULE', `uses("${key}") references an unknown rule (use a key from option.rules or feature.rules)`, at);
213
213
  return null;
214
214
  };
215
+ /** 可被引用的角色键(D56):feature.roles 写成 { 键: role('名字') } 时才有。 */
216
+ const roleKeys = new Set(fdef.roles && !Array.isArray(fdef.roles) ? Object.keys(fdef.roles) : []);
215
217
  // ---------- 层级(T-layers):每层一个完全独立的画面,单独编译、单独渲染 ----------
216
218
  const layers = {};
217
219
  const layerScenes = [];
@@ -219,13 +221,13 @@ export function compile(src, mode) {
219
221
  for (const [lk, ld] of Object.entries(def.layers ?? {})) {
220
222
  const at = ld.source ?? def.source;
221
223
  if (!/^[A-Za-z_][\w-]*$/.test(lk)) {
222
- err('LAYER_UNKNOWN', `层级键「${lk}」只能用字母、数字、下划线和连字符,且不能以 @ 开头`, at);
224
+ err('LAYER_UNKNOWN', `Layer key "${lk}" must use letters, digits, underscores, and hyphens and must not start with @`, at);
223
225
  continue;
224
226
  }
225
227
  const tree = flatten(Array.isArray(ld.tree) ? ld.tree : [ld.tree]);
226
228
  const nodes = tree.filter(isNode);
227
229
  if (!nodes.length) {
228
- err('LAYER_UNKNOWN', `层级「${ld.title}」没有内容:layer(title, <页面 JSX />)`, at);
230
+ err('LAYER_UNKNOWN', `Layer "${ld.title}" has no content: layer(title, <Page />)`, at);
229
231
  continue;
230
232
  }
231
233
  const rootJ = nodes.length === 1 ? nodes[0] : { ...nodes[0], type: 'div', props: {}, children: nodes };
@@ -235,7 +237,9 @@ export function compile(src, mode) {
235
237
  const scene = { id: sceneId, optionId: qid, route: layerRoute, name: `${ld.title} · ${def.name}`, viewport: ld.viewport ?? 'desktop', root, layer: lk };
236
238
  addScene(scene);
237
239
  layerScenes.push({ key: lk, scene });
238
- layers[lk] = { title: ld.title, sceneId };
240
+ if (ld.role !== undefined && !roleKeys.has(ld.role))
241
+ err('ROLE_UNKNOWN', `Layer "${ld.title}" role "${ld.role}" is not in feature.roles${roleKeys.size ? ` (available: ${[...roleKeys].join(', ')})` : ' (feature.roles must use { key: role(\'Name\') })'}`, at);
242
+ layers[lk] = { title: ld.title, sceneId, ...(ld.role !== undefined && roleKeys.has(ld.role) ? { role: ld.role } : {}) };
239
243
  }
240
244
  const hasLayers = layerScenes.length > 0;
241
245
  /** 元素在哪些层级里(多层都有同名 id 时都算)。 */
@@ -246,14 +250,14 @@ export function compile(src, mode) {
246
250
  const resolveTrace = (ids, tmode, at) => {
247
251
  const shown = `trace(${ids.map((x) => JSON.stringify(x)).join(', ')})`;
248
252
  if (!ids.length) {
249
- err('TRACE_UNKNOWN', `${shown} 至少要写一个节点或连线 id`, at);
253
+ err('TRACE_UNKNOWN', `${shown} requires at least one node or edge id`, at);
250
254
  return null;
251
255
  }
252
256
  const has = (d, t) => d.nodes.has(t) || d.edges.some((e) => e.id === t);
253
257
  const d = diagrams.find((x) => ids.every((t) => has(x, t)));
254
258
  if (!d) {
255
259
  const missing = ids.find((t) => !diagrams.some((x) => has(x, t)));
256
- err('TRACE_UNKNOWN', missing ? `${shown} 里的「${missing}」不是任何 Diagram 的节点或连线 id` : `${shown} 的节点不在同一个 Diagram 里`, at);
260
+ err('TRACE_UNKNOWN', missing ? `${shown} contains "${missing}", which is not a node or edge id in any Diagram` : `${shown} has nodes from different Diagrams`, at);
257
261
  return null;
258
262
  }
259
263
  const nodes = [];
@@ -279,7 +283,7 @@ export function compile(src, mode) {
279
283
  // 写连线 id:方向由上一个节点决定(从 to 端接上时逆向走)
280
284
  const reverse = prev !== null && prev === edge.to && prev !== edge.from;
281
285
  if (prev !== null && prev !== edge.from && prev !== edge.to) {
282
- err('TRACE_UNKNOWN', `${shown}:连线「${t}」(${edge.from} → ${edge.to})没有连着上一个节点「${prev}」`, at);
286
+ err('TRACE_UNKNOWN', `${shown}: edge "${t}"(${edge.from} → ${edge.to}) is not connected to the previous node "${prev}"`, at);
283
287
  return null;
284
288
  }
285
289
  edges.push({ id: edge.id, reverse });
@@ -294,11 +298,11 @@ export function compile(src, mode) {
294
298
  const back = d.edges.filter((e) => e.from === t && e.to === prev);
295
299
  const pick = fwd.length ? fwd : back;
296
300
  if (pick.length > 1) {
297
- err('TRACE_AMBIGUOUS', `${shown}:「${prev}」与「${t}」之间有 ${pick.length} 条连线(${pick.map((e) => e.id).join('、')}),请改写成连线 id,例如 trace('${pick[0].id}')`, at);
301
+ err('TRACE_AMBIGUOUS', `${shown}: "${prev}" and "${t}" have ${pick.length} edges (${pick.map((e) => e.id).join(', ')}), use an edge id instead, for example trace('${pick[0].id}')`, at);
298
302
  return null;
299
303
  }
300
304
  if (!pick.length) {
301
- err('TRACE_UNKNOWN', `${shown}:「${prev}」与「${t}」之间没有连线(这一段要能在同一个 Diagram 里找到对应的 Diagram.Edge,方向不限)`, at);
305
+ err('TRACE_UNKNOWN', `${shown}: "${prev}" and "${t}" have no connecting edge (each segment needs a Diagram.Edge in the same Diagram; either direction is allowed)`, at);
302
306
  return null;
303
307
  }
304
308
  edges.push({ id: pick[0].id, reverse: !fwd.length });
@@ -313,12 +317,12 @@ export function compile(src, mode) {
313
317
  const sid = `${qid}.${skey}`;
314
318
  const start = fl.opts.start ?? touched[0] ?? src.config.home ?? [...ctxRoutes][0];
315
319
  if (!start || !ctxRoutes.has(start)) {
316
- err('UNKNOWN_ROUTE', `场景 ${sid} 的起始路径 ${start ?? '(无)'} 不存在`, fl.source);
320
+ err('UNKNOWN_ROUTE', `Scenario ${sid} starts at unknown route ${start ?? '(none)'}`, fl.source);
317
321
  continue;
318
322
  }
319
323
  for (const c of fl.opts.cases ?? []) {
320
324
  if (!fdef.cases?.[c])
321
- err('UNKNOWN_CASE', `场景 ${sid} 覆盖的情况「${c}」不在 feature.cases 中`, fl.source);
325
+ err('UNKNOWN_CASE', `Scenario ${sid} covers case "${c}", which is not in feature.cases`, fl.source);
322
326
  else
323
327
  (scenarioIds[c] ??= []).push(sid);
324
328
  }
@@ -327,12 +331,14 @@ export function compile(src, mode) {
327
331
  const ruleIds = [];
328
332
  const explains = [];
329
333
  const traces = [];
334
+ const forks = [];
335
+ const marks = [];
330
336
  /** 每个 trace 效果解析出的节点(作为镜头目标,与 shot 合并)。 */
331
337
  const traceTargets = new Map();
332
338
  for (const e of st.effects) {
333
339
  if (e.type === 'go') {
334
340
  if (!ctxRoutes.has(e.route))
335
- err('UNKNOWN_ROUTE', `go("${e.route}") 跳转到不存在的路径`, st.source);
341
+ err('UNKNOWN_ROUTE', `go("${e.route}") navigates to an unknown route`, st.source);
336
342
  effects.push({ type: 'navigate', sceneId: sceneOf(e.route) });
337
343
  }
338
344
  else if (e.type === 'uses') {
@@ -344,9 +350,11 @@ export function compile(src, mode) {
344
350
  }
345
351
  else if (e.type === 'set' || e.type === 'append')
346
352
  effects.push({ type: e.type, path: e.path, value: e.value });
353
+ else if (e.type === 'stream')
354
+ effects.push({ type: 'stream', path: e.path, value: e.value });
347
355
  else if (e.type === 'toast')
348
356
  effects.push({ type: 'toast', text: e.text, tone: e.tone });
349
- else if (e.type === 'shot')
357
+ else if (e.type === 'shot' || e.type === 'mark' || e.type === 'fork')
350
358
  continue; // 镜头不是状态变化,下面单独归类
351
359
  else if (e.type === 'explain') {
352
360
  if (e.text.trim())
@@ -367,6 +375,21 @@ export function compile(src, mode) {
367
375
  const sceneBefore = sceneFor.get(sceneOf(stepRouteBefore(fl.steps, i, start)));
368
376
  const sceneAfter = sceneFor.get(sceneOf(stepRouteBefore(fl.steps, i + 1, start)));
369
377
  const actionTarget = 'target' in st.action ? st.action.target : null;
378
+ for (const e of st.effects) {
379
+ if (e.type === 'fork') {
380
+ if (!Object.hasOwn(def.scenarios, e.scenario) || e.scenario === skey)
381
+ err('FORK_UNKNOWN', `Fork target "${e.scenario}" must be another scenario key in the same option`, st.source);
382
+ else if (e.label.trim())
383
+ forks.push({ label: e.label.trim(), scenarioId: `${qid}.${e.scenario}`, at: 0 });
384
+ }
385
+ if (e.type === 'mark') {
386
+ const exists = hasLayers ? layersWith(e.target).length > 0 : !!sceneAfter && !!findIn(sceneAfter.root, e.target);
387
+ if (!exists)
388
+ err('MARK_UNKNOWN', `mark("${e.target}") target is not in ${hasLayers ? 'any layer' : 'the stage for this step'}`, st.source);
389
+ else
390
+ marks.push({ target: e.target, kind: e.kindOf, ...(e.note ? { note: e.note } : {}) });
391
+ }
392
+ }
370
393
  // 镜头:每个 id 在包含它的层级里解析;第一个 shot 的第一个元素所在层级就是这一步的导演层。
371
394
  // 没有声明 layers 的方案:在路径页面里聚焦(伪层级 @page)。
372
395
  const perLayer = new Map();
@@ -387,7 +410,7 @@ export function compile(src, mode) {
387
410
  if (!where.length) {
388
411
  if (fromTrace)
389
412
  continue; // trace 的节点已在 Diagram 里校验过;不在这一步的画面里时不移动镜头
390
- err('SHOT_UNKNOWN', hasLayers ? `shot("${id}") 引用的元素不在任何层级里(层级:${Object.keys(layers).join('、')})` : `shot("${id}") 引用的元素不在这一步的画面里(${sceneAfter?.route ?? sceneBefore?.route ?? '?'})`, st.source);
413
+ err('SHOT_UNKNOWN', hasLayers ? `shot("${id}") references an element absent from all layers (layers: ${Object.keys(layers).join(', ')})` : `shot("${id}") references an element absent from this step's stage (${sceneAfter?.route ?? sceneBefore?.route ?? '?'})`, st.source);
391
414
  continue;
392
415
  }
393
416
  for (const lk of where) {
@@ -402,18 +425,30 @@ export function compile(src, mode) {
402
425
  // 没写 shot 的操作步骤:导演切到操作目标所在的层(播放时观众看得到要点的东西)
403
426
  if (hasLayers && !directorLayer && actionTarget)
404
427
  directorLayer = layersWith(actionTarget)[0] ?? null;
428
+ // 换人的过渡卡(D56):卡片后面导演切到这个人的层级(卡片淡出时已经是 TA 的画面)
429
+ if (st.action.type === 'card' && st.action.role !== undefined) {
430
+ const r = st.action.role;
431
+ if (!roleKeys.has(r))
432
+ err('ROLE_UNKNOWN', `card("${st.action.title}") role "${r}" is not in feature.roles${roleKeys.size ? ` (available: ${[...roleKeys].join(', ')})` : ''}`, st.source);
433
+ else if (hasLayers && !directorLayer)
434
+ directorLayer = Object.entries(layers).find(([, l]) => l.role === r)?.[0] ?? null;
435
+ }
405
436
  const shots = [...perLayer].map(([layer, targets]) => ({ layer, targets }));
437
+ const generated = st.caption === undefined ? autoCaption(st, (hasLayers && actionTarget ? layerSceneWith(actionTarget) : undefined) ?? sceneBefore) : null;
406
438
  return {
407
439
  id: `${sid}#${i + 1}`,
408
440
  kind: st.kind,
409
441
  action: st.action,
410
- caption: st.caption ?? autoCaption(st, (hasLayers && actionTarget ? layerSceneWith(actionTarget) : undefined) ?? sceneBefore),
442
+ caption: st.caption ?? generated.caption,
443
+ ...(generated?.captionAuto ? { captionAuto: generated.captionAuto } : {}),
411
444
  effects,
412
445
  ruleIds: mode === 'publish' ? [] : ruleIds,
413
446
  shots,
414
447
  directorLayer,
415
448
  ...(explains.length ? { explain: explains.join('\n') } : {}),
416
449
  traces,
450
+ forks,
451
+ marks,
417
452
  ...(st.source ? { source: rel(st.source) } : {}),
418
453
  };
419
454
  });
@@ -425,7 +460,7 @@ export function compile(src, mode) {
425
460
  name: fl.name,
426
461
  description: fl.opts.description ?? '',
427
462
  caseIds: mode === 'publish' ? [] : (fl.opts.cases ?? []).filter((c) => fdef.cases?.[c]).map((c) => `${fid}.${c}`),
428
- mock: { label: fl.opts.mock ?? MOCK_LABEL[outcome], outcome },
463
+ mock: { label: fl.opts.mock ?? MOCK_LABEL[outcome], outcome, ...(fl.opts.mock === undefined ? { auto: true } : {}) },
429
464
  initial: { sceneId: sceneOf(start), ui: state.ui, data: state.data, inputs: state.inputs },
430
465
  steps,
431
466
  expected: fl.opts.expected ?? '',
@@ -433,6 +468,17 @@ export function compile(src, mode) {
433
468
  };
434
469
  out.scenarios[sid] = sc;
435
470
  }
471
+ // 所有场景完成后再解析分叉(允许向后声明的场景),不把创作信息参与前缀比较。
472
+ for (const sc of Object.values(out.scenarios)) {
473
+ if (sc.optionId !== qid)
474
+ continue;
475
+ for (const st of sc.steps)
476
+ for (const f of st.forks) {
477
+ const target = out.scenarios[f.scenarioId];
478
+ if (target)
479
+ f.at = commonPrefix(sc.steps, target.steps);
480
+ }
481
+ }
436
482
  const coverage = {};
437
483
  if (mode === 'dev') {
438
484
  for (const ck of Object.keys(fdef.cases ?? {})) {
@@ -443,11 +489,11 @@ export function compile(src, mode) {
443
489
  else if (sids.length)
444
490
  coverage[`${fid}.${ck}`] = { status: 'expressed', scenarioIds: sids, basis: 'derived' };
445
491
  else
446
- coverage[`${fid}.${ck}`] = { status: 'missing', scenarioIds: [], reason: '尚无场景,也没有说明', basis: 'derived' };
492
+ coverage[`${fid}.${ck}`] = { status: 'missing', scenarioIds: [], basis: 'derived' };
447
493
  }
448
494
  for (const ck of Object.keys(def.coverage ?? {}))
449
495
  if (!fdef.cases?.[ck])
450
- err('UNKNOWN_CASE', `方案 ${qid} 的 coverage 声明了不存在的情况「${ck}」`, def.source);
496
+ err('UNKNOWN_CASE', `Option ${qid} coverage declares unknown case "${ck}"`, def.source);
451
497
  }
452
498
  out.options[qid] = {
453
499
  id: qid,
@@ -467,10 +513,10 @@ export function compile(src, mode) {
467
513
  if (mode === 'dev') {
468
514
  for (const r of Object.values(out.rules)) {
469
515
  if (r.origin === 'ai_assumption' && r.confirmation === 'confirmed')
470
- err('ASSUMPTION_CONFIRMED', `规则「${r.text}」是 AI 假设,不能标为已确认;请改为 pending,或在用户确认后把 origin 改为 user`, r.source);
516
+ err('ASSUMPTION_CONFIRMED', `Rule "${r.text}" is an AI assumption and cannot be Confirmed; use pending, or set origin to user after user confirmation`, r.source);
471
517
  for (const c of r.caseIds)
472
518
  if (!out.cases[c])
473
- err('UNKNOWN_CASE', `规则「${r.text}」关联了不存在的情况 ${c}`, r.source);
519
+ err('UNKNOWN_CASE', `Rule "${r.text}" links unknown case ${c}`, r.source);
474
520
  }
475
521
  }
476
522
  // ---------- 7. 发布视图:去掉设计备注 ----------
@@ -490,6 +536,16 @@ export function compile(src, mode) {
490
536
  return { project: parsed.data, diagnostics: diags };
491
537
  }
492
538
  // ---------- 帮助函数 ----------
539
+ /** 角色的颜色(D56):没写 color 时按声明顺序取。 */
540
+ export const ROLE_COLORS = ['#E8590C', '#1971C2', '#2F9E44', '#9C36B5', '#C2255C', '#0C8599'];
541
+ function rolesOf(roles) {
542
+ if (!roles)
543
+ return [];
544
+ const color = (i, c) => c ?? ROLE_COLORS[i % ROLE_COLORS.length];
545
+ if (Array.isArray(roles))
546
+ return roles.map((name, i) => ({ name, desc: '', color: color(i) }));
547
+ return Object.entries(roles).map(([id, r], i) => ({ id, name: r.name, desc: r.desc ?? '', color: color(i, r.color) }));
548
+ }
493
549
  function sortedEntries(o) {
494
550
  return Object.entries(o).sort(([a], [b]) => a.localeCompare(b));
495
551
  }
@@ -552,30 +608,30 @@ function toSceneNode(n, fallbackId, ids, err, rel) {
552
608
  const { id: rawId, when, disabled, disabledReason, unmodeled, note, __from, ...rest } = n.props;
553
609
  const at = n.source;
554
610
  if (!KNOWN.has(n.type))
555
- err('UNKNOWN_COMPONENT', `组件 <${n.type}> 不在目录中(请使用 Mantine 同名组件,运行 joplo skill components 查看目录;目录没有的用 <Custom kind="…"> 占位)`, at);
611
+ err('UNKNOWN_COMPONENT', `Component <${n.type}> is not in the catalog (use Mantine component names; run joplo skill components to list them; use <Custom kind="…"> as a placeholder for missing components)`, at);
556
612
  const interactive = INTERACTIVE_TYPES.has(n.type);
557
613
  if (interactive && typeof rawId !== 'string')
558
- err('MISSING_ID', `可操作组件 <${n.type}>「${labelOf(n)}」必须有 id,步骤和选中都靠它定位`, at);
614
+ err('MISSING_ID', `Interactive component <${n.type}> "${labelOf(n)}" requires an id for steps and selection`, at);
559
615
  if (n.type === 'Diagram.Node' && typeof rawId !== 'string')
560
- err('MISSING_ID', `架构图节点 <Diagram.Node>「${labelOf(n)}」必须有 id,连线的 from / to 靠它`, at);
616
+ err('MISSING_ID', `Diagram node <Diagram.Node> "${labelOf(n)}" requires an id for edge from / to`, at);
561
617
  if (n.type === 'Diagram')
562
618
  checkDiagram(n, err);
563
619
  // 连线没写 id 时用「from->to」:⌥+单击检查与 rules 的 elements 都能引用它
564
620
  const edgeId = n.type === 'Diagram.Edge' && typeof rest.from === 'string' && typeof rest.to === 'string' ? `${rest.from}->${rest.to}` : null;
565
621
  const id = typeof rawId === 'string' ? rawId : (edgeId ?? `_${fallbackId}`);
566
622
  if (ids.has(id))
567
- err('DUPLICATE_ID', `同一画面中 id「${id}」重复(另一处:${rel(ids.get(id)) ?? '未知'})`, at);
623
+ err('DUPLICATE_ID', `Duplicate id "${id}" in the same stage (other location: ${rel(ids.get(id)) ?? 'unknown'})`, at);
568
624
  ids.set(id, at);
569
625
  const props = {};
570
626
  for (const [k, v] of Object.entries(rest)) {
571
627
  if (v === undefined)
572
628
  continue; // 写了属性但值是 undefined(如 description={cond ? '…' : undefined})= 没写
573
629
  if (typeof v === 'function' && !isBind(v)) {
574
- err('FUNCTION_PROP', `<${n.type}> 的属性 ${k} 是函数。行为请写在 option 的 flow 步骤里,而不是事件回调`, at);
630
+ err('FUNCTION_PROP', `<${n.type}> prop ${k} is a function. Put behavior in the option's flow steps instead of event callbacks`, at);
575
631
  continue;
576
632
  }
577
633
  if (isNode(v)) {
578
- err('ELEMENT_PROP', `<${n.type}> 的属性 ${k} 是一个元素。属性只支持字符串、数字、布尔和对象;图标请写成字符(例如 leftSection="+"),内容请写成子节点`, at);
634
+ err('ELEMENT_PROP', `<${n.type}> prop ${k} is an element. Props accept only strings, numbers, booleans, and objects; use characters for icons (for example leftSection="+") and children for content`, at);
579
635
  continue;
580
636
  }
581
637
  props[k] = toPropValue(v);
@@ -621,12 +677,12 @@ function checkDiagram(d, err) {
621
677
  }
622
678
  };
623
679
  walk(d.children);
624
- const known = [...nodes].join('、') || '(没有节点)';
680
+ const known = [...nodes].join(', ') || '(no nodes)';
625
681
  for (const e of edges) {
626
682
  for (const k of ['from', 'to']) {
627
683
  const v = e.props[k];
628
684
  if (typeof v !== 'string' || !nodes.has(v))
629
- err('UNKNOWN_DIAGRAM_NODE', `<Diagram.Edge> 的 ${k}「${typeof v === 'string' ? v : String(v)}」不是这个 Diagram 里的节点 id(可用:${known})`, e.source);
685
+ err('UNKNOWN_DIAGRAM_NODE', `<Diagram.Edge> ${k} "${typeof v === 'string' ? v : String(v)}" is not a node id in this Diagram (available: ${known})`, e.source);
630
686
  }
631
687
  }
632
688
  }
@@ -691,21 +747,24 @@ function autoCaption(st, scene) {
691
747
  const a = st.action;
692
748
  const node = 'target' in a && scene ? findIn(scene.root, a.target) : null;
693
749
  const label = node ? nodeLabel(node) : 'target' in a ? a.target : '';
750
+ const result = (kind, caption, extra = {}) => ({ caption, captionAuto: { kind, label, ...extra } });
694
751
  switch (a.type) {
695
- case 'click':
696
- return `点击「${label}」`;
697
- case 'type':
698
- return `在「${label}」中输入“${a.value}”`;
752
+ case 'click': return result('click', `Click “${label}”`);
753
+ case 'type': return result('type', `Type “${a.value}” in “${label}”`, { value: a.value });
699
754
  case 'select': {
700
755
  const choices = node ? choicesOf(node, NO_STATE) : null;
701
- return `在「${label}」中选择“${(choices && matchChoice(choices, a.value)?.label) ?? a.value}”`;
756
+ const value = (choices && matchChoice(choices, a.value)?.label) ?? a.value;
757
+ return result('select', `Select “${value}” in “${label}”`, { value });
758
+ }
759
+ case 'toggle': {
760
+ const kind = node?.type === 'Checkbox' ? (a.value ? 'check' : 'uncheck') : (a.value ? 'switch_on' : 'switch_off');
761
+ const verb = { check: 'Check', uncheck: 'Uncheck', switch_on: 'Turn on', switch_off: 'Turn off' }[kind];
762
+ return result(kind, `${verb} “${label}”`);
702
763
  }
703
- case 'toggle':
704
- return node?.type === 'Checkbox' ? `${a.value ? '勾选' : '取消勾选'}「${label}」` : `${a.value ? '打开' : '关闭'}「${label}」`;
705
- case 'back':
706
- return '返回上一页';
707
- case 'event':
708
- return a.name;
764
+ case 'press': return result('press', a.keys.trim() ? `Press ${a.keys}` : 'Press a key', { keys: a.keys });
765
+ case 'back': return result('back', 'Go back');
766
+ case 'event': return { caption: a.name };
767
+ case 'card': return { caption: a.title };
709
768
  }
710
769
  }
711
770
  function stripSources(o) {
package/dist/guide.d.ts CHANGED
@@ -1,24 +1,24 @@
1
1
  export declare const SKILL_TOPICS: readonly [{
2
2
  readonly name: "authoring";
3
- readonly description: "目录约定;page / layout / option / flow 的写法";
3
+ readonly description: "Directory conventions; page / layout / option / flow syntax";
4
4
  }, {
5
5
  readonly name: "demo";
6
- readonly description: "层级、镜头、调用链与字幕:怎样做一个好演示";
6
+ readonly description: "Layers, shots, traces, and captions for clear demos";
7
7
  }, {
8
8
  readonly name: "components";
9
- readonly description: "当前版本的组件目录与属性";
9
+ readonly description: "Component catalog and props for this version";
10
10
  }, {
11
11
  readonly name: "cases";
12
- readonly description: "情况维度清单:开工前主动向用户提问";
12
+ readonly description: "Case dimensions: ask the user before starting";
13
13
  }, {
14
14
  readonly name: "check";
15
- readonly description: "读懂诊断,按文件和行号局部修改";
15
+ readonly description: "Read diagnostics and fix the indicated file and line";
16
16
  }, {
17
17
  readonly name: "selection";
18
- readonly description: "用户在看板选中元素后,只改那一处";
18
+ readonly description: "Edit only the element selected by the user in the board";
19
19
  }, {
20
20
  readonly name: "publish";
21
- readonly description: "pick、build、静态站部署";
21
+ readonly description: "pick, build, and static site deployment";
22
22
  }];
23
23
  export type SkillTopic = (typeof SKILL_TOPICS)[number]['name'];
24
24
  /** CLI 传入其 package.json 版本;行数来自同一次生成的主题正文。 */