Tldraw/packages/tlschema/src/shapes/TLBookmarkShape.ts

54 wiersze
1.3 KiB
TypeScript

import { defineMigrations } from '@tldraw/store'
import { T } from '@tldraw/validate'
import { assetIdValidator } from '../assets/TLBaseAsset'
import { TLAssetId } from '../records/TLAsset'
import { TLBaseShape, createShapeValidator } from './TLBaseShape'
/** @public */
export type TLBookmarkShapeProps = {
w: number
h: number
assetId: TLAssetId | null
url: string
}
/** @public */
export type TLBookmarkShape = TLBaseShape<'bookmark', TLBookmarkShapeProps>
/** @internal */
export const bookmarkShapeValidator: T.Validator<TLBookmarkShape> = createShapeValidator(
'bookmark',
T.object({
w: T.nonZeroNumber,
h: T.nonZeroNumber,
assetId: assetIdValidator.nullable(),
url: T.string,
})
)
const Versions = {
NullAssetId: 1,
} as const
/** @internal */
export const bookmarkShapeMigrations = defineMigrations({
currentVersion: Versions.NullAssetId,
migrators: {
[Versions.NullAssetId]: {
up: (shape: TLBookmarkShape) => {
if (shape.props.assetId === undefined) {
return { ...shape, props: { ...shape.props, assetId: null } } as typeof shape
}
return shape
},
down: (shape: TLBookmarkShape) => {
if (shape.props.assetId === null) {
const { assetId: _, ...props } = shape.props
return { ...shape, props } as typeof shape
}
return shape
},
},
},
})