Skip to content

Commit 430d13b

Browse files
committed
Add mixin folder and writeNested.js file to eliminate duplicate code.
1 parent e1ee5f5 commit 430d13b

File tree

3 files changed

+57
-78
lines changed

3 files changed

+57
-78
lines changed

src/components/nav-buttons/ExportMenu.vue

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ import { useExportComponent } from "../composables/useExportComponent.js";
2323
import { mapState } from "vuex";
2424
const { fs, ipcRenderer } = window;
2525

26+
import writeNested from "../../mixins/writeNested";
27+
28+
2629
export default {
2730
name: "ExportProjectComponent",
31+
mixins: [writeNested],
2832
methods: {
2933
useExportComponentBound() {
3034
useExportComponent.bind(this)();
@@ -43,9 +47,6 @@ export default {
4347
.catch((err) => console.log(err));
4448
},
4549
exportProject: function () {
46-
// console.log('this.activeComponentObj: ', this.activeComponentObj); /* *********************** */ //Not sure, it's undefined
47-
console.log('this.componentMap: ', this.componentMap); /* *********************** */
48-
console.log('this.routes: ', this.routes); /* *********************** */ //array of objects on the cssGrid
4950

5051
this.showExportProjectDialog();
5152
},
@@ -160,42 +161,7 @@ export default {
160161
'e-slider':["<el-slider", "</el-slider>"],
161162
'e-card': ["<el-card", "</el-card>"],
162163
};
163-
// function to loop through nested elements
164-
function writeNested(childrenArray, indent) {
165-
if (!childrenArray.length) {
166-
return "";
167-
}
168-
let indented = indent + " ";
169-
let nestedString = "";
170-
171-
childrenArray.forEach((child) => {
172-
nestedString += indented;
173-
if (!child.text) {
174-
nestedString += `<${child}/>\n`;
175-
} else {
176-
nestedString += htmlElementMap[child.text][0];
177-
if (child.class !== "") {
178-
nestedString += " " + "class = " + `"${child.class}"`;
179-
}
180-
if(child.binding !== "") {
181-
nestedString += " " + "v-model = " + `"${child.binding}"`;
182-
}
183-
if (child.text === "img" || child.text === "input" || child.text === "link") {
184-
nestedString += "/>";
185-
} else { nestedString += ">"; }
186164

187-
if (child.children.length) {
188-
nestedString += "\n";
189-
nestedString += writeNested(child.children, indented);
190-
nestedString += indented + htmlElementMap[child.text][1];
191-
nestedString += "\n";
192-
} else {
193-
nestedString += htmlElementMap[child.text][1] + "\n";
194-
}
195-
}
196-
});
197-
return nestedString;
198-
}
199165
// iterate through component's htmllist
200166
let htmlArr = this.componentMap[componentName].htmlList;
201167
let outputStr = ``;
@@ -296,7 +262,6 @@ export default {
296262
}
297263
}
298264
else {
299-
console.log('Test log if route')
300265
return `<template>\n\t${str}${templateTagStr}${routeStr}</div>\n</template>`
301266
}
302267
},

src/components/right-sidebar/CodeSnippet.vue

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ 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";
31+
3032
export default {
3133
data() {
3234
return {
@@ -43,6 +45,7 @@ export default {
4345
// needs access to current component aka activeComponent
4446
...mapState(["componentMap", "activeComponent", "activeComponentObj", "exportAsTypescript"]),
4547
},
48+
mixins: [writeNested],
4649
methods: {
4750
snippetInvoke() {
4851
if (this.activeComponent !== '') {
@@ -125,48 +128,10 @@ export default {
125128
childComponents.forEach(child => {
126129
htmlElementMap[child]=[`<${child}`, ""] //single
127130
})
128-
function writeNested(childrenArray, indent) {
129-
if (!childrenArray.length) {
130-
return "";
131-
}
132-
let indented = indent + " ";
133-
let nestedString = "";
134-
135-
childrenArray.forEach((child) => {
136-
nestedString += indented;
137-
if (!child.text) {
138-
nestedString += `<${child}/>\n`;
139-
} else {
140-
nestedString += htmlElementMap[child.text][0];
141-
if (child.class !== "") {
142-
nestedString += " " + "class=" + `"${child.class}"`;
143-
}
144-
if (child.binding !== "") {
145-
if (child.text !== 'img' || child.text !== 'link') {
146-
nestedString += ` v-model="${child.binding}"`
147-
148-
}
149-
}
150-
if (child.text === "img" || child.text === "input" || child.text === "link" || childComponents.includes(child.text)) {
151-
nestedString += "/>";
152-
} else { nestedString += ">"; }
153-
154-
if (child.children.length) {
155-
nestedString += "\n";
156-
nestedString += writeNested(child.children, indented);
157-
nestedString += indented + htmlElementMap[child.text][1];
158-
nestedString += "\n"
159-
} else {
160-
nestedString += htmlElementMap[child.text][1] + "\n";
161-
}
162-
}
163-
});
164-
return nestedString;
165-
}
166131

167132
// Iterates through active component's HTML elements list and adds to code snippet
168133
let htmlArr = this.componentMap[componentName].htmlList;
169-
let outputStr = ``
134+
let outputStr = ``;
170135
// eslint-disable-next-line no-unused-vars
171136
for (let el of htmlArr) {
172137
if (!el.text) {

src/mixins/writeNested.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Helper function that recursively iterates through the given html element's children and their children's children.
2+
// also adds proper indentation to code snippet
3+
4+
export default {
5+
methods: {
6+
writeNested(childrenArray, indent) {
7+
if (!childrenArray.length) {
8+
return;
9+
}
10+
let indented = indent + " ";
11+
let nestedString = "";
12+
13+
childrenArray.forEach((child) => {
14+
nestedString += indented;
15+
if (!child.text) {
16+
nestedString += `<${child}/>\n`;
17+
}
18+
else {
19+
nestedString += htmlElementMap[child.text][0];
20+
if (child.class !== "") {
21+
nestedString += " " + "class=" + `"${child.class}"`;
22+
}
23+
if (child.binding !== "") {
24+
if (child.text !== 'img' || child.text !== 'link') {
25+
nestedString += ` v-model="${child.binding}"`
26+
27+
}
28+
}
29+
30+
if (child.text === "img" || child.text === "input" || child.text === "link" || childComponents.includes(child.text)) {
31+
nestedString += "/>";
32+
}
33+
else { nestedString += ">"; }
34+
35+
if (child.children.length) {
36+
nestedString += "\n";
37+
nestedString += writeNested(child.children, indented);
38+
nestedString += indented + htmlElementMap[child.text][1];
39+
nestedString += "\n"
40+
}
41+
else {
42+
nestedString += htmlElementMap[child.text][1] + "\n";
43+
}
44+
}
45+
});
46+
return nestedString;
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)