@@ -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 } )
@@ -111,6 +111,7 @@ const FindReplace = ({ view, defaultQuery, defaultShowReplace }: FindReplaceProp
111111 wholeWord,
112112 } )
113113
114+ onSearchBarAction ?.( )
114115 if ( ! newQuery . eq ( query ) ) {
115116 setQuery ( newQuery )
116117 view . dispatch ( { effects : setSearchQuery . of ( newQuery ) } )
@@ -121,6 +122,7 @@ const FindReplace = ({ view, defaultQuery, defaultShowReplace }: FindReplaceProp
121122 useEffect ( ( ) => {
122123 if ( ! defaultQuery . eq ( query ) ) {
123124 setMatchesCount ( getUpdatedSearchMatchesCount ( defaultQuery , view ) )
125+ onSearchBarAction ?.( )
124126 setQuery ( defaultQuery )
125127 }
126128 } , [ defaultQuery ] )
@@ -162,6 +164,7 @@ const FindReplace = ({ view, defaultQuery, defaultShowReplace }: FindReplaceProp
162164 const onFindKeyDown = ( e : ReactKeyboardEvent < HTMLInputElement > ) => {
163165 e . stopPropagation ( )
164166 if ( e . key === 'Enter' ) {
167+ onSearchBarAction ?.( )
165168 e . preventDefault ( )
166169 if ( e . shiftKey ) {
167170 onPrevious ( )
@@ -431,62 +434,65 @@ const FindReplace = ({ view, defaultQuery, defaultShowReplace }: FindReplaceProp
431434 )
432435}
433436
434- export const codeEditorFindReplace = ( view : EditorView ) : Panel => {
435- const dom = document . createElement ( 'div' )
437+ export const getCodeEditorFindReplace =
438+ ( onSearchBarAction : CodeEditorProps [ 'onSearchBarAction' ] ) =>
439+ ( view : EditorView ) : Panel => {
440+ const dom = document . createElement ( 'div' )
436441
437- const keydown = ( e : KeyboardEvent ) => {
438- if ( runScopeHandlers ( view , e , 'search-panel' ) ) {
439- e . preventDefault ( )
440- e . stopPropagation ( )
442+ const keydown = ( e : KeyboardEvent ) => {
443+ if ( runScopeHandlers ( view , e , 'search-panel' ) ) {
444+ e . preventDefault ( )
445+ e . stopPropagation ( )
446+ }
441447 }
442- }
443448
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- }
449+ dom . className =
450+ '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'
451+ dom . onkeydown = keydown
452+
453+ const renderFindReplace = ( ) => {
454+ render (
455+ < FindReplace
456+ view = { view }
457+ defaultQuery = { getSearchQuery ( view . state ) }
458+ defaultShowReplace = { getShowReplaceField ( view . state ) }
459+ onSearchBarAction = { onSearchBarAction }
460+ /> ,
461+ dom ,
462+ )
463+ }
458464
459- const mount = ( ) => {
460- requestAnimationFrame ( ( ) => {
461- const findField = document . querySelector ( '[data-code-editor-find]' ) as HTMLInputElement
462- findField ?. focus ( )
463- findField ?. select ( )
464- } )
465- }
465+ const mount = ( ) => {
466+ requestAnimationFrame ( ( ) => {
467+ const findField = document . querySelector ( '[data-code-editor-find]' ) as HTMLInputElement
468+ findField ?. focus ( )
469+ findField ?. select ( )
470+ } )
471+ }
466472
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- }
473+ const update = ( { transactions, docChanged, state, startState } : ViewUpdate ) => {
474+ transactions . forEach ( ( tr ) => {
475+ tr . effects . forEach ( ( effect ) => {
476+ if ( effect . is ( setSearchQuery ) ) {
477+ renderFindReplace ( )
478+ }
479+ if ( effect . is ( setShowReplaceField ) ) {
480+ renderFindReplace ( )
481+ }
482+ } )
476483 } )
477- } )
478484
479- if ( docChanged || state . readOnly !== startState . readOnly ) {
480- renderFindReplace ( )
485+ if ( docChanged || state . readOnly !== startState . readOnly ) {
486+ renderFindReplace ( )
487+ }
481488 }
482- }
483489
484- renderFindReplace ( )
490+ renderFindReplace ( )
485491
486- return {
487- top : true ,
488- dom,
489- mount,
490- update,
492+ return {
493+ top : true ,
494+ dom,
495+ mount,
496+ update,
497+ }
491498 }
492- }
0 commit comments