Skip to content

Commit 1144575

Browse files
committed
Refine ghost text handling to prevent display when characters exist immediately after the cursor, and enable accepting ghost text via the right arrow key
1 parent 4352bb7 commit 1144575

2 files changed

Lines changed: 67 additions & 5 deletions

File tree

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,18 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
250250
return
251251
}
252252

253+
// Check if there's a character on the right side of the caret
254+
const post_caret_range = range.cloneRange()
255+
post_caret_range.selectNodeContents(input_ref.current)
256+
post_caret_range.setStart(range.endContainer, range.endOffset)
257+
const text_after_cursor = post_caret_range.toString()
258+
259+
// Only allow ghost text if there's nothing after cursor or only whitespace
260+
if (text_after_cursor.length > 0 && text_after_cursor[0].trim() !== '') {
261+
set_ghost_text('')
262+
return
263+
}
264+
253265
const pre_caret_range = range.cloneRange()
254266
pre_caret_range.selectNodeContents(input_ref.current)
255267
pre_caret_range.setEnd(range.startContainer, range.startOffset)
@@ -285,11 +297,10 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
285297
}, [props.value, caret_position, identifiers])
286298

287299
useEffect(() => {
288-
const input_element = input_ref.current
289-
if (input_element && input_element.innerHTML !== highlighted_html) {
290-
const selection_start = get_caret_position_from_div(input_element)
291-
input_element.innerHTML = highlighted_html
292-
set_caret_position_for_div(input_element, selection_start)
300+
if (input_ref.current && input_ref.current.innerHTML !== highlighted_html) {
301+
const selection_start = get_caret_position_from_div(input_ref.current)
302+
input_ref.current.innerHTML = highlighted_html
303+
set_caret_position_for_div(input_ref.current, selection_start)
293304
}
294305
}, [highlighted_html])
295306

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,57 @@ export const use_handlers = (
717717

718718
const handle_key_down = (e: React.KeyboardEvent<HTMLDivElement>) => {
719719
const { key, shiftKey } = e
720+
721+
// Handle arrow-right when ghost text is present
722+
if (key === 'ArrowRight' && ghost_text && !shiftKey) {
723+
const selection = window.getSelection()
724+
if (
725+
selection &&
726+
selection.rangeCount > 0 &&
727+
selection.isCollapsed &&
728+
input_ref.current
729+
) {
730+
const range = selection.getRangeAt(0)
731+
const ghost_node = input_ref.current.querySelector(
732+
'span[data-type="ghost-text"]'
733+
)
734+
735+
// Check if caret is right before the ghost text node
736+
if (ghost_node) {
737+
const { startContainer, startOffset } = range
738+
739+
// Check if we're at the position just before ghost text
740+
let is_before_ghost = false
741+
742+
if (startContainer === ghost_node.previousSibling) {
743+
// Caret is at the end of the text node before ghost
744+
if (startContainer.nodeType === Node.TEXT_NODE) {
745+
is_before_ghost = startOffset === startContainer.textContent?.length
746+
}
747+
} else if (startContainer === ghost_node.parentNode) {
748+
// Caret is in the parent, check if it's right before ghost node
749+
const ghost_index = Array.from(startContainer.childNodes).indexOf(ghost_node as ChildNode)
750+
is_before_ghost = startOffset === ghost_index
751+
}
752+
753+
if (is_before_ghost) {
754+
e.preventDefault()
755+
756+
// Remove ghost text by replacing with empty text node or just removing it
757+
if (ghost_node.parentNode) {
758+
ghost_node.parentNode.removeChild(ghost_node)
759+
}
760+
761+
// Move caret one position to the right in the actual text
762+
const current_pos = get_caret_position_from_div(input_ref.current)
763+
set_caret_position_for_div(input_ref.current, current_pos + 1)
764+
765+
return
766+
}
767+
}
768+
}
769+
}
770+
720771
if ((e.ctrlKey || e.metaKey) && !shiftKey && key.toLowerCase() === 'z') {
721772
e.preventDefault()
722773
if (undo_stack.length > 0) {

0 commit comments

Comments
 (0)