Skip to content

Commit

Permalink
feat: implement option to save a user default config
Browse files Browse the repository at this point in the history
Need to add tests and a button to apply user config to current sheet
  • Loading branch information
mgreminger committed Nov 17, 2024
1 parent 7af4efe commit bfc8bd6
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 21 deletions.
21 changes: 18 additions & 3 deletions src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
history, insertedSheets, activeCell, getSheetJson, getSheetObject, resetSheet, sheetId,
mathCellChanged, nonMathCellChanged, addCell, prefersReducedMotion, modifierKey,
inCellInsertMode, config, unsavedChange, incrementActiveCell,
decrementActiveCell, deleteCell, activeMathField, autosaveNeeded, mathJaxLoaded
decrementActiveCell, deleteCell, activeMathField, autosaveNeeded, mathJaxLoaded,
userDefaultConfig
} from "./stores";
import { getDefaultBaseUnits, getDefaultFluidConfig, isDefaultConfig,
normalizeConfig } from "./sheet/Sheet";
import { isDefaultConfig, type Config, normalizeConfig } from "./sheet/Sheet";
import type { Statement, SubQueryStatement } from "./parser/types";
import type { SystemDefinition } from "./cells/SystemCell";
import type { FluidFunction } from "./cells/FluidCell";
Expand Down Expand Up @@ -92,6 +92,7 @@
import BaseUnitsConfigDialog from "./BaseUnitsConfigDialog.svelte";
import DownloadDocumentModal from "./DownloadDocumentModal.svelte";
import { getBlankStatement } from "./parser/LatexToSympy";
import SetDefaultConfigDialog from "./SetDefaultConfigDialog.svelte";
createCustomUnits();
Expand Down Expand Up @@ -357,6 +358,16 @@
$prefersReducedMotion = mediaQueryList.matches
mediaQueryList.addEventListener('change', handleMotionPreferenceChange);
let tempUserDefaultConfig: Config | undefined = undefined;
try {
tempUserDefaultConfig = await get('defaultConfig');
} catch(e) {
console.log('Error retrieving user default config');
tempUserDefaultConfig = undefined;
}
$userDefaultConfig = tempUserDefaultConfig ?? getDefaultConfig();
$unsavedChange = false;
$autosaveNeeded = false;
await refreshSheet(true);
Expand Down Expand Up @@ -2821,6 +2832,7 @@ Please include a link to this sheet in the email to assist in debugging the prob
<Tabs>
<Tab label="Number Format" />
<Tab label="Default Units" />
<Tab label="Set Default" />
<svelte:fragment slot="content">
<TabContent>
<Checkbox
Expand All @@ -2844,6 +2856,9 @@ Please include a link to this sheet in the email to assist in debugging the prob
bind:baseUnits={$config.customBaseUnits}
/>
</TabContent>
<TabContent>
<SetDefaultConfigDialog />
</TabContent>
</svelte:fragment>
</Tabs>
{/if}
Expand Down
3 changes: 2 additions & 1 deletion src/MathCellConfigDialog.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import { Checkbox, NumberInput, Button,
RadioButtonGroup, RadioButton } from "carbon-components-svelte";
import { defaultMathConfig, copyMathConfig, isDefaultMathConfig,
import { defaultConfig, copyMathConfig, isDefaultMathConfig,
type MathCellConfig, getSafeMathConfig, mathConfigLimits } from "./sheet/Sheet";
import { unsavedChange, autosaveNeeded, mathCellChanged } from "./stores";
import type MathCellElement from "./MathCell.svelte";
Expand All @@ -10,6 +10,7 @@
export let cellLevelConfig = false;
export let mathCellElement: MathCellElement | null = null;
let defaultMathConfig = defaultConfig.mathCellConfig;
let currentMathCellConfig = copyMathConfig(mathCellConfig) ?? copyMathConfig(defaultMathConfig);
export function resetDefaults() {
Expand Down
38 changes: 38 additions & 0 deletions src/SetDefaultConfigDialog.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<script lang="ts">
import { Button } from "carbon-components-svelte";
import CheckmarkOutline from "carbon-icons-svelte/lib/CheckmarkOutline.svelte";
import { type Config, configsEqual, getDefaultConfig } from "./sheet/Sheet";
import { config, userDefaultConfig } from "./stores";
import { set } from 'idb-keyval';
async function setDefaultConfig() {
let saveError = false;
try {
await set('defaultConfig', $config);
} catch (e) {
saveError = true;
}
if (saveError) {
$userDefaultConfig = getDefaultConfig();
} else {
$userDefaultConfig = {...$config};
}
}
$: configsMatch = configsEqual($config, $userDefaultConfig)
</script>

<style>
</style>

<Button
kind="tertiary"
on:click={setDefaultConfig}
icon={configsMatch ? CheckmarkOutline : null}
>
Use as Default Sheet Config
</Button>
50 changes: 34 additions & 16 deletions src/sheet/Sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ function getDefaultMathCellConfig(): MathCellConfig {
};
}

export const defaultMathConfig = getDefaultMathCellConfig();

export const mathConfigLimits = {
precisionUpper: 64,
lowerExpLower: -12,
Expand All @@ -91,21 +89,29 @@ export const mathConfigLimits = {
};

export function isDefaultMathConfig(config: MathCellConfig): boolean {
return mathConfigsEqual(config, defaultConfig.mathCellConfig);
}

export function mathConfigsEqual(config1: MathCellConfig, config2: MathCellConfig): boolean {
return (
config.symbolicOutput === defaultMathConfig.symbolicOutput &&
config.showIntermediateResults === defaultMathConfig.showIntermediateResults &&
config.formatOptions.notation === defaultMathConfig.formatOptions.notation &&
config.formatOptions.precision === defaultMathConfig.formatOptions.precision &&
config.formatOptions.lowerExp === defaultMathConfig.formatOptions.lowerExp &&
config.formatOptions.upperExp === defaultMathConfig.formatOptions.upperExp
config1.symbolicOutput === config2.symbolicOutput &&
config1.showIntermediateResults === config2.showIntermediateResults &&
config1.formatOptions.notation === config2.formatOptions.notation &&
config1.formatOptions.precision === config2.formatOptions.precision &&
config1.formatOptions.lowerExp === config2.formatOptions.lowerExp &&
config1.formatOptions.upperExp === config2.formatOptions.upperExp
)
}
}

export function isDefaultConfig(config: Config): boolean {
return isDefaultMathConfig(config.mathCellConfig) &&
isDefaultBaseUnits(config.customBaseUnits) &&
config.simplifySymbolicExpressions === true &&
config.convertFloatsToFractions === true;
return configsEqual(config, defaultConfig);
}

export function configsEqual(config1: Config, config2: Config): boolean {
return mathConfigsEqual(config1.mathCellConfig, config2.mathCellConfig) &&
baseUnitsEqual(config1.customBaseUnits, config2.customBaseUnits) &&
config1.simplifySymbolicExpressions === config2.simplifySymbolicExpressions &&
config1.convertFloatsToFractions === config2.convertFloatsToFractions;
}

export function copyMathConfig(input: MathCellConfig): MathCellConfig {
Expand All @@ -125,7 +131,7 @@ export function getSafeMathConfig(config: MathCellConfig): MathCellConfig {

// clamp precision
if (config.formatOptions.precision === null) {
safeConfig.formatOptions.precision = defaultMathConfig.formatOptions.precision;
safeConfig.formatOptions.precision = defaultConfig.mathCellConfig.formatOptions.precision;
} else if(config.formatOptions.precision > mathConfigLimits.precisionUpper) {
safeConfig.formatOptions.precision = mathConfigLimits.precisionUpper;
} else if(config.formatOptions.precision < (config.formatOptions.notation === "fixed" ? 0 : 1)) {
Expand All @@ -134,7 +140,7 @@ export function getSafeMathConfig(config: MathCellConfig): MathCellConfig {

// clamp lowerExp
if (config.formatOptions.lowerExp === null) {
safeConfig.formatOptions.lowerExp = defaultMathConfig.formatOptions.lowerExp;
safeConfig.formatOptions.lowerExp = defaultConfig.mathCellConfig.formatOptions.lowerExp;
} else if(config.formatOptions.lowerExp > mathConfigLimits.lowerExpUpper) {
safeConfig.formatOptions.lowerExp = mathConfigLimits.lowerExpUpper;
} else if(config.formatOptions.lowerExp < mathConfigLimits.lowerExpLower) {
Expand All @@ -143,7 +149,7 @@ export function getSafeMathConfig(config: MathCellConfig): MathCellConfig {

// clamp upperExp
if (config.formatOptions.upperExp === null) {
safeConfig.formatOptions.upperExp = defaultMathConfig.formatOptions.upperExp;
safeConfig.formatOptions.upperExp = defaultConfig.mathCellConfig.formatOptions.upperExp;
} else if(config.formatOptions.upperExp > mathConfigLimits.upperExpUpper) {
safeConfig.formatOptions.upperExp = mathConfigLimits.upperExpUpper;
} else if(config.formatOptions.upperExp < mathConfigLimits.upperExpLower) {
Expand Down Expand Up @@ -295,6 +301,8 @@ export const baseUnitSystems = new Map<BaseUnitSystemNames, CustomBaseUnits>([
],
]);

export const defaultConfig = getDefaultConfig();

export function getDefaultBaseUnits(system: BaseUnitSystemNames = "SI"): CustomBaseUnits {
return {...baseUnitSystems.get(system)};
}
Expand All @@ -304,6 +312,16 @@ export function isDefaultBaseUnits(baseUnits: CustomBaseUnits, system: BaseUnitS
return Object.entries(defaultBaseUnits).reduce((acum, [key, value]) => acum && value === baseUnits[key], true);
}

export function baseUnitsEqual(baseUnits1: CustomBaseUnits, baseUnits2: CustomBaseUnits): boolean {
let result = true;

for (const key in baseUnits1) {
result = result && baseUnits1[key] === baseUnits2[key];
}

return result;
}

export function normalizeConfig(inputConfig: Config | undefined): Config {
const outputConfig = inputConfig ?? getDefaultConfig();

Expand Down
3 changes: 2 additions & 1 deletion src/stores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const inCellInsertMode = writable(false);

export const mathJaxLoaded = writable(false);

export const userDefaultConfig = writable(getDefaultConfig());

export async function addCell(type: CellTypes, index?: number) {
const currentCells = get(cells);
Expand Down Expand Up @@ -135,7 +136,7 @@ export function getSheetJson() {
}

export function resetSheet() {
config.set(getDefaultConfig());
config.set({...get(userDefaultConfig)});
cells.set([]);
title.set(defaultTitle);
results.set([]);
Expand Down

0 comments on commit bfc8bd6

Please sign in to comment.