diff --git a/src/common/default_configs.ts b/src/common/default_configs.ts index 9fa3c91e8..b9984cdd6 100644 --- a/src/common/default_configs.ts +++ b/src/common/default_configs.ts @@ -30,6 +30,7 @@ const configs = { http_api_on: false, http_api_only_local: true, tray_mini_window: true, + multi_chose_folder_switch_all: false, // other env: 'PROD' as 'PROD' | 'DEV', diff --git a/src/common/hostsFn.ts b/src/common/hostsFn.ts index 65af0f3f9..223a5fcb9 100644 --- a/src/common/hostsFn.ts +++ b/src/common/hostsFn.ts @@ -77,6 +77,7 @@ export const setOnStateOfItem = ( id: string, on: boolean, default_choice_mode: FolderModeType = 0, + multi_chose_folder_switch_all: boolean = false ): IHostsListObject[] => { let new_list: IHostsListObject[] = lodash.cloneDeep(list) @@ -85,13 +86,24 @@ export const setOnStateOfItem = ( item.on = on - if (!on) return new_list + let itemIsInTopLevel = isInTopLevel(list, id); + if (multi_chose_folder_switch_all) { + item = switchFolderChild(item, on) + !itemIsInTopLevel && switchItemParentIsON(new_list, item, on) + } + + if (!on) { + return new_list + } - if (isInTopLevel(list, id)) { + if (itemIsInTopLevel) { if (default_choice_mode === 1) { new_list.map((item) => { if (item.id !== id) { item.on = false + if (multi_chose_folder_switch_all) { + item = switchFolderChild(item, false) + } } }) } @@ -104,6 +116,9 @@ export const setOnStateOfItem = ( parent.children.map((item) => { if (item.id !== id) { item.on = false + if (multi_chose_folder_switch_all) { + item = switchFolderChild(item, false) + } } }) } @@ -113,6 +128,60 @@ export const setOnStateOfItem = ( return new_list } +export const switchItemParentIsON = ( + list: IHostsListObject[], + item: IHostsListObject, + on: boolean +) => { + let parent = getParentOfItem(list, item.id) + + if (parent) { + if (parent.folder_mode === 1) { + return + } + if (!on) { + parent.on = on + } else if (parent.children) { + let parentOn = true + parent.children.forEach((item) => { + if (!item.on) { + parentOn = false + } + }) + parent.on = parentOn + } + + let itemIsInTopLevel = isInTopLevel(list, parent.id) + if (!itemIsInTopLevel) { + switchItemParentIsON(list, parent, on) + } + } +} + +export const switchFolderChild = ( + item: IHostsListObject, + on: boolean, +): IHostsListObject => { + if (item.type != 'folder') { + return item + } + let folder_mode = item.folder_mode + if (folder_mode === 1) { + return item + } + + if (item.children) { + item.children.forEach((item) => { + item.on = on + if (item.type == 'folder') { + item = switchFolderChild(item, on) + } + }) + } + + return item; +} + export const deleteItemById = (list: IHostsListObject[], id: string) => { let idx = list.findIndex((item) => item.id === id) if (idx >= 0) { diff --git a/src/common/i18n/languages/de.ts b/src/common/i18n/languages/de.ts index 89490f89a..19a960cd7 100644 --- a/src/common/i18n/languages/de.ts +++ b/src/common/i18n/languages/de.ts @@ -77,6 +77,7 @@ 'Läuft auf Port {0}, kann von Software von Drittanbietern wie Alfred verwendet werden, um den Host zu wechseln.', http_api_only_local: 'HTTP-API hört nur auf 127.0.0.1', tray_mini_window: 'Taskleistensymbol-Verknüpfung', + multi_chose_folder_switch_all: 'Mehrfachauswahl-Ordnerschalter zur Steuerung von Unterelementen', ignore_case: 'Groß- und Kleinschreibung ignorieren', import: 'Importieren', import_done: 'Der Import ist abgeschlossen.', diff --git a/src/common/i18n/languages/en.ts b/src/common/i18n/languages/en.ts index 12688e069..f082e27d3 100644 --- a/src/common/i18n/languages/en.ts +++ b/src/common/i18n/languages/en.ts @@ -77,6 +77,7 @@ export default { 'Runs on port {0}, can be used by third-party software such as Alfred to switch hosts.', http_api_only_local: 'HTTP API only listen 127.0.0.1', tray_mini_window: 'taskbar icon shortcut', + multi_chose_folder_switch_all: 'multi-select folder switch to control sub-items', ignore_case: 'Ignore case', import: 'Import', import_done: 'The import is complete.', diff --git a/src/common/i18n/languages/fr.ts b/src/common/i18n/languages/fr.ts index de06c76c2..ecf07ec41 100644 --- a/src/common/i18n/languages/fr.ts +++ b/src/common/i18n/languages/fr.ts @@ -77,6 +77,7 @@ export default { "Actif sur le port {0}, peut être utilisé par un logiciel tier comme Alfred pour changer d'hosts", http_api_only_local: "L'API HTTP n'écoute que sur 127.0.0.1", tray_mini_window: 'raccourci de l\'icône de la barre des tâches', + multi_chose_folder_switch_all: 'Commutateur de dossier à sélection multiple pour contrôler les sous-éléments', ignore_case: 'Ignorer la casse', import: 'Importer', import_done: "L'importation est terminée", diff --git a/src/common/i18n/languages/zh.ts b/src/common/i18n/languages/zh.ts index 928647fd3..33c865079 100644 --- a/src/common/i18n/languages/zh.ts +++ b/src/common/i18n/languages/zh.ts @@ -76,6 +76,7 @@ const lang: LanguageDict = { http_api_on_desc: '运行于 {0} 端口,可用于 Alfred 等第三方软件切换 hosts。', http_api_only_local: 'HTTP API 仅监听 127.0.0.1', tray_mini_window: '任务栏快捷小窗', + multi_chose_folder_switch_all: '多选文件夹开关控制子项目', ignore_case: '忽略大小写', import: '导入', import_done: '导入已完成。', diff --git a/src/main/actions/hosts/getContent.ts b/src/main/actions/hosts/getContent.ts index b9ce73307..6ef90d532 100644 --- a/src/main/actions/hosts/getContent.ts +++ b/src/main/actions/hosts/getContent.ts @@ -3,7 +3,7 @@ * @homepage: https://oldj.net */ -import { getItemFromList, getList } from '@main/actions' +import { configGet, getItemFromList, getList } from '@main/actions' import { swhdb } from '@main/data' import { IHostsContentObject } from '@root/common/data' import { findItemById, flatten } from '@root/common/hostsFn' @@ -28,7 +28,10 @@ const getContentOfHosts = async (id: string): Promise => { let list = await getList() - if (type === 'folder') { + let multi_chose_folder_switch_all = await configGet('multi_chose_folder_switch_all'); + let isSkipFolder = multi_chose_folder_switch_all && hosts.folder_mode !== 1 + + if (type === 'folder' && !isSkipFolder) { const items = flatten(hosts.children || []) let a = await Promise.all( diff --git a/src/renderer/components/List/index.tsx b/src/renderer/components/List/index.tsx index 174afe541..8f320895c 100644 --- a/src/renderer/components/List/index.tsx +++ b/src/renderer/components/List/index.tsx @@ -70,6 +70,7 @@ const List = (props: Props) => { id, on, configs?.choice_mode ?? 0, + configs?.multi_chose_folder_switch_all ?? false, ) let success = await writeHostsToSystem(new_list) if (success) { diff --git a/src/renderer/components/Pref/General.tsx b/src/renderer/components/Pref/General.tsx index b78daadfe..685cbb9c2 100644 --- a/src/renderer/components/Pref/General.tsx +++ b/src/renderer/components/Pref/General.tsx @@ -196,6 +196,19 @@ const General = (props: IProps) => { + + + + onChange({ multi_chose_folder_switch_all: e.target.checked }) + } + > + {lang.multi_chose_folder_switch_all} + + + +