Skip to content

Commit ca0acea

Browse files
committed
completed code generation for next.js
1 parent 8355006 commit ca0acea

File tree

7 files changed

+171
-177
lines changed

7 files changed

+171
-177
lines changed

app/src/components/main/DirectChildComponent.tsx

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ function DirectChildComponent({
9494
return (
9595
<IndirectChild
9696
// combine styles of instance and referenced component. instance styles have higher priority
97+
key={
98+
'indChild' + child.childId.toString() + child.typeId.toString()
99+
}
97100
style={combinedStyle}
98101
placeHolder=""
99102
linkId={null}
@@ -121,12 +124,22 @@ function DirectChildComponent({
121124
style={combinedStyle}
122125
placeHolder={HTMLDefaultPlacholder}
123126
linkId={null}
127+
key={
128+
'indChild' +
129+
child.childId.toString() +
130+
child.typeId.toString()
131+
}
124132
/>
125133
) : (
126134
<IndirectChild
127135
style={combinedStyle}
128136
placeHolder={HTMLDefaultPlacholder}
129137
linkId={null}
138+
key={
139+
'indChild' +
140+
child.childId.toString() +
141+
child.typeId.toString()
142+
}
130143
>
131144
{renderIndirectChildren(child)}
132145
</IndirectChild>
@@ -136,16 +149,24 @@ function DirectChildComponent({
136149
} else if (child.type === 'Route Link') {
137150
return (
138151
<IndirectChild
152+
key={
153+
'indChild' + child.childId.toString() + child.typeId.toString()
154+
}
139155
style={combinedStyle}
140-
placeHolder={''}
156+
placeHolder=""
141157
linkId={child.typeId}
142158
/>
143159
);
144160
}
145161
});
146162
};
147163
return (
148-
<div onClick={onClickHandler} style={combinedStyle} ref={drag}>
164+
<div
165+
onClick={onClickHandler}
166+
key={'childComp' + childId}
167+
style={combinedStyle}
168+
ref={drag}
169+
>
149170
{renderIndirectChildren(referencedComponent)}
150171
</div>
151172
);

app/src/components/main/IndirectChild.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ function IndirectChild({ style, children, placeHolder, linkId }) {
2424
combinedStyle = combineStyles(combinedStyle, { color: 'blue' });
2525
}
2626

27+
console.log('children are ', children);
28+
console.log('place holder is ', placeHolder);
2729
return (
2830
<div style={combinedStyle}>
2931
{linkId ? (
3032
<div onClick={onClickHandlerRoute}>{linkName}</div>
3133
) : (
32-
{ placeHolder }
34+
placeHolder
3335
)}
3436
{children}
3537
</div>

app/src/helperFunctions/generateCode.ts

Lines changed: 91 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
1+
12
import { Component, State, ChildElement } from '../interfaces/InterfacesNew';
23
import HTMLTypes from '../context/HTMLTypes';
34

5+
46
// generate code based on the component heirarchy
5-
const generateUnformattedCode = (comps: Component[], componentId: number) => {
7+
const generateUnformattedCode = (
8+
comps: Component[],
9+
componentId: number,
10+
rootComponents: number[],
11+
projectType: string
12+
) => {
613
const components = [...comps];
714
// find the component that we're going to generate code for
815
const currentComponent = components.find(elem => elem.id === componentId);
916
// find the unique components that we need to import into this component file
1017
let imports: any = [];
18+
let links: boolean = false;
19+
20+
const isRoot = rootComponents.includes(componentId);
1121

12-
// get metadata for each child
22+
// get metadata for each child (e.g. the name/tag of the component/elemnt)
1323
const getEnrichedChildren = (currentComponent: Component | ChildElement) => {
24+
console.log('enriching component ', currentComponent);
1425
const enrichedChildren = currentComponent.children.map((elem: any) => {
1526
const child = { ...elem };
1627
if (child.type === 'Component') {
@@ -28,14 +39,22 @@ const generateUnformattedCode = (comps: Component[], componentId: number) => {
2839
child.children = getEnrichedChildren(child);
2940
}
3041
return child;
42+
} else if (child.type === 'Route Link') {
43+
links = true;
44+
child.name = components.find(
45+
(comp: Component) => comp.id === child.typeId
46+
).name;
47+
return child;
3148
}
3249
});
3350
return enrichedChildren;
3451
};
3552

53+
// write all code that will be under the "return" of the component
3654
const writeNestedElements = (enrichedChildren: any) => {
3755
return `${enrichedChildren
3856
.map((child: any) => {
57+
console.log('the child is ', child);
3958
if (child.type === 'Component') {
4059
return `<${child.name}${formatStyles(child.style)} />`;
4160
} else if (child.type === 'HTML Element') {
@@ -79,6 +98,12 @@ const generateUnformattedCode = (comps: Component[], componentId: number) => {
7998
return `<${child.tag}${formatStyles(child.style)}></${child.tag}>`;
8099
}
81100
}
101+
// route links are only a next.js feature. if the user creates a rotue link and then switches projects, generate code for a normal link instead
102+
else if (child.type === 'Route Link') {
103+
return projectType === 'Next.js'
104+
? `<Link href="/${child.name}"><a>${child.name}</a></Link>`
105+
: `<a>${child.name}</a>`;
106+
}
82107
})
83108
.join('\n')}`;
84109
};
@@ -96,19 +121,35 @@ const generateUnformattedCode = (comps: Component[], componentId: number) => {
96121
const enrichedChildren: any = getEnrichedChildren(currentComponent);
97122

98123
console.log(enrichedChildren);
124+
const next = true;
125+
126+
// import statements differ between root (pages) and regular components (components)
127+
const importsMapped =
128+
projectType === 'Next.js'
129+
? imports
130+
.map((comp: string) => {
131+
return isRoot
132+
? `import ${comp} from '../components/${comp}'`
133+
: `import ${comp} from './${comp}'`;
134+
})
135+
.join('\n')
136+
: imports
137+
.map((comp: string) => {
138+
return `import ${comp} from './${comp}.tsx'`;
139+
})
140+
.join('\n');
99141

100142
const stateful = true;
101143
const classBased = false;
102144

103-
return `
145+
// create final component code. component code differs between classic react and next.js
146+
// classic react code
147+
if (projectType !== 'Next.js') {
148+
return `
104149
${stateful && !classBased ? `import React, {useState} from 'react';` : ''}
105150
${classBased ? `import React, {Component} from 'react';` : ''}
106151
${!stateful && !classBased ? `import React from 'react';` : ''}
107-
${imports
108-
.map((comp: string) => {
109-
return `import ${comp} from './${comp}.tsx'`;
110-
})
111-
.join('\n')}
152+
${importsMapped}
112153
113154
${
114155
classBased
@@ -131,23 +172,48 @@ const generateUnformattedCode = (comps: Component[], componentId: number) => {
131172
132173
${classBased ? `render(): JSX.Element {` : ``}
133174
175+
return (
176+
<div className="${currentComponent.name}" style={props.style}>
177+
${writeNestedElements(enrichedChildren)}
178+
</div>
179+
);
180+
}
181+
${classBased ? `}` : ``}
182+
export default ${currentComponent.name};
183+
`;
184+
}
185+
// next.js component code
186+
else {
187+
return `
188+
import React, { useState } from 'react';
189+
${importsMapped}
190+
import Head from 'next/head'
191+
${links ? `import Link from 'next/link'` : ``}
134192
193+
const ${currentComponent.name} = (props) => {
194+
195+
const [value, setValue] = useState("INITIAL VALUE");
135196
136197
return (
198+
<>
199+
<Head>
200+
<title>${currentComponent.name}</title>
201+
</Head>
137202
<div className="${currentComponent.name}" style={props.style}>
138203
${writeNestedElements(enrichedChildren)}
139204
</div>
205+
</>
140206
);
141207
}
142-
${classBased ? `}` : ``}
208+
143209
export default ${currentComponent.name};
144210
`;
211+
}
145212
};
146213

147214
// formats code with prettier linter
148215
const formatCode = (code: string) => {
149216
return window.api.formatCode(code);
150-
// return code;
151217
// return format(code, {
152218
// singleQuote: true,
153219
// trailingComma: 'es5',
@@ -158,10 +224,21 @@ const formatCode = (code: string) => {
158224
};
159225

160226
// generate code based on component heirarchy and then return the rendered code
161-
const generateCode = (components: Component[], componentId: number) => {
162-
return '';
163-
// const code = generateUnformattedCode(components, componentId);
164-
// return formatCode(code);
227+
const generateCode = (
228+
components: Component[],
229+
componentId: number,
230+
rootComponents: number[],
231+
projectType: string
232+
) => {
233+
console.log('generating next.js code');
234+
// return ''
235+
const code = generateUnformattedCode(
236+
components,
237+
componentId,
238+
rootComponents,
239+
projectType
240+
);
241+
return formatCode(code);
165242
};
166243

167244
export default generateCode;

app/src/helperFunctions/generateNextCode.ts

Lines changed: 0 additions & 136 deletions
This file was deleted.

0 commit comments

Comments
 (0)