mirror of
https://github.com/gpxstudio/gpx.studio.git
synced 2026-11-30 09:34:29 +00:00
refactoring and tests
This commit is contained in:
1 parent
3bb553d6b5
commit
da9abbae5e
45 files changed
+3558
-1564
No files matched your search
@@ -13,170 +13,21 @@ import {
|
||||
type LayerTreeType,
|
||||
} from '$lib/assets/layers';
|
||||
import { browser } from '$app/environment';
|
||||
import type { DistanceUnits, TemperatureUnits, VelocityUnits } from '$lib/unit-conversions';
|
||||
import { engine } from '$lib/engine';
|
||||
import { get, writable, type Writable } from 'svelte/store';
|
||||
import {
|
||||
getArrayValidator,
|
||||
getLayerTreeValidator,
|
||||
getLayerValidator,
|
||||
getValueValidator,
|
||||
Setting,
|
||||
SettingInitOnFirstRead,
|
||||
useSettingsStorage,
|
||||
type StoredSettings,
|
||||
} from './setting';
|
||||
|
||||
/** What the settings were saved as, by key: JSON strings (see `Engine.openStorage`). */
|
||||
export type StoredSettings = Record<string, string>;
|
||||
export type { StoredSettings };
|
||||
|
||||
/** Reads the saved value of a setting, `undefined` if there is none or if it cannot be read. */
|
||||
function storedValue(stored: StoredSettings, key: string): { value: any } | undefined {
|
||||
if (!Object.hasOwn(stored, key)) {
|
||||
return undefined;
|
||||
}
|
||||
try {
|
||||
return { value: JSON.parse(stored[key]) };
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
/** Keeps a value; `undefined` is not something JSON can hold, it means no value. */
|
||||
function save(key: string, value: unknown) {
|
||||
const json = JSON.stringify(value);
|
||||
if (json === undefined) {
|
||||
engine.deleteSetting(key);
|
||||
} else {
|
||||
engine.setSetting(key, json);
|
||||
}
|
||||
}
|
||||
|
||||
export class Setting<V> {
|
||||
private _key: string;
|
||||
private _value: Writable<V>;
|
||||
private _validator?: (value: V) => V;
|
||||
|
||||
constructor(key: string, initial: V, validator?: (value: V) => V) {
|
||||
this._key = key;
|
||||
this._value = writable(initial);
|
||||
this._validator = validator;
|
||||
}
|
||||
|
||||
/** Takes the saved value, if there is one. */
|
||||
connect(stored: StoredSettings) {
|
||||
const saved = storedValue(stored, this._key);
|
||||
if (saved) {
|
||||
this._value.set(this._validator ? this._validator(saved.value) : saved.value);
|
||||
}
|
||||
}
|
||||
|
||||
subscribe(run: (value: V) => void, invalidate?: (value?: V) => void) {
|
||||
return this._value.subscribe(run, invalidate);
|
||||
}
|
||||
|
||||
set(value: V) {
|
||||
if (typeof value === 'object' || value !== get(this._value)) {
|
||||
this._value.set(value);
|
||||
save(this._key, value);
|
||||
}
|
||||
}
|
||||
|
||||
update(callback: (value: any) => any) {
|
||||
this.set(callback(get(this._value)));
|
||||
}
|
||||
}
|
||||
|
||||
/** A setting that has no value until it is connected: then the saved one, or the initial one. */
|
||||
export class SettingInitOnFirstRead<V> {
|
||||
private _key: string;
|
||||
private _value: Writable<V | undefined>;
|
||||
private _initial: V;
|
||||
private _validator?: (value: V) => V;
|
||||
|
||||
constructor(key: string, initial: V, validator?: (value: V) => V) {
|
||||
this._key = key;
|
||||
this._value = writable(undefined);
|
||||
this._initial = initial;
|
||||
this._validator = validator;
|
||||
}
|
||||
|
||||
connect(stored: StoredSettings) {
|
||||
const saved = storedValue(stored, this._key);
|
||||
if (saved) {
|
||||
this._value.set(this._validator ? this._validator(saved.value) : saved.value);
|
||||
} else {
|
||||
this._value.set(this._initial);
|
||||
}
|
||||
}
|
||||
|
||||
initialize() {
|
||||
this.set(this._initial);
|
||||
}
|
||||
|
||||
subscribe(run: (value: V | undefined) => void, invalidate?: (value?: V | undefined) => void) {
|
||||
return this._value.subscribe(run, invalidate);
|
||||
}
|
||||
|
||||
set(value: V) {
|
||||
if (typeof value === 'object' || value !== get(this._value)) {
|
||||
this._value.set(value);
|
||||
save(this._key, value);
|
||||
}
|
||||
}
|
||||
|
||||
update(callback: (value: any) => any) {
|
||||
this.set(callback(get(this._value)));
|
||||
}
|
||||
}
|
||||
|
||||
function getValueValidator<V>(allowed: V[], fallback: V) {
|
||||
const dict = new Set<V>(allowed);
|
||||
return (value: V) => (dict.has(value) ? value : fallback);
|
||||
}
|
||||
|
||||
function getArrayValidator<V>(allowed: V[]) {
|
||||
const dict = new Set<V>(allowed);
|
||||
return (value: V[]) => value.filter((v) => dict.has(v));
|
||||
}
|
||||
|
||||
function getLayerValidator(allowed: Record<string, any>, fallback: string) {
|
||||
return (layer: string) =>
|
||||
allowed.hasOwnProperty(layer) ||
|
||||
layer.startsWith('custom-') ||
|
||||
layer.startsWith('extension-')
|
||||
? layer
|
||||
: fallback;
|
||||
}
|
||||
|
||||
function filterLayerTree(t: LayerTreeType, allowed: LayerTreeType | undefined): LayerTreeType {
|
||||
const filtered: LayerTreeType = {};
|
||||
if (allowed) {
|
||||
Object.entries(allowed).forEach(([key, value]) => {
|
||||
if (Object.hasOwn(t, key)) {
|
||||
if (typeof value === 'boolean') {
|
||||
filtered[key] = t[key];
|
||||
} else if (typeof value === 'object') {
|
||||
filtered[key] = filterLayerTree(
|
||||
typeof t[key] === 'object' ? t[key] : {},
|
||||
value
|
||||
);
|
||||
}
|
||||
} else {
|
||||
filtered[key] = value;
|
||||
}
|
||||
});
|
||||
}
|
||||
Object.entries(t).forEach(([key, value]) => {
|
||||
if (!Object.hasOwn(filtered, key)) {
|
||||
if (typeof value === 'boolean') {
|
||||
if (key.startsWith('custom-') || key.startsWith('extension-')) {
|
||||
filtered[key] = value;
|
||||
}
|
||||
} else if (typeof value === 'object') {
|
||||
filtered[key] = filterLayerTree(value, undefined);
|
||||
}
|
||||
}
|
||||
});
|
||||
return filtered;
|
||||
}
|
||||
|
||||
function getLayerTreeValidator(allowed: LayerTreeType) {
|
||||
return (value: LayerTreeType) => filterLayerTree(value, allowed);
|
||||
}
|
||||
|
||||
type DistanceUnits = 'metric' | 'imperial' | 'nautical';
|
||||
type VelocityUnits = 'speed' | 'pace';
|
||||
type TemperatureUnits = 'celsius' | 'fahrenheit';
|
||||
type AdditionalDataset = 'speed' | 'hr' | 'cad' | 'atemp' | 'power';
|
||||
type ElevationFill = 'slope' | 'surface' | 'highway' | undefined;
|
||||
type RoutingProfile =
|
||||
@@ -191,6 +42,12 @@ type RoutingProfile =
|
||||
type TerrainSource = 'mapterhorn';
|
||||
type StreetViewSource = 'mapillary' | 'google';
|
||||
|
||||
// The settings are kept by the engine, in the storage of the browser.
|
||||
useSettingsStorage({
|
||||
save: (key, json) => engine.setSetting(key, json),
|
||||
remove: (key) => engine.deleteSetting(key),
|
||||
});
|
||||
|
||||
export const settings = {
|
||||
distanceUnits: new Setting<DistanceUnits>(
|
||||
'distanceUnits',
|
||||
@@ -295,7 +152,6 @@ export const settings = {
|
||||
'mapillary',
|
||||
getValueValidator<StreetViewSource>(['mapillary', 'google'], 'mapillary')
|
||||
),
|
||||
fileOrder: new Setting<string[]>('fileOrder', []),
|
||||
defaultOpacity: new Setting('defaultOpacity', 0.7),
|
||||
defaultWidth: new Setting('defaultWidth', browser && window.innerWidth < 600 ? 8 : 5),
|
||||
bottomPanelSize: new Setting('bottomPanelSize', 170),
|
||||
|
||||
Reference in new issue
Block a user