-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
110 lines (100 loc) · 3.3 KB
/
index.js
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
const electron = require('electron');
// The `decorateTerm` hook allows our extension to return a higher order react component
// It supplies us with:
// - Term: The terminal component
// - React: The enture React namespace
// - notify: Helper function for displaying notifications in the operating system
exports.decorateTerm = (Term, { React, notify }) => {
// Define and return our higher order component
return class extends React.Component {
constructor (props, context) {
super(props, context);
this.state = {searchText: ''};
this._onTerminal = this._onTerminal.bind(this);
this._onMouseUp = this._onMouseUp.bind(this);
this._onChange = this._onChange.bind(this);
this._onSearch = this._onSearch.bind(this);
}
_onTerminal (term) {
if (this.props.onTerminal) this.props.onTerminal(term);
this._window = term.document_.defaultView;
this._window.addEventListener('mouseup', this._onMouseUp);
}
// Update this.state.searchText if needed
_onMouseUp () {
const newText = this._window.getSelection().toString();
if (!newText && !this.state.searchText) return;
this.setState({'searchText': newText});
}
_onChange (event) {
this.setState({'searchText': event.target.value});
}
_onSearch () {
const query = this.state.searchText.split(' ').join('+');
electron.shell.openExternal('https://www.google.com/#q=' + query);
}
render () {
let children = [
React.createElement(Term, Object.assign({}, this.props, {
onTerminal: this._onTerminal,
key: 'term'
})),
];
if (this.state.searchText) {
const div = React.createElement(
'div',
{ style: {
position: 'absolute',
right: '-1px',
padding: '5px 10px',
paddingBottom: '8px',
border: '1px solid white',
backgroundColor: 'black',
color: 'white',
zIndex: '100',
fontFamily: 'Menlo, "DejaVu Sans Mono", "Lucida Console", monospace',
}},
React.createElement(
'input',
{
onChange: this._onChange,
value: this.state.searchText,
style: {
width: '350px',
backgroundColor: 'black',
color: 'white',
border: 'none',
fontFamily: 'inherit',
outline: 'none',
},
}
),
React.createElement(
'button',
{
onClick: this._onSearch,
style: {
position: 'absolute',
width: '55px',
height: '23px',
bottom: '-24px',
right: '-1px',
fontFamily: 'inherit',
borderRadius: '0px',
border: '0px',
backgroundColor: 'yellow',
cursor: 'pointer',
},
},
'Search'
)
);
children.unshift(div);
}
return React.createElement('div', {style: {width: '100%', height: '100%', position: 'relative'}}, children);
}
componentWillUnmount () {
this._window.removeEventListener('mouseup', this._onMouseUp);
}
}
};