Skip to content

Commit

Permalink
Update search functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
oldj committed May 8, 2021
1 parent 51e7144 commit 65aa746
Show file tree
Hide file tree
Showing 11 changed files with 247 additions and 65 deletions.
2 changes: 2 additions & 0 deletions src/common/i18n/languages/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,14 @@ export default {
never: 'Never',
new: 'New',
new_version_found: 'New version found',
next: 'Next',
no_access_to_hosts: 'No permission to write to the Hosts file.',
no_record: 'No record',
password: 'Password',
paste: 'Paste',
port: 'Port',
preferences: 'Preferences',
previous: 'Previous',
protocol: 'Protocol',
proxy: 'Proxy',
quit: 'Quit',
Expand Down
2 changes: 2 additions & 0 deletions src/common/i18n/languages/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,14 @@ const lang: LanguageDict = {
never: '从不',
new: '新建',
new_version_found: '发现新版本',
next: '下一个',
no_access_to_hosts: '没有写入 Hosts 文件的权限。',
no_record: '没有记录',
password: '密码',
paste: '粘贴',
port: '端口',
preferences: '选项',
previous: '上一个',
protocol: '协议',
proxy: '代理',
quit: '退出',
Expand Down
25 changes: 18 additions & 7 deletions src/common/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ export interface IPopupMenuOption {
items: IMenuItemOption[];
}

export interface IFindResultItem {
item_title: string;
item_id: string;
item_type: HostsType;
export interface IFindPosition {
start: number;
end: number;
line: number;
Expand All @@ -37,8 +34,22 @@ export interface IFindResultItem {
after: string;
}

export type IFindShowSourceParam = Pick<IFindResultItem,
'item_id' | 'start' | 'end' | 'line' | 'line_pos'
| 'end_line' | 'end_line_pos'> & {
export interface IFindSpliter {
before: string;
match: string;
after: string;
replace?: string;
}

export interface IFindItem {
item_id: string;
item_title: string;
item_type: HostsType;
positions: IFindPosition[];
spliters: IFindSpliter[];
}

export type IFindShowSourceParam = IFindPosition & {
item_id: string;
[key: string]: any;
}
19 changes: 11 additions & 8 deletions src/main/actions/find/findBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@
* @homepage: https://oldj.net
*/

import splitContent from '@main/actions/find/splitContent'
import getContentOfHosts from '@main/actions/hosts/getContent'
import { flatten } from '@root/common/hostsFn'
import { IFindResultItem } from '@root/common/types'
import { IFindItem } from '@root/common/types'
import findInContent from 'src/main/actions/find/findPositionsInContent'
import { getList } from '../index'
import findInContent from './findInContent'

export interface IFindOptions {
is_regexp: boolean;
is_ignore_case: boolean;
}

export default async (keyword: string, options: IFindOptions): Promise<IFindResultItem[]> => {
export default async (keyword: string, options: IFindOptions): Promise<IFindItem[]> => {
console.log(keyword)
let result_items: IFindResultItem[] = []
let result_items: IFindItem[] = []

let tree = await getList()
let items = flatten(tree)
Expand All @@ -35,13 +36,15 @@ export default async (keyword: string, options: IFindOptions): Promise<IFindResu
continue
}
let content = await getContentOfHosts(item.id)
let found = findInContent(content, exp)
result_items = [...result_items, ...found.map(i => ({
...i,
let positions = findInContent(content, exp)

result_items.push({
item_title: item.title || '',
item_id: item.id,
item_type,
}))]
positions,
spliters: splitContent(content, positions),
})
}

return result_items
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
* @homepage: https://oldj.net
*/

import { IFindResultItem } from '@root/common/types'
import { IFindPosition } from '@root/common/types'

type MatchResult = Pick<IFindResultItem, 'start' | 'end' | 'before' | 'match' | 'after' | 'line' | 'line_pos' | 'end_line' | 'end_line_pos'>
type MatchResult = Pick<IFindPosition, 'start' | 'end' | 'before' | 'match' | 'after' | 'line' | 'line_pos' | 'end_line' | 'end_line_pos'>

export default (content: string, exp: RegExp): MatchResult[] => {
let result_items: MatchResult[] = []
Expand Down
34 changes: 34 additions & 0 deletions src/main/actions/find/splitContent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @author: oldj
* @homepage: https://oldj.net
*/

import { IFindPosition, IFindSpliter } from '@root/common/types'

type MatchResult = Pick<IFindPosition, 'start' | 'end' | 'match'> & {
[key: string]: any;
}

export default (content: string, find_results: MatchResult[]): IFindSpliter[] => {
let spliters: IFindSpliter[] = []

let last_end = 0
find_results.map((r, idx) => {
let { start, match } = r
let before = content.slice(last_end, start)
let after = ''

last_end += before.length + match.length
if (idx === find_results.length - 1) {
after = content.slice(last_end)
}

let spliter: IFindSpliter = {
before, after, match,
}

spliters.push(spliter)
})

return spliters
}
12 changes: 11 additions & 1 deletion src/renderer/components/Editor/HostsEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,16 @@ const HostsEditor = (props: Props) => {
})

// console.log(doc.getSelection())
await wait(200)
if (!doc.getSelection()) {
await wait(200)
await setSelection(params)
}
cm_editor.focus()
}

const replaceOne = async (params: IFindShowSourceParam) => {
if (!cm_editor) return
let doc = cm_editor.getDoc()
}

useOnBroadcast('show_source', async (params: IFindShowSourceParam) => {
Expand All @@ -182,6 +188,10 @@ const HostsEditor = (props: Props) => {
setSelection(params)
}, [hosts, cm_editor])

useOnBroadcast('replace_one', async (params: IFindShowSourceParam) => {
if (!cm_editor) return
}, [hosts, cm_editor])

return (
<div className={styles.root}>
<div
Expand Down
4 changes: 4 additions & 0 deletions src/renderer/pages/find.less
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
padding-left: 8px;
cursor: pointer;
user-select: none;

&.selected {
background: var(--swh-tree-selected-bg);
}
}

.result_content {
Expand Down
Loading

0 comments on commit 65aa746

Please sign in to comment.