diff --git a/book/book.toml b/book/book.toml index c324f71a..24aa38b8 100644 --- a/book/book.toml +++ b/book/book.toml @@ -1,10 +1,9 @@ [book] language = "en" -multilingual = false src = "src" title = "Lens Book" [output.html] git-repository-url = "https://github.com/samply/lens/tree/develop/book" -git-repository-icon = "fa-github" +git-repository-icon = "fab-github" edit-url-template = "https://github.com/samply/lens/edit/develop/book/{path}" diff --git a/src/stores/catalogue.ts b/src/stores/catalogue.ts index d691c891..75ebfef1 100644 --- a/src/stores/catalogue.ts +++ b/src/stores/catalogue.ts @@ -322,20 +322,22 @@ export const getCriteriaFromKey = ( }; /** - * Set the catalogue. A warning is logged to the browser console if the catalogue does not match the JSON schema. + * Set the catalogue. A warning is logged to the browser console if the catalogue does not match the JSON schema. Note that the function makes a deep copy of the catalogue so modifying the original object has no effect. */ -export function setCatalogue(cat: Catalogue) { - catalogue.set(cat); +export function setCatalogue(newCatalogue: Catalogue) { + // Make a copy to avoid modifying the original object + const catalogueCopy = structuredClone(newCatalogue); const ajv = new Ajv({ allErrors: true, removeAdditional: true, }); addFormats(ajv); - const valid = ajv.validate(catalogueSchema, cat); + const valid = ajv.validate(catalogueSchema, catalogueCopy); if (!valid) { console.warn( "Catalogue does not conform with JSON schema: " + JSON.stringify(ajv.errors), ); } + catalogue.set(catalogueCopy); } diff --git a/src/stores/options.ts b/src/stores/options.ts index 613b399b..ea218afd 100644 --- a/src/stores/options.ts +++ b/src/stores/options.ts @@ -8,20 +8,22 @@ import optionsSchema from "../../schema/options.schema.json"; export const lensOptions = writable(); /** - * Set the options. A warning is logged to the browser console if the options do not match the JSON schema. + * Set the options. A warning is logged to the browser console if the options do not match the JSON schema. Note that the function makes a deep copy of the options so modifying the original object has no effect. */ -export function setOptions(options: LensOptions) { - lensOptions.set(options); +export function setOptions(newOptions: LensOptions) { + // Make a copy to avoid modifying the original object + const optionsCopy = structuredClone(newOptions); const ajv = new Ajv({ allErrors: true, removeAdditional: true, }); addFormats(ajv); - const valid = ajv.validate(optionsSchema, options); + const valid = ajv.validate(optionsSchema, optionsCopy); if (!valid) { console.warn( "Options do not conform with JSON schema: " + JSON.stringify(ajv.errors), ); } + lensOptions.set(optionsCopy); } diff --git a/src/stores/query.ts b/src/stores/query.ts index 170d52b3..3d30415c 100644 --- a/src/stores/query.ts +++ b/src/stores/query.ts @@ -12,8 +12,11 @@ export function getQueryStore(): QueryItem[][] { return get(queryStore); } +/** + * Set the query store. Note that the function makes a deep copy of the query store so modifying the original object has no effect. + */ export function setQueryStore(query: QueryItem[][]): void { - queryStore.set(query); + queryStore.set(structuredClone(query)); } /** @@ -216,10 +219,10 @@ function findObjectsWithSameName(objectsArray: QueryItem[]): QueryItem[] { } /** - * Adds an item to the currently active query group + * Adds an item to the currently active query group. Note that the function makes a deep copy of the query item so modifying the original object has no effect. * @param queryObject - the object to be added to the store */ export const addItemToActiveQueryGroup = (queryObject: QueryItem): void => { const groupIndex = get(activeQueryGroupIndex); - addItemToQuery(queryObject, groupIndex); + addItemToQuery(structuredClone(queryObject), groupIndex); };