Skip to content

Commit fbbd9a7

Browse files
committed
Add Shift key detection to prevent auto-formatting pasted selections when Shift is held during paste operations
1 parent 4ff70e7 commit fbbd9a7

1 file changed

Lines changed: 38 additions & 3 deletions

File tree

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

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,28 @@ export const use_handlers = (
203203
const [redo_stack, set_redo_stack] = useState<HistoryEntry[]>([])
204204
const raw_caret_pos_ref = useRef(0)
205205
const has_modified_current_entry_ref = useRef(false)
206+
const is_shift_pressed_ref = useRef(false)
207+
208+
useEffect(() => {
209+
const handle_key_down = (e: KeyboardEvent) => {
210+
if (e.key == 'Shift') is_shift_pressed_ref.current = true
211+
}
212+
const handle_key_up = (e: KeyboardEvent) => {
213+
if (e.key == 'Shift') is_shift_pressed_ref.current = false
214+
}
215+
const handle_blur = () => {
216+
is_shift_pressed_ref.current = false
217+
}
218+
window.addEventListener('keydown', handle_key_down)
219+
window.addEventListener('keyup', handle_key_up)
220+
window.addEventListener('blur', handle_blur)
221+
222+
return () => {
223+
window.removeEventListener('keydown', handle_key_down)
224+
window.removeEventListener('keyup', handle_key_up)
225+
window.removeEventListener('blur', handle_blur)
226+
}
227+
}, [])
206228

207229
useEffect(() => {
208230
if (!props.value) {
@@ -457,9 +479,7 @@ export const use_handlers = (
457479
update_value(new_value, raw_start)
458480
}
459481

460-
const handle_paste = (e: React.ClipboardEvent<HTMLDivElement>) => {
461-
e.preventDefault()
462-
const text = e.clipboardData.getData('text/plain')
482+
const perform_paste = (text: string) => {
463483
const selection = window.getSelection()
464484
if (!selection || !selection.rangeCount || !input_ref.current) return
465485
const range = selection.getRangeAt(0)
@@ -490,6 +510,7 @@ export const use_handlers = (
490510
let caret_offset_adjustment = 0
491511

492512
if (
513+
!is_shift_pressed_ref.current &&
493514
props.current_selection &&
494515
text == props.current_selection.text &&
495516
props.currently_open_file_path
@@ -514,6 +535,12 @@ export const use_handlers = (
514535
update_value(new_value, new_caret_pos)
515536
}
516537

538+
const handle_paste = (e: React.ClipboardEvent<HTMLDivElement>) => {
539+
e.preventDefault()
540+
const text = e.clipboardData.getData('text/plain')
541+
perform_paste(text)
542+
}
543+
517544
const handle_input_click = (e: React.MouseEvent<HTMLDivElement>) => {
518545
const target = e.target as HTMLElement
519546
const icon_element = target.closest('[data-role="keyword-icon"]')
@@ -958,6 +985,14 @@ export const use_handlers = (
958985
}
959986
}
960987

988+
if ((e.ctrlKey || e.metaKey) && shiftKey && key.toLowerCase() == 'v') {
989+
e.preventDefault()
990+
navigator.clipboard.readText().then((text) => {
991+
perform_paste(text)
992+
})
993+
return
994+
}
995+
961996
if ((e.ctrlKey || e.metaKey) && !shiftKey) {
962997
if (key == 'ArrowLeft') {
963998
e.preventDefault()

0 commit comments

Comments
 (0)