@archireport/react-native-svg-draw 1.3.0 → 2.0.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 (179) hide show
  1. package/README.md +73 -27
  2. package/lib/commonjs/components/DrawCore/CurrentAnimatedItem.js +55 -54
  3. package/lib/commonjs/components/DrawCore/CurrentAnimatedItem.js.map +1 -1
  4. package/lib/commonjs/components/DrawCore/CurrentAnimatedText.js +3 -14
  5. package/lib/commonjs/components/DrawCore/CurrentAnimatedText.js.map +1 -1
  6. package/lib/commonjs/components/DrawCore/DrawContext.js +18 -0
  7. package/lib/commonjs/components/DrawCore/DrawContext.js.map +1 -0
  8. package/lib/commonjs/components/DrawCore/DrawPad.js +1 -13
  9. package/lib/commonjs/components/DrawCore/DrawPad.js.map +1 -1
  10. package/lib/commonjs/components/DrawCore/DrawProvider.js +103 -0
  11. package/lib/commonjs/components/DrawCore/DrawProvider.js.map +1 -0
  12. package/lib/commonjs/components/DrawCore/Item.js +0 -15
  13. package/lib/commonjs/components/DrawCore/Item.js.map +1 -1
  14. package/lib/commonjs/components/DrawCore/index.js +47 -287
  15. package/lib/commonjs/components/DrawCore/index.js.map +1 -1
  16. package/lib/commonjs/components/DrawCore/useDrawHook.js +76 -0
  17. package/lib/commonjs/components/DrawCore/useDrawHook.js.map +1 -0
  18. package/lib/commonjs/components/DrawWithOptions/ArrowSvg.js +0 -6
  19. package/lib/commonjs/components/DrawWithOptions/ArrowSvg.js.map +1 -1
  20. package/lib/commonjs/components/DrawWithOptions/CancelSvg.js +0 -6
  21. package/lib/commonjs/components/DrawWithOptions/CancelSvg.js.map +1 -1
  22. package/lib/commonjs/components/DrawWithOptions/CircleSvg.js +0 -6
  23. package/lib/commonjs/components/DrawWithOptions/CircleSvg.js.map +1 -1
  24. package/lib/commonjs/components/DrawWithOptions/CloseSvg.js +0 -6
  25. package/lib/commonjs/components/DrawWithOptions/CloseSvg.js.map +1 -1
  26. package/lib/commonjs/components/DrawWithOptions/DoubleHeadSvg.js +0 -7
  27. package/lib/commonjs/components/DrawWithOptions/DoubleHeadSvg.js.map +1 -1
  28. package/lib/commonjs/components/DrawWithOptions/PenSvg.js +0 -6
  29. package/lib/commonjs/components/DrawWithOptions/PenSvg.js.map +1 -1
  30. package/lib/commonjs/components/DrawWithOptions/SendSvg.js +0 -7
  31. package/lib/commonjs/components/DrawWithOptions/SendSvg.js.map +1 -1
  32. package/lib/commonjs/components/DrawWithOptions/SliderSvg.js +31 -0
  33. package/lib/commonjs/components/DrawWithOptions/SliderSvg.js.map +1 -0
  34. package/lib/commonjs/components/DrawWithOptions/SquareSvg.js +0 -7
  35. package/lib/commonjs/components/DrawWithOptions/SquareSvg.js.map +1 -1
  36. package/lib/commonjs/components/DrawWithOptions/TextSvg.js +0 -7
  37. package/lib/commonjs/components/DrawWithOptions/TextSvg.js.map +1 -1
  38. package/lib/commonjs/components/DrawWithOptions/ThrashSvg.js +27 -10
  39. package/lib/commonjs/components/DrawWithOptions/ThrashSvg.js.map +1 -1
  40. package/lib/commonjs/components/DrawWithOptions/index.js +126 -93
  41. package/lib/commonjs/components/DrawWithOptions/index.js.map +1 -1
  42. package/lib/commonjs/components/{DrawCore → Slider}/ColorSlider.js +47 -78
  43. package/lib/commonjs/components/Slider/ColorSlider.js.map +1 -0
  44. package/lib/commonjs/components/Slider/Sliders.js +88 -0
  45. package/lib/commonjs/components/Slider/Sliders.js.map +1 -0
  46. package/lib/commonjs/components/{DrawCore → Slider}/StrokeSlider.js +38 -57
  47. package/lib/commonjs/components/Slider/StrokeSlider.js.map +1 -0
  48. package/lib/commonjs/components/Slider/sliderStyle.js +37 -0
  49. package/lib/commonjs/components/Slider/sliderStyle.js.map +1 -0
  50. package/lib/commonjs/index.js +7 -3
  51. package/lib/commonjs/index.js.map +1 -1
  52. package/lib/commonjs/types.d.js.map +1 -1
  53. package/lib/module/components/DrawCore/CurrentAnimatedItem.js +56 -43
  54. package/lib/module/components/DrawCore/CurrentAnimatedItem.js.map +1 -1
  55. package/lib/module/components/DrawCore/CurrentAnimatedText.js +3 -7
  56. package/lib/module/components/DrawCore/CurrentAnimatedText.js.map +1 -1
  57. package/lib/module/components/DrawCore/DrawContext.js +11 -0
  58. package/lib/module/components/DrawCore/DrawContext.js.map +1 -0
  59. package/lib/module/components/DrawCore/DrawPad.js +1 -5
  60. package/lib/module/components/DrawCore/DrawPad.js.map +1 -1
  61. package/lib/module/components/DrawCore/DrawProvider.js +94 -0
  62. package/lib/module/components/DrawCore/DrawProvider.js.map +1 -0
  63. package/lib/module/components/DrawCore/Item.js +0 -8
  64. package/lib/module/components/DrawCore/Item.js.map +1 -1
  65. package/lib/module/components/DrawCore/index.js +49 -271
  66. package/lib/module/components/DrawCore/index.js.map +1 -1
  67. package/lib/module/components/DrawCore/useDrawHook.js +69 -0
  68. package/lib/module/components/DrawCore/useDrawHook.js.map +1 -0
  69. package/lib/module/components/DrawWithOptions/ArrowSvg.js +0 -1
  70. package/lib/module/components/DrawWithOptions/ArrowSvg.js.map +1 -1
  71. package/lib/module/components/DrawWithOptions/CancelSvg.js +0 -1
  72. package/lib/module/components/DrawWithOptions/CancelSvg.js.map +1 -1
  73. package/lib/module/components/DrawWithOptions/CircleSvg.js +0 -1
  74. package/lib/module/components/DrawWithOptions/CircleSvg.js.map +1 -1
  75. package/lib/module/components/DrawWithOptions/CloseSvg.js +0 -1
  76. package/lib/module/components/DrawWithOptions/CloseSvg.js.map +1 -1
  77. package/lib/module/components/DrawWithOptions/DoubleHeadSvg.js +0 -1
  78. package/lib/module/components/DrawWithOptions/DoubleHeadSvg.js.map +1 -1
  79. package/lib/module/components/DrawWithOptions/PenSvg.js +0 -1
  80. package/lib/module/components/DrawWithOptions/PenSvg.js.map +1 -1
  81. package/lib/module/components/DrawWithOptions/SendSvg.js +0 -3
  82. package/lib/module/components/DrawWithOptions/SendSvg.js.map +1 -1
  83. package/lib/module/components/DrawWithOptions/SliderSvg.js +22 -0
  84. package/lib/module/components/DrawWithOptions/SliderSvg.js.map +1 -0
  85. package/lib/module/components/DrawWithOptions/SquareSvg.js +0 -1
  86. package/lib/module/components/DrawWithOptions/SquareSvg.js.map +1 -1
  87. package/lib/module/components/DrawWithOptions/TextSvg.js +0 -3
  88. package/lib/module/components/DrawWithOptions/TextSvg.js.map +1 -1
  89. package/lib/module/components/DrawWithOptions/ThrashSvg.js +27 -5
  90. package/lib/module/components/DrawWithOptions/ThrashSvg.js.map +1 -1
  91. package/lib/module/components/DrawWithOptions/index.js +126 -73
  92. package/lib/module/components/DrawWithOptions/index.js.map +1 -1
  93. package/lib/module/components/Slider/ColorSlider.js +95 -0
  94. package/lib/module/components/Slider/ColorSlider.js.map +1 -0
  95. package/lib/module/components/Slider/Sliders.js +79 -0
  96. package/lib/module/components/Slider/Sliders.js.map +1 -0
  97. package/lib/module/components/Slider/StrokeSlider.js +82 -0
  98. package/lib/module/components/Slider/StrokeSlider.js.map +1 -0
  99. package/lib/module/components/Slider/sliderStyle.js +29 -0
  100. package/lib/module/components/Slider/sliderStyle.js.map +1 -0
  101. package/lib/module/index.js +1 -0
  102. package/lib/module/index.js.map +1 -1
  103. package/lib/module/types.d.js.map +1 -1
  104. package/lib/typescript/components/DrawCore/CurrentAnimatedItem.d.ts +1 -1
  105. package/lib/typescript/components/DrawCore/CurrentAnimatedItem.d.ts.map +1 -0
  106. package/lib/typescript/components/DrawCore/CurrentAnimatedText.d.ts +1 -1
  107. package/lib/typescript/components/DrawCore/CurrentAnimatedText.d.ts.map +1 -0
  108. package/lib/typescript/components/DrawCore/DrawContext.d.ts +14 -0
  109. package/lib/typescript/components/DrawCore/DrawContext.d.ts.map +1 -0
  110. package/lib/typescript/components/DrawCore/DrawPad.d.ts +1 -1
  111. package/lib/typescript/components/DrawCore/DrawPad.d.ts.map +1 -0
  112. package/lib/typescript/components/DrawCore/DrawProvider.d.ts +6 -0
  113. package/lib/typescript/components/DrawCore/DrawProvider.d.ts.map +1 -0
  114. package/lib/typescript/components/DrawCore/Item.d.ts +1 -1
  115. package/lib/typescript/components/DrawCore/Item.d.ts.map +1 -0
  116. package/lib/typescript/components/DrawCore/index.d.ts +4 -11
  117. package/lib/typescript/components/DrawCore/index.d.ts.map +1 -0
  118. package/lib/typescript/components/DrawCore/useDrawHook.d.ts +18 -0
  119. package/lib/typescript/components/DrawCore/useDrawHook.d.ts.map +1 -0
  120. package/lib/typescript/components/DrawWithOptions/ArrowSvg.d.ts +1 -1
  121. package/lib/typescript/components/DrawWithOptions/ArrowSvg.d.ts.map +1 -0
  122. package/lib/typescript/components/DrawWithOptions/CancelSvg.d.ts +1 -1
  123. package/lib/typescript/components/DrawWithOptions/CancelSvg.d.ts.map +1 -0
  124. package/lib/typescript/components/DrawWithOptions/CircleSvg.d.ts +1 -1
  125. package/lib/typescript/components/DrawWithOptions/CircleSvg.d.ts.map +1 -0
  126. package/lib/typescript/components/DrawWithOptions/CloseSvg.d.ts +1 -1
  127. package/lib/typescript/components/DrawWithOptions/CloseSvg.d.ts.map +1 -0
  128. package/lib/typescript/components/DrawWithOptions/DoubleHeadSvg.d.ts +1 -1
  129. package/lib/typescript/components/DrawWithOptions/DoubleHeadSvg.d.ts.map +1 -0
  130. package/lib/typescript/components/DrawWithOptions/PenSvg.d.ts +1 -1
  131. package/lib/typescript/components/DrawWithOptions/PenSvg.d.ts.map +1 -0
  132. package/lib/typescript/components/DrawWithOptions/SendSvg.d.ts +1 -1
  133. package/lib/typescript/components/DrawWithOptions/SendSvg.d.ts.map +1 -0
  134. package/lib/typescript/components/DrawWithOptions/SliderSvg.d.ts +4 -0
  135. package/lib/typescript/components/DrawWithOptions/SliderSvg.d.ts.map +1 -0
  136. package/lib/typescript/components/DrawWithOptions/SquareSvg.d.ts +1 -1
  137. package/lib/typescript/components/DrawWithOptions/SquareSvg.d.ts.map +1 -0
  138. package/lib/typescript/components/DrawWithOptions/TextSvg.d.ts +1 -1
  139. package/lib/typescript/components/DrawWithOptions/TextSvg.d.ts.map +1 -0
  140. package/lib/typescript/components/DrawWithOptions/ThrashSvg.d.ts +1 -1
  141. package/lib/typescript/components/DrawWithOptions/ThrashSvg.d.ts.map +1 -0
  142. package/lib/typescript/components/DrawWithOptions/index.d.ts +4 -5
  143. package/lib/typescript/components/DrawWithOptions/index.d.ts.map +1 -0
  144. package/lib/typescript/components/Slider/ColorSlider.d.ts +8 -0
  145. package/lib/typescript/components/Slider/ColorSlider.d.ts.map +1 -0
  146. package/lib/typescript/components/Slider/Sliders.d.ts +8 -0
  147. package/lib/typescript/components/Slider/Sliders.d.ts.map +1 -0
  148. package/lib/typescript/components/Slider/StrokeSlider.d.ts +6 -0
  149. package/lib/typescript/components/Slider/StrokeSlider.d.ts.map +1 -0
  150. package/lib/typescript/components/Slider/sliderStyle.d.ts +28 -0
  151. package/lib/typescript/components/Slider/sliderStyle.d.ts.map +1 -0
  152. package/lib/typescript/index.d.ts +2 -0
  153. package/lib/typescript/index.d.ts.map +1 -0
  154. package/package.json +32 -33
  155. package/src/components/DrawCore/CurrentAnimatedItem.tsx +166 -106
  156. package/src/components/DrawCore/DrawContext.tsx +22 -0
  157. package/src/components/DrawCore/DrawPad.tsx +3 -10
  158. package/src/components/DrawCore/DrawProvider.tsx +122 -0
  159. package/src/components/DrawCore/index.tsx +938 -1132
  160. package/src/components/DrawCore/useDrawHook.tsx +70 -0
  161. package/src/components/DrawWithOptions/SliderSvg.tsx +24 -0
  162. package/src/components/DrawWithOptions/ThrashSvg.tsx +29 -2
  163. package/src/components/DrawWithOptions/index.tsx +199 -146
  164. package/src/components/Slider/ColorSlider.tsx +144 -0
  165. package/src/components/Slider/Sliders.tsx +75 -0
  166. package/src/components/Slider/StrokeSlider.tsx +104 -0
  167. package/src/components/Slider/sliderStyle.tsx +28 -0
  168. package/src/index.tsx +1 -0
  169. package/src/types.d.ts +35 -1
  170. package/lib/commonjs/components/DrawCore/ColorSlider.js.map +0 -1
  171. package/lib/commonjs/components/DrawCore/StrokeSlider.js.map +0 -1
  172. package/lib/module/components/DrawCore/ColorSlider.js +0 -119
  173. package/lib/module/components/DrawCore/ColorSlider.js.map +0 -1
  174. package/lib/module/components/DrawCore/StrokeSlider.js +0 -93
  175. package/lib/module/components/DrawCore/StrokeSlider.js.map +0 -1
  176. package/lib/typescript/components/DrawCore/ColorSlider.d.ts +0 -12
  177. package/lib/typescript/components/DrawCore/StrokeSlider.d.ts +0 -9
  178. package/src/components/DrawCore/ColorSlider.tsx +0 -169
  179. package/src/components/DrawCore/StrokeSlider.tsx +0 -117
@@ -1,5 +1,9 @@
1
1
  import React from 'react';
2
- import Animated, { useAnimatedProps } from 'react-native-reanimated';
2
+ import Animated, {
3
+ createAnimatedPropAdapter,
4
+ processColor,
5
+ useAnimatedProps,
6
+ } from 'react-native-reanimated';
3
7
  import { Path, Ellipse, Rect, Line, G } from 'react-native-svg';
4
8
  import type { DrawItem, hslColor, Point } from '../../types';
5
9
 
@@ -49,13 +53,26 @@ const controlPoint = (
49
53
  // create the bezier curve command
50
54
  const bezierCommand = (point: Point, i: number, a: Point[]) => {
51
55
  'worklet';
52
- const endPoint: Point = controlPoint(point, a[i - 1], a[i + 1], true);
53
- if (i === 1) {
54
- const startPoint: Point = controlPoint(a[i - 1], a[i - 2], point, true);
55
- return `C ${startPoint.x},${startPoint.y} ${endPoint.x},${endPoint.y} ${point.x},${point.y}`;
56
- } else {
57
- return `S ${endPoint.x},${endPoint.y} ${point.x},${point.y}`;
56
+ const previousPoint = a[i - 1];
57
+ const nextPoint = a[i + 1];
58
+ if (previousPoint && nextPoint) {
59
+ const endPoint: Point = controlPoint(point, previousPoint, nextPoint, true);
60
+ if (i === 1) {
61
+ const pointBefore = a[i - 2];
62
+ if (pointBefore) {
63
+ const startPoint: Point = controlPoint(
64
+ previousPoint,
65
+ pointBefore,
66
+ point,
67
+ true
68
+ );
69
+ return `C ${startPoint.x},${startPoint.y} ${endPoint.x},${endPoint.y} ${point.x},${point.y}`;
70
+ }
71
+ } else {
72
+ return `S ${endPoint.x},${endPoint.y} ${point.x},${point.y}`;
73
+ }
58
74
  }
75
+ return '';
59
76
  };
60
77
 
61
78
  export const pointsToPath = (points: Point[]) => {
@@ -97,9 +114,9 @@ function hslToRgb(col: hslColor) {
97
114
  const hslRegExp = new RegExp(/hsl\(([\d.]+),\s*(\d+)%,\s*([\d.]+)%\)/);
98
115
  const res = hslRegExp.exec(col);
99
116
 
100
- const h = res ? parseFloat(res[1]) / 360 : 0;
101
- const s = res ? parseFloat(res[2]) / 100 : 0;
102
- const l = res ? parseFloat(res[3]) / 100 : 0;
117
+ const h = res ? parseFloat(res[1] ?? '0') / 360 : 0;
118
+ const s = res ? parseFloat(res[2] ?? '0') / 100 : 0;
119
+ const l = res ? parseFloat(res[3] ?? '0') / 100 : 0;
103
120
 
104
121
  var r, g, b;
105
122
 
@@ -118,111 +135,154 @@ function hslToRgb(col: hslColor) {
118
135
  )})`;
119
136
  }
120
137
 
138
+ const propAdapter = createAnimatedPropAdapter(
139
+ (props: Record<string, unknown>) => {
140
+ if (
141
+ Object.keys(props).includes('fill') &&
142
+ (typeof props.fill === 'string' || typeof props.fill === 'number')
143
+ ) {
144
+ props.fill = { type: 0, payload: processColor(props.fill) };
145
+ }
146
+ if (
147
+ Object.keys(props).includes('stroke') &&
148
+ (typeof props.stroke === 'string' || typeof props.stroke === 'number')
149
+ ) {
150
+ props.stroke = { type: 0, payload: processColor(props.stroke) };
151
+ }
152
+ },
153
+ ['fill', 'stroke']
154
+ );
155
+
121
156
  export default function CurrentAnimatedItem({
122
157
  currentItem,
123
158
  }: {
124
159
  currentItem: Animated.SharedValue<DrawItem | null>;
125
160
  }) {
126
- const ellipseAnimatedProps = useAnimatedProps(() => {
127
- const coordinates =
128
- currentItem.value?.type === 'ellipse'
129
- ? currentItem.value.data
130
- : { cx: -10, cy: -10, rx: 0, ry: 0 };
131
-
132
- return {
133
- cx: coordinates.cx,
134
- cy: coordinates.cy,
135
- rx: coordinates.rx,
136
- ry: coordinates.ry,
137
- stroke: hslToRgb(currentItem.value?.color || 'hsl(0, 0%, 0%)'),
138
- opacity: currentItem.value?.type === 'ellipse' ? 1 : 0,
139
- strokeWidth:
161
+ const ellipseAnimatedProps = useAnimatedProps(
162
+ () => {
163
+ const coordinates =
140
164
  currentItem.value?.type === 'ellipse'
141
- ? currentItem.value.strokeWidth
142
- : 0,
143
- marker: 'url(#selection)',
144
- };
145
- }, [currentItem.value]);
146
-
147
- const singleHeadAnimatedProps = useAnimatedProps(() => {
148
- const coordinates =
149
- currentItem.value?.type === 'singleHead'
150
- ? currentItem.value.data
151
- : { x1: -10, y1: -10, x2: -10, y2: -10 };
152
- return {
153
- x1: coordinates.x1,
154
- y1: coordinates.y1,
155
- x2: coordinates.x2,
156
- y2: coordinates.y2,
157
- stroke: hslToRgb(currentItem.value?.color || 'hsl(0, 0%, 0%)'),
158
- opacity: currentItem.value?.type === 'singleHead' ? 1 : 0,
159
- strokeWidth:
165
+ ? currentItem.value.data
166
+ : { cx: -10, cy: -10, rx: 0, ry: 0 };
167
+
168
+ return {
169
+ cx: coordinates.cx,
170
+ cy: coordinates.cy,
171
+ rx: coordinates.rx,
172
+ ry: coordinates.ry,
173
+ fill: 'transparent',
174
+ stroke: hslToRgb(currentItem.value?.color || 'hsl(0, 0%, 0%)'),
175
+ opacity: currentItem.value?.type === 'ellipse' ? 1 : 0,
176
+ strokeWidth:
177
+ currentItem.value?.type === 'ellipse'
178
+ ? currentItem.value.strokeWidth
179
+ : 0,
180
+ marker: 'url(#selection)',
181
+ };
182
+ },
183
+ null,
184
+ propAdapter
185
+ );
186
+
187
+ const singleHeadAnimatedProps = useAnimatedProps(
188
+ () => {
189
+ const coordinates =
160
190
  currentItem.value?.type === 'singleHead'
161
- ? currentItem.value.strokeWidth
162
- : 0,
163
- markerEnd: 'arrowhead',
164
- };
165
- }, [currentItem.value]);
166
-
167
- const doubleHeadAnimatedProps = useAnimatedProps(() => {
168
- const coordinates =
169
- currentItem.value?.type === 'doubleHead'
170
- ? currentItem.value.data
171
- : { x1: -10, y1: -10, x2: -10, y2: -10 };
172
-
173
- return {
174
- x1: coordinates.x1,
175
- y1: coordinates.y1,
176
- x2: coordinates.x2,
177
- y2: coordinates.y2,
178
- stroke: hslToRgb(currentItem.value?.color || 'hsl(0, 0%, 0%)'),
179
- opacity: currentItem.value?.type === 'doubleHead' ? 1 : 0,
180
- strokeWidth:
191
+ ? currentItem.value.data
192
+ : { x1: -10, y1: -10, x2: -10, y2: -10 };
193
+ return {
194
+ x1: coordinates.x1,
195
+ y1: coordinates.y1,
196
+ x2: coordinates.x2,
197
+ y2: coordinates.y2,
198
+ fill: 'transparent',
199
+ stroke: hslToRgb(currentItem.value?.color || 'hsl(0, 0%, 0%)'),
200
+ opacity: currentItem.value?.type === 'singleHead' ? 1 : 0,
201
+ strokeWidth:
202
+ currentItem.value?.type === 'singleHead'
203
+ ? currentItem.value.strokeWidth
204
+ : 0,
205
+ markerEnd: 'arrowhead',
206
+ };
207
+ },
208
+ null,
209
+ propAdapter
210
+ );
211
+
212
+ const doubleHeadAnimatedProps = useAnimatedProps(
213
+ () => {
214
+ const coordinates =
181
215
  currentItem.value?.type === 'doubleHead'
182
- ? currentItem.value.strokeWidth
183
- : 0,
184
- markerStart: 'side',
185
- markerEnd: 'side',
186
- };
187
- }, [currentItem.value]);
188
-
189
- const rectangleAnimatedProps = useAnimatedProps(() => {
190
- const coordinates =
191
- currentItem.value?.type === 'rectangle'
192
- ? currentItem.value.data
193
- : { x: -10, y: -10, width: 0, height: 0 };
194
- return {
195
- x: coordinates.x,
196
- y: coordinates.y,
197
- width: coordinates.width,
198
- height: coordinates.height,
199
- stroke: hslToRgb(currentItem.value?.color || 'hsl(0, 0%, 0%)'),
200
- opacity: currentItem.value?.type === 'rectangle' ? 1 : 0,
201
- strokeWidth:
216
+ ? currentItem.value.data
217
+ : { x1: -10, y1: -10, x2: -10, y2: -10 };
218
+
219
+ return {
220
+ x1: coordinates.x1,
221
+ y1: coordinates.y1,
222
+ x2: coordinates.x2,
223
+ y2: coordinates.y2,
224
+ fill: 'transparent',
225
+ stroke: hslToRgb(currentItem.value?.color || 'hsl(0, 0%, 0%)'),
226
+ opacity: currentItem.value?.type === 'doubleHead' ? 1 : 0,
227
+ strokeWidth:
228
+ currentItem.value?.type === 'doubleHead'
229
+ ? currentItem.value.strokeWidth
230
+ : 0,
231
+ markerStart: 'side',
232
+ markerEnd: 'side',
233
+ };
234
+ },
235
+ null,
236
+ propAdapter
237
+ );
238
+
239
+ const rectangleAnimatedProps = useAnimatedProps(
240
+ () => {
241
+ const coordinates =
202
242
  currentItem.value?.type === 'rectangle'
203
- ? currentItem.value.strokeWidth
204
- : 0,
205
-
206
- marker: 'url(#selection)',
207
- };
208
- }, [currentItem.value]);
209
-
210
- const penAnimatedProps = useAnimatedProps(() => {
211
- const d = pointsToPath(
212
- currentItem.value?.type === 'pen'
213
- ? currentItem.value.data
214
- : [{ x: -10, y: -10 }]
215
- );
216
- return {
217
- d: d,
218
- strokeWidth:
219
- currentItem.value?.type === 'pen' ? currentItem.value.strokeWidth : 0,
220
- stroke: hslToRgb(currentItem.value?.color || 'hsl(0, 0%, 0%)'),
221
- opacity: currentItem.value?.type === 'pen' ? 1 : 0,
222
- markerStart: 'selection',
223
- markerEnd: 'selection',
224
- };
225
- }, [currentItem.value]);
243
+ ? currentItem.value.data
244
+ : { x: -10, y: -10, width: 0, height: 0 };
245
+ return {
246
+ x: coordinates.x,
247
+ y: coordinates.y,
248
+ width: coordinates.width,
249
+ height: coordinates.height,
250
+ fill: 'transparent',
251
+ stroke: hslToRgb(currentItem.value?.color || 'hsl(0, 0%, 0%)'),
252
+ opacity: currentItem.value?.type === 'rectangle' ? 1 : 0,
253
+ strokeWidth:
254
+ currentItem.value?.type === 'rectangle'
255
+ ? currentItem.value.strokeWidth
256
+ : 0,
257
+
258
+ marker: 'url(#selection)',
259
+ };
260
+ },
261
+ null,
262
+ propAdapter
263
+ );
264
+
265
+ const penAnimatedProps = useAnimatedProps(
266
+ () => {
267
+ const d = pointsToPath(
268
+ currentItem.value?.type === 'pen'
269
+ ? currentItem.value.data
270
+ : [{ x: -10, y: -10 }]
271
+ );
272
+ return {
273
+ d: d,
274
+ strokeWidth:
275
+ currentItem.value?.type === 'pen' ? currentItem.value.strokeWidth : 0,
276
+ stroke: hslToRgb(currentItem.value?.color || 'hsl(0, 0%, 0%)'),
277
+ opacity: currentItem.value?.type === 'pen' ? 1 : 0,
278
+ fill: 'transparent',
279
+ markerStart: 'selection',
280
+ markerEnd: 'selection',
281
+ };
282
+ },
283
+ null,
284
+ propAdapter
285
+ );
226
286
 
227
287
  return (
228
288
  <>
@@ -0,0 +1,22 @@
1
+ import type { DrawItem, hslColor, DrawState, Action } from '../../types';
2
+ import { createContext, RefObject } from 'react';
3
+ import type { SharedValue } from 'react-native-reanimated';
4
+ import type ViewShot from 'react-native-view-shot';
5
+
6
+ export const DrawContext = createContext<{
7
+ drawState: DrawState;
8
+ dispatchDrawStates: React.Dispatch<Action>;
9
+ strokeWidth?: SharedValue<number>;
10
+ color?: SharedValue<hslColor>;
11
+ currentItem?: SharedValue<DrawItem | null>;
12
+ itemIsSelected?: SharedValue<boolean>;
13
+ viewShot?: RefObject<ViewShot>;
14
+ }>({
15
+ drawState: {
16
+ doneItems: [],
17
+ screenStates: [[]],
18
+ drawingMode: 'ellipse',
19
+ cancelEnabled: false,
20
+ },
21
+ dispatchDrawStates: () => true,
22
+ });
@@ -1,17 +1,11 @@
1
1
  import React from 'react';
2
2
  import type Animated from 'react-native-reanimated';
3
- import Svg, {
4
- Circle,
5
- Defs,
6
- Marker,
7
- MarkerUnits,
8
- Polyline,
9
- Use,
10
- } from 'react-native-svg';
3
+ import Svg, { Circle, Defs, Marker, Polyline, Use } from 'react-native-svg';
11
4
  import CurrentAnimatedItem from './CurrentAnimatedItem';
12
5
  import CurrentAnimatedText from './CurrentAnimatedText';
13
6
  import Item from './Item';
14
7
  import type { DrawItem } from '../../types';
8
+ import type { MarkerUnits } from 'react-native-svg/lib/typescript/elements/Marker';
15
9
 
16
10
  const DrawPad = ({
17
11
  currentItem,
@@ -26,7 +20,7 @@ const DrawPad = ({
26
20
  }) => {
27
21
  return (
28
22
  <>
29
- <Svg fillRule="evenodd">
23
+ <Svg>
30
24
  <Defs>
31
25
  <Circle
32
26
  id="selectionIndicator"
@@ -84,7 +78,6 @@ const DrawPad = ({
84
78
 
85
79
  <CurrentAnimatedItem currentItem={currentItem} />
86
80
  </Svg>
87
-
88
81
  <CurrentAnimatedText
89
82
  currentItem={currentItem}
90
83
  onHeightChange={onTextHeightChange}
@@ -0,0 +1,122 @@
1
+ import { DrawContext } from './DrawContext';
2
+ import React, { ReactElement, useMemo, useReducer, useRef } from 'react';
3
+ import type { Action, DrawItem, DrawState, hslColor } from '../../types';
4
+ import { useSharedValue } from 'react-native-reanimated';
5
+ import type ViewShot from 'react-native-view-shot';
6
+
7
+ const initialState: DrawState = {
8
+ doneItems: [],
9
+ screenStates: [[]],
10
+ cancelEnabled: false,
11
+ drawingMode: 'pen',
12
+ };
13
+
14
+ const reducerDrawStates = (drawState: DrawState, action: Action): DrawState => {
15
+ 'worklet';
16
+ switch (action.type) {
17
+ case 'SET_CANCEL_ENABLED':
18
+ return {
19
+ ...drawState,
20
+ cancelEnabled: action.cancelEnabled,
21
+ };
22
+ case 'SET_DRAWING_MODE':
23
+ return {
24
+ ...drawState,
25
+ drawingMode: action.drawingMode,
26
+ };
27
+ case 'ADD_DONE_ITEM':
28
+ return {
29
+ ...drawState,
30
+ doneItems: drawState.doneItems.concat(action.item),
31
+ };
32
+ case 'DELETE_DONE_ITEM':
33
+ const newDoneItems = drawState.doneItems;
34
+ newDoneItems.splice(action.indice, 1);
35
+ return {
36
+ ...drawState,
37
+ doneItems: newDoneItems,
38
+ };
39
+ case 'ADD_SCREEN_STATE':
40
+ if (action.currentItem) {
41
+ // hack sometime rectangle is not draw
42
+ if (
43
+ action.currentItem.type === 'rectangle' &&
44
+ (!action.currentItem.data.width || !action.currentItem.data.height)
45
+ ) {
46
+ return drawState;
47
+ } else {
48
+ return {
49
+ ...drawState,
50
+ screenStates: drawState.screenStates.concat([
51
+ [...drawState.doneItems, action.currentItem],
52
+ ]),
53
+ };
54
+ }
55
+ } else {
56
+ return {
57
+ ...drawState,
58
+ cancelEnabled: action.cancelEnabled ?? drawState.cancelEnabled,
59
+ screenStates: drawState.screenStates.concat([
60
+ [...drawState.doneItems],
61
+ ]),
62
+ };
63
+ }
64
+
65
+ case 'CANCEL':
66
+ const len = drawState.screenStates.length;
67
+ if (len > 1) {
68
+ const newScreenStates = drawState.screenStates;
69
+ newScreenStates.pop();
70
+
71
+ return {
72
+ ...drawState,
73
+ cancelEnabled:
74
+ newScreenStates.length === 1 ? false : drawState.cancelEnabled,
75
+ doneItems: drawState.screenStates[len - 2] ?? [],
76
+ screenStates: newScreenStates,
77
+ };
78
+ } else {
79
+ return drawState;
80
+ }
81
+ }
82
+ };
83
+
84
+ const DrawProvider = ({ children }: { children: ReactElement }) => {
85
+ const itemIsSelected = useSharedValue<boolean>(false);
86
+ const strokeWidth = useSharedValue<number>(2);
87
+ const color = useSharedValue<hslColor>('hsl(0, 100%, 0%)');
88
+ const currentItem = useSharedValue<DrawItem | null>(null);
89
+ const viewShot = useRef<ViewShot>(null);
90
+
91
+ const [drawState, dispatchDrawStates] = useReducer(
92
+ reducerDrawStates,
93
+ initialState
94
+ );
95
+
96
+ const contextValue = useMemo(
97
+ () => ({
98
+ drawState,
99
+ dispatchDrawStates,
100
+ strokeWidth,
101
+ color,
102
+ currentItem,
103
+ itemIsSelected,
104
+ viewShot,
105
+ }),
106
+ [
107
+ drawState,
108
+ dispatchDrawStates,
109
+ strokeWidth,
110
+ color,
111
+ currentItem,
112
+ itemIsSelected,
113
+ viewShot,
114
+ ]
115
+ );
116
+
117
+ return (
118
+ <DrawContext.Provider value={contextValue}>{children}</DrawContext.Provider>
119
+ );
120
+ };
121
+
122
+ export default DrawProvider;