2023-04-25 11:01:25 +00:00
|
|
|
import { BaseRecord, createRecordType, defineMigrations, ID } from '@tldraw/tlstore'
|
|
|
|
import { T } from '@tldraw/tlvalidate'
|
|
|
|
import { idValidator, instanceIdValidator, pageIdValidator, userIdValidator } from '../validation'
|
|
|
|
import { TLInstance } from './TLInstance'
|
|
|
|
import { TLPage } from './TLPage'
|
|
|
|
import { TLUserId } from './TLUser'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TLUserDocument
|
|
|
|
*
|
|
|
|
* Settings that apply to this document for only the specified user
|
|
|
|
*
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface TLUserDocument extends BaseRecord<'user_document'> {
|
|
|
|
userId: TLUserId
|
|
|
|
isPenMode: boolean
|
|
|
|
isGridMode: boolean
|
|
|
|
isDarkMode: boolean
|
|
|
|
isMobileMode: boolean
|
|
|
|
isSnapMode: boolean
|
|
|
|
lastUpdatedPageId: ID<TLPage> | null
|
|
|
|
lastUsedTabId: ID<TLInstance> | null
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @public */
|
|
|
|
export type TLUserDocumentId = ID<TLUserDocument>
|
|
|
|
|
|
|
|
// --- VALIDATION ---
|
|
|
|
/** @public */
|
|
|
|
export const userDocumentTypeValidator: T.Validator<TLUserDocument> = T.model(
|
|
|
|
'user_document',
|
|
|
|
T.object({
|
|
|
|
typeName: T.literal('user_document'),
|
|
|
|
id: idValidator<TLUserDocumentId>('user_document'),
|
|
|
|
userId: userIdValidator,
|
|
|
|
isPenMode: T.boolean,
|
|
|
|
isGridMode: T.boolean,
|
|
|
|
isDarkMode: T.boolean,
|
|
|
|
isMobileMode: T.boolean,
|
|
|
|
isSnapMode: T.boolean,
|
|
|
|
lastUpdatedPageId: pageIdValidator.nullable(),
|
|
|
|
lastUsedTabId: instanceIdValidator.nullable(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
// --- MIGRATIONS ---
|
|
|
|
// STEP 1: Add a new version number here, give it a meaningful name.
|
|
|
|
// It should be 1 higher than the current version
|
2023-05-17 10:45:43 +00:00
|
|
|
export const userDocumentVersions = {
|
2023-04-25 11:01:25 +00:00
|
|
|
Initial: 0,
|
|
|
|
AddSnapMode: 1,
|
|
|
|
AddMissingIsMobileMode: 2,
|
2023-05-17 10:45:43 +00:00
|
|
|
RemoveIsReadOnly: 3,
|
2023-04-25 11:01:25 +00:00
|
|
|
} as const
|
|
|
|
|
|
|
|
/** @public */
|
|
|
|
export const userDocumentTypeMigrations = defineMigrations({
|
2023-05-17 10:45:43 +00:00
|
|
|
firstVersion: userDocumentVersions.Initial,
|
2023-04-25 11:01:25 +00:00
|
|
|
// STEP 2: Update the current version to point to your latest version
|
2023-05-17 10:45:43 +00:00
|
|
|
currentVersion: userDocumentVersions.RemoveIsReadOnly,
|
2023-04-25 11:01:25 +00:00
|
|
|
// STEP 3: Add an up+down migration for the new version here
|
|
|
|
migrators: {
|
2023-05-17 10:45:43 +00:00
|
|
|
[userDocumentVersions.AddSnapMode]: {
|
2023-04-25 11:01:25 +00:00
|
|
|
up: (userDocument: TLUserDocument) => {
|
|
|
|
return { ...userDocument, isSnapMode: false }
|
|
|
|
},
|
|
|
|
down: ({ isSnapMode: _, ...userDocument }: TLUserDocument) => {
|
|
|
|
return userDocument
|
|
|
|
},
|
|
|
|
},
|
2023-05-17 10:45:43 +00:00
|
|
|
[userDocumentVersions.AddMissingIsMobileMode]: {
|
2023-04-25 11:01:25 +00:00
|
|
|
up: (userDocument: TLUserDocument) => {
|
|
|
|
return { ...userDocument, isMobileMode: userDocument.isMobileMode ?? false }
|
|
|
|
},
|
|
|
|
down: ({ isMobileMode: _, ...userDocument }: TLUserDocument) => {
|
|
|
|
return userDocument
|
|
|
|
},
|
|
|
|
},
|
2023-05-17 10:45:43 +00:00
|
|
|
[userDocumentVersions.RemoveIsReadOnly]: {
|
|
|
|
up: ({ isReadOnly: _, ...userDocument }: TLUserDocument & { isReadOnly: boolean }) => {
|
|
|
|
return userDocument
|
|
|
|
},
|
|
|
|
down: (userDocument: TLUserDocument) => {
|
|
|
|
return { ...userDocument, isReadOnly: false }
|
|
|
|
},
|
|
|
|
},
|
2023-04-25 11:01:25 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
/* STEP 4: Add your changes to the record type */
|
|
|
|
|
|
|
|
/* STEP 5: Add up + down migrations for your new version */
|
|
|
|
/** @public */
|
|
|
|
export const TLUserDocument = createRecordType<TLUserDocument>('user_document', {
|
|
|
|
migrations: userDocumentTypeMigrations,
|
|
|
|
validator: userDocumentTypeValidator,
|
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: 'instance',
|
2023-04-25 11:01:25 +00:00
|
|
|
}).withDefaultProperties(
|
|
|
|
(): Omit<TLUserDocument, 'id' | 'typeName' | 'userId'> => ({
|
|
|
|
/* STEP 6: Add any new default values for properties here */
|
|
|
|
isPenMode: false,
|
|
|
|
isGridMode: false,
|
|
|
|
isDarkMode: false,
|
|
|
|
isMobileMode: false,
|
|
|
|
isSnapMode: false,
|
|
|
|
lastUpdatedPageId: null,
|
|
|
|
lastUsedTabId: null,
|
|
|
|
})
|
|
|
|
)
|