@@ -50,7 +50,7 @@ import {
5050 REPLACE_SHORTCUT_KEYS ,
5151} from '../CodeEditor.constants'
5252import { getShowReplaceField , setShowReplaceField } from '../Commands'
53- import { FindReplaceProps , FindReplaceQuery , FindReplaceToggleButtonProps } from '../types'
53+ import { CodeEditorProps , FindReplaceProps , FindReplaceQuery , FindReplaceToggleButtonProps } from '../types'
5454import { getFindReplaceToggleButtonIconClass , getUpdatedSearchMatchesCount } from '../utils'
5555
5656const FindReplaceToggleButton = ( {
@@ -85,7 +85,7 @@ const FindReplaceToggleButton = ({
8585 )
8686}
8787
88- const FindReplace = ( { view, defaultQuery, defaultShowReplace } : FindReplaceProps ) => {
88+ const FindReplace = ( { view, defaultQuery, defaultShowReplace, onSearchBarAction } : FindReplaceProps ) => {
8989 // STATES
9090 const [ query , setQuery ] = useState < SearchQuery > ( new SearchQuery ( { search : '' } ) )
9191 const [ matchesCount , setMatchesCount ] = useState ( { count : 0 , current : 1 } )
@@ -103,6 +103,9 @@ const FindReplace = ({ view, defaultQuery, defaultShowReplace }: FindReplaceProp
103103 search = query . search ,
104104 wholeWord = query . wholeWord ,
105105 } : FindReplaceQuery ) => {
106+ // Calling this irrespective of whether the query has changed or not
107+ onSearchBarAction ( )
108+
106109 const newQuery = new SearchQuery ( {
107110 caseSensitive,
108111 regexp,
@@ -120,6 +123,7 @@ const FindReplace = ({ view, defaultQuery, defaultShowReplace }: FindReplaceProp
120123
121124 useEffect ( ( ) => {
122125 if ( ! defaultQuery . eq ( query ) ) {
126+ onSearchBarAction ( )
123127 setMatchesCount ( getUpdatedSearchMatchesCount ( defaultQuery , view ) )
124128 setQuery ( defaultQuery )
125129 }
@@ -144,13 +148,15 @@ const FindReplace = ({ view, defaultQuery, defaultShowReplace }: FindReplaceProp
144148 const onNext = ( e ?: MouseEvent < HTMLButtonElement > ) => {
145149 e ?. preventDefault ( )
146150 e ?. stopPropagation ( )
151+ onSearchBarAction ( )
147152 findNext ( view )
148153 setMatchesCount ( getUpdatedSearchMatchesCount ( query , view ) )
149154 }
150155
151156 const onPrevious = ( e ?: MouseEvent < HTMLButtonElement > ) => {
152157 e ?. preventDefault ( )
153158 e ?. stopPropagation ( )
159+ onSearchBarAction ( )
154160 findPrevious ( view )
155161 setMatchesCount ( getUpdatedSearchMatchesCount ( query , view ) )
156162 }
@@ -179,6 +185,7 @@ const FindReplace = ({ view, defaultQuery, defaultShowReplace }: FindReplaceProp
179185 e . stopPropagation ( )
180186 if ( e . key === 'Enter' ) {
181187 e . preventDefault ( )
188+ onSearchBarAction ( )
182189 replaceNext ( view )
183190 }
184191 }
@@ -188,10 +195,12 @@ const FindReplace = ({ view, defaultQuery, defaultShowReplace }: FindReplaceProp
188195 }
189196
190197 const onReplaceTextClick = ( ) => {
198+ onSearchBarAction ( )
191199 replaceNext ( view )
192200 }
193201
194202 const onReplaceTextAllClick = ( ) => {
203+ onSearchBarAction ( )
195204 replaceAll ( view )
196205 }
197206
@@ -431,62 +440,65 @@ const FindReplace = ({ view, defaultQuery, defaultShowReplace }: FindReplaceProp
431440 )
432441}
433442
434- export const codeEditorFindReplace = ( view : EditorView ) : Panel => {
435- const dom = document . createElement ( 'div' )
443+ export const getCodeEditorFindReplace =
444+ ( onSearchBarAction : CodeEditorProps [ 'onSearchBarAction' ] ) =>
445+ ( view : EditorView ) : Panel => {
446+ const dom = document . createElement ( 'div' )
436447
437- const keydown = ( e : KeyboardEvent ) => {
438- if ( runScopeHandlers ( view , e , 'search-panel' ) ) {
439- e . preventDefault ( )
440- e . stopPropagation ( )
448+ const keydown = ( e : KeyboardEvent ) => {
449+ if ( runScopeHandlers ( view , e , 'search-panel' ) ) {
450+ e . preventDefault ( )
451+ e . stopPropagation ( )
452+ }
441453 }
442- }
443454
444- dom . className =
445- 'code-editor__search mt-8 mb-4 mr-8 ml-auto p-5 bg__secondary dc__border br-4 dc__w-fit-content fs-14'
446- dom . onkeydown = keydown
447-
448- const renderFindReplace = ( ) => {
449- render (
450- < FindReplace
451- view = { view }
452- defaultQuery = { getSearchQuery ( view . state ) }
453- defaultShowReplace = { getShowReplaceField ( view . state ) }
454- /> ,
455- dom ,
456- )
457- }
455+ dom . className =
456+ 'code-editor__search mt-8 mb-4 mr-8 ml-auto p-5 bg__secondary dc__border br-4 dc__w-fit-content fs-14'
457+ dom . onkeydown = keydown
458+
459+ const renderFindReplace = ( ) => {
460+ render (
461+ < FindReplace
462+ view = { view }
463+ defaultQuery = { getSearchQuery ( view . state ) }
464+ defaultShowReplace = { getShowReplaceField ( view . state ) }
465+ onSearchBarAction = { onSearchBarAction }
466+ /> ,
467+ dom ,
468+ )
469+ }
458470
459- const mount = ( ) => {
460- requestAnimationFrame ( ( ) => {
461- const findField = document . querySelector ( '[data-code-editor-find]' ) as HTMLInputElement
462- findField ?. focus ( )
463- findField ?. select ( )
464- } )
465- }
471+ const mount = ( ) => {
472+ requestAnimationFrame ( ( ) => {
473+ const findField = document . querySelector ( '[data-code-editor-find]' ) as HTMLInputElement
474+ findField ?. focus ( )
475+ findField ?. select ( )
476+ } )
477+ }
466478
467- const update = ( { transactions, docChanged, state, startState } : ViewUpdate ) => {
468- transactions . forEach ( ( tr ) => {
469- tr . effects . forEach ( ( effect ) => {
470- if ( effect . is ( setSearchQuery ) ) {
471- renderFindReplace ( )
472- }
473- if ( effect . is ( setShowReplaceField ) ) {
474- renderFindReplace ( )
475- }
479+ const update = ( { transactions, docChanged, state, startState } : ViewUpdate ) => {
480+ transactions . forEach ( ( tr ) => {
481+ tr . effects . forEach ( ( effect ) => {
482+ if ( effect . is ( setSearchQuery ) ) {
483+ renderFindReplace ( )
484+ }
485+ if ( effect . is ( setShowReplaceField ) ) {
486+ renderFindReplace ( )
487+ }
488+ } )
476489 } )
477- } )
478490
479- if ( docChanged || state . readOnly !== startState . readOnly ) {
480- renderFindReplace ( )
491+ if ( docChanged || state . readOnly !== startState . readOnly ) {
492+ renderFindReplace ( )
493+ }
481494 }
482- }
483495
484- renderFindReplace ( )
496+ renderFindReplace ( )
485497
486- return {
487- top : true ,
488- dom,
489- mount,
490- update,
498+ return {
499+ top : true ,
500+ dom,
501+ mount,
502+ update,
503+ }
491504 }
492- }
0 commit comments