Skip to content

Commit b6afd31

Browse files
committed
Export Project fixed to account for nested HTML elements
1 parent 999623b commit b6afd31

File tree

1 file changed

+46
-14
lines changed

1 file changed

+46
-14
lines changed

src/components/ExportProject.vue

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,55 @@ export default {
143143
input: ['<input />', ''],
144144
navbar: ['<nav>', '</nav>']
145145
}
146+
147+
/* Function to loop through nested elements */
148+
function writeNested (childrenArray, indent) {
149+
if (!childrenArray.length) {
150+
return ''
151+
}
152+
let indented = indent + ' '
153+
let nestedString = ''
154+
155+
childrenArray.forEach(child => {
156+
nestedString += indented
157+
// console.log(child)
158+
if (!child.text) {
159+
nestedString += `<${child}/>\n`
160+
} else {
161+
if (child.children.length) {
162+
nestedString += htmlElementMap[child.text][0]
163+
nestedString += '\n'
164+
nestedString += writeNested(child.children, indented)
165+
nestedString += indented + htmlElementMap[child.text][1]
166+
nestedString += '\n'
167+
} else {
168+
nestedString += htmlElementMap[child.text][0] + htmlElementMap[child.text][1] + '\n'
169+
}
170+
}
171+
})
172+
return nestedString
173+
}
174+
146175
// loop to iterate through compName arr
147176
let htmlArr = this.componentMap[compName].htmlList
148-
let outputStr = ''
177+
let outputStr = ``
149178
for (let el of htmlArr) {
150-
outputStr += '\t\t'
151-
outputStr += htmlElementMap[el.text][0]
152-
outputStr += htmlElementMap[el.text][1]
153-
outputStr += `\n`
179+
// console.log(el)
180+
if (!el.text) {
181+
outputStr += ` <${el}/>\n`
182+
} else {
183+
outputStr += ` `
184+
if (el.children.length) {
185+
outputStr += htmlElementMap[el.text][0]
186+
outputStr += '\n'
187+
outputStr += writeNested(el.children, ` `)
188+
outputStr += ` `
189+
outputStr += htmlElementMap[el.text][1]
190+
outputStr += ` \n`
191+
} else {
192+
outputStr += htmlElementMap[el.text][0] + htmlElementMap[el.text][1] + '\n'
193+
}
194+
}
154195
}
155196
console.log(`outputStr from writeTemplateTag: ${outputStr}`)
156197
return outputStr
@@ -199,15 +240,6 @@ export default {
199240
} else {
200241
// console.log(`else (if compName === 'App'`);
201242
str += `<div>\n`
202-
children.forEach(name => {
203-
str += `\t\t<${
204-
// name.componentName
205-
name
206-
}>\n\t\t</${
207-
// name.componentName
208-
name
209-
}>\n`
210-
})
211243
}
212244
// writes the html tag boilerplate
213245
let templateTagStr = this.writeTemplateTag(compName)

0 commit comments

Comments
 (0)