@archireport/react-native-svg-draw 2.3.2 → 3.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 (44) 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/index.js +65 -71
  9. package/lib/commonjs/components/DrawCore/index.js.map +1 -1
  10. package/lib/commonjs/components/DrawCore/useDrawHook.js +15 -13
  11. package/lib/commonjs/components/DrawCore/useDrawHook.js.map +1 -1
  12. package/lib/commonjs/components/DrawWithOptions/index.js +7 -11
  13. package/lib/commonjs/components/DrawWithOptions/index.js.map +1 -1
  14. package/lib/module/components/DrawCore/DrawContext.js +4 -1
  15. package/lib/module/components/DrawCore/DrawContext.js.map +1 -1
  16. package/lib/module/components/DrawCore/DrawPad.js +10 -4
  17. package/lib/module/components/DrawCore/DrawPad.js.map +1 -1
  18. package/lib/module/components/DrawCore/DrawProvider.js +17 -3
  19. package/lib/module/components/DrawCore/DrawProvider.js.map +1 -1
  20. package/lib/module/components/DrawCore/index.js +65 -71
  21. package/lib/module/components/DrawCore/index.js.map +1 -1
  22. package/lib/module/components/DrawCore/useDrawHook.js +15 -13
  23. package/lib/module/components/DrawCore/useDrawHook.js.map +1 -1
  24. package/lib/module/components/DrawWithOptions/index.js +8 -12
  25. package/lib/module/components/DrawWithOptions/index.js.map +1 -1
  26. package/lib/typescript/components/DrawCore/DrawContext.d.ts +3 -0
  27. package/lib/typescript/components/DrawCore/DrawContext.d.ts.map +1 -1
  28. package/lib/typescript/components/DrawCore/DrawPad.d.ts +2 -1
  29. package/lib/typescript/components/DrawCore/DrawPad.d.ts.map +1 -1
  30. package/lib/typescript/components/DrawCore/DrawProvider.d.ts.map +1 -1
  31. package/lib/typescript/components/DrawCore/index.d.ts +2 -1
  32. package/lib/typescript/components/DrawCore/index.d.ts.map +1 -1
  33. package/lib/typescript/components/DrawCore/useDrawHook.d.ts +4 -1
  34. package/lib/typescript/components/DrawCore/useDrawHook.d.ts.map +1 -1
  35. package/lib/typescript/components/DrawWithOptions/index.d.ts +1 -1
  36. package/lib/typescript/components/DrawWithOptions/index.d.ts.map +1 -1
  37. package/package.json +1 -1
  38. package/src/components/DrawCore/DrawContext.tsx +6 -0
  39. package/src/components/DrawCore/DrawPad.tsx +76 -67
  40. package/src/components/DrawCore/DrawProvider.tsx +23 -1
  41. package/src/components/DrawCore/index.tsx +63 -63
  42. package/src/components/DrawCore/useDrawHook.tsx +12 -7
  43. package/src/components/DrawWithOptions/index.tsx +10 -11
  44. package/src/types.d.ts +5 -3
@@ -226,9 +226,11 @@ const onTextHeightUpdate = (
226
226
  const DrawCore = ({
227
227
  image,
228
228
  backgroundColor,
229
+ actionWithSnapShotUri,
229
230
  }: {
230
231
  image?: ImageRequireSource | ImageURISource;
231
232
  backgroundColor?: string;
233
+ actionWithSnapShotUri?: (uri: string) => Promise<void>;
232
234
  }) => {
233
235
  const {
234
236
  drawState,
@@ -239,26 +241,53 @@ const DrawCore = ({
239
241
  itemIsSelected,
240
242
  viewShot,
241
243
  doubleArrowTextInput,
244
+ captureSnapshot,
245
+ snapShotRequested,
246
+ setSnapShotRequested,
242
247
  } = useDrawHook();
243
248
 
249
+ const [drawRegion, setDrawRegion] = useState<Size | null>(null);
250
+ const [imageSize, setImageSize] = useState<Size | null>(null);
251
+ const [originalImageSize, setOriginalImageSize] = useState<Size | null>(null);
252
+
253
+ useEffect(() => {
254
+ if (snapShotRequested) {
255
+ setSnapShotRequested(false);
256
+ setNewLayoutRequested(true);
257
+ if (
258
+ imageSize?.height &&
259
+ originalImageSize?.height &&
260
+ originalImageSize?.width &&
261
+ image
262
+ ) {
263
+ setOpacity(0);
264
+ setRatioImage(originalImageSize.height / imageSize?.height);
265
+ setImageSize(originalImageSize);
266
+ } else if (!image && drawRegion?.height) {
267
+ const width = 1500;
268
+ const height = width / 1.3333333;
269
+ setOpacity(0);
270
+ setRatioImage(height / drawRegion.height);
271
+ setImageSize({ width, height });
272
+ }
273
+ }
274
+ }, [
275
+ snapShotRequested,
276
+ drawRegion?.height,
277
+ image,
278
+ imageSize?.height,
279
+ originalImageSize,
280
+ setSnapShotRequested,
281
+ ]);
282
+
244
283
  const onCancelChangeWrapper = (arg: boolean) => {
245
284
  dispatchDrawStates({ type: 'SET_CANCEL_ENABLED', cancelEnabled: arg });
246
285
  };
247
286
 
248
287
  const mode = useSharedValue<DrawItemType>('pen');
249
-
250
- const [drawRegion, setDrawRegion] = useState<Size | null>(null);
251
-
252
- const [originalImageSize, setOriginalImageSize] = useState<Size | null>(null);
253
-
254
- const [imageSize, setImageSize] = useState<Size | null>(null);
255
-
256
288
  const drawContainer = useRef<View>(null);
257
-
258
289
  const [textVal, setTextVal] = useState<string>('');
259
-
260
290
  const initialItem = useSharedValue<DrawItem | null>(null);
261
-
262
291
  const textBaseHeight = useSharedValue<number | null>(null);
263
292
 
264
293
  const addDoneItem = useCallback(
@@ -297,19 +326,15 @@ const DrawCore = ({
297
326
  const showTextInput = useSharedValue(false); //TODO: remove
298
327
 
299
328
  const textFocusState = useCallback(() => {
300
- //setShowTextInputState(true);
301
- console.log('textFocusState');
302
329
  doubleArrowTextInput?.current?.focus();
303
330
  }, [doubleArrowTextInput]);
304
331
 
305
332
  const textFocus = useCallback(() => {
306
- console.log('textFocus');
307
333
  textInputRef.current?.focus();
308
334
  }, []);
309
335
 
310
336
  useEffect(() => {
311
337
  if (currentItem.value?.type === 'text') {
312
- console.log('use effect text');
313
338
  showTextInput.value = true;
314
339
  textFocus();
315
340
  currentItem.value = {
@@ -330,10 +355,7 @@ const DrawCore = ({
330
355
  ctx.startX = startX;
331
356
  ctx.startY = startY;
332
357
  ctx.newlyCreated = false;
333
- console.log('**********************************');
334
- console.log('onGestureEvent');
335
- //panPosition.value = withTiming(RIGHT_PANE_WIDTH);
336
- console.log('onStart', currentItem.value?.type);
358
+
337
359
  initialItem.value = currentItem.value;
338
360
  switch (currentItem.value?.type) {
339
361
  case 'ellipse':
@@ -511,8 +533,7 @@ const DrawCore = ({
511
533
  typeof currentItem.value.data.height === 'string'
512
534
  ? parseFloat(currentItem.value.data.height)
513
535
  : currentItem.value.data.height || 0;
514
- console.log(heightText);
515
- console.log(widthText);
536
+
516
537
  if (
517
538
  startX <= xText + THRESHOLD &&
518
539
  startX >= xText - THRESHOLD &&
@@ -538,10 +559,8 @@ const DrawCore = ({
538
559
  (heightText < 0 && startY < yText && startY > yText + heightText))
539
560
  ) {
540
561
  ctx.zone = 'CENTER';
541
- console.log('on active center');
542
562
  } else {
543
563
  ctx.zone = 'OUT';
544
- console.log('on active out');
545
564
  initialItem.value = null;
546
565
 
547
566
  ctx.newlyCreated = true;
@@ -581,8 +600,6 @@ const DrawCore = ({
581
600
  ctx.zone = 'OUT';
582
601
  initialItem.value = null;
583
602
  if (drawState.drawingMode === 'text') {
584
- /* NEW GEOFF */
585
- console.log('on active out');
586
603
  ctx.newlyCreated = true;
587
604
 
588
605
  runOnJS(setTextVal)('');
@@ -609,7 +626,6 @@ const DrawCore = ({
609
626
  ) => {
610
627
  const { startX, startY, zone, newlyCreated } = ctx;
611
628
  if (zone === 'OUT' && newlyCreated === false && mode.value !== 'text') {
612
- console.log('on active out');
613
629
  ctx.newlyCreated = true;
614
630
  /*
615
631
  if (mode.value === 'text') {
@@ -956,7 +972,6 @@ const DrawCore = ({
956
972
  }
957
973
  break;
958
974
  case 'text':
959
- console.log('on active text');
960
975
  if (initialItem.value?.type === currentItem.value.type) {
961
976
  const xText =
962
977
  typeof initialItem.value?.data.x === 'string'
@@ -1064,7 +1079,6 @@ const DrawCore = ({
1064
1079
 
1065
1080
  const sudDidShow = Keyboard.addListener('keyboardDidShow', (event) => {
1066
1081
  // avoid events triggered by InputAccessoryView
1067
- console.log('keyboardDidShow dc');
1068
1082
  if (event.endCoordinates.height > 100) {
1069
1083
  showTextInput.value = true;
1070
1084
  }
@@ -1123,14 +1137,11 @@ const DrawCore = ({
1123
1137
 
1124
1138
  const onPressItem = useCallback(
1125
1139
  (item: DrawItem, index: number) => () => {
1126
- console.log('onPressItem');
1127
- itemIsSelected.value = true;
1128
-
1129
1140
  const previousItem = currentItem.value;
1130
1141
 
1142
+ itemIsSelected.value = true;
1131
1143
  strokeWidth.value = item.strokeWidth;
1132
1144
  color.value = item.color;
1133
- console.log('item', item);
1134
1145
  currentItem.value = item;
1135
1146
 
1136
1147
  deleteDoneItem(index);
@@ -1201,9 +1212,8 @@ const DrawCore = ({
1201
1212
 
1202
1213
  const calculateSizes = useCallback(
1203
1214
  (imageWidth: number, imageHeight: number) => {
1215
+ setOriginalImageSize({ width: imageWidth, height: imageHeight });
1204
1216
  if (drawRegion) {
1205
- setOriginalImageSize({ width: imageWidth, height: imageHeight });
1206
-
1207
1217
  const ratioImageHeight =
1208
1218
  Math.round(((imageHeight * drawRegion.width) / imageWidth) * 100) /
1209
1219
  100;
@@ -1244,36 +1254,21 @@ const DrawCore = ({
1244
1254
  // do not remove keyboard will appear over the drawing frame and not shift it
1245
1255
  useAnimatedKeyboard();
1246
1256
 
1247
- /*
1248
- const onEndEditingTextInput = useCallback(() => {
1249
- console.log('onEndEditingTextInput');
1250
- setShowTextInputState(false);
1251
- if (currentItem.value && currentItem.value.type === 'doubleArrows') {
1252
- console.log(currentItem.value.text);
1253
- addScreenStates(currentItem.value);
1257
+ const [newLayoutRequested, setNewLayoutRequested] = useState(false);
1258
+
1259
+ const onLayout = useCallback(async () => {
1260
+ if (newLayoutRequested) {
1261
+ setNewLayoutRequested(false);
1262
+ const uri = await captureSnapshot();
1263
+ if (uri && typeof actionWithSnapShotUri === 'function') {
1264
+ await actionWithSnapShotUri(uri);
1265
+ }
1254
1266
  }
1255
- }, [currentItem, addScreenStates]);
1267
+ }, [actionWithSnapShotUri, newLayoutRequested, captureSnapshot]);
1256
1268
 
1257
- const onChangeText = useCallback(
1258
- (value: string) => {
1259
- if (
1260
- value &&
1261
- currentItem.value &&
1262
- currentItem.value.type === 'doubleArrows'
1263
- ) {
1264
- console.log('******************');
1265
- console.log(value);
1266
- console.log(currentItem.value);
1269
+ const [ratioImage, setRatioImage] = useState<number>(1);
1270
+ const [opacity, setOpacity] = useState<number>(1);
1267
1271
 
1268
- currentItem.value = {
1269
- ...currentItem.value,
1270
- text: value,
1271
- };
1272
- }
1273
- },
1274
- [currentItem]
1275
- );
1276
- */
1277
1272
  return (
1278
1273
  <View style={styles.container}>
1279
1274
  <View
@@ -1290,7 +1285,10 @@ const DrawCore = ({
1290
1285
  >
1291
1286
  <KeyboardAvoidingView behavior="position" keyboardVerticalOffset={70}>
1292
1287
  <PanGestureHandler onGestureEvent={onGestureEvent}>
1293
- <Animated.View style={imageSize || drawRegion}>
1288
+ <Animated.View
1289
+ style={{ ...(imageSize || drawRegion), opacity: opacity }}
1290
+ onLayout={onLayout}
1291
+ >
1294
1292
  <View ref={drawContainer}>
1295
1293
  {image ? (
1296
1294
  imageSize && originalImageSize ? (
@@ -1308,6 +1306,7 @@ const DrawCore = ({
1308
1306
  doneItems={drawState.doneItems}
1309
1307
  onPressItem={onPressItem}
1310
1308
  onTextHeightChange={onTextHeightChange}
1309
+ ratioImage={ratioImage}
1311
1310
  />
1312
1311
  </ImageBackground>
1313
1312
  </ViewShot>
@@ -1318,9 +1317,9 @@ const DrawCore = ({
1318
1317
  options={{
1319
1318
  format: 'jpg',
1320
1319
  quality: 1,
1321
- ...drawRegion,
1320
+ ...(imageSize || drawRegion),
1322
1321
  }}
1323
- style={drawRegion}
1322
+ style={imageSize || drawRegion}
1324
1323
  >
1325
1324
  <DrawPad
1326
1325
  addBackground
@@ -1328,6 +1327,7 @@ const DrawCore = ({
1328
1327
  doneItems={drawState.doneItems}
1329
1328
  onPressItem={onPressItem}
1330
1329
  onTextHeightChange={onTextHeightChange}
1330
+ ratioImage={ratioImage}
1331
1331
  />
1332
1332
  </ViewShot>
1333
1333
  ) : null}
@@ -11,15 +11,17 @@ const useDrawHook = () => {
11
11
  itemIsSelected,
12
12
  viewShot,
13
13
  doubleArrowTextInput,
14
+ doSnapshot,
15
+ snapShotRequested,
16
+ setSnapShotRequested,
14
17
  } = useContext(DrawContext);
15
18
 
16
- const takeSnapshot = useCallback(async () => {
17
- if (currentItem?.value) {
18
- dispatchDrawStates({ type: 'ADD_DONE_ITEM', item: currentItem.value });
19
- currentItem.value = null;
19
+ const captureSnapshot = useCallback(async () => {
20
+ if (viewShot) {
21
+ return await viewShot.current?.capture?.();
20
22
  }
21
- return viewShot!.current?.capture?.();
22
- }, [currentItem, dispatchDrawStates, viewShot]);
23
+ return null;
24
+ }, [viewShot]);
23
25
 
24
26
  const cancelLastAction = useCallback(() => {
25
27
  itemIsSelected!.value = false;
@@ -62,10 +64,13 @@ const useDrawHook = () => {
62
64
  onColorStrokeChange,
63
65
  itemIsSelected: itemIsSelected!,
64
66
  cancelLastAction,
65
- takeSnapshot: takeSnapshot!,
67
+ captureSnapshot: captureSnapshot!,
66
68
  viewShot: viewShot!,
67
69
  deleteSelectedItem,
68
70
  doubleArrowTextInput,
71
+ snapShotRequested,
72
+ doSnapshot,
73
+ setSnapShotRequested,
69
74
  };
70
75
  };
71
76
 
@@ -1,4 +1,4 @@
1
- import React, { useCallback, useEffect, useState } from 'react';
1
+ import React, { useEffect, useState } from 'react';
2
2
  import {
3
3
  Pressable,
4
4
  View,
@@ -76,7 +76,7 @@ type DrawWithOptionsProps = {
76
76
  linearGradient: React.ComponentType<{ colors: any[] } & ViewProps>;
77
77
  image?: ImageRequireSource | ImageURISource;
78
78
  close?: () => void;
79
- takeSnapshot?: (snap: Promise<string | undefined>) => void;
79
+ actionWithSnapShotUri?: (uri: string) => Promise<void>;
80
80
  backgroundColor?: string;
81
81
  };
82
82
 
@@ -84,16 +84,16 @@ function DrawWithOptionsCore({
84
84
  linearGradient,
85
85
  image,
86
86
  close,
87
- takeSnapshot,
88
87
  backgroundColor,
88
+ actionWithSnapShotUri,
89
89
  }: DrawWithOptionsProps) {
90
90
  const {
91
91
  itemIsSelected,
92
92
  cancelLastAction,
93
- takeSnapshot: takeSnapshotAction,
94
93
  deleteSelectedItem,
95
94
  dispatchDrawStates,
96
95
  drawState,
96
+ doSnapshot,
97
97
  } = useDrawHook();
98
98
 
99
99
  const [showToolbar, setShowToolbar] = useState(true);
@@ -121,11 +121,6 @@ function DrawWithOptionsCore({
121
121
  };
122
122
  }, []);
123
123
 
124
- const takeSnapshotAndGetUri = useCallback(async () => {
125
- if (takeSnapshot) {
126
- takeSnapshot(takeSnapshotAction());
127
- }
128
- }, [takeSnapshot, takeSnapshotAction]);
129
124
  return (
130
125
  <View style={styles.container}>
131
126
  <View style={styles.toolbar}>
@@ -266,7 +261,7 @@ function DrawWithOptionsCore({
266
261
  </View>
267
262
 
268
263
  <View style={styles.actionButton}>
269
- <Pressable onPress={takeSnapshotAndGetUri}>
264
+ <Pressable onPress={doSnapshot}>
270
265
  <SendSvg height={20} width={20} fill="#ffffff" />
271
266
  </Pressable>
272
267
  </View>
@@ -277,7 +272,11 @@ function DrawWithOptionsCore({
277
272
  flex: 1,
278
273
  }}
279
274
  >
280
- <DrawCore image={image} backgroundColor={backgroundColor} />
275
+ <DrawCore
276
+ image={image}
277
+ backgroundColor={backgroundColor}
278
+ actionWithSnapShotUri={actionWithSnapShotUri}
279
+ />
281
280
  </View>
282
281
 
283
282
  <Sliders linearGradient={linearGradient} />
package/src/types.d.ts CHANGED
@@ -1,5 +1,3 @@
1
- import React from 'react';
2
- import { View } from 'react-native';
3
1
  import {
4
2
  ForeignObjectProps,
5
3
  EllipseProps,
@@ -33,12 +31,16 @@ export type DrawItem = (
33
31
 
34
32
  export type DrawItemType = DrawItem['type'];
35
33
 
34
+ export type DrawCoreProps = {
35
+ takeSnapshotAction: () => void;
36
+ };
37
+ /*
36
38
  export type DrawCoreProps = {
37
39
  drawingContainer: React.Ref<View>;
38
40
  deleteSelectedItem: () => void;
39
41
  cancelLastAction: () => void;
40
42
  takeSnapshot: () => Promise<string | undefined>;
41
- };
43
+ };*/
42
44
 
43
45
  export type DrawState = {
44
46
  doneItems: DrawItem[];