Skip to content

Commit

Permalink
fix: apply config normalization to config read from idb
Browse files Browse the repository at this point in the history
Config adds fields over time and may need to be updated, uses same logic that is used when loading old sheets
  • Loading branch information
mgreminger committed Nov 17, 2024
1 parent b1b9967 commit 28f3691
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
9 changes: 4 additions & 5 deletions src/SetDefaultConfigDialog.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@
import { get, set } from 'idb-keyval';
import { Button } from "carbon-components-svelte";
import CheckmarkOutline from "carbon-icons-svelte/lib/CheckmarkOutline.svelte";
import { type Config, configsEqual, getDefaultConfig } from "./sheet/Sheet";
import { type Config, configsEqual, getDefaultConfig, normalizeConfig } from "./sheet/Sheet";
import { config } from "./stores";
let userDefaultConfig: Config | undefined = getDefaultConfig();
let userDefaultConfig: Config = getDefaultConfig();
onMount(async () => {
try {
userDefaultConfig = await get('defaultConfig');
userDefaultConfig = normalizeConfig(await get('defaultConfig'));
} catch(e) {
console.warn('Error attempting to load user default config');
userDefaultConfig = undefined;
userDefaultConfig = getDefaultConfig();
}
userDefaultConfig = userDefaultConfig ?? getDefaultConfig();
});
async function setDefaultConfig() {
Expand Down
18 changes: 10 additions & 8 deletions src/sheet/Sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,14 +322,16 @@ export function baseUnitsEqual(baseUnits1: CustomBaseUnits, baseUnits2: CustomBa
return result;
}

export function normalizeConfig(inputConfig: Config | undefined): Config {
const outputConfig = inputConfig ?? getDefaultConfig();
export function normalizeConfig(config: Config | undefined): Config {
if (!config) {
return getDefaultConfig();
}

outputConfig.customBaseUnits = outputConfig.customBaseUnits ?? getDefaultBaseUnits(); // customBaseUnits may not exist
outputConfig.simplifySymbolicExpressions = outputConfig.simplifySymbolicExpressions ?? true; // simplifySymboicExpressions may not exist
outputConfig.convertFloatsToFractions = outputConfig.convertFloatsToFractions ?? true; // convertFloatsToFractions may not exist
outputConfig.fluidConfig = outputConfig.fluidConfig ?? getDefaultFluidConfig(); // fluidConfig may not exist
outputConfig.mathCellConfig.showIntermediateResults = outputConfig.mathCellConfig.showIntermediateResults ?? false; // may not exist
config.customBaseUnits = config.customBaseUnits ?? getDefaultBaseUnits(); // customBaseUnits may not exist
config.simplifySymbolicExpressions = config.simplifySymbolicExpressions ?? true; // simplifySymboicExpressions may not exist
config.convertFloatsToFractions = config.convertFloatsToFractions ?? true; // convertFloatsToFractions may not exist
config.fluidConfig = config.fluidConfig ?? getDefaultFluidConfig(); // fluidConfig may not exist
config.mathCellConfig.showIntermediateResults = config.mathCellConfig.showIntermediateResults ?? false; // may not exist

return outputConfig;
return config;
}
9 changes: 4 additions & 5 deletions src/stores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import InsertCell from "./cells/InsertCell";
import type { History } from './database/types';
import type { Result, FiniteImagResult, PlotResult,
MatrixResult, SystemResult, DataTableResult } from './resultTypes';
import { type Config, type InsertedSheet, type Sheet, getDefaultConfig } from './sheet/Sheet';
import { type Config, type InsertedSheet, type Sheet, getDefaultConfig, normalizeConfig } from './sheet/Sheet';

const defaultTitle = 'New Sheet';

Expand Down Expand Up @@ -135,15 +135,14 @@ export function getSheetJson() {
}

export async function resetSheet() {
let defaultConfig: Config | undefined;
let defaultConfig: Config;

try {
defaultConfig = await idbGet('defaultConfig');
defaultConfig = normalizeConfig(await idbGet('defaultConfig'));
} catch(e) {
console.warn('Error retrieving default config for idb');
defaultConfig = undefined;
defaultConfig = getDefaultConfig();
}
defaultConfig = defaultConfig ?? getDefaultConfig();

config.set(defaultConfig);
cells.set([]);
Expand Down

0 comments on commit 28f3691

Please sign in to comment.