Skip to content

Commit 376ff19

Browse files
author
jie
committed
merge: fix vis widget library groups
2 parents 161cb94 + 77f6936 commit 376ff19

4 files changed

Lines changed: 54 additions & 19 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { COMPONENT_CATEGORY_DEFS, resolveComponentCategory } from './ComponentsList';
3+
4+
const renderedCategories = new Set(COMPONENT_CATEGORY_DEFS.map((def) => def.key));
5+
6+
describe('ComponentsList category mapping', () => {
7+
it.each([
8+
['basic/text', undefined, 'basic'],
9+
['interaction/basic-switch', 'interaction', 'controls'],
10+
['chart/echarts-line', 'chart', 'charts'],
11+
['custom/alert-list', 'custom', 'business'],
12+
['resources/model-3d', 'resources', 'media'],
13+
['geo/map', 'geo', 'media'],
14+
['industrial/pump', 'industrial', 'industrial'],
15+
['unknown/widget', undefined, 'basic'],
16+
] as const)('maps %s category %s to visible group %s', (componentId, category, expected) => {
17+
const resolved = resolveComponentCategory({ componentId, category });
18+
19+
expect(resolved).toBe(expected);
20+
expect(renderedCategories.has(resolved)).toBe(true);
21+
});
22+
});

apps/studio/src/components/LeftPanel/ComponentsList.tsx

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,44 @@ const ICON_MAP: Record<string, LucideIcon> = {
100100
Shapes,
101101
};
102102

103-
const CATEGORY_DEFS = [
103+
export const COMPONENT_CATEGORY_DEFS = [
104104
{ key: 'basic', Icon: Box },
105105
{ key: 'controls', Icon: MousePointerClick },
106106
{ key: 'charts', Icon: BarChart3 },
107107
{ key: 'media', Icon: Film },
108+
{ key: 'business', Icon: Bell },
108109
{ key: 'decoration', Icon: Shapes },
109110
{ key: 'industrial', Icon: Cpu },
110111
] as const;
111112

112113
type CategoryMap = Record<string, RegistryListEntry[]>;
113114
const HIDDEN_COMPONENT_IDS = new Set(['geo/map-china']);
114115

116+
export type ComponentCategoryKey = (typeof COMPONENT_CATEGORY_DEFS)[number]['key'];
117+
118+
export function resolveComponentCategory(
119+
entry: Pick<RegistryListEntry, 'category' | 'componentId'>,
120+
): ComponentCategoryKey {
121+
const rawCategory =
122+
entry.category?.toLowerCase() || (entry.componentId.split('/')[0] || 'basic').toLowerCase();
123+
const prefixMap: Record<string, ComponentCategoryKey> = {
124+
basic: 'basic',
125+
controls: 'controls',
126+
display: 'basic',
127+
interaction: 'controls',
128+
chart: 'charts',
129+
charts: 'charts',
130+
media: 'media',
131+
resources: 'media',
132+
geo: 'media',
133+
custom: 'business',
134+
decoration: 'decoration',
135+
industrial: 'industrial',
136+
};
137+
138+
return prefixMap[rawCategory] ?? 'basic';
139+
}
140+
115141
function resolveEntryDisplayName(entry: RegistryListEntry, language: string): string {
116142
const normalized = (language || '').toLowerCase();
117143
const baseLanguage = normalized.split('-')[0] ?? normalized;
@@ -162,28 +188,13 @@ export default function ComponentsList({
162188

163189
const entriesByCategory = useMemo(() => {
164190
const map: CategoryMap = {};
165-
const prefixMap: Record<string, string> = {
166-
basic: 'basic',
167-
controls: 'controls',
168-
display: 'basic',
169-
chart: 'charts',
170-
charts: 'charts',
171-
media: 'media',
172-
decoration: 'decoration',
173-
industrial: 'industrial',
174-
};
175-
176-
CATEGORY_DEFS.forEach((def) => {
191+
COMPONENT_CATEGORY_DEFS.forEach((def) => {
177192
map[def.key] = [];
178193
});
179194

180195
entries.forEach((entry) => {
181196
// 优先从注册表读取直接定义好的 category,如果不存在再 fallback 到路径前缀解析
182-
const rawCategory =
183-
((entry as any).category as string | undefined)?.toLowerCase() ||
184-
(entry.componentId.split('/')[0] || 'basic').toLowerCase();
185-
const category = prefixMap[rawCategory] || rawCategory || 'basic';
186-
map[category] = map[category] ?? [];
197+
const category = resolveComponentCategory(entry);
187198
map[category].push(entry);
188199
});
189200

@@ -253,7 +264,7 @@ export default function ComponentsList({
253264
defaultValue={searchQuery ? Object.keys(filteredCategoriesMap) : ['basic']}
254265
className="space-y-2"
255266
>
256-
{CATEGORY_DEFS.map((categoryDef) => {
267+
{COMPONENT_CATEGORY_DEFS.map((categoryDef) => {
257268
const items = filteredCategoriesMap[categoryDef.key] ?? [];
258269
if (items.length === 0 && searchQuery) {
259270
return null;

apps/studio/src/i18n/locales/en/editor.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
"controls": "Controls",
108108
"charts": "Charts",
109109
"media": "Media",
110+
"business": "Business",
110111
"decoration": "Decoration",
111112
"industrial": "Industrial"
112113
}

apps/studio/src/i18n/locales/zh/editor.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
"controls": "控件",
108108
"charts": "图表",
109109
"media": "媒体",
110+
"business": "业务",
110111
"decoration": "装饰",
111112
"industrial": "工业"
112113
}

0 commit comments

Comments
 (0)