From b5bf5eacdb0fa516874da3cb8660ae3c7bc1a3da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Collonval?= Date: Mon, 10 Oct 2022 10:02:26 +0200 Subject: [PATCH] Add support for list of keystrokes (#433) --- packages/commands/src/index.ts | 50 +++++++++++++++-------- packages/commands/tests/src/index.spec.ts | 5 +++ packages/widgets/src/commandpalette.ts | 4 +- packages/widgets/src/menu.ts | 4 +- review/api/commands.api.md | 2 +- 5 files changed, 40 insertions(+), 25 deletions(-) diff --git a/packages/commands/src/index.ts b/packages/commands/src/index.ts index 9c653b80e..d6d28961f 100644 --- a/packages/commands/src/index.ts +++ b/packages/commands/src/index.ts @@ -1186,26 +1186,40 @@ export namespace CommandRegistry { } /** - * Format a keystroke for display on the local system. + * Format keystrokes for display on the local system. + * + * If a list of keystrokes is provided, it will be displayed as + * a comma-separated string + * + * @param keystroke The keystrokes to format + * @returns The keystrokes representation */ - export function formatKeystroke(keystroke: string): string { - let mods = []; - let separator = Platform.IS_MAC ? ' ' : '+'; - let parts = parseKeystroke(keystroke); - if (parts.ctrl) { - mods.push('Ctrl'); - } - if (parts.alt) { - mods.push('Alt'); - } - if (parts.shift) { - mods.push('Shift'); - } - if (Platform.IS_MAC && parts.cmd) { - mods.push('Cmd'); + export function formatKeystroke( + keystroke: string | readonly string[] + ): string { + return typeof keystroke === 'string' + ? formatSingleKey(keystroke) + : keystroke.map(formatSingleKey).join(', '); + + function formatSingleKey(key: string) { + let mods = []; + let separator = Platform.IS_MAC ? ' ' : '+'; + let parts = parseKeystroke(key); + if (parts.ctrl) { + mods.push('Ctrl'); + } + if (parts.alt) { + mods.push('Alt'); + } + if (parts.shift) { + mods.push('Shift'); + } + if (Platform.IS_MAC && parts.cmd) { + mods.push('Cmd'); + } + mods.push(parts.key); + return mods.map(Private.formatKey).join(separator); } - mods.push(parts.key); - return mods.map(Private.formatKey).join(separator); } /** diff --git a/packages/commands/tests/src/index.spec.ts b/packages/commands/tests/src/index.spec.ts index 350c2843d..d19e07325 100644 --- a/packages/commands/tests/src/index.spec.ts +++ b/packages/commands/tests/src/index.spec.ts @@ -1271,6 +1271,11 @@ describe('@lumino/commands', () => { expect(label).to.equal('Alt+Down'); } }); + + it('should format a list of keys', () => { + let label = CommandRegistry.formatKeystroke(['D', 'D']); + expect(label).to.equal('D, D'); + }); }); describe('.normalizeKeystroke()', () => { diff --git a/packages/widgets/src/commandpalette.ts b/packages/widgets/src/commandpalette.ts index dd0cd322a..908d87c25 100644 --- a/packages/widgets/src/commandpalette.ts +++ b/packages/widgets/src/commandpalette.ts @@ -957,9 +957,7 @@ export namespace CommandPalette { */ formatItemShortcut(data: IItemRenderData): h.Child { let kb = data.item.keyBinding; - return kb - ? kb.keys.map(CommandRegistry.formatKeystroke).join(', ') - : null; + return kb ? CommandRegistry.formatKeystroke(kb.keys) : null; } /** diff --git a/packages/widgets/src/menu.ts b/packages/widgets/src/menu.ts index e91570cf0..054162675 100644 --- a/packages/widgets/src/menu.ts +++ b/packages/widgets/src/menu.ts @@ -1369,9 +1369,7 @@ export namespace Menu { */ formatShortcut(data: IRenderData): h.Child { let kb = data.item.keyBinding; - return kb - ? kb.keys.map(CommandRegistry.formatKeystroke).join(', ') - : null; + return kb ? CommandRegistry.formatKeystroke(kb.keys) : null; } } diff --git a/review/api/commands.api.md b/review/api/commands.api.md index b72b81347..81ed057ee 100644 --- a/review/api/commands.api.md +++ b/review/api/commands.api.md @@ -48,7 +48,7 @@ export namespace CommandRegistry { export type Description = { args: ReadonlyJSONObject | null; }; - export function formatKeystroke(keystroke: string): string; + export function formatKeystroke(keystroke: string | readonly string[]): string; export interface ICommandChangedArgs { readonly id: string | undefined; readonly type: 'added' | 'removed' | 'changed' | 'many-changed';