Skip to content

Commit 0eb0a83

Browse files
committed
Implement drag-and-drop functionality for prompt text and context keywords, and refactor keyword handling logic into use-handlers and use-drag-drop hooks
1 parent 82008f5 commit 0eb0a83

6 files changed

Lines changed: 482 additions & 233 deletions

File tree

‎packages/ui/src/components/editor/panel/PromptField/PromptField.module.scss‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
opacity: 0.3;
2626
}
2727
}
28+
29+
&--selecting {
30+
.keyword {
31+
pointer-events: none;
32+
}
33+
}
2834
}
2935
}
3036

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

Lines changed: 47 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { useRef, useEffect, useMemo, useState } from 'react'
1+
import { useRef, useEffect, useMemo, useState, useCallback } from 'react'
22
import styles from './PromptField.module.scss'
33
import cn from 'classnames'
44
import { Icon } from '../../common/Icon'
55
import { get_highlighted_text } from './utils/get-highlighted-text'
66
import { use_handlers } from './hooks/use-handlers'
77
import { use_dropdown } from './hooks/use-dropdown'
8+
import { use_drag_drop } from './hooks/use-drag-drop'
89
import { DropdownMenu } from '../../common/DropdownMenu'
910
import { use_is_narrow_viewport } from '@shared/hooks'
1011
import { search_paths } from '@shared/utils/search-paths'
@@ -79,6 +80,7 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
7980
const [caret_position, set_caret_position] = useState(0)
8081
const [show_at_sign_tooltip, set_show_at_sign_tooltip] = useState(false)
8182
const [show_submit_tooltip, set_show_submit_tooltip] = useState(false)
83+
const [is_text_selecting, set_is_text_selecting] = useState(false)
8284
const is_narrow_viewport = use_is_narrow_viewport(268)
8385
const {
8486
is_dropdown_open,
@@ -92,8 +94,43 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
9294
handle_input_change,
9395
handle_submit,
9496
handle_key_down,
95-
is_history_enabled
96-
} = use_handlers(props, input_ref) // No change to signature, logic moved inside
97+
is_history_enabled,
98+
handle_copy,
99+
handle_input_click
100+
} = use_handlers(props, input_ref)
101+
const { handle_drag_start, handle_drag_over, handle_drop, handle_drag_end } =
102+
use_drag_drop({
103+
input_ref,
104+
value: props.value,
105+
context_file_paths: props.context_file_paths,
106+
on_change: props.on_change
107+
})
108+
109+
const mouse_down_pos_ref = useRef<{ x: number; y: number } | null>(null)
110+
111+
const handle_mouse_down = useCallback((e: React.MouseEvent<HTMLDivElement>) => {
112+
mouse_down_pos_ref.current = { x: e.clientX, y: e.clientY }
113+
114+
const handle_mouse_move = (move_e: MouseEvent) => {
115+
if (mouse_down_pos_ref.current) {
116+
const dx = Math.abs(move_e.clientX - mouse_down_pos_ref.current.x)
117+
const dy = Math.abs(move_e.clientY - mouse_down_pos_ref.current.y)
118+
if (dx >= 1 || dy >= 1) {
119+
set_is_text_selecting(true)
120+
}
121+
}
122+
}
123+
124+
const handle_mouse_up = () => {
125+
set_is_text_selecting(false)
126+
mouse_down_pos_ref.current = null
127+
document.removeEventListener('mousemove', handle_mouse_move)
128+
document.removeEventListener('mouseup', handle_mouse_up)
129+
}
130+
131+
document.addEventListener('mousemove', handle_mouse_move)
132+
document.addEventListener('mouseup', handle_mouse_up)
133+
}, [])
97134

98135
const is_mac = useMemo(() => {
99136
if (typeof navigator == 'undefined') return false
@@ -264,174 +301,6 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
264301
is_history_enabled
265302
])
266303

267-
const apply_deletion = (start_pos: number, end_pos: number) => {
268-
let leading_part = props.value.substring(0, start_pos)
269-
let trailing_part = props.value.substring(end_pos)
270-
271-
// Handle spacing
272-
if (leading_part.endsWith(' ')) {
273-
leading_part = leading_part.slice(0, -1)
274-
} else if (trailing_part.startsWith(' ')) {
275-
trailing_part = trailing_part.substring(1)
276-
}
277-
278-
const new_value = leading_part + trailing_part
279-
const new_raw_cursor_pos = leading_part.length
280-
props.on_change(new_value)
281-
282-
setTimeout(() => {
283-
if (input_ref.current) {
284-
const display_pos = map_raw_pos_to_display_pos(
285-
new_raw_cursor_pos,
286-
new_value,
287-
props.context_file_paths ?? []
288-
)
289-
set_caret_position_for_div(input_ref.current, display_pos)
290-
}
291-
}, 0)
292-
}
293-
294-
const handle_keyword_deletion = (clicked_element: HTMLElement) => {
295-
const input_element = input_ref.current
296-
if (!input_element) return
297-
298-
// Find the keyword element
299-
const keyword_element = clicked_element.closest(
300-
`.${styles['keyword']}`
301-
) as HTMLElement
302-
303-
if (!keyword_element) return
304-
305-
// Check if it's a file keyword
306-
if (keyword_element.classList.contains(styles['keyword--file'])) {
307-
const file_path = keyword_element.getAttribute('title')
308-
if (!file_path || !props.context_file_paths?.includes(file_path)) return
309-
310-
// Find this file path in the raw value
311-
const search_pattern = `\`${file_path}\``
312-
const start_index = props.value.indexOf(search_pattern)
313-
314-
if (start_index !== -1) {
315-
apply_deletion(start_index, start_index + search_pattern.length)
316-
}
317-
}
318-
// Check if it's a changes keyword
319-
else if (keyword_element.classList.contains(styles['keyword--changes'])) {
320-
const branch_name = keyword_element.dataset.branchName
321-
if (!branch_name) return
322-
323-
const search_pattern = `#Changes:${branch_name}`
324-
const start_index = props.value.indexOf(search_pattern)
325-
326-
if (start_index !== -1) {
327-
apply_deletion(start_index, start_index + search_pattern.length)
328-
}
329-
}
330-
// Check if it's a saved context keyword
331-
else if (
332-
keyword_element.classList.contains(styles['keyword--saved-context'])
333-
) {
334-
const context_type = keyword_element.dataset.contextType
335-
const context_name = keyword_element.dataset.contextName
336-
if (!context_type || !context_name) return
337-
338-
const search_pattern = `#SavedContext:${context_type} "${context_name}"`
339-
const start_index = props.value.indexOf(search_pattern)
340-
341-
if (start_index !== -1) {
342-
apply_deletion(start_index, start_index + search_pattern.length)
343-
}
344-
}
345-
// Check if it's a selection keyword
346-
else if (keyword_element.classList.contains(styles['keyword--selection'])) {
347-
const search_pattern = '#Selection'
348-
const start_index = props.value.indexOf(search_pattern)
349-
350-
if (start_index !== -1) {
351-
apply_deletion(start_index, start_index + search_pattern.length)
352-
}
353-
}
354-
// Check if it's a commit keyword
355-
else if (keyword_element.classList.contains(styles['keyword--commit'])) {
356-
const repo_name = keyword_element.dataset.repoName
357-
const commit_hash = keyword_element.dataset.commitHash
358-
const commit_message = keyword_element.dataset.commitMessage
359-
if (!repo_name || !commit_hash || commit_message === undefined) return
360-
361-
const search_pattern = `#Commit:${repo_name}:${commit_hash} "${commit_message}"`
362-
const start_index = props.value.indexOf(search_pattern)
363-
364-
if (start_index !== -1) {
365-
apply_deletion(start_index, start_index + search_pattern.length)
366-
}
367-
}
368-
}
369-
370-
const handle_input_click = (e: React.MouseEvent<HTMLDivElement>) => {
371-
const target = e.target as HTMLElement
372-
const icon_element = target.closest(`.${styles['keyword__icon']}`)
373-
const text_element = target.closest(`.${styles['keyword__text']}`)
374-
375-
if (icon_element) {
376-
e.preventDefault()
377-
e.stopPropagation()
378-
handle_keyword_deletion(icon_element as HTMLElement)
379-
} else if (text_element) {
380-
// Clicking on file name text should open the file
381-
e.preventDefault()
382-
e.stopPropagation()
383-
384-
const file_keyword_element = text_element.closest<HTMLElement>(
385-
`.${styles['keyword--file']}`
386-
)
387-
if (file_keyword_element) {
388-
const file_path = file_keyword_element.getAttribute('title')
389-
if (file_path && props.on_go_to_file) {
390-
props.on_go_to_file(file_path)
391-
}
392-
}
393-
394-
// Keep focus on input
395-
if (input_ref.current) {
396-
input_ref.current.focus()
397-
}
398-
}
399-
}
400-
401-
const handle_copy = (e: React.ClipboardEvent<HTMLDivElement>) => {
402-
const selection = window.getSelection()
403-
if (!selection || selection.rangeCount === 0 || selection.isCollapsed)
404-
return
405-
406-
const range = selection.getRangeAt(0)
407-
const input_element = input_ref.current
408-
if (!input_element || !input_element.contains(range.startContainer)) return
409-
410-
e.preventDefault()
411-
412-
const pre_selection_range = document.createRange()
413-
pre_selection_range.selectNodeContents(input_element)
414-
pre_selection_range.setEnd(range.startContainer, range.startOffset)
415-
const display_start = pre_selection_range.toString().length
416-
417-
pre_selection_range.setEnd(range.endContainer, range.endOffset)
418-
const display_end = pre_selection_range.toString().length
419-
420-
const raw_start = map_display_pos_to_raw_pos(
421-
display_start,
422-
props.value,
423-
props.context_file_paths ?? []
424-
)
425-
const raw_end = map_display_pos_to_raw_pos(
426-
display_end,
427-
props.value,
428-
props.context_file_paths ?? []
429-
)
430-
431-
const raw_text_slice = props.value.substring(raw_start, raw_end)
432-
e.clipboardData.setData('text/plain', raw_text_slice)
433-
}
434-
435304
const has_no_context =
436305
!props.context_file_paths || props.context_file_paths.length == 0
437306

@@ -453,7 +322,8 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
453322
className={cn(styles.container__inner, {
454323
[styles['container__inner--disabled']]:
455324
props.is_in_code_completions_mode &&
456-
(props.has_active_selection || !props.has_active_editor)
325+
(props.has_active_selection || !props.has_active_editor),
326+
[styles['container__inner--selecting']]: is_text_selecting
457327
})}
458328
onKeyDown={(e) => {
459329
if (e.key == 'f' && (e.ctrlKey || e.metaKey)) {
@@ -489,6 +359,11 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
489359
onKeyDown={handle_key_down}
490360
onCopy={handle_copy}
491361
onClick={handle_input_click}
362+
onMouseDown={handle_mouse_down}
363+
onDragStart={handle_drag_start}
364+
onDrop={handle_drop}
365+
onDragOver={handle_drag_over}
366+
onDragEnd={handle_drag_end}
492367
className={cn(styles.input, {
493368
[styles['input-with-tab-hint']]: show_tab_hint,
494369
[styles['input--empty']]: !props.value

0 commit comments

Comments
 (0)