Skip to content

Commit c0069ce

Browse files
committed
Prevent ghost text suggestions when cursor is inside a symbol element
1 parent e01787a commit c0069ce

1 file changed

Lines changed: 40 additions & 24 deletions

File tree

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

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -64,30 +64,46 @@ export const use_ghost_text = ({
6464
if (selection && selection.rangeCount > 0) {
6565
const range = selection.getRangeAt(0)
6666
if (range.collapsed) {
67-
const post_caret_range = range.cloneRange()
68-
post_caret_range.selectNodeContents(input_ref.current)
69-
post_caret_range.setStart(range.endContainer, range.endOffset)
70-
const text_after_cursor = post_caret_range.toString()
71-
72-
if (text_after_cursor == '' || /^\s/.test(text_after_cursor)) {
73-
const pre_caret_range = range.cloneRange()
74-
pre_caret_range.selectNodeContents(input_ref.current)
75-
pre_caret_range.setEnd(range.startContainer, range.startOffset)
76-
const text_before_cursor = pre_caret_range.toString()
77-
const last_word_match = text_before_cursor.match(/[\S]+$/)
78-
79-
if (last_word_match) {
80-
const last_word_with_prefix = last_word_match[0]
81-
const word_start_match = last_word_with_prefix.match(/[a-zA-Z_]/)
82-
if (word_start_match) {
83-
const last_word = last_word_with_prefix.substring(
84-
word_start_match.index ?? 0
85-
)
86-
if (last_word.length >= 2) {
87-
for (const id of identifiers) {
88-
if (id.startsWith(last_word) && id !== last_word) {
89-
potential_ghost_text = id.substring(last_word.length)
90-
break
67+
let is_inside_symbol = false
68+
let current_node: Node | null = range.startContainer
69+
while (current_node && current_node !== input_ref.current) {
70+
if (
71+
current_node.nodeType === Node.ELEMENT_NODE &&
72+
(current_node as HTMLElement).dataset.type?.endsWith('-symbol')
73+
) {
74+
is_inside_symbol = true
75+
break
76+
}
77+
current_node = current_node.parentNode
78+
}
79+
80+
if (!is_inside_symbol) {
81+
const post_caret_range = range.cloneRange()
82+
post_caret_range.selectNodeContents(input_ref.current)
83+
post_caret_range.setStart(range.endContainer, range.endOffset)
84+
const text_after_cursor = post_caret_range.toString()
85+
86+
if (text_after_cursor == '' || /^\s/.test(text_after_cursor)) {
87+
const pre_caret_range = range.cloneRange()
88+
pre_caret_range.selectNodeContents(input_ref.current)
89+
pre_caret_range.setEnd(range.startContainer, range.startOffset)
90+
const text_before_cursor = pre_caret_range.toString()
91+
const last_word_match = text_before_cursor.match(/[\S]+$/)
92+
93+
if (last_word_match) {
94+
const last_word_with_prefix = last_word_match[0]
95+
const word_start_match =
96+
last_word_with_prefix.match(/[a-zA-Z_]/)
97+
if (word_start_match) {
98+
const last_word = last_word_with_prefix.substring(
99+
word_start_match.index ?? 0
100+
)
101+
if (last_word.length >= 2) {
102+
for (const id of identifiers) {
103+
if (id.startsWith(last_word) && id !== last_word) {
104+
potential_ghost_text = id.substring(last_word.length)
105+
break
106+
}
91107
}
92108
}
93109
}

0 commit comments

Comments
 (0)