Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(drawing): reposition toolbar on drawing #657

Merged
merged 28 commits into from
Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
899e419
fix(drawing): reposition toolbar above highest staged annotation
ChenCodes Dec 16, 2020
1394f6d
fix(drawing): fix lint issues
ChenCodes Dec 16, 2020
365f689
fix(drawing): revert passing down annotations
ChenCodes Dec 16, 2020
d7c9b71
fix(drawing): revert altAxis property change
ChenCodes Dec 16, 2020
0cc2d54
fix(drawing): remove isDrawing check
ChenCodes Dec 16, 2020
67b4222
fix(drawing): add useEffect for popper updates
ChenCodes Dec 16, 2020
dc6a56c
fix(drawing): use top / left instead of x / y
ChenCodes Dec 17, 2020
f9da41d
fix(drawing): cleanup unused prop
ChenCodes Dec 17, 2020
bb24912
fix(drawing): make references DRY
ChenCodes Dec 17, 2020
a61d43f
fix(drawing): use drawnPathGroups to determine when update is called
ChenCodes Dec 17, 2020
c84530e
fix(drawing): set default drawnPathGroups
ChenCodes Dec 17, 2020
cd24c4e
fix(drawing): clean up useEffect
ChenCodes Dec 17, 2020
b4961f6
fix(drawing): use shallow / mocks instead of mount
ChenCodes Dec 17, 2020
5811de2
fix(drawing): move popupRef up to DrawingAnnotations
ChenCodes Dec 17, 2020
cc0d6a8
fix(drawing): update tests
ChenCodes Dec 18, 2020
b9f6b39
fix(drawing): clean up useEffect dependencies
ChenCodes Dec 18, 2020
32d73ba
fix(drawing): revert test
ChenCodes Dec 18, 2020
bcd0a7d
fix(drawing): remove whitespace
ChenCodes Dec 18, 2020
fb1cb0f
fix(drawing): remove drawnPathGroups
ChenCodes Dec 18, 2020
cca2300
fix(drawing): clean up lint issues
ChenCodes Dec 18, 2020
0e64c02
fix(drawing): use forwardRef
ChenCodes Dec 18, 2020
db67524
fix(drawing): remove unused export
ChenCodes Dec 18, 2020
c67716a
fix(drawing): add length check
ChenCodes Dec 18, 2020
9d921c6
fix(drawing): fix lint issues
ChenCodes Dec 18, 2020
9171445
fix(drawing): reference PopupBaseRef
ChenCodes Dec 18, 2020
574208d
fix(drawing): add usePrevious / update tests
ChenCodes Dec 18, 2020
838e4a7
fix(annotations): revert usePrevious change and update tests
ChenCodes Jan 4, 2021
3d9b2c9
Merge branch 'master' into reposition-toolbar-on-drawing
ChenCodes Jan 4, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions src/components/Popups/PopupDrawingToolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import PopupBase from './PopupBase';
import { Options, PopupReference, Rect } from './Popper';
import './PopupDrawingToolbar.scss';

export type PopupBaseRef = PopupBase;

export type Props = {
canComment: boolean;
canRedo: boolean;
Expand Down Expand Up @@ -38,28 +40,21 @@ const options: Partial<Options> = {
{
name: 'preventOverflow',
options: {
padding: 5,
altAxis: true,
ChenCodes marked this conversation as resolved.
Show resolved Hide resolved
padding: 50,
ChenCodes marked this conversation as resolved.
Show resolved Hide resolved
},
},
],
placement: 'top',
};

const PopupDrawingToolbar = ({
canComment,
canRedo,
canUndo,
className,
onDelete,
onRedo,
onReply,
onUndo,
reference,
}: Props): JSX.Element => {
const PopupDrawingToolbar = (props: Props, ref: React.Ref<PopupBaseRef>): JSX.Element => {
const { canComment, canRedo, canUndo, className, onDelete, onRedo, onReply, onUndo, reference } = props;
const intl = useIntl();

return (
<PopupBase
ref={ref}
className={classNames(className, 'ba-PopupDrawingToolbar')}
data-resin-component="popupDrawingToolbar"
options={options}
Expand Down Expand Up @@ -111,4 +106,4 @@ const PopupDrawingToolbar = ({
);
};

export default PopupDrawingToolbar;
export default React.forwardRef(PopupDrawingToolbar);
12 changes: 10 additions & 2 deletions src/drawing/DrawingAnnotations.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React from 'react';
import * as React from 'react';
import classNames from 'classnames';
import DecoratedDrawingPath from './DecoratedDrawingPath';
import DrawingList from './DrawingList';
import DrawingCreator from './DrawingCreator';
import DrawingPathGroup from './DrawingPathGroup';
import DrawingSVG, { DrawingSVGRef } from './DrawingSVG';
import DrawingSVGGroup from './DrawingSVGGroup';
import PopupDrawingToolbar from '../components/Popups/PopupDrawingToolbar';
import PopupDrawingToolbar, { PopupBaseRef } from '../components/Popups/PopupDrawingToolbar';
import { AnnotationDrawing, PathGroup } from '../@types';
import { CreatorItemDrawing, CreatorStatus } from '../store';
import './DrawingAnnotations.scss';
Expand Down Expand Up @@ -54,6 +54,7 @@ const DrawingAnnotations = (props: Props): JSX.Element => {
const hasDrawnPathGroups = drawnPathGroups.length > 0;
const hasStashedPathGroups = stashedPathGroups.length > 0;
const hasPathGroups = hasDrawnPathGroups || hasStashedPathGroups;
const popupDrawingToolbarRef = React.useRef<PopupBaseRef>(null);
const stagedGroupRef = React.useRef<SVGGElement>(null);
const { current: drawingSVGGroup } = stagedGroupRef;

Expand Down Expand Up @@ -85,6 +86,12 @@ const DrawingAnnotations = (props: Props): JSX.Element => {
const handleUndo = (): void => {
undoDrawingPathGroup();
};
React.useEffect(() => {
const { current: popup } = popupDrawingToolbarRef;
if (popup?.popper && drawnPathGroups.length) {
popup.popper.update();
}
}, [drawnPathGroups]);

return (
<>
Expand Down Expand Up @@ -124,6 +131,7 @@ const DrawingAnnotations = (props: Props): JSX.Element => {

{isCreating && hasPathGroups && drawingSVGGroup && canShowPopupToolbar && (
<PopupDrawingToolbar
ref={popupDrawingToolbarRef}
canComment={hasDrawnPathGroups}
canRedo={hasStashedPathGroups}
canUndo={hasDrawnPathGroups}
Expand Down
28 changes: 27 additions & 1 deletion src/drawing/__tests__/DrawingAnnotations-test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import * as React from 'react';
import { act } from 'react-dom/test-utils';
import { mount, ReactWrapper } from 'enzyme';
import DrawingAnnotations, { Props } from '../DrawingAnnotations';
Expand All @@ -11,6 +11,7 @@ import { annotations } from '../__mocks__/drawingData';
import { CreatorStatus } from '../../store';

jest.mock('../DrawingList');
jest.mock('../../components/Popups/PopupDrawingToolbar');

describe('DrawingAnnotations', () => {
const getDefaults = (): Props => ({
Expand Down Expand Up @@ -232,4 +233,29 @@ describe('DrawingAnnotations', () => {
},
);
});

describe('state changes', () => {
test.each`
basePathGroups | nextPathGroups | expected
${[]} | ${[]} | ${0}
${[]} | ${pathGroups} | ${1}
${pathGroups} | ${pathGroups} | ${1}
`(
'should call update $expected times if basePathGroups.length is $basePathGroups.length and nextPathGroups.length is $nextPathGroups.length',
({ basePathGroups, nextPathGroups, expected }) => {
const popupRef = { popper: { update: jest.fn() } };
jest.spyOn(React, 'useRef').mockImplementation(() => ({ current: popupRef }));

const wrapper = getWrapper({
canShowPopupToolbar: true,
drawnPathGroups: basePathGroups,
isCreating: true,
});

wrapper.setProps({ drawnPathGroups: nextPathGroups });

expect(popupRef.popper.update).toHaveBeenCalledTimes(expected);
},
);
});
});