UNPKG

svelte-settings

Version:

> [!WARNING] > This project is a work in progress. Do not use it in any of your projects yet.

104 lines (103 loc) 3.54 kB
import type { Icon } from '@lucide/svelte'; import type { Readable } from 'svelte/store'; import type { Changelog } from './types/changelog.js'; export type VisibilityCallback = (ctx: Record<string, unknown>) => boolean; type BaseConfigItem = { id: string; label: string; description?: string; icon?: typeof Icon; visible?: VisibilityCallback; disabled?: boolean | Readable<boolean>; allowsFullscreen?: boolean; action?: () => any; }; export type TextSetting = BaseConfigItem & { type: 'text'; default: string; placeholder?: string; }; export type SelectSetting = BaseConfigItem & { type: 'select'; options: readonly string[]; default: string; }; export type MultiSelectSetting = BaseConfigItem & { type: 'multiselect' | 'multiselect-reorder'; options: readonly string[]; labels?: Record<string, string>; default: string[]; requiresFullscreen?: boolean; }; export type BooleanSetting = BaseConfigItem & { type: 'boolean'; default: boolean; }; export type NumberSetting = BaseConfigItem & { type: 'number'; default: number; min?: number; max?: number; step?: number; unit?: string; }; export type DescriptionItem = BaseConfigItem & { type: 'description'; text: string; }; export type ValueDisplayItem = BaseConfigItem & { type: 'value'; value: string | undefined | Readable<string | undefined> | (() => Promise<string | undefined>); url?: string; }; export type ActionItem = BaseConfigItem & { type: 'action'; action: () => Promise<unknown> | unknown | void; variant?: string; enabled?: Readable<true>; }; export type NotImplementedSetting = BaseConfigItem & { type: 'not-implemented'; default?: unknown; }; type OptionalProp<T, K extends PropertyKey> = T extends Record<K, unknown> ? Omit<T, K> & Partial<Pick<T, K>> : T; export type GroupWrapper = BaseConfigItem & { type: 'group'; children: SettingsBlueprintItem[]; }; export type BasePage = BaseConfigItem & { type: 'page'; children: SettingsBlueprintItem[]; }; export type ListSettingPage = BaseConfigItem & { type: 'list'; default: Array<unknown>; nameProperty: string; children: OptionalProp<SettingsBlueprintItem, 'default'>[]; }; export type ChangelogPage = BaseConfigItem & { type: 'changelog'; changelog: Changelog | Readable<Changelog> | (() => Promise<Changelog>); }; export type SettingsPage = BasePage | ListSettingPage | ChangelogPage; export type SettingsWrapper = GroupWrapper; export type SettingsItem = DescriptionItem | ValueDisplayItem | ActionItem | NotImplementedSetting; export type SettingsInput = TextSetting | SelectSetting | MultiSelectSetting | BooleanSetting | NumberSetting; export type SettingsNested = BasePage | SettingsWrapper; export type SettingsBlueprintItem = SettingsPage | SettingsWrapper | SettingsItem | SettingsInput; export type SettingsBlueprint = SettingsBlueprintItem[]; export type SettingsFromBlueprint<T extends readonly SettingsBlueprintItem[]> = { [K in T[number] as K['id']]: K extends { type: 'group' | 'page'; children: infer C; } ? SettingsFromBlueprint<C & readonly SettingsBlueprintItem[]> : K extends { type: 'select'; options: infer O; } ? O extends readonly unknown[] ? O[number] : never : K extends { type: 'multiselect'; options: infer O; } ? O extends readonly unknown[] ? O[number][] : never : K extends { default: infer D; } ? D : never; }; export {};