Skip to content

Commit 29ef79b

Browse files
authored
Merge pull request #704 from ClDaniel1/multi_chose_folder_switch_all
Multi chose folder switch all
2 parents 8cef451 + 1d71f86 commit 29ef79b

File tree

9 files changed

+95
-4
lines changed

9 files changed

+95
-4
lines changed

src/common/default_configs.ts

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const configs = {
3030
http_api_on: false,
3131
http_api_only_local: true,
3232
tray_mini_window: true,
33+
multi_chose_folder_switch_all: false,
3334

3435
// other
3536
env: 'PROD' as 'PROD' | 'DEV',

src/common/hostsFn.ts

+71-2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export const setOnStateOfItem = (
7777
id: string,
7878
on: boolean,
7979
default_choice_mode: FolderModeType = 0,
80+
multi_chose_folder_switch_all: boolean = false
8081
): IHostsListObject[] => {
8182
let new_list: IHostsListObject[] = lodash.cloneDeep(list)
8283

@@ -85,13 +86,24 @@ export const setOnStateOfItem = (
8586

8687
item.on = on
8788

88-
if (!on) return new_list
89+
let itemIsInTopLevel = isInTopLevel(list, id);
90+
if (multi_chose_folder_switch_all) {
91+
item = switchFolderChild(item, on)
92+
!itemIsInTopLevel && switchItemParentIsON(new_list, item, on)
93+
}
94+
95+
if (!on) {
96+
return new_list
97+
}
8998

90-
if (isInTopLevel(list, id)) {
99+
if (itemIsInTopLevel) {
91100
if (default_choice_mode === 1) {
92101
new_list.map((item) => {
93102
if (item.id !== id) {
94103
item.on = false
104+
if (multi_chose_folder_switch_all) {
105+
item = switchFolderChild(item, false)
106+
}
95107
}
96108
})
97109
}
@@ -104,6 +116,9 @@ export const setOnStateOfItem = (
104116
parent.children.map((item) => {
105117
if (item.id !== id) {
106118
item.on = false
119+
if (multi_chose_folder_switch_all) {
120+
item = switchFolderChild(item, false)
121+
}
107122
}
108123
})
109124
}
@@ -113,6 +128,60 @@ export const setOnStateOfItem = (
113128
return new_list
114129
}
115130

131+
export const switchItemParentIsON = (
132+
list: IHostsListObject[],
133+
item: IHostsListObject,
134+
on: boolean
135+
) => {
136+
let parent = getParentOfItem(list, item.id)
137+
138+
if (parent) {
139+
if (parent.folder_mode === 1) {
140+
return
141+
}
142+
if (!on) {
143+
parent.on = on
144+
} else if (parent.children) {
145+
let parentOn = true
146+
parent.children.forEach((item) => {
147+
if (!item.on) {
148+
parentOn = false
149+
}
150+
})
151+
parent.on = parentOn
152+
}
153+
154+
let itemIsInTopLevel = isInTopLevel(list, parent.id)
155+
if (!itemIsInTopLevel) {
156+
switchItemParentIsON(list, parent, on)
157+
}
158+
}
159+
}
160+
161+
export const switchFolderChild = (
162+
item: IHostsListObject,
163+
on: boolean,
164+
): IHostsListObject => {
165+
if (item.type != 'folder') {
166+
return item
167+
}
168+
let folder_mode = item.folder_mode
169+
if (folder_mode === 1) {
170+
return item
171+
}
172+
173+
if (item.children) {
174+
item.children.forEach((item) => {
175+
item.on = on
176+
if (item.type == 'folder') {
177+
item = switchFolderChild(item, on)
178+
}
179+
})
180+
}
181+
182+
return item;
183+
}
184+
116185
export const deleteItemById = (list: IHostsListObject[], id: string) => {
117186
let idx = list.findIndex((item) => item.id === id)
118187
if (idx >= 0) {

src/common/i18n/languages/de.ts

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
'Läuft auf Port {0}, kann von Software von Drittanbietern wie Alfred verwendet werden, um den Host zu wechseln.',
7878
http_api_only_local: 'HTTP-API hört nur auf 127.0.0.1',
7979
tray_mini_window: 'Taskleistensymbol-Verknüpfung',
80+
multi_chose_folder_switch_all: 'Mehrfachauswahl-Ordnerschalter zur Steuerung von Unterelementen',
8081
ignore_case: 'Groß- und Kleinschreibung ignorieren',
8182
import: 'Importieren',
8283
import_done: 'Der Import ist abgeschlossen.',

src/common/i18n/languages/en.ts

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export default {
7777
'Runs on port {0}, can be used by third-party software such as Alfred to switch hosts.',
7878
http_api_only_local: 'HTTP API only listen 127.0.0.1',
7979
tray_mini_window: 'taskbar icon shortcut',
80+
multi_chose_folder_switch_all: 'multi-select folder switch to control sub-items',
8081
ignore_case: 'Ignore case',
8182
import: 'Import',
8283
import_done: 'The import is complete.',

src/common/i18n/languages/fr.ts

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export default {
7777
"Actif sur le port {0}, peut être utilisé par un logiciel tier comme Alfred pour changer d'hosts",
7878
http_api_only_local: "L'API HTTP n'écoute que sur 127.0.0.1",
7979
tray_mini_window: 'raccourci de l\'icône de la barre des tâches',
80+
multi_chose_folder_switch_all: 'Commutateur de dossier à sélection multiple pour contrôler les sous-éléments',
8081
ignore_case: 'Ignorer la casse',
8182
import: 'Importer',
8283
import_done: "L'importation est terminée",

src/common/i18n/languages/zh.ts

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ const lang: LanguageDict = {
7676
http_api_on_desc: '运行于 {0} 端口,可用于 Alfred 等第三方软件切换 hosts。',
7777
http_api_only_local: 'HTTP API 仅监听 127.0.0.1',
7878
tray_mini_window: '任务栏快捷小窗',
79+
multi_chose_folder_switch_all: '多选文件夹开关控制子项目',
7980
ignore_case: '忽略大小写',
8081
import: '导入',
8182
import_done: '导入已完成。',

src/main/actions/hosts/getContent.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @homepage: https://oldj.net
44
*/
55

6-
import { getItemFromList, getList } from '@main/actions'
6+
import { configGet, getItemFromList, getList } from '@main/actions'
77
import { swhdb } from '@main/data'
88
import { IHostsContentObject } from '@root/common/data'
99
import { findItemById, flatten } from '@root/common/hostsFn'
@@ -28,7 +28,10 @@ const getContentOfHosts = async (id: string): Promise<string> => {
2828

2929
let list = await getList()
3030

31-
if (type === 'folder') {
31+
let multi_chose_folder_switch_all = await configGet('multi_chose_folder_switch_all');
32+
let isSkipFolder = multi_chose_folder_switch_all && hosts.folder_mode !== 1
33+
34+
if (type === 'folder' && !isSkipFolder) {
3235
const items = flatten(hosts.children || [])
3336

3437
let a = await Promise.all(

src/renderer/components/List/index.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ const List = (props: Props) => {
7070
id,
7171
on,
7272
configs?.choice_mode ?? 0,
73+
configs?.multi_chose_folder_switch_all ?? false,
7374
)
7475
let success = await writeHostsToSystem(new_list)
7576
if (success) {

src/renderer/components/Pref/General.tsx

+13
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,19 @@ const General = (props: IProps) => {
196196
</VStack>
197197
</FormControl>
198198

199+
<FormControl>
200+
<VStack align="left">
201+
<Checkbox
202+
isChecked={data.multi_chose_folder_switch_all}
203+
onChange={(e) =>
204+
onChange({ multi_chose_folder_switch_all: e.target.checked })
205+
}
206+
>
207+
{lang.multi_chose_folder_switch_all}
208+
</Checkbox>
209+
</VStack>
210+
</FormControl>
211+
199212
<FormControl>
200213
<VStack align="left">
201214
<Checkbox

0 commit comments

Comments
 (0)