Skip to content

Commit 86ade31

Browse files
authored
Merge pull request #46 from sean1292/ace
Code export fixed as well as proper code for App component was added
2 parents 55bde97 + ca35689 commit 86ade31

File tree

6 files changed

+117
-40
lines changed

6 files changed

+117
-40
lines changed

src/components/bottom/CodePreview.tsx

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ type CodePreviewProps = {
1717
toggleCodeEdit(): void;
1818
codeReadOnly: boolean;
1919
};
20-
2120
class CodePreview extends Component<CodePreviewProps> {
2221
//checking if the code has been asigned yet or not
2322
//if no then generate code and asign to a focus component
@@ -66,34 +65,37 @@ class CodePreview extends Component<CodePreviewProps> {
6665
}}
6766
>
6867
<AceEditor
69-
mode='javascript'
70-
theme='monokai'
71-
width='100%'
72-
height='100%'
68+
mode="javascript"
69+
theme="monokai"
70+
width="100%"
71+
height="100%"
7372
style={{
7473
border: '2px solid #33eb91',
7574
borderRadius: '8px'
7675
}}
76+
enableLiveAutocompletion={true}
7777
onChange={code =>
7878
this.props.updateCode({
7979
componentId: this.props.focusComponent.id,
8080
code
8181
})
8282
}
8383
value={this.props.focusComponent.code}
84-
name='Code_div'
84+
name="Code_div"
8585
readOnly={this.props.codeReadOnly}
8686
editorProps={{ $blockScrolling: true }}
8787
fontSize={16}
88+
// enableBasicAutocompletion={true}
89+
tabSize={2}
8890
/>
8991
<div style={{ display: 'flex', flexDirection: 'column' }}>
9092
<Button
91-
color='primary'
92-
aria-label='Add'
93-
type='submit'
93+
color="primary"
94+
aria-label="Add"
95+
type="submit"
9496
// disabled={!this.state.propKey || !this.state.propType}
95-
variant='contained'
96-
size='large'
97+
variant="contained"
98+
size="large"
9799
style={{ justifySelf: 'center' }}
98100
className={this.props.classes.startEdit}
99101
onClick={e => {

src/reducers/bottomReducers.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ export const addProp = (
2626
(comp: ComponentInt) => comp.id === state.focusComponent.id
2727
);
2828

29+
if (!state.codeReadOnly) {
30+
const check = window.confirm(
31+
`Are you sure you want to add a Prop to the ${state.focusComponent.title} component while the program is in the "Edit Mode"? \n\nAll of the changes to the "Code Preview" for the ${state.focusComponent.title} component will be overridden!`
32+
);
33+
if (!check) {
34+
return {
35+
...state
36+
};
37+
}
38+
}
2939
const newProp: PropInt = {
3040
id: selectedComponent.nextPropId,
3141
key,
@@ -54,7 +64,8 @@ export const addProp = (
5464
focusComponent: modifiedComponent,
5565
historyIndex,
5666
history,
57-
future
67+
future,
68+
codeReadOnly: true
5869
};
5970
};
6071

@@ -69,6 +80,16 @@ export const deleteProp = (state: ApplicationStateInt, propId: number) => {
6980
(comp: ComponentInt) => comp.id === state.focusComponent.id
7081
)
7182
);
83+
if (!state.codeReadOnly) {
84+
const check = window.confirm(
85+
`Are you sure you want to delete a Prop from the ${state.focusComponent.title} component while the program is in the "Edit Mode"? \n\nAll of the changes to the "Code Preview" for the ${state.focusComponent.title} component will be overridden!`
86+
);
87+
if (!check) {
88+
return {
89+
...state
90+
};
91+
}
92+
}
7293

7394
const indexToDelete = modifiedComponent.props.findIndex(
7495
(prop: PropInt) => prop.id === propId
@@ -182,6 +203,16 @@ export const updateHtmlAttr = (
182203
return state;
183204
}
184205

206+
if (!state.codeReadOnly) {
207+
const check = window.confirm(
208+
`Are you sure you want to update the HTML attributes of the ${state.focusComponent.title} component while the program is in the "Edit Mode"? \n\nAll of the changes to the "Code Preview" for the ${state.focusComponent.title} component will be overridden!`
209+
);
210+
if (!check) {
211+
return {
212+
...state
213+
};
214+
}
215+
}
185216
const modifiedChild: any = cloneDeep(state.focusChild);
186217
modifiedChild.HTMLInfo[attr] = value;
187218

@@ -211,6 +242,7 @@ export const updateHtmlAttr = (
211242
focusChild: modifiedChild,
212243
history,
213244
historyIndex,
214-
future
245+
future,
246+
codeReadOnly: true
215247
};
216248
};

src/reducers/leftReducers.ts

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,20 +462,64 @@ export const editComponent = (
462462
) => {
463463
let components = cloneDeep(state.components);
464464
let toEdit = components.find((element: ComponentInt) => element.id === id);
465-
toEdit.title = title;
465+
466+
if (!state.codeReadOnly) {
467+
const check = window.confirm(
468+
`Are you sure you want to change the name of the ${state.focusComponent.title} component while the program is in the "Edit Mode"? \n\nAll of the changes to the "Code Preview" for the ${state.focusComponent.title} component will be overridden!`
469+
);
470+
if (!check) {
471+
return {
472+
...state
473+
};
474+
}
475+
}
476+
477+
// remove whitespace and digits, capitalize first char
478+
const strippedTitle = title
479+
.replace(/[a-z]+/gi, word => word[0].toUpperCase() + word.slice(1))
480+
.replace(/[-_\s0-9\W]+/gi, '');
481+
// duplicate component names not allowed
482+
if (
483+
state.components.find(
484+
(comp: ComponentInt) => comp.title === strippedTitle
485+
) &&
486+
toEdit.title !== strippedTitle
487+
) {
488+
window.alert(
489+
`A component with the name: "${strippedTitle}" already exists.\nPlease think of another name.`
490+
);
491+
return {
492+
...state
493+
};
494+
}
495+
496+
// empty component name not allowed
497+
if (strippedTitle === '') {
498+
window.alert(
499+
`Can't leave the name of the component blank.\nPlease enter a name.`
500+
);
501+
return {
502+
...state
503+
};
504+
}
505+
toEdit.title = strippedTitle;
506+
toEdit.changed = true;
466507
for (const [index, each] of components.entries()) {
508+
each.changed = true;
467509
for (const value of each.childrenArray) {
468510
if (value.childComponentId === id) {
469-
value.componentName = title;
511+
value.componentName = strippedTitle;
470512
}
471513
}
472514
}
473515

474516
return {
475517
...state,
476518
focusChild: state.initialApplicationFocusChild,
519+
focusComponent: toEdit,
477520
editMode: -1,
478-
components
521+
components,
522+
codeReadOnly: true
479523
};
480524
};
481525

src/theme.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ import indigo from '@material-ui/core/colors/indigo';
33

44
const theme = createMuiTheme({
55
typography: {
6-
useNextVariants: true,
6+
useNextVariants: true
77
},
88
palette: {
99
primary: {
1010
light: '#00e676',
1111
// main: '#33eb91',
1212
main: '#01d46d', // less blinding green
1313
dark: '#14a37f',
14-
contrastText: '#fff',
14+
contrastText: '#fff'
1515
},
16-
secondary: indigo,
16+
secondary: indigo
1717
}
1818
});
1919

src/utils/componentRender.util.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,10 @@ const componentRender = (
3434
return 'object';
3535
case 'array':
3636
return 'any[]';
37-
case 'bool':
37+
case 'boolean':
3838
return 'boolean';
3939
case 'function':
4040
return '() => any';
41-
// case 'symbol':
42-
// return 'string';
43-
case 'node':
44-
return 'string';
45-
case 'element':
46-
return 'string';
4741
case 'tuple':
4842
return '[any]';
4943
case 'enum':
@@ -147,13 +141,20 @@ const componentRender = (
147141
}, [])
148142
.join('\n')}
149143
150-
interface Props {
144+
${
145+
title == 'App'
146+
? ''
147+
: `interface Props {
151148
${props.map(prop => `${prop.key}: ${typeSwitcher(prop.type)}\n`)}
152-
};
149+
};`
150+
}
151+
153152
154153
${
155154
classBased
156155
? `class ${title} extends Component {`
156+
: title == 'App'
157+
? `const ${title} = () => {`
157158
: `const ${title} = (props: Props) => {`
158159
}
159160
${
@@ -170,9 +171,13 @@ const componentRender = (
170171
: ``
171172
}
172173
${classBased ? `render(): JSX.Element {` : ``}
173-
const {${props.map(el => el.key).join(', ')}} = ${
174-
classBased ? `this.props` : `props`
175-
};
174+
${
175+
title === 'App'
176+
? ''
177+
: `const {${props.map(el => el.key).join(', ')}} = ${
178+
classBased ? `this.props` : `props`
179+
}`
180+
}
176181
177182
return (
178183
<div>

src/utils/createFiles.util.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const createFiles = (
66
components: any,
77
path: string,
88
appName: string,
9-
exportAppBool: boolean,
9+
exportAppBool: boolean
1010
) => {
1111
let dir = path;
1212
if (exportAppBool === false) {
@@ -30,17 +30,11 @@ const createFiles = (
3030
const newPromise = new Promise((resolve, reject) => {
3131
fs.writeFile(
3232
`${dir}/${component.title}.tsx`,
33-
format(componentRender(component, components), {
34-
singleQuote: true,
35-
trailingComma: 'es5',
36-
bracketSpacing: true,
37-
jsxBracketSameLine: true,
38-
parser: 'typescript',
39-
}),
33+
component.code,
4034
(err: any) => {
4135
if (err) return reject(err.message);
4236
return resolve(path);
43-
},
37+
}
4438
);
4539
});
4640

0 commit comments

Comments
 (0)