2016-02-29 14:08:40 +08:00
|
|
|
import React from 'react';
|
2016-03-07 17:33:38 +08:00
|
|
|
import { Link } from 'react-router';
|
2016-03-25 15:03:53 +08:00
|
|
|
import classNames from 'classnames';
|
2016-02-29 14:08:40 +08:00
|
|
|
import * as utils from '../utils';
|
|
|
|
|
2016-05-11 11:36:47 +08:00
|
|
|
const isLocal = location.port;
|
|
|
|
|
2016-02-29 14:08:40 +08:00
|
|
|
export default class Demo extends React.Component {
|
2016-03-01 14:53:32 +08:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
2016-03-25 15:03:53 +08:00
|
|
|
codeExpand: false,
|
2016-03-01 14:53:32 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-03-25 15:03:53 +08:00
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
if (nextProps.expand === undefined) return;
|
|
|
|
|
2016-03-01 14:53:32 +08:00
|
|
|
this.setState({
|
2016-03-25 15:03:53 +08:00
|
|
|
codeExpand: nextProps.expand,
|
2016-03-01 14:53:32 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-03-30 16:16:18 +08:00
|
|
|
handleCodeExapnd = () => {
|
2016-03-25 15:03:53 +08:00
|
|
|
this.setState({ codeExpand: !this.state.codeExpand });
|
|
|
|
}
|
|
|
|
|
2016-02-29 14:08:40 +08:00
|
|
|
render() {
|
2016-03-24 17:34:29 +08:00
|
|
|
const { id, className, meta, intro, preview, style, src,
|
2016-03-25 15:03:53 +08:00
|
|
|
highlightedCode, highlightedStyle, pathname } = this.props;
|
|
|
|
const codeExpand = this.state.codeExpand;
|
|
|
|
const codeBoxClass = classNames({
|
|
|
|
'code-box': true,
|
|
|
|
[className]: className,
|
|
|
|
expand: codeExpand,
|
|
|
|
});
|
2016-04-01 16:52:47 +08:00
|
|
|
const introChildren = utils.jsonmlToComponent(pathname, ['div'].concat(intro));
|
2016-02-29 14:08:40 +08:00
|
|
|
return (
|
2016-03-25 15:03:53 +08:00
|
|
|
<section className={codeBoxClass} id={id}>
|
2016-02-29 14:08:40 +08:00
|
|
|
<section className="code-box-demo">
|
2016-03-08 15:13:59 +08:00
|
|
|
{
|
2016-03-31 15:22:38 +08:00
|
|
|
meta.iframe ?
|
2016-05-11 11:36:47 +08:00
|
|
|
<iframe src={isLocal ? src : src.replace('./_site', '')} /> :
|
2016-03-08 15:13:59 +08:00
|
|
|
preview
|
|
|
|
}
|
2016-03-01 17:42:23 +08:00
|
|
|
{
|
|
|
|
!!style ?
|
|
|
|
<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">
|
2016-03-08 15:13:59 +08:00
|
|
|
<Link to={{ pathname, query: { scrollTo: id } }}>
|
2016-04-29 12:13:27 +08:00
|
|
|
{meta.title}
|
2016-03-08 15:13:59 +08:00
|
|
|
</Link>
|
2016-02-29 14:08:40 +08:00
|
|
|
</div>
|
2016-04-29 12:13:27 +08:00
|
|
|
{introChildren}
|
2016-03-25 15:03:53 +08:00
|
|
|
<span className="collapse anticon anticon-circle-o-right"
|
2016-03-30 16:16:18 +08:00
|
|
|
onClick={this.handleCodeExapnd}
|
2016-03-25 15:03:53 +08:00
|
|
|
unselectable="none" />
|
2016-02-29 14:08:40 +08:00
|
|
|
</section>
|
2016-03-25 17:07:00 +08:00
|
|
|
<section className={`highlight-wrapper ${codeExpand ? 'highlight-wrapper-expand' : ''}`}
|
|
|
|
key="code">
|
|
|
|
<div className="highlight">
|
|
|
|
<pre>
|
|
|
|
<code className="javascript" dangerouslySetInnerHTML={{
|
|
|
|
__html: highlightedCode,
|
|
|
|
}} />
|
|
|
|
</pre>
|
|
|
|
</div>
|
2016-03-25 15:03:53 +08:00
|
|
|
{
|
2016-04-27 10:56:17 +08:00
|
|
|
highlightedStyle ?
|
2016-03-25 17:07:00 +08:00
|
|
|
<div key="style" className="highlight">
|
|
|
|
<pre>
|
|
|
|
<code className="css" dangerouslySetInnerHTML={{
|
|
|
|
__html: highlightedStyle,
|
|
|
|
}} />
|
|
|
|
</pre>
|
|
|
|
</div> :
|
|
|
|
null
|
2016-03-25 15:03:53 +08:00
|
|
|
}
|
2016-03-25 17:07:00 +08:00
|
|
|
</section>
|
2016-02-29 14:08:40 +08:00
|
|
|
</section>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|