Skip to content

Commit 0de3628

Browse files
committed
Implement delete functionality for selected text in the prompt field using the Delete key
- user can select ALL text and tap 'backspace' key. In this scenario, the history for 'undo' is not updated correctly. Partial selection with 'backspace' works correctly.
1 parent dbe336f commit 0de3628

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

  • packages/ui/src/components/editor/panel/prompts/PromptField/hooks

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,46 @@ export const use_handlers = (
825825
return
826826
}
827827

828+
const selection = window.getSelection()
829+
if (
830+
selection &&
831+
selection.rangeCount > 0 &&
832+
!selection.isCollapsed &&
833+
params.input_ref.current?.contains(
834+
selection.getRangeAt(0).startContainer
835+
)
836+
) {
837+
e.preventDefault()
838+
const range = selection.getRangeAt(0)
839+
const input_element = params.input_ref.current
840+
841+
const pre_selection_range = document.createRange()
842+
pre_selection_range.selectNodeContents(input_element)
843+
pre_selection_range.setEnd(range.startContainer, range.startOffset)
844+
const display_start = pre_selection_range.toString().length
845+
846+
pre_selection_range.setEnd(range.endContainer, range.endOffset)
847+
const display_end = pre_selection_range.toString().length
848+
849+
const raw_start = map_display_pos_to_raw_pos({
850+
display_pos: display_start,
851+
raw_text: props.value,
852+
context_file_paths: props.context_file_paths ?? []
853+
})
854+
const raw_end = map_display_pos_to_raw_pos({
855+
display_pos: display_end,
856+
raw_text: props.value,
857+
context_file_paths: props.context_file_paths ?? []
858+
})
859+
860+
const new_value =
861+
props.value.substring(0, raw_start) + props.value.substring(raw_end)
862+
863+
has_modified_current_entry_ref.current = true
864+
update_value(new_value, raw_start)
865+
return
866+
}
867+
828868
if (e.ctrlKey || e.metaKey) {
829869
e.preventDefault()
830870
const raw_pos = raw_caret_pos_ref.current

0 commit comments

Comments
 (0)