Skip to content

Commit

Permalink
Fix Settings generics
Browse files Browse the repository at this point in the history
  • Loading branch information
mnaoumov committed Jan 21, 2025
1 parent 8302557 commit 283b938
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/obsidian/Plugin/PluginSettingsTabBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,20 @@ export interface BindOptionsExtended<PluginSettings, UIValue, Property extends k
pluginSettingsToComponentValueConverter: (pluginSettingsValue: PluginSettings[Property]) => UIValue;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type ExtractPluginSettings<T extends PluginBase<any>> = PluginSettingsBase & T['settingsClone'];

/**
* Base class for creating plugin settings tabs in Obsidian.
* Provides a method for binding value components to plugin settings and handling changes.
*
* @typeParam TPlugin - The type of the plugin that extends PluginBase.
* @typeParam PluginSettings - The type of the plugin settings object.
*/
export abstract class PluginSettingsTabBase<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
TPlugin extends PluginBase<any>,
PluginSettings extends PluginSettingsBase = TPlugin extends PluginBase<infer P> ? P : never
> extends PluginSettingTab {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export abstract class PluginSettingsTabBase<TPlugin extends PluginBase<any>> extends PluginSettingTab {
private validatorsMap = new WeakMap<ValueComponent<unknown>, () => Promise<boolean>>();

public constructor(public override plugin: PluginBase<PluginSettings>) {
public constructor(public override plugin: TPlugin) {
super(plugin.app, plugin);
this.containerEl.addClass(CssClass.LibraryName, getPluginId(), CssClass.PluginSettingsTab);
}
Expand All @@ -109,8 +108,8 @@ export abstract class PluginSettingsTabBase<
TValueComponent
>(
valueComponent: TValueComponent & ValueComponentWithChangeTracking<UIValue>,
property: KeysMatching<PluginSettings, UIValue>,
options?: BindOptions<PluginSettings, UIValue>
property: KeysMatching<ExtractPluginSettings<TPlugin>, UIValue>,
options?: BindOptions<ExtractPluginSettings<TPlugin>, UIValue>
): TValueComponent;

/**
Expand All @@ -127,11 +126,11 @@ export abstract class PluginSettingsTabBase<
protected bind<
UIValue,
TValueComponent,
Property extends keyof PluginSettings
Property extends keyof ExtractPluginSettings<TPlugin>
>(
valueComponent: TValueComponent & ValueComponentWithChangeTracking<UIValue>,
property: Property,
options: BindOptionsExtended<PluginSettings, UIValue, Property>
options: BindOptionsExtended<ExtractPluginSettings<TPlugin>, UIValue, Property>
): TValueComponent;

/**
Expand All @@ -148,21 +147,25 @@ export abstract class PluginSettingsTabBase<
protected bind<
UIValue,
TValueComponent,
Property extends keyof PluginSettings
Property extends keyof ExtractPluginSettings<TPlugin>
>(
valueComponent: TValueComponent & ValueComponentWithChangeTracking<UIValue>,
property: Property,
options?: BindOptions<PluginSettings, UIValue>
options?: BindOptions<ExtractPluginSettings<TPlugin>, UIValue>
): TValueComponent {
type PluginSettings = ExtractPluginSettings<TPlugin>;
type PropertyType = PluginSettings[Property];
const DEFAULT_OPTIONS: BindOptionsExtended<PluginSettings, UIValue, Property> = {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
componentToPluginSettingsValueConverter: (value: UIValue): PropertyType => value as PropertyType,
pluginSettingsToComponentValueConverter: (value: PropertyType): UIValue => value as UIValue,
shouldAutoSave: true,
shouldShowValidationMessage: true
};

const optionsExt: BindOptionsExtended<PluginSettings, UIValue, Property> = { ...DEFAULT_OPTIONS, ...options };

// eslint-disable-next-line @typescript-eslint/no-unsafe-return
const pluginSettingsFn = (): PluginSettings => optionsExt.pluginSettings ?? this.plugin.settingsClone;

const validate = async (uiValue?: UIValue): Promise<boolean> => {
Expand Down

0 comments on commit 283b938

Please sign in to comment.