-
Notifications
You must be signed in to change notification settings - Fork 2
/
SearchReplaceView.tsx
224 lines (215 loc) · 5.85 KB
/
SearchReplaceView.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import * as React from 'react'
import '@vscode/codicons/dist/codicon.css'
import {
VSCodeTextArea,
VSCodeTextField,
VSCodeButton,
VSCodeDropdown,
VSCodeOption,
VSCodeCheckbox,
} from '@vscode/webview-ui-toolkit/react'
import { css } from '@emotion/css'
import useEvent from '../react/useEvent'
import {
SearchReplaceViewStatus,
SearchReplaceViewValues,
} from './SearchReplaceViewTypes'
export default function SearchReplaceView({
status: {
running,
completed,
total,
numMatches,
numFilesWithMatches,
numFilesWithErrors,
numFilesThatWillChange,
},
values,
onValuesChange,
onReplaceAllClick,
}: {
status: SearchReplaceViewStatus
values: SearchReplaceViewValues
onValuesChange: (values: Partial<SearchReplaceViewValues>) => unknown
// eslint-disable-next-line @typescript-eslint/no-explicit-any
onReplaceAllClick: (e: React.SyntheticEvent<any>) => unknown
}): React.ReactElement {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const handleFindChange = React.useCallback((e: any) => {
onValuesChange({
find: e.target.value,
})
}, [])
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const handleReplaceChange = React.useCallback((e: any) => {
onValuesChange({
replace: e.target.value,
})
}, [])
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const handleIncludeChange = React.useCallback((e: any) => {
onValuesChange({
include: e.target.value,
})
}, [])
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const handleExcludeChange = React.useCallback((e: any) => {
onValuesChange({
exclude: e.target.value,
})
}, [])
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const handleParserChange = React.useCallback((e: any) => {
onValuesChange({
parser: e.target.value,
})
}, [])
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const handlePrettierChange = React.useCallback((e: any) => {
onValuesChange({
prettier: e.target.checked,
})
}, [])
const handleKeyDown = useEvent((e: React.KeyboardEvent<HTMLDivElement>) => {
if (e.ctrlKey || e.metaKey) {
onValuesChange({})
}
})
const [showDetails, setShowDetails] = React.useState(true)
const toggleShowDetails = React.useCallback(
() => setShowDetails((value) => !value),
[]
)
return (
<div
onKeyDown={handleKeyDown}
className={css`
display: flex;
flex-direction: column;
`}
>
<VSCodeTextArea
className={css`
margin-top: 8px;
`}
placeholder="Search"
name="search"
value={values.find}
onInput={handleFindChange}
/>
<VSCodeTextArea
className={css`
margin-top: 4px;
`}
placeholder="Replace"
name="replace"
value={values.replace}
onInput={handleReplaceChange}
/>
<div
className={css`
display: flex;
justify-content: flex-end;
`}
>
<VSCodeButton appearance="icon" onClick={toggleShowDetails}>
<span className="codicon codicon-ellipsis" />
</VSCodeButton>
<VSCodeButton
appearance="icon"
onClick={onReplaceAllClick}
disabled={running || !numFilesThatWillChange}
>
<span className="codicon codicon-replace-all" />
</VSCodeButton>
</div>
{showDetails && (
<div
className={css`
display: flex;
flex-direction: column;
`}
>
<VSCodeTextField
className={css`
margin-top: 4px;
`}
name="filesToInclude"
value={values.include}
onInput={handleIncludeChange}
>
files to include
</VSCodeTextField>
<VSCodeTextField
className={css`
margin-top: 4px;
`}
name="filesToExclude"
value={values.exclude}
onInput={handleExcludeChange}
>
files to exclude
</VSCodeTextField>
<p>Parser</p>
<VSCodeDropdown
position="below"
value={values.parser}
onChange={handleParserChange}
>
<VSCodeOption>babel</VSCodeOption>
<VSCodeOption>babel/auto</VSCodeOption>
<VSCodeOption>recast/babel</VSCodeOption>
<VSCodeOption>recast/babel/auto</VSCodeOption>
</VSCodeDropdown>
<VSCodeCheckbox
checked={values.prettier}
onChange={handlePrettierChange}
>
Use Prettier if Available
</VSCodeCheckbox>
</div>
)}
<div
className={css`
margin-top: 8px;
height: 4px;
position: relative;
`}
>
<div
className={css`
position: absolute;
left: 0;
top: 0;
bottom: 0;
visibility: ${running ? 'visible' : 'hidden'};
width: ${((completed * 100) / (total || 1)).toFixed(1)}%;
background-color: var(--vscode-progressBar-background);
`}
/>
</div>
{numMatches ? (
<div
className={css`
margin-top: 8px;
opacity: 0.65;
`}
>
Found {numMatches} {numMatches === 1 ? 'match' : 'matches'} in{' '}
{numFilesWithMatches} {numFilesWithMatches === 1 ? 'file' : 'files'}
</div>
) : null}
{numFilesWithErrors ? (
<div
className={css`
margin-top: 8px;
color: var(--vscode-list-errorForeground);
`}
>
{numFilesWithErrors} {numFilesWithErrors === 1 ? 'file' : 'files'} had
errors
</div>
) : null}
</div>
)
}