2020-09-06 14:00:25 +02:00
|
|
|
import { useState, useEffect } from 'react';
|
2020-11-07 17:59:37 +02:00
|
|
|
import KeymapService, { KeymapItem } from '@joplin/lib/services/KeymapService';
|
2020-10-31 14:25:12 +02:00
|
|
|
import getLabel from './getLabel';
|
2020-09-06 14:00:25 +02:00
|
|
|
|
|
|
|
const keymapService = KeymapService.instance();
|
|
|
|
|
|
|
|
// This custom hook provides a synchronized snapshot of the keymap residing at KeymapService
|
|
|
|
// All the logic regarding altering and interacting with the keymap is isolated from the components
|
|
|
|
|
2020-10-31 14:25:12 +02:00
|
|
|
function allKeymapItems() {
|
|
|
|
const output = keymapService.getKeymapItems().slice();
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
output.sort((a: KeymapItem, b: KeymapItem) => {
|
2020-10-31 14:25:12 +02:00
|
|
|
return getLabel(a.command).toLocaleLowerCase() < getLabel(b.command).toLocaleLowerCase() ? -1 : +1;
|
|
|
|
});
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2020-09-06 14:00:25 +02:00
|
|
|
const useKeymap = (): [
|
|
|
|
KeymapItem[],
|
|
|
|
Error,
|
2020-11-12 21:13:28 +02:00
|
|
|
(keymapItems: KeymapItem[])=> void,
|
|
|
|
(commandName: string, accelerator: string)=> void,
|
2020-11-12 21:23:54 +02:00
|
|
|
(commandName: string)=> void,
|
2020-09-06 14:00:25 +02:00
|
|
|
] => {
|
2020-10-31 14:25:12 +02:00
|
|
|
const [keymapItems, setKeymapItems] = useState<KeymapItem[]>(() => allKeymapItems());
|
2020-09-06 14:00:25 +02:00
|
|
|
const [keymapError, setKeymapError] = useState<Error>(null);
|
2020-10-09 19:35:46 +02:00
|
|
|
const [mustSave, setMustSave] = useState(false);
|
2020-09-06 14:00:25 +02:00
|
|
|
|
|
|
|
const setAccelerator = (commandName: string, accelerator: string) => {
|
|
|
|
setKeymapItems(prevKeymap => {
|
|
|
|
const newKeymap = [...prevKeymap];
|
|
|
|
|
|
|
|
newKeymap.find(item => item.command === commandName).accelerator = accelerator || null /* Disabled */;
|
|
|
|
return newKeymap;
|
|
|
|
});
|
2020-10-09 19:35:46 +02:00
|
|
|
|
|
|
|
setMustSave(true);
|
2020-09-06 14:00:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const resetAccelerator = (commandName: string) => {
|
|
|
|
const defaultAccelerator = keymapService.getDefaultAccelerator(commandName);
|
|
|
|
setKeymapItems(prevKeymap => {
|
|
|
|
const newKeymap = [...prevKeymap];
|
|
|
|
|
|
|
|
newKeymap.find(item => item.command === commandName).accelerator = defaultAccelerator;
|
|
|
|
return newKeymap;
|
|
|
|
});
|
2020-10-09 19:35:46 +02:00
|
|
|
|
|
|
|
setMustSave(true);
|
2020-09-06 14:00:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const overrideKeymapItems = (customKeymapItems: KeymapItem[]) => {
|
|
|
|
const oldKeymapItems = [...customKeymapItems];
|
2020-10-31 14:25:12 +02:00
|
|
|
keymapService.resetKeymap(); // Start with a fresh keymap
|
2020-09-06 14:00:25 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
// First, try to update the in-memory keymap of KeymapService
|
|
|
|
// This function will throw if there are any issues with the new custom keymap
|
|
|
|
keymapService.overrideKeymap(customKeymapItems);
|
|
|
|
// Then, update the state with the data from KeymapService
|
|
|
|
// Side-effect: Changes will also be saved to the disk
|
|
|
|
setKeymapItems(keymapService.getKeymapItems());
|
2023-02-16 12:55:24 +02:00
|
|
|
} catch (error) {
|
2020-09-06 14:00:25 +02:00
|
|
|
// oldKeymapItems includes even the unchanged keymap items
|
|
|
|
// However, it is not an issue because the logic accounts for such scenarios
|
|
|
|
keymapService.overrideKeymap(oldKeymapItems);
|
2023-02-16 12:55:24 +02:00
|
|
|
throw error;
|
2020-09-06 14:00:25 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
2020-10-09 19:35:46 +02:00
|
|
|
if (!mustSave) return;
|
|
|
|
|
|
|
|
setMustSave(false);
|
|
|
|
|
|
|
|
async function saveKeymap() {
|
|
|
|
try {
|
|
|
|
keymapService.overrideKeymap(keymapItems);
|
|
|
|
await keymapService.saveCustomKeymap();
|
|
|
|
setKeymapError(null);
|
2023-02-16 12:55:24 +02:00
|
|
|
} catch (error) {
|
|
|
|
error.message = `Could not save file: ${error.message}`;
|
2020-10-25 19:46:41 +02:00
|
|
|
setKeymapError(error);
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
2020-09-06 14:00:25 +02:00
|
|
|
}
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2020-11-25 16:40:25 +02:00
|
|
|
void saveKeymap();
|
2020-10-09 19:35:46 +02:00
|
|
|
}, [keymapItems, mustSave]);
|
2020-09-06 14:00:25 +02:00
|
|
|
|
|
|
|
return [keymapItems, keymapError, overrideKeymapItems, setAccelerator, resetAccelerator];
|
|
|
|
};
|
|
|
|
|
|
|
|
export default useKeymap;
|