Skip to content

Commit 43ac5b3

Browse files
committed
Fix caret position handling in PromptField to properly maintain selection when unfocused and adjust keyword deletion spacing logic
1 parent f7f3323 commit 43ac5b3

3 files changed

Lines changed: 16 additions & 13 deletions

File tree

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,22 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
170170

171171
useEffect(() => {
172172
if (input_ref.current && input_ref.current.innerHTML !== highlighted_html) {
173-
const selection_start = get_caret_position_from_div(input_ref.current)
173+
let selection_start = 0
174+
175+
if (is_focused) {
176+
selection_start = get_caret_position_from_div(input_ref.current)
177+
} else {
178+
selection_start = map_raw_pos_to_display_pos(
179+
props.value.length,
180+
props.value,
181+
props.context_file_paths ?? []
182+
)
183+
}
184+
174185
input_ref.current.innerHTML = highlighted_html
175186
set_caret_position_for_div(input_ref.current, selection_start)
176187
}
177-
}, [highlighted_html])
188+
}, [highlighted_html, is_focused, props.value, props.context_file_paths])
178189

179190
useEffect(() => {
180191
if (input_ref.current) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,10 @@ export const use_handlers = (
227227
let leading_part = props.value.substring(0, start_pos)
228228
let trailing_part = props.value.substring(end_pos)
229229

230-
if (leading_part.endsWith(' ')) {
231-
leading_part = leading_part.slice(0, -1)
232-
} else if (trailing_part.startsWith(' ')) {
230+
if (trailing_part.startsWith(' ')) {
233231
trailing_part = trailing_part.substring(1)
232+
} else if (leading_part.endsWith(' ')) {
233+
leading_part = leading_part.slice(0, -1)
234234
}
235235

236236
const new_value = leading_part + trailing_part

‎packages/vscode/src/views/panel/frontend/hooks/use-panel.ts‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -174,16 +174,8 @@ export const use_panel = (vscode: any) => {
174174

175175
const handle_task_forward = (text: string) => {
176176
handle_instructions_change(text, 'edit-context')
177-
178-
if (mode == MODE.WEB) {
179-
handle_web_prompt_type_change('edit-context')
180-
} else {
181-
handle_api_prompt_type_change('edit-context')
182-
}
183-
184177
set_active_view('main')
185178
set_main_view_scroll_reset_key((k) => k + 1)
186-
set_chat_input_focus_and_select_key((k) => k + 1)
187179
}
188180

189181
useEffect(() => {

0 commit comments

Comments
 (0)