[refactor] remove createTLSchema (#1440)

This PR removes `createTLSchema`, now that the default shapes / utils
are in the editor's `TldrawEditorConfig`. There were a few places that
still called `createTLSchema`, though they would have been missing many
parts of the

### Change Type

- [x] `major` — Breaking Change

### Test Plan

- [x] Unit Tests

### Release Notes

- [tlschema] Removes `createTLSchema` in favor of `TldrawEditorConfig`
This commit is contained in:
Steve Ruiz 2023-05-23 14:35:11 +01:00 committed by GitHub
parent 649125cdad
commit abe9237c6e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 63 additions and 116 deletions

View file

@ -30,7 +30,6 @@ import { MatLike } from '@tldraw/primitives';
import { Matrix2d } from '@tldraw/primitives';
import { Matrix2dModel } from '@tldraw/primitives';
import { Migrations } from '@tldraw/tlstore';
import { MigrationsForShapes } from '@tldraw/tlschema';
import { Polyline2d } from '@tldraw/primitives';
import * as React_2 from 'react';
import { default as React_3 } from 'react';
@ -45,6 +44,7 @@ import { sortByIndex } from '@tldraw/indices';
import { StoreSchema } from '@tldraw/tlstore';
import { StoreSnapshot } from '@tldraw/tlstore';
import { StrokePoint } from '@tldraw/primitives';
import { T } from '@tldraw/tlvalidate';
import { TLAlignType } from '@tldraw/tlschema';
import { TLArrowheadType } from '@tldraw/tlschema';
import { TLArrowShape } from '@tldraw/tlschema';
@ -103,7 +103,6 @@ import { TLUserId } from '@tldraw/tlschema';
import { TLUserPresence } from '@tldraw/tlschema';
import { TLVideoAsset } from '@tldraw/tlschema';
import { TLVideoShape } from '@tldraw/tlschema';
import { ValidatorsForShapes } from '@tldraw/tlschema';
import { Vec2d } from '@tldraw/primitives';
import { Vec2dModel } from '@tldraw/tlschema';
import { VecLike } from '@tldraw/primitives';
@ -1807,7 +1806,7 @@ export class TldrawEditorConfig {
// (undocumented)
readonly shapeUtils: UtilsForShapes<TLShape>;
// (undocumented)
readonly shapeValidators: ValidatorsForShapes<TLShape>;
readonly shapeValidators: Record<TLShape['type'], T.Validator<any>>;
// (undocumented)
readonly storeSchema: StoreSchema<TLRecord, TLStoreProps>;
// (undocumented)

View file

@ -1,23 +1,29 @@
import {
CLIENT_FIXUP_SCRIPT,
MigrationsForShapes,
TLAsset,
TLCamera,
TLDOCUMENT_ID,
TLDocument,
TLInstance,
TLInstanceId,
TLInstancePageState,
TLInstancePresence,
TLPage,
TLRecord,
TLShape,
TLStore,
TLStoreProps,
TLUnknownShape,
TLUser,
TLUserDocument,
TLUserId,
ValidatorsForShapes,
TLUserPresence,
arrowShapeTypeMigrations,
arrowShapeTypeValidator,
bookmarkShapeTypeMigrations,
bookmarkShapeTypeValidator,
createTLSchema,
createIntegrityChecker,
defaultDerivePresenceState,
drawShapeTypeMigrations,
drawShapeTypeValidator,
embedShapeTypeMigrations,
@ -34,6 +40,9 @@ import {
lineShapeTypeValidator,
noteShapeTypeMigrations,
noteShapeTypeValidator,
onValidationFailure,
rootShapeTypeMigrations,
storeMigrations,
textShapeTypeMigrations,
textShapeTypeValidator,
videoShapeTypeMigrations,
@ -45,6 +54,7 @@ import {
Store,
StoreSchema,
StoreSnapshot,
createRecordType,
defineMigrations,
} from '@tldraw/tlstore'
import { T } from '@tldraw/tlvalidate'
@ -64,6 +74,15 @@ import { TLTextUtil } from '../app/shapeutils/TLTextUtil/TLTextUtil'
import { TLVideoUtil } from '../app/shapeutils/TLVideoUtil/TLVideoUtil'
import { StateNodeConstructor } from '../app/statechart/StateNode'
/** @public */
export type ValidatorsForShapes<T extends TLUnknownShape> = Record<
T['type'],
{ validate: (record: T) => T }
>
/** @public */
export type MigrationsForShapes<T extends TLUnknownShape> = Record<T['type'], Migrations>
type CustomShapeInfo<T extends TLUnknownShape> = {
util: TLShapeUtilConstructor<any>
validator?: { validate: (record: T) => T }
@ -87,8 +106,11 @@ export class TldrawEditorConfig {
readonly TLShape: RecordType<TLShape, 'type' | 'props' | 'index' | 'parentId'>
readonly tools: readonly StateNodeConstructor[]
// Custom shape utils
readonly shapeUtils: UtilsForShapes<TLShape>
readonly shapeValidators: ValidatorsForShapes<TLShape>
// Validators for shape subtypes
readonly shapeValidators: Record<TLShape['type'], T.Validator<any>>
// Migrations for shape subtypes
readonly shapeMigrations: MigrationsForShapes<TLShape>
constructor(opts: TldrawEditorConfigOptions) {
@ -141,17 +163,46 @@ export class TldrawEditorConfig {
video: videoShapeTypeValidator,
}
// Add custom shapes
for (const [type, shape] of Object.entries(shapes)) {
this.shapeUtils[type] = shape.util
this.shapeMigrations[type] = shape.migrations ?? defineMigrations({})
this.shapeValidators[type] = shape.validator ?? T.any
this.shapeValidators[type] = (shape.validator ?? T.any) as T.Validator<any>
}
this.storeSchema = createTLSchema({
shapeMigrations: this.shapeMigrations,
shapeValidators: this.shapeValidators,
derivePresenceState,
})
const shapeRecord = createRecordType<TLShape>('shape', {
migrations: defineMigrations({
currentVersion: rootShapeTypeMigrations.currentVersion,
firstVersion: rootShapeTypeMigrations.firstVersion,
migrators: rootShapeTypeMigrations.migrators,
subTypeKey: 'type',
subTypeMigrations: this.shapeMigrations,
}),
validator: T.model('shape', T.union('type', { ...this.shapeValidators })),
scope: 'document',
}).withDefaultProperties(() => ({ x: 0, y: 0, rotation: 0, isLocked: false }))
this.storeSchema = StoreSchema.create<TLRecord, TLStoreProps>(
{
asset: TLAsset,
camera: TLCamera,
document: TLDocument,
instance: TLInstance,
instance_page_state: TLInstancePageState,
page: TLPage,
shape: shapeRecord,
user: TLUser,
user_document: TLUserDocument,
user_presence: TLUserPresence,
instance_presence: TLInstancePresence,
},
{
snapshotMigrations: storeMigrations,
onValidationFailure,
createIntegrityChecker: createIntegrityChecker,
derivePresenceState: derivePresenceState ?? defaultDerivePresenceState,
}
)
this.TLShape = this.storeSchema.types.shape as RecordType<
TLShape,

View file

@ -103,13 +103,6 @@ export function createShapeValidator<Type extends string, Props extends object>(
props: Props;
}>;
// @public (undocumented)
export function createTLSchema({ shapeMigrations, shapeValidators, derivePresenceState, }: {
shapeValidators: ValidatorsForShapes<TLShape>;
shapeMigrations: MigrationsForShapes<TLShape>;
derivePresenceState?: (store: TLStore) => Signal<null | TLInstancePresence>;
}): StoreSchema<TLRecord, TLStoreProps>;
// @public (undocumented)
export const cursorTypeValidator: T.Validator<string>;
@ -411,9 +404,6 @@ export const lineShapeTypeMigrations: Migrations;
// @public (undocumented)
export const lineShapeTypeValidator: T.Validator<TLLineShape>;
// @public (undocumented)
export type MigrationsForShapes<T extends TLUnknownShape> = Record<T['type'], Migrations>;
// @public (undocumented)
export const noteShapeTypeMigrations: Migrations;
@ -1378,11 +1368,6 @@ export const userPresenceTypeValidator: T.Validator<TLUserPresence>;
// @public (undocumented)
export const userTypeValidator: T.Validator<TLUser>;
// @public (undocumented)
export type ValidatorsForShapes<T extends TLUnknownShape> = Record<T['type'], {
validate: (record: T) => T;
}>;
// @public (undocumented)
export interface Vec2dModel {
// (undocumented)

View file

@ -1,83 +0,0 @@
import { Migrations, StoreSchema, createRecordType, defineMigrations } from '@tldraw/tlstore'
import { T } from '@tldraw/tlvalidate'
import { Signal } from 'signia'
import { TLRecord } from './TLRecord'
import { TLStore, TLStoreProps, createIntegrityChecker, onValidationFailure } from './TLStore'
import { defaultDerivePresenceState } from './defaultDerivePresenceState'
import { TLAsset } from './records/TLAsset'
import { TLCamera } from './records/TLCamera'
import { TLDocument } from './records/TLDocument'
import { TLInstance } from './records/TLInstance'
import { TLInstancePageState } from './records/TLInstancePageState'
import { TLInstancePresence } from './records/TLInstancePresence'
import { TLPage } from './records/TLPage'
import { TLShape, TLUnknownShape, rootShapeTypeMigrations } from './records/TLShape'
import { TLUser } from './records/TLUser'
import { TLUserDocument } from './records/TLUserDocument'
import { TLUserPresence } from './records/TLUserPresence'
import { storeMigrations } from './schema'
/** @public */
export type ValidatorsForShapes<T extends TLUnknownShape> = Record<
T['type'],
{ validate: (record: T) => T }
>
/** @public */
export type MigrationsForShapes<T extends TLUnknownShape> = Record<T['type'], Migrations>
/** @public */
export function createTLSchema({
shapeMigrations,
shapeValidators,
derivePresenceState,
}: {
shapeValidators: ValidatorsForShapes<TLShape>
shapeMigrations: MigrationsForShapes<TLShape>
derivePresenceState?: (store: TLStore) => Signal<TLInstancePresence | null>
}) {
// Removed check to see whether a shape type has already been defined
const shapeTypeMigrations = defineMigrations({
currentVersion: rootShapeTypeMigrations.currentVersion,
firstVersion: rootShapeTypeMigrations.firstVersion,
migrators: rootShapeTypeMigrations.migrators,
subTypeKey: 'type',
subTypeMigrations: Object.fromEntries(
Object.entries(shapeMigrations) as [TLShape['type'], Migrations][]
),
})
const shapeTypeValidator = T.union(
'type',
Object.fromEntries(Object.entries(shapeValidators) as [TLShape['type'], T.Validator<any>][])
)
const shapeRecord = createRecordType<TLShape>('shape', {
migrations: shapeTypeMigrations,
validator: T.model('shape', shapeTypeValidator),
scope: 'document',
}).withDefaultProperties(() => ({ x: 0, y: 0, rotation: 0, isLocked: false }))
return StoreSchema.create<TLRecord, TLStoreProps>(
{
asset: TLAsset,
camera: TLCamera,
document: TLDocument,
instance: TLInstance,
instance_page_state: TLInstancePageState,
page: TLPage,
shape: shapeRecord,
user: TLUser,
user_document: TLUserDocument,
user_presence: TLUserPresence,
instance_presence: TLInstancePresence,
},
{
snapshotMigrations: storeMigrations,
onValidationFailure,
createIntegrityChecker: createIntegrityChecker,
derivePresenceState: derivePresenceState ?? defaultDerivePresenceState,
}
)
}

View file

@ -24,11 +24,6 @@ export {
type TLVideoAsset,
} from './assets/TLVideoAsset'
export { createAssetValidator, type TLBaseAsset } from './assets/asset-validation'
export {
createTLSchema,
type MigrationsForShapes,
type ValidatorsForShapes,
} from './createTLSchema'
export { defaultDerivePresenceState } from './defaultDerivePresenceState'
export { CLIENT_FIXUP_SCRIPT, fixupRecord } from './fixup'
export { type Box2dModel, type Vec2dModel } from './geometry-types'