Skip to content

Commit

Permalink
Use types
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinNagpal committed Oct 28, 2021
1 parent 954c20d commit 95242ea
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 23 deletions.
27 changes: 17 additions & 10 deletions src/components/Annotation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ export type AnnotationPropsOptional = {
[K in keyof AnnotationProps]?: AnnotationProps[K]; // so that it retains the types
};

const Annotation: ComponentType<AnnotationPropsOptional> = compose(
withRelativeMousePos()
)(function Annotation(options: AnnotationProps & WithRelativeMousePosProps) {
function Annotation(options: AnnotationProps & WithRelativeMousePosProps) {
const props: AnnotationProps = {
...defaultProps,
...options,
};

const [selectorType, setSelectorType] = useState<string>(props.shapes[0]);
const [selectedSelectorType, setSelectedSelectorType] = useState<string>(
props.shapes[0]
);
const targetRef = React.createRef<any>();

const addTargetTouchEventListeners = () => {
Expand Down Expand Up @@ -135,14 +135,14 @@ const Annotation: ComponentType<AnnotationPropsOptional> = compose(
if (!!options[methodName]) {
(options[methodName] as any)(e);
} else {
const selector = getSelectorByType(selectorType);
const selector = getSelectorByType(selectedSelectorType);
if (selector && (selector.methods[methodName] as any)) {
const value = (selector.methods[methodName] as any)(props.value, e);

if (typeof value === 'undefined') {
if (process.env.NODE_ENV !== 'production') {
console.error(`
${methodName} of selector type ${selectorType} returned undefined.
${methodName} of selector type ${selectedSelectorType} returned undefined.
Make sure to explicitly return the previous state
`);
}
Expand Down Expand Up @@ -175,7 +175,10 @@ const Annotation: ComponentType<AnnotationPropsOptional> = compose(

return (
<>
<ToolBar selectorType={selectorType} setSelectorType={setSelectorType} />
<ToolBar
selectedSelectorType={selectedSelectorType}
setSelectedSelectorType={setSelectedSelectorType}
/>
<Container
style={props.style}
onMouseLeave={onTargetMouseLeave}
Expand Down Expand Up @@ -216,7 +219,7 @@ const Annotation: ComponentType<AnnotationPropsOptional> = compose(
{!props.disableOverlay &&
renderOverlay({
annotations: props.annotations,
selectorType: selectorType,
selectorType: selectedSelectorType,
})}
{!props.disableEditor &&
props.value &&
Expand All @@ -231,6 +234,10 @@ const Annotation: ComponentType<AnnotationPropsOptional> = compose(
</Container>
</>
);
});
}

const WrappedAnnotation: ComponentType<AnnotationPropsOptional> = compose(
withRelativeMousePos()
)(Annotation);

export default Annotation;
export default WrappedAnnotation;
16 changes: 11 additions & 5 deletions src/components/ToolBar/ToolBar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import React, { ReactElement } from 'react';
import RectangleSelector from './../../hocs/RectangleSelector';
import PointSelector from './../../hocs/PointSelector';
import OvalSelector from './../../hocs/OvalSelector';
import styled from 'styled-components';
import { RenderToolbarProps } from './../../types';
import CircleSvg from './icons/circle.svg';
Expand Down Expand Up @@ -36,13 +39,16 @@ const TrashIcon = styled(StyledIcon)`
`;

// https://www.w3schools.com/howto/howto_css_icon_bar.asp#
export default function ToolBar(props: RenderToolbarProps): ReactElement {
console.log(props);
export default function ToolBar({
setSelectedSelectorType,
}: RenderToolbarProps): ReactElement {
return (
<OptionsBarDiv>
<CircleIcon />
<PointIcon />
<SquareIcon />
<CircleIcon
onClick={() => setSelectedSelectorType(RectangleSelector.TYPE)}
/>
<PointIcon onClick={() => setSelectedSelectorType(PointSelector.TYPE)} />
<SquareIcon onClick={() => setSelectedSelectorType(OvalSelector.TYPE)} />
<TrashIcon />
</OptionsBarDiv>
);
Expand Down
12 changes: 10 additions & 2 deletions src/hocs/OvalSelector.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { getCoordPercentage } from '../utils/offsetCoordinates';
import { IAnnotation, IGeometry, IPoint, SelectionMode } from '../types/index';
import {
IAnnotation,
IGeometry,
IPoint,
ISelector,
SelectionMode,
} from '../types/index';
import { MouseEvent, TouchEvent } from 'react';

const square = (n: number) => Math.pow(n, 2);
Expand Down Expand Up @@ -113,9 +119,11 @@ function pointerMove(annotation: IAnnotation, e: TouchEvent | MouseEvent) {
return annotation;
}

export default {
const OvalSelector: ISelector = {
TYPE,
intersects,
area,
methods,
};

export default OvalSelector;
12 changes: 10 additions & 2 deletions src/hocs/PointSelector.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { IAnnotation, IContainer, IGeometry, IPoint } from '../types/index';
import {
IAnnotation,
IContainer,
IGeometry,
IPoint,
ISelector,
} from '../types/index';
import { getCoordPercentage } from '../utils/offsetCoordinates';
const MARGIN = 6;

Expand Down Expand Up @@ -53,9 +59,11 @@ export const methods = {
},
};

export default {
const PointSelector: ISelector = {
TYPE,
intersects,
area,
methods,
};

export default PointSelector;
6 changes: 4 additions & 2 deletions src/hocs/RectangleSelector.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MouseEvent, TouchEvent } from 'react';
import { IAnnotation, IGeometry, IPoint } from '../types/index';
import { IAnnotation, IGeometry, IPoint, ISelector } from '../types/index';
import { getCoordPercentage } from '../utils/offsetCoordinates';

export const TYPE = 'RECTANGLE';
Expand Down Expand Up @@ -99,9 +99,11 @@ function pointerMove(annotation: IAnnotation, e: TouchEvent | MouseEvent) {
return annotation;
}

export default {
const RectangleSelector: ISelector = {
TYPE,
intersects,
area,
methods,
};

export default RectangleSelector;
4 changes: 2 additions & 2 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ export interface RenderOverlayProps {
}

export interface RenderToolbarProps {
selectorType: string;
setSelectorType: (selector: string) => void;
selectedSelectorType: string;
setSelectedSelectorType: (selector: string) => void;
}

export interface AnnotationProps {
Expand Down

0 comments on commit 95242ea

Please sign in to comment.