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
| class CodeGenerator { constructor(framework = 'react') { this.framework = framework; this.componentRegistry = new ComponentRegistry(); this.codeTemplates = this.loadTemplates(framework); }
async generateCode(designStructure, options = {}) { const codePieces = [];
const mainComponent = this.generateMainComponent(designStructure, options); codePieces.push(mainComponent);
const childComponents = await this.generateChildComponents(designStructure, options); codePieces.push(...childComponents);
const styles = this.generateStyles(designStructure, options); codePieces.push(styles);
if (options.typescript) { const types = this.generateTypes(designStructure); codePieces.push(types); }
return this.combineCodePieces(codePieces); }
generateMainComponent(designStructure, options) { const componentName = this.inferComponentName(designStructure); const imports = this.generateImports(designStructure); const jsx = this.generateJSX(designStructure);
const componentTemplate = this.codeTemplates.mainComponent;
return componentTemplate .replace('{{componentName}}', componentName) .replace('{{imports}}', imports) .replace('{{jsx}}', jsx); }
generateJSX(designStructure) { let jsx = '';
for (const element of designStructure.hierarchy) { jsx += this.createElementJSX(element, 0); }
return jsx; }
createElementJSX(element, depth = 0) { const indent = ' '.repeat(depth); const elementType = this.mapElementType(element.semanticType); const attributes = this.generateElementAttributes(element); const styleProps = this.generateStyleProps(element);
let jsx = `${indent}<${elementType}`;
if (attributes) { jsx += ` ${attributes}`; }
if (styleProps) { jsx += ` ${styleProps}`; }
if (element.children && element.children.length > 0) { jsx += '>\n'; for (const child of element.children) { jsx += this.createElementJSX(child, depth + 1); } jsx += `${indent}</${elementType}>\n`; } else { jsx += ' />\n'; }
return jsx; }
mapElementType(semanticType) { const elementMap = { 'header': 'header', 'navigation': 'nav', 'main': 'main', 'section': 'section', 'article': 'article', 'aside': 'aside', 'footer': 'footer', 'button': 'button', 'input': 'input', 'label': 'label', 'image': 'img', 'text': 'p', 'heading': (level) => `h${level}`, 'list': 'ul', 'list-item': 'li', 'form': 'form', 'container': 'div' };
if (semanticType.type === 'heading') { return elementMap.heading(semanticType.level || 2); }
return elementMap[semanticType.type] || 'div'; }
generateStyleProps(element) { const styles = element.styles; const styleProps = {};
if (styles.backgroundColor) { styleProps.bgColor = styles.backgroundColor; }
if (styles.fontSize) { styleProps.fontSize = styles.fontSize; }
if (styles.color) { styleProps.color = styles.color; }
if (styles.margin) { styleProps.m = this.normalizeSpacing(styles.margin); }
if (styles.padding) { styleProps.p = this.normalizeSpacing(styles.padding); }
if (Object.keys(styleProps).length === 0) { return ''; }
const propsString = Object.entries(styleProps) .map(([key, value]) => `${key}="${value}"`) .join(' ');
return propsString; }
normalizeSpacing(spacing) { const spacingScale = [0, 4, 8, 12, 16, 24, 32, 48, 64];
return spacingScale.reduce((prev, curr) => Math.abs(curr - spacing) < Math.abs(prev - spacing) ? curr : prev ); } }
class TemplateSystem { constructor() { this.templates = { react: { mainComponent: `import React from 'react';\n{{imports}}\n\nconst {{componentName}} = () => {\n return (\n{{jsx}} );\n};\n\nexport default {{componentName}};`, component: `const {{componentName}} = ({ children, ...props }) => {\n return (\n <div {...props}>\n {children}\n </div>\n );\n};`, styles: `import styled from 'styled-components';\n\nexport const Styled{{componentName}} = styled.div\`\n {{cssRules}}\n\`;` }, vue: { mainComponent: `<template>\n{{jsx}}\n</template>\n\n<script>\n{{imports}}\n\nexport default {\n name: '{{componentName}}',\n components: {\n // component registrations\n }\n};\n</script>\n\n<style scoped>\n{{cssRules}}\n</style>`, }, svelte: { mainComponent: `<script>\n{{imports}}\n</script>\n\n{{jsx}}\n\n<style>\n{{cssRules}}\n</style>`, } }; } }
|