Skip to content

Commit d0afb71

Browse files
authored
Merge pull request #1 from oslabs-beta/parent/child
Parent/Child Relationship Bug + HTML Nesting Bug
2 parents 1fa9c79 + 828e639 commit d0afb71

File tree

2 files changed

+114
-10
lines changed

2 files changed

+114
-10
lines changed

src/components/nav-buttons/ExportMenu.vue

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,11 @@ import { useExportComponent } from "../composables/useExportComponent.js";
2222
import { mapState } from "vuex";
2323
const { fs, ipcRenderer } = window;
2424
25-
import writeNested from "../../mixins/writeNested";
26-
import { result } from "lodash";
25+
// import writeNested from "../../mixins/writeNested";
2726
2827
2928
export default {
3029
name: "ExportProjectComponent",
31-
mixins: [writeNested],
3230
methods: {
3331
useExportComponentBound() {
3432
useExportComponent.bind(this)();
@@ -227,7 +225,42 @@ test('renders ${componentName}', () => {
227225
`
228226
</el-badge>`],
229227
};
230-
228+
// function to loop through nested elements
229+
function writeNested(childrenArray, indent) {
230+
if (!childrenArray.length) {
231+
return "";
232+
}
233+
let indented = indent + " ";
234+
let nestedString = "";
235+
236+
childrenArray.forEach((child) => {
237+
nestedString += indented;
238+
if (!child.text) {
239+
nestedString += `<${child}/>\n`;
240+
} else {
241+
nestedString += htmlElementMap[child.text][0];
242+
if (child.class !== "") {
243+
nestedString += " " + "class = " + `"${child.class}"`;
244+
}
245+
if(child.binding !== "") {
246+
nestedString += " " + "v-model = " + `"${child.binding}"`;
247+
}
248+
if (child.text === "img" || child.text === "input" || child.text === "link") {
249+
nestedString += "/>";
250+
} else { nestedString += ">"; }
251+
252+
if (child.children.length) {
253+
nestedString += "\n";
254+
nestedString += writeNested(child.children, indented);
255+
nestedString += indented + htmlElementMap[child.text][1];
256+
nestedString += "\n";
257+
} else {
258+
nestedString += htmlElementMap[child.text][1] + "\n";
259+
}
260+
}
261+
});
262+
return nestedString;
263+
}
231264
// iterate through component's htmllist
232265
let htmlArr = this.componentMap[componentName].htmlList;
233266
let outputStr = ``;
@@ -328,12 +361,18 @@ test('renders ${componentName}', () => {
328361
return `<template>\n <div id = "${compID}">\n${templateTagStr}${routeStr} </div>\n</template>`;
329362
}
330363
else {
331-
332-
return `<template>\n <div>\n\t${str}${templateTagStr}${routeStr} </div>\n</template>`;
364+
const arrOfChildComp = this.componentMap[componentName].children;
365+
arrOfChildComp.forEach(childName => {
366+
let childNameClass = this.componentMap[childName].htmlAttributes.class;
367+
let childNameClassFullStr = (childNameClass === "") ? "" : ` class = '${childNameClass}'`;
368+
routeStr += ` <${childName}${childNameClassFullStr}></${childName}>\n`
369+
});
370+
371+
return `<template>\n <div>\n${str}${templateTagStr}${routeStr} </div>\n</template>`;
333372
}
334373
}
335374
else {
336-
return `<template>\n\t${str}${templateTagStr}${routeStr}\t</div>\n</template>`
375+
return `<template>\n<div>\n\t${str}${templateTagStr}${routeStr}\t</div>\n</template>`
337376
}
338377
},
339378
/**
@@ -366,6 +405,20 @@ test('renders ${componentName}', () => {
366405
}
367406
368407
let childrenComponentNames = "";
408+
let childComponentImportNames = "";
409+
410+
const arrOfChildComp = this.componentMap[componentName].children;
411+
412+
arrOfChildComp.forEach(childName => {
413+
// Build child component text string
414+
if (childName !== arrOfChildComp[arrOfChildComp.length - 1]){
415+
childrenComponentNames += " " + childName + ",\n";
416+
}
417+
else {
418+
childrenComponentNames += " " + childName + "\n";
419+
}
420+
childComponentImportNames += `import ${childName} from '../components/${childName}.vue';\n`
421+
})
369422
370423
let data = "";
371424
data += " data () {\n return {";
@@ -416,6 +469,8 @@ test('renders ${componentName}', () => {
416469
} else {
417470
output = "\n\n<script>\n";
418471
472+
output+= `\n${childComponentImportNames}`;
473+
419474
output += imports + "\nexport default {\n name: '" + componentName + "'";
420475
421476
}

src/components/right-sidebar/CodeSnippet.vue

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import "prismjs/components/prism-clike";
2727
import "prismjs/components/prism-javascript";
2828
import "prismjs/themes/prism-tomorrow.css"; // import syntax highlighting styles
2929
30-
import writeNested from "../../mixins/writeNested";
30+
// import writeNested from "../../mixins/writeNested";
3131
3232
export default {
3333
data() {
@@ -45,7 +45,6 @@ export default {
4545
// needs access to current component aka activeComponent
4646
...mapState(["componentMap", "activeComponent", "activeComponentObj", "exportAsTypescript"]),
4747
},
48-
mixins: [writeNested],
4948
methods: {
5049
snippetInvoke() {
5150
if (this.activeComponent !== '') {
@@ -77,14 +76,26 @@ export default {
7776
// Creates beginner boilerplate
7877
createTemplate(componentName) {
7978
let templateTagStr = this.writeTemplateTag(componentName);
80-
79+
if (this.activeComponentObj.htmlAttributes) {
8180
//if/else statement to determine if there are class and id attributes present in the html element
8281
if (this.activeComponentObj.htmlAttributes.class !== "" && this.activeComponentObj.htmlAttributes.id !== "") {
8382
return `<template>\n <div id = "${this.activeComponentObj.htmlAttributes.id}" class = "${this.activeComponentObj.htmlAttributes.class}">\n${templateTagStr} </div>\n</template>`;
8483
} else if (this.activeComponentObj.htmlAttributes.class !== "" && this.activeComponentObj.htmlAttributes.id === "") {
8584
return `<template>\n <div class = "${this.activeComponentObj.htmlAttributes.class}">\n${templateTagStr} </div>\n</template>`;
8685
} else if (this.activeComponentObj.htmlAttributes.class === "" && this.activeComponentObj.htmlAttributes.id !== "")
8786
return `<template>\n <div id = "${this.activeComponentObj.htmlAttributes.id}">\n${templateTagStr} </div>\n</template>`;
87+
else {
88+
let routeStr = '';
89+
const arrOfChildComp = this.componentMap[componentName].children;
90+
arrOfChildComp.forEach(childName => {
91+
let childNameClass = this.componentMap[childName].htmlAttributes.class;
92+
let childNameClassFullStr = (childNameClass === "") ? "" : ` class = '${childNameClass}'`;
93+
routeStr += ` <${childName}${childNameClassFullStr}></${childName}>\n`
94+
});
95+
96+
return `<template>\n <div>\n${templateTagStr}${routeStr} </div>\n</template>`;
97+
}
98+
}
8899
else return `<template>\n <div>\n${templateTagStr} </div>\n</template>`;
89100
},
90101
// Creates <template> boilerplate
@@ -172,6 +183,44 @@ export default {
172183
htmlElementMap[child]=[`<${child}`, ""] //single
173184
})
174185
186+
function writeNested(childrenArray, indent) {
187+
if (!childrenArray.length) {
188+
return "";
189+
}
190+
let indented = indent + " ";
191+
let nestedString = "";
192+
193+
childrenArray.forEach((child) => {
194+
nestedString += indented;
195+
if (!child.text) {
196+
nestedString += `<${child}/>\n`;
197+
} else {
198+
nestedString += htmlElementMap[child.text][0];
199+
if (child.class !== "") {
200+
nestedString += " " + "class=" + `"${child.class}"`;
201+
}
202+
if (child.binding !== "") {
203+
if (child.text !== 'img' || child.text !== 'link') {
204+
nestedString += ` v-model="${child.binding}"`
205+
206+
}
207+
}
208+
if (child.text === "img" || child.text === "input" || child.text === "link" || childComponents.includes(child.text)) {
209+
nestedString += "/>";
210+
} else { nestedString += ">"; }
211+
212+
if (child.children.length) {
213+
nestedString += "\n";
214+
nestedString += writeNested(child.children, indented);
215+
nestedString += indented + htmlElementMap[child.text][1];
216+
nestedString += "\n"
217+
} else {
218+
nestedString += htmlElementMap[child.text][1] + "\n";
219+
}
220+
}
221+
});
222+
return nestedString;
223+
}
175224
// Iterates through active component's HTML elements list and adds to code snippet
176225
let htmlArr = this.componentMap[componentName].htmlList;
177226
let outputStr = ``;

0 commit comments

Comments
 (0)