2016-06-27 19:47:48 +00:00
|
|
|
import React, { PropTypes } from 'react';
|
2016-06-23 22:29:55 +00:00
|
|
|
import ReactDOM from 'react-dom';
|
|
|
|
|
|
|
|
class PreviewFrame extends React.Component {
|
|
|
|
|
|
|
|
componentDidMount() {
|
2016-07-08 18:33:06 +00:00
|
|
|
this.hijackConsole();
|
|
|
|
|
2016-06-23 22:29:55 +00:00
|
|
|
if (this.props.isPlaying) {
|
|
|
|
this.renderFrameContents();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-27 19:47:48 +00:00
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
if (this.props.isPlaying !== prevProps.isPlaying) {
|
|
|
|
if (this.props.isPlaying) {
|
|
|
|
this.renderSketch();
|
|
|
|
} else {
|
|
|
|
this.clearPreview();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.props.isPlaying && this.props.content !== prevProps.content) {
|
|
|
|
this.renderSketch();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
ReactDOM.unmountComponentAtNode(ReactDOM.findDOMNode(this).contentDocument.body);
|
|
|
|
}
|
|
|
|
|
2016-06-23 22:29:55 +00:00
|
|
|
clearPreview() {
|
|
|
|
const doc = ReactDOM.findDOMNode(this).contentDocument;
|
|
|
|
doc.write('');
|
|
|
|
doc.close();
|
|
|
|
}
|
|
|
|
|
2016-07-08 18:33:06 +00:00
|
|
|
hijackConsole() {
|
|
|
|
const iframeWindow = ReactDOM.findDOMNode(this).contentWindow;
|
|
|
|
const originalConsole = iframeWindow.console;
|
|
|
|
iframeWindow.console = {};
|
|
|
|
|
|
|
|
const methods = [
|
|
|
|
'debug', 'clear', 'error', 'info', 'log', 'warn'
|
|
|
|
];
|
|
|
|
|
|
|
|
methods.forEach((method) => {
|
|
|
|
iframeWindow.console[method] = (...theArgs) => {
|
|
|
|
originalConsole[method].apply(originalConsole, theArgs);
|
|
|
|
|
|
|
|
// TO DO: do something with the arguments
|
|
|
|
// window.alert(JSON.stringify(theArgs));
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-06-23 22:29:55 +00:00
|
|
|
renderSketch() {
|
|
|
|
const doc = ReactDOM.findDOMNode(this).contentDocument;
|
|
|
|
this.clearPreview();
|
|
|
|
ReactDOM.render(this.props.head, doc.head);
|
|
|
|
const p5Script = doc.createElement('script');
|
|
|
|
p5Script.setAttribute('src', 'https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.0/p5.min.js');
|
|
|
|
doc.body.appendChild(p5Script);
|
|
|
|
|
|
|
|
const sketchScript = doc.createElement('script');
|
|
|
|
sketchScript.textContent = this.props.content;
|
|
|
|
doc.body.appendChild(sketchScript);
|
|
|
|
}
|
|
|
|
|
2016-06-27 19:47:48 +00:00
|
|
|
renderFrameContents() {
|
|
|
|
const doc = ReactDOM.findDOMNode(this).contentDocument;
|
|
|
|
if (doc.readyState === 'complete') {
|
2016-06-27 21:22:54 +00:00
|
|
|
this.renderSketch();
|
2016-06-27 19:47:48 +00:00
|
|
|
} else {
|
|
|
|
setTimeout(this.renderFrameContents, 0);
|
2016-06-23 22:29:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2016-06-27 19:47:48 +00:00
|
|
|
return (
|
|
|
|
<iframe
|
|
|
|
className="preview-frame"
|
|
|
|
frameBorder="0"
|
|
|
|
sandbox="allow-scripts allow-pointer-lock allow-same-origin allow-popups allow-modals allow-forms"
|
|
|
|
title="sketch output"
|
|
|
|
></iframe>
|
|
|
|
);
|
2016-06-23 22:29:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-27 19:47:48 +00:00
|
|
|
PreviewFrame.propTypes = {
|
|
|
|
isPlaying: PropTypes.bool.isRequired,
|
2016-06-27 19:57:36 +00:00
|
|
|
head: PropTypes.object.isRequired,
|
2016-06-27 19:47:48 +00:00
|
|
|
content: PropTypes.string.isRequired
|
|
|
|
};
|
|
|
|
|
2016-06-23 22:29:55 +00:00
|
|
|
export default PreviewFrame;
|