Ver código fonte

fix(paste): do not warn in production (#409)

- when paste the content from clipboard, if the content is not JSON, the `JSON.parse` parse error will be thrown. The info should not be in production build.

Closes #400
main
Yao Wang 3 anos atrás
pai
commit
2c28012839
Nenhuma conta vinculada ao e-mail do autor do commit

+ 15
- 2
packages/tldraw/src/state/TLDR.ts Ver arquivo

@@ -15,6 +15,7 @@ import { Vec } from '@tldraw/vec'
15 15
 import type { TDShapeUtil } from './shapes/TDShapeUtil'
16 16
 import { getShapeUtil } from './shapes'
17 17
 
18
+const isDev = process.env.NODE_ENV === 'development'
18 19
 export class TLDR {
19 20
   // eslint-disable-next-line @typescript-eslint/no-explicit-any
20 21
   static getShapeUtil<T extends TDShape>(type: T['type']): TDShapeUtil<T>
@@ -865,8 +866,8 @@ export class TLDR {
865 866
     return shapes.length === 0
866 867
       ? 1
867 868
       : shapes
868
-          .filter((shape) => shape.parentId === pageId)
869
-          .sort((a, b) => b.childIndex - a.childIndex)[0].childIndex + 1
869
+        .filter((shape) => shape.parentId === pageId)
870
+        .sort((a, b) => b.childIndex - a.childIndex)[0].childIndex + 1
870 871
   }
871 872
 
872 873
   /* -------------------------------------------------- */
@@ -891,4 +892,16 @@ export class TLDR {
891 892
       throw new Error()
892 893
     }
893 894
   }
895
+
896
+  static warn(e: any) {
897
+    if (isDev) {
898
+      console.warn(e);
899
+    }
900
+  }
901
+  static error(e: any) {
902
+    if (isDev) {
903
+      console.error(e)
904
+    }
905
+  }
906
+
894 907
 }

+ 4
- 4
packages/tldraw/src/state/TldrawApp.ts Ver arquivo

@@ -391,12 +391,12 @@ export class TldrawApp extends StateManager<TDSnapshot> {
391 391
         }
392 392
 
393 393
         if (nextPageState.bindingId && !page.bindings[nextPageState.bindingId]) {
394
-          console.warn('Could not find the binding binding!', pageId)
394
+          TLDR.warn(`Could not find the binding of ${pageId}`)
395 395
           delete nextPageState.bindingId
396 396
         }
397 397
 
398 398
         if (nextPageState.editingId && !page.shapes[nextPageState.editingId]) {
399
-          console.warn('Could not find the editing shape!')
399
+          TLDR.warn('Could not find the editing shape!')
400 400
           delete nextPageState.editingId
401 401
         }
402 402
 
@@ -1529,7 +1529,7 @@ export class TldrawApp extends StateManager<TDSnapshot> {
1529 1529
 
1530 1530
           pasteInCurrentPage(data.shapes, data.bindings)
1531 1531
         } catch (e) {
1532
-          console.warn(e)
1532
+          TLDR.warn(e)
1533 1533
 
1534 1534
           const shapeId = Utils.uniqueId()
1535 1535
 
@@ -1984,7 +1984,7 @@ export class TldrawApp extends StateManager<TDSnapshot> {
1984 1984
   startSession = <T extends SessionType>(type: T, ...args: SessionArgsOfType<T>): this => {
1985 1985
     if (this.readOnly && type !== SessionType.Brush) return this
1986 1986
     if (this.session) {
1987
-      console.warn(`Already in a session! (${this.session.constructor.name})`)
1987
+      TLDR.warn(`Already in a session! (${this.session.constructor.name})`)
1988 1988
       this.cancelSession()
1989 1989
     }
1990 1990
 

+ 3
- 2
packages/tldraw/src/state/shapes/ArrowUtil/arrowHelpers.ts Ver arquivo

@@ -5,6 +5,7 @@ import getStroke from 'perfect-freehand'
5 5
 import { EASINGS } from '~constants'
6 6
 import { getShapeStyle } from '../shared/shape-styles'
7 7
 import type { ArrowShape, TldrawHandle } from '~types'
8
+import { TLDR } from '../../TLDR'
8 9
 
9 10
 export function getArrowArcPath(
10 11
   start: TldrawHandle,
@@ -159,7 +160,7 @@ export function getCurvedArrowHeadPoints(
159 160
   const ints = intersectCircleCircle(A, r1 * 0.618, C, r2).points
160 161
 
161 162
   if (!ints) {
162
-    console.warn('Could not find an intersection for the arrow head.')
163
+    TLDR.warn('Could not find an intersection for the arrow head.')
163 164
     return { left: A, right: A }
164 165
   }
165 166
 
@@ -175,7 +176,7 @@ export function getCurvedArrowHeadPoints(
175 176
 export function getStraightArrowHeadPoints(A: number[], B: number[], r: number) {
176 177
   const ints = intersectCircleLineSegment(A, r, A, B).points
177 178
   if (!ints) {
178
-    console.warn('Could not find an intersection for the arrow head.')
179
+    TLDR.warn('Could not find an intersection for the arrow head.')
179 180
     return { left: A, right: A }
180 181
   }
181 182
 

Carregando…
Cancelar
Salvar