mirror of
https://github.com/Foundry376/Mailspring.git
synced 2025-03-01 02:25:45 +08:00
fix(keymaps): Handle empty keymap.json, recover when saving
This commit is contained in:
parent
7de7c53ee2
commit
3344223bfc
2 changed files with 13 additions and 3 deletions
|
@ -83,11 +83,20 @@ export default class CommandKeybinding extends React.Component {
|
||||||
_onFinishedEditing = () => {
|
_onFinishedEditing = () => {
|
||||||
if (this.state.editingBinding) {
|
if (this.state.editingBinding) {
|
||||||
const keymapPath = NylasEnv.keymaps.getUserKeymapPath();
|
const keymapPath = NylasEnv.keymaps.getUserKeymapPath();
|
||||||
|
let keymaps = {};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const exists = fs.existsSync(keymapPath);
|
const exists = fs.existsSync(keymapPath);
|
||||||
const keymaps = exists ? JSON.parse(fs.readFileSync(keymapPath)) : {};
|
if (exists) {
|
||||||
keymaps[this.props.command] = this.state.editingBinding;
|
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));
|
fs.writeFileSync(keymapPath, JSON.stringify(keymaps, null, 2));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
NylasEnv.showErrorDialog(`Nylas was unable to modify your keymaps at ${keymapPath}. ${err.toString()}`);
|
NylasEnv.showErrorDialog(`Nylas was unable to modify your keymaps at ${keymapPath}. ${err.toString()}`);
|
||||||
|
|
|
@ -41,7 +41,8 @@ class KeymapFile {
|
||||||
if (e.code === 'ENOENT') {
|
if (e.code === 'ENOENT') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
throw e;
|
console.error(e);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this._bindings = {};
|
this._bindings = {};
|
||||||
|
|
Loading…
Reference in a new issue