From 0386db00e2982397756d199783f19d874957aac4 Mon Sep 17 00:00:00 2001 From: Borja Gonzalez Date: Tue, 9 Mar 2021 23:30:41 +0100 Subject: [PATCH] Fixed bug with single point lines --- package.json | 2 +- src/DrawArea.tsx | 25 ++++++++++--------------- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index 96fc340..ee8e7b3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-drawarea", - "version": "1.0.6", + "version": "1.0.7", "description": "React components to draw with mouse or touch events", "author": { "name": "Borja González", diff --git a/src/DrawArea.tsx b/src/DrawArea.tsx index ccc3ab4..a96475a 100644 --- a/src/DrawArea.tsx +++ b/src/DrawArea.tsx @@ -5,7 +5,7 @@ import Drawable from './Drawable' import DrawContext from './DrawContext' import Drawed from './Drawed' -import { Lines, Point } from './types' +import { Line, Lines, Point } from './types' type Props = { className?: string @@ -24,30 +24,25 @@ const DrawArea: FC = ({ children, }) => { const [lines, setLines] = useState([]) - const [isDrawing, setIsDrawing] = useState(false) + const [currentLine, setCurrentLine] = useState([]) const reset = () => setLines([]) const undo = () => setLines(lines.slice(0, -1)) - const finishLine = () => setIsDrawing(false) - - const addPoint = (newPoint: Point) => { - if (isDrawing) { - const prevLines = [...lines] - const lastLine = prevLines.pop() - setLines(prevLines.concat([lastLine.concat(newPoint)])) - } else { - setIsDrawing(true) - setLines([...lines, [newPoint]]) - } + const finishLine = () => { + if (currentLine.length > 1) setLines([...lines, currentLine]) + setCurrentLine([]) } + const addPoint = (newPoint: Point) => + setCurrentLine([...currentLine, newPoint]) + const content = hidden ? null : (
- + {disabled || }
) @@ -57,7 +52,7 @@ const DrawArea: FC = ({ return ( 0, reset, undo, }}>