Skip to content

Commit 8d36bc6

Browse files
committed
Add Alt+key shortcuts and visual indicators for edit format selection
1 parent 817af41 commit 8d36bc6

3 files changed

Lines changed: 109 additions & 23 deletions

File tree

packages/ui/src/components/editor/panel/PromptField/PromptField.module.scss

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -287,32 +287,35 @@
287287
font-size: 11px;
288288
pointer-events: all;
289289
position: relative;
290-
font-weight: 500;
291-
color: transparent;
290+
color: var(--vscode-foreground);
292291

293-
&::before {
294-
content: attr(data-text);
292+
&__spacer {
293+
visibility: hidden;
294+
font-weight: 500;
295+
white-space: nowrap;
296+
}
297+
298+
&__text {
295299
position: absolute;
296300
left: 0;
297301
top: 0;
298302
width: 100%;
299303
height: 100%;
304+
display: flex;
305+
align-items: center;
306+
justify-content: center;
300307
font-weight: normal;
301-
color: var(--vscode-foreground);
302308
opacity: 0.5;
309+
white-space: nowrap;
303310
}
304311

305-
&:hover {
306-
&::before {
307-
opacity: 1;
308-
}
312+
&:hover &__text {
313+
opacity: 1;
309314
}
310315

311-
&--selected {
312-
&::before {
313-
font-weight: 500;
314-
opacity: 1;
315-
}
316+
&--selected &__text {
317+
font-weight: 500;
318+
opacity: 1;
316319
}
317320
}
318321
}
@@ -425,3 +428,7 @@
425428
}
426429
}
427430
}
431+
432+
.underlined {
433+
text-decoration: underline;
434+
}

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

Lines changed: 87 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,54 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
8383
const [is_text_selecting, set_is_text_selecting] = useState(false)
8484
const [is_focused, set_is_focused] = useState(false)
8585

86+
const [is_alt_pressed, set_is_alt_pressed] = useState(false)
87+
88+
useEffect(() => {
89+
const handle_key_down = (e: KeyboardEvent) => {
90+
if (e.key == 'Alt') set_is_alt_pressed(true)
91+
92+
if (
93+
e.altKey &&
94+
!e.shiftKey &&
95+
!e.ctrlKey &&
96+
!e.metaKey &&
97+
props.show_edit_format_selector &&
98+
props.on_edit_format_change
99+
) {
100+
let format: EditFormat | undefined
101+
switch (e.code) {
102+
case 'KeyW':
103+
format = 'whole'
104+
break
105+
case 'KeyT':
106+
format = 'truncated'
107+
break
108+
case 'KeyD':
109+
format = 'diff'
110+
break
111+
}
112+
if (format) {
113+
e.preventDefault()
114+
props.on_edit_format_change(format)
115+
}
116+
}
117+
}
118+
const handle_key_up = (e: KeyboardEvent) => {
119+
if (e.key == 'Alt') set_is_alt_pressed(false)
120+
}
121+
const handle_blur = () => {
122+
set_is_alt_pressed(false)
123+
}
124+
window.addEventListener('keydown', handle_key_down)
125+
window.addEventListener('keyup', handle_key_up)
126+
window.addEventListener('blur', handle_blur)
127+
return () => {
128+
window.removeEventListener('keydown', handle_key_down)
129+
window.removeEventListener('keyup', handle_key_up)
130+
window.removeEventListener('blur', handle_blur)
131+
}
132+
}, [props.show_edit_format_selector, props.on_edit_format_change])
133+
86134
const is_narrow_viewport = use_is_narrow_viewport(268)
87135
const {
88136
is_dropdown_open,
@@ -146,10 +194,6 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
146194

147195
const is_mac = use_is_mac()
148196

149-
const display_text = useMemo(() => {
150-
return get_display_text(props.value, props.context_file_paths ?? [])
151-
}, [props.value, props.context_file_paths])
152-
153197
const highlighted_html = useMemo(() => {
154198
return get_highlighted_text({
155199
text: props.value,
@@ -260,6 +304,15 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
260304
const has_no_context =
261305
!props.context_file_paths || props.context_file_paths.length == 0
262306

307+
const edit_format_shortcuts: Record<EditFormat, string> = useMemo(() => {
308+
const modifier = is_mac ? '⌥' : 'Alt+'
309+
return {
310+
whole: `(${modifier}W)`,
311+
truncated: `(${modifier}T)`,
312+
diff: `(${modifier}D)`
313+
}
314+
}, [is_mac])
315+
263316
return (
264317
<div className={styles.container}>
265318
{props.has_active_selection && props.is_in_code_completions_mode && (
@@ -417,6 +470,9 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
417470
const button_text = is_narrow_viewport
418471
? format.charAt(0).toUpperCase()
419472
: format
473+
const is_selected = props.edit_format == format
474+
const should_underline = is_alt_pressed && !is_selected
475+
420476
return (
421477
<button
422478
key={format}
@@ -425,14 +481,37 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
425481
{
426482
[styles[
427483
'footer__right__edit-format__button--selected'
428-
]]: props.edit_format == format
484+
]]: is_selected
429485
}
430486
)}
431-
title={`${props.edit_format_instructions?.[format]}`}
487+
title={`Edit format instructions to include with the prompt ${edit_format_shortcuts[format]}\n\n${
488+
props.edit_format_instructions?.[format]
489+
}`}
432490
onClick={() => props.on_edit_format_change?.(format)}
433-
data-text={button_text}
434491
>
435-
{button_text}
492+
<span
493+
className={
494+
styles['footer__right__edit-format__button__spacer']
495+
}
496+
>
497+
{button_text}
498+
</span>
499+
<span
500+
className={
501+
styles['footer__right__edit-format__button__text']
502+
}
503+
>
504+
{should_underline ? (
505+
<>
506+
<span className={styles['underlined']}>
507+
{button_text.charAt(0)}
508+
</span>
509+
{button_text.substring(1)}
510+
</>
511+
) : (
512+
button_text
513+
)}
514+
</span>
436515
</button>
437516
)
438517
})}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ export const use_handlers = (
699699
}
700700

701701
const handle_key_down = (e: React.KeyboardEvent<HTMLDivElement>) => {
702-
const { key, shiftKey, altKey } = e
702+
const { key, shiftKey } = e
703703

704704
// Handle arrow-right when ghost text is present
705705
if (key === 'ArrowRight' && ghost_text && !shiftKey) {

0 commit comments

Comments
 (0)