2017-08-15 17:17:59 +08:00
|
|
|
/* eslint jsx-a11y/no-noninteractive-element-interactions: 0 */
|
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';
|
2017-04-12 04:49:08 +08:00
|
|
|
import PropTypes from 'prop-types';
|
2016-09-18 14:27:41 +08:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
2017-02-24 11:55:24 +08:00
|
|
|
import CopyToClipboard from 'react-copy-to-clipboard';
|
2016-03-25 15:03:53 +08:00
|
|
|
import classNames from 'classnames';
|
2017-03-02 15:08:42 +08:00
|
|
|
import { Icon, Tooltip } from 'antd';
|
2016-08-17 12:06:38 +08:00
|
|
|
import EditButton from './EditButton';
|
2017-03-18 15:15:00 +08:00
|
|
|
import BrowserFrame from '../BrowserFrame';
|
2017-07-10 22:17:52 +08:00
|
|
|
import { ping } from '../utils';
|
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 = {
|
2017-04-12 04:49:08 +08:00
|
|
|
intl: PropTypes.object,
|
2016-04-20 16:28:07 +08:00
|
|
|
}
|
|
|
|
|
2016-03-01 14:53:32 +08:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
2016-03-25 15:03:53 +08:00
|
|
|
codeExpand: false,
|
2017-02-24 11:55:24 +08:00
|
|
|
sourceCode: '',
|
2017-03-02 15:08:42 +08:00
|
|
|
copied: false,
|
|
|
|
copyTooltipVisible: false,
|
2017-07-10 22:17:52 +08:00
|
|
|
showRiddleButton: false,
|
2016-03-01 14:53:32 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-02-24 11:55:24 +08:00
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
const { highlightedCode } = nextProps;
|
|
|
|
const div = document.createElement('div');
|
|
|
|
div.innerHTML = highlightedCode[1].highlighted;
|
|
|
|
this.setState({ sourceCode: div.textContent });
|
|
|
|
}
|
|
|
|
|
2016-09-20 15:01:25 +08:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
2017-03-02 15:08:42 +08:00
|
|
|
return (this.state.codeExpand || this.props.expand) !== (nextState.codeExpand || nextProps.expand)
|
2017-08-16 10:38:37 +08:00
|
|
|
|| this.state.copied !== nextState.copied
|
|
|
|
|| this.state.copyTooltipVisible !== nextState.copyTooltipVisible;
|
2016-09-20 15:01:25 +08:00
|
|
|
}
|
|
|
|
|
2017-01-13 14:59:14 +08:00
|
|
|
componentDidMount() {
|
|
|
|
const { meta, location } = this.props;
|
|
|
|
if (meta.id === location.hash.slice(1)) {
|
|
|
|
this.anchor.click();
|
|
|
|
}
|
2017-02-24 11:55:24 +08:00
|
|
|
this.componentWillReceiveProps(this.props);
|
2017-07-10 22:17:52 +08:00
|
|
|
|
|
|
|
this.pingTimer = ping((status) => {
|
|
|
|
if (status !== 'timeout' && status !== 'error') {
|
|
|
|
this.setState({
|
|
|
|
showRiddleButton: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2017-01-13 14:59:14 +08:00
|
|
|
}
|
|
|
|
|
2017-08-15 17:17:59 +08:00
|
|
|
handleCodeExpand = () => {
|
2016-03-25 15:03:53 +08:00
|
|
|
this.setState({ codeExpand: !this.state.codeExpand });
|
|
|
|
}
|
|
|
|
|
2017-01-13 14:59:14 +08:00
|
|
|
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() {
|
2017-10-09 13:23:20 +08:00
|
|
|
const { state } = this;
|
|
|
|
const { props } = this;
|
2016-05-27 11:48:08 +08:00
|
|
|
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) {
|
2017-03-18 15:15:00 +08:00
|
|
|
this.liveDemo = meta.iframe
|
2017-05-15 12:03:02 +08:00
|
|
|
? <BrowserFrame><iframe src={src} height={meta.iframe} title="demo" /></BrowserFrame>
|
2017-03-18 15:15:00 +08:00
|
|
|
: preview(React, ReactDOM);
|
2016-10-13 16:52:24 +08:00
|
|
|
}
|
2017-02-24 11:55:24 +08:00
|
|
|
const codeExpand = 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
|
|
|
|
2017-10-09 13:23:20 +08:00
|
|
|
const { locale } = this.context.intl;
|
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,
|
|
|
|
});
|
2017-07-07 16:09:02 +08:00
|
|
|
|
2017-07-10 22:17:52 +08:00
|
|
|
const prefillStyle = `@import 'antd/dist/antd.css';\n\n${style || ''}`.replace(new RegExp(`#${meta.id}\\s*`, 'g'), '');
|
|
|
|
|
2017-07-07 16:09:02 +08:00
|
|
|
const codepenPrefillConfig = {
|
|
|
|
title: `${localizedTitle} - Ant Design Demo`,
|
|
|
|
html: `<div id="container" style="padding: 24px"></div>
|
|
|
|
<script>
|
|
|
|
var mountNode = document.getElementById('container');
|
|
|
|
</script>`,
|
|
|
|
js: state.sourceCode.replace(/import\s+\{\s+(.*)\s+\}\s+from\s+'antd';/, 'const { $1 } = antd;'),
|
2017-07-10 22:17:52 +08:00
|
|
|
css: prefillStyle,
|
2017-07-07 16:09:02 +08:00
|
|
|
editors: '001',
|
|
|
|
css_external: 'https://unpkg.com/antd/dist/antd.css',
|
|
|
|
js_external: [
|
2017-10-06 16:11:49 +08:00
|
|
|
'react@15.x/dist/react.js',
|
|
|
|
'react-dom@15.x/dist/react-dom.js',
|
2017-07-07 16:09:02 +08:00
|
|
|
'moment/min/moment-with-locales.js',
|
|
|
|
'antd/dist/antd-with-locales.js',
|
|
|
|
].map(url => `https://unpkg.com/${url}`).join(';'),
|
|
|
|
js_pre_processor: 'typescript',
|
|
|
|
};
|
2017-07-10 22:17:52 +08:00
|
|
|
const riddlePrefillConfig = {
|
|
|
|
title: `${localizedTitle} - Ant Design Demo`,
|
|
|
|
js: state.sourceCode,
|
|
|
|
css: prefillStyle,
|
|
|
|
};
|
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">
|
2017-01-13 14:59:14 +08:00
|
|
|
<a href={`#${meta.id}`} ref={this.saveAnchor}>
|
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}
|
2017-08-15 17:17:59 +08:00
|
|
|
<Tooltip title={codeExpand ? 'Hide Code' : 'Show Code'}>
|
2017-08-15 21:00:31 +08:00
|
|
|
<span className="code-expand-icon">
|
|
|
|
<img
|
|
|
|
alt="expand code"
|
|
|
|
src="https://gw.alipayobjects.com/zos/rmsportal/wSAkBuJFbdxsosKKpqyq.svg"
|
|
|
|
className={codeExpand ? 'code-expand-icon-hide' : 'code-expand-icon-show'}
|
|
|
|
onClick={this.handleCodeExpand}
|
|
|
|
/>
|
|
|
|
<img
|
|
|
|
alt="expand code"
|
|
|
|
src="https://gw.alipayobjects.com/zos/rmsportal/OpROPHYqWmrMDBFMZtKF.svg"
|
|
|
|
className={codeExpand ? 'code-expand-icon-show' : 'code-expand-icon-hide'}
|
|
|
|
onClick={this.handleCodeExpand}
|
|
|
|
/>
|
|
|
|
</span>
|
2017-08-15 17:17:59 +08:00
|
|
|
</Tooltip>
|
2016-02-29 14:08:40 +08:00
|
|
|
</section>
|
2017-08-16 10:38:37 +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">
|
2017-07-10 22:17:52 +08:00
|
|
|
<div className="code-box-actions">
|
|
|
|
{this.state.showRiddleButton ? (
|
|
|
|
<form action="//riddle.alibaba-inc.com/riddles/define" method="POST" target="_blank">
|
|
|
|
<input type="hidden" name="data" value={JSON.stringify(riddlePrefillConfig)} />
|
|
|
|
<Tooltip title={<FormattedMessage id="app.demo.riddle" />}>
|
|
|
|
<input type="submit" value="Create New Riddle with Prefilled Data" className="code-box-riddle" />
|
|
|
|
</Tooltip>
|
|
|
|
</form>
|
|
|
|
) : null}
|
|
|
|
<form action="https://codepen.io/pen/define" method="POST" target="_blank">
|
|
|
|
<input type="hidden" name="data" value={JSON.stringify(codepenPrefillConfig)} />
|
|
|
|
<Tooltip title={<FormattedMessage id="app.demo.codepen" />}>
|
|
|
|
<input type="submit" value="Create New Pen with Prefilled Data" className="code-box-codepen" />
|
|
|
|
</Tooltip>
|
|
|
|
</form>
|
|
|
|
<CopyToClipboard
|
|
|
|
text={state.sourceCode}
|
|
|
|
onCopy={this.handleCodeCopied}
|
2017-03-02 15:08:42 +08:00
|
|
|
>
|
2017-07-10 22:17:52 +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>
|
|
|
|
</div>
|
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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|