1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-06-30 23:44:55 +02:00

Desktop: Simplified and improve command service, and added command palette

- Commands "enabled" state is now expressed using a "when-clause" like in VSCode
- A command palette has been added to the Tools menu
This commit is contained in:
Laurent Cozic
2020-10-18 21:52:10 +01:00
parent f529adac99
commit 3a57cfea02
78 changed files with 897 additions and 756 deletions

View File

@ -16,7 +16,7 @@ const defaultKeymapItems = {
{ accelerator: 'Cmd+N', command: 'newNote' },
{ accelerator: 'Cmd+T', command: 'newTodo' },
{ accelerator: 'Cmd+S', command: 'synchronize' },
{ accelerator: 'Cmd+P', command: 'print' },
{ accelerator: '', command: 'print' },
{ accelerator: 'Cmd+H', command: 'hideApp' },
{ accelerator: 'Cmd+Q', command: 'quit' },
{ accelerator: 'Cmd+,', command: 'config' },
@ -37,20 +37,21 @@ const defaultKeymapItems = {
{ accelerator: 'Shift+Cmd+L', command: 'focusElementNoteList' },
{ accelerator: 'Shift+Cmd+N', command: 'focusElementNoteTitle' },
{ accelerator: 'Shift+Cmd+B', command: 'focusElementNoteBody' },
{ accelerator: 'Option+Cmd+S', command: 'toggleSidebar' },
{ accelerator: 'Option+Cmd+S', command: 'toggleSideBar' },
{ accelerator: 'Option+Cmd+L', command: 'toggleNoteList' },
{ accelerator: 'Cmd+L', command: 'toggleVisiblePanes' },
{ accelerator: 'Cmd+0', command: 'zoomActualSize' },
{ accelerator: 'Cmd+E', command: 'toggleExternalEditing' },
{ accelerator: 'Option+Cmd+T', command: 'setTags' },
{ accelerator: 'Cmd+G', command: 'gotoAnything' },
{ accelerator: 'Cmd+P', command: 'gotoAnything' },
{ accelerator: 'Shift+Cmd+P', command: 'commandPalette' },
{ accelerator: 'F1', command: 'help' },
],
default: [
{ accelerator: 'Ctrl+N', command: 'newNote' },
{ accelerator: 'Ctrl+T', command: 'newTodo' },
{ accelerator: 'Ctrl+S', command: 'synchronize' },
{ accelerator: 'Ctrl+P', command: 'print' },
{ accelerator: '', command: 'print' },
{ accelerator: 'Ctrl+Q', command: 'quit' },
{ accelerator: 'Ctrl+Alt+I', command: 'insertTemplate' },
{ accelerator: 'Ctrl+C', command: 'textCopy' },
@ -68,14 +69,15 @@ const defaultKeymapItems = {
{ accelerator: 'Ctrl+Shift+L', command: 'focusElementNoteList' },
{ accelerator: 'Ctrl+Shift+N', command: 'focusElementNoteTitle' },
{ accelerator: 'Ctrl+Shift+B', command: 'focusElementNoteBody' },
{ accelerator: 'F10', command: 'toggleSidebar' },
{ accelerator: 'F10', command: 'toggleSideBar' },
{ accelerator: 'F11', command: 'toggleNoteList' },
{ accelerator: 'Ctrl+L', command: 'toggleVisiblePanes' },
{ accelerator: 'Ctrl+0', command: 'zoomActualSize' },
{ accelerator: 'Ctrl+E', command: 'toggleExternalEditing' },
{ accelerator: 'Ctrl+Alt+T', command: 'setTags' },
{ accelerator: 'Ctrl+,', command: 'config' },
{ accelerator: 'Ctrl+G', command: 'gotoAnything' },
{ accelerator: 'Ctrl+P', command: 'gotoAnything' },
{ accelerator: 'Ctrl+Shift+P', command: 'commandPalette' },
{ accelerator: 'F1', command: 'help' },
],
};
@ -90,13 +92,14 @@ interface Keymap {
}
export default class KeymapService extends BaseService {
private keymap: Keymap;
private platform: string;
private customKeymapPath: string;
private defaultKeymapItems: KeymapItem[];
private lastSaveTime_:number;
constructor() {
public constructor() {
super();
this.lastSaveTime_ = Date.now();
@ -106,11 +109,11 @@ export default class KeymapService extends BaseService {
this.initialize();
}
get lastSaveTime():number {
public get lastSaveTime():number {
return this.lastSaveTime_;
}
initialize(platform: string = shim.platformName()) {
public initialize(platform: string = shim.platformName()) {
this.platform = platform;
switch (platform) {
@ -131,7 +134,7 @@ export default class KeymapService extends BaseService {
}
}
async loadCustomKeymap(customKeymapPath: string) {
public async loadCustomKeymap(customKeymapPath: string) {
this.customKeymapPath = customKeymapPath; // Useful for saving the changes later
if (await shim.fsDriver().exists(customKeymapPath)) {
@ -143,7 +146,7 @@ export default class KeymapService extends BaseService {
}
}
async saveCustomKeymap(customKeymapPath: string = this.customKeymapPath) {
public async saveCustomKeymap(customKeymapPath: string = this.customKeymapPath) {
this.logger().info(`KeymapService: Saving keymap to file: ${customKeymapPath}`);
try {
@ -161,7 +164,7 @@ export default class KeymapService extends BaseService {
}
}
acceleratorExists(command: string) {
public acceleratorExists(command: string) {
return !!this.keymap[command];
}
@ -189,33 +192,33 @@ export default class KeymapService extends BaseService {
};
}
setAccelerator(command: string, accelerator: string) {
public setAccelerator(command: string, accelerator: string) {
this.keymap[command].accelerator = accelerator;
}
getAccelerator(command: string) {
public getAccelerator(command: string) {
const item = this.keymap[command];
if (!item) throw new Error(`KeymapService: "${command}" command does not exist!`);
return item.accelerator;
}
getDefaultAccelerator(command: string) {
public getDefaultAccelerator(command: string) {
const defaultItem = this.defaultKeymapItems.find((item => item.command === command));
if (!defaultItem) throw new Error(`KeymapService: "${command}" command does not exist!`);
return defaultItem.accelerator;
}
getCommandNames() {
public getCommandNames() {
return Object.keys(this.keymap);
}
getKeymapItems() {
public getKeymapItems() {
return Object.values(this.keymap);
}
getCustomKeymapItems() {
public getCustomKeymapItems() {
const customkeymapItems: KeymapItem[] = [];
this.defaultKeymapItems.forEach(({ command, accelerator }) => {
const currentAccelerator = this.getAccelerator(command);
@ -236,11 +239,11 @@ export default class KeymapService extends BaseService {
return customkeymapItems;
}
getDefaultKeymapItems() {
public getDefaultKeymapItems() {
return [...this.defaultKeymapItems];
}
overrideKeymap(customKeymapItems: KeymapItem[]) {
public overrideKeymap(customKeymapItems: KeymapItem[]) {
try {
for (let i = 0; i < customKeymapItems.length; i++) {
const item = customKeymapItems[i];
@ -284,7 +287,7 @@ export default class KeymapService extends BaseService {
}
}
validateKeymap(proposedKeymapItem: KeymapItem = null) {
public validateKeymap(proposedKeymapItem: KeymapItem = null) {
const usedAccelerators = new Set();
// Validate as if the proposed change is already present in the current keymap
@ -312,7 +315,7 @@ export default class KeymapService extends BaseService {
}
}
validateAccelerator(accelerator: string) {
public validateAccelerator(accelerator: string) {
let keyFound = false;
const parts = accelerator.split('+');
@ -334,7 +337,7 @@ export default class KeymapService extends BaseService {
if (!isValid) throw new Error(_('Accelerator "%s" is not valid.', accelerator));
}
domToElectronAccelerator(event: KeyboardEvent<HTMLDivElement>) {
public domToElectronAccelerator(event: KeyboardEvent<HTMLDivElement>) {
const parts = [];
const { key, ctrlKey, metaKey, altKey, shiftKey } = event;
@ -358,7 +361,7 @@ export default class KeymapService extends BaseService {
return parts.join('+');
}
static domToElectronKey(domKey: string) {
private static domToElectronKey(domKey: string) {
let electronKey;
if (/^([a-z])$/.test(domKey)) {
@ -398,7 +401,7 @@ export default class KeymapService extends BaseService {
private static instance_:KeymapService = null;
static instance():KeymapService {
public static instance():KeymapService {
if (this.instance_) return this.instance_;
this.instance_ = new KeymapService();