@joplo-app/core 0.5.0 → 0.6.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.
Files changed (65) hide show
  1. package/dist/agent-skill.d.ts +7 -7
  2. package/dist/agent-skill.js +28 -25
  3. package/dist/authoring-works.d.ts +8 -0
  4. package/dist/authoring-works.js +46 -0
  5. package/dist/catalog.d.ts +2 -0
  6. package/dist/compile.d.ts +6 -3
  7. package/dist/compile.js +415 -340
  8. package/dist/content-i18n.js +9 -9
  9. package/dist/embed.d.ts +5 -5
  10. package/dist/embed.js +7 -7
  11. package/dist/entries.d.ts +63 -0
  12. package/dist/entries.js +78 -0
  13. package/dist/feature-diff.d.ts +7 -7
  14. package/dist/feature-diff.js +18 -17
  15. package/dist/features.d.ts +3 -3
  16. package/dist/features.js +3 -3
  17. package/dist/focus-check.js +2 -2
  18. package/dist/focus.d.ts +1 -5
  19. package/dist/focus.js +13 -35
  20. package/dist/guide.js +156 -66
  21. package/dist/index.d.ts +2 -0
  22. package/dist/index.js +2 -0
  23. package/dist/load.js +42 -11
  24. package/dist/local-review-files.js +17 -4
  25. package/dist/local-review.d.ts +8 -7
  26. package/dist/local-review.js +7 -6
  27. package/dist/outline.d.ts +21 -13
  28. package/dist/outline.js +27 -32
  29. package/dist/pace.d.ts +3 -4
  30. package/dist/pace.js +3 -4
  31. package/dist/playback.d.ts +57 -0
  32. package/dist/playback.js +317 -0
  33. package/dist/player.d.ts +9 -195
  34. package/dist/player.js +8 -633
  35. package/dist/playground.d.ts +1 -1
  36. package/dist/playground.js +4 -4
  37. package/dist/plugins/compiler.d.ts +16 -0
  38. package/dist/plugins/compiler.js +48 -9
  39. package/dist/presentation.d.ts +73 -0
  40. package/dist/presentation.js +163 -0
  41. package/dist/publication.d.ts +8 -0
  42. package/dist/publication.js +45 -0
  43. package/dist/result.d.ts +1 -1
  44. package/dist/review.d.ts +18 -10
  45. package/dist/review.js +62 -23
  46. package/dist/runtime.d.ts +2 -0
  47. package/dist/runtime.js +2 -0
  48. package/dist/scene-query.d.ts +77 -0
  49. package/dist/scene-query.js +246 -0
  50. package/dist/schema-issues.js +1 -1
  51. package/dist/schema.d.ts +108 -76
  52. package/dist/schema.js +48 -36
  53. package/dist/scope.d.ts +9 -9
  54. package/dist/scope.js +82 -49
  55. package/dist/selection.d.ts +1 -1
  56. package/dist/sequencing.d.ts +6 -0
  57. package/dist/sequencing.js +21 -0
  58. package/dist/step-plan.d.ts +55 -0
  59. package/dist/step-plan.js +90 -0
  60. package/dist/traits.d.ts +10 -0
  61. package/dist/traits.js +3 -0
  62. package/dist/validate.d.ts +1 -1
  63. package/dist/validate.js +75 -65
  64. package/dist/voice.js +2 -1
  65. package/package.json +15 -3
package/dist/review.js CHANGED
@@ -8,17 +8,19 @@
8
8
  * - 上限:字符串按 Unicode 码点截断(末尾 “…”),数组超出部分丢弃,id 永不截断(Qid ≤ 256、Id ≤ 64 由 schema 保证);
9
9
  * 截断后序列化仍超过 512 KiB 时 assertReviewManifestSize 报错(share 在发任何请求之前失败),不静默丢内容。
10
10
  */
11
+ import { localOnlyWorks, localOnlyElementIds, withoutLocalContributions } from "./publication.js";
11
12
  import { nodeLabel } from "./catalog.js";
12
- import { orderedFeatures } from "./features.js";
13
+ import { featuresOf } from "./features.js";
13
14
  import { JoploError } from "./result.js";
14
- export const REVIEW_MANIFEST_VERSION = 1;
15
+ /** 2(D103):features[].options → works、optionId → workId、scenarios[].featureId 可为 null、新增顶层 works(独立作品)。清单的结构变了就升,云端按版本号严格解析。 */
16
+ export const REVIEW_MANIFEST_VERSION = 2;
15
17
  /** 契约 8.1:评审清单 JSON(UTF-8)最多 512 KiB。 */
16
18
  export const REVIEW_MANIFEST_MAX_BYTES = 512 * 1024;
17
19
  /** 字段上限(客户端截断;SaaS 的 zod 校验至少要接受这些值)。 */
18
20
  export const REVIEW_LIMITS = {
19
21
  features: 100,
20
22
  /** 每个功能的方案数。 */
21
- options: 20,
23
+ works: 20,
22
24
  /** 每个功能的规则数(功能级 + 方案级)。 */
23
25
  rules: 300,
24
26
  questions: 100,
@@ -31,7 +33,7 @@ export const REVIEW_LIMITS = {
31
33
  /** 每一步关联的规则数。 */
32
34
  ruleIds: 50,
33
35
  elements: 2000,
34
- text: { name: 200, goal: 2000, idea: 2000, tradeoff: 500, rule: 2000, question: 2000, caption: 500, explain: 2000, label: 200 },
36
+ text: { name: 200, goal: 2000, summary: 2000, tradeoff: 500, rule: 2000, question: 2000, caption: 500, explain: 2000, label: 200 },
35
37
  };
36
38
  /** 作者写的元素 id(和 Rule.elementIds 同一套);编译器生成的以 `_` 开头,不算。 */
37
39
  export const AUTHOR_ELEMENT_ID = /^[A-Za-z][A-Za-z0-9_-]{0,63}$/;
@@ -43,51 +45,80 @@ export function clipText(text, max) {
43
45
  export function reviewManifest(project, mode) {
44
46
  if (project.mode !== 'dev')
45
47
  throw new JoploError('USAGE', `The review manifest needs the dev IR (rules and questions); got a ${project.mode} IR`);
48
+ const local = localOnlyWorks(project);
49
+ const privateElements = localOnlyElementIds(project, local);
46
50
  const L = REVIEW_LIMITS;
47
51
  const T = L.text;
48
52
  const features = [];
53
+ const works = [];
49
54
  const scenarios = [];
50
55
  /** 参与的方案(publish:每个功能的选定方案)。 */
51
56
  const included = new Set();
52
- for (const f of orderedFeatures(project)) {
53
- const optionIds = (mode === 'publish' ? (f.pick ? [f.pick] : []) : f.optionIds).filter((id) => project.options[id]);
54
- if (mode === 'publish' && !optionIds.length)
57
+ for (const f of featuresOf(project)) {
58
+ if (features.length >= L.features)
59
+ break;
60
+ // 清单读取 dev IR;草稿仍不能随普通分享或评审版上传。
61
+ if (f.workIds.some(id => local.has(id)))
55
62
  continue;
56
- for (const id of optionIds)
63
+ const workIds = (mode === 'publish' ? (f.pick ? [f.pick] : []) : f.workIds).filter((id) => project.works[id]).slice(0, L.works);
64
+ if (mode === 'publish' && !workIds.length)
65
+ continue;
66
+ for (const id of workIds)
57
67
  included.add(id);
58
- const inScope = (optionId) => optionId === undefined || optionIds.includes(optionId);
68
+ const inScope = (workId) => workId === undefined || workIds.includes(workId);
59
69
  features.push({
60
70
  id: f.id,
61
71
  name: clipText(f.name, T.name),
62
72
  goal: clipText(f.goal, T.goal),
63
73
  pick: f.pick ?? null,
64
- options: optionIds.slice(0, L.options).map((id) => {
65
- const o = project.options[id];
66
- return { id: o.id, key: o.key, name: clipText(o.name, T.name), idea: clipText(o.idea, T.idea), tradeoffs: o.tradeoffs.slice(0, L.tradeoffs).map((t) => clipText(t, T.tradeoff)) };
74
+ works: workIds.map((id) => {
75
+ const o = project.works[id];
76
+ return { id: o.id, key: o.key, title: clipText(o.title, T.name), summary: clipText(o.summary, T.summary), tradeoffs: f.alternatives[id].tradeoffs.slice(0, L.tradeoffs).map((t) => clipText(t, T.tradeoff)) };
67
77
  }),
68
78
  rules: Object.values(project.rules)
69
- .filter((r) => r.featureId === f.id && inScope(r.optionId))
79
+ .filter((r) => r.featureId === f.id && inScope(r.workId))
70
80
  .slice(0, L.rules)
71
- .map((r) => ({ id: r.id, optionId: r.optionId ?? null, text: clipText(r.text, T.rule), origin: r.origin, confirmation: r.confirmation, elementIds: r.elementIds.slice(0, L.elementIds) })),
81
+ .map((r) => ({ id: r.id, workId: r.workId ?? null, text: clipText(r.text, T.rule), origin: r.origin, confirmation: r.confirmation, elementIds: r.elementIds.filter(id => !privateElements.has(id)).slice(0, L.elementIds) })),
72
82
  questions: Object.values(project.issues)
73
- .filter((q) => q.featureId === f.id && inScope(q.optionId))
83
+ .filter((q) => q.featureId === f.id && inScope(q.workId))
74
84
  .slice(0, L.questions)
75
- .map((q) => ({ id: q.id, optionId: q.optionId ?? null, text: clipText(q.text, T.question), severity: q.severity })),
85
+ .map((q) => ({ id: q.id, workId: q.workId ?? null, text: clipText(q.text, T.question), severity: q.severity })),
76
86
  });
77
- for (const optionId of optionIds) {
87
+ for (const workId of workIds) {
78
88
  for (const s of Object.values(project.scenarios)) {
79
- if (s.optionId !== optionId)
89
+ if (s.workId !== workId)
80
90
  continue;
81
91
  scenarios.push({
82
92
  id: s.id,
83
93
  featureId: f.id,
84
- optionId: s.optionId,
94
+ workId: s.workId,
85
95
  name: clipText(s.name, T.name),
86
96
  steps: s.steps.slice(0, L.steps).map((st, index) => ({ id: st.id, index, caption: clipText(st.caption, T.caption), explain: st.explain === undefined ? null : clipText(st.explain, T.explain), ruleIds: st.ruleIds.slice(0, L.ruleIds) })),
87
97
  });
88
98
  }
89
99
  }
90
100
  }
101
+ for (const id of project.entryIds) {
102
+ if (works.length >= L.features)
103
+ break;
104
+ const w = project.works[id];
105
+ if (!w || w.featureId || local.has(w.id))
106
+ continue;
107
+ works.push({ id: w.id, key: w.key, title: clipText(w.title, T.name), summary: clipText(w.summary, T.summary) });
108
+ included.add(w.id);
109
+ for (const s of Object.values(project.scenarios)) {
110
+ if (s.workId !== w.id)
111
+ continue;
112
+ scenarios.push({
113
+ id: s.id, featureId: null, workId: w.id, name: clipText(s.name, T.name),
114
+ steps: s.steps.slice(0, L.steps).map((st, index) => ({
115
+ id: st.id, index, caption: clipText(st.caption, T.caption),
116
+ explain: st.explain === undefined ? null : clipText(st.explain, T.explain),
117
+ ruleIds: st.ruleIds.slice(0, L.ruleIds),
118
+ })),
119
+ });
120
+ }
121
+ }
91
122
  // 元素:参与方案能看到的所有画面(发布视图 `@路径`、方案的路径画面与层级画面)里的作者 id;同一个 id 取第一次出现的标签
92
123
  const elements = new Map();
93
124
  const visit = (n) => {
@@ -96,13 +127,21 @@ export function reviewManifest(project, mode) {
96
127
  for (const c of n.children ?? [])
97
128
  visit(c);
98
129
  };
99
- for (const scene of Object.values(project.scenes))
100
- if (scene.optionId === null || included.has(scene.optionId))
101
- visit(scene.root);
130
+ for (const scene of Object.values(project.scenes)) {
131
+ if (scene.workId !== null && !included.has(scene.workId))
132
+ continue;
133
+ const owner = project.routes[scene.route]?.addedBy;
134
+ if (owner && local.has(owner))
135
+ continue;
136
+ const root = withoutLocalContributions(scene.root, local);
137
+ if (root)
138
+ visit(root);
139
+ }
102
140
  return {
103
141
  v: REVIEW_MANIFEST_VERSION,
104
142
  mode,
105
- features: features.slice(0, L.features),
143
+ features,
144
+ works,
106
145
  scenarios: scenarios.slice(0, L.scenarios),
107
146
  elements: [...elements].slice(0, L.elements).map(([id, label]) => ({ id, label })),
108
147
  };
package/dist/runtime.d.ts CHANGED
@@ -10,6 +10,8 @@ export * from './features.js';
10
10
  export * from './review.js';
11
11
  export * from './scope.js';
12
12
  export * from './pace.js';
13
+ export * from './step-plan.js';
14
+ export * from './entries.js';
13
15
  export * from './outline.js';
14
16
  export * from './voice.js';
15
17
  export * from './content-i18n.js';
package/dist/runtime.js CHANGED
@@ -8,6 +8,8 @@ export * from "./features.js";
8
8
  export * from "./review.js";
9
9
  export * from "./scope.js";
10
10
  export * from "./pace.js";
11
+ export * from "./step-plan.js";
12
+ export * from "./entries.js";
11
13
  export * from "./outline.js";
12
14
  export * from "./voice.js";
13
15
  export * from "./content-i18n.js";
@@ -0,0 +1,77 @@
1
+ /**
2
+ * 画面查询(纯函数):在 IR 里找场景、画面与元素,按当前状态取属性的值、判断可见与禁用、推导选项与分栏,
3
+ * 以及方案的层级(每层一个独立画面)。不推进状态——状态转换在 playback.ts,演示投影(镜头、过渡卡、流式输出)在 presentation.ts。
4
+ */
5
+ import { type Choice } from './catalog.js';
6
+ import type { Condition, LayerIcon, Project, Scenario, Scene, SceneNode } from './schema.js';
7
+ export type Toast = {
8
+ id: string;
9
+ text: string;
10
+ tone: 'success' | 'error' | 'info';
11
+ };
12
+ export type PlayState = {
13
+ scenarioId: string;
14
+ /** 已执行的步骤数。 */
15
+ position: number;
16
+ sceneId: string;
17
+ ui: Record<string, unknown>;
18
+ data: Record<string, unknown>;
19
+ inputs: Record<string, unknown>;
20
+ /** 最近一个步骤产生的 Toast;下一步开始时清空,保证“上一步”可复现。 */
21
+ toasts: Toast[];
22
+ completed: null | {
23
+ text: string;
24
+ outcome: 'success' | 'failure' | 'cancelled';
25
+ };
26
+ /** 画面栈,用于 back 动作。 */
27
+ sceneStack: string[];
28
+ };
29
+ export declare function getScenario(project: Project, scenarioId: string): Scenario;
30
+ export declare function getScene(project: Project, sceneId: string): Scene;
31
+ export declare function walkNodes(root: SceneNode, visit: (node: SceneNode, ancestors: SceneNode[]) => void): void;
32
+ /** 返回节点及其祖先链(自根向下,不含自身);找不到返回 null。 */
33
+ export declare function findNode(scene: Scene, nodeId: string): {
34
+ node: SceneNode;
35
+ ancestors: SceneNode[];
36
+ } | null;
37
+ export declare function readPath(state: Pick<PlayState, 'ui' | 'data' | 'inputs'>, path: string): unknown;
38
+ /**
39
+ * 属性值若是 `$ui.x` 形式的整值绑定,则从状态读取;对象与数组逐项解析
40
+ * (例如 declared element 的 navbar={{ collapsed: { mobile: $.ui.navClosed } }});其余原样返回。
41
+ */
42
+ export declare function resolveValue(value: unknown, state: Pick<PlayState, 'ui' | 'data' | 'inputs'>): unknown;
43
+ export declare function resolveProps(node: SceneNode, state: Pick<PlayState, 'ui' | 'data' | 'inputs'>): Record<string, unknown>;
44
+ export declare function evaluateCondition(cond: Condition, state: Pick<PlayState, 'ui' | 'data' | 'inputs'>): boolean;
45
+ /** 画面的舞台宽度(由页面的 viewport 决定)。 */
46
+ export declare function sceneWidth(scene: Pick<Scene, 'viewport'>): number;
47
+ /**
48
+ * 节点自身是否显示:when、declared element 的 opened(兼容旧写法 open),
49
+ * 以及给定舞台宽度时的 visibleFrom / hiddenFrom。
50
+ */
51
+ export declare function isSelfVisible(node: SceneNode, state: PlayState, width?: number): boolean;
52
+ export type LayoutRegion = {
53
+ size: number;
54
+ collapsed: boolean;
55
+ mobile: boolean;
56
+ };
57
+ /** Declarative layout regions share the same width/state interpretation in drawing and playback. */
58
+ export declare function layoutRegions(node: SceneNode, state: Pick<PlayState, 'ui' | 'data' | 'inputs'>, width: number): Record<string, LayoutRegion | null>;
59
+ /** 节点在画面中是否可见:自身与所有祖先都可见;按画面宽度计算断点与 declared element 收起。 */
60
+ export declare function isNodeVisible(scene: Scene, nodeId: string, state: PlayState, width?: number): boolean;
61
+ /** Enumerate declared data, descendant values, or a numeric range. No declaration means unconstrained input. */
62
+ export declare function choicesOf(node: SceneNode, state: Pick<PlayState, 'ui' | 'data' | 'inputs'>): Choice[] | null;
63
+ export declare function isNodeDisabled(node: SceneNode, state: PlayState): boolean;
64
+ /** 方案的层级(源码顺序):每层一个完全独立的画面。没有声明时为空,演示照常显示路径页面。 */
65
+ export declare function layersOf(project: Project, scenarioId: string | null): {
66
+ id: string;
67
+ title: string;
68
+ sceneId: string;
69
+ icon: LayerIcon;
70
+ }[];
71
+ /** 包含某个元素的层级(多层都有同名 id 时都返回)。 */
72
+ export declare function layersWithNode(project: Project, scenarioId: string, nodeId: string): string[];
73
+ /**
74
+ * 步骤目标所在的画面:有层级的方案在各层画面里找(优先当前可见的那一层),否则是状态里的路径画面。
75
+ * 回放校验、select 取值都用它。
76
+ */
77
+ export declare function sceneOfTarget(project: Project, scenario: Scenario, state: PlayState, target: string): Scene | undefined;
@@ -0,0 +1,246 @@
1
+ /**
2
+ * 画面查询(纯函数):在 IR 里找场景、画面与元素,按当前状态取属性的值、判断可见与禁用、推导选项与分栏,
3
+ * 以及方案的层级(每层一个独立画面)。不推进状态——状态转换在 playback.ts,演示投影(镜头、过渡卡、流式输出)在 presentation.ts。
4
+ */
5
+ import { VIEWPORT_SIZE, atWidth, childWidth, breakpointPx, choicesFromData, hiddenAtWidth } from "./catalog.js";
6
+ import { JoploError } from "./result.js";
7
+ // ---------- 查找 ----------
8
+ export function getScenario(project, scenarioId) {
9
+ const sc = project.scenarios[scenarioId];
10
+ if (!sc)
11
+ throw new JoploError('NOT_FOUND', `Scenario does not exist: ${scenarioId}`);
12
+ return sc;
13
+ }
14
+ export function getScene(project, sceneId) {
15
+ const s = project.scenes[sceneId];
16
+ if (!s)
17
+ throw new JoploError('NOT_FOUND', `Stage does not exist: ${sceneId}`);
18
+ return s;
19
+ }
20
+ export function walkNodes(root, visit) {
21
+ const rec = (n, anc) => {
22
+ visit(n, anc);
23
+ const next = [...anc, n];
24
+ for (const c of n.children ?? [])
25
+ rec(c, next);
26
+ };
27
+ rec(root, []);
28
+ }
29
+ /** 返回节点及其祖先链(自根向下,不含自身);找不到返回 null。 */
30
+ export function findNode(scene, nodeId) {
31
+ let found = null;
32
+ walkNodes(scene.root, (node, ancestors) => {
33
+ if (!found && node.id === nodeId)
34
+ found = { node, ancestors };
35
+ });
36
+ return found;
37
+ }
38
+ // ---------- 状态读取与条件 ----------
39
+ export function readPath(state, path) {
40
+ const [root, ...rest] = path.split('.');
41
+ let cur = root === 'ui' ? state.ui : root === 'data' ? state.data : root === 'inputs' ? state.inputs : undefined;
42
+ for (const key of rest) {
43
+ if (cur === null || typeof cur !== 'object')
44
+ return undefined;
45
+ cur = cur[key];
46
+ }
47
+ return cur;
48
+ }
49
+ const BINDING = /^\$((?:ui|data|inputs)(?:\.[A-Za-z0-9_-]+)*)$/;
50
+ /**
51
+ * 属性值若是 `$ui.x` 形式的整值绑定,则从状态读取;对象与数组逐项解析
52
+ * (例如 declared element 的 navbar={{ collapsed: { mobile: $.ui.navClosed } }});其余原样返回。
53
+ */
54
+ export function resolveValue(value, state) {
55
+ if (typeof value === 'string') {
56
+ const m = BINDING.exec(value);
57
+ if (m)
58
+ return readPath(state, m[1]);
59
+ return value;
60
+ }
61
+ if (Array.isArray(value))
62
+ return value.some(hasBinding) ? value.map((v) => resolveValue(v, state)) : value;
63
+ if (value && typeof value === 'object') {
64
+ if (!hasBinding(value))
65
+ return value;
66
+ return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, resolveValue(v, state)]));
67
+ }
68
+ return value;
69
+ }
70
+ function hasBinding(v) {
71
+ if (typeof v === 'string')
72
+ return BINDING.test(v);
73
+ if (Array.isArray(v))
74
+ return v.some(hasBinding);
75
+ if (v && typeof v === 'object')
76
+ return Object.values(v).some(hasBinding);
77
+ return false;
78
+ }
79
+ export function resolveProps(node, state) {
80
+ const out = {};
81
+ for (const [k, v] of Object.entries(node.props ?? {}))
82
+ out[k] = resolveValue(v, state);
83
+ return out;
84
+ }
85
+ const isEmpty = (v) => v === undefined || v === null || v === '' || (Array.isArray(v) && v.length === 0) || (typeof v === 'object' && v !== null && !Array.isArray(v) && Object.keys(v).length === 0);
86
+ export function evaluateCondition(cond, state) {
87
+ if ('truthy' in cond)
88
+ return Boolean(readPath(state, cond.truthy));
89
+ if ('falsy' in cond)
90
+ return !readPath(state, cond.falsy);
91
+ if ('empty' in cond)
92
+ return isEmpty(readPath(state, cond.empty));
93
+ if ('notEmpty' in cond)
94
+ return !isEmpty(readPath(state, cond.notEmpty));
95
+ if ('eq' in cond)
96
+ return readPath(state, cond.eq[0]) === cond.eq[1];
97
+ if ('neq' in cond)
98
+ return readPath(state, cond.neq[0]) !== cond.neq[1];
99
+ if ('in' in cond)
100
+ return cond.in[1].includes(readPath(state, cond.in[0]));
101
+ if ('and' in cond)
102
+ return cond.and.every((c) => evaluateCondition(c, state));
103
+ if ('or' in cond)
104
+ return cond.or.some((c) => evaluateCondition(c, state));
105
+ if ('not' in cond)
106
+ return !evaluateCondition(cond.not, state);
107
+ return false;
108
+ }
109
+ /** 画面的舞台宽度(由页面的 viewport 决定)。 */
110
+ export function sceneWidth(scene) {
111
+ return VIEWPORT_SIZE[scene.viewport === 'mobile' ? 'mobile' : 'desktop'].width;
112
+ }
113
+ /**
114
+ * 节点自身是否显示:when、declared element 的 opened(兼容旧写法 open),
115
+ * 以及给定舞台宽度时的 visibleFrom / hiddenFrom。
116
+ */
117
+ export function isSelfVisible(node, state, width) {
118
+ if (node.when && !evaluateCondition(node.when, state))
119
+ return false;
120
+ if (node.traits?.gate) {
121
+ const opened = node.traits.gate.map(key => node.props?.[key]).find(v => v !== undefined);
122
+ if (!resolveValue(opened, state))
123
+ return false;
124
+ }
125
+ if (width !== undefined && hiddenAtWidth(node.props, width))
126
+ return false;
127
+ return true;
128
+ }
129
+ /** Declarative layout regions share the same width/state interpretation in drawing and playback. */
130
+ export function layoutRegions(node, state, width) {
131
+ const regions = {};
132
+ for (const [key, spec] of Object.entries(node.traits?.layout ?? {})) {
133
+ const value = resolveValue(atWidth(node.props?.[key], width), state);
134
+ if (!value || typeof value !== 'object' || Array.isArray(value)) {
135
+ regions[key] = null;
136
+ continue;
137
+ }
138
+ const props = value;
139
+ const bp = spec.breakpoint ? breakpointPx(props.breakpoint) : null;
140
+ const mobile = bp !== null && width < bp;
141
+ const collapsed = resolveValue(props.collapsed, state);
142
+ const config = collapsed && typeof collapsed === 'object' ? collapsed : {};
143
+ const rawSize = atWidth(resolveValue(props[spec.size], state), width);
144
+ const size = typeof rawSize === 'number' && Number.isFinite(rawSize) ? rawSize : typeof rawSize === 'string' && /^\d+(px)?$/.test(rawSize) ? Number.parseInt(rawSize, 10) : spec.defaultSize;
145
+ regions[key] = { size, mobile, collapsed: spec.breakpoint ? (mobile ? config.mobile === undefined || Boolean(config.mobile) : Boolean(config.desktop)) : Boolean(collapsed) };
146
+ }
147
+ return regions;
148
+ }
149
+ /** 节点在画面中是否可见:自身与所有祖先都可见;按画面宽度计算断点与 declared element 收起。 */
150
+ export function isNodeVisible(scene, nodeId, state, width = sceneWidth(scene)) {
151
+ const hit = findNode(scene, nodeId);
152
+ if (!hit)
153
+ return false;
154
+ let shell = null;
155
+ let shellWidth = width;
156
+ let w = width;
157
+ const chain = [...hit.ancestors, hit.node];
158
+ for (const [index, n] of chain.entries()) {
159
+ if (!isSelfVisible(n, state, w))
160
+ return false;
161
+ const gate = n.traits?.selectionGate;
162
+ if (gate) {
163
+ const ancestors = chain.slice(0, index).reverse();
164
+ const owner = ancestors.find(a => a.type === gate.owner);
165
+ const item = gate.item ? ancestors.find(a => a.type === gate.item) : n;
166
+ if (!owner || !item)
167
+ return false;
168
+ let selected = state.inputs[owner.id] !== undefined ? state.inputs[owner.id] : resolveValue(owner.props?.value ?? owner.props?.defaultValue, state);
169
+ if (gate.fallbackFirst && (selected === undefined || selected === null || selected === ''))
170
+ selected = choicesOf(owner, state)?.[0]?.value;
171
+ const value = String(resolveValue(item.props?.[gate.value], state) ?? '');
172
+ if (Array.isArray(selected) ? !selected.map(String).includes(value) : selected === undefined || selected === null || String(selected) !== value)
173
+ return false;
174
+ }
175
+ if (n.traits?.layout) {
176
+ shell = n;
177
+ shellWidth = w;
178
+ }
179
+ if (shell && n.traits?.region) {
180
+ const r = layoutRegions(shell, state, shellWidth)[n.traits.region];
181
+ if (r?.collapsed)
182
+ return false;
183
+ }
184
+ w = childWidth(n, w, resolveProps(n, state));
185
+ }
186
+ return true;
187
+ }
188
+ /** Enumerate declared data, descendant values, or a numeric range. No declaration means unconstrained input. */
189
+ export function choicesOf(node, state) {
190
+ const spec = node.traits?.choices;
191
+ if (!spec)
192
+ return null;
193
+ const p = node.props ?? {};
194
+ if ('prop' in spec) {
195
+ const choices = choicesFromData(resolveValue(p[spec.prop], state));
196
+ return choices.length ? choices : null;
197
+ }
198
+ if ('range' in spec) {
199
+ const total = Number(resolveValue(p[spec.range], state));
200
+ return Number.isInteger(total) && total > 0 ? Array.from({ length: total }, (_, i) => ({ value: String(i + 1), label: String(i + 1) })) : null;
201
+ }
202
+ const text = (n) => n.type === '#text' ? String(resolveValue(n.props?.text, state) ?? '') : (n.children ?? []).map(text).join('');
203
+ const choices = [];
204
+ walkNodes(node, n => {
205
+ if (n === node || !spec.descendants.includes(n.type) || n.props?.[spec.value ?? 'value'] === undefined)
206
+ return;
207
+ const value = String(resolveValue(n.props[spec.value ?? 'value'], state));
208
+ const label = n.props[spec.label ?? 'label'];
209
+ const control = spec.labelChild ? n.children?.find(c => c.type === spec.labelChild) : n;
210
+ const display = typeof label === 'string' ? label : control ? text(control).trim() : '';
211
+ choices.push({ value, label: display || value });
212
+ });
213
+ return choices.length ? choices : null;
214
+ }
215
+ export function isNodeDisabled(node, state) {
216
+ if (node.disabled === undefined)
217
+ return false;
218
+ if (typeof node.disabled === 'boolean')
219
+ return node.disabled;
220
+ return evaluateCondition(node.disabled, state);
221
+ }
222
+ // ---------- 视角层级与导演镜头(D43 / T-layers:每层一个独立画面) ----------
223
+ /** 方案的层级(源码顺序):每层一个完全独立的画面。没有声明时为空,演示照常显示路径页面。 */
224
+ export function layersOf(project, scenarioId) {
225
+ const sc = scenarioId ? project.scenarios[scenarioId] : null;
226
+ const opt = sc ? project.works[sc.workId] : null;
227
+ // 图标:作者写的优先;没写时手机视口用 mobile,其余用 window
228
+ return Object.entries(opt?.layers ?? {}).map(([id, l]) => ({ id, title: l.title, sceneId: l.sceneId, icon: l.icon ?? (project.scenes[l.sceneId]?.viewport === 'mobile' ? 'mobile' : 'window') }));
229
+ }
230
+ /** 包含某个元素的层级(多层都有同名 id 时都返回)。 */
231
+ export function layersWithNode(project, scenarioId, nodeId) {
232
+ return layersOf(project, scenarioId)
233
+ .filter((l) => { const sc = project.scenes[l.sceneId]; return !!sc && !!findNode(sc, nodeId); })
234
+ .map((l) => l.id);
235
+ }
236
+ /**
237
+ * 步骤目标所在的画面:有层级的方案在各层画面里找(优先当前可见的那一层),否则是状态里的路径画面。
238
+ * 回放校验、select 取值都用它。
239
+ */
240
+ export function sceneOfTarget(project, scenario, state, target) {
241
+ const layers = layersOf(project, scenario.id);
242
+ if (!layers.length)
243
+ return project.scenes[state.sceneId];
244
+ const scenes = layers.map((l) => project.scenes[l.sceneId]).filter((x) => !!x && !!findNode(x, target));
245
+ return scenes.find((x) => isNodeVisible(x, target, state)) ?? scenes[0] ?? project.scenes[state.sceneId];
246
+ }
@@ -47,7 +47,7 @@ export function schemaDiagnostics(root, issues, opts = {}) {
47
47
  const code = condition ? 'INVALID_CONDITION' : 'INVALID_SCHEMA';
48
48
  // 有源位置时说明只写元素内的相对路径,这样展开到多个画面的同一错误说明相同、能合并
49
49
  const message = condition ? whenMessage(value, element) : where ? `${element ? `<${element}> ` : ''}${local || full}: ${issue.message}` : `${full}: ${issue.message}`;
50
- // 受影响的对象:画面 id(scenes.<id>)或表名 + id(options.<id> 等)
50
+ // 受影响的对象:画面 id(scenes.<id>)或表名 + id(works.<id> 等)
51
51
  const scope = path.length >= 2 ? (path[0] === 'scenes' ? path[1] : `${path[0]}.${path[1]}`) : full;
52
52
  const key = JSON.stringify([code, where ?? full, message]);
53
53
  const group = groups.get(key);