ant-design-vue/site/components/demoBox.vue

231 lines
5.7 KiB
Vue
Raw Normal View History

2018-04-04 10:39:21 +00:00
<template>
2019-02-01 09:23:00 +00:00
<section
:id="id"
:class="['code-box', codeExpand ? 'expand': '']"
>
2018-04-04 10:39:21 +00:00
<section class="code-box-demo">
2018-04-20 08:58:40 +00:00
<template v-if="iframeDemo[iframeDemoKey]">
<div class="browser-mockup with-url">
2019-02-01 09:23:00 +00:00
<iframe
:src="iframeDemo[iframeDemoKey]"
height="360"
/>
2018-04-20 08:58:40 +00:00
</div>
</template>
<template v-else>
2019-02-01 09:23:00 +00:00
<slot name="component" />
2018-04-20 08:58:40 +00:00
</template>
2018-04-04 10:39:21 +00:00
</section>
<section class="code-box-meta markdown">
2019-02-01 09:23:00 +00:00
<slot
v-if="isZhCN"
name="description"
/>
<slot
v-else
name="us-description"
/>
2019-01-12 09:19:57 +00:00
<a-tooltip :title="codeExpand ? 'Hide Code' : 'Show Code'">
<span class="code-expand-icon">
<img
2019-01-23 04:43:45 +00:00
width="16"
2019-01-12 09:19:57 +00:00
alt="expand code"
src="https://gw.alipayobjects.com/zos/rmsportal/wSAkBuJFbdxsosKKpqyq.svg"
:class="codeExpand ? 'code-expand-icon-hide' : 'code-expand-icon-show'"
@click="handleCodeExpand"
2019-02-01 09:23:00 +00:00
>
2019-01-12 09:19:57 +00:00
<img
2019-01-23 04:43:45 +00:00
width="16"
2019-01-12 09:19:57 +00:00
alt="expand code"
src="https://gw.alipayobjects.com/zos/rmsportal/OpROPHYqWmrMDBFMZtKF.svg"
:class="codeExpand ? 'code-expand-icon-show' : 'code-expand-icon-hide'"
@click="handleCodeExpand"
2019-02-01 09:23:00 +00:00
>
2019-01-12 09:19:57 +00:00
</span>
</a-tooltip>
2018-04-04 10:39:21 +00:00
</section>
2019-02-01 09:23:00 +00:00
<transition
appear
:css="false"
@enter="enter"
@leave="leave"
>
<section
v-show="codeExpand"
class="highlight-wrapper"
style="position: relative;"
>
2018-04-04 10:39:21 +00:00
<a-tooltip
:title="copied ? '复制成功' : '复制代码'"
:visible="copyTooltipVisible"
2018-08-19 07:57:58 +00:00
@visibleChange="onCopyTooltipVisibleChange"
2018-04-04 10:39:21 +00:00
>
<a-icon
v-clipboard:copy="sourceCode"
v-clipboard:success="handleCodeCopied"
2019-02-01 09:23:00 +00:00
:type="copied && copyTooltipVisible ? 'check' : 'copy'"
class="code-box-code-copy"
2018-04-04 10:39:21 +00:00
/>
</a-tooltip>
2019-02-01 09:23:00 +00:00
<slot name="code" />
2018-04-04 10:39:21 +00:00
</section>
</transition>
</section>
</template>
<script>
2019-01-12 03:33:27 +00:00
import animate from 'antd/_util/openAnimation';
import BaseMixin from 'antd/_util/BaseMixin';
import { isZhCN } from '../util';
2018-04-04 10:39:21 +00:00
export default {
2019-02-01 09:23:00 +00:00
name: 'DemoBox',
2018-04-04 10:39:21 +00:00
mixins: [BaseMixin],
props: {
jsfiddle: Object,
2018-04-20 08:58:40 +00:00
isIframe: Boolean,
2018-04-04 10:39:21 +00:00
},
2018-04-04 15:59:38 +00:00
inject: {
2018-04-20 08:58:40 +00:00
iframeDemo: { default: {}},
2018-07-13 13:55:29 +00:00
demoContext: { default: {}},
2018-04-04 15:59:38 +00:00
},
2018-04-04 10:39:21 +00:00
data () {
2019-01-12 03:33:27 +00:00
const { name = '' } = this.demoContext;
const { us, cn, sourceCode } = this.jsfiddle;
2018-05-08 03:20:07 +00:00
// let sourceCode = `<template>${html}</template>\n`
// sourceCode = script ? sourceCode + '\<script>' + script + '<\/script>' : sourceCode
// sourceCode = style ? sourceCode + '\<style>' + style + '<\/style>' : sourceCode
2019-01-12 03:33:27 +00:00
const usTitle = (us.split('#### ')[1] || '').split('\n')[0] || '';
const cnTitle = (cn.split('#### ')[1] || '').split('\n')[0] || '';
2018-04-06 12:56:19 +00:00
if (process.env.NODE_ENV !== 'production' && usTitle === '') {
throw new Error(
`not have usTitle`,
2019-01-12 03:33:27 +00:00
);
2018-04-06 12:56:19 +00:00
}
2019-01-12 03:33:27 +00:00
const iframeDemoKey = usTitle.split(' ').join('-').toLowerCase();
const id = ['components', name.replace(/-cn\/?$/, ''), 'demo', ...usTitle.split(' ')].join('-').toLowerCase();
2018-04-07 09:38:39 +00:00
2018-09-05 14:43:26 +00:00
if (this.demoContext.store) {
2019-01-12 03:33:27 +00:00
const { currentSubMenu } = this.demoContext.store.getState();
2018-04-07 11:16:21 +00:00
// id = `${id}-${currentSubMenu.length + 1}`
2019-01-12 03:33:27 +00:00
this.demoContext.store.setState({ currentSubMenu: [...currentSubMenu, { cnTitle, usTitle, id }] });
2018-04-04 15:59:38 +00:00
}
2018-04-04 10:39:21 +00:00
return {
2019-01-12 09:19:57 +00:00
codeExpand: false,
2018-04-04 10:39:21 +00:00
isZhCN: isZhCN(name),
copied: false,
copyTooltipVisible: false,
sourceCode,
2018-04-04 15:59:38 +00:00
id,
2018-04-20 08:58:40 +00:00
iframeDemoKey,
2019-01-12 03:33:27 +00:00
};
2018-04-04 10:39:21 +00:00
},
methods: {
2019-01-12 09:19:57 +00:00
handleCodeExpand () {
this.codeExpand = !this.codeExpand;
2018-04-04 10:39:21 +00:00
},
enter: animate.enter,
leave: animate.leave,
handleCodeCopied () {
2019-01-12 03:33:27 +00:00
this.setState({ copied: true });
2018-04-04 10:39:21 +00:00
},
onCopyTooltipVisibleChange (visible) {
if (visible) {
this.setState({
copyTooltipVisible: visible,
copied: false,
2019-01-12 03:33:27 +00:00
});
return;
2018-04-04 10:39:21 +00:00
}
this.setState({
copyTooltipVisible: visible,
2019-01-12 03:33:27 +00:00
});
2018-04-04 10:39:21 +00:00
},
},
2019-01-12 03:33:27 +00:00
};
2018-04-04 10:39:21 +00:00
</script>
<style scoped lang="less">
.box-demo {
padding: 0;
border: 1px solid #e9e9e9;
border-radius: 4px;
box-shadow: none;
margin-top: 20px;
margin-bottom: 20px;
}
.box-demo-show {
padding: 20px 25px 30px;
border-bottom: 1px solid #e9e9e9;
}
.box-demo-description {
position: relative;
padding: 17px 16px 15px 20px;
border-radius: 0 0 6px 6px;
-webkit-transition: background-color 0.4s ease;
transition: background-color 0.4s ease;
width: 100%;
font-size: 12px;
&.bordered {
border-bottom: 1px dashed #e9e9e9;
}
h3, h4 {
position: absolute;
top: -14px;
padding: 1px 8px;
margin-left: -8px;
margin-top: 0;
margin-bottom: 0;
color: #777;
border-radius: 4px;
border-top-left-radius: 0;
background: #fff;
-webkit-transition: background-color 0.4s ease;
transition: background-color 0.4s ease;
.header-anchor {
display: none;
}
}
li {
line-height: 21px;
}
}
.box-demo-code {
-webkit-transition: height .2s ease-in-out;
transition: height .2s ease-in-out;
overflow: auto;
border-top: 1px dashed #e9e9e9;
pre {
margin: 0;
}
code {
margin: 0;
background: #f7f7f7;
padding: .2em .4em;
border-radius: 3px;
font-size: .9em;
border: 1px solid #eee;
}
}
.btn-toggle {
position: absolute;
right: 16px;
bottom: 17px;
cursor: pointer;
width: 18px;
height: 18px;
font-size: 18px;
line-height: 18px;
color: #999;
i {
-webkit-transition: all 0.3s;
transition: all 0.3s;
}
&.open {
i {
-webkit-transform: rotate(-180deg);
-ms-transform: rotate(-180deg);
transform: rotate(-180deg);
}
}
}
</style>