Skip to content

Added a keyboard shortcut for adding files #2395

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
merged 18 commits into from
Aug 2, 2024
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
3 changes: 3 additions & 0 deletions client/common/useKeyDownHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export default function useKeyDownHandlers(keyHandlers) {
/^\d+$/.test(e.code.at(-1)) ? e.code.at(-1) : e.key.toLowerCase()
}`
]?.(e);
} else if (isCtrl && e.altKey && e.code === 'KeyN') {
// specifically for creating a new file
handlers.current[`ctrl-alt-n`]?.(e);
Comment on lines +41 to +43
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would stay away from e.code and prefer e.key. It only makes a difference if the user has a non-standard keyboard layout. e.code is basically "the key in the 'N' position" whereas e.key is "the key that types 'N'".
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key

I guess I only built this hook to support ctrl- and ctrl-shift- and didn't do anything with alt. But we want it to support any ctrl-alt-LETTER and not just ctrl-alt-n. This should do it:

} else if (isCtrl && e.altKey) {
   handlers.current[`ctrl-alt-${e.key.toLowerCase()}`]?.(e);

} else if (isCtrl) {
handlers.current[`ctrl-${e.key.toLowerCase()}`]?.(e);
}
Expand Down
3 changes: 3 additions & 0 deletions client/modules/IDE/components/Header/Nav.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ const ProjectMenu = () => {

const replaceCommand =
metaKey === 'Ctrl' ? `${metaKeyName}+H` : `${metaKeyName}+⌥+F`;
const newFileCommand =
metaKey === 'Ctrl' ? `${metaKeyName}+Alt+N` : `${metaKeyName}+⌥+N`;

return (
<ul className="nav__items-left" role="menubar">
Expand Down Expand Up @@ -220,6 +222,7 @@ const ProjectMenu = () => {
<NavDropdownMenu id="sketch" title={t('Nav.Sketch.Title')}>
<NavMenuItem onClick={() => dispatch(newFile(rootFile.id))}>
{t('Nav.Sketch.AddFile')}
<span className="nav__keyboard-shortcut">{newFileCommand}</span>
</NavMenuItem>
<NavMenuItem onClick={() => dispatch(newFolder(rootFile.id))}>
{t('Nav.Sketch.AddFolder')}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,11 @@ exports[`Nav renders editor version for desktop 1`] = `
role="menuitem"
>
Add File
<span
class="nav__keyboard-shortcut"
>
Ctrl+Alt+N
</span>
</button>
</li>
<li
Expand Down
8 changes: 7 additions & 1 deletion client/modules/IDE/components/IDEKeyHandlers.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
expandSidebar,
showErrorModal,
startSketch,
stopSketch
stopSketch,
newFile
} from '../actions/ide';
import { setAllAccessibleOutput } from '../actions/preferences';
import { cloneProject, saveProject } from '../actions/project';
Expand Down Expand Up @@ -72,6 +73,11 @@ export const useIDEKeyHandlers = ({ getContent }) => {
sidebarIsExpanded ? collapseSidebar() : expandSidebar()
);
},
'ctrl-alt-n': (e) => {
e.preventDefault();
e.stopPropagation();
dispatch(newFile());
},
'ctrl-`': (e) => {
e.preventDefault();
dispatch(consoleIsExpanded ? collapseConsole() : expandConsole());
Expand Down
6 changes: 6 additions & 0 deletions client/modules/IDE/components/KeyboardShortcutModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ function KeyboardShortcutModal() {
const { t } = useTranslation();
const replaceCommand =
metaKey === 'Ctrl' ? `${metaKeyName} + H` : `${metaKeyName} + ⌥ + F`;
const newFileCommand =
metaKey === 'Ctrl' ? `${metaKeyName} + Alt + N` : `${metaKeyName} + ⌥ + N`;
return (
<div className="keyboard-shortcuts">
<h3 className="keyboard-shortcuts__title">
Expand Down Expand Up @@ -69,6 +71,10 @@ function KeyboardShortcutModal() {
<span className="keyboard-shortcut__command">{metaKeyName} + K</span>
<span>{t('KeyboardShortcuts.CodeEditing.ColorPicker')}</span>
</li>
<li className="keyboard-shortcut-item">
<span className="keyboard-shortcut__command">{newFileCommand}</span>
<span>{t('KeyboardShortcuts.CodeEditing.CreateNewFile')}</span>
</li>
</ul>
<h3 className="keyboard-shortcuts__title">General</h3>
<ul className="keyboard-shortcuts__list">
Expand Down
3 changes: 2 additions & 1 deletion translations/locales/en-US/translations.json
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@
"FindNextTextMatch": "Find Next Text Match",
"FindPreviousTextMatch": "Find Previous Text Match",
"CodeEditing": "Code Editing",
"ColorPicker": "Show Inline Color Picker"
"ColorPicker": "Show Inline Color Picker",
"CreateNewFile": "Create New File"
},
"General": {
"StartSketch": "Start Sketch",
Expand Down
Loading