forked from mhmzdev/figma-flutter-mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.ts
More file actions
106 lines (93 loc) · 3.19 KB
/
core.ts
File metadata and controls
106 lines (93 loc) · 3.19 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
// src/extractors/components/core.mts
import type {FigmaNode} from '../../types/figma.js';
import type {
ComponentAnalysis,
ComponentExtractionOptions,
ComponentVariant
} from './types.js';
import {
extractMetadata,
extractLayoutInfo,
extractStylingInfo,
analyzeChildren,
} from './extractor.js';
/**
* Component extraction and analysis class
*/
export class ComponentExtractor {
private options: Required<ComponentExtractionOptions>;
constructor(options: ComponentExtractionOptions = {}) {
this.options = {
maxChildNodes: options.maxChildNodes ?? 10,
maxDepth: options.maxDepth ?? 3,
includeHiddenNodes: options.includeHiddenNodes ?? false,
prioritizeComponents: options.prioritizeComponents ?? true,
extractTextContent: options.extractTextContent ?? true
};
}
/**
* Main component analysis method
*/
async analyzeComponent(node: FigmaNode, userDefinedAsComponent: boolean = false): Promise<ComponentAnalysis> {
const metadata = extractMetadata(node, userDefinedAsComponent);
const layout = extractLayoutInfo(node);
const styling = extractStylingInfo(node);
const {children, nestedComponents, skippedNodes} = analyzeChildren(node, this.options);
return {
metadata,
layout,
styling,
children,
nestedComponents,
skippedNodes: skippedNodes.length > 0 ? skippedNodes : undefined
};
}
/**
* Get extractor options
*/
getOptions(): Required<ComponentExtractionOptions> {
return this.options;
}
}
/**
* Convenience function to analyze a single component
*/
export async function analyzeComponent(
node: FigmaNode,
options: ComponentExtractionOptions = {},
userDefinedAsComponent: boolean = false
): Promise<ComponentAnalysis> {
const extractor = new ComponentExtractor(options);
return extractor.analyzeComponent(node, userDefinedAsComponent);
}
/**
* Convenience function to analyze component with variants
*/
export async function analyzeComponentWithVariants(
componentSetNode: FigmaNode,
options: ComponentExtractionOptions = {}
): Promise<{
variants: ComponentVariant[];
defaultAnalysis?: ComponentAnalysis;
}> {
if (componentSetNode.type !== 'COMPONENT_SET') {
throw new Error('Node is not a COMPONENT_SET');
}
const {VariantAnalyzer} = await import('./variant-analyzer.js');
const variantAnalyzer = new VariantAnalyzer();
const variants = await variantAnalyzer.analyzeComponentSet(componentSetNode);
// Find and analyze the default variant
const defaultVariant = variants.find(v => v.isDefault);
let defaultAnalysis: ComponentAnalysis | undefined;
if (defaultVariant && componentSetNode.children) {
const defaultVariantNode = componentSetNode.children.find(child => child.id === defaultVariant.nodeId);
if (defaultVariantNode) {
const extractor = new ComponentExtractor(options);
defaultAnalysis = await extractor.analyzeComponent(defaultVariantNode, false);
}
}
return {
variants,
defaultAnalysis
};
}