@joplo-app/core 0.2.17 → 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.
Files changed (64) hide show
  1. package/dist/actions.d.ts +6 -0
  2. package/dist/actions.js +34 -0
  3. package/dist/agent-skill.d.ts +10 -13
  4. package/dist/agent-skill.js +52 -31
  5. package/dist/auto-caption.d.ts +22 -0
  6. package/dist/auto-caption.js +166 -0
  7. package/dist/catalog.d.ts +38 -41
  8. package/dist/catalog.js +26 -317
  9. package/dist/compile.d.ts +2 -8
  10. package/dist/compile.js +302 -353
  11. package/dist/content-i18n.js +27 -16
  12. package/dist/feature-diff.d.ts +3 -3
  13. package/dist/feature-diff.js +28 -6
  14. package/dist/focus.d.ts +3 -1
  15. package/dist/focus.js +16 -34
  16. package/dist/guide.d.ts +10 -12
  17. package/dist/guide.js +419 -519
  18. package/dist/index.d.ts +4 -1
  19. package/dist/index.js +4 -1
  20. package/dist/media.d.ts +10 -0
  21. package/dist/media.js +43 -0
  22. package/dist/node.d.ts +1 -2
  23. package/dist/node.js +1 -2
  24. package/dist/outline.d.ts +1 -1
  25. package/dist/outline.js +3 -3
  26. package/dist/pace.d.ts +9 -1
  27. package/dist/pace.js +23 -3
  28. package/dist/player.d.ts +8 -41
  29. package/dist/player.js +85 -148
  30. package/dist/playground.d.ts +38 -0
  31. package/dist/playground.js +111 -0
  32. package/dist/plugin-data.d.ts +11 -0
  33. package/dist/plugin-data.js +96 -0
  34. package/dist/plugins/compiler.d.ts +66 -0
  35. package/dist/plugins/compiler.js +428 -0
  36. package/dist/plugins/data.d.ts +12 -0
  37. package/dist/plugins/data.js +120 -0
  38. package/dist/result.d.ts +1 -1
  39. package/dist/result.js +1 -0
  40. package/dist/runtime.d.ts +22 -0
  41. package/dist/runtime.js +18 -0
  42. package/dist/schema.d.ts +566 -82
  43. package/dist/schema.js +65 -53
  44. package/dist/scope.js +14 -5
  45. package/dist/step-identity.d.ts +1 -1
  46. package/dist/step-identity.js +1 -1
  47. package/dist/themes.d.ts +7 -0
  48. package/dist/themes.js +5 -0
  49. package/dist/traits.d.ts +7 -0
  50. package/dist/traits.js +26 -0
  51. package/dist/validate.js +17 -4
  52. package/dist/voice.d.ts +8 -2
  53. package/dist/voice.js +14 -2
  54. package/package.json +7 -4
  55. package/dist/diagram/elk.d.ts +0 -6
  56. package/dist/diagram/elk.js +0 -56
  57. package/dist/diagram/graph-layout.d.ts +0 -123
  58. package/dist/diagram/graph-layout.js +0 -620
  59. package/dist/diagram/index.d.ts +0 -41
  60. package/dist/diagram/index.js +0 -92
  61. package/dist/diagram/model.d.ts +0 -217
  62. package/dist/diagram/model.js +0 -189
  63. package/dist/diagram/sequence-layout.d.ts +0 -12
  64. package/dist/diagram/sequence-layout.js +0 -185
@@ -1,92 +0,0 @@
1
- import { DEFAULT_ASPECT, gridLayout } from "./graph-layout.js";
2
- import { layoutSequence } from "./sequence-layout.js";
3
- import { GRAPH_FAMILY, SEQUENCE_FAMILY, graphStructure, groupChains, isDiagramRoot, sequenceStructure } from "./model.js";
4
- export * from "./model.js";
5
- export { layoutSequence, SEQ_BADGE } from "./sequence-layout.js";
6
- export { DEFAULT_ASPECT, compactReversals, crossings, gridLayout, layoutGraph, layoutVariant, feedbackEdges, placeLabels, toElk, rootOptions, sampleRoute } from "./graph-layout.js";
7
- /** 舞台画面区域(字幕区在舞台外):电脑 1280 × 800,手机 390 × 760(手机外框内)。 */
8
- const STAGE_AREA = { desktop: { w: 1280, h: 800 }, mobile: { w: 390, h: 760 } };
9
- /**
10
- * 一张图的目标外框比例(D69 补充):作者写了 aspect 就用它;写了数字高度 h 时按“舞台宽度 / h”;
11
- * 否则(h="100%" 或不写,通常是层级里铺满舞台的图)用舞台画面区域的比例。
12
- */
13
- export function diagramGoal(node, viewport = 'desktop') {
14
- const p = node.props ?? {};
15
- const area = STAGE_AREA[viewport] ?? STAGE_AREA.desktop;
16
- const w = area.w - 48;
17
- if (typeof p.aspect === 'number' && Number.isFinite(p.aspect) && p.aspect > 0)
18
- return { aspect: p.aspect, width: w, height: w / p.aspect };
19
- if (typeof p.h === 'number' && p.h > 0)
20
- return { aspect: Math.max(0.3, Math.min(6, w / p.h)), width: w, height: p.h };
21
- const h = area.h - 44;
22
- return { aspect: w / h, width: w, height: h };
23
- }
24
- /** 算一张图表的布局;ELK 出错时退回网格,并通过 onError 报告(编译器转成 DIAGRAM_LAYOUT_FAILED 警告)。 */
25
- export function layoutFor(node, engine, onError, viewport = 'desktop') {
26
- if (node.type === 'Sequence')
27
- return layoutSequence(sequenceStructure(node));
28
- if (node.type !== 'Diagram' && node.type !== 'StateMachine')
29
- return null;
30
- const s = graphStructure(node);
31
- if (!engine)
32
- return gridLayout(s);
33
- try {
34
- return engine(s, diagramGoal(node, viewport));
35
- }
36
- catch (e) {
37
- onError?.(e instanceof Error ? e.message : String(e));
38
- return gridLayout(s);
39
- }
40
- }
41
- /** 给一棵画面树里的每张图表挂上布局(就地修改)。嵌套的图表各自布局。viewport:画面的设备(决定目标比例)。 */
42
- export function attachLayouts(root, engine, onError, viewport = 'desktop') {
43
- const visit = (n) => {
44
- if (isDiagramRoot(n.type)) {
45
- const l = layoutFor(n, engine, (m) => onError?.(n, m), viewport);
46
- if (l)
47
- n.layout = l;
48
- }
49
- for (const c of n.children ?? [])
50
- visit(c);
51
- };
52
- visit(root);
53
- }
54
- export function diagramsIn(root) {
55
- const out = [];
56
- const visit = (n) => {
57
- if (n.type === 'Diagram' || n.type === 'StateMachine') {
58
- const fam = GRAPH_FAMILY[n.type];
59
- const s = graphStructure(n);
60
- const groupIds = new Set(s.groups.filter((g) => !g.id.startsWith('_')).map((g) => g.id));
61
- const d = { id: n.id, type: n.type, nodes: new Set([...s.nodes.map((x) => x.id), ...groupIds]), edges: [], groupsOf: groupChains(s), groupIds };
62
- const walk = (c) => {
63
- if (isDiagramRoot(c.type))
64
- return; // 嵌套的图表自成一图
65
- if (c.type === fam.edge)
66
- d.edges.push({ id: c.id, from: String(c.props?.from ?? ''), to: String(c.props?.to ?? '') });
67
- for (const k of c.children ?? [])
68
- walk(k);
69
- };
70
- for (const k of n.children ?? [])
71
- walk(k);
72
- out.push(d);
73
- }
74
- else if (n.type === 'Sequence') {
75
- const s = sequenceStructure(n);
76
- out.push({
77
- id: n.id,
78
- type: 'Sequence',
79
- nodes: new Set(s.actors.map((a) => a.id)),
80
- edges: s.messages.map((m) => ({ id: m.id, from: m.from, to: m.to })),
81
- groupsOf: new Map(s.actors.map((a) => [a.id, a.group ? [a.group] : []])),
82
- groupIds: new Set(),
83
- });
84
- }
85
- for (const c of n.children ?? [])
86
- visit(c);
87
- };
88
- visit(root);
89
- return out;
90
- }
91
- /** 时序图的消息类型名(compile 生成缺省 id 用)。 */
92
- export const MESSAGE_TYPES = new Set([SEQUENCE_FAMILY.message, GRAPH_FAMILY.StateMachine.edge]);
@@ -1,217 +0,0 @@
1
- /**
2
- * 关系类图表(D69)的结构与几何类型:架构图 Diagram、状态机 StateMachine、时序图 Sequence。
3
- *
4
- * - 结构只取**静态属性**(label / description / kind / group / pin…):state、when 等绑定不参与,
5
- * 所以播放中状态与可见性变化不会改变结构,布局只算一次(一张图在所有步骤里坐标相同)。
6
- * - 布局在编译期算好写进 IR(SceneNode.layout),发布站和看板只负责渲染和动画,浏览器里不跑布局算法。
7
- * - 本文件不依赖 Node,可以被浏览器端(@joplo-app/web)引用;ELK 只在 Node 入口(diagram/elk.ts)里加载。
8
- */
9
- import type { SceneNode } from '../schema.js';
10
- export type Point = {
11
- x: number;
12
- y: number;
13
- };
14
- export type Box = {
15
- x: number;
16
- y: number;
17
- w: number;
18
- h: number;
19
- };
20
- /** 架构图与状态机共用的“图”布局(ELK layered + 层级处理,见 graph-layout.ts)。 */
21
- export type GraphLayout = {
22
- kind: 'graph';
23
- /** elk = 编译期 ELK 布局;grid = 没有布局引擎时的退化排列(只在测试或旧调用方出现)。 */
24
- engine: 'elk' | 'grid';
25
- /** 实际采用的方向(作者没写 direction 时由比例决定,D69 补充)。 */
26
- direction?: 'right' | 'down';
27
- width: number;
28
- height: number;
29
- nodes: Record<string, Box>;
30
- groups: Record<string, Box>;
31
- /** 分组标题(图标 + 文字)所在的矩形,在分组框顶部的留白里。 */
32
- titles: Record<string, Box>;
33
- edges: Record<string, {
34
- points: Point[];
35
- label: Box | null;
36
- }>;
37
- /** 状态机的起点圆点:初始状态 id → 圆点盒子;指向初始状态的连线在 edges[`${START}${id}`]。 */
38
- starts?: Record<string, Box>;
39
- };
40
- /** 时序图布局(sequence-layout.ts,纯函数)。 */
41
- export type SequenceLayout = {
42
- kind: 'sequence';
43
- width: number;
44
- height: number;
45
- /** 参与者头部盒子。 */
46
- actors: Record<string, Box>;
47
- /** 生命线:x 与上下端。 */
48
- lifelines: Record<string, {
49
- x: number;
50
- y1: number;
51
- y2: number;
52
- }>;
53
- /** 消息:折线(自调用是回环),标签框,序号(从 1 起,按源码顺序)。 */
54
- messages: Record<string, {
55
- points: Point[];
56
- label: Box | null;
57
- n: number;
58
- self: boolean;
59
- }>;
60
- /** 参与者分组(业务域框)与它的标题。 */
61
- groups: Record<string, Box>;
62
- titles: Record<string, Box>;
63
- /** 片段(alt / opt / loop / par…)框与它左上角的标签。 */
64
- blocks: Record<string, Box>;
65
- blockTitles: Record<string, Box>;
66
- };
67
- export type DiagramLayout = GraphLayout | SequenceLayout;
68
- /** 状态机起点圆点的 id 前缀(不会与作者 id 冲突:作者 id 只能以字母开头)。 */
69
- export declare const START = "__start:";
70
- export type GraphRootType = 'Diagram' | 'StateMachine';
71
- export declare const GRAPH_FAMILY: {
72
- readonly Diagram: {
73
- readonly node: "Diagram.Node";
74
- readonly edge: "Diagram.Edge";
75
- readonly group: "Diagram.Group";
76
- };
77
- readonly StateMachine: {
78
- readonly node: "StateMachine.State";
79
- readonly edge: "StateMachine.Transition";
80
- readonly group: "StateMachine.Group";
81
- };
82
- };
83
- export declare const SEQUENCE_FAMILY: {
84
- readonly root: "Sequence";
85
- readonly actor: "Sequence.Actor";
86
- readonly message: "Sequence.Message";
87
- readonly group: "Sequence.Group";
88
- readonly block: "Sequence.Block";
89
- };
90
- /** 能被 trace 的图表根组件。 */
91
- export declare const DIAGRAM_ROOTS: readonly ["Diagram", "StateMachine", "Sequence"];
92
- export type DiagramRootType = (typeof DIAGRAM_ROOTS)[number];
93
- export declare const isDiagramRoot: (t: string) => t is DiagramRootType;
94
- /** 节点 / 参与者的 kind(决定图标与外形)。 */
95
- export declare const DIAGRAM_NODE_KINDS: readonly ["client", "service", "model", "database", "queue", "storage", "external", "tool", "user"];
96
- /** 分组图标(预设):节点 kind 之外再加几种常用的“域”图标。 */
97
- export declare const DIAGRAM_GROUP_ICONS: readonly ["client", "service", "model", "database", "queue", "storage", "external", "tool", "user", "cloud", "gateway", "lock", "network", "device", "box", "brain"];
98
- /** 分组色调:映射到主题变量(blue = --jp-info,green = --jp-ok,orange = --jp-warn,red = --jp-danger),其余为固定低饱和色。 */
99
- export declare const DIAGRAM_TONES: readonly ["gray", "blue", "green", "orange", "red", "purple", "teal"];
100
- export declare const DIAGRAM_PINS: readonly ["start", "end"];
101
- export declare const SEQUENCE_BLOCK_KINDS: readonly ["alt", "opt", "loop", "par", "break", "critical"];
102
- /** 分组框顶部标题条的高度(图标 + 文字)与内边距。 */
103
- export declare const GROUP_HEAD = 26;
104
- export declare const GROUP_PAD = 12;
105
- export declare const GROUP_FONT = 12;
106
- export declare const NODE_FONT = 14;
107
- export declare const DESC_FONT = 12;
108
- export declare const EDGE_FONT = 12;
109
- export declare const staticText: (v: unknown) => string;
110
- /** 粗略的文字宽度:中日韩字符按 1 个字号宽,其余按 0.58(与渲染字体无关,结果确定)。 */
111
- export declare function textWidth(s: string, size: number): number;
112
- /** 架构图节点尺寸:只取决于静态的 label / description / kind。 */
113
- export declare function nodeSize(label: string, description: string, kind: string): {
114
- w: number;
115
- h: number;
116
- };
117
- /** 状态机的状态:圆角胶囊,文字居中;终态外圈再留 4px 画双线。 */
118
- export declare function stateSize(label: string, description: string, final: boolean): {
119
- w: number;
120
- h: number;
121
- };
122
- /** 分组标题条的宽度:手写字体的拉丁字母比估算宽,多留 20%(宁可宽也不截断)。 */
123
- export declare const groupTitleWidth: (label: string, icon: boolean) => number;
124
- export declare const edgeLabelSize: (label: string, font?: number) => {
125
- lw: number;
126
- lh: number;
127
- };
128
- export type GNode = {
129
- id: string;
130
- /** 源码顺序(节点与分组统一编号):ELK 按它排同层顺序。 */
131
- seq: number;
132
- label: string;
133
- description: string;
134
- kind: string;
135
- parent: string | null;
136
- w: number;
137
- h: number;
138
- pin?: 'start' | 'end';
139
- /** 状态机:初始状态(前面画一个实心起点)、终态(双线)。 */
140
- initial?: boolean;
141
- final?: boolean;
142
- };
143
- export type GGroup = {
144
- id: string;
145
- seq: number;
146
- label: string;
147
- parent: string | null;
148
- icon?: string;
149
- pin?: 'start' | 'end';
150
- };
151
- export type GEdge = {
152
- id: string;
153
- from: string;
154
- to: string;
155
- label: string;
156
- lw: number;
157
- lh: number;
158
- async?: boolean;
159
- };
160
- /** direction:作者写的方向;auto = 没写,按舞台比例在 right / down 里选(D69 补充)。 */
161
- export type GraphStructure = {
162
- root: GraphRootType;
163
- direction: 'right' | 'down' | 'auto';
164
- groups: GGroup[];
165
- nodes: GNode[];
166
- edges: GEdge[];
167
- };
168
- /**
169
- * 从 IR 里的 Diagram / StateMachine 节点取出结构。分组既可以用嵌套表达,也可以用 group="分组 id" 引用
170
- * (编译期已校验引用存在、不成环、与嵌套不冲突;这里引用无效时退回嵌套的父分组)。
171
- */
172
- export declare function graphStructure(node: SceneNode): GraphStructure;
173
- export type SActor = {
174
- id: string;
175
- label: string;
176
- description: string;
177
- kind: string;
178
- group: string | null;
179
- w: number;
180
- h: number;
181
- };
182
- export type SGroup = {
183
- id: string;
184
- label: string;
185
- icon?: string;
186
- };
187
- export type SMessage = {
188
- id: string;
189
- from: string;
190
- to: string;
191
- label: string;
192
- reply: boolean;
193
- async: boolean;
194
- blocks: string[];
195
- };
196
- export type SBlock = {
197
- id: string;
198
- label: string;
199
- kind: string;
200
- parent: string | null;
201
- };
202
- export type SequenceStructure = {
203
- numbered: boolean;
204
- actors: SActor[];
205
- groups: SGroup[];
206
- messages: SMessage[];
207
- blocks: SBlock[];
208
- };
209
- export declare function actorSize(label: string, description: string, kind: string): {
210
- w: number;
211
- h: number;
212
- };
213
- export declare function sequenceStructure(node: SceneNode): SequenceStructure;
214
- /** 布局缓存与“结构是否变化”的稳定键。 */
215
- export declare const structureKey: (s: GraphStructure | SequenceStructure) => string;
216
- /** 图表根节点里,节点 / 分组的父链(用于 trace 推算分组状态、镜头取景)。 */
217
- export declare function groupChains(s: GraphStructure): Map<string, string[]>;
@@ -1,189 +0,0 @@
1
- /** 状态机起点圆点的 id 前缀(不会与作者 id 冲突:作者 id 只能以字母开头)。 */
2
- export const START = '__start:';
3
- export const GRAPH_FAMILY = {
4
- Diagram: { node: 'Diagram.Node', edge: 'Diagram.Edge', group: 'Diagram.Group' },
5
- StateMachine: { node: 'StateMachine.State', edge: 'StateMachine.Transition', group: 'StateMachine.Group' },
6
- };
7
- export const SEQUENCE_FAMILY = { root: 'Sequence', actor: 'Sequence.Actor', message: 'Sequence.Message', group: 'Sequence.Group', block: 'Sequence.Block' };
8
- /** 能被 trace 的图表根组件。 */
9
- export const DIAGRAM_ROOTS = ['Diagram', 'StateMachine', 'Sequence'];
10
- export const isDiagramRoot = (t) => DIAGRAM_ROOTS.includes(t);
11
- /** 节点 / 参与者的 kind(决定图标与外形)。 */
12
- export const DIAGRAM_NODE_KINDS = ['client', 'service', 'model', 'database', 'queue', 'storage', 'external', 'tool', 'user'];
13
- /** 分组图标(预设):节点 kind 之外再加几种常用的“域”图标。 */
14
- export const DIAGRAM_GROUP_ICONS = [...DIAGRAM_NODE_KINDS, 'cloud', 'gateway', 'lock', 'network', 'device', 'box', 'brain'];
15
- /** 分组色调:映射到主题变量(blue = --jp-info,green = --jp-ok,orange = --jp-warn,red = --jp-danger),其余为固定低饱和色。 */
16
- export const DIAGRAM_TONES = ['gray', 'blue', 'green', 'orange', 'red', 'purple', 'teal'];
17
- export const DIAGRAM_PINS = ['start', 'end'];
18
- export const SEQUENCE_BLOCK_KINDS = ['alt', 'opt', 'loop', 'par', 'break', 'critical'];
19
- // ---------- 尺寸估算(编译期与浏览器共用,结果确定) ----------
20
- /** 分组框顶部标题条的高度(图标 + 文字)与内边距。 */
21
- export const GROUP_HEAD = 26;
22
- export const GROUP_PAD = 12;
23
- export const GROUP_FONT = 12;
24
- export const NODE_FONT = 14;
25
- export const DESC_FONT = 12;
26
- export const EDGE_FONT = 12;
27
- const NODE_MIN_W = 116;
28
- const NODE_MAX_W = 196;
29
- const isBinding = (v) => typeof v === 'string' && /^\$(ui|data|inputs)\b/.test(v);
30
- export const staticText = (v) => (typeof v === 'string' && !isBinding(v) ? v : typeof v === 'number' ? String(v) : '');
31
- /** 粗略的文字宽度:中日韩字符按 1 个字号宽,其余按 0.58(与渲染字体无关,结果确定)。 */
32
- export function textWidth(s, size) {
33
- let w = 0;
34
- for (const ch of s)
35
- w += (ch.codePointAt(0) ?? 0) > 0x2e80 ? size : size * 0.58;
36
- return Math.ceil(w);
37
- }
38
- /** 架构图节点尺寸:只取决于静态的 label / description / kind。 */
39
- export function nodeSize(label, description, kind) {
40
- const icon = kind === 'database' || kind === 'queue' ? 0 : 26;
41
- const labelW = textWidth(label || ' ', NODE_FONT) + icon + 28;
42
- const descW = description ? textWidth(description, DESC_FONT) + 28 : 0;
43
- let w = Math.max(NODE_MIN_W, Math.min(NODE_MAX_W, Math.max(labelW, descW)));
44
- const descLines = description ? Math.min(2, Math.ceil(descW / w)) : 0;
45
- let h = 46 + descLines * 16;
46
- if (kind === 'database')
47
- h += 14; // 圆柱的上下盖
48
- if (kind === 'queue')
49
- w += 20; // 管道的左右端
50
- return { w, h };
51
- }
52
- /** 状态机的状态:圆角胶囊,文字居中;终态外圈再留 4px 画双线。 */
53
- export function stateSize(label, description, final) {
54
- const labelW = textWidth(label || ' ', NODE_FONT) + 32;
55
- const descW = description ? textWidth(description, DESC_FONT) + 28 : 0;
56
- const w = Math.max(88, Math.min(NODE_MAX_W, Math.max(labelW, descW)));
57
- const descLines = description ? Math.min(2, Math.ceil(descW / w)) : 0;
58
- const pad = final ? 8 : 0;
59
- return { w: w + pad, h: 40 + descLines * 16 + pad };
60
- }
61
- /** 分组标题条的宽度:手写字体的拉丁字母比估算宽,多留 20%(宁可宽也不截断)。 */
62
- export const groupTitleWidth = (label, icon) => Math.ceil(textWidth(label || ' ', GROUP_FONT) * 1.2) + (icon ? 19 : 0) + 18;
63
- export const edgeLabelSize = (label, font = EDGE_FONT) => (label ? { lw: textWidth(label, font) + 10, lh: 18 } : { lw: 0, lh: 0 });
64
- const pinOf = (v) => (v === 'start' || v === 'end' ? v : undefined);
65
- /**
66
- * 从 IR 里的 Diagram / StateMachine 节点取出结构。分组既可以用嵌套表达,也可以用 group="分组 id" 引用
67
- * (编译期已校验引用存在、不成环、与嵌套不冲突;这里引用无效时退回嵌套的父分组)。
68
- */
69
- export function graphStructure(node) {
70
- const root = (node.type === 'StateMachine' ? 'StateMachine' : 'Diagram');
71
- const fam = GRAPH_FAMILY[root];
72
- const p = node.props ?? {};
73
- const direction = p.direction === 'down' ? 'down' : p.direction === 'right' ? 'right' : 'auto';
74
- const groups = [];
75
- const nodes = [];
76
- const edges = [];
77
- let seq = 0;
78
- const walk = (children, parent) => {
79
- for (const c of children ?? []) {
80
- const cp = c.props ?? {};
81
- const ref = typeof cp.group === 'string' ? cp.group : null;
82
- if (c.type === fam.group) {
83
- groups.push({ id: c.id, seq: seq++, label: staticText(cp.label), parent: ref ?? parent, ...(typeof cp.icon === 'string' ? { icon: cp.icon } : {}), ...(pinOf(cp.pin) ? { pin: pinOf(cp.pin) } : {}) });
84
- walk(c.children, c.id);
85
- }
86
- else if (c.type === fam.node) {
87
- const label = staticText(cp.label) || c.id;
88
- const description = staticText(cp.description);
89
- if (root === 'StateMachine') {
90
- const final = cp.final === true;
91
- nodes.push({ id: c.id, seq: seq++, label, description, kind: 'state', parent: ref ?? parent, ...stateSize(label, description, final), ...(pinOf(cp.pin) ? { pin: pinOf(cp.pin) } : {}), ...(cp.initial === true ? { initial: true } : {}), ...(final ? { final: true } : {}) });
92
- }
93
- else {
94
- const kind = typeof cp.kind === 'string' ? cp.kind : 'service';
95
- nodes.push({ id: c.id, seq: seq++, label, description, kind, parent: ref ?? parent, ...nodeSize(label, description, kind), ...(pinOf(cp.pin) ? { pin: pinOf(cp.pin) } : {}) });
96
- }
97
- }
98
- else if (c.type === fam.edge) {
99
- const label = staticText(cp.label);
100
- edges.push({ id: c.id, from: String(cp.from ?? ''), to: String(cp.to ?? ''), label, ...edgeLabelSize(label), ...(cp.async === true ? { async: true } : {}) });
101
- }
102
- // 嵌套的另一张图自成一图;其他类型在图里不参与布局
103
- }
104
- };
105
- walk(node.children, null);
106
- // 引用了不存在的分组:退回顶层(编译期已报错,这里只保证不崩)
107
- const gids = new Set(groups.map((g) => g.id));
108
- for (const g of groups)
109
- if (g.parent && !gids.has(g.parent))
110
- g.parent = null;
111
- for (const n of nodes)
112
- if (n.parent && !gids.has(n.parent))
113
- n.parent = null;
114
- // 成环的分组引用:断开(编译期已报 DIAGRAM_GROUP_CYCLE)
115
- const parentOf = new Map(groups.map((g) => [g.id, g.parent]));
116
- for (const g of groups) {
117
- const seen = new Set([g.id]);
118
- for (let q = g.parent; q; q = parentOf.get(q) ?? null) {
119
- if (seen.has(q)) {
120
- g.parent = null;
121
- parentOf.set(g.id, null);
122
- break;
123
- }
124
- seen.add(q);
125
- }
126
- }
127
- return { root, direction, groups, nodes, edges };
128
- }
129
- export function actorSize(label, description, kind) {
130
- // 参与者头部总带图标(数据库、队列也画小图标);手写字体的拉丁字母偏宽,多留 15%
131
- void kind;
132
- const labelW = Math.ceil(textWidth(label || ' ', NODE_FONT) * 1.15) + 22 + 30;
133
- const descW = description ? textWidth(description, DESC_FONT) + 24 : 0;
134
- const w = Math.max(96, Math.min(NODE_MAX_W, Math.max(labelW, descW)));
135
- return { w, h: description ? 58 : 42 };
136
- }
137
- export function sequenceStructure(node) {
138
- const p = node.props ?? {};
139
- const actors = [];
140
- const groups = [];
141
- const messages = [];
142
- const blocks = [];
143
- const walk = (children, group, chain) => {
144
- for (const c of children ?? []) {
145
- const cp = c.props ?? {};
146
- if (c.type === SEQUENCE_FAMILY.group) {
147
- groups.push({ id: c.id, label: staticText(cp.label), ...(typeof cp.icon === 'string' ? { icon: cp.icon } : {}) });
148
- walk(c.children, c.id, chain);
149
- }
150
- else if (c.type === SEQUENCE_FAMILY.actor) {
151
- const label = staticText(cp.label) || c.id;
152
- const description = staticText(cp.description);
153
- const kind = typeof cp.kind === 'string' ? cp.kind : 'service';
154
- actors.push({ id: c.id, label, description, kind, group, ...actorSize(label, description, kind) });
155
- }
156
- else if (c.type === SEQUENCE_FAMILY.block) {
157
- const kind = typeof cp.kind === 'string' ? cp.kind : 'opt';
158
- blocks.push({ id: c.id, label: staticText(cp.label), kind, parent: chain[chain.length - 1] ?? null });
159
- walk(c.children, group, [...chain, c.id]);
160
- }
161
- else if (c.type === SEQUENCE_FAMILY.message) {
162
- messages.push({ id: c.id, from: String(cp.from ?? ''), to: String(cp.to ?? ''), label: staticText(cp.label), reply: cp.reply === true, async: cp.async === true, blocks: [...chain] });
163
- }
164
- }
165
- };
166
- walk(node.children, null, []);
167
- return { numbered: p.numbered !== false, actors, groups, messages, blocks };
168
- }
169
- /** 布局缓存与“结构是否变化”的稳定键。 */
170
- export const structureKey = (s) => JSON.stringify(s);
171
- /** 图表根节点里,节点 / 分组的父链(用于 trace 推算分组状态、镜头取景)。 */
172
- export function groupChains(s) {
173
- const parentOf = new Map(s.groups.map((g) => [g.id, g.parent]));
174
- const chain = (gid) => {
175
- const out = [];
176
- const seen = new Set();
177
- for (let q = gid; q && !seen.has(q); q = parentOf.get(q) ?? null) {
178
- seen.add(q);
179
- out.push(q);
180
- }
181
- return out;
182
- };
183
- const m = new Map();
184
- for (const n of s.nodes)
185
- m.set(n.id, chain(n.parent));
186
- for (const g of s.groups)
187
- m.set(g.id, chain(g.parent));
188
- return m;
189
- }
@@ -1,12 +0,0 @@
1
- /**
2
- * 时序图布局(D69,纯函数、确定):参与者按源码顺序排成列,消息按源码顺序一行一条,片段(alt / opt / loop…)是包住
3
- * 若干行的框,参与者分组是包住若干列的业务域框。不需要通用图布局算法,编译期直接算好写进 IR。
4
- *
5
- * - 列间距:先按相邻头部宽度加最小间隙,再保证每条消息的标签放得下(跨多列的消息把不够的宽度平均分给它跨过的间隙);
6
- * 自调用(from === to)的回环和标签放在生命线右侧,下一列让出位置。
7
- * - 行:消息行高固定(标签在线上方),自调用多一点;片段开始处多一行放片段标签,结束处留一点底。
8
- */
9
- import { type SequenceLayout, type SequenceStructure } from './model.js';
10
- /** 序号徽章占的宽度(numbered 时标签前面画一个圆形序号)。 */
11
- export declare const SEQ_BADGE = 20;
12
- export declare function layoutSequence(s: SequenceStructure): SequenceLayout;