Skip to content

Commit

Permalink
Add more defaults and move handling to a more logical location
Browse files Browse the repository at this point in the history
  • Loading branch information
Cocoa committed Mar 12, 2024
1 parent dd16d4e commit 421a617
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 35 deletions.
19 changes: 3 additions & 16 deletions src/models/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof schema>} 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({});
21 changes: 2 additions & 19 deletions src/utils/formUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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));
};

/**
Expand Down Expand Up @@ -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;
}
7 changes: 7 additions & 0 deletions src/utils/uri.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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));
}

Expand Down

0 comments on commit 421a617

Please sign in to comment.