-
Notifications
You must be signed in to change notification settings - Fork 13
Share shortcut formatting between help/menu/messages #408
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
Merged
maribethb
merged 3 commits into
RaspberryPiFoundation:main
from
microbit-matt-hillsdon:format-shortcuts2
Apr 14, 2025
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import {ShortcutRegistry} from 'blockly'; | ||
| import {keyNames} from './keynames'; | ||
|
|
||
| const isMacPlatform = navigator.platform.startsWith('Mac'); | ||
|
|
||
| /** | ||
| * Format the primary shortcut for this platform in a user facing format. | ||
| * | ||
| * @param action The action name, e.g. "cut". | ||
| * @param format The key format. | ||
| * @returns The formatted shortcut. | ||
| */ | ||
| export function formatActionShortcut( | ||
| action: string, | ||
| format: ShortcutFormat, | ||
| ): string { | ||
| const parts = actionShortcutsForPlatform(action, format)[0]; | ||
| return parts.join(isMacPlatform ? ' ' : ' + '); | ||
| } | ||
|
|
||
| const modifierNamesByFormat: Record<ShortcutFormat, Record<string, string>> = { | ||
| long: { | ||
| 'Control': 'Ctrl', | ||
| 'Meta': '⌘ Command', | ||
| 'Alt': isMacPlatform ? '⌥ Option' : 'Alt', | ||
| }, | ||
| short: { | ||
| 'Control': 'Ctrl', | ||
| 'Meta': '⌘', | ||
| 'Alt': isMacPlatform ? '⌥' : 'Alt', | ||
| }, | ||
| }; | ||
|
|
||
| export type ShortcutFormat = 'long' | 'short'; | ||
|
|
||
| /** | ||
| * Find the relevant shortcuts for the given action for the current platform. | ||
| * Keys are returned in a user facing format. | ||
| * | ||
| * This could be considerably simpler if we only bound shortcuts relevant to the | ||
| * current platform or tagged them with a platform. | ||
| * | ||
| * @param action The action name, e.g. "cut". | ||
| * @param format The key format. | ||
| * @returns The formatted shortcuts. | ||
| */ | ||
| export function actionShortcutsForPlatform( | ||
| action: string, | ||
| format: ShortcutFormat, | ||
| ): string[][] { | ||
| const modifierNames = modifierNamesByFormat[format]; | ||
| const shortcuts = ShortcutRegistry.registry.getKeyCodesByShortcutName(action); | ||
| // See ShortcutRegistry.createSerializedKey for the starting format. | ||
| const named = shortcuts.map((shortcut) => { | ||
| return shortcut | ||
| .split('+') | ||
| .map((maybeNumeric) => keyNames[maybeNumeric] ?? maybeNumeric) | ||
| .map((k) => upperCaseFirst(modifierNames[k] ?? k)); | ||
| }); | ||
|
|
||
| const command = modifierNames['Meta']; | ||
| const option = modifierNames['Alt']; | ||
| const control = modifierNames['Control']; | ||
| // Needed to prefer Command to Option where we've bound Alt. | ||
| named.sort((a, b) => { | ||
| const aValue = a.includes(command) ? 1 : 0; | ||
| const bValue = b.includes(command) ? 1 : 0; | ||
| return bValue - aValue; | ||
| }); | ||
| let currentPlatform = named.filter((shortcut) => { | ||
| const isMacShortcut = | ||
| shortcut.includes(command) || shortcut.includes(option); | ||
| return isMacShortcut === isMacPlatform; | ||
| }); | ||
| currentPlatform = currentPlatform.length === 0 ? named : currentPlatform; | ||
|
|
||
| // If there are modifiers return only one shortcut on the assumption they are | ||
| // intended for different platforms. Otherwise assume they are alternatives. | ||
| const hasModifiers = currentPlatform.some((shortcut) => | ||
| shortcut.some( | ||
| (key) => command === key || option === key || control === key, | ||
| ), | ||
| ); | ||
| return hasModifiers ? [currentPlatform[0]] : currentPlatform; | ||
| } | ||
|
|
||
| /** | ||
| * Convert the first character to uppercase. | ||
| * | ||
| * @param str String. | ||
| * @returns The string in title case. | ||
| */ | ||
| export function upperCaseFirst(str: string) { | ||
| return str.charAt(0).toUpperCase() + str.substring(1); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
generally I'd prefer having a
getShortActionShortcutandgetLongActionShortcutinstead of making the user pass a magic string, if there's only two options.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or potentially having one method with a boolean to switch to short mode or something like that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Slightly complicated by there being short vs long but also individual keys vs joined string. Gone with API that looks nice for the callers in 6f692d4 as in fact we only use short + joined string (for context menu) and long + individual keys (for help dialog).