2016-06-27 22:03:22 +02:00
|
|
|
import React, { PropTypes } from 'react';
|
2016-08-12 03:26:49 +02:00
|
|
|
import EditorAccessibility from '../components/EditorAccessibility';
|
2016-06-24 00:29:55 +02:00
|
|
|
import CodeMirror from 'codemirror';
|
2016-09-07 21:05:25 +02:00
|
|
|
import beautifyJS from 'js-beautify';
|
|
|
|
const beautifyCSS = beautifyJS.css;
|
|
|
|
const beautifyHTML = beautifyJS.html;
|
2016-06-24 00:29:55 +02:00
|
|
|
import 'codemirror/mode/javascript/javascript';
|
2016-07-14 01:52:50 +02:00
|
|
|
import 'codemirror/mode/css/css';
|
|
|
|
import 'codemirror/mode/htmlmixed/htmlmixed';
|
2016-06-24 00:29:55 +02:00
|
|
|
import 'codemirror/addon/selection/active-line';
|
2016-07-12 23:38:24 +02:00
|
|
|
import 'codemirror/addon/lint/lint';
|
|
|
|
import 'codemirror/addon/lint/javascript-lint';
|
2016-07-14 01:52:50 +02:00
|
|
|
import 'codemirror/addon/lint/css-lint';
|
|
|
|
import 'codemirror/addon/lint/html-lint';
|
2016-07-21 20:33:23 +02:00
|
|
|
import 'codemirror/addon/comment/comment';
|
|
|
|
import 'codemirror/keymap/sublime';
|
2016-08-05 22:58:59 +02:00
|
|
|
import 'codemirror/addon/search/jump-to-line';
|
2016-09-12 07:31:30 +02:00
|
|
|
|
2016-07-12 23:38:24 +02:00
|
|
|
import { JSHINT } from 'jshint';
|
|
|
|
window.JSHINT = JSHINT;
|
2016-07-14 01:52:50 +02:00
|
|
|
import { CSSLint } from 'csslint';
|
|
|
|
window.CSSLint = CSSLint;
|
|
|
|
import { HTMLHint } from 'htmlhint';
|
|
|
|
window.HTMLHint = HTMLHint;
|
2016-08-15 17:51:11 +02:00
|
|
|
const beepUrl = require('../../../sounds/audioAlert.mp3');
|
2016-09-07 22:33:01 +02:00
|
|
|
import InlineSVG from 'react-inlinesvg';
|
|
|
|
const downArrowUrl = require('../../../images/down-arrow.svg');
|
|
|
|
import classNames from 'classnames';
|
2016-06-24 00:29:55 +02:00
|
|
|
|
2016-07-14 03:50:59 +02:00
|
|
|
import { debounce } from 'throttle-debounce';
|
2016-09-12 07:31:30 +02:00
|
|
|
import loopProtect from 'loop-protect';
|
2016-06-24 00:29:55 +02:00
|
|
|
|
|
|
|
class Editor extends React.Component {
|
2016-09-07 22:41:56 +02:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.tidyCode = this.tidyCode.bind(this);
|
|
|
|
}
|
2016-06-24 00:29:55 +02:00
|
|
|
componentDidMount() {
|
2016-08-15 17:51:11 +02:00
|
|
|
this.beep = new Audio(beepUrl);
|
2016-09-12 07:31:30 +02:00
|
|
|
this.widgets = [];
|
2016-06-24 00:29:55 +02:00
|
|
|
this._cm = CodeMirror(this.refs.container, { // eslint-disable-line
|
2016-09-20 17:51:09 +02:00
|
|
|
theme: `p5-${this.props.theme}`,
|
2016-07-08 21:58:49 +02:00
|
|
|
value: this.props.file.content,
|
2016-06-24 00:29:55 +02:00
|
|
|
lineNumbers: true,
|
|
|
|
styleActiveLine: true,
|
2016-07-13 17:59:47 +02:00
|
|
|
inputStyle: 'contenteditable',
|
2016-07-12 21:58:11 +02:00
|
|
|
mode: 'javascript',
|
2016-10-05 18:26:49 +02:00
|
|
|
lineWrapping: false,
|
|
|
|
fixedGutter: false,
|
2016-07-12 23:38:24 +02:00
|
|
|
gutters: ['CodeMirror-lint-markers'],
|
2016-08-05 22:58:59 +02:00
|
|
|
keyMap: 'sublime',
|
|
|
|
lint: {
|
2016-09-12 07:31:30 +02:00
|
|
|
onUpdateLinting: () => {
|
|
|
|
debounce(2000, (annotations) => {
|
|
|
|
this.props.clearLintMessage();
|
|
|
|
annotations.forEach((x) => {
|
|
|
|
if (x.from.line > -1) {
|
|
|
|
this.props.updateLintMessage(x.severity, (x.from.line + 1), x.message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (this.props.lintMessages.length > 0 && this.props.lintWarning) {
|
|
|
|
this.beep.play();
|
2016-08-06 05:08:44 +02:00
|
|
|
}
|
|
|
|
});
|
2016-09-12 07:31:30 +02:00
|
|
|
}
|
2016-08-05 22:58:59 +02:00
|
|
|
}
|
2016-06-24 00:29:55 +02:00
|
|
|
});
|
2016-09-21 00:27:10 +02:00
|
|
|
|
2016-09-24 06:46:06 +02:00
|
|
|
this._cm.on('change', debounce(1000, () => {
|
2016-09-21 00:27:10 +02:00
|
|
|
this.props.setUnsavedChanges(true);
|
2016-07-08 21:58:49 +02:00
|
|
|
this.props.updateFileContent(this.props.file.name, this._cm.getValue());
|
2016-10-04 21:35:23 +02:00
|
|
|
if (this.props.autorefresh && this.props.isPlaying) {
|
|
|
|
this.props.startRefreshSketch();
|
|
|
|
}
|
|
|
|
// this.checkForInfiniteLoop((infiniteLoop, prevs) => {
|
|
|
|
// if (!infiniteLoop && prevs && this.props.autorefresh) {
|
|
|
|
// this.props.startRefreshSketch();
|
|
|
|
// }
|
|
|
|
// });
|
2016-07-14 03:50:59 +02:00
|
|
|
}));
|
2016-09-21 00:27:10 +02:00
|
|
|
|
2016-08-05 22:58:59 +02:00
|
|
|
this._cm.on('keyup', () => {
|
2016-08-25 18:32:06 +02:00
|
|
|
const temp = `line ${parseInt((this._cm.getCursor().line) + 1, 10)}`;
|
|
|
|
document.getElementById('current-line').innerHTML = temp;
|
2016-08-05 22:58:59 +02:00
|
|
|
});
|
2016-09-07 18:16:26 +02:00
|
|
|
|
|
|
|
this._cm.on('keydown', (_cm, e) => {
|
|
|
|
if (e.key === 'Tab' && e.shiftKey) {
|
2016-09-07 22:41:56 +02:00
|
|
|
this.tidyCode();
|
2016-09-07 18:16:26 +02:00
|
|
|
}
|
|
|
|
});
|
2016-09-21 00:27:10 +02:00
|
|
|
|
|
|
|
this._cm.getWrapperElement().style['font-size'] = `${this.props.fontSize}px`;
|
|
|
|
this._cm.setOption('indentWithTabs', this.props.isTabIndent);
|
|
|
|
this._cm.setOption('tabSize', this.props.indentationAmount);
|
2016-06-24 00:29:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps) {
|
2016-07-08 21:58:49 +02:00
|
|
|
if (this.props.file.content !== prevProps.file.content &&
|
|
|
|
this.props.file.content !== this._cm.getValue()) {
|
|
|
|
this._cm.setValue(this.props.file.content); // eslint-disable-line no-underscore-dangle
|
2016-09-21 00:27:10 +02:00
|
|
|
setTimeout(() => this.props.setUnsavedChanges(false), 500);
|
2016-06-24 00:29:55 +02:00
|
|
|
}
|
|
|
|
if (this.props.fontSize !== prevProps.fontSize) {
|
|
|
|
this._cm.getWrapperElement().style['font-size'] = `${this.props.fontSize}px`;
|
|
|
|
}
|
2016-07-06 17:27:39 +02:00
|
|
|
if (this.props.indentationAmount !== prevProps.indentationAmount) {
|
|
|
|
this._cm.setOption('tabSize', this.props.indentationAmount);
|
|
|
|
}
|
2016-07-11 05:11:06 +02:00
|
|
|
if (this.props.isTabIndent !== prevProps.isTabIndent) {
|
|
|
|
this._cm.setOption('indentWithTabs', this.props.isTabIndent);
|
|
|
|
}
|
2016-07-14 01:52:50 +02:00
|
|
|
if (this.props.file.name !== prevProps.name) {
|
|
|
|
if (this.props.file.name.match(/.+\.js$/)) {
|
|
|
|
this._cm.setOption('mode', 'javascript');
|
|
|
|
} else if (this.props.file.name.match(/.+\.css$/)) {
|
|
|
|
this._cm.setOption('mode', 'css');
|
|
|
|
} else if (this.props.file.name.match(/.+\.html$/)) {
|
|
|
|
this._cm.setOption('mode', 'htmlmixed');
|
|
|
|
}
|
|
|
|
}
|
2016-09-20 17:51:09 +02:00
|
|
|
|
|
|
|
if (this.props.theme !== prevProps.theme) {
|
|
|
|
this._cm.setOption('theme', `p5-${this.props.theme}`);
|
|
|
|
}
|
2016-06-24 00:29:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this._cm = null;
|
|
|
|
}
|
|
|
|
|
2016-09-07 22:41:56 +02:00
|
|
|
tidyCode() {
|
|
|
|
const beautifyOptions = {
|
|
|
|
indent_size: this.props.indentationAmount,
|
|
|
|
indent_with_tabs: this.props.isTabIndent
|
|
|
|
};
|
|
|
|
|
|
|
|
const mode = this._cm.getOption('mode');
|
|
|
|
if (mode === 'javascript') {
|
|
|
|
this._cm.doc.setValue(beautifyJS(this._cm.doc.getValue(), beautifyOptions));
|
|
|
|
} else if (mode === 'css') {
|
|
|
|
this._cm.doc.setValue(beautifyCSS(this._cm.doc.getValue(), beautifyOptions));
|
|
|
|
} else if (mode === 'htmlmixed') {
|
|
|
|
this._cm.doc.setValue(beautifyHTML(this._cm.doc.getValue(), beautifyOptions));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-12 07:31:30 +02:00
|
|
|
checkForInfiniteLoop(callback) {
|
|
|
|
const prevIsplaying = this.props.isPlaying;
|
|
|
|
let infiniteLoop = false;
|
2016-09-22 23:17:24 +02:00
|
|
|
let prevLine;
|
2016-09-12 07:31:30 +02:00
|
|
|
this.props.resetInfiniteLoops();
|
2016-09-24 06:46:06 +02:00
|
|
|
let iframe;
|
2016-09-12 07:31:30 +02:00
|
|
|
|
|
|
|
for (let i = 0; i < this.widgets.length; ++i) {
|
|
|
|
this._cm.removeLineWidget(this.widgets[i]);
|
|
|
|
}
|
|
|
|
this.widgets.length = 0;
|
|
|
|
|
|
|
|
loopProtect.alias = 'protect';
|
|
|
|
|
2016-09-29 06:54:35 +02:00
|
|
|
let foundInfiniteLoop = false;
|
2016-09-12 07:31:30 +02:00
|
|
|
loopProtect.hit = (line) => {
|
2016-09-29 06:54:35 +02:00
|
|
|
foundInfiniteLoop = true;
|
2016-09-22 23:17:24 +02:00
|
|
|
if (line !== prevLine) {
|
|
|
|
this.props.detectInfiniteLoops();
|
2016-09-29 06:54:35 +02:00
|
|
|
this.props.stopSketch();
|
2016-09-22 23:17:24 +02:00
|
|
|
infiniteLoop = true;
|
|
|
|
callback(infiniteLoop, prevIsplaying);
|
|
|
|
const msg = document.createElement('div');
|
2016-09-24 06:46:06 +02:00
|
|
|
const loopError = `line ${line}: This loop is taking too long to run. This might be an infinite loop.`;
|
2016-09-22 23:17:24 +02:00
|
|
|
msg.appendChild(document.createTextNode(loopError));
|
|
|
|
msg.className = 'lint-error';
|
|
|
|
this.widgets.push(this._cm.addLineWidget(line - 1, msg, { coverGutter: false, noHScroll: true }));
|
|
|
|
prevLine = line;
|
|
|
|
}
|
2016-09-12 07:31:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const processed = loopProtect(this.props.file.content);
|
|
|
|
|
2016-09-29 06:54:35 +02:00
|
|
|
let iframeForLoop = document.getElementById('iframeForLoop');
|
2016-09-24 06:46:06 +02:00
|
|
|
if (iframeForLoop === null) {
|
|
|
|
iframe = document.createElement('iframe');
|
|
|
|
iframe.id = 'iframeForLoop';
|
|
|
|
iframe.style.display = 'none';
|
|
|
|
document.body.appendChild(iframe);
|
2016-09-29 06:54:35 +02:00
|
|
|
iframeForLoop = iframe;
|
2016-09-24 06:46:06 +02:00
|
|
|
} else {
|
|
|
|
iframeForLoop.srcdoc = '';
|
2016-09-29 06:54:35 +02:00
|
|
|
}
|
|
|
|
const win = iframeForLoop.contentWindow;
|
|
|
|
const doc = win.document;
|
|
|
|
doc.open();
|
2016-09-24 06:46:06 +02:00
|
|
|
|
2016-09-29 06:54:35 +02:00
|
|
|
win.protect = loopProtect;
|
2016-09-24 06:46:06 +02:00
|
|
|
|
2016-09-29 06:54:35 +02:00
|
|
|
doc.write(`<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/p5.min.js"></script>
|
|
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/addons/p5.dom.min.js"></script>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<script>
|
|
|
|
${processed}
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>`);
|
|
|
|
win.onerror = () => true;
|
|
|
|
doc.close();
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
if (!foundInfiniteLoop) {
|
|
|
|
callback(infiniteLoop, prevIsplaying, prevLine);
|
|
|
|
}
|
|
|
|
}, 200);
|
2016-09-12 07:31:30 +02:00
|
|
|
}
|
|
|
|
|
2016-06-24 00:29:55 +02:00
|
|
|
_cm: CodeMirror.Editor
|
|
|
|
|
|
|
|
render() {
|
2016-09-07 22:33:01 +02:00
|
|
|
const editorSectionClass = classNames({
|
|
|
|
editor: true,
|
|
|
|
'editor--options': this.props.editorOptionsVisible
|
|
|
|
});
|
|
|
|
|
2016-08-05 22:58:59 +02:00
|
|
|
return (
|
2016-09-07 18:16:26 +02:00
|
|
|
<section
|
|
|
|
title="code editor"
|
|
|
|
role="main"
|
2016-09-07 22:33:01 +02:00
|
|
|
className={editorSectionClass}
|
2016-09-07 18:16:26 +02:00
|
|
|
>
|
2016-09-07 22:33:01 +02:00
|
|
|
<button
|
|
|
|
className="editor__options-button"
|
2016-09-28 18:09:42 +02:00
|
|
|
aria-label="editor options"
|
2016-09-08 04:49:29 +02:00
|
|
|
tabIndex="0"
|
|
|
|
onClick={(e) => {
|
|
|
|
e.target.focus();
|
|
|
|
this.props.showEditorOptions();
|
|
|
|
}}
|
2016-09-07 22:41:56 +02:00
|
|
|
onBlur={() => setTimeout(this.props.closeEditorOptions, 200)}
|
2016-09-07 22:33:01 +02:00
|
|
|
>
|
|
|
|
<InlineSVG src={downArrowUrl} />
|
|
|
|
</button>
|
2016-09-28 18:09:42 +02:00
|
|
|
<ul className="editor__options" title="editor options">
|
2016-09-07 22:33:01 +02:00
|
|
|
<li>
|
2016-09-07 22:41:56 +02:00
|
|
|
<a onClick={this.tidyCode}>Tidy</a>
|
2016-09-07 22:33:01 +02:00
|
|
|
</li>
|
|
|
|
<li>
|
2016-09-07 23:47:22 +02:00
|
|
|
<a onClick={this.props.showKeyboardShortcutModal}>Keyboard Shortcuts</a>
|
2016-09-07 22:33:01 +02:00
|
|
|
</li>
|
|
|
|
</ul>
|
2016-08-12 20:19:23 +02:00
|
|
|
<div ref="container" className="editor-holder" tabIndex="0">
|
2016-08-12 03:26:49 +02:00
|
|
|
</div>
|
|
|
|
<EditorAccessibility
|
|
|
|
lintMessages={this.props.lintMessages}
|
2016-08-12 20:23:34 +02:00
|
|
|
lineNumber={this.props.lineNumber}
|
2016-08-12 03:26:49 +02:00
|
|
|
/>
|
2016-08-12 20:19:23 +02:00
|
|
|
</section>
|
2016-08-05 22:58:59 +02:00
|
|
|
);
|
2016-06-24 00:29:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-27 22:03:22 +02:00
|
|
|
Editor.propTypes = {
|
2016-08-11 20:09:59 +02:00
|
|
|
lintWarning: PropTypes.bool.isRequired,
|
2016-08-12 20:23:34 +02:00
|
|
|
lineNumber: PropTypes.string.isRequired,
|
2016-08-11 19:24:02 +02:00
|
|
|
lintMessages: PropTypes.array.isRequired,
|
|
|
|
updateLintMessage: PropTypes.func.isRequired,
|
|
|
|
clearLintMessage: PropTypes.func.isRequired,
|
|
|
|
updateLineNumber: PropTypes.func.isRequired,
|
2016-07-11 04:52:48 +02:00
|
|
|
indentationAmount: PropTypes.number.isRequired,
|
2016-07-12 05:40:30 +02:00
|
|
|
isTabIndent: PropTypes.bool.isRequired,
|
|
|
|
updateFileContent: PropTypes.func.isRequired,
|
|
|
|
fontSize: PropTypes.number.isRequired,
|
2016-07-08 21:58:49 +02:00
|
|
|
file: PropTypes.shape({
|
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
content: PropTypes.string.isRequired
|
2016-09-07 22:33:01 +02:00
|
|
|
}),
|
|
|
|
editorOptionsVisible: PropTypes.bool.isRequired,
|
|
|
|
showEditorOptions: PropTypes.func.isRequired,
|
2016-09-07 23:47:22 +02:00
|
|
|
closeEditorOptions: PropTypes.func.isRequired,
|
2016-09-21 00:27:10 +02:00
|
|
|
showKeyboardShortcutModal: PropTypes.func.isRequired,
|
2016-09-20 17:51:09 +02:00
|
|
|
setUnsavedChanges: PropTypes.func.isRequired,
|
2016-09-12 07:31:30 +02:00
|
|
|
infiniteLoop: PropTypes.bool.isRequired,
|
|
|
|
detectInfiniteLoops: PropTypes.func.isRequired,
|
|
|
|
resetInfiniteLoops: PropTypes.func.isRequired,
|
2016-09-29 06:54:35 +02:00
|
|
|
startRefreshSketch: PropTypes.func.isRequired,
|
|
|
|
autorefresh: PropTypes.bool.isRequired,
|
2016-09-22 23:17:24 +02:00
|
|
|
isPlaying: PropTypes.bool.isRequired,
|
2016-09-29 06:54:35 +02:00
|
|
|
theme: PropTypes.string.isRequired,
|
|
|
|
stopSketch: PropTypes.func.isRequired
|
2016-06-27 22:03:22 +02:00
|
|
|
};
|
|
|
|
|
2016-06-24 00:29:55 +02:00
|
|
|
export default Editor;
|