Skip to content

Commit

Permalink
feat: inset brush end
Browse files Browse the repository at this point in the history
  • Loading branch information
hamed-musallam committed Jan 31, 2025
1 parent 4425e3a commit 3608177
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 24 deletions.
22 changes: 15 additions & 7 deletions src/component/1d/BrushTracker1D.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ export function BrushTracker1D({ children }) {

if (!inset) return;

dispatch({ type: 'MOVE_INSET', payload: { insetKey: inset.id, shiftX, shiftY: 0 } });
dispatch({
type: 'MOVE_INSET',
payload: { insetKey: inset.id, shiftX, shiftY: 0 },
});
}
}

Expand Down Expand Up @@ -272,15 +275,20 @@ export function BrushTracker1D({ children }) {
brushStartRef.current = null;

const keyModifiers = getModifiersKey(brushData as unknown as MouseEvent);

if (
brushData.mouseButton === 'main' &&
!inset ||
brushData.mouseButton !== 'main' ||
keyModifiers !== primaryKeyIdentifier
) {
dispatch({ type: 'BRUSH_END', payload: brushData });
}
)
return;

Check failure on line 283 in src/component/1d/BrushTracker1D.tsx

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

Expected { after 'if' condition

const { startX, endX } = brushData;
dispatch({
type: 'BRUSH_END_INSET',
payload: { insetKey: inset.id, startX, endX },
});
},
[getModifiersKey, primaryKeyIdentifier, dispatch],
[getModifiersKey, inset, primaryKeyIdentifier, dispatch],
);

const handleOnDoubleClick = useCallback(() => {
Expand Down
14 changes: 8 additions & 6 deletions src/component/1d/inset/InsetProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import type { Margin } from '../../reducer/Reducer.js';

import type { Inset } from './SpectraInsets.js';

export const insetMargin: Margin = {
top: 10,
right: 10,
bottom: 30,
left: 10,
} as const;

interface BaseInsetPagContextProps
extends Pick<Inset, 'xDomain' | 'yDomain' | 'id' | 'spectrumKey'> {
width: number;
Expand Down Expand Up @@ -49,12 +56,7 @@ export function InsetProvider(props: InsetProviderProps) {
return {
width,
height,
margin: {
top: 10,
right: 10,
bottom: 30,
left: 10,
},
margin: insetMargin,
id,
xDomain,
yDomain,
Expand Down
3 changes: 3 additions & 0 deletions src/component/reducer/Reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,9 @@ function innerSpectrumReducer(draft: Draft<State>, action: Action) {
case 'MOVE_INSET': {
return InsetActions.handleMoveInset(draft, action);
}
case 'BRUSH_END_INSET': {
return InsetActions.handleInsetBrushEnd(draft, action);
}
case 'SECRET_THROW_ERROR': {
throw new Error('Error thrown in main reducer');
}
Expand Down
3 changes: 2 additions & 1 deletion src/component/reducer/actions/DomainActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,8 @@ function handleSetAxisDomain(draft: Draft<State>, action: SetAxisDomainAction) {
}

interface Domain {
xDomain: number[]; yDomain: number[]
xDomain: number[];
yDomain: number[];
}

function moveOverAxis(
Expand Down
56 changes: 46 additions & 10 deletions src/component/reducer/actions/InsetActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { v4 } from '@lukeed/uuid';
import type { Draft } from 'immer';

import { isSpectrum1D } from '../../../data/data1d/Spectrum1D/isSpectrum1D.js';
import { insetMargin } from '../../1d/inset/InsetProvider.js';
import type { Inset, InsetBounding } from '../../1d/inset/SpectraInsets.js';
import { getInsetXScale } from '../../1d/utilities/scale.js';
import type { State } from '../Reducer.js';
import { getActiveSpectrum } from '../helper/getActiveSpectrum.js';
import getRange from '../helper/getRange.js';
Expand All @@ -11,6 +13,11 @@ import type { ActionType } from '../types/ActionType.js';
import { moveOverAxis } from './DomainActions.js';
import type { MoveOptions } from './DomainActions.js';

interface BrushInsetBoundary {
startX: number;
endX: number;
}

type AddInsetAction = ActionType<'ADD_INSET', { startX: number; endX: number }>;
type DeleteInsetAction = ActionType<'DELETE_INSET', { insetKey: string }>;
type ChangeInsetBoundingAction = ActionType<
Expand All @@ -21,12 +28,17 @@ type MoveInsetAction = ActionType<
'MOVE_INSET',
MoveOptions & { insetKey: string }
>;
type BrushEndInsetAction = ActionType<
'BRUSH_END_INSET',
BrushInsetBoundary & { insetKey: string }
>;

export type InsetsActions =
| AddInsetAction
| DeleteInsetAction
| ChangeInsetBoundingAction
| MoveInsetAction;
| MoveInsetAction
| BrushEndInsetAction;

function handleAddInset(draft: Draft<State>, action: AddInsetAction) {
const activeSpectrum = getActiveSpectrum(draft);
Expand Down Expand Up @@ -88,9 +100,7 @@ function handleDeleteInset(draft: Draft<State>, action: DeleteInsetAction) {
);
}


function getInset(draft: Draft<State>, insetKey: string) {

const {
view: {
spectra: { activeTab },
Expand All @@ -99,10 +109,8 @@ function getInset(draft: Draft<State>, insetKey: string) {

const insets = draft.insets[activeTab];
return insets.find((inset) => inset.id === insetKey);

}


function handleChangeInsetBounding(
draft: Draft<State>,
action: ChangeInsetBoundingAction,
Expand All @@ -116,16 +124,13 @@ function handleChangeInsetBounding(
inset.bounding = { ...inset.bounding, ...bounding };
}




function handleMoveInset(draft: Draft<State>, action: MoveInsetAction) {
const { insetKey, shiftX, shiftY } = action.payload;
const inset = getInset(draft, insetKey);

if (!inset) return;

const originXDomain = draft.originDomain.xDomains[inset.spectrumKey]
const originXDomain = draft.originDomain.xDomains[inset.spectrumKey];
const originYDomain = draft.originDomain.yDomains[inset.spectrumKey];
const { xDomain, yDomain } = moveOverAxis(
{ shiftX, shiftY },
Expand All @@ -136,5 +141,36 @@ function handleMoveInset(draft: Draft<State>, action: MoveInsetAction) {
inset.yDomain = yDomain;
}

function handleInsetBrushEnd(draft: Draft<State>, action: BrushEndInsetAction) {
const { insetKey, startX: inputStartX, endX: inputEndX } = action.payload;
const inset = getInset(draft, insetKey);

if (!inset) return;

const { mode } = draft;

const {
xDomain: currentXDomain,
bounding: { width },
} = inset;

const xScale = getInsetXScale({
width,
xDomain: currentXDomain,
mode,
margin: insetMargin,
});

const startX = xScale.invert(inputStartX);
const endX = xScale.invert(inputEndX);
const xDomain = startX > endX ? [endX, startX] : [startX, endX];
inset.xDomain = xDomain;
}

export { handleAddInset, handleDeleteInset, handleChangeInsetBounding, handleMoveInset };
export {
handleAddInset,
handleDeleteInset,
handleChangeInsetBounding,
handleMoveInset,
handleInsetBrushEnd,
};

0 comments on commit 3608177

Please sign in to comment.