2024-04-16 11:13:54 +00:00
|
|
|
import {
|
|
|
|
createMigrationIds,
|
|
|
|
createRecordMigrationSequence,
|
|
|
|
createRecordType,
|
|
|
|
RecordId,
|
|
|
|
} from '@tldraw/store'
|
2023-06-03 08:27:44 +00:00
|
|
|
import { T } from '@tldraw/validate'
|
2023-06-03 20:46:53 +00:00
|
|
|
import { TLBaseAsset } from '../assets/TLBaseAsset'
|
2024-04-16 11:13:54 +00:00
|
|
|
import { bookmarkAssetValidator, TLBookmarkAsset } from '../assets/TLBookmarkAsset'
|
|
|
|
import { imageAssetValidator, TLImageAsset } from '../assets/TLImageAsset'
|
|
|
|
import { TLVideoAsset, videoAssetValidator } from '../assets/TLVideoAsset'
|
2023-04-25 11:01:25 +00:00
|
|
|
import { TLShape } from './TLShape'
|
|
|
|
|
|
|
|
/** @public */
|
|
|
|
export type TLAsset = TLImageAsset | TLVideoAsset | TLBookmarkAsset
|
|
|
|
|
2023-06-03 20:46:53 +00:00
|
|
|
/** @internal */
|
|
|
|
export const assetValidator: T.Validator<TLAsset> = T.model(
|
2023-04-25 11:01:25 +00:00
|
|
|
'asset',
|
|
|
|
T.union('type', {
|
2023-06-03 20:46:53 +00:00
|
|
|
image: imageAssetValidator,
|
|
|
|
video: videoAssetValidator,
|
|
|
|
bookmark: bookmarkAssetValidator,
|
2023-04-25 11:01:25 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
|
2023-06-28 14:24:05 +00:00
|
|
|
/** @internal */
|
2024-04-16 11:13:54 +00:00
|
|
|
export const assetVersions = createMigrationIds('com.tldraw.asset', {
|
2023-06-28 14:24:05 +00:00
|
|
|
AddMeta: 1,
|
2024-04-16 11:13:54 +00:00
|
|
|
} as const)
|
2023-06-28 14:24:05 +00:00
|
|
|
|
2023-06-03 20:46:53 +00:00
|
|
|
/** @internal */
|
2024-04-16 11:13:54 +00:00
|
|
|
export const assetMigrations = createRecordMigrationSequence({
|
|
|
|
sequenceId: 'com.tldraw.asset',
|
|
|
|
recordType: 'asset',
|
|
|
|
sequence: [
|
|
|
|
{
|
|
|
|
id: assetVersions.AddMeta,
|
2023-06-28 14:24:05 +00:00
|
|
|
up: (record) => {
|
2024-04-16 11:13:54 +00:00
|
|
|
;(record as any).meta = {}
|
2023-06-28 14:24:05 +00:00
|
|
|
},
|
|
|
|
},
|
2024-04-16 11:13:54 +00:00
|
|
|
],
|
2023-04-25 11:01:25 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
/** @public */
|
|
|
|
export type TLAssetPartial<T extends TLAsset = TLAsset> = T extends T
|
|
|
|
? {
|
|
|
|
id: TLAssetId
|
|
|
|
type: T['type']
|
|
|
|
props?: Partial<T['props']>
|
2023-06-28 14:24:05 +00:00
|
|
|
meta?: Partial<T['meta']>
|
2024-02-07 16:02:22 +00:00
|
|
|
} & Partial<Omit<T, 'type' | 'id' | 'props' | 'meta'>>
|
2023-04-25 11:01:25 +00:00
|
|
|
: never
|
|
|
|
|
|
|
|
/** @public */
|
2023-05-26 13:37:59 +00:00
|
|
|
export const AssetRecordType = createRecordType<TLAsset>('asset', {
|
2023-06-03 20:46:53 +00:00
|
|
|
validator: assetValidator,
|
derived presence state (#1204)
This PR adds
- A new `TLInstancePresence` record type, to collect info about the
presence state in a particular instance of the editor. This will
eventually be used to sync presence data instead of sending
instance-only state across the wire.
- **Record Scopes**
`RecordType` now has a `scope` property which can be one of three
things:
- `document`: the record belongs to the document and should be synced
and persisted freely. Currently: `TLDocument`, `TLPage`, `TLShape`, and
`TLAsset`
- `instance`: the record belongs to a single instance of the store and
should not be synced at all. It should not be persisted directly in most
cases, but rather compiled into a kind of 'instance configuration' to
store alongside the local document data so that when reopening the
associated document it can remember some of the previous instance state.
Currently: `TLInstance`, `TLInstancePageState`, `TLCamera`, `TLUser`,
`TLUserDocument`, `TLUserPresence`
- `presence`: the record belongs to a single instance of the store and
should not be persisted, but may be synced using the special presence
sync protocol. Currently just `TLInstancePresence`
This sets us up for the following changes, which are gonna be pretty
high-impact in terms of integrating tldraw into existing systems:
- Removing `instanceId` as a config option. Each instance gets a
randomly generated ID.
- We'd replace it with an `instanceConfig` option that has stuff like
selectedIds, camera positions, and so on. Then it's up to library users
to get and reinstate the instance config at persistence boundaries.
- Removing `userId` as config option, and removing the `TLUser` type
altogether.
- We might need to revisit when doing auth-enabled features like locking
shapes, but I suspect that will be separate.
2023-04-27 18:03:19 +00:00
|
|
|
scope: 'document',
|
2023-06-28 14:24:05 +00:00
|
|
|
}).withDefaultProperties(() => ({
|
|
|
|
meta: {},
|
|
|
|
}))
|
2023-04-25 11:01:25 +00:00
|
|
|
|
|
|
|
/** @public */
|
2023-06-03 20:46:53 +00:00
|
|
|
export type TLAssetId = RecordId<TLBaseAsset<any, any>>
|
2023-04-25 11:01:25 +00:00
|
|
|
|
|
|
|
/** @public */
|
|
|
|
export type TLAssetShape = Extract<TLShape, { props: { assetId: TLAssetId } }>
|