Skip to content

Commit 159daf9

Browse files
fix: retain the short format for menus
1 parent 4568258 commit 159daf9

File tree

4 files changed

+35
-15
lines changed

4 files changed

+35
-15
lines changed

src/actions/clipboard.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export class Clipboard {
101101
*/
102102
private registerCutContextMenuAction() {
103103
const cutAction: ContextMenuRegistry.RegistryItem = {
104-
displayText: (scope) => `Cut (${formatActionShortcut('cut')})`,
104+
displayText: (scope) => `Cut (${formatActionShortcut('cut', 'short')})`,
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')})`,
199+
displayText: (scope) => `Copy (${formatActionShortcut('copy', 'short')})`,
200200
preconditionFn: (scope) => {
201201
const ws = scope.block?.workspace;
202202
if (!ws) return 'hidden';
@@ -305,7 +305,8 @@ export class Clipboard {
305305
*/
306306
private registerPasteContextMenuAction() {
307307
const pasteAction: ContextMenuRegistry.RegistryItem = {
308-
displayText: (scope) => `Paste (${formatActionShortcut('paste')})`,
308+
displayText: (scope) =>
309+
`Paste (${formatActionShortcut('paste', 'short')})`,
309310
preconditionFn: (scope: ScopeWithConnection) => {
310311
const block = scope.block ?? scope.connection?.getSourceBlock();
311312
const ws = block?.workspace as WorkspaceSvg | null;

src/actions/enter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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');
107+
const shortcut = formatActionShortcut('list_shortcuts', 'short');
108108
const message = `Press ${shortcut} for help on keyboard controls`;
109109
dialog.alert(message);
110110
}

src/shortcut_dialog.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,16 @@ export class ShortcutDialog {
139139
}
140140

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

146146
private actionShortcutToHTML(keys: string[]) {
147+
const separator = navigator.platform.startsWith('Mac') ? '' : ' + ';
147148
return [
148149
`<span class="shortcut-combo">`,
149150
...keys.map((key, index) => {
150-
return `<span class="key">${key}</span>${index < keys.length - 1 ? ' + ' : ''}`;
151+
return `<span class="key">${key}</span>${index < keys.length - 1 ? separator : ''}`;
151152
}),
152153
`</span>`,
153154
].join('');
@@ -298,7 +299,7 @@ Blockly.Css.register(`
298299
border: 1px solid var(--key-border-color);
299300
border-radius: 8px;
300301
display: inline-block;
301-
margin: 0 8px;
302+
margin: 0 4px;
302303
min-width: 2em;
303304
padding: .3em .5em;
304305
text-align: center;

src/shortcut_formatting.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,32 @@ const isMacPlatform = navigator.platform.startsWith('Mac');
77
* Format the primary shortcut for this platform in a user facing format.
88
*
99
* @param action The action name, e.g. "cut".
10+
* @param format The key format.
1011
* @returns The formatted shortcut.
1112
*/
12-
export function formatActionShortcut(action: string): string {
13-
const parts = actionShortcutsForPlatform(action)[0];
14-
return parts.join(' + ');
13+
export function formatActionShortcut(
14+
action: string,
15+
format: ShortcutFormat,
16+
): string {
17+
const parts = actionShortcutsForPlatform(action, format)[0];
18+
return parts.join(isMacPlatform ? ' ' : ' + ');
1519
}
1620

17-
const modifierNames: Record<string, string> = {
18-
'Control': 'Ctrl',
19-
'Meta': '⌘ Command',
20-
'Alt': isMacPlatform ? '⌥ Option' : 'Alt',
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+
},
2132
};
2233

34+
export type ShortcutFormat = 'long' | 'short';
35+
2336
/**
2437
* Find the relevant shortcuts for the given action for the current platform.
2538
* Keys are returned in a user facing format.
@@ -28,9 +41,14 @@ const modifierNames: Record<string, string> = {
2841
* current platform or tagged them with a platform.
2942
*
3043
* @param action The action name, e.g. "cut".
44+
* @param format The key format.
3145
* @returns The formatted shortcuts.
3246
*/
33-
export function actionShortcutsForPlatform(action: string): string[][] {
47+
export function actionShortcutsForPlatform(
48+
action: string,
49+
format: ShortcutFormat,
50+
): string[][] {
51+
const modifierNames = modifierNamesByFormat[format];
3452
const shortcuts = ShortcutRegistry.registry.getKeyCodesByShortcutName(action);
3553
// See ShortcutRegistry.createSerializedKey for the starting format.
3654
const named = shortcuts.map((shortcut) => {

0 commit comments

Comments
 (0)