@mocanvas/editor · class

AssetUtil

declare abstract class AssetUtil<A extends Asset = Asset>

Describes how one kind of asset behaves. One instance per asset type per editor.

Only type and getDefaultProps are required. A util that just needs the editor to accept a new file type is a dozen lines.


Methods · 7

acceptsMimeType(mimeType: string): boolean

Whether this util will take a file of this MIME type.

Matching is case-insensitive, and a type/* entry matches every subtype — browsers report image/jpeg and image/jpg for the same file, and a util should not have to enumerate the ways a platform spells a format.

static configure<C extends TLAssetUtilClass>(this: C, options: Partial<TLAssetUtilOptions>): C

A subclass of this util with options merged in.

The point is to let an app change a util's behaviour without subclassing it: MyAssetUtil.configure({ maxSize: 1e7 }) is a new class, so two editors on one page can run the same util configured differently.

constructor(editor: Editor)
getAssetFromFile(_file: File, _assetId: AssetId): Promise<A | null>

Turn a file into an asset record, or null when this util cannot.

Returning null rather than throwing is deliberate: a drop of ten files where one is unreadable should create nine assets, not fail. The default answers null, which is right for a util whose assets are not made from files at all — a bookmark is made from a URL.

abstract getDefaultProps(): A["props"]

What an asset of this type starts out as.

getSupportedMimeTypes(): readonly string[]

The MIME types this util can build an asset from.

Empty by default: a util that only *resolves* assets created elsewhere should not claim any file type, and claiming one it cannot handle would shadow the util that can.

get type(): A["type"]

The asset type this util handles, read off the constructor.


Properties · 5

readonly editor: Editor
static migrations?: unknown

The migration sequence for this asset type's props.

readonly options: TLAssetUtilOptions

This util's settings; see AssetUtil.configure.

static props?: UnknownRecordProps

Validators for this asset type's props, so the store rejects a malformed asset on write rather than failing to paint it later.

static type: string

The Asset["type"] this util handles. Set it on the subclass.

mocanvas is built by Symbio. Free for non-commercial, non-profit and development use; commercial use needs an agreement. Not affiliated with or endorsed by tldraw.