Skip to content

Commit 29d09dd

Browse files
committed
refactor(infographic): add api to get types
1 parent c687130 commit 29d09dd

4 files changed

Lines changed: 81 additions & 2 deletions

File tree

packages/infographic/src/options/parser.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ function parseDesignStructure(
7878
const structure = getStructure(type);
7979
if (!structure) throw new Error(`Structure ${type} not found`);
8080
const { component } = structure;
81-
return { component: (props) => component({ ...props, ...userProps }) };
81+
return {
82+
...structure,
83+
component: (props) => component({ ...props, ...userProps }),
84+
};
8285
}
8386

8487
function parseDesignTitle(
@@ -108,6 +111,7 @@ function parseDesignItem(
108111
if (!item) throw new Error(`Item ${type} not found`);
109112
const { component, options: itemOptions } = item;
110113
return {
114+
...item,
111115
component: (props) => {
112116
const { indexes } = props;
113117
const { data, themeConfig } = options;

packages/infographic/src/runtime/Infographic.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
parseOptions,
77
} from '../options';
88
import { Renderer } from '../renderer';
9-
import { parseSVG } from '../utils';
9+
import { getTypes, parseSVG } from '../utils';
1010

1111
export class Infographic {
1212
private parsedOptions: ParsedInfographicOptions;
@@ -57,6 +57,13 @@ export class Infographic {
5757
return template;
5858
}
5959

60+
getTypes() {
61+
const design = this.parsedOptions.design;
62+
const structure = design.structure.composites || [];
63+
const items = design.items.map((it) => it.composites || []);
64+
return getTypes({ structure, items });
65+
}
66+
6067
private setView() {
6168
const { container, width, height } = this.parsedOptions;
6269
if (!container) return;
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* 根据结构配置生成 TypeScript 类型字符串
3+
*/
4+
export function getTypes(
5+
composites: { structure: string[]; items: string[][] },
6+
commentsMap: Record<string, string> = {
7+
title: '信息图标题',
8+
desc: '信息图描述',
9+
items: '信息图的内容项',
10+
label: '项目标签文本',
11+
value: '项目数值内容',
12+
icon: '项目图标',
13+
illus: '项目插图',
14+
children: '子级项目(多层结构)',
15+
},
16+
): string {
17+
const { structure, items } = composites;
18+
19+
const lines: string[] = [];
20+
const indent = (level: number) => ' '.repeat(level);
21+
22+
function fieldLine(name: string, type: string, level: number) {
23+
const comment = commentsMap[name];
24+
return comment
25+
? `${indent(level)}/** ${comment} */\n${indent(level)}${name}: ${type};`
26+
: `${indent(level)}${name}: ${type};`;
27+
}
28+
29+
function buildItemType(level: number, indentLevel: number): string {
30+
const fields = items[level];
31+
const fieldLines: string[] = [];
32+
33+
for (const field of fields) {
34+
const type = field === 'value' ? 'number' : 'string';
35+
fieldLines.push(fieldLine(field, type, indentLevel + 1));
36+
}
37+
38+
if (items[level + 1]) {
39+
fieldLines.push(
40+
fieldLine(
41+
'children',
42+
`Array<${buildItemType(level + 1, indentLevel + 1)}>`,
43+
indentLevel + 1,
44+
),
45+
);
46+
}
47+
48+
return `{\n${fieldLines.join('\n')}\n${indent(indentLevel)}}`;
49+
}
50+
51+
lines.push('type InfographicType = {');
52+
53+
// title / desc
54+
if (structure.includes('title')) {
55+
lines.push(fieldLine('title', 'string', 1));
56+
lines.push(fieldLine('desc', 'string', 1));
57+
}
58+
59+
// items
60+
if (structure.includes('item') && items.length > 0) {
61+
lines.push(fieldLine('items', `Array<${buildItemType(0, 1)}>`, 1));
62+
}
63+
64+
lines.push('}');
65+
66+
return lines.join('\n');
67+
}

packages/infographic/src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from './color';
22
export * from './data';
3+
export * from './get-types';
34
export * from './icon';
45
export * from './item';
56
export * from './join';

0 commit comments

Comments
 (0)