Skip to content

Commit

Permalink
Merge pull request #297 from mgreminger/set-default-config
Browse files Browse the repository at this point in the history
feature: set default sheet config
  • Loading branch information
mgreminger authored Nov 24, 2024
2 parents 7e86202 + b197066 commit 25f2f87
Show file tree
Hide file tree
Showing 8 changed files with 308 additions and 44 deletions.
25 changes: 12 additions & 13 deletions src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
history, insertedSheets, activeCell, getSheetJson, getSheetObject, resetSheet, sheetId,
mathCellChanged, nonMathCellChanged, addCell, prefersReducedMotion, modifierKey,
inCellInsertMode, config, unsavedChange, incrementActiveCell,
decrementActiveCell, deleteCell, activeMathField, autosaveNeeded, mathJaxLoaded
} from "./stores";
import { getDefaultBaseUnits, getDefaultFluidConfig, isDefaultConfig } from "./sheet/Sheet";
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";
import type { FluidFunction } from "./cells/FluidCell";
Expand Down Expand Up @@ -91,12 +90,13 @@
import BaseUnitsConfigDialog from "./BaseUnitsConfigDialog.svelte";
import DownloadDocumentModal from "./DownloadDocumentModal.svelte";
import { getBlankStatement } from "./parser/LatexToSympy";
import SetDefaultConfigDialog from "./SetDefaultConfigDialog.svelte";
createCustomUnits();
const apiUrl = window.location.origin;
const currentVersion = 20241102;
const currentVersion = 20241123;
const tutorialHash = "moJCuTwjPi7dZeZn5QiuaP";
const termsVersion = 20240110;
Expand Down Expand Up @@ -720,7 +720,7 @@
async function initializeBlankSheet() {
currentStateObject = null;
resetSheet();
await resetSheet();
await tick();
addCell('math');
await tick();
Expand Down Expand Up @@ -1216,15 +1216,10 @@ Please include a link to this sheet in the email to assist in debugging the prob
$title = sheet.title;
BaseCell.nextId = sheet.nextId;
$sheetId = sheet.sheetId;
// old documents in database will not have the insertedSheets property or a config property
// old documents in database will not have the insertedSheets property
$insertedSheets = sheet.insertedSheets ?? [];
$config = sheet.config ?? getDefaultConfig();
$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
$config = normalizeConfig(sheet.config);
$cells = await Promise.all(sheet.cells.map((value) => cellFactory(value, $config)));
if (!$history.map(item => item.hash !== "file" ? getSheetHash(new URL(item.url)) : "").includes(getSheetHash(window.location))) {
Expand Down Expand Up @@ -2825,6 +2820,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 User Default" />
<svelte:fragment slot="content">
<TabContent>
<Checkbox
Expand All @@ -2848,6 +2844,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
27 changes: 17 additions & 10 deletions src/RequestPersistentStorage.svelte
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
<script lang="ts">
import { onMount } from "svelte";
import { Button } from "carbon-components-svelte";
import Error from "carbon-icons-svelte/lib/Error.svelte";
import Checkmark from "carbon-icons-svelte/lib/Checkmark.svelte";
export let numCheckpoints: number;
export let numCheckpoints: number = undefined;
let persisted = false;
let hostName: string;
onMount(async () => {
persisted = await navigator.storage.persisted();
hostName = window.location.host;
});
async function requestPersistentStorage() {
Expand All @@ -18,8 +22,9 @@
</script>

<p>
EngineeringPaper.xyz uses your browser's local storage to store your most recent {numCheckpoints} autosave
checkpoints and your list of recently visited sheets. Your web browser will not automatically persist this
EngineeringPaper.xyz uses your browser's local storage to store your default sheet config,
your {numCheckpoints ?? ""} most recent autosave
checkpoints, and your list of recently visited sheets. Your web browser will not automatically persist this
local storage and may clear it at any time. Safari is particularly aggressive about freeing this storage
and will automatically clear local storage for a site that has not been visited in the previous seven days.
</p>
Expand All @@ -28,8 +33,8 @@

<p>
Click the button below to request that your browser enables persistent local storage for the
EngineeringPaper.xyz domain. Your browser may popup a dialog that asks you to approve this request.
Chrome and Edge require you to bookmark EngineeringPaper.xyz in order to enable persistent storage.
{hostName} domain. Your browser may popup a dialog that asks you to approve this request.
Chrome and Edge may require you to bookmark {hostName} in order to enable persistent storage.
</p>

<br>
Expand All @@ -45,9 +50,11 @@
<br>

<p>
<button
on:click={requestPersistentStorage}
>
Request Persistent Storage
</button>
<Button
kind="tertiary"
disabled={persisted}
on:click={requestPersistentStorage}
>
Request Persistent Storage
</Button>
</p>
105 changes: 105 additions & 0 deletions src/SetDefaultConfigDialog.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<script lang="ts">
import { onMount } from "svelte";
import { del, get, set } from 'idb-keyval';
import { Button } from "carbon-components-svelte";
import Checkmark from "carbon-icons-svelte/lib/Checkmark.svelte";
import Information from "carbon-icons-svelte/lib/Information.svelte";
import { type Config, configsEqual, getDefaultConfig, normalizeConfig, isDefaultConfig } from "./sheet/Sheet";
import { config } from "./stores";
import RequestPersistentStorage from "./RequestPersistentStorage.svelte";
let userDefaultConfig: Config = getDefaultConfig();
onMount(async () => {
try {
userDefaultConfig = normalizeConfig(await get('defaultConfig'));
} catch(e) {
console.warn('Error attempting to load user default config');
userDefaultConfig = getDefaultConfig();
}
});
async function setDefaultConfig() {
if (currentConfigIsDefaultConfig) {
try {
await del('defaultConfig')
} catch(e) {
console.warn('Error attempting to delete user config');
}
userDefaultConfig = getDefaultConfig();
return;
}
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();
} else {
userDefaultConfig = JSON.parse(JSON.stringify($config));
}
}
function useDefaultConfig() {
$config = JSON.parse(JSON.stringify(userDefaultConfig));
}
$: configsMatch = configsEqual($config, userDefaultConfig);
$: currentConfigIsDefaultConfig = isDefaultConfig($config);
</script>

<style>
div.container {
display: flex;
flex-direction: column;
gap: 20px;
}
</style>


<div class="container">
<p>
{#if configsMatch}
<Checkmark color="green"/> The current sheet config matches the user default config.
{:else if !currentConfigIsDefaultConfig}
<Information color="blue"/> The current sheet config differs from the user default config, the buttons
below can be used to either save this sheet's config as the user default config or apply the user default
config to this sheet.
{:else}
<Information color="blue"/> The current sheet is using the EngineeringPaper.xyz default config which
is different than the user default config. The user default config can be applied to this sheet using
the second button below.
{/if}
</p>

<div class="button-container">
<Button
kind="tertiary"
on:click={setDefaultConfig}
disabled={configsMatch}
>
Use This Sheet's Config as the User Default Config
</Button>

<Button
kind="tertiary"
on:click={useDefaultConfig}
disabled={configsMatch}
>
Apply the User Default Config to This Sheet
</Button>
</div>

<div>
<RequestPersistentStorage />
</div>

</div>
13 changes: 13 additions & 0 deletions src/Updates.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@
}
</style>

<em>November 23, 2024</em>
<h4>New Custom Default Sheet Settings Feature</h4>
<p>
You can now set your own custom default sheet settings (default units, default number
formating, etc.). These defaults will be used whenever you create a new sheet
and these settings can also be applied to existing sheets. All of this functionality
is accessible through the "Set User Default" tab of the "Sheet Settings"
dialog (available using the sheet settings button <SettingsAdjust size={16}/>
at the top toolbar).
</p>

<br>

<em>November 2, 2024</em>
<h4>Matrix Multiplication Improvements</h4>
<p>
Expand Down
64 changes: 48 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,11 +301,37 @@ export const baseUnitSystems = new Map<BaseUnitSystemNames, CustomBaseUnits>([
],
]);

export const defaultConfig = getDefaultConfig();

export function getDefaultBaseUnits(system: BaseUnitSystemNames = "SI"): CustomBaseUnits {
return {...baseUnitSystems.get(system)};
}

export function isDefaultBaseUnits(baseUnits: CustomBaseUnits, system: BaseUnitSystemNames = "SI"): boolean {
const defaultBaseUnits = baseUnitSystems.get(system);
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(config: Config | undefined): Config {
if (!config) {
return getDefaultConfig();
}

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 config;
}
Loading

0 comments on commit 25f2f87

Please sign in to comment.