Skip to content

Commit

Permalink
refactor: create HOC for the resizer component
Browse files Browse the repository at this point in the history
  • Loading branch information
hamed-musallam committed Nov 2, 2023
1 parent 3a0b752 commit 549fc19
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 36 deletions.
23 changes: 8 additions & 15 deletions src/component/1d/integral/IntegralResizable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import { Integral } from 'nmr-processing';

import { useChartData } from '../../context/ChartContext';
import { useDispatch } from '../../context/DispatchContext';
import { useGlobal } from '../../context/GlobalContext';
import { useScaleChecked } from '../../context/ScaleContext';
import Resizer from '../../elements/resizer/Resizer';
import { ResizerWithScale } from '../../elements/ResizerWithScale';
import { HighlightEventSource, useHighlight } from '../../highlight/index';
import { useResizerStatus } from '../../hooks/useResizerStatus';

Expand Down Expand Up @@ -52,10 +51,9 @@ function IntegralResizable({
integralFormat,
}: IntegralResizableProps) {
const { height, margin } = useChartData();
const { viewerRef } = useGlobal();
const { scaleX } = useScaleChecked();
const dispatch = useDispatch();
const { id, integral } = integralData;
const { id, integral, to, from } = integralData;
const highlight = useHighlight([id], {
type: HighlightEventSource.INTEGRAL,
extra: { id },
Expand All @@ -74,25 +72,20 @@ function IntegralResizable({
});
}

const from = integralData.from ? scaleX()(integralData.from) : 0;
const to = integralData.to ? scaleX()(integralData.to) : 0;

const bottom = height - margin.bottom;

const isResizeingActive = useResizerStatus('integral');
const isResizingActive = useResizerStatus('integral');

return (
<g
onMouseEnter={() => highlight.show()}
onMouseLeave={() => highlight.hide()}
>
<Resizer
tag="svg"
initialPosition={{ x1: to, x2: from }}
<ResizerWithScale
from={from}
to={to}
onEnd={handleOnStopResizing}
parentElement={viewerRef}
key={`${id}_${to}_${from}`}
disabled={!isResizeingActive}
disabled={!isResizingActive}
>
{({ x1, x2 }, isActive) => {
const width = x2 - x1;
Expand All @@ -119,7 +112,7 @@ function IntegralResizable({
</g>
);
}}
</Resizer>
</ResizerWithScale>
</g>
);
}
Expand Down
30 changes: 9 additions & 21 deletions src/component/1d/ranges/Range.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/** @jsxImportSource @emotion/react */
import { css } from '@emotion/react';
import { Range as RangeType } from 'nmr-processing';
import { useEffect, useState } from 'react';

import { isRangeAssigned } from '../../../data/data1d/Spectrum1D/isRangeAssigned';
import { checkRangeKind } from '../../../data/utilities/RangeUtilities';
Expand All @@ -11,8 +10,7 @@ import {
} from '../../assignment/AssignmentsContext';
import { filterForIDsWithAssignment } from '../../assignment/utilities/filterForIDsWithAssignment';
import { useDispatch } from '../../context/DispatchContext';
import { useGlobal } from '../../context/GlobalContext';
import Resizer from '../../elements/resizer/Resizer';
import { ResizerWithScale } from '../../elements/ResizerWithScale';
import { HighlightEventSource, useHighlight } from '../../highlight';
import { useResizerStatus } from '../../hooks/useResizerStatus';
import { options } from '../../toolbar/ToolTypes';
Expand Down Expand Up @@ -47,7 +45,6 @@ function Range({
selectedTool,
relativeFormat,
}: RangeProps) {
const { viewerRef } = useGlobal();
const { id, integration, signals, diaIDs, from, to } = range;
const assignmentData = useAssignmentData();
const assignmentRange = useAssignment(id);
Expand All @@ -63,13 +60,6 @@ function Range({

const scaleX = useScaleX();
const dispatch = useDispatch();
const [position, setPosition] = useState({ x1: 0, x2: 0 });

useEffect(() => {
const x2 = scaleX()(from);
const x1 = scaleX()(to);
setPosition({ x1, x2 });
}, [from, scaleX, to]);

const isBlockedByEditing =
selectedTool && selectedTool === options.editRange.id;
Expand Down Expand Up @@ -118,7 +108,7 @@ function Range({
isBlockedByEditing || highlightRange.isActive || assignmentRange.isActive;

const isAssigned = isRangeAssigned(range);
const isResizeingActive = useResizerStatus('rangePicking');
const isResizingActive = useResizerStatus('rangePicking');

return (
<g
Expand All @@ -129,16 +119,14 @@ function Range({
onMouseLeave={mouseLeaveHandler}
{...(!assignmentRange.isActive && { css: style })}
>
<Resizer
tag="svg"
position={position}
<ResizerWithScale
from={from}
to={to}
onEnd={handleOnStopResizing}
parentElement={viewerRef}
disabled={!isResizeingActive}
onMove={(p) => setPosition(p)}
disabled={!isResizingActive}
>
{(xs, isActive) => {
const width = position.x2 - position.x1;
{({ x1, x2 }, isActive) => {
const width = x2 - x1;
return (
<g>
{isAssigned && !isHighlighted && !isActive && (
Expand Down Expand Up @@ -166,7 +154,7 @@ function Range({
</g>
);
}}
</Resizer>
</ResizerWithScale>

{showMultiplicityTrees && (
<MultiplicityTree range={range} onUnlink={unAssignHandler} />
Expand Down
42 changes: 42 additions & 0 deletions src/component/elements/ResizerWithScale.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { useEffect, useState } from 'react';

import { useScaleX } from '../1d/utilities/scale';
import { useGlobal } from '../context/GlobalContext';

import Resizer, { ResizerProps } from './resizer/Resizer';

interface ResizerWithScaleProps {
disabled: boolean;
from: number;
to: number;
onEnd: ResizerProps['onEnd'];
children: ResizerProps['children'];
}

export function ResizerWithScale(props: ResizerWithScaleProps) {
const { from, to, onEnd, disabled, children } = props;
const { viewerRef } = useGlobal();
const scaleX = useScaleX();
const x2 = scaleX()(from);
const x1 = scaleX()(to);
const [position, setPosition] = useState({ x1, x2 });

useEffect(() => {
const x2 = scaleX()(from);
const x1 = scaleX()(to);
setPosition({ x1, x2 });
}, [from, scaleX, to]);

return (
<Resizer
tag="svg"
position={position}
onEnd={onEnd}
parentElement={viewerRef}
disabled={disabled}
onMove={(p) => setPosition(p)}
>
{children}
</Resizer>
);
}

0 comments on commit 549fc19

Please sign in to comment.