|
@@ -15,9 +15,12 @@ import {
|
|
|
useColorMode,
|
|
|
VStack,
|
|
|
} from '@chakra-ui/react'
|
|
|
-import { agent } from '@renderer/core/agent'
|
|
|
+import { actions, agent } from '@renderer/core/agent'
|
|
|
import useOnBroadcast from '@renderer/core/useOnBroadcast'
|
|
|
-import React, { useEffect } from 'react'
|
|
|
+import { IFindResultItem } from '@root/common/types'
|
|
|
+import { useDebounce } from 'ahooks'
|
|
|
+import lodash from 'lodash'
|
|
|
+import React, { useEffect, useState } from 'react'
|
|
|
import { IoSearch } from 'react-icons/io5'
|
|
|
import styles from './find.less'
|
|
|
|
|
@@ -29,6 +32,12 @@ const find = (props: Props) => {
|
|
|
const { lang, setLocale } = useModel('useI18n')
|
|
|
const { configs, loadConfigs } = useModel('useConfigs')
|
|
|
const { colorMode, setColorMode } = useColorMode()
|
|
|
+ const [keyword, setKeyword] = useState('')
|
|
|
+ const [replact_to, setReplaceTo] = useState('')
|
|
|
+ const [is_regexp, setIsRegExp] = useState(false)
|
|
|
+ const [is_ignore_case, setIsIgnoreCase] = useState(false)
|
|
|
+ const [find_result, setFindResult] = useState<IFindResultItem[]>([])
|
|
|
+ const debounced_keyword = useDebounce(keyword, { wait: 500 })
|
|
|
|
|
|
const init = async () => {
|
|
|
if (!configs) return
|
|
@@ -55,8 +64,27 @@ const find = (props: Props) => {
|
|
|
document.title = lang.find_and_replace
|
|
|
}, [lang])
|
|
|
|
|
|
+ useEffect(() => {
|
|
|
+ doFind(debounced_keyword)
|
|
|
+ }, [debounced_keyword, is_regexp, is_ignore_case])
|
|
|
+
|
|
|
useOnBroadcast('config_updated', loadConfigs)
|
|
|
|
|
|
+ const doFind = lodash.debounce(async (v: string) => {
|
|
|
+ console.log('find by:', v)
|
|
|
+ if (!v) {
|
|
|
+ setFindResult([])
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ let result = await actions.findBy(v, {
|
|
|
+ is_regexp,
|
|
|
+ is_ignore_case,
|
|
|
+ })
|
|
|
+ setFindResult(result)
|
|
|
+ console.log(result)
|
|
|
+ }, 500)
|
|
|
+
|
|
|
return (
|
|
|
<div className={styles.root}>
|
|
|
<VStack
|
|
@@ -69,8 +97,13 @@ const find = (props: Props) => {
|
|
|
children={<IoSearch color="gray.300"/>}
|
|
|
/>
|
|
|
<Input
|
|
|
+ autoFocus={true}
|
|
|
placeholder="keywords"
|
|
|
variant="flushed"
|
|
|
+ value={keyword}
|
|
|
+ onChange={(e) => {
|
|
|
+ setKeyword(e.target.value)
|
|
|
+ }}
|
|
|
/>
|
|
|
</InputGroup>
|
|
|
|
|
@@ -82,6 +115,10 @@ const find = (props: Props) => {
|
|
|
<Input
|
|
|
placeholder="replace to"
|
|
|
variant="flushed"
|
|
|
+ value={replact_to}
|
|
|
+ onChange={(e) => {
|
|
|
+ setReplaceTo(e.target.value)
|
|
|
+ }}
|
|
|
/>
|
|
|
</InputGroup>
|
|
|
|
|
@@ -92,8 +129,14 @@ const find = (props: Props) => {
|
|
|
spacing={4}
|
|
|
// justifyContent="flex-start"
|
|
|
>
|
|
|
- <Checkbox>{lang.regexp}</Checkbox>
|
|
|
- <Checkbox>{lang.ignore_case}</Checkbox>
|
|
|
+ <Checkbox
|
|
|
+ checked={is_regexp}
|
|
|
+ onChange={(e) => setIsRegExp(e.target.checked)}
|
|
|
+ >{lang.regexp}</Checkbox>
|
|
|
+ <Checkbox
|
|
|
+ checked={is_ignore_case}
|
|
|
+ onChange={(e) => setIsIgnoreCase(e.target.checked)}
|
|
|
+ >{lang.ignore_case}</Checkbox>
|
|
|
</HStack>
|
|
|
|
|
|
<Box
|
|
@@ -114,11 +157,13 @@ const find = (props: Props) => {
|
|
|
<Button
|
|
|
size="sm"
|
|
|
variant="outline"
|
|
|
+ isDisabled={find_result.length === 0}
|
|
|
>{lang.replace_all}</Button>
|
|
|
<Button
|
|
|
size="sm"
|
|
|
variant="solid"
|
|
|
colorScheme="blue"
|
|
|
+ isDisabled={find_result.length === 0}
|
|
|
>{lang.replace}</Button>
|
|
|
</HStack>
|
|
|
</VStack>
|