Skip to content

Commit

Permalink
Moved repeated logic to be called from command registry
Browse files Browse the repository at this point in the history
  • Loading branch information
e218736 committed Jan 2, 2024
1 parent cffd4aa commit ebc9057
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 60 deletions.
42 changes: 42 additions & 0 deletions packages/commands/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,48 @@ export namespace CommandRegistry {
mods.push(key);
return mods.join(' ');
}

/**
* Format keystroke aria labels.
*
* If a list of keystrokes includes puntuation, it will create an
* aria label and substitue symbol values to text.
*
* @param keystroke The keystrokes to format
* @returns The keystrokes representation
*/
export function formatKeystrokeAriaLabel(
keystroke: readonly string[]
): string {
const keyToText: { [key: string]: string } = {
']': 'Closing bracket',
'[': 'Opening bracket',
',': 'Comma',
'.': 'Full stop',
"'": 'Single quote',
'-': 'Hyphen-minus'
};

let result = CommandRegistry.formatKeystroke(keystroke);

let punctuationRegex = /\p{P}/u;
let punctuations = result?.match(punctuationRegex);
if (!punctuations) {
return result;
}
for (const punctuation of punctuations) {
if (result != null && Object.keys(keyToText).includes(punctuation)) {
const individualKeys = result.split('+');
let index = individualKeys.indexOf(punctuation);
if (index != -1) {
individualKeys[index] = keyToText[punctuation];
}
const textShortcut = individualKeys.join('+');
return textShortcut;
}
}
return result;
}
}

/**
Expand Down
40 changes: 9 additions & 31 deletions packages/widgets/src/commandpalette.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,10 @@ export class CommandPalette extends Widget {
* @returns The command items added to the palette.
*/
addItems(items: CommandPalette.IItemOptions[]): CommandPalette.IItem[] {
const newItems = items.map(item => Private.createItem(this.commands, item));
newItems.forEach(item => this._items.push(item));
const newItems = items.map((item) =>
Private.createItem(this.commands, item)
);
newItems.forEach((item) => this._items.push(item));
this.refresh();
return newItems;
}
Expand Down Expand Up @@ -375,7 +377,7 @@ export class CommandPalette extends Widget {
}

// Find the index of the item which was clicked.
let index = ArrayExt.findFirstIndex(this.contentNode.children, node => {
let index = ArrayExt.findFirstIndex(this.contentNode.children, (node) => {
return node.contains(event.target as HTMLElement);
});

Expand Down Expand Up @@ -984,35 +986,11 @@ export namespace CommandPalette {
* @returns The aria label content to add to the shortcut node.
*/
formatItemAria(data: IItemRenderData): h.Child {
const keyToText: { [key: string]: string } = {
']': 'Closing bracket',
'[': 'Opening bracket',
',': 'Comma',
'.': 'Full stop',
"'": 'Single quote',
'-': 'Hyphen-minus'
};

let kbText = data.item.keyBinding;
let result = kbText ? CommandRegistry.formatKeystroke(kbText.keys) : null;

let punctuationRegex = /\p{P}/u;
let punctuations = result?.match(punctuationRegex);
if (!punctuations) {
return [];
}
for (const punctuation of punctuations) {
if (result != null && Object.keys(keyToText).includes(punctuation)) {
const individualKeys = result.split('+');
let index = individualKeys.indexOf(punctuation);
if (index != -1) {
individualKeys[index] = keyToText[punctuation];
}
const textShortcut = individualKeys.join('+');
return textShortcut;
}
}
return kbText ? CommandRegistry.formatKeystroke(kbText.keys) : null;
return kbText
? CommandRegistry.formatKeystrokeAriaLabel(kbText.keys)
: null;
}

/**
Expand Down Expand Up @@ -1587,7 +1565,7 @@ namespace Private {
get keyBinding(): CommandRegistry.IKeyBinding | null {
let { command, args } = this;
return (
ArrayExt.findLastValue(this._commands.keyBindings, kb => {
ArrayExt.findLastValue(this._commands.keyBindings, (kb) => {
return kb.command === command && JSONExt.deepEqual(kb.args, args);
}) || null
);
Expand Down
34 changes: 5 additions & 29 deletions packages/widgets/src/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ export class Menu extends Widget {
*/
private _evtMouseMove(event: MouseEvent): void {
// Hit test the item nodes for the item under the mouse.
let index = ArrayExt.findFirstIndex(this.contentNode.children, node => {
let index = ArrayExt.findFirstIndex(this.contentNode.children, (node) => {
return ElementExt.hitTest(node, event.clientX, event.clientY);
});

Expand Down Expand Up @@ -1383,35 +1383,11 @@ export namespace Menu {
* @returns The aria label content to add to the shortcut node.
*/
formatShortcutText(data: IRenderData): h.Child {
const keyToText: { [key: string]: string } = {
']': 'Closing bracket',
'[': 'Opening bracket',
',': 'Comma',
'.': 'Full stop',
"'": 'Single quote',
'-': 'Hyphen-minus'
};

let kbText = data.item.keyBinding;
let result = kbText ? CommandRegistry.formatKeystroke(kbText.keys) : null;

let punctuationRegex = /\p{P}/u;
let punctuations = result?.match(punctuationRegex);
if (!punctuations) {
return [];
}
for (const punctuation of punctuations) {
if (result != null && Object.keys(keyToText).includes(punctuation)) {
const individualKeys = result.split('+');
let index = individualKeys.indexOf(punctuation);
if (index != -1) {
individualKeys[index] = keyToText[punctuation];
}
const textShortcut = individualKeys.join('+');
return textShortcut;
}
}
return kbText ? CommandRegistry.formatKeystroke(kbText.keys) : null;
return kbText
? CommandRegistry.formatKeystrokeAriaLabel(kbText.keys)
: null;
}
}

Expand Down Expand Up @@ -1951,7 +1927,7 @@ namespace Private {
if (this.type === 'command') {
let { command, args } = this;
return (
ArrayExt.findLastValue(this._commands.keyBindings, kb => {
ArrayExt.findLastValue(this._commands.keyBindings, (kb) => {
return kb.command === command && JSONExt.deepEqual(kb.args, args);
}) || null
);
Expand Down

0 comments on commit ebc9057

Please sign in to comment.