/* eslint jsx-a11y/no-noninteractive-element-interactions: 0 */
import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import { FormattedMessage } from 'react-intl';
import CopyToClipboard from 'react-copy-to-clipboard';
import classNames from 'classnames';
import LZString from 'lz-string';
import { Icon, Tooltip } from 'antd';
import EditButton from './EditButton';
import BrowserFrame from '../BrowserFrame';
import { ping } from '../utils';
function compress(string) {
return LZString.compressToBase64(string)
.replace(/\+/g, '-') // Convert '+' to '-'
.replace(/\//g, '_') // Convert '/' to '_'
.replace(/=+$/, ''); // Remove ending '='
}
export default class Demo extends React.Component {
static contextTypes = {
intl: PropTypes.object,
}
constructor(props) {
super(props);
this.state = {
codeExpand: false,
sourceCode: '',
copied: false,
copyTooltipVisible: false,
showRiddleButton: false,
};
}
componentWillReceiveProps(nextProps) {
const { highlightedCode } = nextProps;
const div = document.createElement('div');
div.innerHTML = highlightedCode[1].highlighted;
this.setState({ sourceCode: div.textContent });
}
shouldComponentUpdate(nextProps, nextState) {
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);
this.pingTimer = ping((status) => {
if (status !== 'timeout' && status !== 'error') {
this.setState({
showRiddleButton: true,
});
}
});
}
handleCodeExpand = () => {
this.setState({ codeExpand: !this.state.codeExpand });
}
saveAnchor = (anchor) => {
this.anchor = anchor;
}
handleCodeCopied = () => {
this.setState({ copied: true });
}
onCopyTooltipVisibleChange = (visible) => {
if (visible) {
this.setState({
copyTooltipVisible: visible,
copied: false,
});
return;
}
this.setState({
copyTooltipVisible: visible,
});
}
render() {
const { state } = this;
const { props } = this;
const {
meta,
src,
content,
preview,
highlightedCode,
style,
highlightedStyle,
expand,
} = props;
if (!this.liveDemo) {
this.liveDemo = meta.iframe
?