-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnormalizeNode.ts
More file actions
927 lines (814 loc) · 28.5 KB
/
normalizeNode.ts
File metadata and controls
927 lines (814 loc) · 28.5 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
925
926
927
// This file handles node normalization
import type { ProcessedFrame, NormalizedNode, DesignSystem } from './types';
/**
* Safely serialize Figma objects by converting symbols and functions to safe values
* This prevents serialization errors when passing data to the UI
*
* @param value - Any value to serialize
* @returns Serialized version safe for JSON stringification
*/
function safeSerialize(value: any): any {
if (value === null || value === undefined) return null;
if (typeof value === 'symbol') return 'MIXED';
if (typeof value === 'function') return 'FUNCTION';
if (typeof value === 'object') {
if (Array.isArray(value)) {
return value.map(v => safeSerialize(v));
}
const result: any = {};
for (const key in value) {
try {
const val = value[key];
if (typeof val !== 'function' && typeof val !== 'symbol') {
result[key] = safeSerialize(val);
}
} catch (e) {}
}
return result;
}
return value;
}
/**
* Extract design system from all frames
*/
export function extractDesignSystem(frames: FrameNode[]): DesignSystem {
const fontSizes = new Set<number>();
const fontFamilies = new Set<string>();
const fontWeights = new Set<string>();
const lineHeights = new Set<number>();
const colors = new Set<string>();
const spacingValues = new Set<number>();
const borderRadiusValues = new Set<number>();
const shadows = new Set<string>();
const colorUsage: { [key: string]: number } = {};
frames.forEach(frame => {
collectDesignTokens(frame, {
fontSizes,
fontFamilies,
fontWeights,
lineHeights,
colors,
spacingValues,
borderRadiusValues,
shadows,
colorUsage
});
});
// Determine color roles based on frequency and context
// This helps identify primary, secondary, background, and text colors
const colorEntries = Object.entries(colorUsage).sort((a, b) => b[1] - a[1]);
const colorRoles: any = {};
if (colorEntries.length > 0) {
// Most used color is likely primary or background
const topColor = colorEntries[0][0];
// Light colors are typically backgrounds, dark colors are typically primary/accent
if (isLightColor(topColor)) {
colorRoles.background = topColor;
// Find a dark color for text (good contrast with light background)
const darkColor = colorEntries.find(([c]) => !isLightColor(c));
if (darkColor) colorRoles.text = darkColor[0];
} else {
colorRoles.primary = topColor;
}
// Second most common might be accent/secondary
if (colorEntries.length > 1) {
colorRoles.secondary = colorEntries[1][0];
}
}
// Check design consistency
const consistencyWarnings = checkDesignConsistency({
fontSizes: Array.from(fontSizes),
colors: Array.from(colors),
spacing: Array.from(spacingValues),
});
return {
typography: {
fontFamilies: Array.from(fontFamilies),
fontSizes: Array.from(fontSizes).sort((a, b) => a - b),
fontWeights: Array.from(fontWeights),
lineHeights: Array.from(lineHeights).sort((a, b) => a - b),
},
colors: Array.from(colors),
spacing: Array.from(spacingValues).sort((a, b) => a - b),
borderRadius: Array.from(borderRadiusValues).filter(v => v > 0).sort((a, b) => a - b),
shadows: Array.from(shadows),
colorRoles,
consistencyWarnings,
};
}
/**
* Check for design consistency issues
*/
function checkDesignConsistency(tokens: any): any[] {
const warnings: any[] = [];
// Check for similar but different colors
const colors = tokens.colors;
const similarColors: any[] = [];
for (let i = 0; i < colors.length; i++) {
for (let j = i + 1; j < colors.length; j++) {
const similarity = colorSimilarity(colors[i], colors[j]);
if (similarity > 0.85 && similarity < 1.0) {
similarColors.push({
color1: colors[i],
color2: colors[j],
similarity: Math.round(similarity * 100) + '%'
});
}
}
}
if (similarColors.length > 0) {
warnings.push({
type: 'Colors',
message: `Found ${similarColors.length} pairs of similar colors that could be consolidated`,
examples: similarColors.slice(0, 3)
});
}
// Check for font size gaps
const fontSizes = tokens.fontSizes.sort((a: number, b: number) => a - b);
const gaps: any[] = [];
for (let i = 1; i < fontSizes.length; i++) {
const gap = fontSizes[i] - fontSizes[i - 1];
if (gap > 8) {
gaps.push({
from: fontSizes[i - 1],
to: fontSizes[i],
gap: gap
});
}
}
if (gaps.length > 0) {
warnings.push({
type: 'Typography',
message: `Large gaps in font size scale may break visual hierarchy`,
gaps: gaps
});
}
// Check for inconsistent spacing
const spacing = tokens.spacing.filter((s: number) => s > 0);
if (spacing.length > 15) {
warnings.push({
type: 'Spacing',
message: `${spacing.length} different spacing values detected. Consider using a more limited scale (8-12 values)`
});
}
return warnings;
}
/**
* Calculate color similarity using RGB distance
* Returns a value between 0 (completely different) and 1 (identical)
* Used to detect near-duplicate colors that could be consolidated
*
* @param hex1 - First color in hex format (#RRGGBB)
* @param hex2 - Second color in hex format (#RRGGBB)
* @returns Similarity score from 0-1
*/
function colorSimilarity(hex1: string, hex2: string): number {
const r1 = parseInt(hex1.slice(1, 3), 16);
const g1 = parseInt(hex1.slice(3, 5), 16);
const b1 = parseInt(hex1.slice(5, 7), 16);
const r2 = parseInt(hex2.slice(1, 3), 16);
const g2 = parseInt(hex2.slice(3, 5), 16);
const b2 = parseInt(hex2.slice(5, 7), 16);
const rDiff = Math.abs(r1 - r2) / 255;
const gDiff = Math.abs(g1 - g2) / 255;
const bDiff = Math.abs(b1 - b2) / 255;
return 1 - (rDiff + gDiff + bDiff) / 3;
}
/**
* Determine if a color is light or dark using perceived luminance
* Uses the standard luminance formula: 0.299*R + 0.587*G + 0.114*B
* Threshold of 0.7 (70%) classifies as light
*
* @param hex - Color in hex format (#RRGGBB)
* @returns true if color is light, false if dark
*/
function isLightColor(hex: string): boolean {
const r = parseInt(hex.slice(1, 3), 16);
const g = parseInt(hex.slice(3, 5), 16);
const b = parseInt(hex.slice(5, 7), 16);
const luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255;
return luminance > 0.7;
}
/**
* Recursively traverse node tree and collect all design tokens
* Extracts: typography, colors, spacing, borders, shadows, effects
* Also tracks color usage frequency for semantic role detection
*
* @param node - The Figma node to traverse
* @param tokens - Object containing Sets to collect tokens into
*/
function collectDesignTokens(node: SceneNode, tokens: any) {
// Extract text properties
if (node.type === 'TEXT') {
const textNode = node as TextNode;
if (typeof textNode.fontSize === 'number') {
tokens.fontSizes.add(textNode.fontSize);
}
if (typeof textNode.fontName === 'object' && textNode.fontName.family) {
tokens.fontFamilies.add(textNode.fontName.family);
tokens.fontWeights.add(textNode.fontName.style);
}
if (typeof textNode.lineHeight === 'object' && textNode.lineHeight.unit === 'PIXELS') {
tokens.lineHeights.add(textNode.lineHeight.value);
}
}
// Extract spacing from auto layout
if ('layoutMode' in node && (node as any).layoutMode !== 'NONE') {
const frameNode = node as FrameNode;
tokens.spacingValues.add(frameNode.itemSpacing);
tokens.spacingValues.add(frameNode.paddingTop);
tokens.spacingValues.add(frameNode.paddingRight);
tokens.spacingValues.add(frameNode.paddingBottom);
tokens.spacingValues.add(frameNode.paddingLeft);
}
// Extract border radius
if ('cornerRadius' in node && typeof (node as any).cornerRadius === 'number') {
tokens.borderRadiusValues.add((node as any).cornerRadius);
}
// Extract shadows/effects
if ('effects' in node) {
const effects = (node as any).effects;
if (Array.isArray(effects)) {
effects.forEach((effect: any) => {
if (effect.visible && effect.type === 'DROP_SHADOW') {
const shadowStr = `${effect.offset.x}px ${effect.offset.y}px ${effect.radius}px ${rgbToHex(effect.color)}`;
tokens.shadows.add(shadowStr);
}
});
}
}
// Extract colors from fills and track usage
if ('fills' in node) {
const fills = (node as any).fills;
if (Array.isArray(fills)) {
fills.forEach((fill: any) => {
if (fill.type === 'SOLID' && fill.color) {
const hex = rgbToHex(fill.color);
tokens.colors.add(hex);
tokens.colorUsage[hex] = (tokens.colorUsage[hex] || 0) + 1;
}
});
}
}
// Recurse through children
if ('children' in node) {
(node as any).children.forEach((child: SceneNode) => collectDesignTokens(child, tokens));
}
}
/**
* Convert Figma RGB color object (0-1 range) to hex string
* Figma uses 0-1 range, we convert to 0-255 and then to hex
*
* @param color - Figma RGB color object with r, g, b properties (0-1 range)
* @returns Hex color string (#RRGGBB)
*/
function rgbToHex(color: RGB): 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')}`;
}
/**
* Normalize a FRAME node - simplified for flow context
*/
export function normalizeFrame(frame: FrameNode): ProcessedFrame {
const interactions = detectInteractions(frame);
const screenPurpose = inferScreenPurpose(frame);
const contentSummary = extractContentSummary(frame);
const layoutStructure = analyzeLayoutStructure(frame);
const componentPatterns = detectComponentPatterns(frame);
const visualTheme = extractVisualTheme(frame);
const responsiveHints = generateResponsiveHints(frame);
const accessibilityIssues = checkAccessibility(frame, contentSummary, interactions);
const contentGuidelines = analyzeContent(frame, contentSummary);
return {
id: frame.id,
name: frame.name,
width: frame.width,
height: frame.height,
purpose: screenPurpose,
interactions: interactions,
layoutMode: safeSerialize(frame.layoutMode),
contentSummary: contentSummary,
layoutStructure: layoutStructure,
componentPatterns: componentPatterns,
visualTheme: visualTheme,
responsiveHints: responsiveHints,
accessibilityIssues: accessibilityIssues,
contentGuidelines: contentGuidelines,
};
}
/**
* Generate responsive design hints based on layout
*/
function generateResponsiveHints(frame: FrameNode): any {
const hints: any = {
breakpointSuggestion: null,
stackingRecommendations: [],
fluidElements: [],
fixedElements: [],
};
// Infer device type from width
if (frame.width <= 375) {
hints.breakpointSuggestion = 'Mobile (small)';
} else if (frame.width <= 768) {
hints.breakpointSuggestion = 'Mobile/Tablet';
} else if (frame.width <= 1024) {
hints.breakpointSuggestion = 'Tablet/Small Desktop';
} else {
hints.breakpointSuggestion = 'Desktop';
}
// Analyze layout for responsive behavior
frame.children.forEach((child: SceneNode) => {
if ('width' in child && 'layoutMode' in child) {
const childWidth = (child as any).width;
const widthRatio = childWidth / frame.width;
// Full-width elements are fluid
if (widthRatio > 0.9) {
hints.fluidElements.push({
name: child.name,
suggestion: 'Should remain full-width on all screen sizes'
});
}
// Narrow elements might be fixed
else if (childWidth < 300) {
hints.fixedElements.push({
name: child.name,
width: Math.round(childWidth),
suggestion: 'Consider fixed width or min-width'
});
}
// Horizontal layouts should stack on mobile
if ((child as any).layoutMode === 'HORIZONTAL') {
hints.stackingRecommendations.push({
name: child.name,
suggestion: 'Stack vertically on mobile/tablet (<768px)'
});
}
}
});
return hints;
}
/**
* Check accessibility issues
*/
function checkAccessibility(frame: FrameNode, contentSummary: any, interactions: any[]): any[] {
const issues: any[] = [];
// Check heading hierarchy
const headings = contentSummary.headings || [];
if (headings.length > 3) {
issues.push({
type: 'info',
category: 'Heading Hierarchy',
message: `Use proper heading levels (H1 → H2 → H3). Ensure only one H1 per page.`
});
}
// Check touch target sizes
const smallTargets = interactions.filter((int: any) => {
return int.details && int.details.height && int.details.height < 44;
});
if (smallTargets.length > 0) {
issues.push({
type: 'warning',
category: 'Touch Targets',
message: `${smallTargets.length} interactive element(s) smaller than 44px height. Increase size for better mobile usability.`,
elements: smallTargets.map((t: any) => t.details.name)
});
}
// Check color contrast (simplified - would need actual text colors)
issues.push({
type: 'info',
category: 'Color Contrast',
message: 'Ensure text has 4.5:1 contrast ratio against backgrounds (WCAG AA standard)'
});
// Check for alt text reminder
issues.push({
type: 'info',
category: 'Images',
message: 'Add alt text for all images for screen reader accessibility'
});
return issues;
}
/**
* Analyze content for guidelines
*/
function analyzeContent(frame: FrameNode, contentSummary: any): any {
const guidelines: any = {
placeholderContent: [],
characterLimits: [],
dynamicContent: [],
};
// Detect placeholder content
const allText = [
...(contentSummary.headings || []),
...(contentSummary.keyText || []),
...(contentSummary.inputPlaceholders || [])
];
allText.forEach((text: string) => {
const textLower = text.toLowerCase();
// Common placeholder patterns
if (textLower.includes('lorem') ||
textLower.includes('placeholder') ||
textLower.includes('xxx') ||
textLower.match(/^(title|heading|text|label|description)$/i)) {
guidelines.placeholderContent.push(text);
}
// Suggest character limits for truncation
if (text.length > 50) {
guidelines.characterLimits.push({
text: text.substring(0, 40) + '...',
length: text.length,
suggestion: 'Consider truncation with ellipsis if exceeds container width'
});
}
});
// Detect dynamic content indicators
contentSummary.sections?.forEach((section: string) => {
const sectionLower = section.toLowerCase();
if (sectionLower.includes('list') ||
sectionLower.includes('feed') ||
sectionLower.includes('grid') ||
sectionLower.includes('items')) {
guidelines.dynamicContent.push({
section: section,
type: 'list/collection',
suggestion: 'This content should be dynamically generated from data'
});
}
});
return guidelines;
}
/**
* Detect reusable component patterns (cards, forms, nav bars)
*/
function detectComponentPatterns(frame: FrameNode): any[] {
const patterns: any[] = [];
const components = new Map<string, number>();
// Find repeated component instances
function traverse(node: SceneNode) {
if (node.type === 'INSTANCE' || node.type === 'COMPONENT') {
const name = node.name.toLowerCase();
components.set(name, (components.get(name) || 0) + 1);
// Detect common patterns
if (name.includes('card')) {
patterns.push({ type: 'Card', name: node.name, count: 1 });
} else if (name.includes('nav') || name.includes('header')) {
patterns.push({ type: 'Navigation', name: node.name });
} else if (name.includes('form') || name.includes('input group')) {
patterns.push({ type: 'Form Section', name: node.name });
} else if (name.includes('modal') || name.includes('dialog')) {
patterns.push({ type: 'Modal/Dialog', name: node.name });
} else if (name.includes('list') || name.includes('item')) {
patterns.push({ type: 'List Item', name: node.name });
}
}
if ('children' in node) {
(node as any).children.forEach(traverse);
}
}
traverse(frame);
// Find repeated patterns
components.forEach((count, name) => {
if (count > 1) {
patterns.push({
type: 'Repeated Component',
name: name,
count: count,
suggestion: 'This should be a reusable component'
});
}
});
return patterns;
}
/**
* Extract overall visual theme from frame
*/
function extractVisualTheme(frame: FrameNode): any {
const theme: any = {
maxContentWidth: null,
commonBorderRadius: null,
elevationLevels: [],
};
// Check for centered max-width content
frame.children.forEach((child: SceneNode) => {
if ('width' in child && 'x' in child) {
const childWidth = (child as any).width;
const childX = (child as any).x;
const isCentered = Math.abs((childX + childWidth / 2) - (frame.width / 2)) < frame.width * 0.05;
if (isCentered && childWidth < frame.width * 0.95) {
if (!theme.maxContentWidth || childWidth > theme.maxContentWidth) {
theme.maxContentWidth = Math.round(childWidth);
}
}
}
});
return theme;
}
/**
* Analyze layout structure and positioning
*/
function analyzeLayoutStructure(frame: FrameNode): any {
const structure: any = {
regions: {
top: [],
middle: [],
bottom: [],
},
positioning: [],
hierarchy: []
};
const frameHeight = frame.height;
const frameWidth = frame.width;
const topThreshold = frameHeight * 0.2;
const bottomThreshold = frameHeight * 0.8;
// Analyze direct children
frame.children.forEach((child: SceneNode) => {
if (child.type === 'FRAME' || child.type === 'COMPONENT' || child.type === 'INSTANCE') {
const node = child as FrameNode;
const yPos = node.y;
const xPos = node.x;
const isFullWidth = node.width > frameWidth * 0.9;
const isCentered = Math.abs((xPos + node.width / 2) - (frameWidth / 2)) < frameWidth * 0.1;
const regionInfo: any = {
name: node.name,
width: Math.round(node.width),
height: Math.round(node.height),
fullWidth: isFullWidth,
centered: isCentered,
};
// Add layout mode if available
if ('layoutMode' in node && node.layoutMode) {
regionInfo.layout = node.layoutMode;
}
// Add positioning info
if ('layoutPositioning' in node && node.layoutPositioning === 'ABSOLUTE') {
regionInfo.positioning = 'absolute';
structure.positioning.push({
name: node.name,
x: Math.round(xPos),
y: Math.round(yPos),
});
}
// Categorize by vertical region
if (yPos < topThreshold) {
structure.regions.top.push(regionInfo);
} else if (yPos > bottomThreshold) {
structure.regions.bottom.push(regionInfo);
} else {
structure.regions.middle.push(regionInfo);
}
// Build hierarchy
if (node.children && node.children.length > 0) {
structure.hierarchy.push({
parent: node.name,
childCount: node.children.length,
layout: 'layoutMode' in node ? node.layoutMode : null,
});
}
}
});
return structure;
}
/**
* Infer screen purpose from frame name and content
*/
function inferScreenPurpose(frame: FrameNode): string {
const nameLower = frame.name.toLowerCase();
if (nameLower.includes('login') || nameLower.includes('sign in')) {
return 'authentication';
}
if (nameLower.includes('signup') || nameLower.includes('register') || nameLower.includes('sign up')) {
return 'registration';
}
if (nameLower.includes('onboard')) {
return 'onboarding';
}
if (nameLower.includes('home') || nameLower.includes('dashboard') || nameLower.includes('main')) {
return 'main-screen';
}
if (nameLower.includes('profile') || nameLower.includes('account')) {
return 'profile';
}
if (nameLower.includes('settings') || nameLower.includes('preferences')) {
return 'settings';
}
if (nameLower.includes('list') || nameLower.includes('browse')) {
return 'list-view';
}
if (nameLower.includes('detail') || nameLower.includes('view')) {
return 'detail-view';
}
if (nameLower.includes('checkout') || nameLower.includes('payment')) {
return 'checkout';
}
if (nameLower.includes('success') || nameLower.includes('confirmation')) {
return 'confirmation';
}
return 'screen';
}
/**
* Extract content summary organized by component type
*/
function extractContentSummary(frame: FrameNode): any {
const headings: string[] = [];
const bodyText: string[] = [];
const inputFields: string[] = [];
const sections: string[] = [];
function traverse(node: SceneNode, depth: number = 0) {
const nameLower = node.name.toLowerCase();
// Identify major sections/containers
if (depth === 1 && 'children' in node && (node as any).children.length > 3) {
sections.push(node.name);
}
// Extract text by apparent hierarchy
if (node.type === 'TEXT') {
const textNode = node as TextNode;
const text = textNode.characters.trim();
if (!text || text.length < 2) return; // Skip empty or single char
const fontSize = typeof textNode.fontSize === 'number' ? textNode.fontSize : 16;
// Categorize by size and name
if (fontSize >= 24 || nameLower.includes('heading') || nameLower.includes('title')) {
headings.push(text);
} else if (nameLower.includes('input') || nameLower.includes('placeholder')) {
inputFields.push(text);
} else if (text.length > 5) { // Meaningful body text
bodyText.push(text);
}
}
// Recurse
if ('children' in node) {
(node as any).children.forEach((child: SceneNode) => traverse(child, depth + 1));
}
}
traverse(frame);
return {
sections: [...new Set(sections)].slice(0, 5),
headings: [...new Set(headings)].slice(0, 5),
keyText: [...new Set(bodyText)].slice(0, 8),
inputPlaceholders: [...new Set(inputFields)].slice(0, 5),
};
}
export function normalizeNode(node: SceneNode): NormalizedNode {
// Simplified - we don't need detailed node structure anymore
return {
id: node.id,
name: node.name,
type: node.type,
};
}
/**
* Detect interactive elements and potential navigation patterns
*/
/**
* Detect interactive elements within a frame using heuristics
* Identifies buttons, inputs, links, toggles, and icons based on:
* - Node naming patterns ("button", "input", "link", etc.)
* - Visual characteristics (fills, borders, sizing)
* - Component instances
* - Text content patterns
*
* Limits recursion depth to avoid capturing nested UI noise
*
* @param node - The Figma node to analyze
* @returns Array of detected interactive elements with type and description
*/
function detectInteractions(node: SceneNode): any[] {
const interactions: any[] = [];
function getNodeDetails(n: SceneNode): any {
const details: any = {
name: n.name,
};
// Extract visual properties
if ('fills' in n && Array.isArray((n as any).fills) && (n as any).fills.length > 0) {
const fill = (n as any).fills[0];
if (fill.type === 'SOLID' && fill.color) {
details.backgroundColor = rgbToHex(fill.color);
details.backgroundOpacity = fill.opacity !== undefined ? fill.opacity : 1;
}
}
// Get dimensions if available
if ('width' in n && 'height' in n) {
details.width = Math.round((n as any).width);
details.height = Math.round((n as any).height);
}
// Check for corner radius
if ('cornerRadius' in n && (n as any).cornerRadius) {
details.cornerRadius = (n as any).cornerRadius;
}
// Check for borders/strokes
if ('strokes' in n && Array.isArray((n as any).strokes) && (n as any).strokes.length > 0) {
const stroke = (n as any).strokes[0];
if (stroke.type === 'SOLID' && stroke.color) {
details.borderColor = rgbToHex(stroke.color);
details.borderWidth = (n as any).strokeWeight || 1;
}
}
// Check for shadows
if ('effects' in n) {
const effects = (n as any).effects;
if (Array.isArray(effects)) {
const shadow = effects.find((e: any) => e.visible && e.type === 'DROP_SHADOW');
if (shadow) {
details.shadow = `${shadow.offset.x}px ${shadow.offset.y}px ${shadow.radius}px`;
}
}
}
return details;
}
function traverse(n: SceneNode, depth: number = 0) {
const nameLower = n.name.toLowerCase();
// Categorize interactive elements
const isButton = nameLower.includes('button') || nameLower.includes('btn') ||
nameLower.includes('cta') || nameLower.includes('action');
const isInput = nameLower.includes('input') || nameLower.includes('field') ||
nameLower.includes('text field') || nameLower.includes('search');
const isLink = nameLower.includes('link') || nameLower.includes('nav');
const isIcon = nameLower.includes('icon') && depth < 3;
const isToggle = nameLower.includes('toggle') || nameLower.includes('switch') ||
nameLower.includes('checkbox');
// Detect component instances (often buttons/interactive elements)
const isComponent = n.type === 'INSTANCE';
if (isButton || isComponent || isInput || isLink || isIcon || isToggle) {
const details = getNodeDetails(n);
// Extract text content
let textContent = '';
let placeholder = '';
if (n.type === 'TEXT') {
textContent = (n as TextNode).characters.trim();
} else if ('children' in n) {
const textNodes = findTextNodes(n as any);
if (textNodes.length > 0) {
textContent = textNodes.map(t => t.characters.trim()).filter(t => t).join(' ');
}
}
// Determine element type and create description
let elementType = 'interactive element';
let description = '';
if (isButton) {
elementType = 'Button';
const style = details.cornerRadius ? `${details.cornerRadius}px radius` : 'square';
const size = details.height ? (details.height > 50 ? 'large' : details.height > 35 ? 'medium' : 'small') : 'medium';
description = `${size} button with ${style}`;
if (details.backgroundColor) {
description += `, bg: ${details.backgroundColor}`;
}
if (details.shadow) {
description += `, elevated (shadow)`;
}
if (textContent) {
description += ` - text: "${textContent}"`;
}
} else if (isInput) {
elementType = 'Input Field';
const width = details.width ? (details.width > 300 ? 'full-width' : 'standard') : 'standard';
description = `${width} text input`;
if (textContent) {
placeholder = textContent;
description += ` with placeholder "${textContent}"`;
}
} else if (isLink) {
elementType = 'Link';
description = textContent ? `"${textContent}"` : 'text link';
} else if (isIcon) {
elementType = 'Icon Button';
description = `icon`;
if (textContent) {
description += ` (${textContent})`;
}
} else if (isToggle) {
elementType = 'Toggle';
description = textContent ? `toggle: "${textContent}"` : 'toggle control';
} else if (isComponent) {
elementType = 'Component';
description = textContent || n.name;
}
interactions.push({
type: elementType,
description: description,
label: textContent,
details: details,
});
}
// Recursively check children (but limit depth to avoid noise)
if ('children' in n && depth < 4) {
(n as any).children.forEach((child: SceneNode) => traverse(child, depth + 1));
}
}
traverse(node, 0);
return interactions;
}
/**
* Find all text nodes recursively
*/
function findTextNodes(node: SceneNode): TextNode[] {
const textNodes: TextNode[] = [];
if (node.type === 'TEXT') {
textNodes.push(node as TextNode);
}
if ('children' in node) {
(node as any).children.forEach((child: SceneNode) => {
textNodes.push(...findTextNodes(child));
});
}
return textNodes;
}