添加设置的事件监听

This commit is contained in:
unanmed 2023-08-02 23:27:57 +08:00
parent 62f2fbf838
commit b7f0d6f4d8

View File

@ -8,6 +8,7 @@ interface MotaSettingItem<T extends MotaSettingType = MotaSettingType> {
defaults?: boolean | number; defaults?: boolean | number;
step?: number; step?: number;
display?: (value: T) => string; display?: (value: T) => string;
special?: string;
} }
interface SettingEvent extends EmitableEvent { interface SettingEvent extends EmitableEvent {
@ -20,14 +21,13 @@ interface SettingEvent extends EmitableEvent {
class MotaSetting extends EventEmitter<SettingEvent> { class MotaSetting extends EventEmitter<SettingEvent> {
private list: Record<string, MotaSettingItem> = {}; private list: Record<string, MotaSettingItem> = {};
special?: string;
/** /**
* *
*/ */
markAsSpecial(sp: string) { markSpecial(key: string, sp: string) {
this.special = sp; const setting = this.getSettingBy(key.split('.'));
this.register('@special', '', false); setting.special = sp;
return this; return this;
} }
@ -83,7 +83,7 @@ class MotaSetting extends EventEmitter<SettingEvent> {
} }
const old = setting.value as boolean | number; const old = setting.value as boolean | number;
setting.value = value; setting.value = value;
this.emit('valueChange', this.formatKey(key), old, value); this.emit('valueChange', key, old, value);
} }
/** /**
@ -101,7 +101,7 @@ class MotaSetting extends EventEmitter<SettingEvent> {
} }
const old = setting.value as boolean | number; const old = setting.value as boolean | number;
setting.value += value; setting.value += value;
this.emit('valueChange', this.formatKey(key), old, value); this.emit('valueChange', key, old, value);
} }
/** /**
@ -130,10 +130,6 @@ class MotaSetting extends EventEmitter<SettingEvent> {
return now.list[list.at(-1)!] ?? null; return now.list[list.at(-1)!] ?? null;
} }
private formatKey(key: string) {
return key.replace('.@special', '');
}
} }
export const mainSetting = new MotaSetting(); export const mainSetting = new MotaSetting();
@ -159,11 +155,8 @@ mainSetting
new MotaSetting() new MotaSetting()
.register('autoSkill', '自动切换技能', true) .register('autoSkill', '自动切换技能', true)
.register('fixed', '定点查看', true) .register('fixed', '定点查看', true)
.register( .register('hotkey', '快捷键', false)
'hotkey', .markSpecial('hotkey', 'hotkey')
'快捷键',
new MotaSetting().markAsSpecial('hotkey')
)
) )
.register( .register(
'utils', 'utils',