@archireport/react-native-svg-draw 1.0.1 → 1.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 (53) hide show
  1. package/lib/commonjs/components/DrawCore/ColorSlider.js +8 -5
  2. package/lib/commonjs/components/DrawCore/ColorSlider.js.map +1 -1
  3. package/lib/commonjs/components/DrawCore/CurrentAnimatedItem.js +56 -6
  4. package/lib/commonjs/components/DrawCore/CurrentAnimatedItem.js.map +1 -1
  5. package/lib/commonjs/components/DrawCore/DrawPad.js +4 -4
  6. package/lib/commonjs/components/DrawCore/DrawPad.js.map +1 -1
  7. package/lib/commonjs/components/DrawCore/Item.js +3 -5
  8. package/lib/commonjs/components/DrawCore/Item.js.map +1 -1
  9. package/lib/commonjs/components/DrawCore/StrokeSlider.js +5 -2
  10. package/lib/commonjs/components/DrawCore/StrokeSlider.js.map +1 -1
  11. package/lib/commonjs/components/DrawCore/index.js +139 -52
  12. package/lib/commonjs/components/DrawCore/index.js.map +1 -1
  13. package/lib/commonjs/components/DrawWithOptions/CancelSvg.js +35 -0
  14. package/lib/commonjs/components/DrawWithOptions/CancelSvg.js.map +1 -0
  15. package/lib/commonjs/components/DrawWithOptions/PenSvg.js +27 -0
  16. package/lib/commonjs/components/DrawWithOptions/PenSvg.js.map +1 -0
  17. package/lib/commonjs/components/DrawWithOptions/index.js +30 -1
  18. package/lib/commonjs/components/DrawWithOptions/index.js.map +1 -1
  19. package/lib/module/components/DrawCore/ColorSlider.js +9 -6
  20. package/lib/module/components/DrawCore/ColorSlider.js.map +1 -1
  21. package/lib/module/components/DrawCore/CurrentAnimatedItem.js +53 -7
  22. package/lib/module/components/DrawCore/CurrentAnimatedItem.js.map +1 -1
  23. package/lib/module/components/DrawCore/DrawPad.js +4 -4
  24. package/lib/module/components/DrawCore/DrawPad.js.map +1 -1
  25. package/lib/module/components/DrawCore/Item.js +2 -6
  26. package/lib/module/components/DrawCore/Item.js.map +1 -1
  27. package/lib/module/components/DrawCore/StrokeSlider.js +5 -2
  28. package/lib/module/components/DrawCore/StrokeSlider.js.map +1 -1
  29. package/lib/module/components/DrawCore/index.js +140 -53
  30. package/lib/module/components/DrawCore/index.js.map +1 -1
  31. package/lib/module/components/DrawWithOptions/CancelSvg.js +22 -0
  32. package/lib/module/components/DrawWithOptions/CancelSvg.js.map +1 -0
  33. package/lib/module/components/DrawWithOptions/PenSvg.js +14 -0
  34. package/lib/module/components/DrawWithOptions/PenSvg.js.map +1 -0
  35. package/lib/module/components/DrawWithOptions/index.js +28 -1
  36. package/lib/module/components/DrawWithOptions/index.js.map +1 -1
  37. package/lib/typescript/components/DrawCore/ColorSlider.d.ts +2 -1
  38. package/lib/typescript/components/DrawCore/CurrentAnimatedItem.d.ts +2 -1
  39. package/lib/typescript/components/DrawCore/StrokeSlider.d.ts +2 -1
  40. package/lib/typescript/components/DrawCore/index.d.ts +1 -0
  41. package/lib/typescript/components/DrawWithOptions/CancelSvg.d.ts +2 -0
  42. package/lib/typescript/components/DrawWithOptions/PenSvg.d.ts +2 -0
  43. package/package.json +5 -5
  44. package/src/components/DrawCore/ColorSlider.tsx +11 -6
  45. package/src/components/DrawCore/CurrentAnimatedItem.tsx +59 -8
  46. package/src/components/DrawCore/DrawPad.tsx +2 -2
  47. package/src/components/DrawCore/Item.tsx +3 -11
  48. package/src/components/DrawCore/StrokeSlider.tsx +5 -1
  49. package/src/components/DrawCore/index.tsx +1105 -971
  50. package/src/components/DrawWithOptions/CancelSvg.tsx +23 -0
  51. package/src/components/DrawWithOptions/PenSvg.tsx +14 -0
  52. package/src/components/DrawWithOptions/index.tsx +34 -0
  53. package/src/types.d.ts +4 -1
@@ -1,4 +1,4 @@
1
- import React, { useCallback, useEffect, useImperativeHandle, useRef, useState } from 'react';
1
+ import React, { useCallback, useEffect, useImperativeHandle, useReducer, useRef, useState } from 'react';
2
2
  import { StyleSheet, TextInput, View, Keyboard, ImageBackground, Image, Platform, InputAccessoryView } from 'react-native';
3
3
  import Animated, { runOnJS, useAnimatedGestureHandler, useAnimatedReaction, useAnimatedStyle, useSharedValue, withTiming } from 'react-native-reanimated';
4
4
  import { PanGestureHandler } from 'react-native-gesture-handler';
@@ -54,7 +54,7 @@ const styles = StyleSheet.create({
54
54
  }
55
55
  });
56
56
 
57
- function pDistance(point, line) {
57
+ const pDistance = (point, line) => {
58
58
  'worklet';
59
59
 
60
60
  const {
@@ -96,17 +96,17 @@ function pDistance(point, line) {
96
96
  var dx = x - xx;
97
97
  var dy = y - yy;
98
98
  return Math.sqrt(dx * dx + dy * dy);
99
- }
99
+ };
100
100
 
101
101
  const AnimatedTextInput = Animated.createAnimatedComponent(TextInput);
102
102
  const DEFAULT_TEXT = '';
103
103
  const THRESHOLD = 20;
104
104
 
105
- const drawNewItem = (mode, currentItem, updateDoneItems, position, style) => {
105
+ const drawNewItem = (mode, currentItem, addDoneItem, position, style) => {
106
106
  'worklet';
107
107
 
108
108
  if (currentItem.value) {
109
- runOnJS(updateDoneItems)(currentItem.value);
109
+ runOnJS(addDoneItem)(currentItem.value);
110
110
  }
111
111
 
112
112
  switch (mode.value) {
@@ -198,11 +198,63 @@ const onTextHeightUpdate = (currentItem, textBaseHeight, height) => {
198
198
  }
199
199
  };
200
200
 
201
+ const reducerDrawStates = (drawStates, action) => {
202
+ 'worklet';
203
+
204
+ switch (action.type) {
205
+ case 'ADD_DONE_ITEM':
206
+ return { ...drawStates,
207
+ doneItems: drawStates.doneItems.concat(action.item)
208
+ };
209
+
210
+ case 'DELETE_DONE_ITEM':
211
+ const newDoneItems = drawStates.doneItems;
212
+ newDoneItems.splice(action.indice, 1);
213
+ return { ...drawStates,
214
+ doneItems: newDoneItems
215
+ };
216
+
217
+ case 'ADD_SCREEN_STATE':
218
+ if (action.currentItem) {
219
+ return { ...drawStates,
220
+ screenStates: drawStates.screenStates.concat([[...drawStates.doneItems, action.currentItem]])
221
+ };
222
+ } else {
223
+ return { ...drawStates,
224
+ screenStates: drawStates.screenStates.concat([[...drawStates.doneItems]])
225
+ };
226
+ }
227
+
228
+ case 'CANCEL':
229
+ const len = drawStates.screenStates.length;
230
+
231
+ if (len > 1) {
232
+ const newScreenStates = drawStates.screenStates;
233
+ newScreenStates.pop();
234
+
235
+ if (newScreenStates.length === 1) {
236
+ var _action$onCancelChang;
237
+
238
+ (_action$onCancelChang = action.onCancelChange) === null || _action$onCancelChang === void 0 ? void 0 : _action$onCancelChang.call(action, false);
239
+ }
240
+
241
+ return {
242
+ doneItems: drawStates.screenStates[len - 2],
243
+ screenStates: newScreenStates
244
+ };
245
+ } else {
246
+ return drawStates;
247
+ }
248
+
249
+ }
250
+ };
251
+
201
252
  const DrawCore = /*#__PURE__*/React.forwardRef(({
202
253
  drawingMode,
203
254
  image,
204
255
  linearGradient,
205
- onSelectionChange
256
+ onSelectionChange,
257
+ onCancelChange
206
258
  }, ref) => {
207
259
  var _textInputRef$current3, _textInputRef$current4;
208
260
 
@@ -215,42 +267,76 @@ const DrawCore = /*#__PURE__*/React.forwardRef(({
215
267
  const [textVal, setTextVal] = useState('');
216
268
  const currentItem = useSharedValue(null);
217
269
  const initialItem = useSharedValue(null);
218
- const [doneItems, setDoneItems] = useState([]);
270
+ const [drawStates, dispatchDrawStates] = useReducer(reducerDrawStates, {
271
+ doneItems: [],
272
+ screenStates: [[]]
273
+ });
219
274
  const textBaseHeight = useSharedValue(null);
220
- const updateDoneItems = useCallback(done => {
221
- setDoneItems(previous => previous.concat(done));
275
+ const addDoneItem = useCallback(item => {
276
+ dispatchDrawStates({
277
+ type: 'ADD_DONE_ITEM',
278
+ item: item
279
+ });
280
+ }, []);
281
+ const deleteDoneItem = useCallback(indice => {
282
+ dispatchDrawStates({
283
+ type: 'DELETE_DONE_ITEM',
284
+ indice: indice
285
+ });
222
286
  }, []);
287
+ const addScreenStates = useCallback(item => {
288
+ dispatchDrawStates({
289
+ type: 'ADD_SCREEN_STATE',
290
+ currentItem: item
291
+ });
292
+ }, []);
293
+ const cancelAction = useCallback(() => {
294
+ dispatchDrawStates({
295
+ type: 'CANCEL',
296
+ onCancelChange: onCancelChange
297
+ });
298
+ }, [onCancelChange]);
223
299
  useImperativeHandle(ref, () => ({
224
300
  drawingContainer: drawContainer,
225
301
  deleteSelectedItem: () => {
226
302
  if (currentItem.value) {
227
303
  currentItem.value = null;
304
+ addScreenStates(null);
228
305
  }
229
306
 
230
307
  onSelectionChange === null || onSelectionChange === void 0 ? void 0 : onSelectionChange(false);
308
+ onCancelChange === null || onCancelChange === void 0 ? void 0 : onCancelChange(true);
309
+ },
310
+ cancelLastAction: () => {
311
+ onSelectionChange === null || onSelectionChange === void 0 ? void 0 : onSelectionChange(false);
312
+
313
+ if (currentItem.value) {
314
+ currentItem.value = null;
315
+ }
316
+
317
+ cancelAction();
231
318
  },
232
319
  takeSnapshot: async () => {
233
320
  var _viewShot$current, _viewShot$current$cap;
234
321
 
235
322
  if (currentItem.value) {
236
- updateDoneItems(currentItem.value);
323
+ addDoneItem(currentItem.value);
237
324
  currentItem.value = null;
238
325
  }
239
326
 
240
327
  return (_viewShot$current = viewShot.current) === null || _viewShot$current === void 0 ? void 0 : (_viewShot$current$cap = _viewShot$current.capture) === null || _viewShot$current$cap === void 0 ? void 0 : _viewShot$current$cap.call(_viewShot$current);
241
328
  }
242
- }), [currentItem, onSelectionChange, updateDoneItems]);
329
+ }), [currentItem, onSelectionChange, onCancelChange, addScreenStates, cancelAction, addDoneItem]);
243
330
  useEffect(() => {
244
331
  mode.value = drawingMode;
245
332
 
246
333
  if (currentItem.value) {
247
- updateDoneItems(currentItem.value);
334
+ addDoneItem(currentItem.value);
248
335
  }
249
336
 
250
337
  currentItem.value = null;
251
338
  onSelectionChange === null || onSelectionChange === void 0 ? void 0 : onSelectionChange(false);
252
- }, [drawingMode, mode, currentItem, updateDoneItems, onSelectionChange]);
253
- const addNewItem = runOnJS(updateDoneItems);
339
+ }, [drawingMode, mode, currentItem, onSelectionChange, addDoneItem]);
254
340
  const strokeWidth = useSharedValue(2);
255
341
  const color = useSharedValue('hsl(0, 100%, 0%)');
256
342
  const panPosition = useSharedValue(0);
@@ -260,6 +346,11 @@ const DrawCore = /*#__PURE__*/React.forwardRef(({
260
346
 
261
347
  (_textInputRef$current = textInputRef.current) === null || _textInputRef$current === void 0 ? void 0 : _textInputRef$current.focus();
262
348
  }, []);
349
+ const onColorStrokeChange = useCallback(() => {
350
+ if (currentItem.value) {
351
+ addScreenStates(currentItem.value);
352
+ }
353
+ }, [addScreenStates, currentItem.value]);
263
354
  useEffect(() => {
264
355
  var _currentItem$value2;
265
356
 
@@ -275,13 +366,11 @@ const DrawCore = /*#__PURE__*/React.forwardRef(({
275
366
  }, [currentItem, textVal]);
276
367
  const onGestureEvent = useAnimatedGestureHandler({
277
368
  onStart: ({
278
- x,
279
- y
369
+ x: startX,
370
+ y: startY
280
371
  }, ctx) => {
281
372
  var _currentItem$value3;
282
373
 
283
- const startX = x;
284
- const startY = y;
285
374
  ctx.startX = startX;
286
375
  ctx.startY = startY;
287
376
  ctx.newlyCreated = false;
@@ -403,8 +492,8 @@ const DrawCore = /*#__PURE__*/React.forwardRef(({
403
492
  }
404
493
  },
405
494
  onActive: ({
406
- x,
407
- y,
495
+ x: currentX,
496
+ y: currentY,
408
497
  translationX,
409
498
  translationY
410
499
  }, ctx) => {
@@ -424,7 +513,7 @@ const DrawCore = /*#__PURE__*/React.forwardRef(({
424
513
  runOnJS(setTextVal)('');
425
514
  }
426
515
 
427
- drawNewItem(mode, currentItem, addNewItem, {
516
+ drawNewItem(mode, currentItem, addDoneItem, {
428
517
  x: startX,
429
518
  y: startY
430
519
  }, {
@@ -433,6 +522,7 @@ const DrawCore = /*#__PURE__*/React.forwardRef(({
433
522
  color
434
523
  });
435
524
  onSelectionChange && runOnJS(onSelectionChange)(true);
525
+ onCancelChange && runOnJS(onCancelChange)(true);
436
526
  }
437
527
 
438
528
  switch ((_currentItem$value4 = currentItem.value) === null || _currentItem$value4 === void 0 ? void 0 : _currentItem$value4.type) {
@@ -453,8 +543,8 @@ const DrawCore = /*#__PURE__*/React.forwardRef(({
453
543
  strokeWidth: currentItem.value.strokeWidth,
454
544
  color: currentItem.value.color,
455
545
  data: currentItem.value.data.concat({
456
- x: x,
457
- y: y
546
+ x: currentX,
547
+ y: currentY
458
548
  })
459
549
  };
460
550
  }
@@ -811,6 +901,8 @@ const DrawCore = /*#__PURE__*/React.forwardRef(({
811
901
  text: currentItem.value.text !== DEFAULT_TEXT ? (_currentItem$value$te = currentItem.value.text) !== null && _currentItem$value$te !== void 0 ? _currentItem$value$te : '' : ''
812
902
  };
813
903
  }
904
+
905
+ runOnJS(addScreenStates)(currentItem.value);
814
906
  }
815
907
  }, []);
816
908
  const rightPaneStyle = useAnimatedStyle(() => {
@@ -819,7 +911,7 @@ const DrawCore = /*#__PURE__*/React.forwardRef(({
819
911
  translateX: panPosition.value
820
912
  }]
821
913
  };
822
- }, [panPosition.value]);
914
+ });
823
915
  useEffect(() => {
824
916
  const sudDidHide = Keyboard.addListener('keyboardDidHide', () => {
825
917
  showTextInput.value = false;
@@ -857,8 +949,8 @@ const DrawCore = /*#__PURE__*/React.forwardRef(({
857
949
  color: color.value
858
950
  };
859
951
  }, ({
860
- strokeWidth,
861
- color
952
+ strokeWidth: sw,
953
+ color: c
862
954
  }) => {
863
955
  var _currentItem$value6;
864
956
 
@@ -867,8 +959,8 @@ const DrawCore = /*#__PURE__*/React.forwardRef(({
867
959
  currentItem.value = {
868
960
  type: currentItem.value.type,
869
961
  data: currentItem.value.data,
870
- strokeWidth,
871
- color
962
+ strokeWidth: sw,
963
+ color: c
872
964
  };
873
965
  break;
874
966
 
@@ -876,8 +968,8 @@ const DrawCore = /*#__PURE__*/React.forwardRef(({
876
968
  currentItem.value = {
877
969
  type: currentItem.value.type,
878
970
  data: currentItem.value.data,
879
- strokeWidth,
880
- color
971
+ strokeWidth: sw,
972
+ color: c
881
973
  };
882
974
  break;
883
975
 
@@ -885,8 +977,8 @@ const DrawCore = /*#__PURE__*/React.forwardRef(({
885
977
  currentItem.value = {
886
978
  type: currentItem.value.type,
887
979
  data: currentItem.value.data,
888
- strokeWidth,
889
- color
980
+ strokeWidth: sw,
981
+ color: c
890
982
  };
891
983
  break;
892
984
 
@@ -894,8 +986,8 @@ const DrawCore = /*#__PURE__*/React.forwardRef(({
894
986
  currentItem.value = {
895
987
  type: currentItem.value.type,
896
988
  data: currentItem.value.data,
897
- strokeWidth,
898
- color
989
+ strokeWidth: sw,
990
+ color: c
899
991
  };
900
992
  break;
901
993
 
@@ -903,8 +995,8 @@ const DrawCore = /*#__PURE__*/React.forwardRef(({
903
995
  currentItem.value = {
904
996
  type: currentItem.value.type,
905
997
  data: currentItem.value.data,
906
- strokeWidth,
907
- color
998
+ strokeWidth: sw,
999
+ color: c
908
1000
  };
909
1001
  break;
910
1002
 
@@ -912,8 +1004,8 @@ const DrawCore = /*#__PURE__*/React.forwardRef(({
912
1004
  currentItem.value = {
913
1005
  type: currentItem.value.type,
914
1006
  data: currentItem.value.data,
915
- strokeWidth,
916
- color,
1007
+ strokeWidth: sw,
1008
+ color: c,
917
1009
  text: currentItem.value.text
918
1010
  };
919
1011
  break;
@@ -925,14 +1017,10 @@ const DrawCore = /*#__PURE__*/React.forwardRef(({
925
1017
  strokeWidth.value = item.strokeWidth;
926
1018
  color.value = item.color;
927
1019
  currentItem.value = item;
928
- setDoneItems(done => {
929
- const copy = [...done];
930
- copy.splice(index, 1);
931
- return copy;
932
- });
1020
+ deleteDoneItem(index);
933
1021
 
934
1022
  if (previousItem) {
935
- updateDoneItems(previousItem);
1023
+ addDoneItem(previousItem);
936
1024
  }
937
1025
 
938
1026
  if (item.type === 'text') {
@@ -944,7 +1032,7 @@ const DrawCore = /*#__PURE__*/React.forwardRef(({
944
1032
 
945
1033
  (_textInputRef$current2 = textInputRef.current) === null || _textInputRef$current2 === void 0 ? void 0 : _textInputRef$current2.blur();
946
1034
  }
947
- }, [color, currentItem, strokeWidth, updateDoneItems, onSelectionChange]);
1035
+ }, [onSelectionChange, currentItem, strokeWidth, color, deleteDoneItem, addDoneItem]);
948
1036
  const onTextHeightChange = useCallback(height => {
949
1037
  onTextHeightUpdate(currentItem, textBaseHeight, height);
950
1038
  }, [currentItem, textBaseHeight]);
@@ -992,8 +1080,6 @@ const DrawCore = /*#__PURE__*/React.forwardRef(({
992
1080
  });
993
1081
  }
994
1082
  }, /*#__PURE__*/React.createElement(PanGestureHandler, {
995
- activeOffsetX: 2,
996
- activeOffsetY: 2,
997
1083
  onGestureEvent: onGestureEvent
998
1084
  }, /*#__PURE__*/React.createElement(Animated.View, {
999
1085
  style: imageSize || drawRegion
@@ -1003,8 +1089,7 @@ const DrawCore = /*#__PURE__*/React.forwardRef(({
1003
1089
  ref: viewShot,
1004
1090
  options: {
1005
1091
  format: 'jpg',
1006
- quality: 1,
1007
- ...originalImageSize
1092
+ quality: 1
1008
1093
  },
1009
1094
  style: imageSize
1010
1095
  }, /*#__PURE__*/React.createElement(ImageBackground, {
@@ -1012,7 +1097,7 @@ const DrawCore = /*#__PURE__*/React.forwardRef(({
1012
1097
  style: styles.bgImage
1013
1098
  }, /*#__PURE__*/React.createElement(DrawPad, {
1014
1099
  currentItem: currentItem,
1015
- doneItems: doneItems,
1100
+ doneItems: drawStates.doneItems,
1016
1101
  onPressItem: onPressItem,
1017
1102
  onTextHeightChange: onTextHeightChange
1018
1103
  }))) : null : drawRegion ? /*#__PURE__*/React.createElement(ViewShot, {
@@ -1025,7 +1110,7 @@ const DrawCore = /*#__PURE__*/React.forwardRef(({
1025
1110
  style: drawRegion
1026
1111
  }, /*#__PURE__*/React.createElement(DrawPad, {
1027
1112
  currentItem: currentItem,
1028
- doneItems: doneItems,
1113
+ doneItems: drawStates.doneItems,
1029
1114
  onPressItem: onPressItem,
1030
1115
  onTextHeightChange: onTextHeightChange
1031
1116
  })) : null))), /*#__PURE__*/React.createElement(Animated.View, {
@@ -1035,12 +1120,14 @@ const DrawCore = /*#__PURE__*/React.forwardRef(({
1035
1120
  }, /*#__PURE__*/React.createElement(StrokeSlider, {
1036
1121
  minValue: 2,
1037
1122
  maxValue: 10,
1038
- stroke: strokeWidth
1123
+ stroke: strokeWidth,
1124
+ onStrokeChange: onColorStrokeChange
1039
1125
  })), /*#__PURE__*/React.createElement(View, {
1040
1126
  style: styles.colorSliderContainer
1041
1127
  }, /*#__PURE__*/React.createElement(ColorSlider, {
1042
1128
  color: color,
1043
- linearGradient: linearGradient
1129
+ linearGradient: linearGradient,
1130
+ onColorChange: onColorStrokeChange
1044
1131
  })))), Platform.OS === 'ios' ? /*#__PURE__*/React.createElement(InputAccessoryView, null, /*#__PURE__*/React.createElement(AnimatedTextInput, {
1045
1132
  ref: textInputRef,
1046
1133
  style: [styles.textInput, textInputStyle],