瀏覽代碼

Merge pull request #704 from ClDaniel1/multi_chose_folder_switch_all

Multi chose folder switch all
oldj 3 年之前
父節點
當前提交
29ef79b027

+ 1 - 0
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',

+ 71 - 2
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) {

+ 1 - 0
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.',

+ 1 - 0
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.',

+ 1 - 0
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",

+ 1 - 0
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: '导入已完成。',

+ 5 - 2
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<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(

+ 1 - 0
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) {

+ 13 - 0
src/renderer/components/Pref/General.tsx

@@ -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