Skip to content

Commit

Permalink
fix: delete filter
Browse files Browse the repository at this point in the history
  • Loading branch information
hamed-musallam committed Nov 22, 2024
1 parent b860f84 commit 7f6b32d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import useSpectrum from '../../../hooks/useSpectrum.js';

import { filterOptionPanels } from './index.js';

const nonRemovableFilters = new Set<BaseFilterEntry['name']>([
export const nonRemovableFilters = new Set<BaseFilterEntry['name']>([
'digitalFilter',
'digitalFilter2D',
]);
Expand Down
34 changes: 27 additions & 7 deletions src/component/reducer/actions/FiltersActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -1179,6 +1180,31 @@ function handleEnableFilter(draft: Draft<State>, 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<State>, action: DeleteFilterAction) {
const activeSpectrum = getActiveSpectrum(draft);
Expand All @@ -1189,14 +1215,8 @@ function handleDeleteFilter(draft: Draft<State>, 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);
Expand Down

0 comments on commit 7f6b32d

Please sign in to comment.