Skip to content

Commit

Permalink
Make URLs slightly cleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
Cocoa committed Mar 11, 2024
1 parent 261a32f commit dd16d4e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/models/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,18 @@ export const schema = z.object({
});

/** @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,
};
21 changes: 19 additions & 2 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 { schema } from '../models/configuration';
import { defaultConfig, 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,7 +47,8 @@ const fromFormToConfiguration = (form) =>
* @param { import("../models/configuration").Configuration } configuration The configuration to set
*/
const setConfigurationToUrl = (configuration) => {
updateWindowLocation(setUrlParams(window.location.href, configuration));
const cleanedConfig = removeDefaults(configuration);
updateWindowLocation(setUrlParams(window.location.href, cleanedConfig));
};

/**
Expand Down Expand Up @@ -108,3 +109,19 @@ 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;
}

0 comments on commit dd16d4e

Please sign in to comment.