Browse Source

optimization(shape and brush): drag and drop event

RoyLiu 4 years ago
parent
commit
0172dd3755
3 changed files with 116 additions and 49 deletions
  1. 58 26
      components/Ink/index.tsx
  2. 6 1
      components/OuterRect/index.tsx
  3. 52 22
      components/Shape/index.tsx

+ 58 - 26
components/Ink/index.tsx

@@ -20,14 +20,11 @@ const Ink: React.FC<AnnotationElementPropsType> = ({
   scale,
   scale,
   id,
   id,
 }: AnnotationElementPropsType) => {
 }: AnnotationElementPropsType) => {
-  let points: PointType[][] = [];
-  let tempRect: HTMLCoordinateType = { top: 0, left: 0, width: 0, height: 0 };
-  const borderWidth = bdwidth * scale;
-
-  points = pointCalc(position as PointType[][], viewport.height, scale);
-  tempRect = rectCalcWithPoint(points, borderWidth);
-
+  const [points, setPoints] = useState<PointType[][]>([]);
   const [rect, setRect] = useState({ top: 0, left: 0, width: 0, height: 0 });
   const [rect, setRect] = useState({ top: 0, left: 0, width: 0, height: 0 });
+  const [moving, setMoving] = useState(false);
+
+  const borderWidth = bdwidth * scale;
 
 
   const handleScaleOrMove = ({
   const handleScaleOrMove = ({
     top,
     top,
@@ -35,28 +32,48 @@ const Ink: React.FC<AnnotationElementPropsType> = ({
     width = 0,
     width = 0,
     height = 0,
     height = 0,
   }: CoordType): void => {
   }: CoordType): void => {
-    const xScaleRate = width / tempRect.width;
-    const yScaleRate = height / tempRect.height;
-    const xDistance = left - tempRect.left;
-    const yDistance = tempRect.top - top;
-
-    const newPosition = (position as PointType[][])[0].map((ele) => ({
-      x: (ele.x + xDistance) * (xScaleRate || 1),
-      y: (ele.y + yDistance) * (yScaleRate || 1),
-    }));
-
-    setRect({
-      top,
-      left,
-      width,
-      height,
-    });
+    setMoving(true);
+
+    setRect((currentRect) => {
+      const rx = width / currentRect.width;
+      const ry = height / currentRect.height;
+      const dx = left - currentRect.left;
+      const dy = currentRect.top - top;
+      let disX = 0;
+      let disY = 0;
+
+      setPoints((currentPoint) => {
+        const newRect = rectCalcWithPoint(currentPoint, borderWidth);
+        disX = newRect.left - left;
+        disY = newRect.top - top;
 
 
-    onUpdate({
-      position: [newPosition],
+        const newPoints = (currentPoint as PointType[][])[0].map((ele) => {
+          let x = (ele.x + dx) * rx;
+          let y = (ele.y - dy) * ry;
+
+          if (rx !== 1 || ry !== 1) {
+            x = x - disX;
+          }
+          if (ry !== 1) {
+            y = y - disY;
+          }
+
+          return {
+            x,
+            y,
+          };
+        });
+        return [newPoints];
+      });
+
+      return { top, left, width, height };
     });
     });
   };
   };
 
 
+  const handleMouseUp = () => {
+    setMoving(false);
+  };
+
   useEffect(() => {
   useEffect(() => {
     const newPoints = pointCalc(
     const newPoints = pointCalc(
       position as PointType[][],
       position as PointType[][],
@@ -64,9 +81,23 @@ const Ink: React.FC<AnnotationElementPropsType> = ({
       scale,
       scale,
     );
     );
     const newRect = rectCalcWithPoint(newPoints, borderWidth);
     const newRect = rectCalcWithPoint(newPoints, borderWidth);
+    setPoints(newPoints);
     setRect(newRect);
     setRect(newRect);
   }, [viewport, scale]);
   }, [viewport, scale]);
 
 
+  useEffect(() => {
+    if (!moving && points[0]) {
+      const newPoints = (points as PointType[][])[0].map((ele) => ({
+        x: ele.x / scale,
+        y: (viewport.height - ele.y) / scale,
+      }));
+
+      onUpdate({
+        position: [newPoints],
+      });
+    }
+  }, [moving, points]);
+
   return (
   return (
     <>
     <>
       <AnnotationContainer
       <AnnotationContainer
@@ -76,7 +107,7 @@ const Ink: React.FC<AnnotationElementPropsType> = ({
         width={`${rect.width}px`}
         width={`${rect.width}px`}
         height={`${rect.height}px`}
         height={`${rect.height}px`}
       >
       >
-        <SVG viewBox={calcViewBox(tempRect, borderWidth)}>
+        <SVG viewBox={calcViewBox(rect, borderWidth)}>
           {points.map((ele: PointType[], index: number) => {
           {points.map((ele: PointType[], index: number) => {
             const key = `${id}_path_${index}`;
             const key = `${id}_path_${index}`;
             return (
             return (
@@ -103,6 +134,7 @@ const Ink: React.FC<AnnotationElementPropsType> = ({
           height={rect.height}
           height={rect.height}
           onMove={handleScaleOrMove}
           onMove={handleScaleOrMove}
           onScale={handleScaleOrMove}
           onScale={handleScaleOrMove}
+          onMouseUp={handleMouseUp}
         />
         />
       ) : (
       ) : (
         ''
         ''

+ 6 - 1
components/OuterRect/index.tsx

@@ -17,6 +17,7 @@ type Props = {
   onScale?: (scaleCoord: CoordType) => void;
   onScale?: (scaleCoord: CoordType) => void;
   onClick?: (event: React.MouseEvent) => void;
   onClick?: (event: React.MouseEvent) => void;
   onDoubleClick?: () => void;
   onDoubleClick?: () => void;
+  onMouseUp?: () => void;
 };
 };
 
 
 type ObjPositionType = {
 type ObjPositionType = {
@@ -48,10 +49,11 @@ const OuterRect: React.FC<Props> = ({
   onScale,
   onScale,
   onClick,
   onClick,
   onDoubleClick,
   onDoubleClick,
+  onMouseUp,
 }: Props) => {
 }: Props) => {
   const data = generateCirclesData(width, height);
   const data = generateCirclesData(width, height);
   const [state, setState] = useState(initState);
   const [state, setState] = useState(initState);
-  const [cursorPosition, setRef] = useCursorPosition(20);
+  const [cursorPosition, setRef] = useCursorPosition(25);
 
 
   const handleMouseDown = (e: React.MouseEvent | React.TouchEvent): void => {
   const handleMouseDown = (e: React.MouseEvent | React.TouchEvent): void => {
     e.preventDefault();
     e.preventDefault();
@@ -75,6 +77,9 @@ const OuterRect: React.FC<Props> = ({
   const handleMouseUp = (): void => {
   const handleMouseUp = (): void => {
     setRef(null);
     setRef(null);
     setState(initState);
     setState(initState);
+    if (onMouseUp) {
+      onMouseUp();
+    }
   };
   };
 
 
   const calcMoveResult = (
   const calcMoveResult = (

+ 52 - 22
components/Shape/index.tsx

@@ -1,4 +1,4 @@
-import React from 'react';
+import React, { useEffect, useState } from 'react';
 
 
 import OuterRect from '../OuterRect';
 import OuterRect from '../OuterRect';
 import SvgShapeElement from '../SvgShapeElement';
 import SvgShapeElement from '../SvgShapeElement';
@@ -23,7 +23,13 @@ const Shape: React.FC<AnnotationElementPropsType> = ({
   isCollapse,
   isCollapse,
   onUpdate,
   onUpdate,
 }: AnnotationElementPropsType) => {
 }: AnnotationElementPropsType) => {
-  const annotRect = rectCalc(position as PositionType, viewport.height, scale);
+  const [moving, setMoving] = useState(false);
+  const [localePos, setLocalePos] = useState({
+    top: 0,
+    left: 0,
+    width: 0,
+    height: 0,
+  });
 
 
   const handleScaleOrMove = ({
   const handleScaleOrMove = ({
     top,
     top,
@@ -31,36 +37,59 @@ const Shape: React.FC<AnnotationElementPropsType> = ({
     width = 0,
     width = 0,
     height = 0,
     height = 0,
   }: CoordType): void => {
   }: CoordType): void => {
+    setMoving(true);
     const newPosition = {
     const newPosition = {
       top,
       top,
       left,
       left,
-      bottom: top + (height || annotRect.height),
-      right: left + (width || annotRect.width),
+      height,
+      width,
     };
     };
 
 
-    onUpdate({
-      position: parsePositionForBackend(
-        obj_type,
-        newPosition,
-        viewport.height,
-        scale,
-      ),
-    });
+    setLocalePos(newPosition);
   };
   };
 
 
+  const handleMouseUp = () => {
+    setMoving(false);
+  };
+
+  useEffect(() => {
+    const rect = rectCalc(position as PositionType, viewport.height, scale);
+    setLocalePos(rect);
+  }, [viewport, scale]);
+
+  useEffect(() => {
+    if (!moving) {
+      const newPosition = {
+        top: localePos.top,
+        left: localePos.left,
+        right: localePos.left + localePos.width,
+        bottom: localePos.top + localePos.height,
+      };
+
+      onUpdate({
+        position: parsePositionForBackend(
+          obj_type,
+          newPosition as PositionType,
+          viewport.height,
+          scale,
+        ),
+      });
+    }
+  }, [moving, localePos]);
+
   return (
   return (
     <>
     <>
       <AnnotationContainer
       <AnnotationContainer
         id={id}
         id={id}
-        top={`${annotRect.top}px`}
-        left={`${annotRect.left}px`}
-        width={`${annotRect.width}px`}
-        height={`${annotRect.height}px`}
+        top={`${localePos.top}px`}
+        left={`${localePos.left}px`}
+        width={`${localePos.width}px`}
+        height={`${localePos.height}px`}
       >
       >
         <SvgShapeElement
         <SvgShapeElement
           shape={obj_type}
           shape={obj_type}
-          width={annotRect.width}
-          height={annotRect.height}
+          width={localePos.width}
+          height={localePos.height}
           bdcolor={bdcolor}
           bdcolor={bdcolor}
           transparency={transparency}
           transparency={transparency}
           bdwidth={bdwidth * scale}
           bdwidth={bdwidth * scale}
@@ -70,12 +99,13 @@ const Shape: React.FC<AnnotationElementPropsType> = ({
       </AnnotationContainer>
       </AnnotationContainer>
       {!isCollapse ? (
       {!isCollapse ? (
         <OuterRect
         <OuterRect
-          top={annotRect.top}
-          left={annotRect.left}
-          width={annotRect.width}
-          height={annotRect.height}
+          top={localePos.top}
+          left={localePos.left}
+          width={localePos.width}
+          height={localePos.height}
           onMove={handleScaleOrMove}
           onMove={handleScaleOrMove}
           onScale={handleScaleOrMove}
           onScale={handleScaleOrMove}
+          onMouseUp={handleMouseUp}
         />
         />
       ) : (
       ) : (
         ''
         ''