Skip to content

Commit 6f692d4

Browse files
chore: rename shortcut formatting functions
1 parent 159daf9 commit 6f692d4

File tree

4 files changed

+35
-32
lines changed

4 files changed

+35
-32
lines changed

src/actions/clipboard.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import * as Constants from '../constants';
1818
import type {BlockSvg, WorkspaceSvg} from 'blockly';
1919
import {Navigation} from '../navigation';
2020
import {ScopeWithConnection} from './action_menu';
21-
import {formatActionShortcut} from '../shortcut_formatting';
21+
import {getShortActionShortcut} from '../shortcut_formatting';
2222

2323
const KeyCodes = blocklyUtils.KeyCodes;
2424
const createSerializedKey = ShortcutRegistry.registry.createSerializedKey.bind(
@@ -101,7 +101,7 @@ export class Clipboard {
101101
*/
102102
private registerCutContextMenuAction() {
103103
const cutAction: ContextMenuRegistry.RegistryItem = {
104-
displayText: (scope) => `Cut (${formatActionShortcut('cut', 'short')})`,
104+
displayText: (scope) => `Cut (${getShortActionShortcut('cut')})`,
105105
preconditionFn: (scope) => {
106106
const ws = scope.block?.workspace;
107107
if (!ws) return 'hidden';
@@ -196,7 +196,7 @@ export class Clipboard {
196196
*/
197197
private registerCopyContextMenuAction() {
198198
const copyAction: ContextMenuRegistry.RegistryItem = {
199-
displayText: (scope) => `Copy (${formatActionShortcut('copy', 'short')})`,
199+
displayText: (scope) => `Copy (${getShortActionShortcut('copy')})`,
200200
preconditionFn: (scope) => {
201201
const ws = scope.block?.workspace;
202202
if (!ws) return 'hidden';
@@ -305,8 +305,7 @@ export class Clipboard {
305305
*/
306306
private registerPasteContextMenuAction() {
307307
const pasteAction: ContextMenuRegistry.RegistryItem = {
308-
displayText: (scope) =>
309-
`Paste (${formatActionShortcut('paste', 'short')})`,
308+
displayText: (scope) => `Paste (${getShortActionShortcut('paste')})`,
310309
preconditionFn: (scope: ScopeWithConnection) => {
311310
const block = scope.block ?? scope.connection?.getSourceBlock();
312311
const ws = block?.workspace as WorkspaceSvg | null;

src/actions/enter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import type {
2222

2323
import * as Constants from '../constants';
2424
import type {Navigation} from '../navigation';
25-
import {formatActionShortcut} from '../shortcut_formatting';
25+
import {getShortActionShortcut} from '../shortcut_formatting';
2626
import {Mover} from './mover';
2727

2828
const KeyCodes = BlocklyUtils.KeyCodes;
@@ -104,7 +104,7 @@ export class EnterAction {
104104
} else if (nodeType === ASTNode.types.BLOCK) {
105105
const block = curNode.getLocation() as Block;
106106
if (!this.tryShowFullBlockFieldEditor(block)) {
107-
const shortcut = formatActionShortcut('list_shortcuts', 'short');
107+
const shortcut = getShortActionShortcut('list_shortcuts');
108108
const message = `Press ${shortcut} for help on keyboard controls`;
109109
dialog.alert(message);
110110
}

src/shortcut_dialog.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import * as Blockly from 'blockly/core';
88
import * as Constants from './constants';
99
import {ShortcutRegistry} from 'blockly/core';
1010
import {
11-
actionShortcutsForPlatform,
11+
getLongActionShortcutsAsKeys,
1212
upperCaseFirst,
1313
} from './shortcut_formatting';
1414

@@ -139,7 +139,7 @@ export class ShortcutDialog {
139139
}
140140

141141
private actionShortcutsToHTML(action: string) {
142-
const shortcuts = actionShortcutsForPlatform(action, 'long');
142+
const shortcuts = getLongActionShortcutsAsKeys(action);
143143
return shortcuts.map((keys) => this.actionShortcutToHTML(keys)).join(' / ');
144144
}
145145

src/shortcut_formatting.ts

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,39 @@ import {keyNames} from './keynames';
44
const isMacPlatform = navigator.platform.startsWith('Mac');
55

66
/**
7-
* Format the primary shortcut for this platform in a user facing format.
7+
* Find the primary shortcut for this platform and return it as single string
8+
* in a short user facing format.
89
*
910
* @param action The action name, e.g. "cut".
10-
* @param format The key format.
1111
* @returns The formatted shortcut.
1212
*/
13-
export function formatActionShortcut(
14-
action: string,
15-
format: ShortcutFormat,
16-
): string {
17-
const parts = actionShortcutsForPlatform(action, format)[0];
13+
export function getShortActionShortcut(action: string): string {
14+
const parts = getActionShortcutsAsKeys(action, shortModifierNames)[0];
1815
return parts.join(isMacPlatform ? ' ' : ' + ');
1916
}
2017

21-
const modifierNamesByFormat: Record<ShortcutFormat, Record<string, string>> = {
22-
long: {
23-
'Control': 'Ctrl',
24-
'Meta': '⌘ Command',
25-
'Alt': isMacPlatform ? '⌥ Option' : 'Alt',
26-
},
27-
short: {
28-
'Control': 'Ctrl',
29-
'Meta': '⌘',
30-
'Alt': isMacPlatform ? '⌥' : 'Alt',
31-
},
18+
/**
19+
* Find the relevant shortcuts for the given action for the current platform.
20+
* Keys are returned in a long user facing format.
21+
*
22+
* @param action The action name, e.g. "cut".
23+
* @returns The formatted shortcuts as individual keys.
24+
*/
25+
export function getLongActionShortcutsAsKeys(action: string): string[][] {
26+
return getActionShortcutsAsKeys(action, longModifierNames);
27+
}
28+
29+
const longModifierNames: Record<string, string> = {
30+
'Control': 'Ctrl',
31+
'Meta': '⌘ Command',
32+
'Alt': isMacPlatform ? '⌥ Option' : 'Alt',
3233
};
3334

34-
export type ShortcutFormat = 'long' | 'short';
35+
const shortModifierNames: Record<string, string> = {
36+
'Control': 'Ctrl',
37+
'Meta': '⌘',
38+
'Alt': isMacPlatform ? '⌥' : 'Alt',
39+
};
3540

3641
/**
3742
* Find the relevant shortcuts for the given action for the current platform.
@@ -41,14 +46,13 @@ export type ShortcutFormat = 'long' | 'short';
4146
* current platform or tagged them with a platform.
4247
*
4348
* @param action The action name, e.g. "cut".
44-
* @param format The key format.
49+
* @param modifierNames The names to use for the Meta/Control/Alt modifiers.
4550
* @returns The formatted shortcuts.
4651
*/
47-
export function actionShortcutsForPlatform(
52+
function getActionShortcutsAsKeys(
4853
action: string,
49-
format: ShortcutFormat,
54+
modifierNames: Record<string, string>,
5055
): string[][] {
51-
const modifierNames = modifierNamesByFormat[format];
5256
const shortcuts = ShortcutRegistry.registry.getKeyCodesByShortcutName(action);
5357
// See ShortcutRegistry.createSerializedKey for the starting format.
5458
const named = shortcuts.map((shortcut) => {

0 commit comments

Comments
 (0)