15
15
</template >
16
16
17
17
<script >
18
- import { mapState } from ' vuex' ;
19
- import PrismEditor from ' vue-prism-editor' ;
20
- import ' prismjs' ;
21
- import ' prismjs/themes/prism-okaidia.css' ;
22
- import ' vue-prism-editor/dist/VuePrismEditor.css' ;
18
+ import { mapState } from ' vuex'
19
+ import PrismEditor from ' vue-prism-editor'
20
+ import ' prismjs'
21
+ import ' prismjs/themes/prism-okaidia.css'
22
+ import ' vue-prism-editor/dist/VuePrismEditor.css'
23
23
24
24
export default {
25
- data () {
25
+ data () {
26
26
return {
27
27
code: ` Your component boilerplate will be displayed here.` ,
28
28
lineNumbers: true ,
29
- height: null ,
30
- };
29
+ height: null
30
+ }
31
31
},
32
32
components: {
33
- PrismEditor,
33
+ PrismEditor
34
34
},
35
35
computed: {
36
36
// needs access to current component aka activeComponent
37
- ... mapState ([' componentMap' , ' activeComponent' ]),
37
+ ... mapState ([' componentMap' , ' activeComponent' ])
38
38
},
39
39
methods: {
40
- getWindowHeight (e ) {
40
+ getWindowHeight (e ) {
41
41
let minHeight =
42
- window .outerHeight < 900 ? 22 : window .outerHeight < 1035 ? 24 : 27.5 ;
43
- this .height = minHeight;
42
+ window .outerHeight < 900 ? 22 : window .outerHeight < 1035 ? 24 : 27.5
43
+ this .height = minHeight
44
44
},
45
45
// calls createTemplate and createBoiler to generate snippet
46
- createCodeSnippet (componentName , children ) {
46
+ createCodeSnippet (componentName , children ) {
47
47
let result = ` ${ this .createTemplate (
48
48
componentName,
49
49
children
50
- )}${ this .createBoiler (componentName, children)} ` ;
51
- return result;
50
+ )}${ this .createBoiler (componentName, children)} `
51
+ return result
52
52
},
53
- createTemplate (componentName , children ) {
54
- let output = ` ` ;
55
- output += ` <div>\n ` ;
53
+ createTemplate (componentName , children ) {
54
+ let output = ` `
55
+ output += ` <div>\n `
56
56
// children.forEach(name => {
57
57
// output += ` <${name}>\n </${name}>\n`;
58
58
// });
59
- let templateTagStr = this .writeTemplateTag (componentName);
60
- return ` <template>\n ${ output}${ templateTagStr} </div>\n </template>` ;
59
+ let templateTagStr = this .writeTemplateTag (componentName)
60
+ return ` <template>\n ${ output}${ templateTagStr} </div>\n </template>`
61
61
},
62
62
63
63
// We need a helper function to recursively iterate through the given html element's children and their children's children.
64
64
65
-
66
- writeTemplateTag (componentName ) {
65
+ writeTemplateTag (componentName ) {
67
66
// console.log('writeTemplateTag invoked!')
68
67
// create reference object
69
68
const htmlElementMap = {
@@ -77,120 +76,115 @@ export default {
77
76
' list-ol' : [' <ol>' , ' </ol>' ],
78
77
' list-ul' : [' <ul>' , ' </ul>' ],
79
78
input: [' <input />' , ' ' ],
80
- navbar: [' <nav>' , ' </nav>' ],
81
- };
82
-
83
- function writeNested (childrenArray , indent ){
84
- if (! childrenArray .length ){
85
- return ' '
79
+ navbar: [' <nav>' , ' </nav>' ]
86
80
}
87
- let indented = indent + ' '
88
- let nestedString = ' '
89
81
90
- childrenArray .forEach (child => {
91
- nestedString += indented
92
- // console.log(child)
93
- if (! child .text ){
94
- nestedString += ` <${ child} />\n `
95
- }
96
- else {
97
- if (child .children .length ){
98
- nestedString += htmlElementMap[child .text ][0 ]
99
- nestedString += ' \n ' ;
100
- nestedString += writeNested (child .children ,indented)
101
- nestedString += indented + htmlElementMap[child .text ][1 ]
102
- nestedString += ' \n '
103
- }
104
- else {
105
- nestedString += htmlElementMap[child .text ][0 ]+ htmlElementMap[child .text ][1 ]+ ' \n ' ;
106
- }
82
+ function writeNested (childrenArray , indent ) {
83
+ if (! childrenArray .length ) {
84
+ return ' '
107
85
}
108
- })
109
- return nestedString
110
- }
86
+ let indented = indent + ' '
87
+ let nestedString = ' '
111
88
89
+ childrenArray .forEach (child => {
90
+ nestedString += indented
91
+ // console.log(child)
92
+ if (! child .text ) {
93
+ nestedString += ` <${ child} />\n `
94
+ } else {
95
+ if (child .children .length ) {
96
+ nestedString += htmlElementMap[child .text ][0 ]
97
+ nestedString += ' \n '
98
+ nestedString += writeNested (child .children , indented)
99
+ nestedString += indented + htmlElementMap[child .text ][1 ]
100
+ nestedString += ' \n '
101
+ } else {
102
+ nestedString += htmlElementMap[child .text ][0 ] + htmlElementMap[child .text ][1 ] + ' \n '
103
+ }
104
+ }
105
+ })
106
+ return nestedString
107
+ }
112
108
113
109
// loop to iterate through compName arr
114
- let htmlArr = this .componentMap [componentName].htmlList ;
115
- let outputStr = ` ` ;
110
+ let htmlArr = this .componentMap [componentName].htmlList
111
+ let outputStr = ` `
116
112
for (let el of htmlArr) {
117
- // console.log(el)
118
- if (! el .text ){
113
+ // console.log(el)
114
+ if (! el .text ) {
119
115
outputStr += ` <${ el} />\n `
120
- }
121
- else {
122
- outputStr += ` ` ;
123
- if (el .children .length ){
124
- outputStr += htmlElementMap[el .text ][0 ];
125
- outputStr += ' \n '
126
- outputStr += writeNested (el .children ,` ` )
116
+ } else {
127
117
outputStr += ` `
128
- outputStr += htmlElementMap[el .text ][1 ];
129
- outputStr += ` \n ` ;
130
- }
131
- else {
132
- outputStr += htmlElementMap[el .text ][0 ]+ htmlElementMap[el .text ][1 ]+ ' \n ' ;
118
+ if (el .children .length ) {
119
+ outputStr += htmlElementMap[el .text ][0 ]
120
+ outputStr += ' \n '
121
+ outputStr += writeNested (el .children , ` ` )
122
+ outputStr += ` `
123
+ outputStr += htmlElementMap[el .text ][1 ]
124
+ outputStr += ` \n `
125
+ } else {
126
+ outputStr += htmlElementMap[el .text ][0 ] + htmlElementMap[el .text ][1 ] + ' \n '
133
127
}
134
128
}
135
129
}
136
130
// console.log(`outputStr from writeTemplateTag: ${outputStr}`)
137
- return outputStr;
131
+ return outputStr
138
132
},
139
- createBoiler (componentName , children ) {
140
- let str = ' ' ;
133
+ createBoiler (componentName , children ) {
134
+ let str = ' '
141
135
children .forEach (name => {
142
- str += ` import ${ name} from '@/components/${ name} .vue';\n ` ;
143
- });
144
- let childrenComponentNames = ' ' ;
136
+ str += ` import ${ name} from '@/components/${ name} .vue';\n `
137
+ })
138
+ let childrenComponentNames = ' '
145
139
children .forEach (name => {
146
- childrenComponentNames += ` ${ name} ,\n ` ;
147
- });
148
- return ` \n\n <script>\n ${ str} \n export default {\n name: '${ componentName} ',\n components: {\n ${ childrenComponentNames} }\n };\n <\/ script>\n\n <style scoped>\n </style>` ;
149
- },
140
+ childrenComponentNames += ` ${ name} ,\n `
141
+ })
142
+ return ` \n\n <script>\n ${ str} \n export default {\n name: '${ componentName} ',\n components: {\n ${ childrenComponentNames} }\n };\n <\/ script>\n\n <style scoped>\n </style>`
143
+ }
150
144
},
151
- mounted () {
145
+ mounted () {
152
146
// https://vuejs.org/v2/api/#Vue-nextTick
153
147
// kinda like a promise, used for the window resize
154
148
this .$nextTick (() => {
155
- window .addEventListener (' resize' , this .getWindowHeight );
149
+ window .addEventListener (' resize' , this .getWindowHeight )
156
150
157
- this .getWindowHeight ();
158
- });
151
+ this .getWindowHeight ()
152
+ })
159
153
},
160
154
// updates code snippet, but broken cause children undefined, shows `function () { [native code] }`
161
- updated () {
155
+ updated () {
162
156
// console.log(`code: ${this.createCodeSnippet(this.activeComponent, this.componentMap[this.activeComponent].children)}`)
163
157
// console.log('component map: ', this.componentMap);
164
158
if (this .componentMap [this .activeComponent ]) {
165
159
this .code = ` ${ this .createCodeSnippet (
166
160
this .activeComponent ,
167
161
this .componentMap [this .activeComponent ].children
168
- )} ` ;
162
+ )} `
169
163
} else {
170
- this .code = ` Your component boilerplate will be displayed here.` ;
164
+ this .code = ` Your component boilerplate will be displayed here.`
171
165
}
172
166
},
173
- beforeDestroy () {
174
- window .removeEventListener (' resize' , this .getWindowHeight );
167
+ beforeDestroy () {
168
+ window .removeEventListener (' resize' , this .getWindowHeight )
175
169
},
176
170
watch: {
177
171
componentMap: {
178
172
deep: true ,
179
- handler () {
173
+ handler () {
180
174
// console.log('component Map has changed');
181
175
if (this .componentMap [this .activeComponent ]) {
182
176
this .code = ` ${ this .createCodeSnippet (
183
177
this .activeComponent ,
184
178
this .componentMap [this .activeComponent ].children
185
- )} ` ;
179
+ )} `
186
180
}
187
- },
181
+ }
188
182
},
189
- activeComponent : function () {
183
+ activeComponent : function () {
190
184
// console.log('watching active component');
191
- },
192
- },
193
- };
185
+ }
186
+ }
187
+ }
194
188
</script >
195
189
196
190
<style lang="stylus" scoped>
0 commit comments