Expose migrations, validators, and versions from tlschema (#3613)

Previously, we weren't exporting migrations & validators for our default
shapes. This meant that it wasn't possible to make your own tlschema
with both our default shapes and some of your own (e.g. for custom
multiplayer). This fixes that by exposing all the migrations,
validators, and versions from tlschema.

### Change Type
- [x] `sdk` — Changes the tldraw SDK
- [x] `bugfix` — Bug fix
This commit is contained in:
alex 2024-04-25 15:31:26 +01:00 committed by GitHub
parent 15dd56a75e
commit f78719b054
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
33 changed files with 87 additions and 157 deletions

View file

@ -27,7 +27,7 @@ export const ArrowShapeArrowheadEndStyle: EnumStyleProp<"arrow" | "bar" | "diamo
// @public (undocumented)
export const ArrowShapeArrowheadStartStyle: EnumStyleProp<"arrow" | "bar" | "diamond" | "dot" | "inverted" | "none" | "pipe" | "square" | "triangle">;
// @internal (undocumented)
// @public (undocumented)
export const arrowShapeMigrations: TLShapePropsMigrations;
// @public (undocumented)
@ -76,16 +76,16 @@ export const arrowShapeProps: {
// @public
export const assetIdValidator: T.Validator<TLAssetId>;
// @internal (undocumented)
// @public (undocumented)
export const assetMigrations: MigrationSequence;
// @public (undocumented)
export const AssetRecordType: RecordType<TLAsset, "props" | "type">;
// @internal (undocumented)
// @public (undocumented)
export const assetValidator: T.Validator<TLAsset>;
// @internal (undocumented)
// @public (undocumented)
export const bookmarkShapeMigrations: TLShapePropsMigrations;
// @public (undocumented)
@ -192,6 +192,11 @@ export const DefaultFontStyle: EnumStyleProp<"draw" | "mono" | "sans" | "serif">
// @public (undocumented)
export const DefaultHorizontalAlignStyle: EnumStyleProp<"end-legacy" | "end" | "middle-legacy" | "middle" | "start-legacy" | "start">;
// @public (undocumented)
export const defaultShapeSchemas: {
[T in TLDefaultShape['type']]: SchemaShapeInfo;
};
// @public (undocumented)
export const DefaultSizeStyle: EnumStyleProp<"l" | "m" | "s" | "xl">;
@ -201,7 +206,7 @@ export const DefaultVerticalAlignStyle: EnumStyleProp<"end" | "middle" | "start"
// @public (undocumented)
export const DocumentRecordType: RecordType<TLDocument, never>;
// @internal (undocumented)
// @public (undocumented)
export const drawShapeMigrations: TLShapePropsMigrations;
// @public (undocumented)
@ -420,7 +425,7 @@ export type EmbedDefinition = {
readonly width: number;
};
// @internal (undocumented)
// @public (undocumented)
export const embedShapeMigrations: TLShapePropsMigrations;
// @public
@ -456,7 +461,7 @@ export class EnumStyleProp<T> extends StyleProp<T> {
readonly values: readonly T[];
}
// @internal (undocumented)
// @public (undocumented)
export const frameShapeMigrations: TLShapePropsMigrations;
// @public (undocumented)
@ -469,7 +474,7 @@ export const frameShapeProps: {
// @public (undocumented)
export const GeoShapeGeoStyle: EnumStyleProp<"arrow-down" | "arrow-left" | "arrow-right" | "arrow-up" | "check-box" | "cloud" | "diamond" | "ellipse" | "hexagon" | "octagon" | "oval" | "pentagon" | "rectangle" | "rhombus-2" | "rhombus" | "star" | "trapezoid" | "triangle" | "x-box">;
// @internal (undocumented)
// @public (undocumented)
export const geoShapeMigrations: TLShapePropsMigrations;
// @public (undocumented)
@ -501,13 +506,13 @@ export function getDefaultTranslationLocale(): TLLanguage['locale'];
// @internal (undocumented)
export function getShapePropKeysByStyle(props: Record<string, T.Validatable<any>>): Map<StyleProp<unknown>, string>;
// @internal (undocumented)
// @public (undocumented)
export const groupShapeMigrations: TLShapePropsMigrations;
// @internal (undocumented)
// @public (undocumented)
export const groupShapeProps: ShapeProps<TLGroupShape>;
// @internal (undocumented)
// @public (undocumented)
export const highlightShapeMigrations: TLShapePropsMigrations;
// @public (undocumented)
@ -522,10 +527,10 @@ export const highlightShapeProps: {
size: EnumStyleProp<"l" | "m" | "s" | "xl">;
};
// @internal (undocumented)
// @public (undocumented)
export function idValidator<Id extends RecordId<UnknownRecord>>(prefix: Id['__type__']['typeName']): T.Validator<Id>;
// @internal (undocumented)
// @public (undocumented)
export const imageShapeMigrations: TLShapePropsMigrations;
// @public (undocumented)
@ -667,7 +672,7 @@ export const LANGUAGES: readonly [{
readonly locale: "zh-tw";
}];
// @internal (undocumented)
// @public (undocumented)
export const lineShapeMigrations: TLShapePropsMigrations;
// @public (undocumented)
@ -687,7 +692,7 @@ export const lineShapeProps: {
// @public (undocumented)
export const LineShapeSplineStyle: EnumStyleProp<"cubic" | "line">;
// @internal (undocumented)
// @public (undocumented)
export const noteShapeMigrations: TLShapePropsMigrations;
// @public (undocumented)
@ -703,10 +708,10 @@ export const noteShapeProps: {
verticalAlign: EnumStyleProp<"end" | "middle" | "start">;
};
// @internal (undocumented)
// @public (undocumented)
export const opacityValidator: T.Validator<number>;
// @internal (undocumented)
// @public (undocumented)
export const pageIdValidator: T.Validator<TLPageId>;
// @public (undocumented)
@ -718,7 +723,7 @@ export const parentIdValidator: T.Validator<TLParentId>;
// @public (undocumented)
export const PointerRecordType: RecordType<TLPointer, never>;
// @internal (undocumented)
// @public (undocumented)
export const rootShapeMigrations: MigrationSequence;
// @public (undocumented)
@ -728,7 +733,7 @@ export type SchemaShapeInfo = {
props?: Record<string, AnyValidator>;
};
// @internal (undocumented)
// @public (undocumented)
export const scribbleValidator: T.Validator<TLScribble>;
// @public (undocumented)
@ -771,7 +776,7 @@ export class StyleProp<Type> implements T.Validatable<Type> {
// @public (undocumented)
export type StylePropValue<T extends StyleProp<any>> = T extends StyleProp<infer U> ? U : never;
// @internal (undocumented)
// @public (undocumented)
export const textShapeMigrations: TLShapePropsMigrations;
// @public (undocumented)
@ -1290,7 +1295,7 @@ export interface VecModel {
// @public (undocumented)
export const vecModelValidator: T.Validator<VecModel>;
// @internal (undocumented)
// @public (undocumented)
export const videoShapeMigrations: TLShapePropsMigrations;
// @public (undocumented)

View file

@ -17,7 +17,7 @@ export type TLBookmarkAsset = TLBaseAsset<
}
>
/** @internal */
/** @public */
export const bookmarkAssetValidator: T.Validator<TLBookmarkAsset> = createAssetValidator(
'bookmark',
T.object({
@ -34,7 +34,7 @@ const Versions = createMigrationIds('com.tldraw.asset.bookmark', {
export { Versions as bookmarkAssetVersions }
/** @internal */
/** @public */
export const bookmarkAssetMigrations = createRecordMigrationSequence({
sequenceId: 'com.tldraw.asset.bookmark',
recordType: 'asset',

View file

@ -19,7 +19,7 @@ export type TLImageAsset = TLBaseAsset<
}
>
/** @internal */
/** @public */
export const imageAssetValidator: T.Validator<TLImageAsset> = createAssetValidator(
'image',
T.object({
@ -40,7 +40,7 @@ const Versions = createMigrationIds('com.tldraw.asset.image', {
export { Versions as imageAssetVersions }
/** @internal */
/** @public */
export const imageAssetMigrations = createRecordMigrationSequence({
sequenceId: 'com.tldraw.asset.image',
recordType: 'asset',

View file

@ -19,7 +19,7 @@ export type TLVideoAsset = TLBaseAsset<
}
>
/** @internal */
/** @public */
export const videoAssetValidator: T.Validator<TLVideoAsset> = createAssetValidator(
'video',
T.object({
@ -40,7 +40,7 @@ const Versions = createMigrationIds('com.tldraw.asset.video', {
export { Versions as videoAssetVersions }
/** @internal */
/** @public */
export const videoAssetMigrations = createRecordMigrationSequence({
sequenceId: 'com.tldraw.asset.video',
recordType: 'asset',

View file

@ -52,7 +52,8 @@ export type SchemaShapeInfo = {
/** @public */
export type TLSchema = StoreSchema<TLRecord, TLStoreProps>
const defaultShapes: { [T in TLDefaultShape['type']]: SchemaShapeInfo } = {
/** @public */
export const defaultShapeSchemas: { [T in TLDefaultShape['type']]: SchemaShapeInfo } = {
arrow: { migrations: arrowShapeMigrations, props: arrowShapeProps },
bookmark: { migrations: bookmarkShapeMigrations, props: bookmarkShapeProps },
draw: { migrations: drawShapeMigrations, props: drawShapeProps },
@ -75,7 +76,7 @@ const defaultShapes: { [T in TLDefaultShape['type']]: SchemaShapeInfo } = {
*
* @public */
export function createTLSchema({
shapes = defaultShapes,
shapes = defaultShapeSchemas,
migrations,
}: {
shapes?: Record<string, SchemaShapeInfo>

View file

@ -10,7 +10,12 @@ export { type TLBookmarkAsset } from './assets/TLBookmarkAsset'
export { type TLImageAsset } from './assets/TLImageAsset'
export { type TLVideoAsset } from './assets/TLVideoAsset'
export { createPresenceStateDerivation } from './createPresenceStateDerivation'
export { createTLSchema, type SchemaShapeInfo, type TLSchema } from './createTLSchema'
export {
createTLSchema,
defaultShapeSchemas,
type SchemaShapeInfo,
type TLSchema,
} from './createTLSchema'
export {
TL_CANVAS_UI_COLOR_TYPES,
canvasUiColorTypeValidator,

View file

@ -35,7 +35,7 @@ export const TL_CURSOR_TYPES = new Set([
* @public */
export type TLCursorType = SetValue<typeof TL_CURSOR_TYPES>
/** @internal */
/** @public */
export const cursorTypeValidator = T.setEnum(TL_CURSOR_TYPES)
/**
@ -47,7 +47,7 @@ export interface TLCursor {
rotation: number
}
/** @internal */
/** @public */
export const cursorValidator: T.Validator<TLCursor> = T.object<TLCursor>({
type: cursorTypeValidator,
rotation: T.number,

View file

@ -3,7 +3,7 @@ import { T } from '@tldraw/validate'
/** @public */
export type TLOpacityType = number
/** @internal */
/** @public */
export const opacityValidator = T.number.check((n) => {
if (n < 0 || n > 1) {
throw new T.ValidationError('Opacity must be between 0 and 1')

View file

@ -25,7 +25,7 @@ export type TLScribble = {
taper: boolean
}
/** @internal */
/** @public */
export const scribbleValidator: T.Validator<TLScribble> = T.object({
id: T.string,
points: T.arrayOf(vecModelValidator),

View file

@ -1,7 +1,7 @@
import type { RecordId, UnknownRecord } from '@tldraw/store'
import { T } from '@tldraw/validate'
/** @internal */
/** @public */
export function idValidator<Id extends RecordId<UnknownRecord>>(
prefix: Id['__type__']['typeName']
): T.Validator<Id> {

View file

@ -14,7 +14,7 @@ import { TLShape } from './TLShape'
/** @public */
export type TLAsset = TLImageAsset | TLVideoAsset | TLBookmarkAsset
/** @internal */
/** @public */
export const assetValidator: T.Validator<TLAsset> = T.model(
'asset',
T.union('type', {
@ -24,12 +24,12 @@ export const assetValidator: T.Validator<TLAsset> = T.model(
})
)
/** @internal */
/** @public */
export const assetVersions = createMigrationIds('com.tldraw.asset', {
AddMeta: 1,
} as const)
/** @internal */
/** @public */
export const assetMigrations = createRecordMigrationSequence({
sequenceId: 'com.tldraw.asset',
recordType: 'asset',

View file

@ -27,7 +27,7 @@ export interface TLCamera extends BaseRecord<'camera', TLCameraId> {
* @public */
export type TLCameraId = RecordId<TLCamera>
/** @internal */
/** @public */
export const cameraValidator: T.Validator<TLCamera> = T.model(
'camera',
T.object({
@ -40,12 +40,12 @@ export const cameraValidator: T.Validator<TLCamera> = T.model(
})
)
/** @internal */
/** @public */
export const cameraVersions = createMigrationIds('com.tldraw.camera', {
AddMeta: 1,
})
/** @internal */
/** @public */
export const cameraMigrations = createRecordMigrationSequence({
sequenceId: 'com.tldraw.camera',
recordType: 'camera',

View file

@ -19,7 +19,7 @@ export interface TLDocument extends BaseRecord<'document', RecordId<TLDocument>>
meta: JsonObject
}
/** @internal */
/** @public */
export const documentValidator: T.Validator<TLDocument> = T.model(
'document',
T.object({
@ -31,13 +31,13 @@ export const documentValidator: T.Validator<TLDocument> = T.model(
})
)
/** @internal */
/** @public */
export const documentVersions = createMigrationIds('com.tldraw.document', {
AddName: 1,
AddMeta: 2,
} as const)
/** @internal */
/** @public */
export const documentMigrations = createRecordMigrationSequence({
sequenceId: 'com.tldraw.document',
recordType: 'document',

View file

@ -73,7 +73,7 @@ export interface TLInstance extends BaseRecord<'instance', TLInstanceId> {
/** @public */
export type TLInstanceId = RecordId<TLInstance>
/** @internal */
/** @public */
export const instanceIdValidator = idValidator<TLInstanceId>('instance')
export function createInstanceRecordType(stylesById: Map<string, StyleProp<unknown>>) {
@ -197,7 +197,7 @@ export function createInstanceRecordType(stylesById: Map<string, StyleProp<unkno
)
}
/** @internal */
/** @public */
export const instanceVersions = createMigrationIds('com.tldraw.instance', {
AddTransparentExportBgs: 1,
RemoveDialog: 2,

View file

@ -23,10 +23,10 @@ export interface TLPage extends BaseRecord<'page', TLPageId> {
/** @public */
export type TLPageId = RecordId<TLPage>
/** @internal */
/** @public */
export const pageIdValidator = idValidator<TLPageId>('page')
/** @internal */
/** @public */
export const pageValidator: T.Validator<TLPage> = T.model(
'page',
T.object({
@ -38,12 +38,12 @@ export const pageValidator: T.Validator<TLPage> = T.model(
})
)
/** @internal */
/** @public */
export const pageVersions = createMigrationIds('com.tldraw.page', {
AddMeta: 1,
})
/** @internal */
/** @public */
export const pageMigrations = createRecordMigrationSequence({
sequenceId: 'com.tldraw.page',
recordType: 'page',

View file

@ -32,7 +32,7 @@ export interface TLInstancePageState
meta: JsonObject
}
/** @internal */
/** @public */
export const instancePageStateValidator: T.Validator<TLInstancePageState> = T.model(
'instance_page_state',
T.object({
@ -50,7 +50,7 @@ export const instancePageStateValidator: T.Validator<TLInstancePageState> = T.mo
})
)
/** @internal */
/** @public */
export const instancePageStateVersions = createMigrationIds('com.tldraw.instance_page_state', {
AddCroppingId: 1,
RemoveInstanceIdAndCameraId: 2,

View file

@ -24,7 +24,7 @@ export interface TLPointer extends BaseRecord<'pointer', TLPointerId> {
/** @public */
export type TLPointerId = RecordId<TLPointer>
/** @internal */
/** @public */
export const pointerValidator: T.Validator<TLPointer> = T.model(
'pointer',
T.object({
@ -37,12 +37,12 @@ export const pointerValidator: T.Validator<TLPointer> = T.model(
})
)
/** @internal */
/** @public */
export const pointerVersions = createMigrationIds('com.tldraw.pointer', {
AddMeta: 1,
})
/** @internal */
/** @public */
export const pointerMigrations = createRecordMigrationSequence({
sequenceId: 'com.tldraw.pointer',
recordType: 'pointer',

View file

@ -40,7 +40,7 @@ export interface TLInstancePresence extends BaseRecord<'instance_presence', TLIn
/** @public */
export type TLInstancePresenceID = RecordId<TLInstancePresence>
/** @internal */
/** @public */
export const instancePresenceValidator: T.Validator<TLInstancePresence> = T.model(
'instance_presence',
T.object({
@ -72,7 +72,7 @@ export const instancePresenceValidator: T.Validator<TLInstancePresence> = T.mode
})
)
/** @internal */
/** @public */
export const instancePresenceVersions = createMigrationIds('com.tldraw.instance_presence', {
AddScribbleDelay: 1,
RemoveInstanceId: 2,

View file

@ -92,7 +92,7 @@ export type TLShapeProp = keyof TLShapeProps
/** @public */
export type TLParentId = TLPageId | TLShapeId
/** @internal */
/** @public */
export const rootShapeVersions = createMigrationIds('com.tldraw.shape', {
AddIsLocked: 1,
HoistOpacity: 2,
@ -100,7 +100,7 @@ export const rootShapeVersions = createMigrationIds('com.tldraw.shape', {
AddWhite: 4,
} as const)
/** @internal */
/** @public */
export const rootShapeMigrations = createRecordMigrationSequence({
sequenceId: 'com.tldraw.shape',
recordType: 'shape',

View file

@ -88,7 +88,7 @@ export const arrowShapeVersions = createShapePropsMigrationIds('arrow', {
AddLabelPosition: 3,
})
/** @internal */
/** @public */
export const arrowShapeMigrations = createShapePropsMigrationSequence({
sequence: [
{

View file

@ -28,7 +28,7 @@ const Versions = createShapePropsMigrationIds('bookmark', {
export { Versions as bookmarkShapeVersions }
/** @internal */
/** @public */
export const bookmarkShapeMigrations = createShapePropsMigrationSequence({
sequence: [
{

View file

@ -43,7 +43,7 @@ const Versions = createShapePropsMigrationIds('draw', {
export { Versions as drawShapeVersions }
/** @internal */
/** @public */
export const drawShapeMigrations = createShapePropsMigrationSequence({
sequence: [
{

View file

@ -625,7 +625,7 @@ const Versions = createShapePropsMigrationIds('embed', {
export { Versions as embedShapeVersions }
/** @internal */
/** @public */
export const embedShapeMigrations = createShapePropsMigrationSequence({
sequence: [
{

View file

@ -14,7 +14,7 @@ type TLFrameShapeProps = ShapePropsType<typeof frameShapeProps>
/** @public */
export type TLFrameShape = TLBaseShape<'frame', TLFrameShapeProps>
/** @internal */
/** @public */
export const frameShapeMigrations = createShapePropsMigrationSequence({
sequence: [],
})

View file

@ -83,7 +83,7 @@ const geoShapeVersions = createShapePropsMigrationIds('geo', {
export { geoShapeVersions as geoShapeVersions }
/** @internal */
/** @public */
export const geoShapeMigrations = createShapePropsMigrationSequence({
sequence: [
{

View file

@ -7,8 +7,8 @@ export type TLGroupShapeProps = { [key in never]: undefined }
/** @public */
export type TLGroupShape = TLBaseShape<'group', TLGroupShapeProps>
/** @internal */
/** @public */
export const groupShapeProps: ShapeProps<TLGroupShape> = {}
/** @internal */
/** @public */
export const groupShapeMigrations = createShapePropsMigrationSequence({ sequence: [] })

View file

@ -20,5 +20,5 @@ export type TLHighlightShapeProps = ShapePropsType<typeof highlightShapeProps>
/** @public */
export type TLHighlightShape = TLBaseShape<'highlight', TLHighlightShapeProps>
/** @internal */
/** @public */
export const highlightShapeMigrations = createShapePropsMigrationSequence({ sequence: [] })

View file

@ -40,7 +40,7 @@ const Versions = createShapePropsMigrationIds('image', {
export { Versions as imageShapeVersions }
/** @internal */
/** @public */
export const imageShapeMigrations = createShapePropsMigrationSequence({
sequence: [
{

View file

@ -42,7 +42,7 @@ export type TLLineShapeProps = ShapePropsType<typeof lineShapeProps>
/** @public */
export type TLLineShape = TLBaseShape<'line', TLLineShapeProps>
/** @internal */
/** @public */
export const lineShapeVersions = createShapePropsMigrationIds('line', {
AddSnapHandles: 1,
RemoveExtraHandleProps: 2,
@ -50,7 +50,7 @@ export const lineShapeVersions = createShapePropsMigrationIds('line', {
PointIndexIds: 4,
})
/** @internal */
/** @public */
export const lineShapeMigrations = createShapePropsMigrationSequence({
sequence: [
{

View file

@ -41,7 +41,7 @@ const Versions = createShapePropsMigrationIds('note', {
export { Versions as noteShapeVersions }
/** @internal */
/** @public */
export const noteShapeMigrations = createShapePropsMigrationSequence({
sequence: [
{

View file

@ -34,7 +34,7 @@ const Versions = createShapePropsMigrationIds('text', {
export { Versions as textShapeVersions }
/** @internal */
/** @public */
export const textShapeMigrations = createShapePropsMigrationSequence({
sequence: [
{

View file

@ -30,7 +30,7 @@ const Versions = createShapePropsMigrationIds('video', {
export { Versions as videoShapeVersions }
/** @internal */
/** @public */
export const videoShapeMigrations = createShapePropsMigrationSequence({
sequence: [
{

View file

@ -1,86 +1,5 @@
import {
arrowShapeMigrations,
arrowShapeProps,
bookmarkShapeMigrations,
bookmarkShapeProps,
createTLSchema,
drawShapeMigrations,
drawShapeProps,
embedShapeMigrations,
embedShapeProps,
frameShapeMigrations,
frameShapeProps,
geoShapeMigrations,
geoShapeProps,
groupShapeMigrations,
groupShapeProps,
highlightShapeMigrations,
highlightShapeProps,
imageShapeMigrations,
imageShapeProps,
lineShapeMigrations,
lineShapeProps,
noteShapeMigrations,
noteShapeProps,
textShapeMigrations,
textShapeProps,
videoShapeMigrations,
videoShapeProps,
} from '@tldraw/tlschema'
import { createTLSchema, defaultShapeSchemas } from '@tldraw/tlschema'
export const schema = createTLSchema({
shapes: {
group: {
props: groupShapeProps,
migrations: groupShapeMigrations,
},
text: {
props: textShapeProps,
migrations: textShapeMigrations,
},
bookmark: {
props: bookmarkShapeProps,
migrations: bookmarkShapeMigrations,
},
draw: {
props: drawShapeProps,
migrations: drawShapeMigrations,
},
geo: {
props: geoShapeProps,
migrations: geoShapeMigrations,
},
note: {
props: noteShapeProps,
migrations: noteShapeMigrations,
},
line: {
props: lineShapeProps,
migrations: lineShapeMigrations,
},
frame: {
props: frameShapeProps,
migrations: frameShapeMigrations,
},
arrow: {
props: arrowShapeProps,
migrations: arrowShapeMigrations,
},
highlight: {
props: highlightShapeProps,
migrations: highlightShapeMigrations,
},
embed: {
props: embedShapeProps,
migrations: embedShapeMigrations,
},
image: {
props: imageShapeProps,
migrations: imageShapeMigrations,
},
video: {
props: videoShapeProps,
migrations: videoShapeMigrations,
},
},
shapes: defaultShapeSchemas,
})