Mailspring/app/internal_packages/preferences/lib/tabs/keymaps/command-item.jsx

175 lines
4.5 KiB
React
Raw Normal View History

import React from 'react';
import ReactDOM from 'react-dom';
2017-09-27 02:33:08 +08:00
import PropTypes from 'prop-types';
import _ from 'underscore';
2017-09-27 02:46:00 +08:00
import { Flexbox } from 'mailspring-component-kit';
import fs from 'fs';
2017-09-27 02:33:08 +08:00
import { keyAndModifiersForEvent } from './mousetrap-keybinding-helpers';
export default class CommandKeybinding extends React.Component {
static propTypes = {
2017-09-27 02:33:08 +08:00
bindings: PropTypes.array,
label: PropTypes.string,
command: PropTypes.string,
};
constructor(props) {
super(props);
this.state = {
editing: false,
2017-09-27 02:33:08 +08:00
};
}
componentDidUpdate() {
2017-09-27 02:33:08 +08:00
const { modifiers, keys, editing } = this.state;
if (editing) {
2017-09-27 02:33:08 +08:00
const finished = (modifiers.length > 0 && keys.length > 0) || keys.length >= 2;
if (finished) {
ReactDOM.findDOMNode(this).blur();
}
}
}
_formatKeystrokes(original) {
// On Windows, display cmd-shift-c
2017-09-27 02:33:08 +08:00
if (process.platform === 'win32') return original;
// Replace "cmd" => ⌘, etc.
const modifiers = [
[/\+(?!$)/gi, ''],
[/command/gi, '⌘'],
[/meta/gi, '⌘'],
[/alt/gi, '⌥'],
[/shift/gi, '⇧'],
[/ctrl/gi, '^'],
2017-09-27 02:33:08 +08:00
[/mod/gi, process.platform === 'darwin' ? '⌘' : '^'],
];
let clean = original;
for (const [regexp, char] of modifiers) {
clean = clean.replace(regexp, char);
}
// ⌘⇧c => ⌘⇧C
if (clean !== original) {
clean = clean.toUpperCase();
}
// backspace => Backspace
if (original.length > 1 && clean === original) {
clean = clean[0].toUpperCase() + clean.slice(1);
}
return clean;
}
_renderKeystrokes = (keystrokes, idx) => {
const elements = [];
const splitKeystrokes = keystrokes.split(' ');
splitKeystrokes.forEach((keystroke, kidx) => {
elements.push(<span key={kidx}>{this._formatKeystrokes(keystroke)}</span>);
if (kidx < splitKeystrokes.length - 1) {
2017-09-27 02:33:08 +08:00
elements.push(
<span className="then" key={`then${kidx}`}>
{' '}
then{' '}
</span>
);
}
});
return (
2017-09-27 02:33:08 +08:00
<span key={`keystrokes-${idx}`} className="shortcut-value">
{elements}
</span>
);
2017-09-27 02:33:08 +08:00
};
_onEdit = () => {
2017-09-27 02:33:08 +08:00
this.setState({ editing: true, editingBinding: null, keys: [], modifiers: [] });
2017-09-27 02:36:58 +08:00
AppEnv.keymaps.suspendAllKeymaps();
2017-09-27 02:33:08 +08:00
};
_onFinishedEditing = () => {
if (this.state.editingBinding) {
2017-09-27 02:36:58 +08:00
const keymapPath = AppEnv.keymaps.getUserKeymapPath();
let keymaps = {};
try {
const exists = fs.existsSync(keymapPath);
if (exists) {
keymaps = JSON.parse(fs.readFileSync(keymapPath));
}
} catch (err) {
console.error(err);
}
keymaps[this.props.command] = this.state.editingBinding;
try {
fs.writeFileSync(keymapPath, JSON.stringify(keymaps, null, 2));
} catch (err) {
2017-09-27 02:36:58 +08:00
AppEnv.showErrorDialog(
2017-09-27 02:33:08 +08:00
`Nylas was unable to modify your keymaps at ${keymapPath}. ${err.toString()}`
);
}
}
2017-09-27 02:33:08 +08:00
this.setState({ editing: false, editingBinding: null });
2017-09-27 02:36:58 +08:00
AppEnv.keymaps.resumeAllKeymaps();
2017-09-27 02:33:08 +08:00
};
2017-09-27 02:33:08 +08:00
_onKey = event => {
if (!this.state.editing) {
return;
}
event.preventDefault();
event.stopPropagation();
const [eventKey, eventMods] = keyAndModifiersForEvent(event);
if (!eventKey || ['mod', 'meta', 'command', 'ctrl', 'alt', 'shift'].includes(eventKey)) {
return;
}
2017-09-27 02:33:08 +08:00
let { keys, modifiers } = this.state;
keys = keys.concat([eventKey]);
modifiers = _.uniq(modifiers.concat(eventMods));
let editingBinding = keys.join(' ');
if (modifiers.length > 0) {
editingBinding = [].concat(modifiers, keys).join('+');
editingBinding = editingBinding.replace(/(meta|command|ctrl)/g, 'mod');
}
2017-09-27 02:33:08 +08:00
this.setState({ keys, modifiers, editingBinding });
};
render() {
2017-09-27 02:33:08 +08:00
const { editing, editingBinding } = this.state;
const bindings = editingBinding ? [editingBinding] : this.props.bindings;
2017-09-27 02:33:08 +08:00
let value = 'None';
if (bindings.length > 0) {
value = _.uniq(bindings).map(this._renderKeystrokes);
}
2017-09-27 02:33:08 +08:00
let classnames = 'shortcut';
if (editing) {
2017-09-27 02:33:08 +08:00
classnames += ' editing';
}
return (
<Flexbox
className={classnames}
tabIndex={-1}
onKeyDown={this._onKey}
onKeyPress={this._onKey}
onFocus={this._onEdit}
onBlur={this._onFinishedEditing}
>
2017-09-27 02:33:08 +08:00
<div className="col-left shortcut-name">{this.props.label}</div>
<div className="col-right">
<div className="values">{value}</div>
</div>
</Flexbox>
);
}
}