|
| 1 | +// src/extractors/components/deduplicated-extractor.mts |
| 2 | + |
| 3 | +import type { FigmaNode } from '../../types/figma.mjs'; |
| 4 | +import type { FlutterStyleDefinition } from '../flutter/style-library.mjs'; |
| 5 | +import { FlutterStyleLibrary } from '../flutter/style-library.mjs'; |
| 6 | +import { |
| 7 | + extractStylingInfo, |
| 8 | + extractLayoutInfo, |
| 9 | + extractMetadata, |
| 10 | + extractTextInfo, |
| 11 | + createNestedComponentInfo |
| 12 | +} from './extractor.mjs'; |
| 13 | +import type { |
| 14 | + ComponentMetadata, |
| 15 | + LayoutInfo, |
| 16 | + StylingInfo, |
| 17 | + NestedComponentInfo, |
| 18 | + TextInfo |
| 19 | +} from './types.mjs'; |
| 20 | + |
| 21 | +export interface DeduplicatedComponentAnalysis { |
| 22 | + metadata: ComponentMetadata; |
| 23 | + styleRefs: Record<string, string>; |
| 24 | + children: DeduplicatedComponentChild[]; |
| 25 | + nestedComponents: NestedComponentInfo[]; |
| 26 | + newStyleDefinitions?: Record<string, FlutterStyleDefinition>; |
| 27 | +} |
| 28 | + |
| 29 | +export interface DeduplicatedComponentChild { |
| 30 | + nodeId: string; |
| 31 | + name: string; |
| 32 | + type: string; |
| 33 | + styleRefs: string[]; |
| 34 | + semanticType?: string; |
| 35 | + textContent?: string; |
| 36 | +} |
| 37 | + |
| 38 | +export class DeduplicatedComponentExtractor { |
| 39 | + private styleLibrary = FlutterStyleLibrary.getInstance(); |
| 40 | + |
| 41 | + async analyzeComponent(node: FigmaNode, trackNewStyles = false): Promise<DeduplicatedComponentAnalysis> { |
| 42 | + const styling = extractStylingInfo(node); |
| 43 | + const layout = extractLayoutInfo(node); |
| 44 | + const metadata = extractMetadata(node, false); // assuming not user-defined unless specified |
| 45 | + |
| 46 | + const styleRefs: Record<string, string> = {}; |
| 47 | + const newStyles = new Set<string>(); |
| 48 | + |
| 49 | + // Process decoration styles |
| 50 | + if (this.hasDecorationProperties(styling)) { |
| 51 | + const beforeCount = this.styleLibrary.getAllStyles().length; |
| 52 | + styleRefs.decoration = this.styleLibrary.addStyle('decoration', { |
| 53 | + fills: styling.fills, |
| 54 | + cornerRadius: styling.cornerRadius, |
| 55 | + effects: styling.effects |
| 56 | + }); |
| 57 | + if (trackNewStyles && this.styleLibrary.getAllStyles().length > beforeCount) { |
| 58 | + newStyles.add(styleRefs.decoration); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // Process padding styles |
| 63 | + if (layout.padding) { |
| 64 | + const beforeCount = this.styleLibrary.getAllStyles().length; |
| 65 | + styleRefs.padding = this.styleLibrary.addStyle('padding', { padding: layout.padding }); |
| 66 | + if (trackNewStyles && this.styleLibrary.getAllStyles().length > beforeCount) { |
| 67 | + newStyles.add(styleRefs.padding); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // Process children with deduplication |
| 72 | + const children = await this.analyzeChildren(node); |
| 73 | + const nestedComponents = this.extractNestedComponents(node); |
| 74 | + |
| 75 | + const result: DeduplicatedComponentAnalysis = { |
| 76 | + metadata, |
| 77 | + styleRefs, |
| 78 | + children, |
| 79 | + nestedComponents |
| 80 | + }; |
| 81 | + |
| 82 | + if (trackNewStyles && newStyles.size > 0) { |
| 83 | + result.newStyleDefinitions = this.getStyleDefinitions(Array.from(newStyles)); |
| 84 | + } |
| 85 | + |
| 86 | + return result; |
| 87 | + } |
| 88 | + |
| 89 | + private async analyzeChildren(node: FigmaNode): Promise<DeduplicatedComponentChild[]> { |
| 90 | + if (!node.children) return []; |
| 91 | + |
| 92 | + const children: DeduplicatedComponentChild[] = []; |
| 93 | + |
| 94 | + for (const child of node.children) { |
| 95 | + if (!child.visible) continue; |
| 96 | + |
| 97 | + const childStyleRefs: string[] = []; |
| 98 | + |
| 99 | + // Extract child styling |
| 100 | + const childStyling = extractStylingInfo(child); |
| 101 | + if (this.hasDecorationProperties(childStyling)) { |
| 102 | + const decorationRef = this.styleLibrary.addStyle('decoration', { |
| 103 | + fills: childStyling.fills, |
| 104 | + cornerRadius: childStyling.cornerRadius, |
| 105 | + effects: childStyling.effects |
| 106 | + }); |
| 107 | + childStyleRefs.push(decorationRef); |
| 108 | + } |
| 109 | + |
| 110 | + // Extract text styling for text nodes |
| 111 | + let textContent: string | undefined; |
| 112 | + if (child.type === 'TEXT') { |
| 113 | + const textInfo = extractTextInfo(child); |
| 114 | + if (textInfo) { |
| 115 | + textContent = textInfo.content; |
| 116 | + |
| 117 | + // Add text style to library |
| 118 | + if (child.style) { |
| 119 | + const textStyleRef = this.styleLibrary.addStyle('text', { |
| 120 | + fontFamily: child.style.fontFamily, |
| 121 | + fontSize: child.style.fontSize, |
| 122 | + fontWeight: child.style.fontWeight |
| 123 | + }); |
| 124 | + childStyleRefs.push(textStyleRef); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + children.push({ |
| 130 | + nodeId: child.id, |
| 131 | + name: child.name, |
| 132 | + type: child.type, |
| 133 | + styleRefs: childStyleRefs, |
| 134 | + semanticType: this.detectSemanticType(child), |
| 135 | + textContent |
| 136 | + }); |
| 137 | + } |
| 138 | + |
| 139 | + return children; |
| 140 | + } |
| 141 | + |
| 142 | + private hasDecorationProperties(styling: StylingInfo): boolean { |
| 143 | + return !!(styling.fills?.length || styling.cornerRadius !== undefined || styling.effects?.dropShadows?.length); |
| 144 | + } |
| 145 | + |
| 146 | + private extractTextContent(node: any): string { |
| 147 | + return node.characters || node.name || ''; |
| 148 | + } |
| 149 | + |
| 150 | + private detectSemanticType(node: any): string | undefined { |
| 151 | + // Simplified semantic detection |
| 152 | + if (node.type === 'TEXT') { |
| 153 | + const content = this.extractTextContent(node).toLowerCase(); |
| 154 | + if (['click', 'submit', 'save', 'cancel'].some(word => content.includes(word))) { |
| 155 | + return 'button'; |
| 156 | + } |
| 157 | + return 'text'; |
| 158 | + } |
| 159 | + return undefined; |
| 160 | + } |
| 161 | + |
| 162 | + private extractNestedComponents(node: FigmaNode): NestedComponentInfo[] { |
| 163 | + if (!node.children) return []; |
| 164 | + |
| 165 | + const nestedComponents: NestedComponentInfo[] = []; |
| 166 | + |
| 167 | + for (const child of node.children) { |
| 168 | + if (child.type === 'COMPONENT' || child.type === 'INSTANCE' || child.type === 'COMPONENT_SET') { |
| 169 | + nestedComponents.push(createNestedComponentInfo(child)); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + return nestedComponents; |
| 174 | + } |
| 175 | + |
| 176 | + private getStyleDefinitions(styleIds: string[]): Record<string, FlutterStyleDefinition> { |
| 177 | + const definitions: Record<string, FlutterStyleDefinition> = {}; |
| 178 | + styleIds.forEach(id => { |
| 179 | + const definition = this.styleLibrary.getStyle(id); |
| 180 | + if (definition) { |
| 181 | + definitions[id] = definition; |
| 182 | + } |
| 183 | + }); |
| 184 | + return definitions; |
| 185 | + } |
| 186 | +} |
0 commit comments