naive-ui/demo/utils/ComponentDemo.vue

158 lines
3.5 KiB
Vue
Raw Normal View History

2019-09-29 17:54:20 +08:00
<i18n>
{
2019-12-23 16:31:26 +08:00
"zh-CN": {
2019-09-29 17:54:20 +08:00
"show": "显示代码",
"hide": "收起代码"
},
2019-12-23 16:31:26 +08:00
"en-US": {
2019-09-29 17:54:20 +08:00
"show": "Show Code",
"hide": "Hide Code"
}
}
</i18n>
2019-09-17 19:24:39 +08:00
<template>
2019-12-22 23:19:08 +08:00
<n-card
2020-03-04 03:01:15 +08:00
v-if="isShow"
2019-12-22 23:19:08 +08:00
class="demo-card"
:segmented="{
footer: true
2019-09-17 19:24:39 +08:00
}"
2019-12-22 23:19:08 +08:00
:content-style="contentStyle"
2019-09-17 19:24:39 +08:00
>
2019-12-22 23:19:08 +08:00
<template v-slot:header>
2019-11-17 00:57:30 +08:00
<slot name="title" />
2019-12-22 23:19:08 +08:00
</template>
2020-03-09 17:18:01 +08:00
2019-12-22 23:19:08 +08:00
<template v-slot:header-extra>
2020-03-09 17:18:01 +08:00
<n-tooltip
:delay="300"
:placement="'top'"
:show-arrow="true"
:controller="controller"
>
<template v-slot:activator>
<a target="_blank" :href="gheUrl" style="margin-right:10px;">
<n-button
size="tiny"
ghost
>
<template v-slot:icon>
<open-outline />
</template>
</n-button>
</a>
</template>
2020-03-13 05:21:12 +08:00
{{ $t('ghe url') }} {{ name }}
2020-03-09 17:18:01 +08:00
</n-tooltip>
2019-09-21 17:03:37 +08:00
<n-tooltip
2019-09-28 13:47:54 +08:00
:delay="300"
2019-09-21 17:03:37 +08:00
:placement="'top'"
2020-01-30 17:38:14 +08:00
:show-arrow="true"
2019-09-29 19:07:16 +08:00
:controller="controller"
2019-09-21 17:03:37 +08:00
>
<template v-slot:activator>
<n-button
size="tiny"
ghost
circle
@click="toggleCodeDisplay"
2019-10-24 18:04:31 +08:00
>
<template v-slot:icon>
<md-code />
</template>
</n-button>
2019-09-21 17:03:37 +08:00
</template>
2019-09-29 17:54:20 +08:00
{{ !showCode ? $t('show') : $t('hide') }}
2019-09-21 17:03:37 +08:00
</n-tooltip>
2019-12-22 23:19:08 +08:00
</template>
<slot name="content" />
<slot name="demo" />
2019-12-23 22:18:04 +08:00
<template v-if="showCode" v-slot:footer>
2019-09-27 15:56:33 +08:00
<n-scrollbar>
<slot name="code" />
</n-scrollbar>
2019-12-22 23:19:08 +08:00
</template>
</n-card>
2019-09-17 19:24:39 +08:00
</template>
<script>
2020-02-12 13:44:53 +08:00
import mdCode from '../../src/_icons/md-code'
2020-03-09 17:18:01 +08:00
import openOutline from '../../src/_icons/open-outline'
import { state } from '../store'
2019-09-17 19:24:39 +08:00
export default {
2019-10-24 18:04:31 +08:00
components: {
2020-03-09 17:18:01 +08:00
mdCode,
openOutline
2019-10-24 18:04:31 +08:00
},
2019-10-14 17:49:23 +08:00
inject: {
NDocumentation: {
default: null
}
},
2019-09-17 19:24:39 +08:00
data () {
return {
2019-09-20 00:22:55 +08:00
showCode: false,
2019-12-22 23:19:08 +08:00
contentStyle: null,
2020-03-04 03:01:15 +08:00
controller: {},
isShow: true,
name: '',
isDebug: false,
state: state
2020-03-04 03:01:15 +08:00
}
},
computed: {
mode () {
return this.state.mode
2020-03-09 17:18:01 +08:00
},
gheUrl () {
const resourcePath = this.NDocumentation.url
const reg = /demo.*/
2020-03-13 05:21:12 +08:00
const path = resourcePath.match(reg)[0].replace('index.md', this.name + '.md')
2020-03-09 17:18:01 +08:00
return 'https://***REMOVED***/tree/develop/' + path
2019-09-17 19:24:39 +08:00
}
},
2019-09-20 00:22:55 +08:00
watch: {
2019-09-29 19:07:16 +08:00
showCode () {
2019-12-22 23:19:08 +08:00
this.contentStyle = {
transition: 'none'
}
2019-09-29 19:07:16 +08:00
this.$nextTick().then(() => {
this.controller.updatePosition()
2019-12-22 23:19:08 +08:00
this.contentStyle = null
2019-09-29 19:07:16 +08:00
})
2020-03-04 03:01:15 +08:00
},
mode () {
this.init()
2019-09-20 00:22:55 +08:00
}
},
mounted () {
2020-03-04 03:01:15 +08:00
this.name = this.$el.id
this.init()
2019-09-20 00:22:55 +08:00
},
2019-09-17 19:24:39 +08:00
methods: {
2019-09-20 00:22:55 +08:00
toggleCodeDisplay () {
2019-09-17 19:24:39 +08:00
this.showCode = !this.showCode
2020-03-04 03:01:15 +08:00
},
init () {
2020-03-13 05:21:12 +08:00
console.log('this111111', this.NDocumentation.url)
2020-03-04 03:01:15 +08:00
const map = this.NDocumentation.anchorLinkMap
2020-03-05 13:46:19 +08:00
this.isDebug = this.name && (~this.name.indexOf('debug') || ~this.name.indexOf('Debug'))
2020-03-04 03:01:15 +08:00
if (this.isDebug) {
if (this.mode === 'debug') {
this.isShow = true
map.set(this.name, String(this.$scopedSlots.title()[0].text).trim())
} else {
this.isShow = false
map.delete(this.name)
}
} else {
map.set(this.name, String(this.$scopedSlots.title()[0].text).trim())
}
this.NDocumentation.anchorLinkMap = new Map(map, this.$scopedSlots.title()[0].text.trim())
2019-09-17 19:24:39 +08:00
}
}
}
</script>