forked from mhmzdev/figma-flutter-mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextractor.ts
More file actions
924 lines (784 loc) · 29.1 KB
/
extractor.ts
File metadata and controls
924 lines (784 loc) · 29.1 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
// src/extractors/components/extractor.mts
import type {FigmaNode, FigmaColor, FigmaEffect} from '../../types/figma.js';
import type {
ComponentMetadata,
LayoutInfo,
StylingInfo,
ComponentChild,
NestedComponentInfo,
SkippedNodeInfo,
CategorizedEffects,
ColorInfo,
StrokeInfo,
CornerRadii,
PaddingInfo,
TextInfo,
ComponentExtractionOptions
} from './types.js';
import { detectSemanticTypeAdvanced, generateSemanticContext } from '../../tools/flutter/semantic-detection.js';
/**
* Extract component metadata
*/
export function extractMetadata(node: FigmaNode, userDefinedAsComponent: boolean): ComponentMetadata {
const metadata: ComponentMetadata = {
name: node.name,
type: node.type as 'COMPONENT' | 'COMPONENT_SET' | 'FRAME',
nodeId: node.id,
isUserDefinedComponent: userDefinedAsComponent
};
// Add component-specific metadata
if (node.type === 'COMPONENT' || node.type === 'COMPONENT_SET') {
metadata.componentKey = (node as any).componentKey;
}
if (node.type === 'COMPONENT_SET') {
metadata.variantCount = node.children?.length || 0;
}
return metadata;
}
/**
* Extract layout information
*/
export function extractLayoutInfo(node: FigmaNode): LayoutInfo {
const layout: LayoutInfo = {
type: determineLayoutType(node),
dimensions: {
width: node.absoluteBoundingBox?.width || 0,
height: node.absoluteBoundingBox?.height || 0
}
};
// Auto-layout specific properties
if (node.layoutMode) {
layout.direction = node.layoutMode === 'HORIZONTAL' ? 'horizontal' : 'vertical';
layout.spacing = node.itemSpacing || 0;
// Extract padding
if (hasPadding(node)) {
layout.padding = extractPadding(node);
}
// Alignment properties
layout.alignItems = (node as any).primaryAxisAlignItems;
layout.justifyContent = (node as any).counterAxisAlignItems;
}
// Constraints
if (node.constraints) {
layout.constraints = node.constraints;
}
return layout;
}
/**
* Extract styling information
*/
export function extractStylingInfo(node: FigmaNode): StylingInfo {
const styling: StylingInfo = {};
// Fills (background colors/gradients)
if (node.fills && node.fills.length > 0) {
styling.fills = node.fills.map(convertFillToColorInfo);
}
// Strokes (borders)
if (node.strokes && node.strokes.length > 0) {
styling.strokes = node.strokes.map(convertStrokeInfo);
}
// Effects (shadows, blurs)
if (node.effects && node.effects.length > 0) {
styling.effects = categorizeEffects(node.effects);
}
// Corner radius
const cornerRadius = extractCornerRadius(node);
if (cornerRadius) {
styling.cornerRadius = cornerRadius;
}
// Opacity
if ((node as any).opacity !== undefined && (node as any).opacity !== 1) {
styling.opacity = (node as any).opacity;
}
return styling;
}
/**
* Analyze child nodes with prioritization and limits
*/
export function analyzeChildren(
node: FigmaNode,
options: Required<ComponentExtractionOptions>
): {
children: ComponentChild[];
nestedComponents: NestedComponentInfo[];
skippedNodes: SkippedNodeInfo[];
} {
const children: ComponentChild[] = [];
const nestedComponents: NestedComponentInfo[] = [];
const skippedNodes: SkippedNodeInfo[] = [];
if (!node.children || node.children.length === 0) {
return {children, nestedComponents, skippedNodes};
}
// Filter visible nodes unless includeHiddenNodes is true
let visibleChildren = node.children;
if (!options.includeHiddenNodes) {
visibleChildren = node.children.filter(child => child.visible !== false);
}
// Calculate visual importance for all children
const childrenWithImportance = visibleChildren.map(child => ({
node: child,
importance: calculateVisualImportance(child),
isComponent: isComponentNode(child)
}));
// Sort by importance (components first if prioritized, then by visual importance)
childrenWithImportance.sort((a, b) => {
if (options.prioritizeComponents) {
if (a.isComponent && !b.isComponent) return -1;
if (!a.isComponent && b.isComponent) return 1;
}
return b.importance - a.importance;
});
// Process up to maxChildNodes
const processedCount = Math.min(childrenWithImportance.length, options.maxChildNodes);
for (let i = 0; i < childrenWithImportance.length; i++) {
const {node: child, importance, isComponent} = childrenWithImportance[i];
if (i < processedCount) {
// Check if this is a nested component
if (isComponent) {
nestedComponents.push(createNestedComponentInfo(child));
}
// Pass parent and siblings for better semantic detection
const siblings = visibleChildren.filter(sibling => sibling.id !== child.id);
children.push(createComponentChild(child, importance, isComponent, options, node, siblings));
} else {
// Track skipped nodes
skippedNodes.push({
nodeId: child.id,
name: child.name,
type: child.type,
reason: 'max_nodes'
});
}
}
return {children, nestedComponents, skippedNodes};
}
/**
* Create nested component information
*/
export function createNestedComponentInfo(node: FigmaNode): NestedComponentInfo {
return {
nodeId: node.id,
name: node.name,
componentKey: (node as any).componentKey,
masterComponent: (node as any).masterComponent?.key,
isComponentInstance: node.type === 'INSTANCE',
needsSeparateAnalysis: true,
instanceType: node.type === 'INSTANCE' ? 'COMPONENT' : node.type as 'COMPONENT' | 'COMPONENT_SET'
};
}
/**
* Create component child information
*/
export function createComponentChild(
node: FigmaNode,
importance: number,
isNestedComponent: boolean,
options: Required<ComponentExtractionOptions>,
parent?: FigmaNode,
siblings?: FigmaNode[]
): ComponentChild {
const child: ComponentChild = {
nodeId: node.id,
name: node.name,
type: node.type,
isNestedComponent,
visualImportance: importance
};
// Extract basic info for non-component children
if (!isNestedComponent) {
child.basicInfo = {
layout: extractBasicLayout(node),
styling: extractBasicStyling(node)
};
// Extract text info for text nodes
if (node.type === 'TEXT' && options.extractTextContent) {
child.basicInfo.text = extractTextInfo(node, parent, siblings);
}
}
return child;
}
/**
* Calculate visual importance score (1-10)
*/
export function calculateVisualImportance(node: FigmaNode): number {
let score = 0;
// Size importance (0-4 points)
const area = (node.absoluteBoundingBox?.width || 0) * (node.absoluteBoundingBox?.height || 0);
if (area > 10000) score += 4;
else if (area > 5000) score += 3;
else if (area > 1000) score += 2;
else if (area > 100) score += 1;
// Type importance (0-3 points)
if (node.type === 'COMPONENT' || node.type === 'INSTANCE') score += 3;
else if (node.type === 'FRAME') score += 2;
else if (node.type === 'TEXT') score += 2;
else if (node.type === 'VECTOR') score += 1;
// Styling importance (0-2 points)
if (node.fills && node.fills.length > 0) score += 1;
if (node.effects && node.effects.length > 0) score += 1;
// Has children importance (0-1 point)
if (node.children && node.children.length > 0) score += 1;
return Math.min(score, 10);
}
/**
* Check if node is a component
*/
export function isComponentNode(node: FigmaNode): boolean {
return node.type === 'COMPONENT' || node.type === 'INSTANCE' || node.type === 'COMPONENT_SET';
}
/**
* Determine layout type from node properties
*/
export function determineLayoutType(node: FigmaNode): 'auto-layout' | 'absolute' | 'frame' {
if (node.layoutMode) {
return 'auto-layout';
}
if (node.type === 'FRAME' || node.type === 'COMPONENT') {
return 'frame';
}
return 'absolute';
}
/**
* Check if node has padding
*/
export function hasPadding(node: FigmaNode): boolean {
return !!(node.paddingTop || node.paddingRight || node.paddingBottom || node.paddingLeft);
}
/**
* Extract padding information
*/
export function extractPadding(node: FigmaNode): PaddingInfo {
const top = node.paddingTop || 0;
const right = node.paddingRight || 0;
const bottom = node.paddingBottom || 0;
const left = node.paddingLeft || 0;
return {
top,
right,
bottom,
left,
isUniform: top === right && right === bottom && bottom === left
};
}
/**
* Convert fill to color info
*/
export function convertFillToColorInfo(fill: any): ColorInfo {
const colorInfo: ColorInfo = {
type: fill.type,
opacity: fill.opacity
};
if (fill.color) {
colorInfo.color = fill.color;
colorInfo.hex = rgbaToHex(fill.color);
}
if (fill.gradientStops) {
colorInfo.gradientStops = fill.gradientStops;
}
return colorInfo;
}
/**
* Convert stroke to stroke info
*/
export function convertStrokeInfo(stroke: any): StrokeInfo {
return {
type: stroke.type,
color: stroke.color,
hex: rgbaToHex(stroke.color),
weight: (stroke as any).strokeWeight || 1,
align: (stroke as any).strokeAlign
};
}
/**
* Categorize effects for Flutter mapping
*/
export function categorizeEffects(effects: FigmaEffect[]): CategorizedEffects {
const categorized: CategorizedEffects = {
dropShadows: [],
innerShadows: [],
blurs: []
};
effects.forEach(effect => {
if (effect.type === 'DROP_SHADOW' && effect.visible !== false) {
categorized.dropShadows.push({
color: effect.color!,
hex: rgbaToHex(effect.color!),
offset: effect.offset || {x: 0, y: 0},
radius: effect.radius,
spread: effect.spread,
opacity: effect.color?.a || 1
});
} else if (effect.type === 'INNER_SHADOW' && effect.visible !== false) {
categorized.innerShadows.push({
color: effect.color!,
hex: rgbaToHex(effect.color!),
offset: effect.offset || {x: 0, y: 0},
radius: effect.radius,
spread: effect.spread,
opacity: effect.color?.a || 1
});
} else if ((effect.type === 'LAYER_BLUR' || effect.type === 'BACKGROUND_BLUR') && effect.visible !== false) {
categorized.blurs.push({
type: effect.type,
radius: effect.radius
});
}
});
return categorized;
}
/**
* Extract corner radius
*/
export function extractCornerRadius(node: FigmaNode): number | CornerRadii | undefined {
const nodeAny = node as any;
if (nodeAny.cornerRadius !== undefined) {
return nodeAny.cornerRadius;
}
// Check for individual corner radii
if (nodeAny.rectangleCornerRadii && Array.isArray(nodeAny.rectangleCornerRadii)) {
const [topLeft, topRight, bottomRight, bottomLeft] = nodeAny.rectangleCornerRadii;
const isUniform = topLeft === topRight && topRight === bottomRight && bottomRight === bottomLeft;
if (isUniform) {
return topLeft;
}
return {
topLeft,
topRight,
bottomLeft,
bottomRight,
isUniform: false
};
}
return undefined;
}
/**
* Extract basic layout info for non-component children
*/
export function extractBasicLayout(node: FigmaNode): Partial<LayoutInfo> {
return {
type: determineLayoutType(node),
dimensions: {
width: node.absoluteBoundingBox?.width || 0,
height: node.absoluteBoundingBox?.height || 0
}
};
}
/**
* Extract basic styling info for non-component children
*/
export function extractBasicStyling(node: FigmaNode): Partial<StylingInfo> {
const styling: Partial<StylingInfo> = {};
if (node.fills && node.fills.length > 0) {
styling.fills = node.fills.slice(0, 1).map(convertFillToColorInfo); // Limit to primary fill
}
const cornerRadius = extractCornerRadius(node);
if (cornerRadius) {
styling.cornerRadius = cornerRadius;
}
return styling;
}
/**
* Extract enhanced text information
*/
export function extractTextInfo(node: FigmaNode, parent?: FigmaNode, siblings?: FigmaNode[]): TextInfo | undefined {
if (node.type !== 'TEXT') return undefined;
const textContent = getActualTextContent(node);
const isPlaceholder = isPlaceholderText(textContent);
return {
content: textContent,
isPlaceholder,
fontFamily: node.style?.fontFamily,
fontSize: node.style?.fontSize,
fontWeight: node.style?.fontWeight,
textAlign: node.style?.textAlignHorizontal,
textCase: detectTextCase(textContent),
semanticType: detectSemanticType(textContent, node.name, node, parent, siblings),
placeholder: isPlaceholder
};
}
/**
* Get actual text content from various sources
*/
function getActualTextContent(node: FigmaNode): string {
// 1. Primary source: characters property (official Figma API text content)
if (node.characters && node.characters.trim().length > 0) {
return node.characters.trim();
}
// 2. Check fills for text content (sometimes stored in fill metadata)
if (node.fills) {
for (const fill of node.fills) {
if ((fill as any).textData || (fill as any).content) {
const textContent = (fill as any).textData || (fill as any).content;
if (textContent && textContent.trim().length > 0) {
return textContent.trim();
}
}
}
}
// 3. Check for text in component properties (for component instances)
if (node.type === 'INSTANCE' && (node as any).componentProperties) {
const textProps = extractTextFromComponentProperties((node as any).componentProperties);
if (textProps && textProps.trim().length > 0) {
return textProps.trim();
}
}
// 4. Analyze node name for meaningful content
const nodeName = node.name;
// If node name looks like actual content (not generic), use it
if (isLikelyActualContent(nodeName)) {
return nodeName;
}
// 5. Fallback to node name with placeholder flag
return nodeName;
}
/**
* Check if node name looks like actual content vs generic label
*/
function isLikelyActualContent(name: string): boolean {
const genericPatterns = [
/^text$/i,
/^label$/i,
/^heading$/i,
/^title$/i,
/^body\s*\d*$/i,
/^text\s*\d+$/i,
/^heading\s*\d+$/i,
/^h\d+$/i,
/^lorem\s+ipsum/i,
/^sample\s+text/i,
/^placeholder/i,
/^example\s+text/i,
/^demo\s+text/i,
/^text\s*layer/i,
/^component\s*\d+/i
];
// If it matches generic patterns, it's probably not actual content
if (genericPatterns.some(pattern => pattern.test(name))) {
return false;
}
// If it's very short and common UI text, it might be actual content
const shortUIText = ['ok', 'yes', 'no', 'save', 'cancel', 'close', 'menu', 'home', 'back', 'next', 'login', 'signup'];
if (name.length <= 8 && shortUIText.includes(name.toLowerCase())) {
return true;
}
// If it contains real words and is reasonably long, likely actual content
if (name.length > 3 && name.length < 100) {
// Check if it has word-like structure
const hasWords = /\b[a-zA-Z]{2,}\b/.test(name);
const hasSpaces = name.includes(' ');
if (hasWords && (hasSpaces || name.length > 8)) {
return true;
}
}
return false;
}
/**
* Check if text content is placeholder/dummy text
*/
function isPlaceholderText(content: string): boolean {
if (!content || content.trim().length === 0) {
return true;
}
const trimmedContent = content.trim().toLowerCase();
// Common placeholder patterns
const placeholderPatterns = [
/lorem\s+ipsum/i,
/dolor\s+sit\s+amet/i,
/consectetur\s+adipiscing/i,
/the\s+quick\s+brown\s+fox/i,
/sample\s+text/i,
/placeholder/i,
/example\s+text/i,
/demo\s+text/i,
/test\s+content/i,
/dummy\s+text/i,
/text\s+goes\s+here/i,
/your\s+text\s+here/i,
/add\s+text\s+here/i,
/enter\s+text/i,
/\[.*\]/, // Text in brackets like [Your text here]
/^heading\s*\d*$/i,
/^title\s*\d*$/i,
/^body\s*\d*$/i,
/^text\s*\d*$/i,
/^label\s*\d*$/i,
/^h[1-6]$/i,
/^paragraph$/i,
/^caption$/i,
/^subtitle$/i,
/^overline$/i
];
// Check against placeholder patterns
if (placeholderPatterns.some(pattern => pattern.test(content))) {
return true;
}
// Check for generic single words that are likely placeholders
const genericWords = [
'text', 'label', 'title', 'heading', 'body', 'content',
'description', 'subtitle', 'caption', 'paragraph', 'copy'
];
if (genericWords.includes(trimmedContent)) {
return true;
}
// Check for repeated characters (like "AAAA" or "xxxx")
if (content.length > 2 && /^(.)\1+$/.test(content.trim())) {
return true;
}
// Check for Lorem Ipsum variations
if (/lorem|ipsum|dolor|sit|amet|consectetur|adipiscing|elit/i.test(content)) {
return true;
}
return false;
}
/**
* Extract text from component properties
*/
function extractTextFromComponentProperties(properties: any): string | null {
if (!properties || typeof properties !== 'object') {
return null;
}
// Look for common text property names
const textPropertyNames = ['text', 'label', 'title', 'content', 'value', 'caption'];
for (const propName of textPropertyNames) {
if (properties[propName] && typeof properties[propName] === 'string') {
return properties[propName];
}
}
// Look for any string property that might contain text
for (const [key, value] of Object.entries(properties)) {
if (typeof value === 'string' && value.length > 0 && !isPlaceholderText(value)) {
return value;
}
}
return null;
}
/**
* Detect text case pattern
*/
function detectTextCase(content: string): 'uppercase' | 'lowercase' | 'capitalize' | 'sentence' | 'mixed' {
if (content.length === 0) return 'mixed';
const isAllUpper = content === content.toUpperCase() && content !== content.toLowerCase();
const isAllLower = content === content.toLowerCase() && content !== content.toUpperCase();
if (isAllUpper) return 'uppercase';
if (isAllLower) return 'lowercase';
// Check if it's title case (first letter of each word capitalized)
const words = content.split(/\s+/);
const isTitleCase = words.every(word => {
return word.length === 0 || word[0] === word[0].toUpperCase();
});
if (isTitleCase) return 'capitalize';
// Check if it's sentence case (first letter capitalized, rest normal)
if (content[0] === content[0].toUpperCase()) {
return 'sentence';
}
return 'mixed';
}
/**
* Detect semantic type of text based on content and context
* Enhanced with multi-factor analysis and confidence scoring
*/
function detectSemanticType(
content: string,
nodeName: string,
node?: any,
parent?: any,
siblings?: any[]
): 'heading' | 'body' | 'label' | 'button' | 'link' | 'caption' | 'error' | 'success' | 'warning' | 'other' {
// Skip detection for placeholder text
if (isPlaceholderText(content)) {
return 'other';
}
// Use advanced semantic detection if node properties are available
if (node) {
try {
const context = generateSemanticContext(node, parent, siblings);
const classification = detectSemanticTypeAdvanced(content, nodeName, context, node);
// Only use advanced classification if confidence is high enough
if (classification.confidence >= 0.6) {
return classification.type;
}
// Log reasoning for debugging (in development)
if (process.env.NODE_ENV === 'development') {
console.debug(`Low confidence (${classification.confidence}) for "${content}": ${classification.reasoning.join(', ')}`);
}
} catch (error) {
// Fall back to legacy detection if advanced detection fails
console.warn('Advanced semantic detection failed, using legacy method:', error);
}
}
// Legacy detection as fallback
return detectSemanticTypeLegacy(content, nodeName);
}
/**
* Legacy semantic type detection (fallback)
*/
function detectSemanticTypeLegacy(content: string, nodeName: string): 'heading' | 'body' | 'label' | 'button' | 'link' | 'caption' | 'error' | 'success' | 'warning' | 'other' {
const lowerContent = content.toLowerCase().trim();
const lowerNodeName = nodeName.toLowerCase();
// Button text patterns - exact matches for common button labels
const buttonPatterns = [
/^(click|tap|press|submit|send|save|cancel|ok|yes|no|continue|next|back|close|done|finish|start|begin)$/i,
/^(login|log in|sign in|signup|sign up|register|logout|log out|sign out)$/i,
/^(buy|purchase|add|remove|delete|edit|update|create|new|get started|learn more|try now)$/i,
/^(download|upload|share|copy|paste|cut|undo|redo|refresh|reload|search|filter|sort|apply|reset|clear)$/i,
/^(accept|decline|agree|disagree|confirm|verify|validate|approve|reject)$/i
];
if (buttonPatterns.some(pattern => pattern.test(content)) || lowerNodeName.includes('button')) {
return 'button';
}
// Error/status text patterns - more comprehensive detection
const errorPatterns = /error|invalid|required|missing|failed|wrong|incorrect|forbidden|unauthorized|not found|unavailable|expired|timeout/i;
const successPatterns = /success|completed|done|saved|updated|created|uploaded|downloaded|sent|delivered|confirmed|verified|approved/i;
const warningPatterns = /warning|caution|note|important|attention|notice|alert|reminder|tip|info|information/i;
if (errorPatterns.test(content)) {
return 'error';
}
if (successPatterns.test(content)) {
return 'success';
}
if (warningPatterns.test(content)) {
return 'warning';
}
// Link patterns - navigation and informational links
const linkPatterns = [
/^(learn more|read more|see more|view all|show all|details|click here|view details)$/i,
/^(about|contact|help|support|faq|terms|privacy|policy|documentation|docs|guide|tutorial)$/i,
/^(home|dashboard|profile|settings|preferences|account|billing|notifications|security)$/i
];
if (linkPatterns.some(pattern => pattern.test(content)) || lowerNodeName.includes('link')) {
return 'link';
}
// Heading patterns - based on structure and context
if (content.length < 80 && !content.endsWith('.') && !content.includes('\n')) {
if (lowerNodeName.includes('heading') || lowerNodeName.includes('title') || /h[1-6]/.test(lowerNodeName)) {
return 'heading';
}
// Check if it looks like a title (short, starts with capital, no sentence punctuation)
if (content.length < 50 && /^[A-Z]/.test(content) && !/[.!?]$/.test(content)) {
return 'heading';
}
}
// Label patterns - form labels and descriptive text
if (content.length < 40 && (content.endsWith(':') || lowerNodeName.includes('label') || lowerNodeName.includes('field'))) {
return 'label';
}
// Caption patterns - short descriptive text
if (content.length < 120 && (
lowerNodeName.includes('caption') ||
lowerNodeName.includes('subtitle') ||
lowerNodeName.includes('description') ||
lowerNodeName.includes('meta')
)) {
return 'caption';
}
// Body text - longer content, paragraphs
if (content.length > 80 || content.includes('\n') || content.includes('. ')) {
return 'body';
}
return 'other';
}
/**
* Generate Flutter widget suggestion based on semantic type and text info
*/
export function generateFlutterTextWidget(textInfo: TextInfo): string {
// Escape single quotes in content for Dart strings
const escapedContent = textInfo.content.replace(/'/g, "\\'");
if (textInfo.isPlaceholder) {
return `Text('${escapedContent}') // TODO: Replace with actual content`;
}
// Generate style properties based on text info
const styleProps: string[] = [];
if (textInfo.fontFamily) {
styleProps.push(`fontFamily: '${textInfo.fontFamily}'`);
}
if (textInfo.fontSize) {
styleProps.push(`fontSize: ${textInfo.fontSize}`);
}
if (textInfo.fontWeight && textInfo.fontWeight !== 400) {
const fontWeight = textInfo.fontWeight >= 700 ? 'FontWeight.bold' :
textInfo.fontWeight >= 600 ? 'FontWeight.w600' :
textInfo.fontWeight >= 500 ? 'FontWeight.w500' :
textInfo.fontWeight <= 300 ? 'FontWeight.w300' : 'FontWeight.normal';
styleProps.push(`fontWeight: ${fontWeight}`);
}
const customStyle = styleProps.length > 0 ? `TextStyle(${styleProps.join(', ')})` : null;
switch (textInfo.semanticType) {
case 'button':
return `ElevatedButton(\n onPressed: () {\n // TODO: Implement button action\n },\n child: Text('${escapedContent}'),\n)`;
case 'link':
return `TextButton(\n onPressed: () {\n // TODO: Implement navigation\n },\n child: Text('${escapedContent}'),\n)`;
case 'heading':
const headingStyle = customStyle || 'Theme.of(context).textTheme.headlineMedium';
return `Text(\n '${escapedContent}',\n style: ${headingStyle},\n)`;
case 'body':
const bodyStyle = customStyle || 'Theme.of(context).textTheme.bodyMedium';
return `Text(\n '${escapedContent}',\n style: ${bodyStyle},\n)`;
case 'caption':
const captionStyle = customStyle || 'Theme.of(context).textTheme.bodySmall';
return `Text(\n '${escapedContent}',\n style: ${captionStyle},\n)`;
case 'label':
const labelStyle = customStyle || 'Theme.of(context).textTheme.labelMedium';
return `Text(\n '${escapedContent}',\n style: ${labelStyle},\n)`;
case 'error':
const errorStyle = customStyle ?
`${customStyle.slice(0, -1)}, color: Theme.of(context).colorScheme.error)` :
'TextStyle(color: Theme.of(context).colorScheme.error)';
return `Text(\n '${escapedContent}',\n style: ${errorStyle},\n)`;
case 'success':
const successStyle = customStyle ?
`${customStyle.slice(0, -1)}, color: Colors.green)` :
'TextStyle(color: Colors.green)';
return `Text(\n '${escapedContent}',\n style: ${successStyle},\n)`;
case 'warning':
const warningStyle = customStyle ?
`${customStyle.slice(0, -1)}, color: Colors.orange)` :
'TextStyle(color: Colors.orange)';
return `Text(\n '${escapedContent}',\n style: ${warningStyle},\n)`;
default:
return customStyle ?
`Text(\n '${escapedContent}',\n style: ${customStyle},\n)` :
`Text('${escapedContent}')`;
}
}
/**
* Get all text content from a component tree
*/
export function extractAllTextContent(node: FigmaNode): Array<{nodeId: string, textInfo: TextInfo, widgetSuggestion: string}> {
const textNodes: Array<{nodeId: string, textInfo: TextInfo, widgetSuggestion: string}> = [];
traverseForText(node, textNodes);
return textNodes;
}
/**
* Recursively traverse node tree to find all text nodes
*/
function traverseForText(
node: FigmaNode,
results: Array<{nodeId: string, textInfo: TextInfo, widgetSuggestion: string}>,
depth: number = 0
): void {
if (depth > 5) return; // Prevent infinite recursion
if (node.type === 'TEXT') {
const textInfo = extractTextInfo(node);
if (textInfo) {
results.push({
nodeId: node.id,
textInfo,
widgetSuggestion: generateFlutterTextWidget(textInfo)
});
}
}
if (node.children) {
node.children.forEach(child => {
traverseForText(child, results, depth + 1);
});
}
}
/**
* Convert RGBA color to hex string
*/
export function rgbaToHex(color: FigmaColor): string {
const r = Math.round(color.r * 255);
const g = Math.round(color.g * 255);
const b = Math.round(color.b * 255);
return `#${r.toString(16).padStart(2, '0')}${g.toString(16).padStart(2, '0')}${b.toString(16).padStart(2, '0')}`.toUpperCase();
}