Tldraw/packages/tlschema/src/records/TLUser.ts

62 wiersze
1.5 KiB
TypeScript
Czysty Zwykły widok Historia

2023-04-25 11:01:25 +00:00
import { BaseRecord, createRecordType, defineMigrations, ID } from '@tldraw/tlstore'
import { T } from '@tldraw/tlvalidate'
import { getDefaultTranslationLocale } from '../translations'
2023-04-25 11:01:25 +00:00
import { userIdValidator } from '../validation'
/**
* A user of tldraw
*
* @public
*/
export interface TLUser extends BaseRecord<'user'> {
name: string
locale: string
}
/** @public */
export type TLUserId = ID<TLUser>
// --- VALIDATION ---
/** @public */
export const userTypeValidator: T.Validator<TLUser> = T.model(
'user',
T.object({
typeName: T.literal('user'),
id: userIdValidator,
name: T.string,
locale: T.string,
})
)
// --- MIGRATIONS ---
// STEP 1: Add a new version number here, give it a meaningful name.
// It should be 1 higher than the current version
const Versions = {
Initial: 0,
} as const
/** @public */
export const userTypeMigrations = defineMigrations({
// STEP 2: Update the current version to point to your latest version
currentVersion: Versions.Initial,
firstVersion: Versions.Initial,
migrators: {
// STEP 3: Add an up+down migration for the new version here
},
})
/** @public */
export const TLUser = createRecordType<TLUser>('user', {
migrations: userTypeMigrations,
validator: userTypeValidator,
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<TLUser, 'id' | 'typeName'> => {
let locale = 'en'
2023-04-25 11:01:25 +00:00
if (typeof window !== 'undefined' && window.navigator) {
locale = getDefaultTranslationLocale(window.navigator.languages)
2023-04-25 11:01:25 +00:00
}
return {
name: 'New User',
locale,
2023-04-25 11:01:25 +00:00
}
})