Skip to content

Commit 0c76e3f

Browse files
committed
Refactor prompt history navigation to only enable when the input field is unmodified, preventing history from overwriting user edits
1 parent 3d08625 commit 0c76e3f

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ export const use_drag_drop = ({
7676
}
7777

7878
const handle_drop = (e: React.DragEvent<HTMLDivElement>) => {
79-
// Check for internal drag. If it is, handle it.
80-
// If not, let the browser handle it. It will fire 'onInput'.
8179
if (!dragged_text_range_ref.current) {
8280
return
8381
}

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ export const use_handlers = (
163163
const [undo_stack, set_undo_stack] = useState<HistoryEntry[]>([])
164164
const [redo_stack, set_redo_stack] = useState<HistoryEntry[]>([])
165165
const raw_caret_pos_ref = useRef(0)
166+
const has_modified_current_entry_ref = useRef(false)
166167

167168
useEffect(() => {
168169
const on_selection_change = () => {
@@ -178,7 +179,11 @@ export const use_handlers = (
178179

179180
const is_at_end =
180181
raw_pos == props.value.length && !!selection?.isCollapsed
181-
set_is_history_enabled(is_at_end)
182+
183+
// Only enable history if at end AND haven't modified the current entry
184+
const is_history_check_enabled =
185+
is_at_end && !has_modified_current_entry_ref.current
186+
set_is_history_enabled(is_history_check_enabled)
182187
}
183188
}
184189
document.addEventListener('selectionchange', on_selection_change)
@@ -189,6 +194,8 @@ export const use_handlers = (
189194

190195
const update_value = (new_value: string, caret_pos?: number) => {
191196
if (new_value === props.value) return
197+
// Mark that we've modified the current entry (not from history navigation)
198+
has_modified_current_entry_ref.current = true
192199
set_undo_stack((prev) => [
193200
...prev,
194201
{ value: props.value, raw_caret_pos: raw_caret_pos_ref.current }
@@ -367,7 +374,6 @@ export const use_handlers = (
367374
handle_keyword_deletion_by_click(keyword_element)
368375
}
369376
} else if (text_element) {
370-
// Clicking on file name text should open the file
371377
e.preventDefault()
372378
e.stopPropagation()
373379

@@ -381,7 +387,6 @@ export const use_handlers = (
381387
}
382388
}
383389

384-
// Keep focus on input
385390
if (input_ref.current) {
386391
input_ref.current.focus()
387392
}
@@ -390,6 +395,7 @@ export const use_handlers = (
390395

391396
const handle_clear = () => {
392397
update_value('')
398+
has_modified_current_entry_ref.current = false
393399
set_history_index(-1)
394400
}
395401

@@ -438,6 +444,7 @@ export const use_handlers = (
438444
}
439445

440446
update_value(new_raw_value)
447+
has_modified_current_entry_ref.current = true
441448
set_history_index(-1)
442449

443450
if (char_before_caret == '@') {
@@ -702,6 +709,8 @@ export const use_handlers = (
702709

703710
const update_and_set_caret = (value: string) => {
704711
update_value(value, value.length)
712+
// Reset the modification flag when navigating history
713+
has_modified_current_entry_ref.current = false
705714
}
706715

707716
if (e.key === 'ArrowUp') {
@@ -716,6 +725,7 @@ export const use_handlers = (
716725
set_history_index(new_index)
717726
update_and_set_caret(active_history[new_index])
718727
} else if (history_index === 0) {
728+
has_modified_current_entry_ref.current = false
719729
set_history_index(-1)
720730
update_and_set_caret('')
721731
}
@@ -893,6 +903,7 @@ export const use_handlers = (
893903
props.context_file_paths ?? []
894904
)
895905
set_history_index(-1)
906+
has_modified_current_entry_ref.current = true
896907
}
897908
return
898909
}
@@ -916,6 +927,7 @@ export const use_handlers = (
916927
props.context_file_paths ?? []
917928
)
918929
set_history_index(-1)
930+
has_modified_current_entry_ref.current = true
919931
}
920932
return
921933
}

0 commit comments

Comments
 (0)