Skip to content

Commit

Permalink
Fixed bug with single point lines
Browse files Browse the repository at this point in the history
  • Loading branch information
bgonp committed Mar 9, 2021
1 parent 771a8d4 commit 0386db0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
25 changes: 10 additions & 15 deletions src/DrawArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,30 +24,25 @@ const DrawArea: FC<Props> = ({
children,
}) => {
const [lines, setLines] = useState<Lines>([])
const [isDrawing, setIsDrawing] = useState<boolean>(false)
const [currentLine, setCurrentLine] = useState<Line>([])

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
: (
<div className={className}>
<Drawed color={color} lines={lines} thickness={thickness} />
<Drawed color={color} lines={[...lines, currentLine]} thickness={thickness} />
{disabled || <Drawable addPoint={addPoint} finishLine={finishLine} />}
</div>
)
Expand All @@ -57,7 +52,7 @@ const DrawArea: FC<Props> = ({
return (
<DrawContext.Provider value={{
lines,
isDrawing,
isDrawing: currentLine.length > 0,
reset,
undo,
}}>
Expand Down

0 comments on commit 0386db0

Please sign in to comment.