From 421a617a38830574d62346f630b2fa66170c8376 Mon Sep 17 00:00:00 2001 From: Cocoa Date: Tue, 12 Mar 2024 13:42:59 -0400 Subject: [PATCH] Add more defaults and move handling to a more logical location --- src/models/configuration.js | 19 +++---------------- src/utils/formUtils.js | 21 ++------------------- src/utils/uri.js | 7 +++++++ 3 files changed, 12 insertions(+), 35 deletions(-) diff --git a/src/models/configuration.js b/src/models/configuration.js index 1ba5950..ab3c71a 100644 --- a/src/models/configuration.js +++ b/src/models/configuration.js @@ -94,23 +94,10 @@ export const schema = z.object({ foreEdgePaddingPt: urlSafe(z.coerce.number()).default(0), // specific to wacky small wackySpacing, // specific to wacky small flyleafs: urlSafe(z.coerce.number()).default(1), - paperSizeCustomWidth: urlSafe(z.coerce.number()), - paperSizeCustomHeight: urlSafe(z.coerce.number()), + paperSizeCustomWidth: urlSafe(z.coerce.number()).default(0), + paperSizeCustomHeight: urlSafe(z.coerce.number()).default(0), }); /** @typedef {z.infer} Configuration */ -export const defaultConfig = { - rotatePage: false, - paperRotation90: false, - cropMarks: false, - cutMarks: false, - pdfEdgeMarks: false, - mainForeEdgePaddingPt: 0, - bindingEdgePaddingPt: 0, - topEdgePaddingPt: 0, - bottomEdgePaddingPt: 0, - sigLength: 4, // Specific to standard - foreEdgePaddingPt: 0, // specific to wacky small - flyleafs: 1, -}; +export const defaultConfig = schema.parse({}); diff --git a/src/utils/formUtils.js b/src/utils/formUtils.js index dcdbbb4..a477b5b 100644 --- a/src/utils/formUtils.js +++ b/src/utils/formUtils.js @@ -2,7 +2,7 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. -import { defaultConfig, schema } from '../models/configuration'; +import { schema } from '../models/configuration'; import { clearLocalSettings, getLocalSettings, setLocalSettings } from './localStorageUtils'; import { renderFormFromSettings, renderInfoBox, renderPageCount, renderWacky } from './renderUtils'; import { clearUrlParams, setUrlParams, toUrlParams, updateWindowLocation } from './uri'; @@ -47,8 +47,7 @@ const fromFormToConfiguration = (form) => * @param { import("../models/configuration").Configuration } configuration The configuration to set */ const setConfigurationToUrl = (configuration) => { - const cleanedConfig = removeDefaults(configuration); - updateWindowLocation(setUrlParams(window.location.href, cleanedConfig)); + updateWindowLocation(setUrlParams(window.location.href, configuration)); }; /** @@ -109,19 +108,3 @@ export const resetForm = () => { updateWindowLocation(clearUrlParams(window.location.href)); return loadForm(); }; - -/** - * Returns a version of the configuration with any items set to default values removed - * to keep url strings a little cleaner. - * @param { import("../models/configuration").Configuration } configuration The form to save - * @returns { import("../models/configuration").Configuration } The updated configuration set - */ -function removeDefaults(configuration) { - const cleaned = {}; - Object.keys(configuration).forEach((key) => { - if (defaultConfig[key] != configuration[key]) { - cleaned[key] = configuration[key]; - } - }); - return cleaned; -} diff --git a/src/utils/uri.js b/src/utils/uri.js index 51092f6..33849ce 100644 --- a/src/utils/uri.js +++ b/src/utils/uri.js @@ -2,6 +2,8 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. +import { defaultConfig } from '../models/configuration'; + /** * Gets parameters from a URL. * @param { string } url The URL to get the params from @@ -26,6 +28,11 @@ export const setUrlParams = (url, params) => { continue; } + if (value === defaultConfig[key]) { + urlRepresentation.searchParams.delete(key); + continue; + } + urlRepresentation.searchParams.set(key, String(value)); }