2020-08-13 15:18:26 +08:00
< template >
< div
class = "demo-block"
: class = "[blockClass, { 'hover': hovering }]"
@ mouseenter = "hovering = true"
@ mouseleave = "hovering = false"
>
< div class = "source" >
< slot name = "source" > < / slot >
< / div >
< div ref = "meta" class = "meta" >
< div v-if ="$slots.default" class="description" >
< slot > < / slot >
< / div >
< div class = "highlight" >
< slot name = "highlight" > < / slot >
< / div >
< / div >
< div
ref = "control"
class = "demo-block-control"
: class = "{ 'is-fixed': fixedControl }"
@ click = "isExpanded = !isExpanded"
>
< transition name = "arrow-slide" >
< i : class = "[iconClass, { 'hovering': hovering }]" > < / i >
< / transition >
< transition name = "text-slide" >
< span v-show ="hovering" > {{ controlText }} < / span >
< / transition >
< el -tooltip effect = "dark" :content ="langConfig['tooltip-text']" placement = "right" >
< transition name = "text-slide" >
< el -button
v - show = "hovering || isExpanded"
size = "small"
type = "text"
class = "control-button"
@ click . stop = "goCodepen"
>
{ { langConfig [ 'button-text' ] } }
< / e l - b u t t o n >
< / transition >
< / e l - t o o l t i p >
< / div >
< / div >
< / template >
< script >
import { nextTick } from 'vue'
2020-09-28 16:09:10 +08:00
import hljs from 'highlight.js'
2020-08-13 15:18:26 +08:00
import compoLang from '../i18n/component.json'
import { stripScript , stripStyle , stripTemplate } from '../util'
const version = '1.0.0' // element version
2020-12-27 23:54:01 +08:00
const stripTemplateAndRemoveTemplate = code => {
const result = stripTemplate ( code )
if ( result . indexOf ( '<template>' ) === 0 ) {
return result . replace ( /^<template>/ , '' ) . replace ( /<\/template>$/ , '' )
}
return result
}
2020-08-13 15:18:26 +08:00
export default {
data ( ) {
return {
codepen : {
script : '' ,
html : '' ,
style : '' ,
} ,
hovering : false ,
isExpanded : false ,
fixedControl : false ,
scrollParent : null ,
}
} ,
computed : {
lang ( ) {
return this . $route . path . split ( '/' ) [ 1 ]
} ,
langConfig ( ) {
return compoLang . filter ( config => config . lang === this . lang ) [ 0 ] [ 'demo-block' ]
} ,
blockClass ( ) {
return ` demo- ${ this . lang } demo- ${ this . $router . currentRoute . value . path . split ( '/' ) . pop ( ) } `
} ,
iconClass ( ) {
return this . isExpanded ? 'el-icon-caret-top' : 'el-icon-caret-bottom'
} ,
controlText ( ) {
return this . isExpanded ? this . langConfig [ 'hide-text' ] : this . langConfig [ 'show-text' ]
} ,
codeArea ( ) {
return this . $el . getElementsByClassName ( 'meta' ) [ 0 ]
} ,
codeAreaHeight ( ) {
if ( this . $el . getElementsByClassName ( 'description' ) . length > 0 ) {
return this . $el . getElementsByClassName ( 'description' ) [ 0 ] . clientHeight +
this . $el . getElementsByClassName ( 'highlight' ) [ 0 ] . clientHeight + 20
}
return this . $el . getElementsByClassName ( 'highlight' ) [ 0 ] . clientHeight
} ,
} ,
watch : {
isExpanded ( val ) {
this . codeArea . style . height = val ? ` ${ this . codeAreaHeight + 1 } px ` : '0'
if ( ! val ) {
this . fixedControl = false
this . $refs . control . style . left = '0'
this . removeScrollHandler ( )
return
}
setTimeout ( ( ) => {
this . scrollParent = document . querySelector ( '.page-component__scroll > .el-scrollbar__wrap' )
this . scrollParent && this . scrollParent . addEventListener ( 'scroll' , this . scrollHandler )
this . scrollHandler ( )
} , 200 )
} ,
} ,
created ( ) {
const highlight = this . $slots . highlight ( )
if ( highlight && highlight [ 0 ] ) {
let code = ''
let cur = highlight [ 0 ]
2020-12-04 11:55:06 +08:00
if ( cur . type === 'pre' && ( cur . children && cur . children [ 0 ] ) ) {
2020-08-13 15:18:26 +08:00
cur = cur . children [ 0 ]
2020-12-04 11:55:06 +08:00
if ( cur . type === 'code' ) {
code = cur . children
2020-08-13 15:18:26 +08:00
}
}
if ( code ) {
2020-12-27 23:54:01 +08:00
this . codepen . html = stripTemplateAndRemoveTemplate ( code )
2020-08-13 15:18:26 +08:00
this . codepen . script = stripScript ( code )
this . codepen . style = stripStyle ( code )
}
}
} ,
mounted ( ) {
nextTick ( ( ) => {
let highlight = this . $el . getElementsByClassName ( 'highlight' ) [ 0 ]
if ( this . $el . getElementsByClassName ( 'description' ) . length === 0 ) {
highlight . style . width = '100%'
highlight . borderRight = 'none'
}
2020-09-28 16:09:10 +08:00
try {
hljs . highlightBlock ( highlight . querySelector ( 'code' ) )
} catch ( error ) {
console . log ( error )
}
2020-08-13 15:18:26 +08:00
} )
} ,
beforeUnmount ( ) {
this . removeScrollHandler ( )
} ,
methods : {
goCodepen ( ) {
// since 2.6.2 use code rather than jsfiddle https://blog.codepen.io/documentation/api/prefill/
const { script , html , style } = this . codepen
2021-01-07 20:24:44 +08:00
const resourcesTpl = '<scr' + 'ipt src="//unpkg.com/vue@next"></scr' + 'ipt>' +
2020-12-04 11:55:06 +08:00
'\n<scr' + ` ipt src="//unpkg.com/element-plus/lib/index.full.js"></scr ` + 'ipt>'
2020-08-13 15:18:26 +08:00
let htmlTpl = ` ${ resourcesTpl } \ n<div id="app"> \ n ${ html . trim ( ) } \ n</div> `
2020-12-04 11:55:06 +08:00
let cssTpl = ` @import url("//unpkg.com/element-plus/lib/theme-chalk/index.css"); \ n ${ ( style || '' ) . trim ( ) } \ n `
2020-12-14 13:47:19 +08:00
let jsTpl = script ? script . replace ( /export default/ , 'var Main =' ) . trim ( ) . replace ( /import ({.*}) from 'vue'/g , ( s , s1 ) => ` const ${ s1 } = Vue ` ) . replace ( /import ({.*}) from 'element-plus'/g , ( s , s1 ) => ` const ${ s1 } = ElementPlus ` ) : 'var Main = {}'
2020-12-04 11:55:06 +08:00
jsTpl += '\n;const app = Vue.createApp(Main);\napp.use(ElementPlus);\napp.mount("#app")'
2020-08-13 15:18:26 +08:00
const data = {
js : jsTpl ,
css : cssTpl ,
html : htmlTpl ,
}
const form = document . getElementById ( 'fiddle-form' ) || document . createElement ( 'form' )
while ( form . firstChild ) {
form . removeChild ( form . firstChild )
}
form . method = 'POST'
form . action = 'https://codepen.io/pen/define/'
form . target = '_blank'
form . style . display = 'none'
const input = document . createElement ( 'input' )
input . setAttribute ( 'name' , 'data' )
input . setAttribute ( 'type' , 'hidden' )
input . setAttribute ( 'value' , JSON . stringify ( data ) )
form . appendChild ( input )
document . body . appendChild ( form )
form . submit ( )
} ,
scrollHandler ( ) {
const { top , bottom , left } = this . $refs . meta . getBoundingClientRect ( )
this . fixedControl = bottom > document . documentElement . clientHeight &&
top + 44 <= document . documentElement . clientHeight
this . $refs . control . style . left = this . fixedControl ? ` ${ left } px ` : '0'
} ,
removeScrollHandler ( ) {
this . scrollParent && this . scrollParent . removeEventListener ( 'scroll' , this . scrollHandler )
} ,
} ,
}
< / script >
< style lang = "scss" scoped >
. demo - block {
border : solid 1 px # ebebeb ;
border - radius : 3 px ;
transition : .2 s ;
& . hover {
box - shadow : 0 0 8 px 0 rgba ( 232 , 237 , 250 , .6 ) , 0 2 px 4 px 0 rgba ( 232 , 237 , 250 , .5 ) ;
}
code {
font - family : Menlo , Monaco , Consolas , Courier , monospace ;
}
. demo - button {
float : right ;
}
. source {
padding : 24 px ;
}
. meta {
background - color : # fafafa ;
border - top : solid 1 px # eaeefb ;
overflow : hidden ;
height : 0 ;
transition : height .2 s ;
}
. description {
padding : 20 px ;
box - sizing : border - box ;
border : solid 1 px # ebebeb ;
border - radius : 3 px ;
font - size : 14 px ;
line - height : 22 px ;
color : # 666 ;
word - break : break - word ;
margin : 10 px ;
background - color : # fff ;
p {
margin : 0 ;
line - height : 26 px ;
}
code {
color : # 5 e6d82 ;
background - color : # e6effb ;
margin : 0 4 px ;
display : inline - block ;
padding : 1 px 5 px ;
font - size : 12 px ;
border - radius : 3 px ;
height : 18 px ;
line - height : 18 px ;
}
}
. highlight {
pre {
margin : 0 ;
}
code . hljs {
margin : 0 ;
border : none ;
max - height : none ;
border - radius : 0 ;
& : : before {
content : none ;
}
}
}
. demo - block - control {
border - top : solid 1 px # eaeefb ;
height : 44 px ;
box - sizing : border - box ;
background - color : # fff ;
border - bottom - left - radius : 4 px ;
border - bottom - right - radius : 4 px ;
text - align : center ;
margin - top : - 1 px ;
color : # d3dce6 ;
cursor : pointer ;
position : relative ;
& . is - fixed {
position : fixed ;
bottom : 0 ;
width : 868 px ;
}
i {
font - size : 16 px ;
line - height : 44 px ;
transition : .3 s ;
& . hovering {
transform : translateX ( - 40 px ) ;
}
}
> span {
position : absolute ;
transform : translateX ( - 30 px ) ;
font - size : 14 px ;
line - height : 44 px ;
transition : .3 s ;
display : inline - block ;
}
& : hover {
color : # 409 EFF ;
background - color : # f9fafc ;
}
& . text - slide - enter ,
& . text - slide - leave - active {
opacity : 0 ;
transform : translateX ( 10 px ) ;
}
. control - button {
line - height : 26 px ;
position : absolute ;
top : 0 ;
right : 0 ;
font - size : 14 px ;
padding - left : 5 px ;
padding - right : 25 px ;
}
}
}
< / style >