Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | 9x 9x 21x 20x 20x 10x 10x 1x 9x 8x 8x 6x 9x 7x 7x 21x 7x 6x 3x 2x 2x 2x 6x 6x 1x 1x 8x 8x 2x 2x 4x 4x 2x 2x 4x | // The player's values for Be-Music Skins' `settings`: persistence per skin id across reloads, and the Options dialog
// controllers built from a skin's settings schema.
import type { BeMusicSkinSetting } from '@be-music/player-skin-sdk';
const STORAGE_KEY = 'be-music.skin-settings';
/**
* The stored map's revision. `0` while the shape is still being defined: it may change incompatibly without the
* number moving. Any other stamp is unreadable.
*/
const STORAGE_VERSION = 0;
/** One stored setting value: a choice's option value, a number, or a toggle's state. */
export type StoredSkinSettingValue = string | number | boolean;
/** Stored values per skin id, each keyed by setting id. */
export type StoredSkinSettings = Map<string, Record<string, StoredSkinSettingValue>>;
function isStoredValue(value: unknown): value is StoredSkinSettingValue {
return (
typeof value === 'string' || typeof value === 'boolean' || (typeof value === 'number' && Number.isFinite(value))
);
}
/**
* Parses stored skin settings. Values that are not a string, a finite number, or a boolean are dropped; anything
* unreadable yields no settings. Values are not checked against any schema here — the scenes do that with
* `resolveBeMusicSkinSettings`, so a value an older release of a skin stored falls back to the default there.
*/
export function parseStoredSkinSettings(raw: string | null | undefined): StoredSkinSettings {
const settings: StoredSkinSettings = new Map();
if (!raw) return settings;
let parsed: unknown;
try {
parsed = JSON.parse(raw);
} catch {
return settings;
}
if (typeof parsed !== 'object' || parsed === null) return settings;
const { version, skins } = parsed as { version?: unknown; skins?: unknown };
if (version !== STORAGE_VERSION || typeof skins !== 'object' || skins === null) return settings;
for (const [skinId, entry] of Object.entries(skins)) {
if (typeof entry !== 'object' || entry === null || Array.isArray(entry)) continue;
const values: Record<string, StoredSkinSettingValue> = {};
for (const [id, value] of Object.entries(entry)) {
if (isStoredValue(value)) values[id] = value;
}
settings.set(skinId, values);
}
return settings;
}
/** Serializes skin settings as `{ version: 0, skins: { [skinId]: { [settingId]: value } } }`. */
export function serializeStoredSkinSettings(settings: StoredSkinSettings): string {
return JSON.stringify({ version: STORAGE_VERSION, skins: Object.fromEntries(settings) });
}
/**
* Records `value` for setting `id` of skin `skinId`, keeping the skin's other values, and returns the skin's values. A
* new object replaces the skin's entry, so values already handed to a scene are never mutated.
*/
export function setStoredSkinSetting(
settings: StoredSkinSettings,
skinId: string,
id: string,
value: StoredSkinSettingValue,
): Readonly<Record<string, StoredSkinSettingValue>> {
const values = { ...settings.get(skinId), [id]: value };
settings.set(skinId, values);
return values;
}
export function readStoredSkinSettings(): StoredSkinSettings {
try {
return parseStoredSkinSettings(window.localStorage.getItem(STORAGE_KEY));
} catch {
return new Map();
}
}
export function storeSkinSettings(settings: StoredSkinSettings): void {
try {
window.localStorage.setItem(STORAGE_KEY, serializeStoredSkinSettings(settings));
} catch {
// Private windows / blocked or full storage: the values still apply for this session.
}
}
/** An Options dialog controller for one skin setting. */
export type SkinSettingController =
| {
kind: 'dropdown';
id: string;
name: string;
/** Shown label → option value, converted to a select's choices by the dialog. */
options: Record<string, string>;
}
| { kind: 'slider'; id: string; name: string; min: number; max: number; step: number | undefined }
| { kind: 'checkbox'; id: string; name: string };
/**
* The Options dialog controllers for a skin's `settings` schema, in declaration order: a choice becomes a dropdown, a
* number a slider over `min..max` (with the unit in its name), a toggle a checkbox. Option labels that repeat get the
* option value appended so every option stays pickable.
*/
export function describeSkinSettingControllers(
settings: readonly BeMusicSkinSetting[] | undefined,
): SkinSettingController[] {
return (settings ?? []).map((setting): SkinSettingController => {
switch (setting.kind) {
case 'choice': {
const options: Record<string, string> = {};
for (const option of setting.options) {
const label = option.label in options ? `${option.label} (${option.value})` : option.label;
options[label] = option.value;
}
return { kind: 'dropdown', id: setting.id, name: setting.label, options };
}
case 'number':
return {
kind: 'slider',
id: setting.id,
name: setting.unit === undefined ? setting.label : `${setting.label} (${setting.unit})`,
min: setting.min,
max: setting.max,
step: setting.step,
};
case 'toggle':
return { kind: 'checkbox', id: setting.id, name: setting.label };
}
});
}
|