-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRightView.jsx
131 lines (113 loc) · 4.03 KB
/
RightView.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { ipcRenderer } from 'electron';
import React, { Component } from 'react';
import fs from 'fs-extra';
import highlighter from 'node-syntaxhighlighter';
import $ from 'jquery';
let isResizing = false;
let rightViewInstance = null;
const languageJava = highlighter.getLanguage("java");
class RightView extends Component {
constructor(props) {
super(props);
this.state = {
width: 500,
output: []
}
ipcRenderer.on('run', () => this.props.onRun(this.props.src));
rightViewInstance = this;
this.handleResize = this.handleResize.bind(this);
}
componentWillReceiveProps(nextProps) {
if (nextProps.src && this.state.width === 0) {
this.setState({
width: 499
});
setTimeout(() => this.setState({ width: 500 }), 10);
}
}
handleResize(e) {
if (!isResizing)
return;
e.preventDefault();
const newWidth = $(window).width() - e.clientX;
this.setState({
width: newWidth > 75 ? newWidth : 0
});
}
componentDidMount() {
$("#resize-handle").on("mousedown", () => isResizing = true);
$(document)
.on("mouseup", () => { isResizing = false })
.on("mousemove", this.handleResize);
}
componentWillUnmount() {
rightViewInstance = null;
}
addOutput(text, type) {
if (type === "error")
text = "!" + type + "!" + text;
this.setState((state, props) => {
state.output.push(text);
return {
output: state.output
}
});
}
renderOutput() {
const output = [];
for (let i in this.state.output) {
const line = this.state.output[i];
if (line.startsWith("!error!"))
output.push(<span className="error" key={i}>{line.replace("!error!", "")}</span>)
else
output.push(<span key={i}>{line}</span>);
}
return output;
}
renderContent() {
if (!this.props.src)
return "";
else if (this.props.src.endsWith(".pdf"))
return <webview className="full-height" src={this.props.src} plugins="true"></webview>;
const content = fs.readFileSync(this.props.src, "utf8");
return <div id="code-view" dangerouslySetInnerHTML={{ __html: highlighter.highlight(content, languageJava) }}></div>;
}
render() {
let style = { width: this.state.width + "px" };
if (this.state.width === 0 || !this.props.src)
style.display = "none";
return (
<div>
<div className="right-view" style={style}>
<div id="resize-handle"></div>
{this.renderContent()}
<pre id="console-output">
{this.renderOutput()}
</pre>
<div id="control-bar">
<button title="Close" onClick={this.props.onClose}><i className="fa fa-times"></i></button>
<div className="divider" hidden={!this.props.showRunButton}></div>
<button title="Run" onClick={() => this.props.onRun(this.props.src)} hidden={!this.props.showRunButton}><i className="fa fa-play"></i> Run</button>
<div className="divider" hidden={this.state.output.length === 0}></div>
<button title="Clear" onClick={() => this.setState({ output: [] })} hidden={this.state.output.length === 0}><i className="fa fa-ban"></i> Clear</button>
</div>
</div>
<div style={style}></div>
</div>
);
}
}
function addOutput(text, error) {
if (rightViewInstance) {
rightViewInstance.addOutput(text, error);
}
}
function clearOutput() {
if (rightViewInstance) {
rightViewInstance.setState({
output: []
});
}
}
export { addOutput, clearOutput };
export default RightView;