UNPKG

@tldraw/tlschema

Version:

tldraw infinite canvas SDK (schema).

8 lines (7 loc) 4.63 kB
{ "version": 3, "sources": ["../../src/assets/TLBaseAsset.ts"], "sourcesContent": ["import { BaseRecord } from '@tldraw/store'\nimport { JsonObject } from '@tldraw/utils'\nimport { T } from '@tldraw/validate'\nimport { idValidator } from '../misc/id-validator'\nimport { TLAssetId } from '../records/TLAsset'\n\n/**\n * Base interface for all asset records in tldraw. Assets represent external resources\n * like images, videos, or bookmarks that shapes can reference. This interface extends\n * the base record system with asset-specific typing.\n *\n * @param Type - The specific asset type identifier (e.g., 'image', 'video', 'bookmark')\n * @param Props - The properties object specific to this asset type\n *\n * @example\n * ```ts\n * // Define a custom asset type\n * interface MyCustomAsset extends TLBaseAsset<'custom', { url: string; title: string }> {}\n *\n * const customAsset: MyCustomAsset = {\n * id: 'asset:custom123',\n * typeName: 'asset',\n * type: 'custom',\n * props: {\n * url: 'https://example.com',\n * title: 'My Custom Asset'\n * },\n * meta: {}\n * }\n * ```\n *\n * @public\n */\nexport interface TLBaseAsset<Type extends string, Props> extends BaseRecord<'asset', TLAssetId> {\n\t/** The specific type of this asset (e.g., 'image', 'video', 'bookmark') */\n\ttype: Type\n\t/** Type-specific properties for this asset */\n\tprops: Props\n\t/** User-defined metadata that can be attached to this asset */\n\tmeta: JsonObject\n}\n\n/**\n * A validator for asset record type IDs. This validator ensures that asset IDs\n * follow the correct format and type structure required by tldraw's asset system.\n * Asset IDs are prefixed with 'asset:' followed by a unique identifier.\n *\n * @example\n * ```ts\n * import { assetIdValidator } from '@tldraw/tlschema'\n *\n * // Valid asset ID\n * const validId = 'asset:abc123'\n * console.log(assetIdValidator.isValid(validId)) // true\n *\n * // Invalid asset ID\n * const invalidId = 'shape:abc123'\n * console.log(assetIdValidator.isValid(invalidId)) // false\n * ```\n *\n * @public\n */\nexport const assetIdValidator = idValidator<TLAssetId>('asset')\n\n/**\n * Creates a validator for a specific asset record type. This factory function generates\n * a complete validator that validates the entire asset record structure including the\n * base properties (id, typeName, type, meta) and the type-specific props.\n *\n * @param type - The asset type identifier (e.g., 'image', 'video', 'bookmark')\n * @param props - A validator or per-key validator record for the asset's type-specific properties\n * @param meta - An optional per-key validator record for the asset's meta properties\n * @returns A complete validator for the asset record type\n *\n * @example\n * ```ts\n * import { createAssetValidator, TLBaseAsset } from '@tldraw/tlschema'\n * import { T } from '@tldraw/validate'\n *\n * // Define a custom asset type\n * type TLCustomAsset = TLBaseAsset<'custom', {\n * url: string\n * title: string\n * description?: string\n * }>\n *\n * // Create validator using a per-key record (recommended)\n * const customAssetValidator = createAssetValidator('custom', {\n * url: T.string,\n * title: T.string,\n * description: T.string.optional()\n * })\n *\n * // Or using a T.object validator\n * const customAssetValidator2 = createAssetValidator('custom', T.object({\n * url: T.string,\n * title: T.string,\n * description: T.string.optional()\n * }))\n * ```\n *\n * @public\n */\nexport function createAssetValidator<\n\tType extends string,\n\tProps extends JsonObject,\n\tMeta extends JsonObject = JsonObject,\n>(\n\ttype: Type,\n\tprops?: T.Validator<Props> | { [K in keyof Props]: T.Validatable<Props[K]> },\n\tmeta?: { [K in keyof Meta]: T.Validatable<Meta[K]> }\n) {\n\t// Determine if props is a Validator instance or a per-key record\n\tconst propsValidator =\n\t\tprops instanceof T.Validator ? props : props ? T.object(props) : (T.jsonValue as any)\n\n\treturn T.object<TLBaseAsset<Type, Props>>({\n\t\tid: assetIdValidator,\n\t\ttypeName: T.literal('asset'),\n\t\ttype: T.literal(type),\n\t\tprops: propsValidator,\n\t\tmeta: meta ? T.object(meta) : (T.jsonValue as T.ObjectValidator<JsonObject>),\n\t})\n}\n"], "mappings": "AAEA,SAAS,SAAS;AAClB,SAAS,mBAAmB;AA2DrB,MAAM,mBAAmB,YAAuB,OAAO;AAyCvD,SAAS,qBAKf,MACA,OACA,MACC;AAED,QAAM,iBACL,iBAAiB,EAAE,YAAY,QAAQ,QAAQ,EAAE,OAAO,KAAK,IAAK,EAAE;AAErE,SAAO,EAAE,OAAiC;AAAA,IACzC,IAAI;AAAA,IACJ,UAAU,EAAE,QAAQ,OAAO;AAAA,IAC3B,MAAM,EAAE,QAAQ,IAAI;AAAA,IACpB,OAAO;AAAA,IACP,MAAM,OAAO,EAAE,OAAO,IAAI,IAAK,EAAE;AAAA,EAClC,CAAC;AACF;", "names": [] }