From c5d47e5267334bd807ea75808d0e9d0ca2118cbf Mon Sep 17 00:00:00 2001 From: Mingze Xiao Date: Mon, 16 Nov 2020 23:18:05 -0800 Subject: [PATCH] feat(drawing): Pass down rootEl --- src/drawing/DrawingList.tsx | 51 +++++++++++++---------------------- src/drawing/DrawingPath.tsx | 13 ++++++--- src/drawing/DrawingTarget.tsx | 9 ++++--- 3 files changed, 33 insertions(+), 40 deletions(-) diff --git a/src/drawing/DrawingList.tsx b/src/drawing/DrawingList.tsx index 6e13313f1..65a94e532 100644 --- a/src/drawing/DrawingList.tsx +++ b/src/drawing/DrawingList.tsx @@ -4,7 +4,7 @@ import noop from 'lodash/noop'; import DrawingTarget from './DrawingTarget'; import useOutsideEvent from '../common/useOutsideEvent'; import DrawingSVG, { DrawingSVGRef } from './DrawingSVG'; -import { AnnotationDrawing, Dimensions } from '../@types'; +import { AnnotationDrawing } from '../@types'; import { checkValue } from '../utils/util'; import { getShape } from './drawingUtil'; @@ -32,48 +32,35 @@ export function sortDrawing({ target: targetA }: AnnotationDrawing, { target: ta export function DrawingList({ activeId = null, annotations, className, onSelect = noop }: Props): JSX.Element { const [isListening, setIsListening] = React.useState(true); - const [rootDimensions, setRootDimensions] = React.useState(); - const rootElRef = React.createRef(); - const getRootEl = (): DrawingSVGRef | null => rootElRef.current; + const [rootEl, setRootEl] = React.useState(null); + const setRef = React.useCallback(node => setRootEl(node), []); // Trigger re-render when svg gets resized // Document-level event handlers for focus and pointer control - useOutsideEvent('mousedown', rootElRef, (): void => { + useOutsideEvent('mousedown', { current: rootEl }, (): void => { onSelect(null); setIsListening(false); }); - useOutsideEvent('mouseup', rootElRef, (): void => setIsListening(true)); - - React.useEffect(() => { - const rootEl = getRootEl(); - if (!rootEl) { - return; - } - setRootDimensions({ - height: rootEl.clientHeight, - width: rootEl.clientWidth, - }); - }, []); // eslint-disable-line react-hooks/exhaustive-deps + useOutsideEvent('mouseup', { current: rootEl }, (): void => setIsListening(true)); return ( - {rootDimensions && - annotations - .filter(filterDrawing) - .sort(sortDrawing) - .map(({ id, target }) => ( - - ))} + {annotations + .filter(filterDrawing) + .sort(sortDrawing) + .map(({ id, target }) => ( + + ))} ); } diff --git a/src/drawing/DrawingPath.tsx b/src/drawing/DrawingPath.tsx index 36d576917..a87b2163d 100644 --- a/src/drawing/DrawingPath.tsx +++ b/src/drawing/DrawingPath.tsx @@ -2,15 +2,16 @@ import * as React from 'react'; import classNames from 'classnames'; import { v4 as uuidv4 } from 'uuid'; import { white } from 'box-ui-elements/es/styles/variables'; -import { Dimensions, PathGroup } from '../@types'; +import { DrawingSVGRef } from './DrawingSVG'; +import { PathGroup } from '../@types'; import { Point } from '../region/transformUtil'; import { isVectorEffectSupported } from './drawingUtil'; import './DrawingPath.scss'; export type Props = { isActive?: boolean; - rootDimensions: Dimensions; pathGroup: PathGroup; + rootEl: DrawingSVGRef | null; }; export const DRAWING_TARGET_BORDER = 1; @@ -44,18 +45,22 @@ export const getPathCommands = (points: Point[]): string => { export const DrawingPath = (props: Props): JSX.Element => { const { isActive = false, - rootDimensions: { height, width }, pathGroup: { paths, stroke: { color, size }, }, + rootEl, } = props; let strokeWidth = size; let strokeBorderWidth = size + DRAWING_TARGET_BORDER * 2; if (!isVectorEffectSupported()) { - const scale = Math.min(width, height) / 100; + if (!rootEl) { + return ; + } + const { clientHeight, clientWidth } = rootEl; + const scale = Math.min(clientHeight, clientWidth) / 100; strokeWidth /= scale; strokeBorderWidth /= scale; } diff --git a/src/drawing/DrawingTarget.tsx b/src/drawing/DrawingTarget.tsx index eab60d8db..7025c926a 100644 --- a/src/drawing/DrawingTarget.tsx +++ b/src/drawing/DrawingTarget.tsx @@ -3,8 +3,9 @@ import classNames from 'classnames'; import noop from 'lodash/noop'; import { v4 as uuidv4 } from 'uuid'; import DrawingPath from './DrawingPath'; -import { Dimensions, TargetDrawing } from '../@types'; +import { DrawingSVGRef } from './DrawingSVG'; import { MOUSE_PRIMARY } from '../constants'; +import { TargetDrawing } from '../@types'; import { getCenter, getShape } from './drawingUtil'; import './DrawingTarget.scss'; @@ -13,7 +14,7 @@ export type Props = { className?: string; isActive?: boolean; onSelect?: (annotationId: string) => void; - rootDimensions: Dimensions; + rootEl: DrawingSVGRef | null; target: TargetDrawing; }; @@ -25,7 +26,7 @@ export const DrawingTarget = (props: Props, ref: React.Ref): J className, isActive = false, onSelect = noop, - rootDimensions, + rootEl, target: { path_groups: pathGroups }, } = props; const shape = getShape(pathGroups); @@ -82,7 +83,7 @@ export const DrawingTarget = (props: Props, ref: React.Ref): J key={`${annotationId}-${uuidv4()}`} isActive={isActive} pathGroup={pathGroup} - rootDimensions={rootDimensions} + rootEl={rootEl} /> ))}