@tldraw/tlschema 4.3.0-canary.fd6b7f2a8adc → 4.3.0-next.2b3bfbba757b

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.
@@ -79,51 +79,12 @@ export type TLDefaultShape =
79
79
  */
80
80
  export type TLUnknownShape = TLBaseShape<string, object>
81
81
 
82
- /** @public */
83
- // eslint-disable-next-line @typescript-eslint/no-empty-object-type
84
- export interface TLGlobalShapePropsMap {}
85
-
86
- /** @public */
87
- // prettier-ignore
88
- export type TLIndexedShapes = {
89
- // We iterate over a union of augmented keys and default shape types.
90
- // This allows us to include (or conditionally exclude or override) the default shapes in one go.
91
- //
92
- // In the `as` clause we are filtering out disabled shapes.
93
- [K in keyof TLGlobalShapePropsMap | TLDefaultShape['type'] as K extends TLDefaultShape['type']
94
- ? // core shapes are always available and cannot be overridden so we just include them
95
- K extends 'group'
96
- ? K
97
- : K extends keyof TLGlobalShapePropsMap
98
- ? // if it extends a nullish value the user has disabled this shape type so we filter it out with never
99
- TLGlobalShapePropsMap[K] extends null | undefined
100
- ? never
101
- : K
102
- : K
103
- : K]: K extends 'group'
104
- ? // core shapes are always available and cannot be overridden so we just include them
105
- Extract<TLDefaultShape, { type: K }>
106
- : K extends TLDefaultShape['type']
107
- ? // if it's a default shape type we need to check if it's been overridden
108
- K extends keyof TLGlobalShapePropsMap
109
- ? // if it has been overriden then use the custom shape definition
110
- TLBaseShape<K, TLGlobalShapePropsMap[K]>
111
- : // if it has not been overriden then reuse existing type aliases for better type display
112
- Extract<TLDefaultShape, { type: K }>
113
- : // use the custom shape definition
114
- TLBaseShape<K, TLGlobalShapePropsMap[K & keyof TLGlobalShapePropsMap]>
115
- }
116
-
117
82
  /**
118
- * The set of all shapes that are available in the editor.
83
+ * The set of all shapes that are available in the editor, including unknown shapes.
119
84
  *
120
85
  * This is the primary shape type used throughout tldraw. It includes both the
121
86
  * built-in default shapes and any custom shapes that might be added.
122
87
  *
123
- * You can use this type without a type argument to work with any shape, or pass
124
- * a specific shape type string (e.g., `'geo'`, `'arrow'`, `'text'`) to narrow
125
- * down to that specific shape type.
126
- *
127
88
  * @example
128
89
  * ```ts
129
90
  * // Work with any shape in the editor
@@ -134,16 +95,11 @@ export type TLIndexedShapes = {
134
95
  * y: shape.y + deltaY
135
96
  * }
136
97
  * }
137
- *
138
- * // Narrow to a specific shape type by passing the type as a generic argument
139
- * function getArrowLabel(shape: TLShape<'arrow'>): string {
140
- * return shape.props.text // TypeScript knows this is a TLArrowShape
141
- * }
142
98
  * ```
143
99
  *
144
100
  * @public
145
101
  */
146
- export type TLShape<K extends keyof TLIndexedShapes = keyof TLIndexedShapes> = TLIndexedShapes[K]
102
+ export type TLShape = TLDefaultShape | TLUnknownShape
147
103
 
148
104
  /**
149
105
  * A partial version of a shape, useful for updates and patches.
@@ -183,57 +139,6 @@ export type TLShapePartial<T extends TLShape = TLShape> = T extends T
183
139
  } & Partial<Omit<T, 'type' | 'id' | 'props' | 'meta'>>
184
140
  : never
185
141
 
186
- /**
187
- * A partial version of a shape, useful for creating shapes.
188
- *
189
- * This type represents a shape where all properties except `type` are optional.
190
- * It's commonly used when creating shapes.
191
- *
192
- * @example
193
- * ```ts
194
- * // Create a shape
195
- * const shapeCreate: TLCreateShapePartial = {
196
- * type: 'geo',
197
- * x: 100,
198
- * y: 200
199
- * }
200
- *
201
- * // Create shape properties
202
- * const propsCreate: TLCreateShapePartial<TLGeoShape> = {
203
- * type: 'geo',
204
- * props: {
205
- * w: 150,
206
- * h: 100
207
- * }
208
- * }
209
- * ```
210
- *
211
- * @public
212
- */
213
- export type TLCreateShapePartial<T extends TLShape = TLShape> = T extends T
214
- ? {
215
- type: T['type']
216
- props?: Partial<T['props']>
217
- meta?: Partial<T['meta']>
218
- } & Partial<Omit<T, 'type' | 'props' | 'meta'>>
219
- : never
220
-
221
- /**
222
- * Extract a shape type by its props.
223
- *
224
- * This utility type takes a props object type and returns the corresponding shape type
225
- * from the TLShape union whose props match the given type.
226
- *
227
- * @example
228
- * ```ts
229
- * type MyShape = ExtractShapeByProps<{ w: number; h: number }>
230
- * // MyShape is now the type of shape(s) that have props with w and h as numbers
231
- * ```
232
- *
233
- * @public
234
- */
235
- export type ExtractShapeByProps<P> = Extract<TLShape, { props: P }>
236
-
237
142
  /**
238
143
  * A unique identifier for a shape record.
239
144
  *
@@ -248,7 +153,7 @@ export type ExtractShapeByProps<P> = Extract<TLShape, { props: P }>
248
153
  *
249
154
  * @public
250
155
  */
251
- export type TLShapeId = RecordId<TLShape>
156
+ export type TLShapeId = RecordId<TLUnknownShape>
252
157
 
253
158
  /**
254
159
  * The ID of a shape's parent, which can be either a page or another shape.
@@ -290,7 +195,7 @@ export const rootShapeVersions = createMigrationIds('com.tldraw.shape', {
290
195
  HoistOpacity: 2,
291
196
  AddMeta: 3,
292
197
  AddWhite: 4,
293
- })
198
+ } as const)
294
199
 
295
200
  /**
296
201
  * Migration sequence for the root shape record type.
@@ -564,7 +469,7 @@ export function createShapePropsMigrationIds<
564
469
  * @internal
565
470
  */
566
471
  export function createShapeRecordType(shapes: Record<string, SchemaPropsInfo>) {
567
- return createRecordType('shape', {
472
+ return createRecordType<TLShape>('shape', {
568
473
  scope: 'document',
569
474
  validator: T.model(
570
475
  'shape',
@@ -1,5 +1,5 @@
1
1
  import { VecModel } from '../misc/geometry-types'
2
- import { ExtractShapeByProps } from '../records/TLShape'
2
+ import { TLBaseShape } from './TLBaseShape'
3
3
 
4
4
  /**
5
5
  * Defines cropping parameters for shapes that support cropping.
@@ -71,4 +71,4 @@ export interface TLShapeCrop {
71
71
  *
72
72
  * @public
73
73
  */
74
- export type ShapeWithCrop = ExtractShapeByProps<{ w: number; h: number; crop: TLShapeCrop | null }>
74
+ export type ShapeWithCrop = TLBaseShape<string, { w: number; h: number; crop: TLShapeCrop | null }>
@@ -1,3 +1,4 @@
1
+ import { BaseRecord } from '@tldraw/store'
1
2
  import { IndexKey, JsonObject } from '@tldraw/utils'
2
3
  import { T } from '@tldraw/validate'
3
4
  import { TLOpacityType, opacityValidator } from '../misc/TLOpacity'
@@ -8,37 +9,18 @@ import { TLParentId, TLShapeId } from '../records/TLShape'
8
9
  * Base interface for all shapes in tldraw.
9
10
  *
10
11
  * This interface defines the common properties that all shapes share, regardless of their
11
- * specific type. Every default shape extends this base with additional type-specific properties.
12
- *
13
- * Custom shapes should be defined by augmenting the TLGlobalShapePropsMap type and getting the shape type from the TLShape type.
12
+ * specific type. Every shape extends this base with additional type-specific properties.
14
13
  *
15
14
  * @example
16
15
  * ```ts
17
- * // Define a default shape type
18
- * interface TLArrowShape extends TLBaseShape<'arrow', {
19
- * kind: TLArrowShapeKind
20
- * labelColor: TLDefaultColorStyle
21
- * color: TLDefaultColorStyle
22
- * fill: TLDefaultFillStyle
23
- * dash: TLDefaultDashStyle
24
- * size: TLDefaultSizeStyle
25
- * arrowheadStart: TLArrowShapeArrowheadStyle
26
- * arrowheadEnd: TLArrowShapeArrowheadStyle
27
- * font: TLDefaultFontStyle
28
- * start: VecModel
29
- * end: VecModel
30
- * bend: number
31
- * richText: TLRichText
32
- * labelPosition: number
33
- * scale: number
34
- * elbowMidPoint: number
35
- * }> {}
16
+ * // Define a custom shape type
17
+ * interface MyCustomShape extends TLBaseShape<'custom', { size: number; color: string }> {}
36
18
  *
37
19
  * // Create a shape instance
38
- * const arrowShape: TLArrowShape = {
20
+ * const myShape: MyCustomShape = {
39
21
  * id: 'shape:abc123',
40
22
  * typeName: 'shape',
41
- * type: 'arrow',
23
+ * type: 'custom',
42
24
  * x: 100,
43
25
  * y: 200,
44
26
  * rotation: 0,
@@ -47,10 +29,8 @@ import { TLParentId, TLShapeId } from '../records/TLShape'
47
29
  * isLocked: false,
48
30
  * opacity: 1,
49
31
  * props: {
50
- * kind: 'arc',
51
- * start: { x: 0, y: 0 },
52
- * end: { x: 100, y: 100 },
53
- * // ... other props
32
+ * size: 50,
33
+ * color: 'blue'
54
34
  * },
55
35
  * meta: {}
56
36
  * }
@@ -58,12 +38,8 @@ import { TLParentId, TLShapeId } from '../records/TLShape'
58
38
  *
59
39
  * @public
60
40
  */
61
- export interface TLBaseShape<Type extends string, Props extends object> {
62
- // using real `extends BaseRecord<'shape', TLShapeId>` introduces a circularity in the types
63
- // and for that reason those "base members" have to be declared manually here
64
- readonly id: TLShapeId
65
- readonly typeName: 'shape'
66
-
41
+ export interface TLBaseShape<Type extends string, Props extends object>
42
+ extends BaseRecord<'shape', TLShapeId> {
67
43
  type: Type
68
44
  x: number
69
45
  y: number
@@ -72,8 +72,7 @@ export const storeMigrations = createMigrationSequence({
72
72
  for (const [id, record] of objectMapEntries(store)) {
73
73
  if (
74
74
  record.typeName === 'shape' &&
75
- 'type' in record &&
76
- (record.type === 'icon' || record.type === 'code')
75
+ ((record as TLShape).type === 'icon' || (record as TLShape).type === 'code')
77
76
  ) {
78
77
  delete store[id]
79
78
  }