2016-04-01 06:52:03 +08:00
|
|
|
import React from 'react';
|
|
|
|
import path from 'path';
|
|
|
|
import fs from 'fs';
|
2016-04-13 09:09:13 +08:00
|
|
|
import { remote } from 'electron';
|
|
|
|
import { Flexbox } from 'nylas-component-kit';
|
|
|
|
|
2016-05-06 08:05:51 +08:00
|
|
|
import displayedKeybindings from './keymaps/displayed-keybindings';
|
|
|
|
import CommandItem from './keymaps/command-item';
|
2016-04-01 06:52:03 +08:00
|
|
|
|
|
|
|
class PreferencesKeymaps extends React.Component {
|
|
|
|
|
|
|
|
static displayName = 'PreferencesKeymaps';
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
config: React.PropTypes.object,
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.state = {
|
|
|
|
templates: [],
|
|
|
|
bindings: this._getStateFromKeymaps(),
|
|
|
|
};
|
|
|
|
this._loadTemplates();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this._disposable = NylasEnv.keymaps.onDidReloadKeymap(() => {
|
|
|
|
this.setState({bindings: this._getStateFromKeymaps()});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this._disposable.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
_getStateFromKeymaps() {
|
|
|
|
const bindings = {};
|
|
|
|
for (const section of displayedKeybindings) {
|
|
|
|
for (const [command] of section.items) {
|
2016-04-25 01:16:25 +08:00
|
|
|
bindings[command] = NylasEnv.keymaps.getBindingsForCommand(command) || [];
|
2016-04-01 06:52:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return bindings;
|
|
|
|
}
|
|
|
|
|
|
|
|
_loadTemplates() {
|
|
|
|
const templatesDir = path.join(NylasEnv.getLoadSettings().resourcePath, 'keymaps', 'templates');
|
|
|
|
fs.readdir(templatesDir, (err, files) => {
|
2016-10-18 08:59:33 +08:00
|
|
|
if (!files || !(files instanceof Array)) return;
|
2016-04-01 06:52:03 +08:00
|
|
|
let templates = files.filter((filename) => {
|
2016-04-25 01:16:25 +08:00
|
|
|
return path.extname(filename) === '.json';
|
2016-04-01 06:52:03 +08:00
|
|
|
});
|
|
|
|
templates = templates.map((filename) => {
|
|
|
|
return path.parse(filename).name;
|
|
|
|
});
|
|
|
|
this.setState({templates: templates});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_onShowUserKeymaps() {
|
|
|
|
const keymapsFile = NylasEnv.keymaps.getUserKeymapPath();
|
|
|
|
if (!fs.existsSync(keymapsFile)) {
|
2016-05-06 08:05:51 +08:00
|
|
|
fs.writeFileSync(keymapsFile, '{}');
|
2016-04-01 06:52:03 +08:00
|
|
|
}
|
2016-05-06 08:05:51 +08:00
|
|
|
remote.shell.showItemInFolder(keymapsFile);
|
2016-04-01 06:52:03 +08:00
|
|
|
}
|
|
|
|
|
2016-05-06 08:05:51 +08:00
|
|
|
_onDeleteUserKeymap() {
|
|
|
|
const chosen = remote.dialog.showMessageBox(NylasEnv.getCurrentWindow(), {
|
|
|
|
type: 'info',
|
|
|
|
message: "Are you sure?",
|
|
|
|
detail: "Delete your custom key bindings and reset to the template defaults?",
|
|
|
|
buttons: ['Cancel', 'Reset'],
|
|
|
|
});
|
2016-04-01 06:52:03 +08:00
|
|
|
|
2016-05-06 08:05:51 +08:00
|
|
|
if (chosen === 1) {
|
|
|
|
const keymapsFile = NylasEnv.keymaps.getUserKeymapPath();
|
|
|
|
fs.writeFileSync(keymapsFile, '{}');
|
2016-04-01 06:52:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-06 08:05:51 +08:00
|
|
|
_renderBindingsSection = (section) => {
|
2016-04-01 06:52:03 +08:00
|
|
|
return (
|
2016-05-06 08:05:51 +08:00
|
|
|
<section key={`section-${section.title}`}>
|
|
|
|
<div className="shortcut-section-title">{section.title}</div>
|
|
|
|
{
|
|
|
|
section.items.map(([command, label]) => {
|
|
|
|
return (
|
|
|
|
<CommandItem
|
|
|
|
key={command}
|
|
|
|
command={command}
|
|
|
|
label={label}
|
|
|
|
bindings={this.state.bindings[command]}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
</section>
|
2016-04-01 06:52:03 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div className="container-keymaps">
|
|
|
|
<section>
|
|
|
|
<Flexbox className="container-dropdown">
|
|
|
|
<div>Shortcut set:</div>
|
|
|
|
<div className="dropdown">
|
|
|
|
<select
|
|
|
|
style={{margin: 0}}
|
2016-05-06 08:05:51 +08:00
|
|
|
tabIndex={-1}
|
2016-04-01 06:52:03 +08:00
|
|
|
value={this.props.config.get('core.keymapTemplate')}
|
2016-05-07 07:23:48 +08:00
|
|
|
onChange={(event) => this.props.config.set('core.keymapTemplate', event.target.value)}
|
|
|
|
>
|
2016-04-01 06:52:03 +08:00
|
|
|
{this.state.templates.map((template) => {
|
|
|
|
return <option key={template} value={template}>{template}</option>
|
|
|
|
})}
|
|
|
|
</select>
|
|
|
|
</div>
|
2016-10-18 08:59:33 +08:00
|
|
|
<div style={{flex: 1}} />
|
2016-05-06 08:05:51 +08:00
|
|
|
<button className="btn" onClick={this._onDeleteUserKeymap}>Reset to Defaults</button>
|
2016-04-01 06:52:03 +08:00
|
|
|
</Flexbox>
|
2016-05-06 08:05:51 +08:00
|
|
|
<p>
|
|
|
|
You can choose a shortcut set to use keyboard shortcuts of familiar email clients.
|
2016-05-14 07:24:31 +08:00
|
|
|
To edit a shortcut, click it in the list below and enter a replacement on the keyboard.
|
2016-05-06 08:05:51 +08:00
|
|
|
</p>
|
2016-04-01 06:52:03 +08:00
|
|
|
{displayedKeybindings.map(this._renderBindingsSection)}
|
|
|
|
</section>
|
|
|
|
<section>
|
|
|
|
<h2>Customization</h2>
|
2016-05-06 08:05:51 +08:00
|
|
|
<p>You can manage your custom shortcuts directly by editing your shortcuts file.</p>
|
2016-04-01 06:52:03 +08:00
|
|
|
<button className="btn" onClick={this._onShowUserKeymaps}>Edit custom shortcuts</button>
|
|
|
|
</section>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default PreferencesKeymaps;
|