Skip to content

Commit a728d3f

Browse files
committed
Fix caret position restoration logic and remove redundant prompt field focus calls
1 parent 6d5b2f4 commit a728d3f

2 files changed

Lines changed: 15 additions & 25 deletions

File tree

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

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

171171
useEffect(() => {
172172
if (input_ref.current && input_ref.current.innerHTML !== highlighted_html) {
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-
173+
const selection = window.getSelection()
174+
const should_restore_caret =
175+
document.activeElement === input_ref.current &&
176+
selection &&
177+
selection.rangeCount > 0 &&
178+
selection.anchorNode &&
179+
input_ref.current.contains(selection.anchorNode)
180+
181+
const selection_start = should_restore_caret
182+
? get_caret_position_from_div(input_ref.current)
183+
: 0
185184
input_ref.current.innerHTML = highlighted_html
186-
set_caret_position_for_div(input_ref.current, selection_start)
185+
if (should_restore_caret) {
186+
set_caret_position_for_div(input_ref.current, selection_start)
187+
}
187188
}
188-
}, [highlighted_html, is_focused, props.value, props.context_file_paths])
189+
}, [highlighted_html])
189190

190191
useEffect(() => {
191192
if (input_ref.current) {
@@ -228,14 +229,6 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
228229
}
229230
}, [props.focus_and_select_key])
230231

231-
useEffect(() => {
232-
const input_element = input_ref.current
233-
if (input_element) {
234-
const is_focused = document.activeElement === input_element
235-
if (!is_focused) input_element.focus()
236-
}
237-
}, [props.value])
238-
239232
useEffect(() => {
240233
const on_selection_change = () => {
241234
if (document.activeElement === input_ref.current && input_ref.current) {

‎packages/vscode/src/views/panel/backend/message-handlers/handle-show-prompt-template-quick-pick.ts‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,6 @@ export const handle_show_prompt_template_quick_pick = async (
352352
}
353353

354354
await set_instructions(prompt_text)
355-
panel_provider.send_message({
356-
command: 'FOCUS_PROMPT_FIELD'
357-
})
358355
}
359356
}),
360357
templates_quick_pick.onDidChangeValue((value) => {

0 commit comments

Comments
 (0)