2018-04-04 18:39:21 +08:00
|
|
|
<template>
|
2019-02-01 17:23:00 +08:00
|
|
|
<section
|
|
|
|
:id="id"
|
|
|
|
:class="['code-box', codeExpand ? 'expand': '']"
|
|
|
|
>
|
2018-04-04 18:39:21 +08:00
|
|
|
<section class="code-box-demo">
|
2018-04-20 16:58:40 +08:00
|
|
|
<template v-if="iframeDemo[iframeDemoKey]">
|
|
|
|
<div class="browser-mockup with-url">
|
2019-02-01 17:23:00 +08:00
|
|
|
<iframe
|
|
|
|
:src="iframeDemo[iframeDemoKey]"
|
|
|
|
height="360"
|
|
|
|
/>
|
2018-04-20 16:58:40 +08:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
<template v-else>
|
2019-02-01 17:23:00 +08:00
|
|
|
<slot name="component" />
|
2018-04-20 16:58:40 +08:00
|
|
|
</template>
|
2018-04-04 18:39:21 +08:00
|
|
|
</section>
|
|
|
|
<section class="code-box-meta markdown">
|
2019-02-01 17:23:00 +08:00
|
|
|
<slot
|
|
|
|
v-if="isZhCN"
|
|
|
|
name="description"
|
|
|
|
/>
|
|
|
|
<slot
|
|
|
|
v-else
|
|
|
|
name="us-description"
|
|
|
|
/>
|
2019-01-12 17:19:57 +08:00
|
|
|
<a-tooltip :title="codeExpand ? 'Hide Code' : 'Show Code'">
|
|
|
|
<span class="code-expand-icon">
|
|
|
|
<img
|
2019-01-23 12:43:45 +08:00
|
|
|
width="16"
|
2019-01-12 17:19:57 +08: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 17:23:00 +08:00
|
|
|
>
|
2019-01-12 17:19:57 +08:00
|
|
|
<img
|
2019-01-23 12:43:45 +08:00
|
|
|
width="16"
|
2019-01-12 17:19:57 +08: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 17:23:00 +08:00
|
|
|
>
|
2019-01-12 17:19:57 +08:00
|
|
|
</span>
|
|
|
|
</a-tooltip>
|
2018-04-04 18:39:21 +08:00
|
|
|
</section>
|
2019-02-01 17:23:00 +08:00
|
|
|
<transition
|
|
|
|
appear
|
|
|
|
:css="false"
|
|
|
|
@enter="enter"
|
|
|
|
@leave="leave"
|
|
|
|
>
|
|
|
|
<section
|
|
|
|
v-show="codeExpand"
|
|
|
|
class="highlight-wrapper"
|
|
|
|
style="position: relative;"
|
|
|
|
>
|
2018-04-04 18:39:21 +08:00
|
|
|
<a-tooltip
|
|
|
|
:title="copied ? '复制成功' : '复制代码'"
|
|
|
|
:visible="copyTooltipVisible"
|
2018-08-19 15:57:58 +08:00
|
|
|
@visibleChange="onCopyTooltipVisibleChange"
|
2018-04-04 18:39:21 +08:00
|
|
|
>
|
|
|
|
<a-icon
|
|
|
|
v-clipboard:copy="sourceCode"
|
|
|
|
v-clipboard:success="handleCodeCopied"
|
2019-02-01 17:23:00 +08:00
|
|
|
:type="copied && copyTooltipVisible ? 'check' : 'copy'"
|
|
|
|
class="code-box-code-copy"
|
2018-04-04 18:39:21 +08:00
|
|
|
/>
|
|
|
|
</a-tooltip>
|
2019-02-01 17:23:00 +08:00
|
|
|
<slot name="code" />
|
2018-04-04 18:39:21 +08:00
|
|
|
</section>
|
|
|
|
</transition>
|
|
|
|
</section>
|
|
|
|
</template>
|
|
|
|
<script>
|
2019-01-12 11:33:27 +08:00
|
|
|
import animate from 'antd/_util/openAnimation';
|
|
|
|
import BaseMixin from 'antd/_util/BaseMixin';
|
|
|
|
import { isZhCN } from '../util';
|
2018-04-04 18:39:21 +08:00
|
|
|
export default {
|
2019-02-01 17:23:00 +08:00
|
|
|
name: 'DemoBox',
|
2018-04-04 18:39:21 +08:00
|
|
|
mixins: [BaseMixin],
|
|
|
|
props: {
|
|
|
|
jsfiddle: Object,
|
2018-04-20 16:58:40 +08:00
|
|
|
isIframe: Boolean,
|
2018-04-04 18:39:21 +08:00
|
|
|
},
|
2018-04-04 23:59:38 +08:00
|
|
|
inject: {
|
2018-04-20 16:58:40 +08:00
|
|
|
iframeDemo: { default: {}},
|
2018-07-13 21:55:29 +08:00
|
|
|
demoContext: { default: {}},
|
2018-04-04 23:59:38 +08:00
|
|
|
},
|
2018-04-04 18:39:21 +08:00
|
|
|
data () {
|
2019-01-12 11:33:27 +08:00
|
|
|
const { name = '' } = this.demoContext;
|
|
|
|
const { us, cn, sourceCode } = this.jsfiddle;
|
2018-05-08 11:20:07 +08:00
|
|
|
// let sourceCode = `<template>${html}</template>\n`
|
|
|
|
// sourceCode = script ? sourceCode + '\<script>' + script + '<\/script>' : sourceCode
|
|
|
|
// sourceCode = style ? sourceCode + '\<style>' + style + '<\/style>' : sourceCode
|
2019-01-12 11:33:27 +08:00
|
|
|
const usTitle = (us.split('#### ')[1] || '').split('\n')[0] || '';
|
|
|
|
const cnTitle = (cn.split('#### ')[1] || '').split('\n')[0] || '';
|
2018-04-06 20:56:19 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production' && usTitle === '') {
|
|
|
|
throw new Error(
|
|
|
|
`not have usTitle`,
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-04-06 20:56:19 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
const iframeDemoKey = usTitle.split(' ').join('-').toLowerCase();
|
|
|
|
const id = ['components', name.replace(/-cn\/?$/, ''), 'demo', ...usTitle.split(' ')].join('-').toLowerCase();
|
2018-04-07 17:38:39 +08:00
|
|
|
|
2018-09-05 22:43:26 +08:00
|
|
|
if (this.demoContext.store) {
|
2019-01-12 11:33:27 +08:00
|
|
|
const { currentSubMenu } = this.demoContext.store.getState();
|
2018-04-07 19:16:21 +08:00
|
|
|
// id = `${id}-${currentSubMenu.length + 1}`
|
2019-01-12 11:33:27 +08:00
|
|
|
this.demoContext.store.setState({ currentSubMenu: [...currentSubMenu, { cnTitle, usTitle, id }] });
|
2018-04-04 23:59:38 +08:00
|
|
|
}
|
2018-04-04 18:39:21 +08:00
|
|
|
return {
|
2019-01-12 17:19:57 +08:00
|
|
|
codeExpand: false,
|
2018-04-04 18:39:21 +08:00
|
|
|
isZhCN: isZhCN(name),
|
|
|
|
copied: false,
|
|
|
|
copyTooltipVisible: false,
|
|
|
|
sourceCode,
|
2018-04-04 23:59:38 +08:00
|
|
|
id,
|
2018-04-20 16:58:40 +08:00
|
|
|
iframeDemoKey,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-04-04 18:39:21 +08:00
|
|
|
},
|
|
|
|
methods: {
|
2019-01-12 17:19:57 +08:00
|
|
|
handleCodeExpand () {
|
|
|
|
this.codeExpand = !this.codeExpand;
|
2018-04-04 18:39:21 +08:00
|
|
|
},
|
|
|
|
enter: animate.enter,
|
|
|
|
leave: animate.leave,
|
|
|
|
handleCodeCopied () {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.setState({ copied: true });
|
2018-04-04 18:39:21 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
onCopyTooltipVisibleChange (visible) {
|
|
|
|
if (visible) {
|
|
|
|
this.setState({
|
|
|
|
copyTooltipVisible: visible,
|
|
|
|
copied: false,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
|
|
|
return;
|
2018-04-04 18:39:21 +08:00
|
|
|
}
|
|
|
|
this.setState({
|
|
|
|
copyTooltipVisible: visible,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-04-04 18:39:21 +08:00
|
|
|
},
|
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-04-04 18:39:21 +08: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>
|