Skip to content

Commit

Permalink
Pass plugin instead of tab
Browse files Browse the repository at this point in the history
  • Loading branch information
mnaoumov committed Aug 25, 2024
1 parent dea6cd6 commit 02b1a49
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 25 deletions.
1 change: 0 additions & 1 deletion src/obsidian/Plugin/PluginSettingsTabBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,3 @@ export abstract class PluginSettingsTabBase<
super(plugin.app, plugin);
}
}

41 changes: 17 additions & 24 deletions src/obsidian/Plugin/UIComponent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { KeysMatching } from "../../@types.ts";
import type { PluginSettingsTabBase } from "./PluginSettingsTabBase.ts";
import type { PluginBase } from "./PluginBase.ts";
import {
DropdownComponent,
Expand Down Expand Up @@ -78,29 +77,27 @@ type BindUIComponentOptions<PluginSettings, Property extends keyof PluginSetting
/**
* Binds a value component to a property in the plugin settings with optional automatic saving and value conversion.
*
* @typeParam PluginSettingsTab - The type of the plugin settings tab that extends `PluginSettingsTabBase`.
* @typeParam Plugin - The type of the plugin that extends `PluginBase`.
* @typeParam TUIComponent - The type of the value component extending `UIComponent`.
* @typeParam Property - The key of the plugin setting that the component is bound to.
* @typeParam UIValueType - The inferred type based on the UI component's type.
* @typeParam TPlugin - The inferred type of the plugin that extends `PluginBase`.
* @typeParam PluginSettings - The inferred type of the plugin settings object.
*
* @param pluginSettingsTab - The plugin settings tab that contains the UI component.
* @param plugin - The plugin.
* @param uiComponent - The component that will display and interact with the setting value.
* @param property - The property key in `PluginSettings` to bind to the UI component.
* @param options - Configuration options.
*
* @returns The `UIComponent` instance that was bound to the property.
*/
export function bindUiComponent<
PluginSettingsTab extends PluginSettingsTabBase<TPlugin, PluginSettings>,
Plugin extends PluginBase<object>,
TUIComponent extends UIComponent<unknown>,
Property extends KeysMatching<PluginSettings, UIValueType>,
UIValueType = TUIComponent extends UIComponent<infer P> ? P : never,
TPlugin extends PluginBase<PluginSettings> = PluginSettingsTab extends PluginSettingsTabBase<infer P> ? P : never,
PluginSettings extends object = TPlugin extends PluginBase<infer P> ? P : never,
PluginSettings extends object = Plugin extends PluginBase<infer P> ? P : never,
>(
pluginSettingsTab: PluginSettingsTab,
plugin: Plugin,
uiComponent: TUIComponent,
property: Property,
options?: BindUIComponentOptions<PluginSettings, Property, UIValueType>
Expand All @@ -109,29 +106,27 @@ export function bindUiComponent<
/**
* Binds a value component to a property in the plugin settings with optional automatic saving and value conversion.
*
* @typeParam PluginSettingsTab - The type of the plugin settings tab that extends `PluginSettingsTabBase`.
* @typeParam Plugin - The type of the plugin that extends `PluginBase`.
* @typeParam TUIComponent - The type of the value component extending `UIComponent`.
* @typeParam Property - The key of the plugin setting that the component is bound to.
* @typeParam UIValueType - The inferred type based on the UI component's type.
* @typeParam TPlugin - The inferred type of the plugin that extends `PluginBase`.
* @typeParam PluginSettings - The inferred type of the plugin settings object.
*
* @param pluginSettingsTab - The plugin settings tab that contains the UI component.
* @param plugin - The plugin.
* @param uiComponent - The component that will display and interact with the setting value.
* @param property - The property key in `PluginSettings` to bind to the UI component.
* @param options - Configuration options.
*
* @returns The `UIComponent` instance that was bound to the property.
*/
export function bindUiComponent<
PluginSettingsTab extends PluginSettingsTabBase<TPlugin, PluginSettings>,
Plugin extends PluginBase<object>,
TUIComponent extends UIComponent<unknown>,
Property extends keyof PluginSettings,
UIValueType = TUIComponent extends UIComponent<infer P> ? P : never,
TPlugin extends PluginBase<PluginSettings> = PluginSettingsTab extends PluginSettingsTabBase<infer P> ? P : never,
PluginSettings extends object = TPlugin extends PluginBase<infer P> ? P : never,
PluginSettings extends object = Plugin extends PluginBase<infer P> ? P : never,
>(
pluginSettingsTab: PluginSettingsTab,
plugin: Plugin,
uiComponent: TUIComponent,
property: Property,
options: BindUIComponentOptions<PluginSettings, Property, UIValueType>
Expand All @@ -140,29 +135,27 @@ export function bindUiComponent<
/**
* Binds a value component to a property in the plugin settings with optional automatic saving and value conversion.
*
* @typeParam PluginSettingsTab - The type of the plugin settings tab that extends `PluginSettingsTabBase`.
* @typeParam Plugin - The type of the plugin that extends `PluginBase`.
* @typeParam TUIComponent - The type of the value component extending `UIComponent`.
* @typeParam Property - The key of the plugin setting that the component is bound to.
* @typeParam UIValueType - The inferred type based on the UI component's type.
* @typeParam TPlugin - The inferred type of the plugin that extends `PluginBase`.
* @typeParam PluginSettings - The inferred type of the plugin settings object.
*
* @param pluginSettingsTab - The plugin settings tab that contains the UI component.
* @param plugin - The plugin.
* @param uiComponent - The component that will display and interact with the setting value.
* @param property - The property key in `PluginSettings` to bind to the UI component.
* @param options - Configuration options.
*
* @returns The `UIComponent` instance that was bound to the property.
*/
export function bindUiComponent<
PluginSettingsTab extends PluginSettingsTabBase<TPlugin, PluginSettings>,
Plugin extends PluginBase<object>,
TUIComponent extends UIComponent<unknown>,
Property extends keyof PluginSettings,
UIValueType = TUIComponent extends UIComponent<infer P> ? P : never,
TPlugin extends PluginBase<PluginSettings> = PluginSettingsTab extends PluginSettingsTabBase<infer P> ? P : never,
PluginSettings extends object = TPlugin extends PluginBase<infer P> ? P : never,
PluginSettings extends object = Plugin extends PluginBase<infer P> ? P : never,
>(
pluginSettingsTab: PluginSettingsTab,
plugin: Plugin,
uiComponent: TUIComponent,
property: Property,
options?: BindUIComponentOptions<PluginSettings, Property, UIValueType>
Expand All @@ -171,7 +164,7 @@ export function bindUiComponent<
settingToUIValueConverter: (value): UIValueType => value as UIValueType,
uiToSettingValueConverter: (value): PluginSettings[Property] => value as PluginSettings[Property],
};
const pluginSettings = options.pluginSettings ?? pluginSettingsTab.plugin.settingsCopy;
const pluginSettings = options.pluginSettings ?? plugin.settingsCopy as PluginSettings;
(uiComponent as UIComponent<UIValueType>)
.setValue(options.settingToUIValueConverter(pluginSettings[property]))
.onChange(async (uiValue) => {
Expand All @@ -188,7 +181,7 @@ export function bindUiComponent<
}
pluginSettings[property] = options.uiToSettingValueConverter(uiValue);
if (options.autoSave ?? true) {
await pluginSettingsTab.plugin.saveSettings(pluginSettings);
await plugin.saveSettings(pluginSettings);
}
});
return uiComponent;
Expand Down

0 comments on commit 02b1a49

Please sign in to comment.