-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
165 additions
and
113 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import React, { FC } from 'react' | ||
import * as PropTypes from 'prop-types' | ||
|
||
import Drawable from 'components/Drawable' | ||
import Drawed from 'components/Drawed' | ||
import DrawContext from 'contexts/DrawContext' | ||
import useDrawArea from 'hooks/useDrawArea' | ||
|
||
type Props = { | ||
className?: string | ||
color?: string | ||
disabled?: boolean | ||
hidden?: boolean | ||
thickness?: number | ||
} | ||
|
||
const DrawArea: FC<Props> = ({ | ||
className = '', | ||
color = '#000000', | ||
disabled = false, | ||
hidden = false, | ||
thickness = 10, | ||
children, | ||
}) => { | ||
const { isDrawing, lines, addPoint, finishLine, reset, undo } = useDrawArea() | ||
|
||
const content = hidden | ||
? null | ||
: ( | ||
<div className={className}> | ||
<Drawed color={color} lines={lines} thickness={thickness} /> | ||
{disabled || <Drawable addPoint={addPoint} finishLine={finishLine} />} | ||
</div> | ||
) | ||
|
||
if (!children) return content | ||
|
||
return ( | ||
<DrawContext.Provider value={{ isDrawing, lines, reset, undo }}> | ||
{children} | ||
{content} | ||
</DrawContext.Provider> | ||
) | ||
} | ||
|
||
DrawArea.propTypes = { | ||
className: PropTypes.string, | ||
color: PropTypes.string, | ||
disabled: PropTypes.bool, | ||
hidden: PropTypes.bool, | ||
thickness: PropTypes.number, | ||
} | ||
|
||
export default DrawArea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import React, { CSSProperties, FC, useRef } from 'react' | ||
|
||
import useDrawable from 'hooks/useDrawable' | ||
|
||
import { Point } from 'types' | ||
|
||
type Props = { | ||
addPoint: (point: Point) => void | ||
finishLine: () => void | ||
} | ||
|
||
const style: CSSProperties = { | ||
position: 'absolute', | ||
inset: 0, | ||
} | ||
|
||
const Drawable: FC<Props> = ({ addPoint, finishLine }) => { | ||
const ref = useRef<HTMLDivElement>(null) | ||
const { startDrawing, keepDrawing, endDrawing } = useDrawable({ ref, addPoint, finishLine }) | ||
|
||
return ( | ||
<div | ||
ref={ref} | ||
style={style} | ||
onMouseDown={startDrawing} | ||
onMouseMove={keepDrawing} | ||
onMouseUp={endDrawing} | ||
onMouseLeave={endDrawing} | ||
onTouchStart={startDrawing} | ||
onTouchMove={keepDrawing} | ||
onTouchEnd={endDrawing} | ||
/> | ||
) | ||
} | ||
|
||
export default Drawable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { useCallback, useMemo, useState } from 'react' | ||
|
||
import { Line, Lines, Point } from 'types' | ||
|
||
type UseDrawAreaType = { | ||
isDrawing: boolean | ||
lines: Lines | ||
addPoint: (point: Point) => void | ||
finishLine: () => void | ||
reset: () => void | ||
undo: () => void | ||
} | ||
|
||
const useDrawArea = (): UseDrawAreaType => { | ||
const [lines, setLines] = useState<Lines>([]) | ||
const [newLine, setNewLine] = useState<Line>([]) | ||
|
||
const allLines: Lines = useMemo( | ||
() => newLine.length === 0 ? lines : [...lines, newLine], | ||
[lines, newLine] | ||
) | ||
|
||
const isDrawing: boolean = newLine.length > 0 | ||
|
||
const addPoint = useCallback( | ||
(newPoint: Point) => setNewLine([...newLine, newPoint]), | ||
[newLine] | ||
) | ||
|
||
const finishLine = useCallback( | ||
() => { | ||
if (newLine.length > 1) setLines(allLines) | ||
setNewLine([]) | ||
}, [allLines, newLine] | ||
) | ||
|
||
const reset = useCallback(() => setLines([]), []) | ||
|
||
const undo = useCallback(() => setLines(lines.slice(0, -1)), [lines]) | ||
|
||
return { | ||
isDrawing, | ||
lines: allLines, | ||
addPoint, | ||
finishLine, | ||
reset, | ||
undo, | ||
} | ||
} | ||
|
||
export default useDrawArea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import DrawArea from './DrawArea' | ||
import DrawContext from './DrawContext' | ||
import DrawArea from 'components/DrawArea' | ||
import DrawContext from 'contexts/DrawContext' | ||
|
||
export { DrawArea, DrawContext } |