Skip to content

Commit 1d64577

Browse files
committed
Refactor keyboard shortcut handling to correctly display ⌘ C for copying the prompt on macOS and standardize word-jump logic
1 parent 1281299 commit 1d64577

2 files changed

Lines changed: 20 additions & 10 deletions

File tree

‎packages/ui/src/components/editor/panel/PromptField/PromptField.tsx‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
8484
const [is_text_selecting, set_is_text_selecting] = useState(false)
8585
const [is_focused, set_is_focused] = useState(false)
8686
const [show_file_match_hint, set_show_file_match_hint] = useState(false)
87-
const file_match_hint_timer_ref = useRef<ReturnType<typeof setTimeout> | null>(null)
87+
const file_match_hint_timer_ref = useRef<ReturnType<
88+
typeof setTimeout
89+
> | null>(null)
8890
const has_shown_hint_once_ref = useRef(false)
8991
const last_matching_word_ref = useRef<string | null>(null)
9092

@@ -150,7 +152,6 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
150152
)
151153

152154
const is_mac = use_is_mac()
153-
const mod_key = is_mac ? '⌘' : 'Ctrl'
154155

155156
const display_text = useMemo(() => {
156157
return get_display_text(props.value, props.context_file_paths ?? [])
@@ -529,12 +530,12 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
529530
items={[
530531
{
531532
label: 'Select...',
532-
shortcut: is_mac ? `${mod_key} Return` : `${mod_key}+Enter`,
533+
shortcut: is_mac ? '⌘ Return' : 'Ctrl+Enter',
533534
on_click: handle_select_click
534535
},
535536
{
536537
label: 'Copy prompt',
537-
shortcut: is_mac ? `${mod_key}⌥C` : `${mod_key}+Alt+C`,
538+
shortcut: is_mac ? '⌘ C' : 'Ctrl+C',
538539
on_click: handle_copy_click
539540
}
540541
]}

‎packages/ui/src/components/editor/panel/PromptField/hooks/use-handlers.ts‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -766,11 +766,20 @@ export const use_handlers = (
766766
}
767767
}
768768

769+
if ((e.ctrlKey || e.metaKey) && !shiftKey && key.toLowerCase() == 'c') {
770+
const selection = window.getSelection()
771+
if (!selection || selection.rangeCount === 0 || selection.isCollapsed) {
772+
e.preventDefault()
773+
props.on_copy()
774+
return
775+
}
776+
}
777+
769778
if ((e.ctrlKey || e.metaKey) && !shiftKey) {
770779
if (key == 'ArrowLeft') {
771780
e.preventDefault()
772781
const raw_pos = raw_caret_pos_ref.current
773-
if (raw_pos === 0) return
782+
if (raw_pos == 0) return
774783

775784
const { value, context_file_paths = [] } = props
776785
let i = raw_pos - 1
@@ -804,7 +813,7 @@ export const use_handlers = (
804813
const is_word_char = /\w/.test(value[i])
805814
while (i >= 0) {
806815
const current_is_word = /\w/.test(value[i])
807-
if (/\s/.test(value[i]) || current_is_word !== is_word_char) {
816+
if (/\s/.test(value[i]) || current_is_word != is_word_char) {
808817
break
809818
}
810819
i--
@@ -825,7 +834,7 @@ export const use_handlers = (
825834
e.preventDefault()
826835
const { value, context_file_paths = [] } = props
827836
const raw_pos = raw_caret_pos_ref.current
828-
if (raw_pos === value.length) return
837+
if (raw_pos == value.length) return
829838

830839
let i = raw_pos
831840

@@ -867,7 +876,7 @@ export const use_handlers = (
867876
}
868877
}
869878

870-
if ((e.ctrlKey || e.metaKey) && !shiftKey && key.toLowerCase() === 'z') {
879+
if ((e.ctrlKey || e.metaKey) && !shiftKey && key.toLowerCase() == 'z') {
871880
e.preventDefault()
872881
if (undo_stack.length > 0) {
873882
const prev_entry = undo_stack[undo_stack.length - 1]
@@ -888,8 +897,8 @@ export const use_handlers = (
888897
return
889898
}
890899
if (
891-
((e.ctrlKey || e.metaKey) && key.toLowerCase() === 'y') ||
892-
((e.ctrlKey || e.metaKey) && shiftKey && key.toLowerCase() === 'z')
900+
((e.ctrlKey || e.metaKey) && key.toLowerCase() == 'y') ||
901+
((e.ctrlKey || e.metaKey) && shiftKey && key.toLowerCase() == 'z')
893902
) {
894903
e.preventDefault()
895904
if (redo_stack.length > 0) {

0 commit comments

Comments
 (0)