1- import React , { useEffect , useState } from 'react' ;
1+ import React , { useEffect , useId , useState } from 'react' ;
2+
23import deepEqual from 'deep-equal' ;
4+
35import { ActionButton , Button , IconComponent , Input , Label , Popover , PopoverContent , PopoverTrigger , Switch } from '@/components' ;
6+ import { SearchAndReplace } from '@/extensions/SearchAndReplace/SearchAndReplace' ;
47import { useLocale } from '@/locales' ;
5- import { ON_SEARCH_RESULTS , SearchAndReplace } from '@/extensions/SearchAndReplace/SearchAndReplace' ;
8+ import { listenEvent } from '@/utils/customEvents/customEvents' ;
9+ import { eventName } from '@/utils/customEvents/events.constant' ;
610
711function SearchAndReplaceButton ( { editor, ...props } : any ) {
812 const { t } = useLocale ( ) ;
@@ -13,6 +17,7 @@ function SearchAndReplaceButton({ editor, ...props }: any) {
1317 const [ replaceValue , setReplaceValue ] = useState ( '' ) ;
1418 const [ visible , setVisible ] = useState ( false ) ;
1519 const [ caseSensitive , setCaseSensitive ] = useState ( false ) ;
20+ const id = useId ( ) ;
1621
1722 useEffect ( ( ) => {
1823 if ( ! visible ) {
@@ -61,102 +66,122 @@ function SearchAndReplaceButton({ editor, ...props }: any) {
6166 setResults ( prevResults => ( deepEqual ( prevResults , results ) ? prevResults : results ) ) ;
6267 } ;
6368
64- window . addEventListener ( ON_SEARCH_RESULTS , listener ) ;
69+ eventName . setEventNameSearchReplace ( id ) ;
70+
71+ listenEvent ( eventName . getEventNameSearchReplace ( ) , listener ) ;
6572
6673 return ( ) => {
6774 if ( ! searchExtension )
6875 return ;
69- window . removeEventListener ( ON_SEARCH_RESULTS , listener ) ;
76+ listenEvent ( eventName . getEventNameSearchReplace ( ) , listener ) ;
7077 } ;
7178 } , [ visible , editor ] ) ;
7279
7380 return (
7481 < Popover
75- open = { visible }
7682 onOpenChange = { setVisible }
83+ open = { visible }
7784 >
7885 < PopoverTrigger
79- disabled = { props ?. disabled }
8086 asChild
87+ disabled = { props ?. disabled }
8188 >
8289 < ActionButton
83- tooltip = { props ?. tooltip }
84- isActive = { props ?. isActive }
8590 disabled = { props ?. disabled }
91+ isActive = { props ?. isActive }
92+ tooltip = { props ?. tooltip }
8693 >
8794 < IconComponent name = { props ?. icon } />
8895 </ ActionButton >
8996 </ PopoverTrigger >
97+
9098 < PopoverContent
91- hideWhenDetached
92- className = "richtext-w-full"
9399 align = "start"
100+ className = "richtext-w-full"
101+ hideWhenDetached
94102 side = "bottom"
95103 >
96104
97105 < div className = "richtext-mb-[6px] richtext-flex richtext-items-center richtext-justify-between" >
98106 < Label >
99107 { t ( 'editor.search.dialog.text' ) }
100108 </ Label >
109+
101110 < span className = "richtext-font-semibold" >
102111 { results . length > 0 ? `${ currentIndex + 1 } /${ results . length } ` : '0/0' }
103112 </ span >
104113 </ div >
105- < div className = "richtext-flex richtext-w-full richtext-max-w-sm richtext-items-center richtext-gap-1.5 richtext-mb-[10px]" >
114+
115+ < div className = "richtext-mb-[10px] richtext-flex richtext-w-full richtext-max-w-sm richtext-items-center richtext-gap-1.5" >
106116 < Input
107- type = "text"
108- required
117+ autoFocus
109118 className = "richtext-w-full"
119+ onChange = { e => setSearchValue ( e . target . value ) }
110120 placeholder = "Text"
111- autoFocus
121+ required
122+ type = "text"
112123 value = { searchValue }
113- onChange = { e => setSearchValue ( e . target . value ) }
114124 />
115125
116- < Button disabled = { results . length === 0 } className = "richtext-flex-1" onClick = { editor . commands . goToPrevSearchResult } >
126+ < Button className = "richtext-flex-1"
127+ disabled = { results . length === 0 }
128+ onClick = { editor . commands . goToPrevSearchResult }
129+ >
117130 < IconComponent name = "ChevronUp" />
118131 </ Button >
119132
120- < Button disabled = { results . length === 0 } className = "richtext-flex-1" onClick = { editor . commands . goToNextSearchResult } >
133+ < Button className = "richtext-flex-1"
134+ disabled = { results . length === 0 }
135+ onClick = { editor . commands . goToNextSearchResult }
136+ >
121137 < IconComponent name = "ChevronDown" />
122138 </ Button >
123139
124140 </ div >
141+
125142 < Label className = "richtext-mb-[6px]" >
126143 { t ( 'editor.replace.dialog.text' ) }
127144 </ Label >
128- < div className = "richtext-flex richtext-w-full richtext-max-w-sm richtext-items-center richtext-gap-1.5 richtext-mb-[5px]" >
129- < div className = "richtext-relative richtext-items-center richtext-w-full richtext-max-w-sm" >
145+
146+ < div className = "richtext-mb-[5px] richtext-flex richtext-w-full richtext-max-w-sm richtext-items-center richtext-gap-1.5" >
147+ < div className = "richtext-relative richtext-w-full richtext-max-w-sm richtext-items-center" >
130148 < Input
131- type = "text"
132- required
133149 className = "richtext-w-80"
150+ onChange = { e => setReplaceValue ( e . target . value ) }
134151 placeholder = "Text"
152+ required
153+ type = "text"
135154 value = { replaceValue }
136- onChange = { e => setReplaceValue ( e . target . value ) }
137155 />
138156 </ div >
139157 </ div >
140158
141- < div className = "richtext-flex richtext-items-center richtext-space-x-2 richtext-mb-[10px] " >
159+ < div className = "richtext-mb-[10px] richtext- flex richtext-items-center richtext-space-x-2" >
142160 < Switch
143161 checked = { caseSensitive }
144162 onCheckedChange = { ( e : any ) => {
145163 setCaseSensitive ( e ) ;
146164 editor . commands . setCaseSensitive ( e ) ;
147165 } }
148166 />
167+
149168 < Label >
150169 { t ( 'editor.replace.caseSensitive' ) }
151170 </ Label >
152171 </ div >
153172
154173 < div className = "richtext-flex richtext-items-center richtext-gap-[10px]" >
155- < Button disabled = { results . length === 0 } className = "richtext-flex-1" onClick = { editor . commands . replace } >
174+ < Button className = "richtext-flex-1"
175+ disabled = { results . length === 0 }
176+ onClick = { editor . commands . replace }
177+ >
156178 { t ( 'editor.replace.dialog.text' ) }
157179 </ Button >
158180
159- < Button disabled = { results . length === 0 } className = "richtext-flex-1" onClick = { editor . commands . replaceAll } >
181+ < Button className = "richtext-flex-1"
182+ disabled = { results . length === 0 }
183+ onClick = { editor . commands . replaceAll }
184+ >
160185 { t ( 'editor.replaceAll.dialog.text' ) }
161186 </ Button >
162187 </ div >
0 commit comments