Skip to content

Commit dc93c88

Browse files
authored
Merge pull request #28 from drewngyen/master
added a few changes to spacing in code snippets
2 parents 830271f + 09eecf1 commit dc93c88

File tree

2 files changed

+88
-3
lines changed

2 files changed

+88
-3
lines changed

src/components/CodeSnippet.vue

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
<template>
22
<div>
33
<!-- <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+
/>
512
</div>
613
</template>
714

815
<script>
16+
import { mapState } from 'vuex'
917
import PrismEditor from 'vue-prism-editor'
1018
import 'prismjs'
1119
import 'prismjs/themes/prism-okaidia.css'
@@ -14,26 +22,95 @@ import 'vue-prism-editor/dist/VuePrismEditor.css'
1422
export default {
1523
data () {
1624
return {
17-
code: 'console.log(Hellor world);\naww yis\nsome boilerplate here',
25+
code: `Select a component`,
1826
lineNumbers: true,
1927
height: null
2028
}
2129
},
2230
components: {
2331
PrismEditor
2432
},
33+
computed: {
34+
// needs access to current component aka activeComponent
35+
...mapState(['componentMap', 'activeComponent'])
36+
},
2537
methods: {
2638
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
2841
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}\nexport default {\n name: '${componentName}',\n components: {\n${childrenComponentNames} }\n};\n<\/script>\n\n<style scoped>\n</style>`
2997
}
3098
},
3199
mounted () {
100+
// https://vuejs.org/v2/api/#Vue-nextTick
101+
// kinda like a promise, used for the window resize
32102
this.$nextTick(() => {
33103
window.addEventListener('resize', this.getWindowHeight)
34104
35105
this.getWindowHeight()
36106
})
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)}`
37114
},
38115
beforeDestroy () {
39116
window.removeEventListener('resize', this.getWindowHeight)
@@ -42,6 +119,7 @@ export default {
42119
</script>
43120

44121
<style lang="stylus" scoped>
122+
// resize handled by vue lifecycle methods
45123
.code-editor {
46124
}
47125
</style>

src/layouts/MyLayout.vue

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ export default {
5252
</script>
5353

5454
<style lang="stylus">
55+
.q-toolbar {
56+
min-height: 30px;
57+
}
58+
.deano-size {
59+
min-height: 2in
60+
height: 3in
61+
}
5562
// css styling for the drawer items
5663
.drawer-menu {
5764
background: white;

0 commit comments

Comments
 (0)