ant-design/site/theme/template/Content/Demo.jsx

165 lines
4.6 KiB
React
Raw Normal View History

2016-02-29 14:08:40 +08:00
import React from 'react';
import ReactDOM from 'react-dom';
import { FormattedMessage } from 'react-intl';
import CopyToClipboard from 'react-copy-to-clipboard';
import classNames from 'classnames';
2017-03-02 15:08:42 +08:00
import { Icon, Tooltip } from 'antd';
import EditButton from './EditButton';
2017-03-18 15:15:00 +08:00
import BrowserFrame from '../BrowserFrame';
2016-05-11 11:36:47 +08:00
2016-02-29 14:08:40 +08:00
export default class Demo extends React.Component {
2016-04-20 16:28:07 +08:00
static contextTypes = {
intl: React.PropTypes.object,
}
2016-03-01 14:53:32 +08:00
constructor(props) {
super(props);
this.state = {
codeExpand: false,
sourceCode: '',
2017-03-02 15:08:42 +08:00
copied: false,
copyTooltipVisible: false,
2016-03-01 14:53:32 +08:00
};
}
componentWillReceiveProps(nextProps) {
const { highlightedCode } = nextProps;
const div = document.createElement('div');
div.innerHTML = highlightedCode[1].highlighted;
this.setState({ sourceCode: div.textContent });
}
shouldComponentUpdate(nextProps, nextState) {
2017-03-02 15:08:42 +08:00
return (this.state.codeExpand || this.props.expand) !== (nextState.codeExpand || nextProps.expand)
|| this.state.copied !== nextState.copied
|| this.state.copyTooltipVisible !== nextState.copyTooltipVisible;
}
componentDidMount() {
const { meta, location } = this.props;
if (meta.id === location.hash.slice(1)) {
this.anchor.click();
}
this.componentWillReceiveProps(this.props);
}
2016-03-30 16:16:18 +08:00
handleCodeExapnd = () => {
this.setState({ codeExpand: !this.state.codeExpand });
}
saveAnchor = (anchor) => {
this.anchor = anchor;
}
2017-03-02 15:08:42 +08:00
handleCodeCopied = () => {
this.setState({ copied: true });
}
onCopyTooltipVisibleChange = (visible) => {
if (visible) {
this.setState({
copyTooltipVisible: visible,
copied: false,
});
return;
}
this.setState({
copyTooltipVisible: visible,
});
}
2016-02-29 14:08:40 +08:00
render() {
const state = this.state;
const props = this.props;
const {
meta,
src,
content,
preview,
highlightedCode,
style,
highlightedStyle,
2016-09-19 11:23:41 +08:00
expand,
} = props;
if (!this.liveDemo) {
2017-03-18 15:15:00 +08:00
this.liveDemo = meta.iframe
? <BrowserFrame><iframe src={src} height={meta.iframe} /></BrowserFrame>
: preview(React, ReactDOM);
}
const codeExpand = state.codeExpand || expand;
const codeBoxClass = classNames({
'code-box': true,
expand: codeExpand,
});
2016-05-20 18:03:54 +08:00
2016-04-20 16:28:07 +08:00
const locale = this.context.intl.locale;
const localizedTitle = meta.title[locale] || meta.title;
const localizeIntro = content[locale] || content;
const introChildren = props.utils
2016-07-30 14:31:36 +08:00
.toReactComponent(['div'].concat(localizeIntro));
const highlightClass = classNames({
'highlight-wrapper': true,
'highlight-wrapper-expand': codeExpand,
});
2016-02-29 14:08:40 +08:00
return (
<section className={codeBoxClass} id={meta.id}>
2016-02-29 14:08:40 +08:00
<section className="code-box-demo">
{this.liveDemo}
2016-03-01 17:42:23 +08:00
{
2016-08-23 21:00:35 +08:00
style ?
2016-03-01 17:42:23 +08:00
<style dangerouslySetInnerHTML={{ __html: style }} /> :
null
}
2016-02-29 14:08:40 +08:00
</section>
<section className="code-box-meta markdown">
<div className="code-box-title">
<a href={`#${meta.id}`} ref={this.saveAnchor}>
2016-05-03 10:34:44 +08:00
{localizedTitle}
</a>
<EditButton title={<FormattedMessage id="app.content.edit-page" />} filename={meta.filename} />
2016-02-29 14:08:40 +08:00
</div>
{introChildren}
2016-09-29 23:14:19 +08:00
<Icon type="down-circle-o" title="Show Code" className="collapse" onClick={this.handleCodeExapnd} />
2016-02-29 14:08:40 +08:00
</section>
<section className={highlightClass}
key="code"
>
2016-03-25 17:07:00 +08:00
<div className="highlight">
2017-02-24 19:05:26 +08:00
<CopyToClipboard
text={state.sourceCode}
2017-03-02 15:08:42 +08:00
onCopy={this.handleCodeCopied}
2017-02-24 19:05:26 +08:00
>
2017-03-02 15:08:42 +08:00
<Tooltip
visible={state.copyTooltipVisible}
onVisibleChange={this.onCopyTooltipVisibleChange}
title={
<FormattedMessage
id={`app.demo.${state.copied ? 'copied' : 'copy'}`}
/>
}
>
<Icon
type={(state.copied && state.copyTooltipVisible) ? 'check' : 'copy'}
className="code-box-code-copy"
/>
</Tooltip>
</CopyToClipboard>
{props.utils.toReactComponent(highlightedCode)}
2016-03-25 17:07:00 +08:00
</div>
{
2016-04-27 10:56:17 +08:00
highlightedStyle ?
2016-03-25 17:07:00 +08:00
<div key="style" className="highlight">
<pre>
2016-10-10 19:40:53 +08:00
<code className="css" dangerouslySetInnerHTML={{ __html: highlightedStyle }} />
2016-03-25 17:07:00 +08:00
</pre>
</div> :
null
}
2016-03-25 17:07:00 +08:00
</section>
2016-02-29 14:08:40 +08:00
</section>
);
}
}