|
1 | 1 | import React, { useEffect, useRef, useId, forwardRef, useImperativeHandle } from 'react'; |
2 | | -import { type StrokeOptions } from 'perfect-freehand'; |
3 | 2 | import { getBoundingClientRect, getClinetXY, defaultStyle, useEvent } from './utils'; |
4 | 3 |
|
5 | 4 | import { useDispatch } from './store'; |
6 | | -import { SignatureRef } from './'; |
| 5 | +import { SignatureRef, SignatureProps } from './'; |
7 | 6 |
|
8 | | -export interface SignatureProps extends React.SVGProps<SVGSVGElement> { |
9 | | - prefixCls?: string; |
10 | | - options?: StrokeOptions; |
11 | | - readonly?: boolean; |
12 | | - onPointer?: (points: number[][]) => void; |
13 | | -} |
| 7 | +export const Signature = forwardRef<SignatureRef, Omit<SignatureProps, 'defaultPoints' | 'renderPath' | 'options'>>( |
| 8 | + (props, ref) => { |
| 9 | + const { className, prefixCls = 'w-signature', style, readonly = false, onPointer, children, ...others } = props; |
| 10 | + const cls = [className, prefixCls].filter(Boolean).join(' '); |
| 11 | + const $svg = useRef<SVGSVGElement>(null); |
| 12 | + const $path = useRef<SVGPathElement>(); |
| 13 | + const pointsRef = useRef<number[][]>(); |
| 14 | + const pointCount = useRef<number>(0); |
| 15 | + const pointId = useId(); |
| 16 | + const dispatch = useDispatch(); |
| 17 | + useImperativeHandle<SignatureRef, SignatureRef>( |
| 18 | + ref, |
| 19 | + () => ({ svg: $svg.current, dispatch, clear: () => dispatch({}) }), |
| 20 | + [$svg.current, dispatch], |
| 21 | + ); |
14 | 22 |
|
15 | | -export const Signature = forwardRef<SignatureRef, SignatureProps>((props, ref) => { |
16 | | - const { |
17 | | - className, |
18 | | - prefixCls = 'w-signature', |
19 | | - style, |
20 | | - readonly = false, |
21 | | - onPointer, |
22 | | - options, |
23 | | - children, |
24 | | - ...others |
25 | | - } = props; |
26 | | - const cls = [className, prefixCls].filter(Boolean).join(' '); |
27 | | - const $svg = useRef<SVGSVGElement>(null); |
28 | | - const $path = useRef<SVGPathElement>(); |
29 | | - const pointsRef = useRef<number[][]>(); |
30 | | - const pointCount = useRef<number>(0); |
31 | | - const pointId = useId(); |
32 | | - const dispatch = useDispatch(); |
33 | | - useImperativeHandle<SignatureRef, SignatureRef>( |
34 | | - ref, |
35 | | - () => ({ svg: $svg.current, dispatch, clear: () => dispatch({}) }), |
36 | | - [$svg.current, dispatch], |
37 | | - ); |
38 | | - |
39 | | - const handlePointerDown = useEvent((e: React.PointerEvent<SVGSVGElement>) => { |
40 | | - if (readonly) return; |
41 | | - pointCount.current += 1; |
42 | | - const { offsetY, offsetX } = getBoundingClientRect($svg.current); |
43 | | - const clientX = e.clientX || e.nativeEvent.clientX; |
44 | | - const clientY = e.clientY || e.nativeEvent.clientY; |
45 | | - pointsRef.current = [[clientX - offsetX, clientY - offsetY]]; |
46 | | - const pathElm = document.createElementNS('http://www.w3.org/2000/svg', 'path'); |
47 | | - $path.current = pathElm; |
48 | | - $svg.current!.appendChild(pathElm); |
49 | | - dispatch({ |
50 | | - [pointId + pointCount.current]: pointsRef.current, |
51 | | - }); |
52 | | - }); |
53 | | - |
54 | | - const handlePointerMove = useEvent((e: PointerEvent) => { |
55 | | - if ($path.current) { |
| 23 | + const handlePointerDown = useEvent((e: React.PointerEvent<SVGSVGElement>) => { |
| 24 | + if (readonly) return; |
| 25 | + pointCount.current += 1; |
56 | 26 | const { offsetY, offsetX } = getBoundingClientRect($svg.current); |
57 | | - const { clientX, clientY } = getClinetXY(e); |
58 | | - pointsRef.current = [...pointsRef.current!, [clientX - offsetX, clientY - offsetY]]; |
| 27 | + const clientX = e.clientX || e.nativeEvent.clientX; |
| 28 | + const clientY = e.clientY || e.nativeEvent.clientY; |
| 29 | + pointsRef.current = [[clientX - offsetX, clientY - offsetY]]; |
| 30 | + const pathElm = document.createElementNS('http://www.w3.org/2000/svg', 'path'); |
| 31 | + $path.current = pathElm; |
| 32 | + $svg.current!.appendChild(pathElm); |
59 | 33 | dispatch({ |
60 | 34 | [pointId + pointCount.current]: pointsRef.current, |
61 | 35 | }); |
62 | | - } |
63 | | - }); |
| 36 | + }); |
| 37 | + |
| 38 | + const handlePointerMove = useEvent((e: PointerEvent) => { |
| 39 | + if ($path.current) { |
| 40 | + const { offsetY, offsetX } = getBoundingClientRect($svg.current); |
| 41 | + const { clientX, clientY } = getClinetXY(e); |
| 42 | + pointsRef.current = [...pointsRef.current!, [clientX - offsetX, clientY - offsetY]]; |
| 43 | + dispatch({ |
| 44 | + [pointId + pointCount.current]: pointsRef.current, |
| 45 | + }); |
| 46 | + } |
| 47 | + }); |
64 | 48 |
|
65 | | - const handlePointerUp = useEvent(() => { |
66 | | - let result = pointsRef.current || []; |
67 | | - onPointer && props.onPointer!(result); |
68 | | - $path.current = undefined; |
69 | | - pointsRef.current = undefined; |
70 | | - }); |
| 49 | + const handlePointerUp = useEvent(() => { |
| 50 | + let result = pointsRef.current || []; |
| 51 | + onPointer && props.onPointer!(result); |
| 52 | + $path.current = undefined; |
| 53 | + pointsRef.current = undefined; |
| 54 | + }); |
71 | 55 |
|
72 | | - useEffect(() => { |
73 | | - if (readonly) return; |
74 | | - document.addEventListener('pointermove', handlePointerMove); |
75 | | - document.addEventListener('pointerup', handlePointerUp); |
76 | | - return () => { |
| 56 | + useEffect(() => { |
77 | 57 | if (readonly) return; |
78 | | - document.removeEventListener('pointermove', handlePointerMove); |
79 | | - document.removeEventListener('pointerup', handlePointerUp); |
80 | | - }; |
81 | | - }, []); |
82 | | - return ( |
83 | | - <svg {...others} ref={$svg} className={cls} onPointerDown={handlePointerDown} style={{ ...defaultStyle, ...style }}> |
84 | | - {children} |
85 | | - </svg> |
86 | | - ); |
87 | | -}); |
| 58 | + document.addEventListener('pointermove', handlePointerMove); |
| 59 | + document.addEventListener('pointerup', handlePointerUp); |
| 60 | + return () => { |
| 61 | + if (readonly) return; |
| 62 | + document.removeEventListener('pointermove', handlePointerMove); |
| 63 | + document.removeEventListener('pointerup', handlePointerUp); |
| 64 | + }; |
| 65 | + }, []); |
| 66 | + return ( |
| 67 | + <svg |
| 68 | + {...others} |
| 69 | + ref={$svg} |
| 70 | + className={cls} |
| 71 | + onPointerDown={handlePointerDown} |
| 72 | + style={{ ...defaultStyle, ...style }} |
| 73 | + > |
| 74 | + {children} |
| 75 | + </svg> |
| 76 | + ); |
| 77 | + }, |
| 78 | +); |
0 commit comments