Tldraw/packages/tlschema/src/assets/TLImageAsset.ts

72 wiersze
1.5 KiB
TypeScript

import { defineMigrations } from '@tldraw/tlstore'
import { T } from '@tldraw/tlvalidate'
import { createAssetValidator, TLBaseAsset } from './asset-validation'
// --- DEFINITION ---
/** @public */
export type TLImageAsset = TLBaseAsset<
'image',
{
w: number
h: number
name: string
isAnimated: boolean
mimeType: string | null
src: string | null
}
>
/** @public */
export const imageAssetTypeValidator: T.Validator<TLImageAsset> = createAssetValidator(
'image',
T.object({
w: T.number,
h: T.number,
name: T.string,
isAnimated: T.boolean,
mimeType: T.string.nullable(),
src: T.string.nullable(),
})
)
const Versions = {
AddIsAnimated: 1,
RenameWidthHeight: 2,
} as const
/** @public */
export const imageAssetMigrations = defineMigrations({
currentVersion: Versions.RenameWidthHeight,
migrators: {
[Versions.AddIsAnimated]: {
up: (asset) => {
return {
...asset,
props: {
...asset.props,
isAnimated: false,
},
}
},
down: (asset) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { isAnimated, ...rest } = asset.props
return {
...asset,
props: rest,
}
},
},
[Versions.RenameWidthHeight]: {
up: (asset) => {
const { width, height, ...others } = asset.props
return { ...asset, props: { w: width, h: height, ...others } }
},
down: (asset) => {
const { w, h, ...others } = asset.props
return { ...asset, props: { width: w, height: h, ...others } }
},
},
},
})