Skip to content

Commit

Permalink
Backport PR jupyterlab#433: Add support for list of keystrokes
Browse files Browse the repository at this point in the history
  • Loading branch information
fcollonval committed Oct 11, 2022
1 parent 7c16d52 commit 49ca474
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 24 deletions.
50 changes: 32 additions & 18 deletions packages/commands/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1169,26 +1169,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);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions packages/commands/tests/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,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()', () => {
Expand Down
4 changes: 1 addition & 3 deletions packages/widgets/src/commandpalette.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1039,9 +1039,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;
}

/**
Expand Down
4 changes: 1 addition & 3 deletions packages/widgets/src/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1436,9 +1436,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;
}
}

Expand Down

0 comments on commit 49ca474

Please sign in to comment.