@@ -3,7 +3,8 @@ import React from "react";
33import { useTranslation } from "react-i18next" ;
44import KeyBind from "../../components/KeyBind" ;
55import { KeyBinds } from "../../core/service/controlService/shortcutKeysEngine/KeyBinds" ;
6- import { Field } from "./_field" ;
6+ import { Field , FieldGroup } from "./_field" ;
7+ import { shortcutKeysGroups } from "../../core/service/controlService/shortcutKeysEngine/shortcutKeysGroup" ;
78
89export default function KeyBindsPage ( ) {
910 const [ keyBinds , setKeyBinds ] = React . useState < [ id : string , { key : string ; modifiers : KeyBinds . KeyModifiers } ] [ ] > ( [ ] ) ;
@@ -14,29 +15,94 @@ export default function KeyBindsPage() {
1415 } ) ;
1516 } , [ ] ) ;
1617
18+ const getKeybindObjectById = ( id : string ) => {
19+ return keyBinds . find ( ( item ) => item [ 0 ] === id ) ?. [ 1 ] ;
20+ } ;
21+
22+ /**
23+ * 获取未加入分组的快捷键
24+ * @returns
25+ */
26+ const getUnGroupedKeys = ( ) => {
27+ return keyBinds . filter ( ( item ) => ! shortcutKeysGroups . some ( ( group ) => group . keys . includes ( item [ 0 ] ) ) ) ;
28+ } ;
29+
1730 const { t } = useTranslation ( "keyBinds" ) ;
1831
19- return keyBinds
20- . map ( ( [ id , bind ] ) => (
21- < Field
22- key = { id }
23- icon = { < Keyboard /> }
24- title = { t ( `${ id } .title` , { defaultValue : id } ) }
25- description = { t ( `${ id } .description` , { defaultValue : "" } ) }
26- >
27- < KeyBind
28- value = { bind }
29- onChange = { ( value ) => {
30- KeyBinds . set ( id , value . key , value . modifiers ) ;
31- setKeyBinds ( ( prev ) => prev . map ( ( item ) => ( item [ 0 ] === id ? [ item [ 0 ] , value ] : item ) ) ) ;
32- } }
33- />
34- </ Field >
35- ) )
36- . sort ( ( a , b ) => {
37- if ( a . key === null && b . key === null ) return 0 ; // 两者均为 null,相等
38- if ( a . key === null ) return 1 ; // a.key 为 null,把它排到后面
39- if ( b . key === null ) return - 1 ; // b.key 为 null,把它排到后面
40- return a . key . localeCompare ( b . key ) ; // 正常比较
41- } ) ;
32+ return (
33+ < >
34+ { shortcutKeysGroups . map ( ( group , i ) => {
35+ console . log ( group ) ;
36+ return (
37+ < FieldGroup title = { t ( group . title ) } key = { i } >
38+ { group . keys . map ( ( id ) => (
39+ < Field
40+ key = { id }
41+ icon = { < Keyboard /> }
42+ title = { t ( `${ id } .title` , { defaultValue : id } ) }
43+ description = { t ( `${ id } .description` , { defaultValue : "" } ) }
44+ >
45+ < KeyBind
46+ value = { getKeybindObjectById ( id ) }
47+ onChange = { ( value ) => {
48+ KeyBinds . set ( id , value . key , value . modifiers ) ;
49+ setKeyBinds ( ( prev ) => prev . map ( ( item ) => ( item [ 0 ] === id ? [ item [ 0 ] , value ] : item ) ) ) ;
50+ } }
51+ />
52+ </ Field >
53+ ) ) }
54+ </ FieldGroup >
55+ ) ;
56+ } ) }
57+ < FieldGroup title = { t ( "otherKeys" ) } description = { "未分组的快捷键" } >
58+ { getUnGroupedKeys ( )
59+ . map ( ( [ id , bind ] ) => (
60+ < Field
61+ key = { id }
62+ icon = { < Keyboard /> }
63+ title = { t ( `${ id } .title` , { defaultValue : id } ) }
64+ description = { t ( `${ id } .description` , { defaultValue : "" } ) }
65+ >
66+ < KeyBind
67+ value = { bind }
68+ onChange = { ( value ) => {
69+ KeyBinds . set ( id , value . key , value . modifiers ) ;
70+ setKeyBinds ( ( prev ) => prev . map ( ( item ) => ( item [ 0 ] === id ? [ item [ 0 ] , value ] : item ) ) ) ;
71+ } }
72+ />
73+ </ Field >
74+ ) )
75+ . sort ( ( a , b ) => {
76+ if ( a . key === null && b . key === null ) return 0 ; // 两者均为 null,相等
77+ if ( a . key === null ) return 1 ; // a.key 为 null,把它排到后面
78+ if ( b . key === null ) return - 1 ; // b.key 为 null,把它排到后面
79+ return a . key . localeCompare ( b . key ) ; // 正常比较
80+ } ) }
81+ </ FieldGroup >
82+ { /* <h1>{t("title")}</h1>
83+ {keyBinds
84+ .map(([id, bind]) => (
85+ <Field
86+ key={id}
87+ icon={<Keyboard />}
88+ title={t(`${id}.title`, { defaultValue: id })}
89+ description={t(`${id}.description`, { defaultValue: "" })}
90+ >
91+ <KeyBind
92+ value={bind}
93+ onChange={(value) => {
94+ KeyBinds.set(id, value.key, value.modifiers);
95+ setKeyBinds((prev) => prev.map((item) => (item[0] === id ? [item[0], value] : item)));
96+ }}
97+ />
98+ </Field>
99+ ))
100+ .sort((a, b) => {
101+ if (a.key === null && b.key === null) return 0; // 两者均为 null,相等
102+ if (a.key === null) return 1; // a.key 为 null,把它排到后面
103+ if (b.key === null) return -1; // b.key 为 null,把它排到后面
104+ return a.key.localeCompare(b.key); // 正常比较
105+ }) } */ }
106+ </ >
107+ ) ;
42108}
0 commit comments