Fixes types

This commit is contained in:
Steve Ruiz 2021-08-12 14:45:47 +01:00
parent 2673a97cc8
commit edc29dfbcf
8 changed files with 25 additions and 44 deletions

View file

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

View file

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

View file

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

View file

@ -1,25 +1,13 @@
import type { import type { TLPage, TLPageState, TLShape, TLBounds, TLShapeUtils, TLBinding } from '../../types'
TLPage,
TLPageState,
TLShape,
TLBounds,
TLShapeUtils,
} from '../../types'
import Utils from '../../utils' import Utils from '../../utils'
import { useTLContext } from '../hooks/useTLContext' import { useTLContext } from '../hooks/useTLContext'
function canvasToScreen( function canvasToScreen(point: number[], camera: TLPageState['camera']): number[] {
point: number[], return [(point[0] + camera.point[0]) * camera.zoom, (point[1] + camera.point[1]) * camera.zoom]
camera: TLPageState['camera']
): number[] {
return [
(point[0] + camera.point[0]) * camera.zoom,
(point[1] + camera.point[1]) * camera.zoom,
]
} }
export function useSelection<T extends TLShape>( export function useSelection<T extends TLShape>(
page: TLPage<T>, page: TLPage<T, TLBinding>,
pageState: TLPageState, pageState: TLPageState,
shapeUtils: TLShapeUtils<T> shapeUtils: TLShapeUtils<T>
) { ) {
@ -59,14 +47,8 @@ export function useSelection<T extends TLShape>(
} }
if (bounds) { if (bounds) {
const [minX, minY] = canvasToScreen( const [minX, minY] = canvasToScreen([bounds.minX, bounds.minY], pageState.camera)
[bounds.minX, bounds.minY], const [maxX, maxY] = canvasToScreen([bounds.maxX, bounds.maxY], pageState.camera)
pageState.camera
)
const [maxX, maxY] = canvasToScreen(
[bounds.maxX, bounds.maxY],
pageState.camera
)
rScreenBounds.current = { rScreenBounds.current = {
minX, minX,

View file

@ -6,13 +6,14 @@ import type {
TLShape, TLShape,
TLShapeUtils, TLShapeUtils,
TLCallbacks, TLCallbacks,
TLBinding,
} from '../../types' } from '../../types'
import Utils, { Vec } from '../../utils' import Utils, { Vec } from '../../utils'
function addToShapeTree<T extends TLShape>( function addToShapeTree<T extends TLShape>(
shape: TLShape, shape: TLShape,
branch: IShapeTreeNode[], branch: IShapeTreeNode[],
shapes: TLPage<T>['shapes'], shapes: TLPage<T, TLBinding>['shapes'],
selectedIds: string[], selectedIds: string[],
info: { info: {
bindingId?: string bindingId?: string
@ -46,7 +47,7 @@ function addToShapeTree<T extends TLShape>(
} }
export function useShapeTree<T extends TLShape>( export function useShapeTree<T extends TLShape>(
page: TLPage<T>, page: TLPage<T, TLBinding>,
pageState: TLPageState, pageState: TLPageState,
shapeUtils: TLShapeUtils<T>, shapeUtils: TLShapeUtils<T>,
onChange?: TLCallbacks['onChange'] onChange?: TLCallbacks['onChange']
@ -87,8 +88,7 @@ export function useShapeTree<T extends TLShape>(
return ( return (
// TODO: Some shapes should always render (lines, rays) // TODO: Some shapes should always render (lines, rays)
Utils.boundsContain(viewport, shapeBounds) || Utils.boundsContain(viewport, shapeBounds) || Utils.boundsCollide(viewport, shapeBounds)
Utils.boundsCollide(viewport, shapeBounds)
) )
}) })
@ -105,9 +105,7 @@ export function useShapeTree<T extends TLShape>(
shapesToRender shapesToRender
.sort((a, b) => a.childIndex - b.childIndex) .sort((a, b) => a.childIndex - b.childIndex)
.forEach((shape) => .forEach((shape) => addToShapeTree(shape, tree, page.shapes, selectedIds, pageState))
addToShapeTree(shape, tree, page.shapes, selectedIds, pageState)
)
return tree return tree
} }

View file

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

View file

@ -1,11 +1,11 @@
/* --------------------- Primary -------------------- */ /* --------------------- Primary -------------------- */
export interface TLPage<T extends TLShape> { export interface TLPage<T extends TLShape, B extends TLBinding> {
id: string id: string
name?: string name?: string
childIndex?: number childIndex?: number
shapes: Record<string, T> shapes: Record<string, T>
bindings: Record<string, TLBinding> bindings: Record<string, B>
backgroundColor?: string backgroundColor?: string
} }

View file

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