Allows for additional properties

This commit is contained in:
Steve Ruiz 2021-09-13 16:59:37 +01:00
parent 2f4a1f97a2
commit 22fbca58d3
7 changed files with 25 additions and 17 deletions

View file

@ -14,7 +14,7 @@ import { Inputs } from '../../inputs'
import { useTLTheme, TLContext, TLContextType } from '../../hooks' import { useTLTheme, TLContext, TLContextType } from '../../hooks'
import type { TLShapeUtil } from '+index' import type { TLShapeUtil } from '+index'
export interface RendererProps<T extends TLShape, E extends Element, M = any> export interface RendererProps<T extends TLShape, E extends Element = any, M = any>
extends Partial<TLCallbacks<T>> { extends Partial<TLCallbacks<T>> {
/** /**
* An object containing instances of your shape classes. * An object containing instances of your shape classes.

View file

@ -12,8 +12,9 @@ export interface BoxShape extends TLShape {
size: number[] size: number[]
} }
export const Box = new ShapeUtil<BoxShape, SVGSVGElement, null>(() => { export const Box = new ShapeUtil<BoxShape, SVGSVGElement, null, { age: number }>(() => {
return { return {
age: 100,
type: 'box', type: 'box',
defaultProps: { defaultProps: {
id: 'example1', id: 'example1',
@ -90,6 +91,10 @@ describe('shape utils', () => {
expect(Box).toBeTruthy() expect(Box).toBeTruthy()
}) })
it('creates a shape utils with extended properties', () => {
expect(Box.age).toBe(100)
})
it('creates a shape', () => { it('creates a shape', () => {
expect(Box.create({ id: 'box1' })).toStrictEqual({ expect(Box.create({ id: 'box1' })).toStrictEqual({
...boxShape, ...boxShape,

View file

@ -5,13 +5,14 @@ import type { TLShape, TLShapeUtil } from '+types'
import Utils from '+utils' import Utils from '+utils'
import { intersectPolylineBounds, intersectRayBounds } from '@tldraw/intersect' import { intersectPolylineBounds, intersectRayBounds } from '@tldraw/intersect'
export const ShapeUtil = function <T extends TLShape, E extends Element, M = any>( export const ShapeUtil = function <T extends TLShape, E extends Element, M = any, K = unknown>(
this: TLShapeUtil<T, E, M>, this: TLShapeUtil<T, E, M> & K,
fn: ( fn: (
this: TLShapeUtil<T, E, M> this: TLShapeUtil<T, E, M> & K
) => Partial<TLShapeUtil<T, E, M>> & ) => Partial<TLShapeUtil<T, E, M>> &
Pick<TLShapeUtil<T, E, M>, 'type' | 'defaultProps' | 'Component' | 'Indicator' | 'getBounds'> Pick<TLShapeUtil<T, E, M>, 'type' | 'defaultProps' | 'Component' | 'Indicator' | 'getBounds'> &
) { K
): TLShapeUtil<T, E, M> & ReturnType<typeof fn> {
const defaults: Partial<TLShapeUtil<T, E, M>> = { const defaults: Partial<TLShapeUtil<T, E, M>> = {
refMap: new Map(), refMap: new Map(),
@ -211,10 +212,14 @@ export const ShapeUtil = function <T extends TLShape, E extends Element, M = any
return this return this
} as unknown as { } as unknown as {
new <T extends TLShape, E extends Element, M = any>( new <T extends TLShape, E extends Element, M = any, K = unknown>(
fn: ( fn: (
this: TLShapeUtil<T, E, M> this: TLShapeUtil<T, E, M>
) => Partial<TLShapeUtil<T, E, M>> & ) => Partial<TLShapeUtil<T, E, M>> &
Pick<TLShapeUtil<T, E, M>, 'type' | 'defaultProps' | 'Component' | 'Indicator' | 'getBounds'> Pick<
): TLShapeUtil<T, E, M> TLShapeUtil<T, E, M>,
'type' | 'defaultProps' | 'Component' | 'Indicator' | 'getBounds'
> &
K
): TLShapeUtil<T, E, M> & ReturnType<typeof fn>
} }

View file

@ -13,6 +13,6 @@ export const tldrawShapeUtils: Record<TLDrawShapeType, any> = {
[TLDrawShapeType.PostIt]: PostIt, [TLDrawShapeType.PostIt]: PostIt,
} }
export function getShapeUtils<T extends TLDrawShape>(type: TLDrawShapeType) { export function getShapeUtils<T extends TLDrawShape>(type: T['type']) {
return tldrawShapeUtils[type] as TLDrawShapeUtil<T, any> return tldrawShapeUtils[type] as TLDrawShapeUtil<T>
} }

View file

@ -1,7 +1,6 @@
import { brushUpdater, Utils } from '@tldraw/core' import { brushUpdater, Utils } from '@tldraw/core'
import { Vec } from '@tldraw/vec' import { Vec } from '@tldraw/vec'
import { Data, Session, TLDrawPatch, TLDrawStatus } from '~types' import { Data, Session, TLDrawPatch, TLDrawStatus } from '~types'
import { getShapeUtils } from '~shape'
import { TLDR } from '~state/tldr' import { TLDR } from '~state/tldr'
export class BrushSession implements Session { export class BrushSession implements Session {

View file

@ -15,9 +15,9 @@ import { Vec } from '@tldraw/vec'
export class TLDR { export class TLDR {
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
static getShapeUtils<T extends TLDrawShape>(type: T['type']): TLDrawShapeUtil<T, any> static getShapeUtils<T extends TLDrawShape>(type: T['type']): TLDrawShapeUtil<T>
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
static getShapeUtils<T extends TLDrawShape>(shape: T): TLDrawShapeUtil<T, any> static getShapeUtils<T extends TLDrawShape>(shape: T): TLDrawShapeUtil<T>
static getShapeUtils<T extends TLDrawShape>(shape: T | T['type']) { static getShapeUtils<T extends TLDrawShape>(shape: T | T['type']) {
return getShapeUtils<T>(typeof shape === 'string' ? shape : shape.type) return getShapeUtils<T>(typeof shape === 'string' ? shape : shape.type)
} }

View file

@ -212,8 +212,7 @@ export type TLDrawShape =
| GroupShape | GroupShape
| PostItShape | PostItShape
export interface TLDrawShapeUtil<T extends TLDrawShape, E extends Element> export type TLDrawShapeUtil<T extends TLDrawShape> = TLShapeUtil<T, any, TLDrawMeta> & {
extends TLShapeUtil<T, E, TLDrawMeta> {
toolType: TLDrawToolType toolType: TLDrawToolType
} }