Skip to content

Commit

Permalink
remove duplicate code from the stories
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinNagpal committed Oct 29, 2021
1 parent 393d32f commit 457ea98
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 35 deletions.
2 changes: 2 additions & 0 deletions src/components/Annotation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ function Annotation(options: AnnotationProps & WithRelativeMousePosProps) {
{annotations.map((annotation) => (
<RenderShape
key={annotation.data.id}
editMode={props.editorMode}
annotation={annotation}
renderContent={props.renderContent}
isInSelectionMode={!!tmpAnnotation}
Expand All @@ -241,6 +242,7 @@ function Annotation(options: AnnotationProps & WithRelativeMousePosProps) {
{!props.disableSelector && tmpAnnotation?.geometry && (
<RenderShape
key={tmpAnnotation.data.id}
editMode={props.editorMode}
annotation={tmpAnnotation}
renderContent={props.renderContent}
isInSelectionMode={!!tmpAnnotation}
Expand Down
20 changes: 15 additions & 5 deletions src/components/Shapes/withShapeWrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import React, { ComponentType, useState } from 'react';
import { ShapeProps, WrappedShapeProps } from '../../types';
import { EditorMode, ShapeProps, WrappedShapeProps } from '../../types';

export const withShapeWrapper = (
DecoratedShape: ComponentType<ShapeProps>
): ComponentType<
Omit<WrappedShapeProps, 'isMouseOver' | 'onMouseEnter' | 'onMouseLeave'>
> => {
const WrappedComponent = (props: WrappedShapeProps) => {
const { annotation, onClick, renderContent, selectedAnnotation } = props;
const {
annotation,
editMode,
onClick,
renderContent,
selectedAnnotation,
} = props;
const [mouseHovered, setMouseHovered] = useState<boolean>(false);
const reactElement =
mouseHovered && renderContent && props.annotation.data.text

const shouldShowContent =
editMode === EditorMode.Annotate && mouseHovered && renderContent;

const reactContentElement =
shouldShowContent && props.annotation.data.text
? renderContent(props)
: null;

Expand All @@ -25,7 +35,7 @@ export const withShapeWrapper = (
onMouseLeave={() => setMouseHovered(false)}
/>

{reactElement}
{reactContentElement}
</div>
);
};
Expand Down
6 changes: 0 additions & 6 deletions src/components/Stories/AnnotationToolbarButtons.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ import {
RectangleTemplateWithExistingAnnotations,
} from './AnnotationStoryTemplate';

const defaultImageUrl =
'https://raw.githubusercontent.com/RobinNagpal/react-image-annotation-ts/HEAD/example/img.jpeg';

const meta: Meta = {
title: 'StackedAnnotationsHighlightOnly',
component: Annotation,
Expand All @@ -30,21 +27,18 @@ export default meta;

export const Oval = OvalTemplateWithExistingAnnotations.bind({});
Oval.args = {
src: defaultImageUrl,
editorMode: EditorMode.HighlightOnly,
shapes: [OvalSelector.TYPE],
};

export const Rectangle = RectangleTemplateWithExistingAnnotations.bind({});
Rectangle.args = {
src: defaultImageUrl,
editorMode: EditorMode.HighlightOnly,
shapes: [RectangleSelector.TYPE],
};

export const Point = PointTemplateWithExistingAnnotations.bind({});
Point.args = {
src: defaultImageUrl,
editorMode: EditorMode.HighlightOnly,
shapes: [PointSelector.TYPE],
};
7 changes: 1 addition & 6 deletions src/components/Stories/DefaultAnnotation.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import {
DefaultAnnotationStoryTemplate,
} from './AnnotationStoryTemplate';

const defaultImageUrl =
'https://raw.githubusercontent.com/RobinNagpal/react-image-annotation-ts/HEAD/example/img.jpeg';

const meta: Meta = {
title: 'Annotation',
component: Annotation,
Expand All @@ -23,6 +20,4 @@ export default meta;
const StoryTemplate: Story<AnnotationPropsOptional> = DefaultAnnotationStoryTemplate;

export const Default = StoryTemplate.bind({});
Default.args = {
src: defaultImageUrl,
};
Default.args = {};
6 changes: 0 additions & 6 deletions src/components/Stories/HighlightModeAnnotation.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ import {
DefaultAnnotationStoryTemplate,
} from './AnnotationStoryTemplate';

const defaultImageUrl =
'https://raw.githubusercontent.com/RobinNagpal/react-image-annotation-ts/HEAD/example/img.jpeg';

const meta: Meta = {
title: 'HighlightAnnotation',
component: Annotation,
Expand All @@ -31,21 +28,18 @@ const StoryTemplate: Story<AnnotationPropsOptional> = DefaultAnnotationStoryTemp
export const Oval = StoryTemplate.bind({});

Oval.args = {
src: defaultImageUrl,
editorMode: EditorMode.HighlightOnly,
shapes: [OvalSelector.TYPE],
};

export const Rectangle = StoryTemplate.bind({});
Rectangle.args = {
src: defaultImageUrl,
editorMode: EditorMode.HighlightOnly,
shapes: [RectangleSelector.TYPE],
};

export const Point = StoryTemplate.bind({});
Point.args = {
src: defaultImageUrl,
editorMode: EditorMode.HighlightOnly,
shapes: [PointSelector.TYPE],
};
6 changes: 0 additions & 6 deletions src/components/Stories/StackedAnnotationsAnnotate.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ import {
RectangleTemplateWithExistingAnnotations,
} from './AnnotationStoryTemplate';

const defaultImageUrl =
'https://raw.githubusercontent.com/RobinNagpal/react-image-annotation-ts/HEAD/example/img.jpeg';

const meta: Meta = {
title: 'StackedAnnotationsAnnotate',
component: Annotation,
Expand All @@ -30,21 +27,18 @@ export default meta;

export const Oval = OvalTemplateWithExistingAnnotations.bind({});
Oval.args = {
src: defaultImageUrl,
editorMode: EditorMode.Annotate,
shapes: [OvalSelector.TYPE],
};

export const Rectangle = RectangleTemplateWithExistingAnnotations.bind({});
Rectangle.args = {
src: defaultImageUrl,
editorMode: EditorMode.Annotate,
shapes: [RectangleSelector.TYPE],
};

export const Point = PointTemplateWithExistingAnnotations.bind({});
Point.args = {
src: defaultImageUrl,
editorMode: EditorMode.Annotate,
shapes: [PointSelector.TYPE],
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ import {
RectangleTemplateWithExistingAnnotations,
} from './AnnotationStoryTemplate';

const defaultImageUrl =
'https://raw.githubusercontent.com/RobinNagpal/react-image-annotation-ts/HEAD/example/img.jpeg';

const meta: Meta = {
title: 'StackedAnnotationsHighlightOnly',
component: Annotation,
Expand All @@ -30,21 +27,18 @@ export default meta;

export const Oval = OvalTemplateWithExistingAnnotations.bind({});
Oval.args = {
src: defaultImageUrl,
editorMode: EditorMode.HighlightOnly,
shapes: [OvalSelector.TYPE],
};

export const Rectangle = RectangleTemplateWithExistingAnnotations.bind({});
Rectangle.args = {
src: defaultImageUrl,
editorMode: EditorMode.HighlightOnly,
shapes: [RectangleSelector.TYPE],
};

export const Point = PointTemplateWithExistingAnnotations.bind({});
Point.args = {
src: defaultImageUrl,
editorMode: EditorMode.HighlightOnly,
shapes: [PointSelector.TYPE],
};
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export type WrappedShapeProps = Omit<
ShapeProps,
'isMouseOver' | 'onMouseEnter' | 'onMouseLeave'
> & {
editMode: EditorMode;
isInSelectionMode: boolean;
onClick?: (annotation: IAnnotation) => void;
renderContent?: (props: ContentProps) => ReactElement | null;
Expand Down

0 comments on commit 457ea98

Please sign in to comment.