Skip to content

Commit 55f36c6

Browse files
committed
feat: add defaultPoints props.
1 parent e73a615 commit 55f36c6

File tree

9 files changed

+226
-133
lines changed

9 files changed

+226
-133
lines changed

core/README.md

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,16 @@ export default function App() {
4242
import React, { useRef, useEffect } from "react";
4343
import Signature, { getStroke, getSvgPathFromStroke, defaultOptions } from '@uiw/react-signature';
4444

45-
const point1 = [[81.546875,38.87890625],[82.34375,37.55078125],[84.27734375,35.2265625],[87.73046875,33.2421875],[94.98046875,30.87890625],[106.29296875,28.6796875],[118.23828125,27.71875],[126.6953125,29.45703125],[134.9375,36.22265625],[141.8359375,47.13671875],[143.9765625,59.53515625],[141.8671875,76],[128.1484375,98.9609375],[106.203125,118.87109375],[95.04296875,125.8203125],[94.5859375,125.04296875],[94.7265625,122.04296875],[97.90234375,115.30859375],[109.28515625,102.3046875],[129.75390625,86.796875],[155.27734375,76.14453125],[184.99609375,71.8828125],[218.16015625,73.33984375],[249.26953125,77.8671875],[264.2421875,81.10546875],[266.51171875,82.35546875],[267.69921875,82.99609375],[267.64453125,83.08203125],[267.73046875,82.6875],[267.73828125,80.4921875],[267.83203125,75.4765625],[268.36328125,66.6484375],[268.84765625,56.4609375],[268.421875,46.88671875],[266.9296875,40.86328125],[264.90625,38.4765625],[262.87890625,37.30859375],[258.68359375,39.13671875],[251.265625,43.80078125],[242.21484375,51.79296875],[232.60546875,63.21484375],[222.6484375,78.53125],[213.01171875,94.58984375],[206.3203125,104.57421875],[201.64453125,109.59375],[198.61328125,111.87109375],[197.24609375,111.3671875],[195.421875,109.23828125],[193.87890625,104.91015625],[193.7265625,99.140625],[198.671875,89.87109375],[214.1015625,75.14453125],[243.25390625,54.8671875],[290.3671875,29.3515625],[329.31640625,10.6875],[338.9609375,6.70703125],[338.5234375,9.07421875],[336.8671875,15.4453125],[331.4296875,28.4140625],[323.234375,43.5234375],[312.70703125,57.9609375],[302.296875,66.97265625],[293.85546875,70.734375],[287.6484375,72.25],[283.45703125,70.9453125],[280.51171875,68.09375],[279.5390625,64.13671875],[280.828125,60.0234375],[284.98828125,56.40625],[294.63671875,54.62109375],[307.8984375,56.17578125],[316.7890625,62.65234375],[317.5859375,74.11328125],[309.1640625,92.50390625],[295.12109375,110.55859375],[282.93359375,123.125],[278.58203125,129.00390625],[279.46875,129.80859375],[285.25390625,129.26953125],[299.71484375,125.71875],[321.41015625,118.89453125],[349.296875,108.58203125],[372.53125,98.8671875],[387.57421875,91],[395.23046875,84.53125],[396.7421875,79.3046875],[395.31640625,75.45703125],[389.86328125,73.57421875],[374.875,77.3203125],[347.36328125,88.46875],[310.44140625,107.75],[273.14453125,130.30078125],[252.08984375,145.77734375],[246.2890625,152.421875],[245.4609375,153.8046875],[245.3671875,153.1640625],[245.78515625,148.46875],[247.5859375,136.29296875],[250.55078125,119.38671875],[253.1796875,107.87890625],[255.4765625,102.29296875],[257.40625,99.61328125],[260.58203125,99.8125],[273.9453125,105.66015625],[296.24609375,118.10546875],[326.40234375,138.03125],[355.63671875,158.3515625],[368.66796875,167.1796875],[373.015625,170.12109375],[374.7578125,170.8203125],[374.73046875,169.78125],[374.6953125,167.3203125],[374.765625,161.75],[375.6328125,154.09375],[377.94921875,146.08984375],[380.94921875,140.23046875],[383.6640625,136.3046875],[385.0546875,134.08203125],[385.19140625,133.45703125],[384.81640625,132.41015625]];
4645
const point2 = [[335.0078125,77.8828125],[335.31640625,77.984375],[336.16796875,78.30078125],[337.640625,79.0546875],[339.65234375,80.44140625],[342.88671875,82.921875],[347.0625,86.28125],[353.9296875,91.546875],[358.88671875,95.125],[359.7734375,95.6875],[360.48828125,96.19921875],[360.2265625,96.42578125]];
4746

47+
const points = {
48+
"path-01": [[81.546875,38.87890625],[82.34375,37.55078125],[84.27734375,35.2265625],[87.73046875,33.2421875],[94.98046875,30.87890625],[106.29296875,28.6796875],[118.23828125,27.71875],[126.6953125,29.45703125],[134.9375,36.22265625],[141.8359375,47.13671875],[143.9765625,59.53515625],[141.8671875,76],[128.1484375,98.9609375],[106.203125,118.87109375],[95.04296875,125.8203125],[94.5859375,125.04296875],[94.7265625,122.04296875],[97.90234375,115.30859375],[109.28515625,102.3046875],[129.75390625,86.796875],[155.27734375,76.14453125],[184.99609375,71.8828125],[218.16015625,73.33984375],[249.26953125,77.8671875],[264.2421875,81.10546875],[266.51171875,82.35546875],[267.69921875,82.99609375],[267.64453125,83.08203125],[267.73046875,82.6875],[267.73828125,80.4921875],[267.83203125,75.4765625],[268.36328125,66.6484375],[268.84765625,56.4609375],[268.421875,46.88671875],[266.9296875,40.86328125],[264.90625,38.4765625],[262.87890625,37.30859375],[258.68359375,39.13671875],[251.265625,43.80078125],[242.21484375,51.79296875],[232.60546875,63.21484375],[222.6484375,78.53125],[213.01171875,94.58984375],[206.3203125,104.57421875],[201.64453125,109.59375],[198.61328125,111.87109375],[197.24609375,111.3671875],[195.421875,109.23828125],[193.87890625,104.91015625],[193.7265625,99.140625],[198.671875,89.87109375],[214.1015625,75.14453125],[243.25390625,54.8671875],[290.3671875,29.3515625],[329.31640625,10.6875],[338.9609375,6.70703125],[338.5234375,9.07421875],[336.8671875,15.4453125],[331.4296875,28.4140625],[323.234375,43.5234375],[312.70703125,57.9609375],[302.296875,66.97265625],[293.85546875,70.734375],[287.6484375,72.25],[283.45703125,70.9453125],[280.51171875,68.09375],[279.5390625,64.13671875],[280.828125,60.0234375],[284.98828125,56.40625],[294.63671875,54.62109375],[307.8984375,56.17578125],[316.7890625,62.65234375],[317.5859375,74.11328125],[309.1640625,92.50390625],[295.12109375,110.55859375],[282.93359375,123.125],[278.58203125,129.00390625],[279.46875,129.80859375],[285.25390625,129.26953125],[299.71484375,125.71875],[321.41015625,118.89453125],[349.296875,108.58203125],[372.53125,98.8671875],[387.57421875,91],[395.23046875,84.53125],[396.7421875,79.3046875],[395.31640625,75.45703125],[389.86328125,73.57421875],[374.875,77.3203125],[347.36328125,88.46875],[310.44140625,107.75],[273.14453125,130.30078125],[252.08984375,145.77734375],[246.2890625,152.421875],[245.4609375,153.8046875],[245.3671875,153.1640625],[245.78515625,148.46875],[247.5859375,136.29296875],[250.55078125,119.38671875],[253.1796875,107.87890625],[255.4765625,102.29296875],[257.40625,99.61328125],[260.58203125,99.8125],[273.9453125,105.66015625],[296.24609375,118.10546875],[326.40234375,138.03125],[355.63671875,158.3515625],[368.66796875,167.1796875],[373.015625,170.12109375],[374.7578125,170.8203125],[374.73046875,169.78125],[374.6953125,167.3203125],[374.765625,161.75],[375.6328125,154.09375],[377.94921875,146.08984375],[380.94921875,140.23046875],[383.6640625,136.3046875],[385.0546875,134.08203125],[385.19140625,133.45703125],[384.81640625,132.41015625]],
49+
"path-02": [[335.0078125,77.8828125],[335.31640625,77.984375],[336.16796875,78.30078125],[337.640625,79.0546875],[339.65234375,80.44140625],[342.88671875,82.921875],[347.0625,86.28125],[353.9296875,91.546875],[358.88671875,95.125],[359.7734375,95.6875],[360.48828125,96.19921875],[360.2265625,96.42578125]]
50+
}
51+
4852
export default function App() {
49-
const stroke = getStroke(point1, defaultOptions);
50-
const pathData = getSvgPathFromStroke(stroke);
51-
const pathData2 = getSvgPathFromStroke(getStroke(point2, defaultOptions));
5253
return (
53-
<Signature readonly fill="#6b85e4">
54-
<path d={pathData} />
55-
<path d={pathData2} />
56-
</Signature>
54+
<Signature defaultPoints={points} readonly fill="#6b85e4" />
5755
);
5856
}
5957
```
@@ -126,31 +124,12 @@ export interface SignatureProps extends React.SVGProps<SVGSVGElement> {
126124
prefixCls?: string;
127125
options?: StrokeOptions;
128126
readonly?: boolean;
127+
defaultPoints?: Record<string, number[][]>;
129128
onPointer?: (points: number[][]) => void;
130129
}
131130
export default function Signature(props?: SignatureProps): React.JSX.Element;
132131
```
133132

134-
utils
135-
136-
```ts
137-
import { type StrokeOptions } from 'perfect-freehand';
138-
/**
139-
* Turn the points returned from perfect-freehand into SVG path data.
140-
*/
141-
export declare function getSvgPathFromStroke(stroke: number[][]): string;
142-
export declare const defaultOptions: StrokeOptions;
143-
export declare const getBoundingClientRect: (el: SVGSVGElement | null) => {
144-
offsetX: number;
145-
offsetY: number;
146-
};
147-
export declare const getClinetXY: ({ clientX, clientY }: PointerEvent) => {
148-
clientX: number;
149-
clientY: number;
150-
};
151-
export declare const defaultStyle: React.CSSProperties;
152-
```
153-
154133
### Options
155134

156135
The options object is optional, as are each of its properties.

core/src/Paths.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import React, { Fragment } from 'react';
2+
import { getStroke } from 'perfect-freehand';
3+
import { useStore } from './store';
4+
import { useOptionStore } from './options';
5+
import { getSvgPathFromStroke } from './utils';
6+
7+
export const Paths = () => {
8+
const { options, ...data } = useStore();
9+
return (
10+
<Fragment>
11+
{Object.keys(data).map((key) => (
12+
<CreatePath key={key} data={data[key]} />
13+
))}
14+
</Fragment>
15+
);
16+
};
17+
18+
type CreatePathProps = {
19+
data: number[][];
20+
};
21+
22+
const CreatePath = ({ data = [] }: CreatePathProps) => {
23+
const options = useOptionStore();
24+
const stroke = getStroke(data, options);
25+
const pathData = getSvgPathFromStroke(stroke);
26+
return (
27+
<Fragment>
28+
<path d={pathData} />
29+
</Fragment>
30+
);
31+
};

core/src/Signature.tsx

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

core/src/index.tsx

Lines changed: 24 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,39 @@
1-
import React, { useEffect, useRef, forwardRef, useImperativeHandle } from 'react';
2-
import { getStroke, type StrokeOptions } from 'perfect-freehand';
3-
import {
4-
getSvgPathFromStroke,
5-
getBoundingClientRect,
6-
getClinetXY,
7-
defaultOptions,
8-
defaultStyle,
9-
useEvent,
10-
} from './utils';
1+
import React, { useReducer, forwardRef, useEffect } from 'react';
2+
import { type StrokeOptions } from 'perfect-freehand';
3+
import { PointerContext, PointerDispatchContext, reducer } from './store';
4+
import { OptionContext, OptionDispatchContext, reducerOption, defaultOptions } from './options';
5+
import { Signature as Container } from './Signature';
6+
import { Paths } from './Paths';
117

128
export * from 'perfect-freehand';
139
export * from './utils';
10+
export * from './options';
1411

1512
export interface SignatureProps extends React.SVGProps<SVGSVGElement> {
1613
prefixCls?: string;
1714
options?: StrokeOptions;
1815
readonly?: boolean;
16+
defaultPoints?: Record<string, number[][]>;
1917
onPointer?: (points: number[][]) => void;
2018
}
2119

22-
const Signature = forwardRef<SVGSVGElement, SignatureProps>((props, ref) => {
23-
const {
24-
className,
25-
prefixCls = 'w-signature',
26-
style,
27-
readonly = false,
28-
onPointer,
29-
options,
30-
children,
31-
...others
32-
} = props;
33-
const cls = [className, prefixCls].filter(Boolean).join(' ');
34-
const $svg = useRef<SVGSVGElement>(null);
35-
const $path = useRef<SVGPathElement>();
36-
const pointsRef = useRef<number[][]>();
37-
38-
useImperativeHandle<SVGSVGElement | null, SVGSVGElement | null>(ref, () => $svg.current, [$svg.current]);
39-
40-
const handlePointerDown = useEvent((e: React.PointerEvent<SVGSVGElement>) => {
41-
const { offsetY, offsetX } = getBoundingClientRect($svg.current);
42-
const clientX = e.clientX || e.nativeEvent.clientX;
43-
const clientY = e.clientY || e.nativeEvent.clientY;
44-
pointsRef.current = [[clientX - offsetX, clientY - offsetY]];
45-
const pathElm = document.createElementNS('http://www.w3.org/2000/svg', 'path');
46-
$path.current = pathElm;
47-
$svg.current!.appendChild(pathElm);
48-
});
49-
50-
const handlePointerMove = useEvent((e: PointerEvent) => {
51-
if ($path.current) {
52-
const resultOptions = { ...defaultOptions, ...options };
53-
console.log(resultOptions);
54-
const { offsetY, offsetX } = getBoundingClientRect($svg.current);
55-
const { clientX, clientY } = getClinetXY(e);
56-
pointsRef.current = [...pointsRef.current!, [clientX - offsetX, clientY - offsetY]];
57-
const stroke = getStroke(pointsRef.current!, resultOptions);
58-
const pathData = getSvgPathFromStroke(stroke);
59-
$path.current?.setAttribute('d', pathData);
60-
}
61-
});
62-
63-
const handlePointerUp = useEvent(() => {
64-
let result = pointsRef.current || [];
65-
onPointer && props.onPointer!(result);
66-
$path.current = undefined;
67-
pointsRef.current = undefined;
68-
});
69-
70-
useEffect(() => {
71-
if (readonly) return;
72-
document.addEventListener('pointermove', handlePointerMove);
73-
document.addEventListener('pointerup', handlePointerUp);
74-
return () => {
75-
if (readonly) return;
76-
document.removeEventListener('pointermove', handlePointerMove);
77-
document.removeEventListener('pointerup', handlePointerUp);
78-
};
79-
}, []);
80-
20+
const Signature = forwardRef<SVGSVGElement, SignatureProps>(({ children, options, defaultPoints, ...props }, ref) => {
21+
const [state, dispatch] = useReducer(reducer, Object.assign({}, defaultPoints));
22+
const [stateOption, dispatchOption] = useReducer(reducerOption, Object.assign({ ...defaultOptions }, options));
23+
useEffect(() => dispatchOption({ ...options }), [options]);
8124
return (
82-
<svg {...others} ref={$svg} className={cls} onPointerDown={handlePointerDown} style={{ ...defaultStyle, ...style }}>
83-
{children}
84-
</svg>
25+
<PointerContext.Provider value={state}>
26+
<PointerDispatchContext.Provider value={dispatch}>
27+
<Container {...props} ref={ref}>
28+
<OptionContext.Provider value={stateOption}>
29+
<OptionDispatchContext.Provider value={dispatchOption}>
30+
<Paths />
31+
</OptionDispatchContext.Provider>
32+
</OptionContext.Provider>
33+
{children}
34+
</Container>
35+
</PointerDispatchContext.Provider>
36+
</PointerContext.Provider>
8537
);
8638
});
8739

core/src/options.tsx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { createContext, useContext } from 'react';
2+
import { type StrokeOptions } from 'perfect-freehand';
3+
4+
export const defaultOptions: InitialOptionState = {
5+
size: 6,
6+
smoothing: 0.46,
7+
thinning: 0.73,
8+
streamline: 0.5,
9+
easing: (t) => t,
10+
start: {
11+
taper: 0,
12+
easing: (t) => t,
13+
cap: true,
14+
},
15+
end: {
16+
taper: 0,
17+
easing: (t) => t,
18+
cap: true,
19+
},
20+
};
21+
22+
export const OptionContext = createContext<InitialOptionState>(defaultOptions);
23+
export const OptionDispatchContext = createContext<Dispatch>(() => {});
24+
25+
type Dispatch = React.Dispatch<InitialOptionState>;
26+
type InitialOptionState = StrokeOptions;
27+
28+
export function reducerOption(tasks: InitialOptionState, action: InitialOptionState) {
29+
return { ...tasks, ...action };
30+
}
31+
32+
export const useOptionStore = () => {
33+
return useContext(OptionContext);
34+
};
35+
36+
export const useOptionDispatch = () => {
37+
return useContext(OptionDispatchContext);
38+
};

core/src/store.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { createContext, useContext } from 'react';
2+
import { type StrokeOptions } from 'perfect-freehand';
3+
4+
export const initialState: InitialState = {};
5+
6+
export const PointerContext = createContext<InitialState>(initialState);
7+
export const PointerDispatchContext = createContext<Dispatch>(() => {});
8+
9+
type Dispatch = React.Dispatch<InitialState>;
10+
11+
type InitialState = Record<string, number[][]>;
12+
13+
export function reducer(tasks: InitialState, action: InitialState) {
14+
return { ...tasks, ...action };
15+
}
16+
17+
export const useStore = () => {
18+
return useContext(PointerContext);
19+
};
20+
21+
export const useDispatch = () => {
22+
return useContext(PointerDispatchContext);
23+
};

core/src/utils.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,6 @@ export function getSvgPathFromStroke(stroke: number[][]) {
1919
return d.join(' ');
2020
}
2121

22-
export const defaultOptions: StrokeOptions = {
23-
size: 6,
24-
smoothing: 0.46,
25-
thinning: 0.73,
26-
streamline: 0.5,
27-
easing: (t) => t,
28-
start: {
29-
taper: 0,
30-
easing: (t) => t,
31-
cap: true,
32-
},
33-
end: {
34-
taper: 0,
35-
easing: (t) => t,
36-
cap: true,
37-
},
38-
};
39-
4022
export const getBoundingClientRect = (el: SVGSVGElement | null) => {
4123
const rect = el?.getBoundingClientRect();
4224
const offsetX = rect?.left || 0;

website/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
},
99
"license": "MIT",
1010
"dependencies": {
11+
"@uiw/copy-to-clipboard": "^1.0.16",
1112
"@uiw/react-markdown-preview-example": "^2.0.0",
1213
"@uiw/react-signature": "1.0.0",
1314
"react": "^18.2.0",

0 commit comments

Comments
 (0)