Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for list of keystrokes #433

Merged
merged 1 commit into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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