1
1
<template >
2
2
<div >
3
3
<!-- <input type="checkbox" v-model="lineNumbers"> Linenumbers -->
4
- <prism-editor v-model =" code" language =" js" :line-numbers =" lineNumbers" class =" code-editor" :style =" { height: `${height}vh` }" />
4
+ {{ `${activeComponent}.vue` }}
5
+ <prism-editor
6
+ v-model =" code"
7
+ language =" js"
8
+ :line-numbers =" lineNumbers"
9
+ class =" code-editor"
10
+ :style =" { height: `${height}vh` }"
11
+ />
5
12
</div >
6
13
</template >
7
14
8
15
<script >
16
+ import { mapState } from ' vuex'
9
17
import PrismEditor from ' vue-prism-editor'
10
18
import ' prismjs'
11
19
import ' prismjs/themes/prism-okaidia.css'
@@ -14,26 +22,95 @@ import 'vue-prism-editor/dist/VuePrismEditor.css'
14
22
export default {
15
23
data () {
16
24
return {
17
- code: ' console.log(Hellor world); \n aww yis \n some boilerplate here ' ,
25
+ code: ` Select a component ` ,
18
26
lineNumbers: true ,
19
27
height: null
20
28
}
21
29
},
22
30
components: {
23
31
PrismEditor
24
32
},
33
+ computed: {
34
+ // needs access to current component aka activeComponent
35
+ ... mapState ([' componentMap' , ' activeComponent' ])
36
+ },
25
37
methods: {
26
38
getWindowHeight (e ) {
27
- let minHeight = (window .outerHeight < 900 ) ? 22 : (window .outerHeight < 1035 ) ? 24 : 27.5
39
+ let minHeight =
40
+ window .outerHeight < 900 ? 22 : window .outerHeight < 1035 ? 24 : 27.5
28
41
this .height = minHeight
42
+ },
43
+ // calls createTemplate and createBoiler to generate snippet
44
+ createCodeSnippet (componentName , children ) {
45
+ let result = ` ${ this .createTemplate (componentName, children)}${ this .createBoiler (componentName, children)} `
46
+ console .log (` createCodeSnippet result: ${ result} ` )
47
+ return result
48
+ },
49
+ createTemplate (componentName , children ) {
50
+ let output = ` `
51
+ // let htmlArr = this.componentMap[compName].htmlList;
52
+ output += ` <div>\n `
53
+ children .forEach (name => {
54
+ output += ` <${ name} >\n </${ name} >\n `
55
+ })
56
+ let templateTagStr = this .writeTemplateTag (componentName)
57
+ return ` <template>\n ${ output}${ templateTagStr} </div>\n </template>`
58
+ },
59
+ writeTemplateTag (componentName ) {
60
+ console .log (' writeTemplateTag invoked!' )
61
+ // create reference object
62
+ const htmlElementMap = {
63
+ div: [' <div>' , ' </div>' ],
64
+ button: [' <button>' , ' </button>' ],
65
+ form: [' <form>' , ' </form>' ],
66
+ img: [' <img>' , ' ' ],
67
+ link: [' <a href="#"/>' , ' ' ],
68
+ list: [' <li>' , ' </li>' ],
69
+ paragraph: [' <p>' , ' </p>' ],
70
+ ' list-ol' : [' <ol>' , ' </ol>' ],
71
+ ' list-ul' : [' <ul>' , ' </ul>' ],
72
+ input: [' <input />' , ' ' ],
73
+ navbar: [' <nav>' , ' </nav>' ]
74
+ }
75
+ // loop to iterate through compName arr
76
+ let htmlArr = this .componentMap [componentName].htmlList
77
+ let outputStr = ` `
78
+ for (let el of htmlArr) {
79
+ outputStr += ` `
80
+ outputStr += htmlElementMap[el .text ][0 ]
81
+ outputStr += htmlElementMap[el .text ][1 ]
82
+ outputStr += ` \n `
83
+ }
84
+ console .log (` outputStr from writeTemplateTag: ${ outputStr} ` )
85
+ return outputStr
86
+ },
87
+ createBoiler (componentName , children ) {
88
+ let str = ' '
89
+ children .forEach (name => {
90
+ str += ` import ${ name} from '@/components/${ name} .vue';\n `
91
+ })
92
+ let childrenComponentNames = ' '
93
+ children .forEach (name => {
94
+ childrenComponentNames += ` ${ name} ,\n `
95
+ })
96
+ 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>`
29
97
}
30
98
},
31
99
mounted () {
100
+ // https://vuejs.org/v2/api/#Vue-nextTick
101
+ // kinda like a promise, used for the window resize
32
102
this .$nextTick (() => {
33
103
window .addEventListener (' resize' , this .getWindowHeight )
34
104
35
105
this .getWindowHeight ()
36
106
})
107
+ // set code to this new string literal mofo
108
+ // this.code = `${this.createCodeSnippet(this.activeComponent, this.componentMap[this.activeComponent].children)}`
109
+ },
110
+ // updates code snippet, but broken cause children undefined, shows `function () { [native code] }`
111
+ updated () {
112
+ console .log (` code: ${ this .createCodeSnippet (this .activeComponent , this .componentMap [this .activeComponent ].children )} ` )
113
+ this .code = ` ${ this .createCodeSnippet (this .activeComponent , this .componentMap [this .activeComponent ].children )} `
37
114
},
38
115
beforeDestroy () {
39
116
window .removeEventListener (' resize' , this .getWindowHeight )
@@ -42,6 +119,7 @@ export default {
42
119
</script >
43
120
44
121
<style lang="stylus" scoped>
122
+ // resize handled by vue lifecycle methods
45
123
.code-editor {
46
124
}
47
125
</style >
0 commit comments