Browse Source

Fixes types

main
Steve Ruiz 4 years ago
parent
commit
edc29dfbcf

+ 2
- 2
packages/core/src/renderer/components/canvas.tsx View File

@@ -2,7 +2,7 @@ import * as React from 'react'
2 2
 import { ErrorBoundary } from 'react-error-boundary'
3 3
 import { useZoomEvents, useSafariFocusOutFix, useCanvasEvents, useCameraCss } from '../hooks'
4 4
 import { ErrorFallback } from './error-fallback'
5
-import type { TLPage, TLPageState, TLShape } from '../../types'
5
+import type { TLBinding, TLPage, TLPageState, TLShape } from '../../types'
6 6
 
7 7
 import { Brush } from './brush'
8 8
 import { Defs } from './defs'
@@ -14,7 +14,7 @@ function resetError() {
14 14
 }
15 15
 
16 16
 interface CanvasProps<T extends TLShape> {
17
-  page: TLPage<T>
17
+  page: TLPage<T, TLBinding>
18 18
   pageState: TLPageState
19 19
   hideBounds?: boolean
20 20
   hideHandles?: boolean

+ 2
- 2
packages/core/src/renderer/components/page.tsx View File

@@ -1,6 +1,6 @@
1 1
 import * as React from 'react'
2 2
 import { useShapeTree } from '../hooks/useShapeTree'
3
-import type { IShapeTreeNode, TLPage, TLPageState, TLShape } from '../../types'
3
+import type { IShapeTreeNode, TLBinding, TLPage, TLPageState, TLShape } from '../../types'
4 4
 import { Shape as ShapeComponent } from './shape'
5 5
 import { useHandles, useRenderOnResize, useTLContext } from '../hooks'
6 6
 import { Bounds } from './bounds'
@@ -9,7 +9,7 @@ import { useSelection } from '../hooks'
9 9
 import { Handles } from './handles'
10 10
 
11 11
 interface PageProps<T extends TLShape> {
12
-  page: TLPage<T>
12
+  page: TLPage<T, TLBinding>
13 13
   pageState: TLPageState
14 14
   hideBounds: boolean
15 15
   hideHandles: boolean

+ 2
- 5
packages/core/src/renderer/hooks/useHandles.ts View File

@@ -1,9 +1,6 @@
1
-import type { TLPage, TLPageState, TLShape } from '../../types'
1
+import type { TLBinding, TLPage, TLPageState, TLShape } from '../../types'
2 2
 
3
-export function useHandles<T extends TLShape>(
4
-  page: TLPage<T>,
5
-  pageState: TLPageState
6
-) {
3
+export function useHandles<T extends TLShape>(page: TLPage<T, TLBinding>, pageState: TLPageState) {
7 4
   const { selectedIds } = pageState
8 5
 
9 6
   let shapeWithHandles: TLShape | undefined = undefined

+ 6
- 24
packages/core/src/renderer/hooks/useSelection.tsx View File

@@ -1,25 +1,13 @@
1
-import type {
2
-  TLPage,
3
-  TLPageState,
4
-  TLShape,
5
-  TLBounds,
6
-  TLShapeUtils,
7
-} from '../../types'
1
+import type { TLPage, TLPageState, TLShape, TLBounds, TLShapeUtils, TLBinding } from '../../types'
8 2
 import Utils from '../../utils'
9 3
 import { useTLContext } from '../hooks/useTLContext'
10 4
 
11
-function canvasToScreen(
12
-  point: number[],
13
-  camera: TLPageState['camera']
14
-): number[] {
15
-  return [
16
-    (point[0] + camera.point[0]) * camera.zoom,
17
-    (point[1] + camera.point[1]) * camera.zoom,
18
-  ]
5
+function canvasToScreen(point: number[], camera: TLPageState['camera']): number[] {
6
+  return [(point[0] + camera.point[0]) * camera.zoom, (point[1] + camera.point[1]) * camera.zoom]
19 7
 }
20 8
 
21 9
 export function useSelection<T extends TLShape>(
22
-  page: TLPage<T>,
10
+  page: TLPage<T, TLBinding>,
23 11
   pageState: TLPageState,
24 12
   shapeUtils: TLShapeUtils<T>
25 13
 ) {
@@ -59,14 +47,8 @@ export function useSelection<T extends TLShape>(
59 47
   }
60 48
 
61 49
   if (bounds) {
62
-    const [minX, minY] = canvasToScreen(
63
-      [bounds.minX, bounds.minY],
64
-      pageState.camera
65
-    )
66
-    const [maxX, maxY] = canvasToScreen(
67
-      [bounds.maxX, bounds.maxY],
68
-      pageState.camera
69
-    )
50
+    const [minX, minY] = canvasToScreen([bounds.minX, bounds.minY], pageState.camera)
51
+    const [maxX, maxY] = canvasToScreen([bounds.maxX, bounds.maxY], pageState.camera)
70 52
 
71 53
     rScreenBounds.current = {
72 54
       minX,

+ 5
- 7
packages/core/src/renderer/hooks/useShapeTree.tsx View File

@@ -6,13 +6,14 @@ import type {
6 6
   TLShape,
7 7
   TLShapeUtils,
8 8
   TLCallbacks,
9
+  TLBinding,
9 10
 } from '../../types'
10 11
 import Utils, { Vec } from '../../utils'
11 12
 
12 13
 function addToShapeTree<T extends TLShape>(
13 14
   shape: TLShape,
14 15
   branch: IShapeTreeNode[],
15
-  shapes: TLPage<T>['shapes'],
16
+  shapes: TLPage<T, TLBinding>['shapes'],
16 17
   selectedIds: string[],
17 18
   info: {
18 19
     bindingId?: string
@@ -46,7 +47,7 @@ function addToShapeTree<T extends TLShape>(
46 47
 }
47 48
 
48 49
 export function useShapeTree<T extends TLShape>(
49
-  page: TLPage<T>,
50
+  page: TLPage<T, TLBinding>,
50 51
   pageState: TLPageState,
51 52
   shapeUtils: TLShapeUtils<T>,
52 53
   onChange?: TLCallbacks['onChange']
@@ -87,8 +88,7 @@ export function useShapeTree<T extends TLShape>(
87 88
 
88 89
     return (
89 90
       // TODO: Some shapes should always render (lines, rays)
90
-      Utils.boundsContain(viewport, shapeBounds) ||
91
-      Utils.boundsCollide(viewport, shapeBounds)
91
+      Utils.boundsContain(viewport, shapeBounds) || Utils.boundsCollide(viewport, shapeBounds)
92 92
     )
93 93
   })
94 94
 
@@ -105,9 +105,7 @@ export function useShapeTree<T extends TLShape>(
105 105
 
106 106
   shapesToRender
107 107
     .sort((a, b) => a.childIndex - b.childIndex)
108
-    .forEach((shape) =>
109
-      addToShapeTree(shape, tree, page.shapes, selectedIds, pageState)
110
-    )
108
+    .forEach((shape) => addToShapeTree(shape, tree, page.shapes, selectedIds, pageState))
111 109
 
112 110
   return tree
113 111
 }

+ 2
- 1
packages/core/src/renderer/renderer.tsx View File

@@ -8,6 +8,7 @@ import type {
8 8
   TLShapeUtils,
9 9
   TLTheme,
10 10
   TLBounds,
11
+  TLBinding,
11 12
 } from '../types'
12 13
 import { Canvas } from './components/canvas'
13 14
 import { useTLTheme } from './hooks/useStyle'
@@ -17,7 +18,7 @@ export interface RendererProps<T extends TLShape>
17 18
   extends Partial<TLSettings>,
18 19
     Partial<TLCallbacks> {
19 20
   shapeUtils: TLShapeUtils<T>
20
-  page: TLPage<T>
21
+  page: TLPage<T, TLBinding>
21 22
   pageState: TLPageState
22 23
   theme?: Partial<TLTheme>
23 24
   hideBounds?: boolean

+ 2
- 2
packages/core/src/types.tsx View File

@@ -1,11 +1,11 @@
1 1
 /* --------------------- Primary -------------------- */
2 2
 
3
-export interface TLPage<T extends TLShape> {
3
+export interface TLPage<T extends TLShape, B extends TLBinding> {
4 4
   id: string
5 5
   name?: string
6 6
   childIndex?: number
7 7
   shapes: Record<string, T>
8
-  bindings: Record<string, TLBinding>
8
+  bindings: Record<string, B>
9 9
   backgroundColor?: string
10 10
 }
11 11
 

+ 4
- 1
packages/tldraw/tsconfig.json View File

@@ -7,6 +7,9 @@
7 7
     "jsx": "preserve",
8 8
     "lib": ["dom", "esnext"],
9 9
     "module": "esnext",
10
-    "outDir": "./dist/types"
10
+    "outDir": "./dist/types",
11
+    "paths": {
12
+      "@tldraw/core": ["packages/core/dist"]
13
+    }
11 14
   }
12 15
 }

Loading…
Cancel
Save