feat(controlManager): add getters for controls and custom storageId parameter for persistent storage

This commit is contained in:
Ilia Mashkov
2026-02-07 18:05:14 +03:00
parent 180abd150d
commit 10919a9881

View File

@@ -1,4 +1,3 @@
import type { ControlId } from '$features/SetupFont/model/state/manager.svelte';
import { import {
type ControlDataModel, type ControlDataModel,
type ControlModel, type ControlModel,
@@ -9,6 +8,7 @@ import {
} from '$shared/lib'; } from '$shared/lib';
import { SvelteMap } from 'svelte/reactivity'; import { SvelteMap } from 'svelte/reactivity';
import { import {
type ControlId,
DEFAULT_FONT_SIZE, DEFAULT_FONT_SIZE,
DEFAULT_FONT_WEIGHT, DEFAULT_FONT_WEIGHT,
DEFAULT_LETTER_SPACING, DEFAULT_LETTER_SPACING,
@@ -16,6 +16,7 @@ import {
} from '../../model'; } from '../../model';
type ControlOnlyFields<T extends string = string> = Omit<ControlModel<T>, keyof ControlDataModel>; type ControlOnlyFields<T extends string = string> = Omit<ControlModel<T>, keyof ControlDataModel>;
export interface Control extends ControlOnlyFields<ControlId> { export interface Control extends ControlOnlyFields<ControlId> {
instance: TypographyControl; instance: TypographyControl;
} }
@@ -29,11 +30,11 @@ export class TypographyControlManager {
constructor(configs: ControlModel<ControlId>[], storage: PersistentStore<TypographySettings>) { constructor(configs: ControlModel<ControlId>[], storage: PersistentStore<TypographySettings>) {
this.#storage = storage; this.#storage = storage;
// 1. Initial Load // Initial Load
const saved = storage.value; const saved = storage.value;
this.#baseSize = saved.fontSize; this.#baseSize = saved.fontSize;
// 2. Setup Controls // Setup Controls
configs.forEach(config => { configs.forEach(config => {
const initialValue = this.#getInitialValue(config.id, saved); const initialValue = this.#getInitialValue(config.id, saved);
@@ -46,7 +47,7 @@ export class TypographyControlManager {
}); });
}); });
// 3. The Sync Effect (UI -> Storage) // The Sync Effect (UI -> Storage)
// We access .value explicitly to ensure Svelte 5 tracks the dependency // We access .value explicitly to ensure Svelte 5 tracks the dependency
$effect.root(() => { $effect.root(() => {
$effect(() => { $effect(() => {
@@ -65,7 +66,7 @@ export class TypographyControlManager {
}; };
}); });
// 4. The Font Size Proxy Effect // The Font Size Proxy Effect
// This handles the "Multiplier" logic specifically for the Font Size Control // This handles the "Multiplier" logic specifically for the Font Size Control
$effect(() => { $effect(() => {
const ctrl = this.#controls.get('font_size')?.instance; const ctrl = this.#controls.get('font_size')?.instance;
@@ -123,10 +124,32 @@ export class TypographyControlManager {
if (ctrl) ctrl.value = val * this.#multiplier; if (ctrl) ctrl.value = val * this.#multiplier;
} }
/**
* Getters for controls
*/
get controls() { get controls() {
return Array.from(this.#controls.values()); return Array.from(this.#controls.values());
} }
get weightControl() {
return this.#controls.get('font_weight')?.instance;
}
get sizeControl() {
return this.#controls.get('font_size')?.instance;
}
get heightControl() {
return this.#controls.get('line_height')?.instance;
}
get spacingControl() {
return this.#controls.get('letter_spacing')?.instance;
}
/**
* Getters for values (besides font-size)
*/
get weight() { get weight() {
return this.#controls.get('font_weight')?.instance.value ?? DEFAULT_FONT_WEIGHT; return this.#controls.get('font_weight')?.instance.value ?? DEFAULT_FONT_WEIGHT;
} }
@@ -175,10 +198,14 @@ export interface TypographySettings {
* Creates a typography control manager that handles a collection of typography controls. * Creates a typography control manager that handles a collection of typography controls.
* *
* @param configs - Array of control configurations. * @param configs - Array of control configurations.
* @param storageId - Persistent storage identifier.
* @returns - Typography control manager instance. * @returns - Typography control manager instance.
*/ */
export function createTypographyControlManager(configs: ControlModel<ControlId>[]) { export function createTypographyControlManager(
const storage = createPersistentStore<TypographySettings>('glyphdiff:typography', { configs: ControlModel<ControlId>[],
storageId: string = 'glyphdiff:typography',
) {
const storage = createPersistentStore<TypographySettings>(storageId, {
fontSize: DEFAULT_FONT_SIZE, fontSize: DEFAULT_FONT_SIZE,
fontWeight: DEFAULT_FONT_WEIGHT, fontWeight: DEFAULT_FONT_WEIGHT,
lineHeight: DEFAULT_LINE_HEIGHT, lineHeight: DEFAULT_LINE_HEIGHT,