Skip to content

Commit

Permalink
Merge pull request #704 from ClDaniel1/multi_chose_folder_switch_all
Browse files Browse the repository at this point in the history
Multi chose folder switch all
  • Loading branch information
oldj authored Apr 17, 2022
2 parents 8cef451 + 1d71f86 commit 29ef79b
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/common/default_configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
73 changes: 71 additions & 2 deletions src/common/hostsFn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
}
}
})
}
Expand All @@ -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)
}
}
})
}
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/common/i18n/languages/de.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand Down
1 change: 1 addition & 0 deletions src/common/i18n/languages/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand Down
1 change: 1 addition & 0 deletions src/common/i18n/languages/fr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/common/i18n/languages/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '导入已完成。',
Expand Down
7 changes: 5 additions & 2 deletions src/main/actions/hosts/getContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -28,7 +28,10 @@ const getContentOfHosts = async (id: string): Promise<string> => {

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(
Expand Down
1 change: 1 addition & 0 deletions src/renderer/components/List/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 13 additions & 0 deletions src/renderer/components/Pref/General.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,19 @@ const General = (props: IProps) => {
</VStack>
</FormControl>

<FormControl>
<VStack align="left">
<Checkbox
isChecked={data.multi_chose_folder_switch_all}
onChange={(e) =>
onChange({ multi_chose_folder_switch_all: e.target.checked })
}
>
{lang.multi_chose_folder_switch_all}
</Checkbox>
</VStack>
</FormControl>

<FormControl>
<VStack align="left">
<Checkbox
Expand Down

0 comments on commit 29ef79b

Please sign in to comment.