UNPKG

@tldraw/tlschema

Version:

tldraw infinite canvas SDK (schema).

160 lines (159 loc) • 6.96 kB
import { StoreSchema } from "@tldraw/store"; import { objectMapValues } from "@tldraw/utils"; import { bookmarkAssetMigrations, bookmarkAssetProps } from "./assets/TLBookmarkAsset.mjs"; import { imageAssetMigrations, imageAssetProps } from "./assets/TLImageAsset.mjs"; import { videoAssetMigrations, videoAssetProps } from "./assets/TLVideoAsset.mjs"; import { arrowBindingMigrations, arrowBindingProps } from "./bindings/TLArrowBinding.mjs"; import { assetMigrations, createAssetRecordType } from "./records/TLAsset.mjs"; import { createBindingRecordType } from "./records/TLBinding.mjs"; import { CameraRecordType, cameraMigrations } from "./records/TLCamera.mjs"; import { createCustomRecordType, processCustomRecordMigrations } from "./records/TLCustomRecord.mjs"; import { DocumentRecordType, documentMigrations } from "./records/TLDocument.mjs"; import { createInstanceRecordType, instanceMigrations } from "./records/TLInstance.mjs"; import { PageRecordType, pageMigrations } from "./records/TLPage.mjs"; import { InstancePageStateRecordType, instancePageStateMigrations } from "./records/TLPageState.mjs"; import { PointerRecordType, pointerMigrations } from "./records/TLPointer.mjs"; import { InstancePresenceRecordType, instancePresenceMigrations } from "./records/TLPresence.mjs"; import { createShapeRecordType, getShapePropKeysByStyle, rootShapeMigrations } from "./records/TLShape.mjs"; import { UserRecordType, createUserRecordType, userMigrations } from "./records/TLUser.mjs"; import { processPropsMigrations } from "./recordsWithProps.mjs"; import { arrowShapeMigrations, arrowShapeProps } from "./shapes/TLArrowShape.mjs"; import { bookmarkShapeMigrations, bookmarkShapeProps } from "./shapes/TLBookmarkShape.mjs"; import { drawShapeMigrations, drawShapeProps } from "./shapes/TLDrawShape.mjs"; import { embedShapeMigrations, embedShapeProps } from "./shapes/TLEmbedShape.mjs"; import { frameShapeMigrations, frameShapeProps } from "./shapes/TLFrameShape.mjs"; import { geoShapeMigrations, geoShapeProps } from "./shapes/TLGeoShape.mjs"; import { groupShapeMigrations, groupShapeProps } from "./shapes/TLGroupShape.mjs"; import { highlightShapeMigrations, highlightShapeProps } from "./shapes/TLHighlightShape.mjs"; import { imageShapeMigrations, imageShapeProps } from "./shapes/TLImageShape.mjs"; import { lineShapeMigrations, lineShapeProps } from "./shapes/TLLineShape.mjs"; import { noteShapeMigrations, noteShapeProps } from "./shapes/TLNoteShape.mjs"; import { textShapeMigrations, textShapeProps } from "./shapes/TLTextShape.mjs"; import { videoShapeMigrations, videoShapeProps } from "./shapes/TLVideoShape.mjs"; import { storeMigrations } from "./store-migrations.mjs"; import { createIntegrityChecker, onValidationFailure } from "./TLStore.mjs"; const defaultShapeSchemas = { arrow: { migrations: arrowShapeMigrations, props: arrowShapeProps }, bookmark: { migrations: bookmarkShapeMigrations, props: bookmarkShapeProps }, draw: { migrations: drawShapeMigrations, props: drawShapeProps }, embed: { migrations: embedShapeMigrations, props: embedShapeProps }, frame: { migrations: frameShapeMigrations, props: frameShapeProps }, geo: { migrations: geoShapeMigrations, props: geoShapeProps }, group: { migrations: groupShapeMigrations, props: groupShapeProps }, highlight: { migrations: highlightShapeMigrations, props: highlightShapeProps }, image: { migrations: imageShapeMigrations, props: imageShapeProps }, line: { migrations: lineShapeMigrations, props: lineShapeProps }, note: { migrations: noteShapeMigrations, props: noteShapeProps }, text: { migrations: textShapeMigrations, props: textShapeProps }, video: { migrations: videoShapeMigrations, props: videoShapeProps } }; const defaultBindingSchemas = { arrow: { migrations: arrowBindingMigrations, props: arrowBindingProps } }; const defaultAssetSchemas = { image: { migrations: imageAssetMigrations, props: imageAssetProps }, video: { migrations: videoAssetMigrations, props: videoAssetProps }, bookmark: { migrations: bookmarkAssetMigrations, props: bookmarkAssetProps } }; function createTLSchema({ shapes = defaultShapeSchemas, bindings = defaultBindingSchemas, assets = defaultAssetSchemas, user, records = {}, migrations } = {}) { const stylesById = /* @__PURE__ */ new Map(); for (const shape of objectMapValues(shapes)) { for (const style of getShapePropKeysByStyle(shape.props ?? {}).keys()) { if (stylesById.has(style.id) && stylesById.get(style.id) !== style) { throw new Error(`Multiple StyleProp instances with the same id: ${style.id}`); } stylesById.set(style.id, style); } } const ShapeRecordType = createShapeRecordType(shapes); const BindingRecordType = createBindingRecordType(bindings); const _AssetRecordType = createAssetRecordType(assets); const InstanceRecordType = createInstanceRecordType(stylesById); const CustomUserRecordType = user ? createUserRecordType(user) : UserRecordType; const builtInTypeNames = /* @__PURE__ */ new Set([ "asset", "binding", "camera", "document", "instance", "instance_page_state", "page", "instance_presence", "pointer", "shape", "store", "user" ]); const customRecordTypes = {}; for (const [typeName, config] of Object.entries(records)) { if (builtInTypeNames.has(typeName)) { throw new Error( `Custom record type name '${typeName}' conflicts with tldraw's built-in record type of that name. Choose a different name instead.` ); } customRecordTypes[typeName] = createCustomRecordType(typeName, config); } return StoreSchema.create( { asset: _AssetRecordType, binding: BindingRecordType, camera: CameraRecordType, document: DocumentRecordType, instance: InstanceRecordType, instance_page_state: InstancePageStateRecordType, page: PageRecordType, instance_presence: InstancePresenceRecordType, pointer: PointerRecordType, shape: ShapeRecordType, user: CustomUserRecordType, ...customRecordTypes }, { migrations: [ storeMigrations, assetMigrations, cameraMigrations, documentMigrations, instanceMigrations, instancePageStateMigrations, pageMigrations, instancePresenceMigrations, pointerMigrations, rootShapeMigrations, userMigrations, ...processPropsMigrations("asset", assets), ...processPropsMigrations("shape", shapes), ...processPropsMigrations("binding", bindings), ...processCustomRecordMigrations(records), ...(user?.migrations ?? []), ...(migrations ?? []) ], onValidationFailure, createIntegrityChecker } ); } export { createTLSchema, defaultAssetSchemas, defaultBindingSchemas, defaultShapeSchemas }; //# sourceMappingURL=createTLSchema.mjs.map