UNPKG

@tldraw/editor

Version:

tldraw infinite canvas SDK (editor).

8 lines (7 loc) 3.25 kB
{ "version": 3, "sources": ["../../../../src/lib/editor/assets/AssetUtil.ts"], "sourcesContent": ["import { LegacyMigrations, MigrationSequence } from '@tldraw/store'\nimport {\n\tRecordProps,\n\tTLAsset,\n\tTLAssetId,\n\tTLPropsMigrations,\n\tTLUnknownAsset,\n} from '@tldraw/tlschema'\nimport type { Editor } from '../Editor'\n\n/** @public */\nexport interface TLAssetUtilConstructor<\n\tT extends TLAsset = TLAsset,\n\tU extends AssetUtil<T> = AssetUtil<T>,\n> {\n\tnew (editor: Editor): U\n\ttype: T['type']\n\tprops?: RecordProps<T>\n\tmigrations?: LegacyMigrations | TLPropsMigrations | MigrationSequence\n}\n\n/**\n * Abstract base class for defining asset-type-specific behavior.\n *\n * Each asset type (image, video, bookmark, etc.) has a corresponding AssetUtil that handles\n * type-specific operations like determining supported MIME types and creating assets from files.\n *\n * @public\n */\nexport abstract class AssetUtil<Asset extends TLAsset = TLAsset> {\n\t/** Configure this asset util's {@link AssetUtil.options | `options`}. */\n\tstatic configure<T extends TLAssetUtilConstructor<any, any>>(\n\t\tthis: T,\n\t\toptions: T extends new (...args: any[]) => { options: infer Options } ? Partial<Options> : never\n\t): T {\n\t\t// @ts-expect-error -- typescript has no idea what's going on here but it's fine\n\t\treturn class extends this {\n\t\t\t// @ts-expect-error\n\t\t\toptions = { ...this.options, ...options }\n\t\t}\n\t}\n\n\tconstructor(public editor: Editor) {}\n\n\t/**\n\t * Options for this asset util. Override this to provide customization options for your asset.\n\t * Use {@link AssetUtil.configure} to customize existing asset utils.\n\t */\n\toptions = {}\n\n\tstatic props?: RecordProps<TLUnknownAsset>\n\tstatic migrations?: LegacyMigrations | TLPropsMigrations | MigrationSequence\n\n\t/**\n\t * The type of the asset util, which should match the asset's type.\n\t */\n\tstatic type: string\n\n\t/**\n\t * Get the default props for an asset of this type.\n\t */\n\tabstract getDefaultProps(): Asset['props']\n\n\t/**\n\t * Get the MIME types that this asset type supports.\n\t * Return an empty array if this asset type doesn't support files (e.g. bookmarks).\n\t */\n\tgetSupportedMimeTypes(): readonly string[] {\n\t\treturn []\n\t}\n\n\t/**\n\t * Check whether this asset type accepts a given MIME type.\n\t */\n\tacceptsMimeType(mimeType: string): boolean {\n\t\treturn this.getSupportedMimeTypes().includes(mimeType)\n\t}\n\n\t/**\n\t * Create an asset from a file. Return null if this asset type can't handle the file.\n\t */\n\tasync getAssetFromFile(_file: File, _assetId: TLAssetId): Promise<Asset | null> {\n\t\treturn null\n\t}\n}\n"], "mappings": "AA6BO,MAAe,UAA2C;AAAA,EAahE,YAAmB,QAAgB;AAAhB;AAAA,EAAiB;AAAA,EAAjB;AAAA;AAAA,EAXnB,OAAO,UAEN,SACI;AAEJ,WAAO,cAAc,KAAK;AAAA;AAAA,MAEzB,UAAU,EAAE,GAAG,KAAK,SAAS,GAAG,QAAQ;AAAA,IACzC;AAAA,EACD;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,UAAU,CAAC;AAAA,EAEX,OAAO;AAAA,EACP,OAAO;AAAA;AAAA;AAAA;AAAA,EAKP,OAAO;AAAA;AAAA;AAAA;AAAA;AAAA,EAWP,wBAA2C;AAC1C,WAAO,CAAC;AAAA,EACT;AAAA;AAAA;AAAA;AAAA,EAKA,gBAAgB,UAA2B;AAC1C,WAAO,KAAK,sBAAsB,EAAE,SAAS,QAAQ;AAAA,EACtD;AAAA;AAAA;AAAA;AAAA,EAKA,MAAM,iBAAiB,OAAa,UAA4C;AAC/E,WAAO;AAAA,EACR;AACD;", "names": [] }