2016-02-29 14:08:40 +08:00
|
|
|
import React from 'react';
|
2016-05-27 11:48:08 +08:00
|
|
|
import ReactDOM from 'react-dom';
|
2016-09-18 14:27:41 +08:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
2016-03-25 15:03:53 +08:00
|
|
|
import classNames from 'classnames';
|
2016-09-29 23:14:19 +08:00
|
|
|
import { Icon } from 'antd';
|
2016-08-17 12:06:38 +08:00
|
|
|
import EditButton from './EditButton';
|
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 = {
|
2016-03-25 15:03:53 +08:00
|
|
|
codeExpand: false,
|
2016-03-01 14:53:32 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-09-20 15:01:25 +08:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
|
|
|
return (this.state.codeExpand || this.props.expand) !== (nextState.codeExpand || nextProps.expand);
|
|
|
|
}
|
|
|
|
|
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-05-27 11:48:08 +08:00
|
|
|
const props = this.props;
|
|
|
|
const {
|
|
|
|
meta,
|
|
|
|
src,
|
|
|
|
content,
|
|
|
|
preview,
|
|
|
|
highlightedCode,
|
|
|
|
style,
|
|
|
|
highlightedStyle,
|
2016-09-19 11:23:41 +08:00
|
|
|
expand,
|
2016-05-27 11:48:08 +08:00
|
|
|
} = props;
|
2016-10-13 16:52:24 +08:00
|
|
|
if (!this.liveDemo) {
|
|
|
|
this.liveDemo = meta.iframe ? <iframe src={src} /> : preview(React, ReactDOM);
|
|
|
|
}
|
2016-05-27 11:48:08 +08:00
|
|
|
|
2016-09-19 11:23:41 +08:00
|
|
|
const codeExpand = this.state.codeExpand || expand;
|
2016-03-25 15:03:53 +08:00
|
|
|
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;
|
2016-05-27 11:48:08 +08:00
|
|
|
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));
|
2016-05-27 11:48:08 +08:00
|
|
|
|
|
|
|
const highlightClass = classNames({
|
|
|
|
'highlight-wrapper': true,
|
|
|
|
'highlight-wrapper-expand': codeExpand,
|
|
|
|
});
|
2016-02-29 14:08:40 +08:00
|
|
|
return (
|
2016-05-27 11:48:08 +08:00
|
|
|
<section className={codeBoxClass} id={meta.id}>
|
2016-02-29 14:08:40 +08:00
|
|
|
<section className="code-box-demo">
|
2016-10-13 16:52:24 +08:00
|
|
|
{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">
|
2016-05-27 11:48:08 +08:00
|
|
|
<a href={`#${meta.id}`}>
|
2016-05-03 10:34:44 +08:00
|
|
|
{localizedTitle}
|
2016-05-27 11:48:08 +08:00
|
|
|
</a>
|
2016-09-18 14:27:41 +08:00
|
|
|
<EditButton title={<FormattedMessage id="app.content.edit-page" />} filename={meta.filename} />
|
2016-02-29 14:08:40 +08:00
|
|
|
</div>
|
2016-04-29 12:13:27 +08:00
|
|
|
{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>
|
2016-05-27 11:48:08 +08:00
|
|
|
<section className={highlightClass}
|
2016-06-06 13:54:10 +08:00
|
|
|
key="code"
|
|
|
|
>
|
2016-03-25 17:07:00 +08:00
|
|
|
<div className="highlight">
|
2016-05-27 11:48:08 +08:00
|
|
|
{props.utils.toReactComponent(highlightedCode)}
|
2016-03-25 17:07:00 +08:00
|
|
|
</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>
|
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 15:03:53 +08:00
|
|
|
}
|
2016-03-25 17:07:00 +08:00
|
|
|
</section>
|
2016-02-29 14:08:40 +08:00
|
|
|
</section>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|