Skip to content

Commit 19d5ed0

Browse files
committed
feat(CodeEditor): add onSearchBarAction prop for enhanced search functionality
1 parent 45aad22 commit 19d5ed0

File tree

3 files changed

+61
-53
lines changed

3 files changed

+61
-53
lines changed

src/Shared/Components/CodeEditor/CodeEditor.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import {
4848
replaceAll,
4949
showReplaceFieldState,
5050
} from './Commands'
51-
import { codeEditorFindReplace, readOnlyTooltip, yamlHighlight } from './Extensions'
51+
import { getCodeEditorFindReplace, readOnlyTooltip, yamlHighlight } from './Extensions'
5252
import { CodeEditorContextProps, CodeEditorProps } from './types'
5353
import { getFoldGutterElement, getLanguageExtension, getValidationSchema, parseValueToCode } from './utils'
5454

@@ -90,6 +90,7 @@ const CodeEditor = <DiffView extends boolean = false>({
9090
autoFocus,
9191
disableSearch = false,
9292
onOpenSearchPanel = noop,
93+
onSearchBarAction = noop,
9394
}: CodeEditorProps<DiffView>) => {
9495
// HOOKS
9596
const { appTheme } = useTheme()
@@ -216,7 +217,7 @@ const CodeEditor = <DiffView extends boolean = false>({
216217
foldingCompartment.of(foldConfig),
217218
lintGutter(),
218219
search({
219-
createPanel: codeEditorFindReplace,
220+
createPanel: getCodeEditorFindReplace(onSearchBarAction),
220221
}),
221222
showReplaceFieldState,
222223
...(mode === MODES.YAML ? [yamlHighlight] : []),

src/Shared/Components/CodeEditor/Extensions/findAndReplace.tsx

Lines changed: 56 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import {
5050
REPLACE_SHORTCUT_KEYS,
5151
} from '../CodeEditor.constants'
5252
import { getShowReplaceField, setShowReplaceField } from '../Commands'
53-
import { FindReplaceProps, FindReplaceQuery, FindReplaceToggleButtonProps } from '../types'
53+
import { CodeEditorProps, FindReplaceProps, FindReplaceQuery, FindReplaceToggleButtonProps } from '../types'
5454
import { getFindReplaceToggleButtonIconClass, getUpdatedSearchMatchesCount } from '../utils'
5555

5656
const 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-
}

src/Shared/Components/CodeEditor/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export type CodeEditorProps<DiffView extends boolean = false> = {
7979
diffView?: DiffView
8080
theme?: AppThemeType
8181
onOpenSearchPanel?: () => void
82+
onSearchBarAction?: () => void
8283
} & CodeEditorPropsBasedOnDiffView<DiffView>
8384

8485
export interface GetCodeEditorHeightReturnType {
@@ -97,7 +98,7 @@ export type FindReplaceQuery = Partial<
9798
Pick<SearchQuery, 'search' | 'wholeWord' | 'regexp' | 'replace' | 'caseSensitive'>
9899
>
99100

100-
export interface FindReplaceProps {
101+
export interface FindReplaceProps extends Pick<CodeEditorProps, 'onSearchBarAction'> {
101102
view: EditorView
102103
/** Default value for Search Query state. */
103104
defaultQuery: SearchQuery

0 commit comments

Comments
 (0)