forked from mhmzdev/figma-flutter-mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariant-analyzer.ts
More file actions
333 lines (283 loc) · 11.4 KB
/
variant-analyzer.ts
File metadata and controls
333 lines (283 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// src/extractors/components/variant-analyzer.mts
import type {FigmaNode} from '../../types/figma.js';
import type {ComponentVariant} from './types.js';
/**
* Variant analysis for ComponentSets
*/
export class VariantAnalyzer {
/**
* Analyze component set and extract all variants
*/
async analyzeComponentSet(componentSetNode: FigmaNode): Promise<ComponentVariant[]> {
if (componentSetNode.type !== 'COMPONENT_SET') {
throw new Error('Node is not a COMPONENT_SET');
}
const variants: ComponentVariant[] = [];
if (!componentSetNode.children || componentSetNode.children.length === 0) {
return variants;
}
// Process each variant (child component)
componentSetNode.children.forEach(child => {
if (child.type === 'COMPONENT') {
const variant = this.extractVariantInfo(child);
variants.push(variant);
}
});
// Determine default variant
const defaultVariant = this.determineDefaultVariant(variants);
if (defaultVariant) {
variants.forEach(variant => {
variant.isDefault = variant.nodeId === defaultVariant.nodeId;
});
}
return variants;
}
/**
* Extract variant information from a component node
*/
private extractVariantInfo(componentNode: FigmaNode): ComponentVariant {
const nodeAny = componentNode as any;
return {
nodeId: componentNode.id,
name: componentNode.name,
properties: this.parseVariantProperties(componentNode.name),
isDefault: false // Will be set later by determineDefaultVariant
};
}
/**
* Parse variant properties from component name
* Figma variant names are typically in format: "Property=Value, Property2=Value2"
*/
private parseVariantProperties(componentName: string): Record<string, string> {
const properties: Record<string, string> = {};
try {
// Split by comma to get individual property=value pairs
const pairs = componentName.split(',').map(pair => pair.trim());
pairs.forEach(pair => {
const [property, value] = pair.split('=').map(str => str.trim());
if (property && value) {
properties[property] = value;
}
});
// If no properties found, treat the whole name as a single property
if (Object.keys(properties).length === 0) {
properties['variant'] = componentName;
}
} catch (error) {
// Fallback: treat component name as single variant property
properties['variant'] = componentName;
}
return properties;
}
/**
* Determine which variant should be considered the default
*/
determineDefaultVariant(variants: ComponentVariant[]): ComponentVariant | null {
if (variants.length === 0) {
return null;
}
// Strategy 1: Look for variants with "default" in name or properties
const defaultByName = variants.find(variant =>
variant.name.toLowerCase().includes('default') ||
Object.values(variant.properties).some(value =>
value.toLowerCase().includes('default')
)
);
if (defaultByName) {
return defaultByName;
}
// Strategy 2: Look for variants with "primary" in name or properties
const primaryVariant = variants.find(variant =>
variant.name.toLowerCase().includes('primary') ||
Object.values(variant.properties).some(value =>
value.toLowerCase().includes('primary')
)
);
if (primaryVariant) {
return primaryVariant;
}
// Strategy 3: Look for common default-like values
const defaultValues = ['normal', 'regular', 'medium', 'enabled', 'active', 'standard'];
const defaultByValue = variants.find(variant =>
Object.values(variant.properties).some(value =>
defaultValues.includes(value.toLowerCase())
)
);
if (defaultByValue) {
return defaultByValue;
}
// Strategy 4: Look for first variant that has state=enabled, type=primary, etc.
const prioritizedVariant = variants.find(variant => {
const props = variant.properties;
return (
(props.state && props.state.toLowerCase() === 'enabled') ||
(props.type && props.type.toLowerCase() === 'primary') ||
(props.size && props.size.toLowerCase() === 'medium')
);
});
if (prioritizedVariant) {
return prioritizedVariant;
}
// Fallback: Return first variant
return variants[0];
}
/**
* Check if variant selection should be prompted to user
*/
shouldPromptForVariantSelection(variants: ComponentVariant[]): boolean {
return variants.length > 3;
}
/**
* Get variant selection prompt information
*/
getVariantSelectionInfo(variants: ComponentVariant[]): {
totalCount: number;
variantNames: string[];
variantProperties: Record<string, Set<string>>;
defaultVariant?: ComponentVariant;
} {
const variantNames = variants.map(v => v.name);
const defaultVariant = variants.find(v => v.isDefault);
// Collect all unique property keys and their possible values
const variantProperties: Record<string, Set<string>> = {};
variants.forEach(variant => {
Object.entries(variant.properties).forEach(([key, value]) => {
if (!variantProperties[key]) {
variantProperties[key] = new Set();
}
variantProperties[key].add(value);
});
});
return {
totalCount: variants.length,
variantNames,
variantProperties,
defaultVariant
};
}
/**
* Filter variants by user selection criteria
*/
filterVariantsBySelection(
variants: ComponentVariant[],
selection: {
variantNames?: string[];
properties?: Record<string, string>;
includeDefault?: boolean;
}
): ComponentVariant[] {
let filtered = variants;
// Filter by variant names if specified
if (selection.variantNames && selection.variantNames.length > 0) {
filtered = filtered.filter(variant =>
selection.variantNames!.some(name =>
variant.name.toLowerCase().includes(name.toLowerCase())
)
);
}
// Filter by specific properties if specified
if (selection.properties && Object.keys(selection.properties).length > 0) {
filtered = filtered.filter(variant => {
return Object.entries(selection.properties!).every(([key, value]) => {
return variant.properties[key] &&
variant.properties[key].toLowerCase() === value.toLowerCase();
});
});
}
// Include default if requested
if (selection.includeDefault) {
const defaultVariant = variants.find(v => v.isDefault);
if (defaultVariant && !filtered.includes(defaultVariant)) {
filtered.push(defaultVariant);
}
}
return filtered;
}
/**
* Compare variants to identify differences
* Useful for understanding what changes between variants
*/
compareVariants(variants: ComponentVariant[]): {
commonProperties: Record<string, string>;
differentProperties: Record<string, Set<string>>;
uniqueProperties: Record<string, Record<string, string>>;
} {
const commonProperties: Record<string, string> = {};
const differentProperties: Record<string, Set<string>> = {};
const uniqueProperties: Record<string, Record<string, string>> = {};
if (variants.length === 0) {
return {commonProperties, differentProperties, uniqueProperties};
}
// Get all property keys from all variants
const allPropertyKeys = new Set<string>();
variants.forEach(variant => {
Object.keys(variant.properties).forEach(key => allPropertyKeys.add(key));
});
// Analyze each property
allPropertyKeys.forEach(propertyKey => {
const values = new Set<string>();
const variantsByValue: Record<string, string[]> = {};
variants.forEach(variant => {
const value = variant.properties[propertyKey];
if (value) {
values.add(value);
if (!variantsByValue[value]) {
variantsByValue[value] = [];
}
variantsByValue[value].push(variant.name);
}
});
// If all variants have the same value for this property, it's common
if (values.size === 1 && variants.every(v => v.properties[propertyKey])) {
commonProperties[propertyKey] = Array.from(values)[0];
}
// If variants have different values, it's a differentiating property
else if (values.size > 1) {
differentProperties[propertyKey] = values;
}
// If only some variants have this property, it's unique to those variants
else {
variants.forEach(variant => {
if (variant.properties[propertyKey]) {
if (!uniqueProperties[variant.name]) {
uniqueProperties[variant.name] = {};
}
uniqueProperties[variant.name][propertyKey] = variant.properties[propertyKey];
}
});
}
});
return {commonProperties, differentProperties, uniqueProperties};
}
/**
* Generate summary of variant analysis
*/
generateVariantSummary(variants: ComponentVariant[]): string {
if (variants.length === 0) {
return 'No variants found in component set.';
}
const defaultVariant = variants.find(v => v.isDefault);
const comparison = this.compareVariants(variants);
let summary = `Found ${variants.length} variants:\n`;
// List all variants
variants.forEach((variant, index) => {
const defaultMark = variant.isDefault ? ' (default)' : '';
summary += `${index + 1}. ${variant.name}${defaultMark}\n`;
});
// Show differentiating properties
if (Object.keys(comparison.differentProperties).length > 0) {
summary += '\nVariant properties:\n';
Object.entries(comparison.differentProperties).forEach(([prop, values]) => {
summary += `- ${prop}: ${Array.from(values).join(', ')}\n`;
});
}
// Show common properties
if (Object.keys(comparison.commonProperties).length > 0) {
summary += '\nShared properties:\n';
Object.entries(comparison.commonProperties).forEach(([prop, value]) => {
summary += `- ${prop}: ${value}\n`;
});
}
return summary;
}
}