@archireport/react-native-svg-draw 2.3.2 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/README.md +34 -35
  2. package/lib/commonjs/components/DrawCore/DrawContext.js +4 -1
  3. package/lib/commonjs/components/DrawCore/DrawContext.js.map +1 -1
  4. package/lib/commonjs/components/DrawCore/DrawPad.js +9 -3
  5. package/lib/commonjs/components/DrawCore/DrawPad.js.map +1 -1
  6. package/lib/commonjs/components/DrawCore/DrawProvider.js +16 -2
  7. package/lib/commonjs/components/DrawCore/DrawProvider.js.map +1 -1
  8. package/lib/commonjs/components/DrawCore/Item.js +12 -35
  9. package/lib/commonjs/components/DrawCore/Item.js.map +1 -1
  10. package/lib/commonjs/components/DrawCore/index.js +574 -576
  11. package/lib/commonjs/components/DrawCore/index.js.map +1 -1
  12. package/lib/commonjs/components/DrawCore/useDrawHook.js +15 -13
  13. package/lib/commonjs/components/DrawCore/useDrawHook.js.map +1 -1
  14. package/lib/commonjs/components/DrawWithOptions/index.js +7 -11
  15. package/lib/commonjs/components/DrawWithOptions/index.js.map +1 -1
  16. package/lib/module/components/DrawCore/DrawContext.js +4 -1
  17. package/lib/module/components/DrawCore/DrawContext.js.map +1 -1
  18. package/lib/module/components/DrawCore/DrawPad.js +10 -4
  19. package/lib/module/components/DrawCore/DrawPad.js.map +1 -1
  20. package/lib/module/components/DrawCore/DrawProvider.js +17 -3
  21. package/lib/module/components/DrawCore/DrawProvider.js.map +1 -1
  22. package/lib/module/components/DrawCore/Item.js +14 -37
  23. package/lib/module/components/DrawCore/Item.js.map +1 -1
  24. package/lib/module/components/DrawCore/index.js +575 -577
  25. package/lib/module/components/DrawCore/index.js.map +1 -1
  26. package/lib/module/components/DrawCore/useDrawHook.js +15 -13
  27. package/lib/module/components/DrawCore/useDrawHook.js.map +1 -1
  28. package/lib/module/components/DrawWithOptions/index.js +8 -12
  29. package/lib/module/components/DrawWithOptions/index.js.map +1 -1
  30. package/lib/typescript/components/DrawCore/DrawContext.d.ts +3 -0
  31. package/lib/typescript/components/DrawCore/DrawContext.d.ts.map +1 -1
  32. package/lib/typescript/components/DrawCore/DrawPad.d.ts +2 -1
  33. package/lib/typescript/components/DrawCore/DrawPad.d.ts.map +1 -1
  34. package/lib/typescript/components/DrawCore/DrawProvider.d.ts.map +1 -1
  35. package/lib/typescript/components/DrawCore/Item.d.ts.map +1 -1
  36. package/lib/typescript/components/DrawCore/index.d.ts +2 -1
  37. package/lib/typescript/components/DrawCore/index.d.ts.map +1 -1
  38. package/lib/typescript/components/DrawCore/useDrawHook.d.ts +4 -1
  39. package/lib/typescript/components/DrawCore/useDrawHook.d.ts.map +1 -1
  40. package/lib/typescript/components/DrawWithOptions/index.d.ts +1 -1
  41. package/lib/typescript/components/DrawWithOptions/index.d.ts.map +1 -1
  42. package/package.json +5 -1
  43. package/src/components/DrawCore/DrawContext.tsx +6 -0
  44. package/src/components/DrawCore/DrawPad.tsx +76 -67
  45. package/src/components/DrawCore/DrawProvider.tsx +23 -1
  46. package/src/components/DrawCore/Item.tsx +15 -47
  47. package/src/components/DrawCore/index.tsx +109 -114
  48. package/src/components/DrawCore/useDrawHook.tsx +12 -7
  49. package/src/components/DrawWithOptions/index.tsx +10 -11
  50. package/src/types.d.ts +5 -3
@@ -1,6 +1,6 @@
1
1
  import React from 'react';
2
2
  import type Animated from 'react-native-reanimated';
3
- import Svg, { Circle, Defs, Marker, Polyline, Use } from 'react-native-svg';
3
+ import Svg, { Circle, Defs, G, Marker, Polyline, Use } from 'react-native-svg';
4
4
  import CurrentAnimatedItem from './CurrentAnimatedItem';
5
5
  import CurrentAnimatedText from './CurrentAnimatedText';
6
6
  import Item from './Item';
@@ -13,86 +13,95 @@ const DrawPad = ({
13
13
  onPressItem,
14
14
  onTextHeightChange,
15
15
  addBackground = false,
16
+ ratioImage = 1,
16
17
  }: {
17
18
  currentItem: Animated.SharedValue<DrawItem | null>;
18
19
  doneItems: Array<DrawItem>;
19
20
  onPressItem: (item: DrawItem, index: number) => () => void;
20
21
  onTextHeightChange: (height: number) => void;
21
22
  addBackground?: boolean;
23
+ ratioImage?: number;
22
24
  }) => {
23
25
  return (
24
26
  <>
25
- <Svg style={addBackground ? { backgroundColor: 'white' } : null}>
26
- <Defs>
27
- <Circle
28
- id="selectionIndicator"
29
- fill="#3a6cff"
30
- r={10}
31
- cx={0}
32
- cy={0}
33
- strokeWidth={1}
34
- stroke="black"
35
- />
36
- <Marker
37
- id="arrowhead"
38
- markerUnits={'strokeWidth' as MarkerUnits}
39
- refX="0"
40
- refY="0"
41
- orient="auto"
42
- >
43
- <Polyline
44
- points="-2,-2 0,0 -2,2 0,0"
45
- stroke="context-stroke"
46
- strokeLinecap="round"
47
- strokeLinejoin="round"
27
+ <Svg
28
+ style={addBackground ? { backgroundColor: 'white' } : null}
29
+ preserveAspectRatio="xMinYMin slice"
30
+ height="100%"
31
+ width="100%"
32
+ >
33
+ <G scale={ratioImage}>
34
+ <Defs>
35
+ <Circle
36
+ id="selectionIndicator"
37
+ fill="#3a6cff"
38
+ r={10}
39
+ cx={0}
40
+ cy={0}
41
+ strokeWidth={1}
42
+ stroke="black"
48
43
  />
49
- </Marker>
50
- <Marker
51
- id="arrowheadStart"
52
- markerUnits={'strokeWidth' as MarkerUnits}
53
- refX="0"
54
- refY="0"
55
- orient="auto"
56
- >
57
- <Polyline
58
- points="2,-2 0,0 2,2 0,0"
59
- stroke="context-stroke"
60
- strokeLinecap="round"
61
- strokeLinejoin="round"
62
- />
63
- </Marker>
44
+ <Marker
45
+ id="arrowhead"
46
+ markerUnits={'strokeWidth' as MarkerUnits}
47
+ refX="0"
48
+ refY="0"
49
+ orient="auto"
50
+ >
51
+ <Polyline
52
+ points="-2,-2 0,0 -2,2 0,0"
53
+ stroke="context-stroke"
54
+ strokeLinecap="round"
55
+ strokeLinejoin="round"
56
+ />
57
+ </Marker>
58
+ <Marker
59
+ id="arrowheadStart"
60
+ markerUnits={'strokeWidth' as MarkerUnits}
61
+ refX="0"
62
+ refY="0"
63
+ orient="auto"
64
+ >
65
+ <Polyline
66
+ points="2,-2 0,0 2,2 0,0"
67
+ stroke="context-stroke"
68
+ strokeLinecap="round"
69
+ strokeLinejoin="round"
70
+ />
71
+ </Marker>
64
72
 
65
- <Marker
66
- id="side"
67
- markerUnits={'strokeWidth' as MarkerUnits}
68
- refX="0"
69
- refY="0"
70
- orient="auto"
71
- >
72
- <Polyline
73
- stroke="context-stroke"
74
- points="0,-2 0,2 0,0"
75
- strokeLinecap="round"
76
- strokeLinejoin="round"
77
- />
78
- </Marker>
73
+ <Marker
74
+ id="side"
75
+ markerUnits={'strokeWidth' as MarkerUnits}
76
+ refX="0"
77
+ refY="0"
78
+ orient="auto"
79
+ >
80
+ <Polyline
81
+ stroke="context-stroke"
82
+ points="0,-2 0,2 0,0"
83
+ strokeLinecap="round"
84
+ strokeLinejoin="round"
85
+ />
86
+ </Marker>
79
87
 
80
- <Marker
81
- id="selection"
82
- markerUnits={'userSpaceOnUse' as MarkerUnits}
83
- refX="0"
84
- refY="0"
85
- orient="auto"
86
- >
87
- <Use href="#selectionIndicator" />
88
- </Marker>
89
- </Defs>
88
+ <Marker
89
+ id="selection"
90
+ markerUnits={'userSpaceOnUse' as MarkerUnits}
91
+ refX="0"
92
+ refY="0"
93
+ orient="auto"
94
+ >
95
+ <Use href="#selectionIndicator" />
96
+ </Marker>
97
+ </Defs>
90
98
 
91
- {doneItems.map((item, index) => (
92
- <Item key={index} item={item} onPress={onPressItem(item, index)} />
93
- ))}
99
+ {doneItems.map((item, index) => (
100
+ <Item key={index} item={item} onPress={onPressItem(item, index)} />
101
+ ))}
94
102
 
95
- <CurrentAnimatedItem />
103
+ <CurrentAnimatedItem />
104
+ </G>
96
105
  </Svg>
97
106
  <CurrentAnimatedText
98
107
  currentItem={currentItem}
@@ -1,5 +1,12 @@
1
1
  import { DrawContext } from './DrawContext';
2
- import React, { ReactElement, useMemo, useReducer, useRef } from 'react';
2
+ import React, {
3
+ ReactElement,
4
+ useCallback,
5
+ useMemo,
6
+ useReducer,
7
+ useRef,
8
+ useState,
9
+ } from 'react';
3
10
  import type { Action, DrawItem, DrawState, hslColor } from '../../types';
4
11
  import { useSharedValue } from 'react-native-reanimated';
5
12
  import type ViewShot from 'react-native-view-shot';
@@ -92,10 +99,19 @@ const DrawProvider = ({ children }: { children: ReactElement }) => {
92
99
  const viewShot = useRef<ViewShot>(null);
93
100
  const doubleArrowTextInput = useRef<TextInput>(null);
94
101
 
102
+ const [snapShotRequested, setSnapShotRequested] = useState(false);
103
+
95
104
  const [drawState, dispatchDrawStates] = useReducer(
96
105
  reducerDrawStates,
97
106
  initialState
98
107
  );
108
+ const doSnapshot = useCallback(() => {
109
+ if (currentItem?.value) {
110
+ dispatchDrawStates({ type: 'ADD_DONE_ITEM', item: currentItem.value });
111
+ currentItem.value = null;
112
+ }
113
+ setSnapShotRequested(true);
114
+ }, [currentItem, dispatchDrawStates]);
99
115
 
100
116
  const contextValue = useMemo(
101
117
  () => ({
@@ -107,6 +123,9 @@ const DrawProvider = ({ children }: { children: ReactElement }) => {
107
123
  itemIsSelected,
108
124
  viewShot,
109
125
  doubleArrowTextInput,
126
+ doSnapshot,
127
+ snapShotRequested,
128
+ setSnapShotRequested,
110
129
  }),
111
130
  [
112
131
  drawState,
@@ -117,6 +136,9 @@ const DrawProvider = ({ children }: { children: ReactElement }) => {
117
136
  itemIsSelected,
118
137
  viewShot,
119
138
  doubleArrowTextInput,
139
+ doSnapshot,
140
+ snapShotRequested,
141
+ setSnapShotRequested,
120
142
  ]
121
143
  );
122
144
 
@@ -1,31 +1,14 @@
1
1
  import React from 'react';
2
- import { View, Text, StyleSheet } from 'react-native';
3
2
  import {
4
3
  G,
5
4
  Line,
6
5
  Ellipse,
7
6
  Rect,
8
7
  Path,
9
- ForeignObject,
10
8
  Text as SvgText,
11
9
  } from 'react-native-svg';
12
10
  import type { DrawItem } from '../../types';
13
- import { hslToRgb, pointsToPath } from './CurrentAnimatedItem';
14
-
15
- const styles = StyleSheet.create({
16
- textZone: {
17
- paddingHorizontal: 10,
18
- },
19
- textContainer: {
20
- //backgroundColor: 'rgba(0, 0, 0, 0.6)',
21
- //borderRadius: 10,
22
- paddingVertical: 13,
23
- paddingHorizontal: 18,
24
- },
25
- text: {
26
- color: 'white',
27
- },
28
- });
11
+ import { pointsToPath } from './CurrentAnimatedItem';
29
12
 
30
13
  export const distance = (x1: number, y1: number, x2: number, y2: number) => {
31
14
  return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
@@ -263,38 +246,23 @@ export default function Item({
263
246
  />
264
247
  );
265
248
  case 'text':
249
+ //console.log('Rendering text item:', JSON.stringify(item, null, 2));
266
250
  return (
267
- <G onPress={onPress}>
268
- <Rect {...item.data} onPress={onPress} fill={'transparent'} />
269
- <ForeignObject
270
- x={item.data.x}
271
- y={item.data.y}
272
- width={item.data.width}
273
- key={item.text}
251
+ <G onPress={onPress} fill={'red'}>
252
+ <G
253
+ x={String(item.data.x)}
254
+ y={String(Number(item.data.y ?? 0) + 2 * item.strokeWidth + 13)}
274
255
  >
275
- <View
276
- style={[
277
- styles.textZone,
278
- {
279
- width: item.data.width as number,
280
- },
281
- ]}
256
+ <SvgText
257
+ stroke={item.color}
258
+ fill={item.color}
259
+ textAnchor="start"
260
+ fontSize={2 * item.strokeWidth + 10}
261
+ onPress={onPress}
282
262
  >
283
- <View style={styles.textContainer}>
284
- <Text
285
- style={[
286
- styles.text,
287
- {
288
- fontSize: 10 + item.strokeWidth * 2,
289
- color: item.color ? hslToRgb(item.color) : 'white',
290
- },
291
- ]}
292
- >
293
- {item.text}
294
- </Text>
295
- </View>
296
- </View>
297
- </ForeignObject>
263
+ {item.text}
264
+ </SvgText>
265
+ </G>
298
266
  </G>
299
267
  );
300
268