@@ -7,49 +7,43 @@ import {
77 useCallback
88} from 'react'
99
10- type UseGhostTextParams = {
10+ export const use_ghost_text = ( params : {
1111 value : string
1212 input_ref : RefObject < HTMLDivElement >
1313 is_focused : boolean
1414 currently_open_file_text ?: string
1515 caret_position : number
16- }
17-
18- export const use_ghost_text = ( {
19- value,
20- input_ref,
21- is_focused,
22- currently_open_file_text,
23- caret_position
24- } : UseGhostTextParams ) => {
16+ } ) => {
2517 const [ ghost_text , set_ghost_text ] = useState ( '' )
2618 const ghost_text_debounce_timer_ref = useRef < ReturnType <
2719 typeof setTimeout
2820 > | null > ( null )
2921 const [ can_show_ghost_text , set_can_show_ghost_text ] = useState ( false )
30- const prev_is_focused_ref = useRef ( is_focused )
22+ const prev_is_focused_ref = useRef ( params . is_focused )
3123 const initial_value_on_focus_ref = useRef ( '' )
3224
3325 useEffect ( ( ) => {
34- if ( is_focused && ! prev_is_focused_ref . current ) {
26+ if ( params . is_focused && ! prev_is_focused_ref . current ) {
3527 set_can_show_ghost_text ( false )
36- initial_value_on_focus_ref . current = value
28+ initial_value_on_focus_ref . current = params . value
3729 } else if (
38- is_focused &&
30+ params . is_focused &&
3931 ! can_show_ghost_text &&
40- value !== initial_value_on_focus_ref . current
32+ params . value !== initial_value_on_focus_ref . current
4133 ) {
4234 set_can_show_ghost_text ( true )
4335 }
44- prev_is_focused_ref . current = is_focused
45- } , [ is_focused , value , can_show_ghost_text ] )
36+ prev_is_focused_ref . current = params . is_focused
37+ } , [ params . is_focused , params . value , can_show_ghost_text ] )
4638
4739 const identifiers = useMemo ( ( ) => {
48- if ( ! currently_open_file_text ) return new Set < string > ( )
49- const matches = currently_open_file_text . match ( / [ a - z A - Z _ ] [ a - z A - Z 0 - 9 _ ] * / g)
40+ if ( ! params . currently_open_file_text ) return new Set < string > ( )
41+ const matches = params . currently_open_file_text . match (
42+ / [ a - z A - Z _ ] [ a - z A - Z 0 - 9 _ ] * / g
43+ )
5044 if ( ! matches ) return new Set < string > ( )
5145 return new Set ( matches . filter ( ( id ) => id . length >= 3 ) )
52- } , [ currently_open_file_text ] )
46+ } , [ params . currently_open_file_text ] )
5347
5448 useEffect ( ( ) => {
5549 if ( ghost_text_debounce_timer_ref . current ) {
@@ -59,14 +53,14 @@ export const use_ghost_text = ({
5953
6054 let potential_ghost_text = ''
6155
62- if ( input_ref . current && is_focused && can_show_ghost_text ) {
56+ if ( params . input_ref . current && params . is_focused && can_show_ghost_text ) {
6357 const selection = window . getSelection ( )
6458 if ( selection && selection . rangeCount > 0 ) {
6559 const range = selection . getRangeAt ( 0 )
6660 if ( range . collapsed ) {
6761 let is_inside_symbol = false
6862 let current_node : Node | null = range . startContainer
69- while ( current_node && current_node !== input_ref . current ) {
63+ while ( current_node && current_node !== params . input_ref . current ) {
7064 if (
7165 current_node . nodeType === Node . ELEMENT_NODE &&
7266 ( current_node as HTMLElement ) . dataset . type ?. endsWith ( '-symbol' )
@@ -79,13 +73,13 @@ export const use_ghost_text = ({
7973
8074 if ( ! is_inside_symbol ) {
8175 const post_caret_range = range . cloneRange ( )
82- post_caret_range . selectNodeContents ( input_ref . current )
76+ post_caret_range . selectNodeContents ( params . input_ref . current )
8377 post_caret_range . setStart ( range . endContainer , range . endOffset )
8478 const text_after_cursor = post_caret_range . toString ( )
8579
8680 if ( text_after_cursor == '' || / ^ \s / . test ( text_after_cursor ) ) {
8781 const pre_caret_range = range . cloneRange ( )
88- pre_caret_range . selectNodeContents ( input_ref . current )
82+ pre_caret_range . selectNodeContents ( params . input_ref . current )
8983 pre_caret_range . setEnd ( range . startContainer , range . startOffset )
9084 const text_before_cursor = pre_caret_range . toString ( )
9185 const last_word_match = text_before_cursor . match ( / [ \S ] + $ / )
@@ -132,17 +126,17 @@ export const use_ghost_text = ({
132126 }
133127 }
134128 } , [
135- value ,
136- caret_position ,
129+ params . value ,
130+ params . caret_position ,
137131 identifiers ,
138- is_focused ,
132+ params . is_focused ,
139133 ghost_text ,
140- input_ref ,
134+ params . input_ref ,
141135 can_show_ghost_text
142136 ] )
143137
144138 useEffect ( ( ) => {
145- const input = input_ref . current
139+ const input = params . input_ref . current
146140 if ( ! input ) return
147141
148142 const existing_ghost = input . querySelector ( 'span[data-type="ghost-text"]' )
@@ -170,12 +164,12 @@ export const use_ghost_text = ({
170164 }
171165 }
172166 }
173- } , [ ghost_text , input_ref ] )
167+ } , [ ghost_text , params . input_ref ] )
174168
175169 const handle_accept_ghost_text = useCallback ( ( ) => {
176- if ( ! ghost_text || ! input_ref . current ) return
170+ if ( ! ghost_text || ! params . input_ref . current ) return
177171
178- const ghost_node = input_ref . current . querySelector (
172+ const ghost_node = params . input_ref . current . querySelector (
179173 'span[data-type="ghost-text"]'
180174 )
181175 if ( ghost_node ) {
@@ -195,11 +189,11 @@ export const use_ghost_text = ({
195189 }
196190 }
197191
198- input_ref . current . dispatchEvent (
192+ params . input_ref . current . dispatchEvent (
199193 new Event ( 'input' , { bubbles : true , cancelable : true } )
200194 )
201195 }
202- } , [ ghost_text , input_ref ] )
196+ } , [ ghost_text , params . input_ref ] )
203197
204198 return { ghost_text, handle_accept_ghost_text }
205199}
0 commit comments