Skip to content

Commit 75df4fd

Browse files
Removed Separator from Tree
Fixed Indentaiton in Code Preview Co-authored: Ron Fu [email protected]
1 parent ac24d84 commit 75df4fd

File tree

5 files changed

+70
-87
lines changed

5 files changed

+70
-87
lines changed

app/src/components/bottom/UseStateModal.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import StateContext from '../../context/context';
44
import TableStateProps from '../right/TableStateProps';
55

66
// TODO: typescript interface or type check
7-
function UseStateModal({ updateAttributeWithState, attributeToChange, childId }) {
7+
function UseStateModal({ updateAttributeWithState, deleteAttributeWithState, attributeToChange, childId }) {
88
const [state, dispatch] = useContext(StateContext);
99
const [open, setOpen] = useState(false);
1010

@@ -36,10 +36,11 @@ function UseStateModal({ updateAttributeWithState, attributeToChange, childId })
3636
<TableStateProps
3737
providerId = {componentProviderId}
3838
selectHandler={(table) => {
39+
console.log('componentProviderId = ',componentProviderId);
40+
console.log('table.row = ',table.row);
3941
updateAttributeWithState(attributeToChange, componentProviderId, table.row.id);
40-
setOpen(false);
4142
}}
42-
deleteHandler={() => func()}
43+
deleteHandler={() => deleteAttributeWithState()}
4344
isThemeLight={true}
4445
/>
4546
</div>

app/src/components/right/TableStateProps.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,8 @@ const getColumns = (props) => {
4646
renderCell: function renderCell(params:any) {
4747
const getIdRow = () => {
4848
const { api } = params;
49-
console.log('Line 49 params =', params);
50-
console.log('Line 50 id = ', params.id);
5149
// const fields = api.getAllColumns().map((c: any) => c.field).filter((c : any) => c !== '__check__' && !!c);
5250
return params.id;
53-
5451
// return params.getValue(fields[0]);
5552
};
5653
return (

app/src/containers/CustomizationPanel.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,20 @@ const CustomizationPanel = ({ isThemeLight }): JSX.Element => {
207207

208208
// function to pass into UseStateModal to use state to update attribute
209209
const updateAttributeWithState = (attributeName, componentProviderId, statePropsId) => {
210-
210+
console.log('attributeName = ',attributeName); //returns compText or compLink
211+
console.log('statePropsId = ', statePropsId);
211212
// get the stateProps of the componentProvider
212213
const currentComponent = state.components[componentProviderId - 1];
213214
const currentComponentProps = currentComponent.stateProps;
214215
const newInput = currentComponentProps[statePropsId - 1].value;
215216

217+
console.log('currentComponent = ',currentComponent);
218+
console.log('currentComponentProps =', currentComponentProps);
219+
console.log('newInput', newInput);
220+
216221
if (attributeName === 'compText') {
217222
const newContextObj = useContextObj;
223+
console.log('newContextObj=',newContextObj)
218224
if (!newContextObj[componentProviderId]) {
219225
newContextObj[componentProviderId] = {};
220226
}
@@ -234,6 +240,7 @@ const CustomizationPanel = ({ isThemeLight }): JSX.Element => {
234240
setUseContextObj(newContextObj);
235241
}
236242

243+
const deleteAttributeWithState = alert('Hello from Customization Panel');
237244
// TODO: set something to signify that state was used
238245
// so it can be handled in generateCode
239246

app/src/helperFunctions/generateCode.ts

Lines changed: 57 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,24 @@ declare global {
1212
}
1313
}
1414

15+
// generate code based on component hierarchy and then return the rendered code
16+
const generateCode = (
17+
components: Component[],
18+
componentId: number,
19+
rootComponents: number[],
20+
projectType: string,
21+
HTMLTypes: HTMLType[]
22+
) => {
23+
const code = generateUnformattedCode(
24+
components,
25+
componentId,
26+
rootComponents,
27+
projectType,
28+
HTMLTypes
29+
);
30+
return formatCode(code);
31+
};
32+
1533
// generate code based on the component hierarchy
1634
const generateUnformattedCode = (
1735
comps: Component[],
@@ -23,14 +41,15 @@ const generateUnformattedCode = (
2341
const components = [...comps];
2442

2543
// find the component that we're going to generate code for
26-
const currentComponent = components.find(elem => elem.id === componentId);
44+
const currComponent = components.find(elem => elem.id === componentId);
2745
// find the unique components that we need to import into this component file
2846
let imports: any = [];
2947
let providers: string = '';
3048
let context: string = '';
3149
let links: boolean = false;
3250

3351
const isRoot = rootComponents.includes(componentId);
52+
let importReactRouter = false;;
3453

3554
// returns an array of objects which may include components, html elements, and/or route links
3655
const getEnrichedChildren = (currentComponent: Component | ChildElement) => {
@@ -111,7 +130,7 @@ const generateUnformattedCode = (
111130

112131
// function to fix the spacing of the ace editor for new lines of added content. This was breaking on nested components, leaving everything right justified.
113132
const tabSpacer = (level: number) => {
114-
let tabs = ''
133+
let tabs = ' '
115134
for (let i = 0; i < level; i++) tabs += ' ';
116135
return tabs;
117136
}
@@ -188,14 +207,12 @@ const generateUnformattedCode = (
188207
const writeStateProps = (stateArray: any) => {
189208
let stateToRender = '';
190209
for (const element of stateArray) {
191-
stateToRender += levelSpacer(2, 3) + element + ';'
210+
stateToRender += levelSpacer(2, 2) + element + ';'
192211
}
193212
return stateToRender
194213
}
195214

196-
const enrichedChildren: any = getEnrichedChildren(currentComponent);
197-
198-
const next = true;
215+
const enrichedChildren: any = getEnrichedChildren(currComponent);
199216

200217
// import statements differ between root (pages) and regular components (components)
201218
const importsMapped =
@@ -213,9 +230,6 @@ const generateUnformattedCode = (
213230
})
214231
.join('\n');
215232

216-
const stateful = true;
217-
const classBased = false;
218-
let importReactRouter;
219233

220234
const createState = (stateProps) => {
221235
let state = '{';
@@ -229,10 +243,10 @@ const generateUnformattedCode = (
229243
return state;
230244
}
231245
// check for context
232-
if (currentComponent.useContext) {
246+
if (currComponent.useContext) {
233247

234-
for (const providerId of Object.keys(currentComponent.useContext)) {
235-
const attributesAndStateIds = currentComponent.useContext[String(providerId)]; //currently just from App
248+
for (const providerId of Object.keys(currComponent.useContext)) {
249+
const attributesAndStateIds = currComponent.useContext[String(providerId)]; //currently just from App
236250
const providerComponent = components[providerId - 1];
237251
providers += 'const ' + providerComponent.name.toLowerCase() + 'Context = useContext(' + providerComponent.name + 'Context);\n';
238252

@@ -253,57 +267,37 @@ const generateUnformattedCode = (
253267
// classic react code
254268
if (projectType === 'Classic React') {
255269
return `
256-
${stateful && !classBased ? `import React, { useState, createContext, useContext } from 'react';` : ''}
270+
${`import React, { useState, createContext, useContext } from 'react';`}
257271
${importReactRouter ? `import { BrowserRouter as Router, Route, Switch, Link } from 'react-router-dom';`: ``}
258-
${classBased ? `import React, { Component } from 'react';` : ''}
259-
${!stateful && !classBased ? `import React, { createContext, useContext } from 'react';` : ''}
260272
${importsMapped}
261273
${providers}
262274
${context}
275+
${`const ${currComponent.name} = (props: any): JSX.Element => {`}
276+
${` const [value, setValue] = useState<any | undefined>("INITIAL VALUE");${writeStateProps(currComponent.useStateCodes)}`}
263277
${
264-
classBased
265-
? `class ${currentComponent.name} extends Component {`
266-
: `const ${currentComponent.name} = (props: any): JSX.Element => {`
267-
}
268-
${
269-
stateful && !classBased
270-
? `const [value, setValue] = useState<any | undefined>("INITIAL VALUE");${writeStateProps(currentComponent.useStateCodes)}
271-
`
272-
: ``
273-
}
274-
${
275-
isRoot && currentComponent.stateProps.length !== 0
276-
? `const ${currentComponent.name}Context = createContext(${createState(currentComponent.stateProps)});`
278+
isRoot && currComponent.stateProps.length !== 0
279+
? ` const ${currComponent.name}Context = createContext(${createState(currComponent.stateProps)});`
277280
: ``
278-
279-
}
280-
${
281-
classBased && stateful
282-
? `constructor(props) {
283-
super(props);
284-
this.state = {}
285-
}`
286-
: ``
287281
}
288-
${classBased ? `render(): JSX.Element {` : ``}
289-
${!importReactRouter ?
290-
`return (
291-
<${currentComponent.name}Context.Provider value="">
292-
<div className="${currentComponent.name}" ${formatStyles(currentComponent)}>
282+
${!importReactRouter
283+
? ` return (
284+
<${currComponent.name}Context.Provider value="">
285+
<div className="${currComponent.name}" ${formatStyles(currComponent)}>
293286
${writeNestedElements(enrichedChildren)}
294-
</div>
295-
</${currentComponent.name}Context.Provider>
296-
);` : `return (
297-
<${currentComponent.name}Context.Provider value="">
298-
<Router>
299-
<div className="${currentComponent.name}" ${formatStyles(currentComponent)}>
300-
${writeNestedElements(enrichedChildren)}
301287
</div>
302-
</Router>
303-
</${currentComponent.name}Context.Provider>
304-
);`}
305-
${`}`}
306-
export default ${currentComponent.name};
288+
</${currComponent.name}Context.Provider>
289+
);`
290+
: ` return (
291+
<${currComponent.name}Context.Provider value="">
292+
<Router>
293+
<div className="${currComponent.name}" ${formatStyles(currComponent)}>
294+
${` ` + writeNestedElements(enrichedChildren)}
295+
</div>
296+
</Router>
297+
</${currComponent.name}Context.Provider>
298+
);`}
299+
${`}\n`}
300+
export default ${currComponent.name};
307301
`;
308302
}
309303
// next.js component code
@@ -314,7 +308,7 @@ const generateUnformattedCode = (
314308
import Head from 'next/head'
315309
${links ? `import Link from 'next/link'` : ``}
316310
317-
const ${currentComponent.name} = (props): JSX.Element => {
311+
const ${currComponent.name} = (props): JSX.Element => {
318312
319313
const [value, setValue] = useState<any | undefined>("INITIAL VALUE");
320314
@@ -323,18 +317,18 @@ const generateUnformattedCode = (
323317
${
324318
isRoot
325319
? `<Head>
326-
<title>${currentComponent.name}</title>
320+
<title>${currComponent.name}</title>
327321
</Head>`
328322
: ``
329323
}
330-
<div className="${currentComponent.name}" style={props.style}>
324+
<div className="${currComponent.name}" style={props.style}>
331325
${writeNestedElements(enrichedChildren)}
332326
</div>
333327
</>
334328
);
335329
}
336330
337-
export default ${currentComponent.name};
331+
export default ${currComponent.name};
338332
`;
339333
} else {
340334
// gatsby component code
@@ -345,7 +339,7 @@ const generateUnformattedCode = (
345339
${links ? `import { Link } from 'gatsby'` : ``}
346340
347341
348-
const ${currentComponent.name} = (props: any): JSX.Element => {
342+
const ${currComponent.name} = (props: any): JSX.Element => {
349343
350344
const[value, setValue] = useState<any | undefined>("INITIAL VALUE");
351345
@@ -354,18 +348,18 @@ const generateUnformattedCode = (
354348
${
355349
isRoot
356350
? `<head>
357-
<title>${currentComponent.name}</title>
351+
<title>${currComponent.name}</title>
358352
</head>`
359353
: ``
360354
}
361-
<div className="${currentComponent.name}" style={props.style}>
355+
<div className="${currComponent.name}" style={props.style}>
362356
${writeNestedElements(enrichedChildren)}
363357
</div>
364358
</>
365359
);
366360
}
367361
368-
export default ${currentComponent.name};
362+
export default ${currComponent.name};
369363
`;
370364
}
371365
};
@@ -390,22 +384,6 @@ const formatCode = (code: string) => {
390384
}
391385
};
392386

393-
// generate code based on component hierarchy and then return the rendered code
394-
const generateCode = (
395-
components: Component[],
396-
componentId: number,
397-
rootComponents: number[],
398-
projectType: string,
399-
HTMLTypes: HTMLType[]
400-
) => {
401-
const code = generateUnformattedCode(
402-
components,
403-
componentId,
404-
rootComponents,
405-
projectType,
406-
HTMLTypes
407-
);
408-
return formatCode(code);
409-
};
387+
410388

411389
export default generateCode;

app/src/tree/TreeChart.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function TreeChart({ data }) { // data is components from state - passed in from
3737
i -= 1;
3838
}
3939
// if element has a children array and that array has length, recursive call
40-
else if ((arr[i].name === 'div' || arr[i].name === 'form' || arr[i].type === 'Component') && arr[i].children.length) {
40+
else if ((arr[i].name === 'div' || arr[i].name === 'form' || arr[i].type === 'Component' || arr[i].name === 'LinkTo' || arr[i].name === 'Switch' || arr[i].name === 'Route') && arr[i].children.length) {
4141
// if element is a component, replace it with deep clone of latest version (to update with new HTML elements)
4242
if (arr[i].type === 'Component') arr[i] = cloneDeep(data.find(component => component.name === arr[i].name));
4343
removeSeparators(arr[i].children);

0 commit comments

Comments
 (0)