fix merge conflict with therewasaguy-console

This commit is contained in:
catarak 2016-07-20 23:02:45 -04:00
commit 051e3771ee
9 changed files with 182 additions and 9 deletions

View file

@ -38,5 +38,7 @@ export const SET_BLOB_URL = 'SET_BLOB_URL';
export const EXPAND_SIDEBAR = 'EXPAND_SIDEBAR'; export const EXPAND_SIDEBAR = 'EXPAND_SIDEBAR';
export const COLLAPSE_SIDEBAR = 'COLLAPSE_SIDEBAR'; export const COLLAPSE_SIDEBAR = 'COLLAPSE_SIDEBAR';
export const CONSOLE_EVENT = 'CONSOLE_EVENT';
// eventually, handle errors more specifically and better // eventually, handle errors more specifically and better
export const ERROR = 'ERROR'; export const ERROR = 'ERROR';

View file

@ -25,6 +25,13 @@ export function setSelectedFile(fileId) {
}; };
} }
export function dispatchConsoleEvent(...args) {
return {
type: ActionTypes.CONSOLE_EVENT,
event: args[0].data
};
}
export function newFile() { export function newFile() {
return { return {
type: ActionTypes.SHOW_MODAL type: ActionTypes.SHOW_MODAL

View file

@ -0,0 +1,57 @@
import React, { PropTypes } from 'react';
/**
* How many console messages to store
* @type {Number}
*/
const consoleMax = 5;
class Console extends React.Component {
constructor(props) {
super(props);
/**
* An array of React Elements that include previous console messages
* @type {Array}
*/
this.children = [];
}
componentWillReceiveProps(nextProps) {
if (nextProps.isPlaying && !this.props.isPlaying) {
this.children = [];
} else if (nextProps.consoleEvent !== this.props.consoleEvent) {
const args = nextProps.consoleEvent.arguments;
const method = nextProps.consoleEvent.method;
const nextChild = (
<div key={this.children.length} className={`preview-console__${method}`}>
{Object.keys(args).map((key) => <span key={`${this.children.length}-${key}`}>{args[key]}</span>)}
</div>
);
this.children.push(nextChild);
}
}
shouldComponentUpdate(nextProps) {
return (nextProps.consoleEvent !== this.props.consoleEvent) || (nextProps.isPlaying && !this.props.isPlaying);
}
render() {
const childrenToDisplay = this.children.slice(-consoleMax);
return (
<div ref="console" className="preview-console">
{childrenToDisplay}
</div>
);
}
}
Console.propTypes = {
consoleEvent: PropTypes.object,
isPlaying: PropTypes.bool.isRequired
};
export default Console;

View file

@ -3,12 +3,68 @@ import ReactDOM from 'react-dom';
import escapeStringRegexp from 'escape-string-regexp'; import escapeStringRegexp from 'escape-string-regexp';
import srcDoc from 'srcdoc-polyfill'; import srcDoc from 'srcdoc-polyfill';
const hijackConsoleScript = `<script>
document.addEventListener('DOMContentLoaded', function() {
var iframeWindow = window;
var originalConsole = iframeWindow.console;
iframeWindow.console = {};
var methods = [
'debug', 'clear', 'error', 'info', 'log', 'warn'
];
methods.forEach( function(method) {
iframeWindow.console[method] = function() {
originalConsole[method].apply(originalConsole, arguments);
var args = Array.from(arguments);
args = args.map(function(i) {
// catch objects
return (typeof i === 'string') ? i : JSON.stringify(i);
});
// post message to parent window
window.parent.postMessage({
method: method,
arguments: args,
source: 'sketch'
}, '*');
};
});
// catch reference errors, via http://stackoverflow.com/a/12747364/2994108
window.onerror = function (msg, url, lineNo, columnNo, error) {
var string = msg.toLowerCase();
var substring = "script error";
var data = {};
if (string.indexOf(substring) > -1){
data = 'Script Error: See Browser Console for Detail';
} else {
data = msg + ' Line: ' + lineNo + 'column: ' + columnNo;
}
window.parent.postMessage({
method: 'error',
arguments: data,
source: 'sketch'
}, '*');
return false;
};
});
</script>`;
class PreviewFrame extends React.Component { class PreviewFrame extends React.Component {
componentDidMount() { componentDidMount() {
if (this.props.isPlaying) { if (this.props.isPlaying) {
this.renderFrameContents(); this.renderFrameContents();
} }
window.addEventListener('message', (msg) => {
if (msg.data.source === 'sketch') {
this.props.dispatchConsoleEvent(msg);
}
});
} }
componentDidUpdate(prevProps) { componentDidUpdate(prevProps) {
@ -70,21 +126,24 @@ class PreviewFrame extends React.Component {
htmlFile = htmlFile.replace(fileRegex, `<style>\n${cssFile.content}\n</style>`); htmlFile = htmlFile.replace(fileRegex, `<style>\n${cssFile.content}\n</style>`);
}); });
// const htmlHead = htmlFile.match(/(?:<head.*?>)([\s\S]*?)(?:<\/head>)/gmi);
// const headRegex = new RegExp('head', 'i');
// let htmlHeadContents = htmlHead[0].split(headRegex)[1];
// htmlHeadContents = htmlHeadContents.slice(1, htmlHeadContents.length - 2);
// htmlHeadContents += '<link rel="stylesheet" type="text/css" href="/preview-styles.css" />\n';
// htmlFile = htmlFile.replace(/(?:<head.*?>)([\s\S]*?)(?:<\/head>)/gmi, `<head>\n${htmlHeadContents}\n</head>`);
htmlFile += hijackConsoleScript;
return htmlFile; return htmlFile;
} }
renderSketch() { renderSketch() {
const doc = ReactDOM.findDOMNode(this); const doc = ReactDOM.findDOMNode(this);
if (this.props.isPlaying) { if (this.props.isPlaying) {
// TODO add polyfill for this
// doc.srcdoc = this.injectLocalFiles();
srcDoc.set(doc, this.injectLocalFiles()); srcDoc.set(doc, this.injectLocalFiles());
} else { } else {
// doc.srcdoc = ''; doc.srcdoc = '';
srcDoc.set(doc, ''); srcDoc.set(doc, ' ');
doc.contentWindow.document.open();
doc.contentWindow.document.write('');
doc.contentWindow.document.close();
} }
} }
@ -106,7 +165,7 @@ class PreviewFrame extends React.Component {
frameBorder="0" frameBorder="0"
title="sketch output" title="sketch output"
sandbox="allow-scripts allow-pointer-lock allow-same-origin allow-popups allow-modals allow-forms" sandbox="allow-scripts allow-pointer-lock allow-same-origin allow-popups allow-modals allow-forms"
></iframe> />
); );
} }
} }
@ -120,7 +179,9 @@ PreviewFrame.propTypes = {
}), }),
jsFiles: PropTypes.array.isRequired, jsFiles: PropTypes.array.isRequired,
cssFiles: PropTypes.array.isRequired, cssFiles: PropTypes.array.isRequired,
files: PropTypes.array.isRequired files: PropTypes.array.isRequired,
dispatchConsoleEvent: PropTypes.func.isRequired,
children: PropTypes.element
}; };
export default PreviewFrame; export default PreviewFrame;

View file

@ -6,6 +6,7 @@ import Toolbar from '../components/Toolbar';
import Preferences from '../components/Preferences'; import Preferences from '../components/Preferences';
import NewFileModal from '../components/NewFileModal'; import NewFileModal from '../components/NewFileModal';
import Nav from '../../../components/Nav'; import Nav from '../../../components/Nav';
import Console from '../components/Console';
import { bindActionCreators } from 'redux'; import { bindActionCreators } from 'redux';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import * as FileActions from '../actions/files'; import * as FileActions from '../actions/files';
@ -85,6 +86,11 @@ class IDEView extends React.Component {
<link type="text/css" rel="stylesheet" href="/preview-styles.css" /> <link type="text/css" rel="stylesheet" href="/preview-styles.css" />
} }
isPlaying={this.props.ide.isPlaying} isPlaying={this.props.ide.isPlaying}
dispatchConsoleEvent={this.props.dispatchConsoleEvent}
/>
<Console
consoleEvent={this.props.ide.consoleEvent}
isPlaying={this.props.ide.isPlaying}
/> />
{(() => { {(() => {
if (this.props.ide.modalIsVisible) { if (this.props.ide.modalIsVisible) {
@ -98,6 +104,7 @@ class IDEView extends React.Component {
return ''; return '';
})()} })()}
</div> </div>
); );
} }
} }
@ -114,6 +121,7 @@ IDEView.propTypes = {
saveProject: PropTypes.func.isRequired, saveProject: PropTypes.func.isRequired,
ide: PropTypes.shape({ ide: PropTypes.shape({
isPlaying: PropTypes.bool.isRequired, isPlaying: PropTypes.bool.isRequired,
consoleEvent: PropTypes.object,
modalIsVisible: PropTypes.bool.isRequired, modalIsVisible: PropTypes.bool.isRequired,
sidebarIsExpanded: PropTypes.bool.isRequired sidebarIsExpanded: PropTypes.bool.isRequired
}).isRequired, }).isRequired,
@ -152,6 +160,7 @@ IDEView.propTypes = {
htmlFile: PropTypes.object.isRequired, htmlFile: PropTypes.object.isRequired,
jsFiles: PropTypes.array.isRequired, jsFiles: PropTypes.array.isRequired,
cssFiles: PropTypes.array.isRequired, cssFiles: PropTypes.array.isRequired,
dispatchConsoleEvent: PropTypes.func.isRequired,
newFile: PropTypes.func.isRequired, newFile: PropTypes.func.isRequired,
closeNewFileModal: PropTypes.func.isRequired, closeNewFileModal: PropTypes.func.isRequired,
expandSidebar: PropTypes.func.isRequired, expandSidebar: PropTypes.func.isRequired,

View file

@ -3,6 +3,10 @@ import * as ActionTypes from '../../../constants';
const initialState = { const initialState = {
isPlaying: false, isPlaying: false,
selectedFile: '1', selectedFile: '1',
consoleEvent: {
method: undefined,
arguments: []
},
modalIsVisible: false, modalIsVisible: false,
sidebarIsExpanded: true sidebarIsExpanded: true
}; };
@ -19,6 +23,8 @@ const ide = (state = initialState, action) => {
case ActionTypes.SET_PROJECT: case ActionTypes.SET_PROJECT:
case ActionTypes.NEW_PROJECT: case ActionTypes.NEW_PROJECT:
return Object.assign({}, state, { selectedFile: action.selectedFile }); return Object.assign({}, state, { selectedFile: action.selectedFile });
case ActionTypes.CONSOLE_EVENT:
return Object.assign({}, state, { consoleEvent: action.event });
case ActionTypes.SHOW_MODAL: case ActionTypes.SHOW_MODAL:
return Object.assign({}, state, { modalIsVisible: true }); return Object.assign({}, state, { modalIsVisible: true });
case ActionTypes.HIDE_MODAL: case ActionTypes.HIDE_MODAL:

View file

@ -39,3 +39,6 @@ $dark-button-active-color: $white;
$ide-border-color: #f4f4f4; $ide-border-color: #f4f4f4;
$editor-selected-line-color: #f3f3f3; $editor-selected-line-color: #f3f3f3;
$input-border-color: #979797; $input-border-color: #979797;
$console-warn-color: #ffbe05;
$console-error-color: #ff5f52;

View file

@ -0,0 +1,27 @@
.preview-console {
position: fixed;
width:100%;
height:60px;
right:0px;
bottom: 0px;
background:$dark-background-color;
z-index:1000;
& > {
position:relative;
text-align:left;
}
// assign styles to different types of console messages
.preview-console__log {
color: $dark-secondary-text-color;
}
.preview-console__error {
color: $console-error-color;
}
.preview-console__warn {
color: $console-warn-color;
}
}

View file

@ -18,6 +18,7 @@
@import 'components/sketch-list'; @import 'components/sketch-list';
@import 'components/sidebar'; @import 'components/sidebar';
@import 'components/modal'; @import 'components/modal';
@import 'components/console';
@import 'layout/ide'; @import 'layout/ide';
@import 'layout/sketch-list'; @import 'layout/sketch-list';