@mocanvas/editor · class
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): booleanWhether 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>): CA 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: Editorstatic migrations?: unknownThe migration sequence for this asset type's props.
readonly options: TLAssetUtilOptionsThis util's settings; see AssetUtil.configure.
static props?: UnknownRecordPropsValidators for this asset type's props, so the store rejects a malformed
asset on write rather than failing to paint it later.
static type: stringThe 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.