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

85 wiersze
1.8 KiB
TypeScript

import { defineMigrations } from '@tldraw/store'
import { T } from '@tldraw/validate'
import { createAssetValidator, TLBaseAsset } from './TLBaseAsset'
/**
* An asset for images such as PNGs and JPEGs, used by the TLImageShape.
*
* @public */
export type TLImageAsset = TLBaseAsset<
'image',
{
w: number
h: number
name: string
isAnimated: boolean
mimeType: string | null
src: string | null
}
>
/** @internal */
export const imageAssetValidator: 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.srcUrl.nullable(),
})
)
const Versions = {
AddIsAnimated: 1,
RenameWidthHeight: 2,
MakeUrlsValid: 3,
} as const
/** @internal */
export const imageAssetMigrations = defineMigrations({
currentVersion: Versions.MakeUrlsValid,
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 } }
},
},
[Versions.MakeUrlsValid]: {
up: (asset: TLImageAsset) => {
const src = asset.props.src
if (src && !T.srcUrl.isValid(src)) {
return { ...asset, props: { ...asset.props, src: '' } }
}
return asset
},
down: (asset) => asset,
},
},
})