Skip to content

Commit

Permalink
Add support for list of keystrokes (#433)
Browse files Browse the repository at this point in the history
  • Loading branch information
fcollonval authored Oct 10, 2022
1 parent 06de257 commit b5bf5ea
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 25 deletions.
50 changes: 32 additions & 18 deletions packages/commands/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
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 @@ -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()', () => {
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 @@ -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;
}

/**
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 @@ -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;
}
}

Expand Down
2 changes: 1 addition & 1 deletion review/api/commands.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit b5bf5ea

Please sign in to comment.