Skip to content

Commit

Permalink
fix: get user default config from idb each time a new sheet is created
Browse files Browse the repository at this point in the history
Covers case where user sets default config on one tab and uses it on another
  • Loading branch information
mgreminger committed Nov 17, 2024
1 parent bfc8bd6 commit b1b9967
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 24 deletions.
16 changes: 2 additions & 14 deletions src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
history, insertedSheets, activeCell, getSheetJson, getSheetObject, resetSheet, sheetId,
mathCellChanged, nonMathCellChanged, addCell, prefersReducedMotion, modifierKey,
inCellInsertMode, config, unsavedChange, incrementActiveCell,
decrementActiveCell, deleteCell, activeMathField, autosaveNeeded, mathJaxLoaded,
userDefaultConfig
} from "./stores";
decrementActiveCell, deleteCell, activeMathField, autosaveNeeded, mathJaxLoaded } from "./stores";
import { isDefaultConfig, type Config, normalizeConfig } from "./sheet/Sheet";
import type { Statement, SubQueryStatement } from "./parser/types";
import type { SystemDefinition } from "./cells/SystemCell";
Expand Down Expand Up @@ -358,16 +356,6 @@
$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 @@ -732,7 +720,7 @@
async function initializeBlankSheet() {
currentStateObject = null;
resetSheet();
await resetSheet();
await tick();
addCell('math');
await tick();
Expand Down
24 changes: 19 additions & 5 deletions src/SetDefaultConfigDialog.svelte
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
<script lang="ts">
import { onMount } from "svelte";
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 { config, userDefaultConfig } from "./stores";
import { set } from 'idb-keyval';
import { config } from "./stores";
let userDefaultConfig: Config | undefined = getDefaultConfig();
onMount(async () => {
try {
userDefaultConfig = await get('defaultConfig');
} catch(e) {
console.warn('Error attempting to load user default config');
userDefaultConfig = undefined;
}
userDefaultConfig = userDefaultConfig ?? getDefaultConfig();
});
async function setDefaultConfig() {
let saveError = false;
try {
await set('defaultConfig', $config);
} catch (e) {
console.warn('Error attempting to save user default config');
saveError = true;
}
if (saveError) {
$userDefaultConfig = getDefaultConfig();
userDefaultConfig = getDefaultConfig();
} else {
$userDefaultConfig = {...$config};
userDefaultConfig = JSON.parse(JSON.stringify($config));
}
}
$: configsMatch = configsEqual($config, $userDefaultConfig)
$: configsMatch = configsEqual($config, userDefaultConfig)
</script>

Expand Down
19 changes: 14 additions & 5 deletions src/stores.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type Writable, writable, type Readable, readable, get } from 'svelte/store';
import { get as idbGet } from 'idb-keyval';

import type { Cell } from './cells/Cells';
import { BaseCell, type CellTypes } from './cells/BaseCell';
Expand All @@ -17,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 InsertedSheet, type Sheet, getDefaultConfig } from './sheet/Sheet';
import { type Config, type InsertedSheet, type Sheet, getDefaultConfig } from './sheet/Sheet';

const defaultTitle = 'New Sheet';

Expand Down Expand Up @@ -54,8 +55,6 @@ 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);
const currentResults = get(results);
Expand Down Expand Up @@ -135,8 +134,18 @@ export function getSheetJson() {
return ' ' + JSON.stringify(sheet);
}

export function resetSheet() {
config.set({...get(userDefaultConfig)});
export async function resetSheet() {
let defaultConfig: Config | undefined;

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

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

0 comments on commit b1b9967

Please sign in to comment.