@@ -3,51 +3,65 @@ import "../components/settingsItem";
33// eslint-disable-next-line import-x/no-unassigned-import
44import "../components/settingsDescription" ;
55// eslint-disable-next-line import-x/no-unassigned-import
6- import "../components/settingsSeparator" ;
6+ import "../components/settingsGroupTitle" ;
7+ import { SETTINGS_ITEMS , type SettingsData , type SettingsItemData } from "./settingsItems" ;
78import { i18n , runtime } from "webextension-polyfill" ;
89import { loadSettingsFromStorage , writeSettingsToStorage } from "../common/settings" ;
9- import { SETTINGS_ITEMS } from "./settingsItems" ;
1010import type { Settings } from "../../types/common/settings" ;
1111// eslint-disable-next-line no-duplicate-imports
1212import type { SettingsDescription } from "../components/settingsDescription" ;
1313// eslint-disable-next-line no-duplicate-imports
14- import type { SettingsItem } from "../components/settingsItem " ;
14+ import type { SettingsGroupTitle } from "../components/settingsGroupTitle " ;
1515// eslint-disable-next-line no-duplicate-imports
16- import type { SettingsSeparator } from "../components/settingsSeparator " ;
16+ import type { SettingsItem } from "../components/settingsItem " ;
1717import { TRANSLATION_ATTRIBUTE } from "../common/constants" ;
1818import { Translator } from "../common/translator" ;
1919
2020/**
21- * Create new settings separator element.
21+ * Create new settings group title element.
2222 * @param translationKey translation key
2323 * @returns created settings separator
2424 */
25- const createSettingsSeparator = ( translationKey : string ) : SettingsSeparator => {
26- const separator = document . createElement ( "settings-separator " ) ;
25+ const createGroupTitle = ( translationKey : string ) : SettingsGroupTitle => {
26+ const separator = document . createElement ( "settings-group-title " ) ;
2727 separator . setAttribute ( TRANSLATION_ATTRIBUTE , translationKey ) ;
2828
2929 return separator ;
3030} ;
3131
32- const createSettingsDescription = ( translationKey : string ) : SettingsDescription => {
32+ /**
33+ * Create new settings description element.
34+ * @param translationKey translation key
35+ * @returns created settings description
36+ */
37+ const createGroupDescription = ( translationKey : string ) : SettingsDescription => {
3338 const description = document . createElement ( "settings-description" ) ;
3439 description . setAttribute ( TRANSLATION_ATTRIBUTE , translationKey ) ;
3540
3641 return description ;
3742} ;
3843
3944/**
40- * Create new settings item element.
41- * @param settingsName settings name
42- * @param translationKey translation key
43- * @param checked settings status
44- * @returns created settings item
45+ * Create a new settings item element.
46+ * @param settingsItemData Settings item data
47+ * @param checked Settings status
48+ * @param isFirstItem Whether this is the first item
49+ * @param isLastItem Whether this is the last item
50+ * @returns The created settings item
4551 */
46- const createSettingsItem = ( settingsName : string , translationKey : string , checked : boolean ) : SettingsItem => {
52+ const createSettingsItem = (
53+ settingsItemData : SettingsItemData ,
54+ checked : boolean ,
55+ isFirstItem : boolean ,
56+ isLastItem : boolean
57+ // eslint-disable-next-line max-params
58+ ) : SettingsItem => {
4759 const item = document . createElement ( "settings-item" ) ;
48- item . settingsName = settingsName ;
49- item . setAttribute ( TRANSLATION_ATTRIBUTE , translationKey ) ;
60+ item . settingsName = settingsItemData . settingsName ;
61+ item . setAttribute ( TRANSLATION_ATTRIBUTE , settingsItemData . translationKey ) ;
5062 item . checked = checked ;
63+ item . isFirstItem = isFirstItem ;
64+ item . isLastItem = isLastItem ;
5165 item . addEventListener ( "change" , ( ) => {
5266 void writeSettingsToStorage ( { [ item . settingsName ] : item . checked } ) ;
5367 } ) ;
@@ -64,28 +78,28 @@ const runTranslation = (): void => {
6478} ;
6579
6680/**
67- * Create settings item or separator element from data .
81+ * Create settings items from group .
6882 * @param settings settings
69- * @param data settings item data
70- * @returns created element
83+ * @param group settings item group
84+ * @returns created elements
7185 */
72- const createItemsFromData = (
73- settings : Settings ,
74- data : ( typeof SETTINGS_ITEMS ) [ number ]
75- ) : SettingsSeparator | SettingsDescription | SettingsItem => {
76- switch ( data . type ) {
77- case "separator" :
78- return createSettingsSeparator ( data . translationKey ) ;
79-
80- case "description" :
81- return createSettingsDescription ( data . translationKey ) ;
82-
83- case "checkbox" :
84- return createSettingsItem ( data . settingsName , data . translationKey , settings [ data . settingsName ] ) ;
86+ const createItemsFromGroup = ( settings : Settings , group : SettingsData [ number ] ) : DocumentFragment => {
87+ const fragment = document . createDocumentFragment ( ) ;
88+ fragment . appendChild ( createGroupTitle ( group . groupName ) ) ;
89+ if ( group . description ) {
90+ fragment . appendChild ( createGroupDescription ( group . description ) ) ;
91+ }
8592
86- default :
87- throw new Error ( "Invalid type" ) ;
93+ for ( const [ index , item ] of group . items . entries ( ) ) {
94+ // eslint-disable-next-line no-magic-numbers
95+ const isFirstItem = index === 0 ;
96+ // eslint-disable-next-line no-magic-numbers
97+ const isLastItem = index === group . items . length - 1 ;
98+ const settingsItem = createSettingsItem ( item , settings [ item . settingsName ] , isFirstItem , isLastItem ) ;
99+ fragment . appendChild ( settingsItem ) ;
88100 }
101+
102+ return fragment ;
89103} ;
90104
91105/**
@@ -98,8 +112,8 @@ const loadSettings = async (): Promise<void> => {
98112 if ( ! fieldset ) throw new Error ( "Failed to get fieldset" ) ;
99113 const fragment = document . createDocumentFragment ( ) ;
100114
101- for ( const data of SETTINGS_ITEMS ) {
102- const element = createItemsFromData ( settings , data ) ;
115+ for ( const settingsGroup of SETTINGS_ITEMS ) {
116+ const element = createItemsFromGroup ( settings , settingsGroup ) ;
103117 fragment . appendChild ( element ) ;
104118 }
105119
0 commit comments