瀏覽代碼

Fix a bug in the search function. #634

oldj 3 年之前
父節點
當前提交
744bf45300
共有 4 個文件被更改,包括 16 次插入13 次删除
  1. 2 2
      src/common/types.d.ts
  2. 4 1
      src/main/actions/find/findBy.ts
  3. 4 4
      src/main/actions/find/splitContent.ts
  4. 6 6
      src/renderer/pages/find.tsx

+ 2 - 2
src/common/types.d.ts

@@ -34,7 +34,7 @@ export interface IFindPosition {
   after: string
 }
 
-export interface IFindSpliter {
+export interface IFindSplitter {
   before: string
   match: string
   after: string
@@ -46,7 +46,7 @@ export interface IFindItem {
   item_title: string
   item_type: HostsType
   positions: IFindPosition[]
-  spliters: IFindSpliter[]
+  splitters: IFindSplitter[]
 }
 
 export type IFindShowSourceParam = IFindPosition & {

+ 4 - 1
src/main/actions/find/findBy.ts

@@ -40,13 +40,16 @@ export default async (
     }
     let content = await getContentOfHosts(item.id)
     let positions = findInContent(content, exp)
+    if (positions.length === 0) {
+      continue
+    }
 
     result_items.push({
       item_title: item.title || '',
       item_id: item.id,
       item_type,
       positions,
-      spliters: splitContent(content, positions),
+      splitters: splitContent(content, positions),
     })
   }
 

+ 4 - 4
src/main/actions/find/splitContent.ts

@@ -3,7 +3,7 @@
  * @homepage: https://oldj.net
  */
 
-import { IFindPosition, IFindSpliter } from '@root/common/types'
+import { IFindPosition, IFindSplitter } from '@root/common/types'
 
 type MatchResult = Pick<IFindPosition, 'start' | 'end' | 'match'> & {
   [key: string]: any
@@ -12,8 +12,8 @@ type MatchResult = Pick<IFindPosition, 'start' | 'end' | 'match'> & {
 export default (
   content: string,
   find_results: MatchResult[],
-): IFindSpliter[] => {
-  let spliters: IFindSpliter[] = []
+): IFindSplitter[] => {
+  let spliters: IFindSplitter[] = []
 
   let last_end = 0
   find_results.map((r, idx) => {
@@ -26,7 +26,7 @@ export default (
       after = content.slice(last_end)
     }
 
-    let spliter: IFindSpliter = {
+    let spliter: IFindSplitter = {
       before,
       after,
       match,

+ 6 - 6
src/renderer/pages/find.tsx

@@ -211,12 +211,12 @@ const find = (props: Props) => {
 
     let r = find_result.find((i) => i.item_id === pos.item_id)
     if (!r) return
-    let spliters = r.spliters
-    let sp = spliters[pos.index]
+    let splitters = r.splitters
+    let sp = splitters[pos.index]
     if (!sp) return
     sp.replace = replace_to
 
-    const content = spliters
+    const content = splitters
       .map((sp) => `${sp.before}${sp.replace ?? sp.match}${sp.after}`)
       .join('')
     await actions.setHostsContent(pos.item_id, content)
@@ -229,9 +229,9 @@ const find = (props: Props) => {
 
   const replaceAll = async () => {
     for (let item of find_result) {
-      let { item_id, item_type, spliters } = item
-      if (item_type !== 'local') continue
-      const content = spliters
+      let { item_id, item_type, splitters } = item
+      if (item_type !== 'local' || splitters.length === 0) continue
+      const content = splitters
         .map((sp) => `${sp.before}${replace_to}${sp.after}`)
         .join('')
       await actions.setHostsContent(item_id, content)