UNPKG

@tldraw/tlschema

Version:

A tiny little drawing app (schema).

8 lines (7 loc) 4.15 kB
{ "version": 3, "sources": ["../src/createPresenceStateDerivation.ts"], "sourcesContent": ["import { Signal, computed } from '@tldraw/state'\nimport { TLStore } from './TLStore'\nimport { CameraRecordType } from './records/TLCamera'\nimport { TLINSTANCE_ID } from './records/TLInstance'\nimport { InstancePageStateRecordType } from './records/TLPageState'\nimport { TLPOINTER_ID } from './records/TLPointer'\nimport { InstancePresenceRecordType, TLInstancePresence } from './records/TLPresence'\n\n/**\n * The information about a user which is used for multiplayer features.\n * @public\n */\nexport interface TLPresenceUserInfo {\n\t/**\n\t * id - A unique identifier for the user. This should be the same across all devices and sessions.\n\t */\n\tid: string\n\t/**\n\t * The user's display name.\n\t */\n\tname?: string | null\n\t/**\n\t * The user's color. If not given, a random color will be assigned.\n\t */\n\tcolor?: string | null\n}\n\n/**\n * Creates a derivation that represents the current presence state of the current user.\n * @public\n */\nexport function createPresenceStateDerivation(\n\t$user: Signal<TLPresenceUserInfo>,\n\tinstanceId?: TLInstancePresence['id']\n) {\n\treturn (store: TLStore): Signal<TLInstancePresence | null> => {\n\t\treturn computed('instancePresence', () => {\n\t\t\tconst user = $user.get()\n\t\t\tif (!user) return null\n\n\t\t\tconst state = getDefaultUserPresence(store, user)\n\t\t\tif (!state) return null\n\n\t\t\treturn InstancePresenceRecordType.create({\n\t\t\t\t...state,\n\t\t\t\tid: instanceId ?? InstancePresenceRecordType.createId(store.id),\n\t\t\t})\n\t\t})\n\t}\n}\n\n/** @public */\nexport type TLPresenceStateInfo = Parameters<(typeof InstancePresenceRecordType)['create']>[0]\n\n/** @public */\nexport function getDefaultUserPresence(store: TLStore, user: TLPresenceUserInfo) {\n\tconst instance = store.get(TLINSTANCE_ID)\n\tconst pageState = store.get(InstancePageStateRecordType.createId(instance?.currentPageId))\n\tconst camera = store.get(CameraRecordType.createId(instance?.currentPageId))\n\tconst pointer = store.get(TLPOINTER_ID)\n\tif (!pageState || !instance || !camera || !pointer) {\n\t\treturn null\n\t}\n\n\treturn {\n\t\tselectedShapeIds: pageState.selectedShapeIds,\n\t\tbrush: instance.brush,\n\t\tscribbles: instance.scribbles,\n\t\tuserId: user.id,\n\t\tuserName: user.name ?? '',\n\t\tfollowingUserId: instance.followingUserId,\n\t\tcamera: {\n\t\t\tx: camera.x,\n\t\t\ty: camera.y,\n\t\t\tz: camera.z,\n\t\t},\n\t\tcolor: user.color ?? '#FF0000',\n\t\tcurrentPageId: instance.currentPageId,\n\t\tcursor: {\n\t\t\tx: pointer.x,\n\t\t\ty: pointer.y,\n\t\t\trotation: instance.cursor.rotation,\n\t\t\ttype: instance.cursor.type,\n\t\t},\n\t\tlastActivityTimestamp: pointer.lastActivityTimestamp,\n\t\tscreenBounds: instance.screenBounds,\n\t\tchatMessage: instance.chatMessage,\n\t\tmeta: {},\n\t} satisfies TLPresenceStateInfo\n}\n"], "mappings": "AAAA,SAAiB,gBAAgB;AAEjC,SAAS,wBAAwB;AACjC,SAAS,qBAAqB;AAC9B,SAAS,mCAAmC;AAC5C,SAAS,oBAAoB;AAC7B,SAAS,kCAAsD;AAyBxD,SAAS,8BACf,OACA,YACC;AACD,SAAO,CAAC,UAAsD;AAC7D,WAAO,SAAS,oBAAoB,MAAM;AACzC,YAAM,OAAO,MAAM,IAAI;AACvB,UAAI,CAAC,KAAM,QAAO;AAElB,YAAM,QAAQ,uBAAuB,OAAO,IAAI;AAChD,UAAI,CAAC,MAAO,QAAO;AAEnB,aAAO,2BAA2B,OAAO;AAAA,QACxC,GAAG;AAAA,QACH,IAAI,cAAc,2BAA2B,SAAS,MAAM,EAAE;AAAA,MAC/D,CAAC;AAAA,IACF,CAAC;AAAA,EACF;AACD;AAMO,SAAS,uBAAuB,OAAgB,MAA0B;AAChF,QAAM,WAAW,MAAM,IAAI,aAAa;AACxC,QAAM,YAAY,MAAM,IAAI,4BAA4B,SAAS,UAAU,aAAa,CAAC;AACzF,QAAM,SAAS,MAAM,IAAI,iBAAiB,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