Skip to content

Commit 54c4173

Browse files
committed
Prevent unintended text input in prompt field while Left Alt is pressed
- block passing values into the prompt field when left alt is pressed - recent changes: [Changes], now when we press alt+i shortcut, the caret moves to the beginning of the prompt field └── packages └── ui └── src └── components └── editor └── common └── prompts ├── PromptField │ ├── PromptField.module.scss │ ├── PromptField.stories.tsx │ ├── PromptField.tsx │ ├── components │ │ ├── CompletedTasks │ │ │ ├── CompletedTasks.module.scss │ │ │ ├── CompletedTasks.tsx │ │ │ └── index.ts │ │ ├── Footer │ │ │ ├── Footer.module.scss │ │ │ ├── Footer.tsx │ │ │ └── index.ts │ │ └── Tabs │ │ ├── Tabs.module.scss │ │ ├── Tabs.tsx │ │ └── index.ts │ ├── hooks │ │ ├── use-drag-drop.ts │ │ ├── use-dropdown.ts │ │ ├── use-ghost-text.ts │ │ ├── use-handlers │ │ │ ├── handle-copy.ts │ │ │ ├── handle-cut.ts │ │ │ ├── handle-input-change.ts │ │ │ ├── handle-input-click.ts │ │ │ ├── handle-key-down.ts │ │ │ ├── handle-paste.ts │ │ │ ├── handle-submit.ts │ │ │ ├── index.ts │ │ │ ├── types.ts │ │ │ ├── use-handlers.ts │ │ │ └── utils.ts │ │ └── use-keyboard-shortcuts.ts │ └── index.ts ├── Template │ ├── Template.module.scss │ ├── Template.tsx │ └── index.ts └── shared ├── hooks │ └── use-undo-redo.ts └── symbols ├── caret.ts ├── get-highlighted-text.tsx ├── index.ts ├── position-mapping.ts ├── prompt-symbols.module.scss ├── reconstruct-raw-value.ts └── use-symbol-deletion.ts
1 parent f3fd81c commit 54c4173

3 files changed

Lines changed: 36 additions & 4 deletions

File tree

‎apps/editor/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@
436436
"submenus": [
437437
{
438438
"id": "codeWebChat.editorSubmenu",
439-
"label": "CodeWebChat"
439+
"label": "Code Web Chat"
440440
}
441441
],
442442
"menus": {

‎packages/ui/src/components/editor/common/prompts/PromptField/PromptField.tsx‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ export const PromptField: React.FC<PromptField.Props> = (props) => {
164164
!!(props.selected_files && props.selected_files.length > 0) ||
165165
props.completed_tasks.length > 0
166166

167-
const { is_alt_pressed, handle_container_key_down } =
167+
const { is_alt_pressed, is_left_alt_pressed_ref, handle_container_key_down } =
168168
use_keyboard_shortcuts(props)
169169

170170
const {
@@ -422,6 +422,19 @@ export const PromptField: React.FC<PromptField.Props> = (props) => {
422422
contentEditable={true}
423423
suppressContentEditableWarning={true}
424424
onInput={(e) => {
425+
if (is_left_alt_pressed_ref.current) {
426+
if (
427+
input_ref.current &&
428+
input_ref.current.innerHTML !== highlighted_html
429+
) {
430+
input_ref.current.innerHTML = highlighted_html
431+
set_caret_position_for_div(
432+
input_ref.current,
433+
caret_position
434+
)
435+
}
436+
return
437+
}
425438
set_should_show_ghost_text(true)
426439
handle_input_change(e)
427440
}}

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77

88
export const use_keyboard_shortcuts = (props: PromptField.Props) => {
99
const [is_alt_pressed, set_is_alt_pressed] = useState(false)
10+
const is_left_alt_pressed_ref = useRef(false)
1011
const pressed_keys_ref = useRef(new Set<string>())
1112
const is_alt_pressed_raw_ref = useRef(false)
1213
const is_alt_consumed_ref = useRef(false)
@@ -45,6 +46,12 @@ export const use_keyboard_shortcuts = (props: PromptField.Props) => {
4546

4647
useEffect(() => {
4748
const handle_key_down = (e: KeyboardEvent) => {
49+
if (e.code === 'AltLeft') {
50+
is_left_alt_pressed_ref.current = true
51+
} else if (!e.altKey) {
52+
is_left_alt_pressed_ref.current = false
53+
}
54+
4855
if (props.are_keyboard_shortcuts_disabled) return
4956

5057
pressed_keys_ref.current.add(e.code)
@@ -142,6 +149,10 @@ export const use_keyboard_shortcuts = (props: PromptField.Props) => {
142149
}
143150

144151
const handle_key_up = (e: KeyboardEvent) => {
152+
if (e.code === 'AltLeft' || !e.altKey) {
153+
is_left_alt_pressed_ref.current = false
154+
}
155+
145156
pressed_keys_ref.current.delete(e.code)
146157
if (e.code === 'AltLeft') {
147158
is_alt_consumed_ref.current = false
@@ -157,6 +168,7 @@ export const use_keyboard_shortcuts = (props: PromptField.Props) => {
157168
}
158169

159170
const handle_blur = () => {
171+
is_left_alt_pressed_ref.current = false
160172
pressed_keys_ref.current.clear()
161173
is_alt_consumed_ref.current = false
162174
update_alt_pressed(false)
@@ -172,8 +184,11 @@ export const use_keyboard_shortcuts = (props: PromptField.Props) => {
172184
}
173185
}, [
174186
props.show_edit_format_selector,
175-
props.on_edit_format_change,
176187
props.edit_format,
188+
props.show_terminal_interactivity_selector,
189+
props.is_terminal_interactivity_disabled,
190+
props.terminal_interactivity,
191+
props.targets_order,
177192
props.are_keyboard_shortcuts_disabled
178193
])
179194

@@ -342,5 +357,9 @@ export const use_keyboard_shortcuts = (props: PromptField.Props) => {
342357
}
343358
}
344359

345-
return { is_alt_pressed, handle_container_key_down }
360+
return {
361+
is_alt_pressed,
362+
is_left_alt_pressed_ref,
363+
handle_container_key_down
364+
}
346365
}

0 commit comments

Comments
 (0)