Skip to content

Commit 0d9bf1a

Browse files
committed
Add keyboard shortcuts for tab navigation and management
- allow closing tab with the same keyboard shortcut as in google chrome e.g. cmd+w - allow moving between tabs left/right with the same keyboard shortcut as in google chrome - allow creating tabs with the same keyboard shortcut as in google chrome ├── apps │ └── editor │ └── src │ └── views │ └── prompt │ └── frontend │ ├── Main │ │ ├── Main.tsx │ │ └── MainView │ │ └── MainView.tsx │ ├── Prompt.tsx │ └── hooks │ └── use-prompt │ ├── hooks │ │ └── use-instructions.ts │ └── use-prompt.ts └── packages └── ui └── src └── components └── editor └── common └── prompts └── PromptField ├── PromptField.tsx ├── components │ └── Tabs │ └── Tabs.tsx └── hooks ├── use-handlers │ └── handle-input-click.ts └── use-keyboard-shortcuts.ts
1 parent 91af289 commit 0d9bf1a

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

‎packages/ui/src/components/editor/common/prompts/PromptField/hooks/use-keyboard-shortcuts.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,55 @@ export const use_keyboard_shortcuts = (props: PromptField.Props) => {
140140
) => {
141141
if (props.are_keyboard_shortcuts_disabled) return
142142

143+
const is_next_tab =
144+
(e.ctrlKey && e.key === 'Tab' && !e.shiftKey && !e.altKey && !e.metaKey) ||
145+
(e.ctrlKey && e.key === 'PageDown' && !e.shiftKey && !e.altKey && !e.metaKey) ||
146+
(e.metaKey && e.altKey && e.key === 'ArrowRight' && !e.shiftKey && !e.ctrlKey) ||
147+
(e.metaKey && e.shiftKey && (e.key === ']' || e.key === '}' || e.code === 'BracketRight') && !e.altKey && !e.ctrlKey)
148+
149+
const is_prev_tab =
150+
(e.ctrlKey && e.key === 'Tab' && e.shiftKey && !e.altKey && !e.metaKey) ||
151+
(e.ctrlKey && e.key === 'PageUp' && !e.shiftKey && !e.altKey && !e.metaKey) ||
152+
(e.metaKey && e.altKey && e.key === 'ArrowLeft' && !e.shiftKey && !e.ctrlKey) ||
153+
(e.metaKey && e.shiftKey && (e.key === '[' || e.key === '{' || e.code === 'BracketLeft') && !e.altKey && !e.ctrlKey)
154+
155+
if (is_next_tab) {
156+
e.preventDefault()
157+
e.stopPropagation()
158+
if (props.tabs_count > 1) {
159+
props.on_tab_change((props.active_tab_index + 1) % props.tabs_count)
160+
}
161+
return
162+
}
163+
164+
if (is_prev_tab) {
165+
e.preventDefault()
166+
e.stopPropagation()
167+
if (props.tabs_count > 1) {
168+
props.on_tab_change(
169+
(props.active_tab_index - 1 + props.tabs_count) % props.tabs_count
170+
)
171+
}
172+
return
173+
}
174+
143175
if ((e.ctrlKey || e.metaKey) && !e.shiftKey && !e.altKey) {
176+
if (e.key.toLowerCase() === 'w') {
177+
if (props.tabs_count > 1) {
178+
e.preventDefault()
179+
e.stopPropagation()
180+
props.on_tab_delete(props.active_tab_index)
181+
return
182+
}
183+
}
184+
185+
if (e.key.toLowerCase() === 't') {
186+
e.preventDefault()
187+
e.stopPropagation()
188+
props.on_new_tab()
189+
return
190+
}
191+
144192
const match = e.key.match(/^[1-9]$/)
145193
if (match) {
146194
const index = parseInt(match[0], 10) - 1

0 commit comments

Comments
 (0)