CodeSimulator.vue 3.5 KB
Newer Older
D
DCloud_LXH 已提交
1 2 3 4 5 6 7 8 9

<script>
export default {
	props:{
		src:{
			type:String,
			default:''
		}
	},
10 11 12 13 14
	data(){
		return {
			activeIndex: 0
		}
	},
D
DCloud_LXH 已提交
15 16 17 18 19 20 21
	mounted(){
		this.onWindowResize()
		window.addEventListener('resize', this.onWindowResize)
	},
	beforeDestroy(){
		window.removeEventListener('resize', this.onWindowResize)
	},
22
	methods:{
D
DCloud_LXH 已提交
23 24 25 26 27 28
		onWindowResize(){
			const contentWidth = getComputedStyle(document.querySelector('.theme-default-content')).width
			if (window.matchMedia('(max-width: 410px)').matches) {
				this.$refs.codeIframe.style.maxWidth = contentWidth
			}
		},
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
		onClick(index){
			this.activeIndex = index
		},
		createdDom(h,node){
			let headerDom = []
			node.forEach((v,index)=>{
				headerDom.push(h('p',{class:`pages-tabs-header-text ${this.activeIndex === index?'pages-tabs--active':''}`,on:{click:(e)=>{
						this.onClick(index)
				}}},v.title),)
			})
			return this.renderDom(h,h('div',{class:'page-tabs'},[
				h('div',{class:'pages-tabs-header'},headerDom),
				h('div',{class:'page-snippet-code',key:this.activeIndex},[node[this.activeIndex].node]),
			]))
		},
		renderDom(h,node){
			return h('div',{class:'page-runtime'},
				[
					h('div',{class:'page-snippet'},[node]),
					h('div',{class:'code-content',style:{display:this.src?'block':'none'}},[
						h('iframe',{class:'code-iframe',attrs:{
							src:this.src,
							frameborder:'0'
D
DCloud_LXH 已提交
52
						},ref:'codeIframe'})
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
					]),
				
				]
			)
		}
	},
	render(h){
		const columns = this.$slots.default || []
		let boxObj = []
		let realDom = []
		columns.forEach((v,i)=>{
			if(v.tag && v.children){
				realDom.push(v)
			}
		})
		realDom.forEach((vnode,index)=>{
			let code = vnode.children[0]
			if(vnode.tag === 'div' && code.tag === 'pre'){
				let i = index - 1
				if(i >= 0){
					let textDom = realDom[i]
					if(textDom.tag === 'blockquote'){
						let text = textDom.children[0]
						let p = text.children[0]
						boxObj.push({
							title:p.text,
							node:vnode
						})
					}
					
				}
			}
		})
		if(boxObj.length > 0){
			return h('div',null,[this.createdDom(h,boxObj)])
		}else{
			if(this.src){
				return this.renderDom(h,this.$slots.default)
			}else{
				return h('div',null,this.$slots.default)
			}
		}
D
DCloud_LXH 已提交
95 96 97 98
	}
}
</script>

D
DCloud_LXH 已提交
99
<style lang="stylus" scoped>
D
DCloud_LXH 已提交
100 101 102
.page-runtime {
	display: flex;
	height: 667px;
103
	border: 1px #eee solid;
M
mehaotian 已提交
104
	margin-top: 16px;
D
DCloud_LXH 已提交
105 106 107 108 109 110
}
.page-snippet {
	width: 100%;
	overflow: hidden;

}
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
.page-snippet-code {
	height: calc(100% - 50px);
}

.page-tabs {
	height: 100%;
	box-sizing: border-box;
}
.pages-tabs-header {
	display: flex;
	height: 50px;
	background-color: #222;
}
.pages-tabs-header-text {
	display: flex;
	flex-direction: column;
	justify-content: center;
	margin: 5px;
	margin-bottom: 0px;
	border-top-left-radius: 5px;
	border-top-right-radius: 5px;
	padding: 0 45px;
	text-align: center;
	font-size: 18px;
	color: #eee;
	background:transparent;
	cursor: pointer;
	-moz-user-select:none; /*火狐*/
    -webkit-user-select:none; /*webkit浏览器*/
    -ms-user-select:none; /*IE10*/
    -khtml-user-select:none; /*早期浏览器*/
    user-select:none;
}
.pages-tabs--active {
M
mehaotian 已提交
145 146
	// background:#282c34;
	background-color: #292d3e;
147 148 149 150
	color: #fff;
	font-weight: bold;
}

D
DCloud_LXH 已提交
151 152 153 154
.page-snippet div[class*="language-"]{
	width: 100%;
	height: 100%;
	border-radius: 0;
M
mehaotian 已提交
155
	overflow: auto;
D
DCloud_LXH 已提交
156 157 158 159

}
.page-snippet pre[class*="language-"]{
	margin: 0;
160
	padding: 20px 20px;
D
DCloud_LXH 已提交
161 162
	width: 100%;
	height: 100%;
M
mehaotian 已提交
163
	// overflow: auto;
D
DCloud_LXH 已提交
164 165 166 167 168 169 170
	box-sizing: border-box;
}
.code-iframe {
	flex-shrink: 0;
	width: 375px;
	height: 667px;
}
D
DCloud_LXH 已提交
171 172 173 174 175

@media (max-width: $MQMobileNarrow)
	{$contentClass}
    div[class*="language-"]
			margin 0 !important
D
DCloud_LXH 已提交
176
</style>