From dfaa9e2722ab765bee7caed67a229797d6bb055c Mon Sep 17 00:00:00 2001 From: GooseOb Date: Thu, 12 Dec 2024 02:10:52 +0100 Subject: [PATCH] [skip ci] reduce global types --- src/compute-settings.ts | 2 +- src/config/index.ts | 1 + src/config/save.ts | 1 + src/config/types.ts | 26 +++++++++++++++++++ src/config/update.ts | 2 ++ src/config/value.ts | 2 ++ src/menu/controls.ts | 2 ++ src/menu/get-controls-creators.ts | 2 ++ src/menu/init.ts | 2 +- src/menu/section.ts | 1 + src/menu/types.ts | 4 +++ src/player/apply-settings.ts | 1 + src/player/plr.ts | 1 + src/player/types.ts | 2 ++ src/player/value-setters.ts | 7 +++--- src/types.d.ts | 14 +++++++++++ src/types.ts | 42 ------------------------------- 17 files changed, 65 insertions(+), 47 deletions(-) create mode 100644 src/config/types.ts create mode 100644 src/menu/types.ts create mode 100644 src/player/types.ts create mode 100644 src/types.d.ts delete mode 100644 src/types.ts diff --git a/src/compute-settings.ts b/src/compute-settings.ts index 9463a99..a3d5b3f 100644 --- a/src/compute-settings.ts +++ b/src/compute-settings.ts @@ -9,7 +9,7 @@ import { plr } from './player'; * 3. Global custom speed * 4. Global speed */ -export const computeSettings = (doUseNormalSpeed: boolean): Cfg => { +export const computeSettings = (doUseNormalSpeed: boolean): config.Cfg => { const channel = config.channel.get(); const settings = { ...config.value.global, diff --git a/src/config/index.ts b/src/config/index.ts index 23b73d9..0b48779 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -3,3 +3,4 @@ export * from './save'; export * from './prune'; export * from './value'; export * from './current-channel'; +export type * from './types'; diff --git a/src/config/save.ts b/src/config/save.ts index 923aa3b..391771a 100644 --- a/src/config/save.ts +++ b/src/config/save.ts @@ -1,3 +1,4 @@ +import type { ScriptCfg } from './types'; import { update } from './update'; import { value } from './value'; diff --git a/src/config/types.ts b/src/config/types.ts new file mode 100644 index 0000000..0467ec0 --- /dev/null +++ b/src/config/types.ts @@ -0,0 +1,26 @@ +export type FlagName = + | 'shortsToUsual' + | 'newTab' + | 'copySubs' + | 'standardMusicSpeed' + | 'enhancedBitrate'; + +type YtCfg = { + speed?: string; + quality?: string; + volume?: string; + subtitles?: boolean; +}; + +export type Cfg = YtCfg & { + customSpeed?: string; +}; + +export type Setting = keyof Cfg; + +export type ScriptCfg = { + _v: number; + global: Cfg; + channels: Record; + flags: Record; +}; diff --git a/src/config/update.ts b/src/config/update.ts index 1b04a93..1ce3d49 100644 --- a/src/config/update.ts +++ b/src/config/update.ts @@ -1,3 +1,5 @@ +import type { ScriptCfg } from './types'; + /** * @returns if the cfg was updated */ diff --git a/src/config/value.ts b/src/config/value.ts index add1e20..1658116 100644 --- a/src/config/value.ts +++ b/src/config/value.ts @@ -1,3 +1,5 @@ +import type { ScriptCfg } from './types'; + const cfgLocalStorage = localStorage[STORAGE_NAME]; export const value: ScriptCfg = cfgLocalStorage diff --git a/src/menu/controls.ts b/src/menu/controls.ts index eed8f60..7f947aa 100644 --- a/src/menu/controls.ts +++ b/src/menu/controls.ts @@ -1,4 +1,6 @@ import { text } from '../text'; +import type { SettingControls } from './types'; +import type { Cfg, FlagName, ScriptCfg } from '../config'; const updateValuesIn = (controls: SettingControls, cfgPart: Readonly) => { controls[SPEED].value = cfgPart[SPEED] || text.DEFAULT; diff --git a/src/menu/get-controls-creators.ts b/src/menu/get-controls-creators.ts index 9769f56..f390076 100644 --- a/src/menu/get-controls-creators.ts +++ b/src/menu/get-controls-creators.ts @@ -1,5 +1,7 @@ import { checkbox, input, option, selectEl } from '../utils/element-creators'; import { text } from '../text'; +import type { SettingControls } from './types'; +import type { Setting } from '../config'; type GetControlCreator = < TSetting extends Setting, diff --git a/src/menu/init.ts b/src/menu/init.ts index 9898c91..02fb622 100644 --- a/src/menu/init.ts +++ b/src/menu/init.ts @@ -17,7 +17,7 @@ import * as get from '../element-getters'; const controlCheckboxDiv = ( id: string, - flagName: FlagName, + flagName: config.FlagName, textContent: string ): HTMLDivElement => { const cont = div({ className: 'check-cont' }); diff --git a/src/menu/section.ts b/src/menu/section.ts index faecb24..f28606a 100644 --- a/src/menu/section.ts +++ b/src/menu/section.ts @@ -8,6 +8,7 @@ import { value } from './value'; import { getElCreator } from '../utils'; import { plr } from '../player'; import { controls } from './controls'; +import type { Cfg } from '../config'; export const section = ( sectionId: typeof SECTION_GLOBAL | typeof SECTION_LOCAL, diff --git a/src/menu/types.ts b/src/menu/types.ts new file mode 100644 index 0000000..ac06a79 --- /dev/null +++ b/src/menu/types.ts @@ -0,0 +1,4 @@ +import type { Setting } from '../config'; + +type Control = HTMLSelectElement | HTMLInputElement; +export type SettingControls = Record; diff --git a/src/player/apply-settings.ts b/src/player/apply-settings.ts index 5c699bf..88401ef 100644 --- a/src/player/apply-settings.ts +++ b/src/player/apply-settings.ts @@ -1,3 +1,4 @@ +import type { Cfg, Setting } from '../config'; import { restoreFocusAfter } from '../utils'; import { plr } from './plr'; import { valueSetters } from './value-setters'; diff --git a/src/player/plr.ts b/src/player/plr.ts index 4b39836..6e3dde2 100644 --- a/src/player/plr.ts +++ b/src/player/plr.ts @@ -1,5 +1,6 @@ import * as get from '../element-getters'; import { delay, until, restoreFocusAfter, findInNodeList } from '../utils'; +import type { YtSettingItem, YtSettingName } from './types'; export const plr = { async set(el: HTMLElement) { diff --git a/src/player/types.ts b/src/player/types.ts new file mode 100644 index 0000000..d4d257f --- /dev/null +++ b/src/player/types.ts @@ -0,0 +1,2 @@ +export type YtSettingName = typeof SPEED | typeof QUALITY; +export type YtSettingItem = HTMLDivElement & { role: 'menuitem' }; diff --git a/src/player/value-setters.ts b/src/player/value-setters.ts index f684d28..9b23104 100644 --- a/src/player/value-setters.ts +++ b/src/player/value-setters.ts @@ -1,8 +1,7 @@ import * as config from '../config'; import * as logger from '../logger'; import { plr } from '../player'; - -type YtSettingName = typeof SPEED | typeof QUALITY; +import type { YtSettingName } from './types'; type Comparator = (target: string, current: string) => boolean; const comparators: { readonly [P in YtSettingName]: Comparator } = { @@ -16,7 +15,9 @@ const comparators: { readonly [P in YtSettingName]: Comparator } = { type ValueSetterHelpers = { _ytSettingItem(settingName: YtSettingName, value: string): void; }; -type ValueSetters = { [P in Setting]: (value: Required[P]) => void }; +type ValueSetters = { + [P in config.Setting]: (value: Required[P]) => void; +}; export const valueSetters: ValueSetters & ValueSetterHelpers = { _ytSettingItem(settingName, value) { diff --git a/src/types.d.ts b/src/types.d.ts new file mode 100644 index 0000000..a74fcd4 --- /dev/null +++ b/src/types.d.ts @@ -0,0 +1,14 @@ +type DeepReadonly = T extends (infer R)[] + ? DeepReadonlyArray + : T extends Function + ? T + : T extends object + ? DeepReadonlyObject + : T; +type DeepReadonlyArray = ReadonlyArray>; +type DeepReadonlyObject = { + readonly [P in keyof T]: DeepReadonly; +}; + +type Props = Partial & object; +type ControlItem = { item: HTMLDivElement; elem: T }; diff --git a/src/types.ts b/src/types.ts deleted file mode 100644 index 91ca4a8..0000000 --- a/src/types.ts +++ /dev/null @@ -1,42 +0,0 @@ -type DeepReadonly = T extends (infer R)[] - ? DeepReadonlyArray - : T extends Function - ? T - : T extends object - ? DeepReadonlyObject - : T; -type DeepReadonlyArray = ReadonlyArray>; -type DeepReadonlyObject = { - readonly [P in keyof T]: DeepReadonly; -}; - -type FlagName = - | 'shortsToUsual' - | 'newTab' - | 'copySubs' - | 'standardMusicSpeed' - | 'enhancedBitrate'; -type YtCfg = { - speed?: string; - quality?: string; - volume?: string; - subtitles?: boolean; -}; -type Cfg = YtCfg & { - customSpeed?: string; -}; -type Setting = keyof Cfg; -type ScriptCfg = { - _v: number; - global: Cfg; - channels: Record; - flags: Record; -}; -type YtSettingItem = HTMLDivElement & { role: 'menuitem' }; -type InputWithHint = HTMLInputElement & { hint: Hint }; -type ReadonlyInputWithHint = DeepReadonly; -type Control = HTMLSelectElement | HTMLInputElement; -type SettingControls = Record; -type Props = Partial & object; -type ControlItem = { item: HTMLDivElement; elem: T }; -type Ref = { value: T };