-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
106 lines (97 loc) · 2.79 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
const Mousetrap = require('mousetrap');
const prevTabKey = "Ctrl+Shift+Tab";
const nextTabKey = "Ctrl+Tab";
const prevPaneKey = "CmdOrCtrl+Alt+Left";
const nextPaneKey = "CmdOrCtrl+Alt+Right";
const merge = (one, two) => Object.assign({}, one, two);
const mousetrapify = key => key.replace('CmdOrCtrl', 'mod').toLowerCase();
let focusedWindow;
const registerWindow = win => {
win.on('focus', () => { focusedWindow = win; });
win.on('blur', () => {
if (focusedWindow === win) {
focusedWindow = undefined;
}
});
}
const selectPane = type => {
if (focusedWindow) {
focusedWindow.rpc.emit(`${type} pane req`);
}
}
const decorateMenu = menu => menu.map(menuItem => {
if (menuItem.role !== 'window') {
return menuItem;
}
const newMenuItem = Object.assign({}, menuItem);
newMenuItem.submenu = newMenuItem.submenu.map(submenuItem => {
const newSubmmenuItem = Object.assign({}, submenuItem);
switch (newSubmmenuItem.label) {
case 'Show Previous Tab': newSubmmenuItem.accelerator = prevTabKey; break;
case 'Show Next Tab': newSubmmenuItem.accelerator = nextTabKey; break;
case 'Select Previous Pane': newSubmmenuItem.accelerator = prevPaneKey; break;
case 'Select Next Pane': newSubmmenuItem.accelerator = nextPaneKey; break;
}
return newSubmmenuItem;
});
return newMenuItem;
});
const decorateTerms = (Terms, { React, notify, Notification }) => {
return class extends React.Component {
constructor(props, context) {
super(props, context);
this.handleFocusActive = this.handleFocusActive.bind(this);
this.onTermsRef = this.onTermsRef.bind(this);
}
handleFocusActive() {
const term = this.terms.getActiveTerm();
if (term) {
term.focus();
}
}
attachKeyListeners() {
const term = this.terms.getActiveTerm();
if (!term) {
return;
}
const document = term.getTermDocument();
const keys = new Mousetrap(document);
keys.bind(
mousetrapify(prevPaneKey),
() => selectPane('prev')
);
keys.bind(
mousetrapify(nextPaneKey),
() => selectPane('next')
);
this.keys = keys;
}
onTermsRef(terms) {
this.terms = terms;
}
componentDidUpdate(prev) {
if (prev.activeSession !== this.props.activeSession) {
if (this.keys) {
this.keys.reset();
}
this.handleFocusActive();
this.attachKeyListeners();
}
}
componentWillUnmount() {
if (this.keys) {
this.keys.reset();
}
}
render() {
return React.createElement(Terms, merge(this.props, {
ref: this.onTermsRef
}));
}
}
}
module.exports = {
onWindow: registerWindow,
decorateTerms: decorateTerms,
decorateMenu: decorateMenu,
}