@tldraw/editor 4.6.0-internal.e29318c66fb0 → 4.6.0-next.0c36db38afea

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (155) hide show
  1. package/dist-cjs/index.d.ts +184 -24
  2. package/dist-cjs/index.js +5 -4
  3. package/dist-cjs/index.js.map +2 -2
  4. package/dist-cjs/lib/TldrawEditor.js +4 -2
  5. package/dist-cjs/lib/TldrawEditor.js.map +2 -2
  6. package/dist-cjs/lib/components/default-components/DefaultCanvas.js +1 -1
  7. package/dist-cjs/lib/components/default-components/DefaultCanvas.js.map +2 -2
  8. package/dist-cjs/lib/components/default-components/DefaultCollaboratorHint.js +1 -1
  9. package/dist-cjs/lib/components/default-components/DefaultCollaboratorHint.js.map +2 -2
  10. package/dist-cjs/lib/config/{createTLUser.js → createTLCurrentUser.js} +9 -9
  11. package/dist-cjs/lib/config/createTLCurrentUser.js.map +7 -0
  12. package/dist-cjs/lib/config/createTLStore.js +24 -1
  13. package/dist-cjs/lib/config/createTLStore.js.map +2 -2
  14. package/dist-cjs/lib/config/defaultShapes.js.map +2 -2
  15. package/dist-cjs/lib/editor/Editor.js +115 -8
  16. package/dist-cjs/lib/editor/Editor.js.map +3 -3
  17. package/dist-cjs/lib/editor/managers/EdgeScrollManager/EdgeScrollManager.js +1 -1
  18. package/dist-cjs/lib/editor/managers/EdgeScrollManager/EdgeScrollManager.js.map +2 -2
  19. package/dist-cjs/lib/editor/managers/SnapManager/BoundsSnaps.js +1 -1
  20. package/dist-cjs/lib/editor/managers/SnapManager/BoundsSnaps.js.map +2 -2
  21. package/dist-cjs/lib/editor/managers/SnapManager/HandleSnaps.js.map +2 -2
  22. package/dist-cjs/lib/editor/managers/SpatialIndexManager/SpatialIndexManager.js +4 -4
  23. package/dist-cjs/lib/editor/managers/SpatialIndexManager/SpatialIndexManager.js.map +2 -2
  24. package/dist-cjs/lib/editor/managers/TextManager/TextManager.js +102 -30
  25. package/dist-cjs/lib/editor/managers/TextManager/TextManager.js.map +2 -2
  26. package/dist-cjs/lib/editor/managers/UserPreferencesManager/UserPreferencesManager.js.map +2 -2
  27. package/dist-cjs/lib/editor/shapes/ShapeUtil.js +10 -0
  28. package/dist-cjs/lib/editor/shapes/ShapeUtil.js.map +2 -2
  29. package/dist-cjs/lib/editor/types/clipboard-types.js.map +1 -1
  30. package/dist-cjs/lib/exports/StyleEmbedder.js +1 -1
  31. package/dist-cjs/lib/exports/StyleEmbedder.js.map +2 -2
  32. package/dist-cjs/lib/exports/getSvgJsx.js.map +2 -2
  33. package/dist-cjs/lib/hooks/useEditorComponents.js.map +2 -2
  34. package/dist-cjs/lib/hooks/useGestureEvents.js +171 -127
  35. package/dist-cjs/lib/hooks/useGestureEvents.js.map +3 -3
  36. package/dist-cjs/lib/hooks/useLocalStore.js.map +2 -2
  37. package/dist-cjs/lib/options.js +3 -0
  38. package/dist-cjs/lib/options.js.map +2 -2
  39. package/dist-cjs/lib/primitives/Box.js +1 -1
  40. package/dist-cjs/lib/primitives/Box.js.map +2 -2
  41. package/dist-cjs/lib/primitives/geometry/Arc2d.js +2 -2
  42. package/dist-cjs/lib/primitives/geometry/Arc2d.js.map +2 -2
  43. package/dist-cjs/lib/primitives/geometry/Circle2d.js +2 -2
  44. package/dist-cjs/lib/primitives/geometry/Circle2d.js.map +2 -2
  45. package/dist-cjs/lib/primitives/geometry/Ellipse2d.js +2 -2
  46. package/dist-cjs/lib/primitives/geometry/Ellipse2d.js.map +2 -2
  47. package/dist-cjs/lib/primitives/geometry/Geometry2d.js +4 -3
  48. package/dist-cjs/lib/primitives/geometry/Geometry2d.js.map +2 -2
  49. package/dist-cjs/lib/primitives/geometry/Polyline2d.js +1 -1
  50. package/dist-cjs/lib/primitives/geometry/Polyline2d.js.map +2 -2
  51. package/dist-cjs/lib/primitives/geometry/Stadium2d.js +1 -1
  52. package/dist-cjs/lib/primitives/geometry/Stadium2d.js.map +2 -2
  53. package/dist-cjs/lib/utils/getSvgPathFromPoints.js.map +2 -2
  54. package/dist-cjs/lib/utils/sync/TLLocalSyncClient.js +1 -1
  55. package/dist-cjs/lib/utils/sync/TLLocalSyncClient.js.map +2 -2
  56. package/dist-cjs/version.js +3 -3
  57. package/dist-cjs/version.js.map +1 -1
  58. package/dist-esm/index.d.mts +184 -24
  59. package/dist-esm/index.mjs +12 -5
  60. package/dist-esm/index.mjs.map +2 -2
  61. package/dist-esm/lib/TldrawEditor.mjs +4 -2
  62. package/dist-esm/lib/TldrawEditor.mjs.map +2 -2
  63. package/dist-esm/lib/components/default-components/DefaultCanvas.mjs +1 -1
  64. package/dist-esm/lib/components/default-components/DefaultCanvas.mjs.map +2 -2
  65. package/dist-esm/lib/components/default-components/DefaultCollaboratorHint.mjs +1 -1
  66. package/dist-esm/lib/components/default-components/DefaultCollaboratorHint.mjs.map +2 -2
  67. package/dist-esm/lib/config/{createTLUser.mjs → createTLCurrentUser.mjs} +6 -6
  68. package/dist-esm/lib/config/createTLCurrentUser.mjs.map +7 -0
  69. package/dist-esm/lib/config/createTLStore.mjs +28 -2
  70. package/dist-esm/lib/config/createTLStore.mjs.map +2 -2
  71. package/dist-esm/lib/config/defaultShapes.mjs.map +2 -2
  72. package/dist-esm/lib/editor/Editor.mjs +118 -9
  73. package/dist-esm/lib/editor/Editor.mjs.map +3 -3
  74. package/dist-esm/lib/editor/managers/EdgeScrollManager/EdgeScrollManager.mjs +1 -1
  75. package/dist-esm/lib/editor/managers/EdgeScrollManager/EdgeScrollManager.mjs.map +2 -2
  76. package/dist-esm/lib/editor/managers/SnapManager/BoundsSnaps.mjs +1 -1
  77. package/dist-esm/lib/editor/managers/SnapManager/BoundsSnaps.mjs.map +2 -2
  78. package/dist-esm/lib/editor/managers/SnapManager/HandleSnaps.mjs.map +2 -2
  79. package/dist-esm/lib/editor/managers/SpatialIndexManager/SpatialIndexManager.mjs +4 -4
  80. package/dist-esm/lib/editor/managers/SpatialIndexManager/SpatialIndexManager.mjs.map +2 -2
  81. package/dist-esm/lib/editor/managers/TextManager/TextManager.mjs +102 -30
  82. package/dist-esm/lib/editor/managers/TextManager/TextManager.mjs.map +2 -2
  83. package/dist-esm/lib/editor/managers/UserPreferencesManager/UserPreferencesManager.mjs.map +2 -2
  84. package/dist-esm/lib/editor/shapes/ShapeUtil.mjs +10 -0
  85. package/dist-esm/lib/editor/shapes/ShapeUtil.mjs.map +2 -2
  86. package/dist-esm/lib/exports/StyleEmbedder.mjs +1 -1
  87. package/dist-esm/lib/exports/StyleEmbedder.mjs.map +2 -2
  88. package/dist-esm/lib/exports/getSvgJsx.mjs.map +2 -2
  89. package/dist-esm/lib/hooks/useEditorComponents.mjs.map +2 -2
  90. package/dist-esm/lib/hooks/useGestureEvents.mjs +171 -127
  91. package/dist-esm/lib/hooks/useGestureEvents.mjs.map +3 -3
  92. package/dist-esm/lib/hooks/useLocalStore.mjs.map +2 -2
  93. package/dist-esm/lib/options.mjs +3 -0
  94. package/dist-esm/lib/options.mjs.map +2 -2
  95. package/dist-esm/lib/primitives/Box.mjs +1 -1
  96. package/dist-esm/lib/primitives/Box.mjs.map +2 -2
  97. package/dist-esm/lib/primitives/geometry/Arc2d.mjs +2 -2
  98. package/dist-esm/lib/primitives/geometry/Arc2d.mjs.map +2 -2
  99. package/dist-esm/lib/primitives/geometry/Circle2d.mjs +2 -2
  100. package/dist-esm/lib/primitives/geometry/Circle2d.mjs.map +2 -2
  101. package/dist-esm/lib/primitives/geometry/Ellipse2d.mjs +2 -2
  102. package/dist-esm/lib/primitives/geometry/Ellipse2d.mjs.map +2 -2
  103. package/dist-esm/lib/primitives/geometry/Geometry2d.mjs +4 -3
  104. package/dist-esm/lib/primitives/geometry/Geometry2d.mjs.map +2 -2
  105. package/dist-esm/lib/primitives/geometry/Polyline2d.mjs +1 -1
  106. package/dist-esm/lib/primitives/geometry/Polyline2d.mjs.map +2 -2
  107. package/dist-esm/lib/primitives/geometry/Stadium2d.mjs +1 -1
  108. package/dist-esm/lib/primitives/geometry/Stadium2d.mjs.map +2 -2
  109. package/dist-esm/lib/utils/getSvgPathFromPoints.mjs.map +2 -2
  110. package/dist-esm/lib/utils/sync/TLLocalSyncClient.mjs +1 -1
  111. package/dist-esm/lib/utils/sync/TLLocalSyncClient.mjs.map +2 -2
  112. package/dist-esm/version.mjs +3 -3
  113. package/dist-esm/version.mjs.map +1 -1
  114. package/editor.css +13 -0
  115. package/package.json +8 -9
  116. package/src/index.ts +14 -2
  117. package/src/lib/TldrawEditor.tsx +9 -7
  118. package/src/lib/components/default-components/DefaultCanvas.tsx +1 -1
  119. package/src/lib/components/default-components/DefaultCollaboratorHint.tsx +1 -1
  120. package/src/lib/config/TLEditorSnapshot.test.ts +1 -1
  121. package/src/lib/config/{createTLUser.ts → createTLCurrentUser.ts} +6 -6
  122. package/src/lib/config/createTLStore.ts +36 -2
  123. package/src/lib/config/defaultShapes.ts +1 -1
  124. package/src/lib/editor/Editor.ts +148 -10
  125. package/src/lib/editor/managers/EdgeScrollManager/EdgeScrollManager.ts +1 -1
  126. package/src/lib/editor/managers/SnapManager/BoundsSnaps.ts +1 -1
  127. package/src/lib/editor/managers/SnapManager/HandleSnaps.ts +1 -1
  128. package/src/lib/editor/managers/SpatialIndexManager/SpatialIndexManager.ts +5 -5
  129. package/src/lib/editor/managers/TextManager/TextManager.test.ts +18 -4
  130. package/src/lib/editor/managers/TextManager/TextManager.ts +140 -34
  131. package/src/lib/editor/managers/UserPreferencesManager/UserPreferencesManager.test.ts +2 -2
  132. package/src/lib/editor/managers/UserPreferencesManager/UserPreferencesManager.ts +2 -2
  133. package/src/lib/editor/shapes/ShapeUtil.ts +13 -2
  134. package/src/lib/editor/types/clipboard-types.ts +2 -1
  135. package/src/lib/exports/StyleEmbedder.ts +1 -1
  136. package/src/lib/exports/getSvgJsx.tsx +1 -1
  137. package/src/lib/hooks/useEditorComponents.tsx +1 -1
  138. package/src/lib/hooks/useGestureEvents.ts +240 -168
  139. package/src/lib/hooks/useLocalStore.ts +1 -1
  140. package/src/lib/options.ts +108 -0
  141. package/src/lib/primitives/Box.test.ts +30 -0
  142. package/src/lib/primitives/Box.ts +1 -1
  143. package/src/lib/primitives/geometry/Arc2d.ts +2 -2
  144. package/src/lib/primitives/geometry/Circle2d.ts +2 -2
  145. package/src/lib/primitives/geometry/Ellipse2d.ts +2 -2
  146. package/src/lib/primitives/geometry/Geometry2d.test.ts +21 -0
  147. package/src/lib/primitives/geometry/Geometry2d.ts +4 -3
  148. package/src/lib/primitives/geometry/Polyline2d.ts +1 -1
  149. package/src/lib/primitives/geometry/Stadium2d.ts +1 -1
  150. package/src/lib/utils/getSvgPathFromPoints.ts +1 -1
  151. package/src/lib/utils/sync/TLLocalSyncClient.test.ts +1 -1
  152. package/src/lib/utils/sync/TLLocalSyncClient.ts +1 -1
  153. package/src/version.ts +3 -3
  154. package/dist-cjs/lib/config/createTLUser.js.map +0 -7
  155. package/dist-esm/lib/config/createTLUser.mjs.map +0 -7
@@ -5,7 +5,7 @@ import { useShallowObjectIdentity } from '../hooks/useIdentity'
5
5
  import { TLUserPreferences, getUserPreferences, setUserPreferences } from './TLUserPreferences'
6
6
 
7
7
  /** @public */
8
- export interface TLUser {
8
+ export interface TLCurrentUser {
9
9
  readonly userPreferences: Signal<TLUserPreferences>
10
10
  // eslint-disable-next-line tldraw/method-signature-style
11
11
  readonly setUserPreferences: (userPreferences: TLUserPreferences) => void
@@ -16,13 +16,13 @@ const defaultLocalStorageUserPrefs = computed('defaultLocalStorageUserPrefs', ()
16
16
  )
17
17
 
18
18
  /** @public */
19
- export function createTLUser(
19
+ export function createTLCurrentUser(
20
20
  opts = {} as {
21
21
  userPreferences?: Signal<TLUserPreferences>
22
22
  // eslint-disable-next-line tldraw/method-signature-style
23
23
  setUserPreferences?: (userPreferences: TLUserPreferences) => void
24
24
  }
25
- ): TLUser {
25
+ ): TLCurrentUser {
26
26
  return {
27
27
  userPreferences: opts.userPreferences ?? defaultLocalStorageUserPrefs,
28
28
  setUserPreferences: opts.setUserPreferences ?? setUserPreferences,
@@ -32,11 +32,11 @@ export function createTLUser(
32
32
  /**
33
33
  * @public
34
34
  */
35
- export function useTldrawUser(opts: {
35
+ export function useTldrawCurrentUser(opts: {
36
36
  userPreferences?: Signal<TLUserPreferences> | TLUserPreferences
37
37
  // eslint-disable-next-line tldraw/method-signature-style
38
38
  setUserPreferences?: (userPreferences: TLUserPreferences) => void
39
- }): TLUser {
39
+ }): TLCurrentUser {
40
40
  const prefs = useShallowObjectIdentity(opts.userPreferences ?? defaultLocalStorageUserPrefs)
41
41
  const userAtom = useAtom<TLUserPreferences | Signal<TLUserPreferences>>('userAtom', prefs)
42
42
  useEffect(() => {
@@ -45,7 +45,7 @@ export function useTldrawUser(opts: {
45
45
 
46
46
  return useMemo(
47
47
  () =>
48
- createTLUser({
48
+ createTLCurrentUser({
49
49
  userPreferences: computed('userPreferences', () => {
50
50
  const userStuff = userAtom.get()
51
51
  return isSignal(userStuff) ? userStuff.get() : userStuff
@@ -1,4 +1,4 @@
1
- import { Signal } from '@tldraw/state'
1
+ import { Signal, computed } from '@tldraw/state'
2
2
  import { HistoryEntry, MigrationSequence, SerializedStore, Store, StoreSchema } from '@tldraw/store'
3
3
  import {
4
4
  CustomRecordInfo,
@@ -8,13 +8,19 @@ import {
8
8
  TLStore,
9
9
  TLStoreProps,
10
10
  TLStoreSnapshot,
11
+ TLUser,
12
+ TLUserStore,
13
+ UserRecordType,
14
+ createCachedUserResolve,
11
15
  createTLSchema,
16
+ createUserId,
12
17
  } from '@tldraw/tlschema'
13
18
  import { FileHelpers, assert } from '@tldraw/utils'
14
19
  import { Editor } from '../editor/Editor'
15
- import { TLEditorSnapshot, loadSnapshot } from './TLEditorSnapshot'
16
20
  import { TLAnyBindingUtilConstructor, checkBindings } from './defaultBindings'
17
21
  import { TLAnyShapeUtilConstructor, checkShapesAndAddCore } from './defaultShapes'
22
+ import { TLEditorSnapshot, loadSnapshot } from './TLEditorSnapshot'
23
+ import { defaultUserPreferences, getUserPreferences } from './TLUserPreferences'
18
24
 
19
25
  /** @public */
20
26
  export interface TLStoreBaseOptions {
@@ -30,6 +36,9 @@ export interface TLStoreBaseOptions {
30
36
  /** How should this store upload & resolve assets? */
31
37
  assets?: TLAssetStore
32
38
 
39
+ /** How should this store resolve users for attribution? */
40
+ users?: TLUserStore
41
+
33
42
  /** Called when the store is connected to an {@link @tldraw/editor#Editor}. */
34
43
  onMount?(editor: Editor): void | (() => void)
35
44
  }
@@ -61,6 +70,21 @@ export type TLStoreEventInfo = HistoryEntry<TLRecord>
61
70
 
62
71
  const defaultAssetResolve: NonNullable<TLAssetStore['resolve']> = (asset) => asset.props.src
63
72
 
73
+ const _defaultCurrentUser: Signal<TLUser | null> = computed('defaultCurrentUser', () => {
74
+ const prefs = getUserPreferences()
75
+ if (!prefs.id) return null
76
+ return UserRecordType.create({
77
+ id: createUserId(prefs.id),
78
+ name: prefs.name ?? '',
79
+ color: prefs.color ?? defaultUserPreferences.color,
80
+ })
81
+ })
82
+
83
+ /** @public */
84
+ export const defaultUserStore: TLUserStore = {
85
+ currentUser: _defaultCurrentUser,
86
+ }
87
+
64
88
  /** @public */
65
89
  export const inlineBase64AssetStore: TLAssetStore = {
66
90
  upload: async (_, file) => {
@@ -107,6 +131,7 @@ export function createTLStore({
107
131
  defaultName = '',
108
132
  id,
109
133
  assets = inlineBase64AssetStore,
134
+ users = defaultUserStore,
110
135
  onMount,
111
136
  collaboration,
112
137
  ...rest
@@ -124,6 +149,15 @@ export function createTLStore({
124
149
  resolve: assets.resolve ?? defaultAssetResolve,
125
150
  remove: assets.remove ?? (() => Promise.resolve()),
126
151
  },
152
+ users: {
153
+ currentUser: users.currentUser,
154
+ resolve:
155
+ users.resolve ??
156
+ createCachedUserResolve((userId) => {
157
+ const current = users.currentUser.get()
158
+ return current && current.id === createUserId(userId) ? current : null
159
+ }),
160
+ },
127
161
  onMount: (editor) => {
128
162
  assert(editor instanceof Editor)
129
163
  onMount?.(editor)
@@ -1,5 +1,5 @@
1
- import { TLShapeUtilConstructor } from '../editor/shapes/ShapeUtil'
2
1
  import { GroupShapeUtil } from '../editor/shapes/group/GroupShapeUtil'
2
+ import { TLShapeUtilConstructor } from '../editor/shapes/ShapeUtil'
3
3
 
4
4
  /** @public */
5
5
  export type TLAnyShapeUtilConstructor = TLShapeUtilConstructor<any>
@@ -10,10 +10,10 @@ import {
10
10
  import {
11
11
  ComputedCache,
12
12
  RecordType,
13
- StoreSideEffects,
14
- StoreSnapshot,
15
13
  UnknownRecord,
16
14
  reverseRecordsDiff,
15
+ StoreSnapshot,
16
+ StoreSideEffects,
17
17
  } from '@tldraw/store'
18
18
  import {
19
19
  CameraRecordType,
@@ -50,9 +50,13 @@ import {
50
50
  TLShapePartial,
51
51
  TLStore,
52
52
  TLStoreSnapshot,
53
+ TLUser,
54
+ TLUserId,
53
55
  TLVideoAsset,
56
+ UserRecordType,
54
57
  createBindingId,
55
58
  createShapeId,
59
+ createUserId,
56
60
  getShapePropKeysByStyle,
57
61
  isPageId,
58
62
  isShapeId,
@@ -90,15 +94,15 @@ import {
90
94
  uniqueId,
91
95
  } from '@tldraw/utils'
92
96
  import EventEmitter from 'eventemitter3'
97
+ import { TLCurrentUser, createTLCurrentUser } from '../config/createTLCurrentUser'
98
+ import { TLAnyBindingUtilConstructor, checkBindings } from '../config/defaultBindings'
99
+ import { TLAnyShapeUtilConstructor, checkShapesAndAddCore } from '../config/defaultShapes'
93
100
  import {
94
101
  TLEditorSnapshot,
95
102
  TLLoadSnapshotOptions,
96
103
  getSnapshot,
97
104
  loadSnapshot,
98
105
  } from '../config/TLEditorSnapshot'
99
- import { TLUser, createTLUser } from '../config/createTLUser'
100
- import { TLAnyBindingUtilConstructor, checkBindings } from '../config/defaultBindings'
101
- import { TLAnyShapeUtilConstructor, checkShapesAndAddCore } from '../config/defaultShapes'
102
106
  import {
103
107
  DEFAULT_ANIMATION_OPTIONS,
104
108
  DEFAULT_CAMERA_OPTIONS,
@@ -115,14 +119,13 @@ import { tlmenus } from '../globals/menus'
115
119
  import { tltime } from '../globals/time'
116
120
  import { TldrawOptions, defaultTldrawOptions } from '../options'
117
121
  import { Box, BoxLike } from '../primitives/Box'
118
- import { Mat, MatLike } from '../primitives/Mat'
119
- import { Vec, VecLike } from '../primitives/Vec'
120
122
  import { EASINGS } from '../primitives/easings'
121
123
  import { Geometry2d } from '../primitives/geometry/Geometry2d'
122
124
  import { Group2d } from '../primitives/geometry/Group2d'
123
125
  import { intersectPolygonPolygon } from '../primitives/intersect'
126
+ import { Mat, MatLike } from '../primitives/Mat'
124
127
  import { PI, approximately, areAnglesCompatible, clamp, pointInPolygon } from '../primitives/utils'
125
- import { ReadonlySharedStyleMap, SharedStyle, SharedStyleMap } from '../utils/SharedStylesMap'
128
+ import { Vec, VecLike } from '../primitives/Vec'
126
129
  import { areShapesContentEqual } from '../utils/areShapesContentEqual'
127
130
  import { dataUrlToFile } from '../utils/assets'
128
131
  import { debugFlags } from '../utils/debug-flags'
@@ -137,6 +140,7 @@ import { getReorderingShapesChanges } from '../utils/reorderShapes'
137
140
  import { getDroppedShapesToNewParents, kickoutOccludedShapes } from '../utils/reparenting'
138
141
  import { TLTextOptions, TiptapEditor } from '../utils/richText'
139
142
  import { applyRotationToSnapshotShapes, getRotationSnapshot } from '../utils/rotation'
143
+ import { ReadonlySharedStyleMap, SharedStyle, SharedStyleMap } from '../utils/SharedStylesMap'
140
144
  import { BindingOnDeleteOptions, BindingUtil } from './bindings/BindingUtil'
141
145
  import { bindingsIndex } from './derivations/bindingsIndex'
142
146
  import { notVisibleShapes } from './derivations/notVisibleShapes'
@@ -221,7 +225,7 @@ export interface TLEditorOptions {
221
225
  /**
222
226
  * A user defined externally to replace the default user.
223
227
  */
224
- user?: TLUser
228
+ user?: TLCurrentUser
225
229
  /**
226
230
  * The editor's initial active tool (or other state node id).
227
231
  */
@@ -346,7 +350,7 @@ export class Editor extends EventEmitter<TLEventMap> {
346
350
 
347
351
  this._textOptions = atom('text options', options?.text ?? null)
348
352
 
349
- this.user = new UserPreferencesManager(user ?? createTLUser(), inferDarkMode ?? false)
353
+ this.user = new UserPreferencesManager(user ?? createTLCurrentUser(), inferDarkMode ?? false)
350
354
  this.disposables.add(() => this.user.dispose())
351
355
 
352
356
  this.getContainer = getContainer
@@ -357,6 +361,12 @@ export class Editor extends EventEmitter<TLEventMap> {
357
361
  this.fonts = new FontManager(this, fontAssetUrls)
358
362
 
359
363
  this._tickManager = new TickManager(this)
364
+ this.disposables.add(() => {
365
+ // Reset camera state to 'idle' so the store isn't left stuck at 'moving'
366
+ // when tick events stop (e.g. React strict mode disposes while camera is moving)
367
+ this.off('tick', this._decayCameraStateTimeout)
368
+ this._setCameraState('idle')
369
+ })
360
370
 
361
371
  this.inputs = new InputsManager(this)
362
372
 
@@ -820,6 +830,15 @@ export class Editor extends EventEmitter<TLEventMap> {
820
830
  })
821
831
  )
822
832
  }
833
+
834
+ this.disposables.add(
835
+ react('sync current user record', () => {
836
+ const user = this.store.props.users.currentUser.get()
837
+ if (user) {
838
+ this._ensureUserRecord(user)
839
+ }
840
+ })
841
+ )
823
842
  }
824
843
 
825
844
  private readonly _getShapeVisibility?: TLEditorOptions['getShapeVisibility']
@@ -3972,6 +3991,94 @@ export class Editor extends EventEmitter<TLEventMap> {
3972
3991
  return this.getCollaborators().filter((c) => c.currentPageId === currentPageId)
3973
3992
  }
3974
3993
 
3994
+ // Attribution
3995
+
3996
+ /**
3997
+ * Get the current user's ID for attribution purposes.
3998
+ * Also ensures a `user:` record exists in the store for the current user.
3999
+ * Returns `null` when the user store has no current user.
4000
+ *
4001
+ * @public
4002
+ */
4003
+ getAttributionUserId(): string | null {
4004
+ const user = this.store.props.users.currentUser.get()
4005
+ if (!user) return null
4006
+ this._ensureUserRecord(user)
4007
+ return UserRecordType.parseId(user.id)
4008
+ }
4009
+
4010
+ /**
4011
+ * Ensure a user record exists in the store for the given user,
4012
+ * updating it if the data has changed.
4013
+ *
4014
+ * @internal
4015
+ */
4016
+ _ensureUserRecord(user: TLUser): void {
4017
+ const existing = this.store.get(user.id)
4018
+ if (
4019
+ existing &&
4020
+ existing.name === user.name &&
4021
+ existing.color === user.color &&
4022
+ existing.imageUrl === user.imageUrl &&
4023
+ existing.meta === user.meta
4024
+ ) {
4025
+ return
4026
+ }
4027
+ this.run(
4028
+ () => {
4029
+ this.store.put([user])
4030
+ },
4031
+ { history: 'ignore' }
4032
+ )
4033
+ }
4034
+
4035
+ /**
4036
+ * Resolve a display name for a user ID. Asks the
4037
+ * {@link @tldraw/tlschema#TLUserStore} first (the app's source of truth),
4038
+ * falling back to the `user:` record in the store.
4039
+ *
4040
+ * @public
4041
+ */
4042
+ getAttributionDisplayName(userId: string | null): string | null {
4043
+ if (!userId) return null
4044
+ return (
4045
+ this.store.props.users.resolve(userId).get()?.name ??
4046
+ this.store.get(createUserId(userId))?.name ??
4047
+ null
4048
+ )
4049
+ }
4050
+
4051
+ /**
4052
+ * Resolve a user record by ID. Asks the
4053
+ * {@link @tldraw/tlschema#TLUserStore} first (the app's source of truth),
4054
+ * falling back to the `user:` record in the store.
4055
+ *
4056
+ * @public
4057
+ */
4058
+ getAttributionUser(userId: string | null): TLUser | null {
4059
+ if (!userId) return null
4060
+ return (
4061
+ this.store.props.users.resolve(userId).get() ?? this.store.get(createUserId(userId)) ?? null
4062
+ )
4063
+ }
4064
+
4065
+ /**
4066
+ * Collect user IDs referenced by a set of shapes via shape-specific props
4067
+ * (e.g. `textFirstEditedBy` on notes).
4068
+ *
4069
+ * @internal
4070
+ */
4071
+ _getReferencedUserIds(shapes: TLShape[]): Set<string> {
4072
+ const userIds = new Set<string>()
4073
+ for (const shape of shapes) {
4074
+ const util = this.getShapeUtil(shape)
4075
+ for (const id of util.getReferencedUserIds(shape)) {
4076
+ userIds.add(id)
4077
+ }
4078
+ }
4079
+ return userIds
4080
+ }
4081
+
3975
4082
  // Following
3976
4083
 
3977
4084
  // When we are 'locked on' to a user, our camera is derived from their camera.
@@ -9071,6 +9178,7 @@ export class Editor extends EventEmitter<TLEventMap> {
9071
9178
  opts = {} as { force?: boolean }
9072
9179
  ): Promise<void> {
9073
9180
  if (!opts.force && this.getIsReadonly()) return
9181
+
9074
9182
  return this.externalContentHandlers[info.type]?.(info as any)
9075
9183
  }
9076
9184
 
@@ -9156,12 +9264,23 @@ export class Editor extends EventEmitter<TLEventMap> {
9156
9264
  assets.push(asset)
9157
9265
  }
9158
9266
 
9267
+ const users: TLUser[] = []
9268
+ const seenUserIds = new Set<TLUserId>()
9269
+ for (const userId of this._getReferencedUserIds(shapes)) {
9270
+ const recordId = createUserId(userId)
9271
+ if (seenUserIds.has(recordId)) continue
9272
+ seenUserIds.add(recordId)
9273
+ const user = this.store.get(recordId)
9274
+ if (user) users.push(user)
9275
+ }
9276
+
9159
9277
  return {
9160
9278
  schema: this.store.schema.serialize(),
9161
9279
  shapes,
9162
9280
  rootShapeIds,
9163
9281
  bindings,
9164
9282
  assets,
9283
+ users,
9165
9284
  }
9166
9285
  })
9167
9286
  }
@@ -9237,6 +9356,7 @@ export class Editor extends EventEmitter<TLEventMap> {
9237
9356
  const assets: TLAsset[] = []
9238
9357
  const shapes: TLShape[] = []
9239
9358
  const bindings: TLBinding[] = []
9359
+ const users: TLUser[] = []
9240
9360
 
9241
9361
  // Let's treat the content as a store, and then migrate that store.
9242
9362
  const store: StoreSnapshot<TLRecord> = {
@@ -9246,6 +9366,7 @@ export class Editor extends EventEmitter<TLEventMap> {
9246
9366
  ...Object.fromEntries(
9247
9367
  content.bindings?.map((bindings) => [bindings.id, bindings] as const) ?? []
9248
9368
  ),
9369
+ ...Object.fromEntries(content.users?.map((user) => [user.id, user] as const) ?? []),
9249
9370
  },
9250
9371
  schema: content.schema,
9251
9372
  }
@@ -9267,6 +9388,23 @@ export class Editor extends EventEmitter<TLEventMap> {
9267
9388
  bindings.push(record)
9268
9389
  break
9269
9390
  }
9391
+ case 'user': {
9392
+ users.push(record)
9393
+ break
9394
+ }
9395
+ }
9396
+ }
9397
+
9398
+ if (users.length > 0) {
9399
+ const existingUserIds = new Set(
9400
+ this.store
9401
+ .allRecords()
9402
+ .filter((r): r is TLUser => r.typeName === 'user')
9403
+ .map((r) => r.id)
9404
+ )
9405
+ const usersToCreate = users.filter((u) => !existingUserIds.has(u.id))
9406
+ if (usersToCreate.length > 0) {
9407
+ this.store.put(usersToCreate)
9270
9408
  }
9271
9409
  }
9272
9410
 
@@ -1,5 +1,5 @@
1
- import { Vec } from '../../../primitives/Vec'
2
1
  import { EASINGS } from '../../../primitives/easings'
2
+ import { Vec } from '../../../primitives/Vec'
3
3
  import type { Editor } from '../../Editor'
4
4
 
5
5
  /** @public */
@@ -10,8 +10,8 @@ import {
10
10
  isSelectionCorner,
11
11
  } from '../../../primitives/Box'
12
12
  import { Mat } from '../../../primitives/Mat'
13
- import { Vec } from '../../../primitives/Vec'
14
13
  import { rangeIntersection, rangesOverlap } from '../../../primitives/utils'
14
+ import { Vec } from '../../../primitives/Vec'
15
15
  import type { Editor } from '../../Editor'
16
16
  import type {
17
17
  GapsSnapIndicator,
@@ -1,8 +1,8 @@
1
1
  import { computed } from '@tldraw/state'
2
2
  import { TLHandle, TLShape, TLShapeId, VecModel } from '@tldraw/tlschema'
3
3
  import { assertExists, uniqueId } from '@tldraw/utils'
4
- import { Vec } from '../../../primitives/Vec'
5
4
  import { Geometry2d } from '../../../primitives/geometry/Geometry2d'
5
+ import { Vec } from '../../../primitives/Vec'
6
6
  import type { Editor } from '../../Editor'
7
7
  import type { PointsSnapIndicator, SnapData, SnapManager } from './SnapManager'
8
8
 
@@ -1,7 +1,7 @@
1
1
  import { Computed, RESET_VALUE, computed, isUninitialized } from '@tldraw/state'
2
2
  import type { RecordsDiff } from '@tldraw/store'
3
- import type { TLRecord } from '@tldraw/tlschema'
4
3
  import { TLPageId, TLShape, TLShapeId, isShape } from '@tldraw/tlschema'
4
+ import type { TLRecord } from '@tldraw/tlschema'
5
5
  import { objectMapValues } from '@tldraw/utils'
6
6
  import { Box } from '../../../primitives/Box'
7
7
  import type { Editor } from '../../Editor'
@@ -74,7 +74,7 @@ export class SpatialIndexManager {
74
74
  // Collect all shape elements for bulk loading
75
75
  for (const shape of shapes) {
76
76
  const bounds = this.editor.getShapePageBounds(shape.id)
77
- if (bounds) {
77
+ if (bounds && bounds.isValid()) {
78
78
  elements.push({
79
79
  minX: bounds.minX,
80
80
  minY: bounds.minY,
@@ -101,7 +101,7 @@ export class SpatialIndexManager {
101
101
  for (const shape of objectMapValues(changes.added) as TLShape[]) {
102
102
  if (isShape(shape) && this.editor.getAncestorPageId(shape) === this.lastPageId) {
103
103
  const bounds = this.editor.getShapePageBounds(shape.id)
104
- if (bounds) {
104
+ if (bounds && bounds.isValid()) {
105
105
  this.rbush.upsert(shape.id, bounds)
106
106
  }
107
107
  processedShapeIds.add(shape.id)
@@ -125,7 +125,7 @@ export class SpatialIndexManager {
125
125
 
126
126
  if (isOnPage) {
127
127
  const bounds = this.editor.getShapePageBounds(to.id)
128
- if (bounds) {
128
+ if (bounds && bounds.isValid()) {
129
129
  this.rbush.upsert(to.id, bounds)
130
130
  }
131
131
  } else {
@@ -145,7 +145,7 @@ export class SpatialIndexManager {
145
145
  const indexedBounds = this.rbush.getBounds(shapeId)
146
146
 
147
147
  if (!this.areBoundsEqual(currentBounds, indexedBounds)) {
148
- if (currentBounds) {
148
+ if (currentBounds && currentBounds.isValid()) {
149
149
  this.rbush.upsert(shapeId, currentBounds)
150
150
  } else {
151
151
  this.rbush.remove(shapeId)
@@ -3,6 +3,22 @@ import { Editor } from '../../Editor'
3
3
  import { TextManager, TLMeasureTextSpanOpts } from './TextManager'
4
4
 
5
5
  // Create a simple mock DOM environment
6
+ function createMockStyle() {
7
+ const properties = new Map<string, string>()
8
+
9
+ return {
10
+ setProperty: vi.fn((key: string, value: string) => {
11
+ properties.set(key, value)
12
+ }),
13
+ getPropertyValue: vi.fn((key: string) => properties.get(key) ?? ''),
14
+ removeProperty: vi.fn((key: string) => {
15
+ const previous = properties.get(key) ?? ''
16
+ properties.delete(key)
17
+ return previous
18
+ }),
19
+ }
20
+ }
21
+
6
22
  const mockElement = {
7
23
  classList: { add: vi.fn() },
8
24
  tabIndex: -1,
@@ -10,10 +26,7 @@ const mockElement = {
10
26
  innerHTML: '',
11
27
  textContent: '',
12
28
  setAttribute: vi.fn(),
13
- style: {
14
- setProperty: vi.fn(),
15
- getPropertyValue: vi.fn(() => ''),
16
- },
29
+ style: createMockStyle(),
17
30
  scrollWidth: 100,
18
31
  getBoundingClientRect: vi.fn(() => ({
19
32
  width: 100,
@@ -31,6 +44,7 @@ const mockElement = {
31
44
  // Mock document.createElement to return our mock element
32
45
  const mockCreateElement = vi.fn(() => {
33
46
  const element = { ...mockElement }
47
+ element.style = createMockStyle()
34
48
  element.cloneNode = vi.fn(() => ({ ...element }))
35
49
 
36
50
  // Make textContent and innerHTML reactive like real DOM elements