From 7f6b32dc207f5890de6d646567e6ce973a0bf07a Mon Sep 17 00:00:00 2001 From: hamed musallam Date: Fri, 22 Nov 2024 10:46:47 +0100 Subject: [PATCH] fix: delete filter --- .../Filters/FiltersSectionsPanel.tsx | 2 +- .../reducer/actions/FiltersActions.ts | 34 +++++++++++++++---- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/component/panels/filtersPanel/Filters/FiltersSectionsPanel.tsx b/src/component/panels/filtersPanel/Filters/FiltersSectionsPanel.tsx index c245e2ed4..647616cab 100644 --- a/src/component/panels/filtersPanel/Filters/FiltersSectionsPanel.tsx +++ b/src/component/panels/filtersPanel/Filters/FiltersSectionsPanel.tsx @@ -21,7 +21,7 @@ import useSpectrum from '../../../hooks/useSpectrum.js'; import { filterOptionPanels } from './index.js'; -const nonRemovableFilters = new Set([ +export const nonRemovableFilters = new Set([ 'digitalFilter', 'digitalFilter2D', ]); diff --git a/src/component/reducer/actions/FiltersActions.ts b/src/component/reducer/actions/FiltersActions.ts index f1318d320..84f183528 100644 --- a/src/component/reducer/actions/FiltersActions.ts +++ b/src/component/reducer/actions/FiltersActions.ts @@ -27,6 +27,7 @@ import { isSpectrum2D } from '../../../data/data2d/Spectrum2D/index.js'; import type { ExclusionZone } from '../../../data/types/data1d/ExclusionZone.js'; import { getXScale } from '../../1d/utilities/scale.js'; import { get2DXScale, get2DYScale } from '../../2d/utilities/scale.js'; +import { nonRemovableFilters } from '../../panels/filtersPanel/Filters/FiltersSectionsPanel.js'; import type { Tool } from '../../toolbar/ToolTypes.js'; import { options as Tools } from '../../toolbar/ToolTypes.js'; import { getSpectraByNucleus } from '../../utility/getSpectraByNucleus.js'; @@ -1179,6 +1180,31 @@ function handleEnableFilter(draft: Draft, action: EnableFilterAction) { } } +function deleteFilter(datum: Spectrum, id?: string) { + const filters = datum.filters.slice(0); + + let removedFilter; + if (!id) { + datum.filters = filters.filter((filter) => + nonRemovableFilters.has(filter.name), + ) as typeof filters; + } else { + removedFilter = datum.filters.find((filter) => filter.id === id); + datum.filters = filters.filter( + (filter) => filter.id !== id, + ) as typeof filters; + } + + // do not reprocess the filters when the deleted filter is inactive + if (removedFilter && !removedFilter.enabled) return; + + if (isSpectrum1D(datum)) { + Filters1DManager.reapplyFilters(datum); + } else { + Filters2DManager.reapplyFilters(datum); + } +} + //action function handleDeleteFilter(draft: Draft, action: DeleteFilterAction) { const activeSpectrum = getActiveSpectrum(draft); @@ -1189,14 +1215,8 @@ function handleDeleteFilter(draft: Draft, action: DeleteFilterAction) { const filterID = action?.payload?.id; const datum = draft.data[activeSpectrum.index]; + deleteFilter(datum, filterID); - //apply filter into the spectrum - if (isSpectrum1D(datum)) { - Filters1DManager.deleteFilter(datum, filterID); - } - if (isSpectrum2D(datum)) { - Filters2DManager.deleteFilter(datum, filterID); - } draft.toolOptions.data.activeFilterID = null; resetSelectedTool(draft); setDomain(draft);