@tldraw/editor 4.6.0-next.74727ac95b72 → 4.6.0-next.a31a8a6b83f2

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 (126) hide show
  1. package/dist-cjs/index.d.ts +434 -114
  2. package/dist-cjs/index.js +16 -6
  3. package/dist-cjs/index.js.map +3 -3
  4. package/dist-cjs/lib/TldrawEditor.js +59 -14
  5. package/dist-cjs/lib/TldrawEditor.js.map +3 -3
  6. package/dist-cjs/lib/components/default-components/CanvasShapeIndicators.js +3 -3
  7. package/dist-cjs/lib/components/default-components/CanvasShapeIndicators.js.map +2 -2
  8. package/dist-cjs/lib/config/{createTLUser.js → createTLCurrentUser.js} +9 -9
  9. package/dist-cjs/lib/config/createTLCurrentUser.js.map +7 -0
  10. package/dist-cjs/lib/config/createTLStore.js +30 -0
  11. package/dist-cjs/lib/config/createTLStore.js.map +2 -2
  12. package/dist-cjs/lib/config/defaultAssets.js +36 -0
  13. package/dist-cjs/lib/config/defaultAssets.js.map +7 -0
  14. package/dist-cjs/lib/editor/Editor.js +310 -8
  15. package/dist-cjs/lib/editor/Editor.js.map +3 -3
  16. package/dist-cjs/lib/editor/assets/AssetUtil.js +66 -0
  17. package/dist-cjs/lib/editor/assets/AssetUtil.js.map +7 -0
  18. package/dist-cjs/lib/editor/managers/FontManager/FontManager.js.map +2 -2
  19. package/dist-cjs/lib/editor/managers/ThemeManager/ThemeManager.js +106 -0
  20. package/dist-cjs/lib/editor/managers/ThemeManager/ThemeManager.js.map +7 -0
  21. package/dist-cjs/lib/editor/managers/ThemeManager/defaultThemes.js +586 -0
  22. package/dist-cjs/lib/editor/managers/ThemeManager/defaultThemes.js.map +7 -0
  23. package/dist-cjs/lib/editor/managers/UserPreferencesManager/UserPreferencesManager.js +6 -4
  24. package/dist-cjs/lib/editor/managers/UserPreferencesManager/UserPreferencesManager.js.map +2 -2
  25. package/dist-cjs/lib/editor/shapes/ShapeUtil.js +19 -0
  26. package/dist-cjs/lib/editor/shapes/ShapeUtil.js.map +2 -2
  27. package/dist-cjs/lib/editor/types/SvgExportContext.js.map +2 -2
  28. package/dist-cjs/lib/editor/types/clipboard-types.js.map +1 -1
  29. package/dist-cjs/lib/editor/types/external-content.js.map +1 -1
  30. package/dist-cjs/lib/exports/getSvgJsx.js +12 -7
  31. package/dist-cjs/lib/exports/getSvgJsx.js.map +2 -2
  32. package/dist-cjs/lib/globals/environment.js +18 -1
  33. package/dist-cjs/lib/globals/environment.js.map +2 -2
  34. package/dist-cjs/lib/hooks/{useIsDarkMode.js → useColorMode.js} +14 -10
  35. package/dist-cjs/lib/hooks/useColorMode.js.map +7 -0
  36. package/dist-cjs/lib/hooks/useCursor.js +3 -7
  37. package/dist-cjs/lib/hooks/useCursor.js.map +2 -2
  38. package/dist-cjs/lib/hooks/useDarkMode.js +4 -4
  39. package/dist-cjs/lib/hooks/useDarkMode.js.map +2 -2
  40. package/dist-cjs/lib/hooks/useGestureEvents.js +171 -127
  41. package/dist-cjs/lib/hooks/useGestureEvents.js.map +3 -3
  42. package/dist-cjs/lib/utils/reparenting.js +2 -1
  43. package/dist-cjs/lib/utils/reparenting.js.map +2 -2
  44. package/dist-cjs/lib/utils/richText.js.map +2 -2
  45. package/dist-cjs/version.js +3 -3
  46. package/dist-cjs/version.js.map +1 -1
  47. package/dist-esm/index.d.mts +434 -114
  48. package/dist-esm/index.mjs +21 -9
  49. package/dist-esm/index.mjs.map +2 -2
  50. package/dist-esm/lib/TldrawEditor.mjs +62 -14
  51. package/dist-esm/lib/TldrawEditor.mjs.map +3 -3
  52. package/dist-esm/lib/components/default-components/CanvasShapeIndicators.mjs +3 -3
  53. package/dist-esm/lib/components/default-components/CanvasShapeIndicators.mjs.map +2 -2
  54. package/dist-esm/lib/config/{createTLUser.mjs → createTLCurrentUser.mjs} +6 -6
  55. package/dist-esm/lib/config/createTLCurrentUser.mjs.map +7 -0
  56. package/dist-esm/lib/config/createTLStore.mjs +36 -1
  57. package/dist-esm/lib/config/createTLStore.mjs.map +2 -2
  58. package/dist-esm/lib/config/defaultAssets.mjs +16 -0
  59. package/dist-esm/lib/config/defaultAssets.mjs.map +7 -0
  60. package/dist-esm/lib/editor/Editor.mjs +312 -8
  61. package/dist-esm/lib/editor/Editor.mjs.map +3 -3
  62. package/dist-esm/lib/editor/assets/AssetUtil.mjs +46 -0
  63. package/dist-esm/lib/editor/assets/AssetUtil.mjs.map +7 -0
  64. package/dist-esm/lib/editor/managers/FontManager/FontManager.mjs.map +2 -2
  65. package/dist-esm/lib/editor/managers/ThemeManager/ThemeManager.mjs +88 -0
  66. package/dist-esm/lib/editor/managers/ThemeManager/ThemeManager.mjs.map +7 -0
  67. package/dist-esm/lib/editor/managers/ThemeManager/defaultThemes.mjs +568 -0
  68. package/dist-esm/lib/editor/managers/ThemeManager/defaultThemes.mjs.map +7 -0
  69. package/dist-esm/lib/editor/managers/UserPreferencesManager/UserPreferencesManager.mjs +6 -4
  70. package/dist-esm/lib/editor/managers/UserPreferencesManager/UserPreferencesManager.mjs.map +2 -2
  71. package/dist-esm/lib/editor/shapes/ShapeUtil.mjs +19 -0
  72. package/dist-esm/lib/editor/shapes/ShapeUtil.mjs.map +2 -2
  73. package/dist-esm/lib/editor/types/SvgExportContext.mjs.map +2 -2
  74. package/dist-esm/lib/exports/getSvgJsx.mjs +12 -10
  75. package/dist-esm/lib/exports/getSvgJsx.mjs.map +2 -2
  76. package/dist-esm/lib/globals/environment.mjs +18 -1
  77. package/dist-esm/lib/globals/environment.mjs.map +2 -2
  78. package/dist-esm/lib/hooks/useColorMode.mjs +19 -0
  79. package/dist-esm/lib/hooks/useColorMode.mjs.map +7 -0
  80. package/dist-esm/lib/hooks/useCursor.mjs +3 -7
  81. package/dist-esm/lib/hooks/useCursor.mjs.map +2 -2
  82. package/dist-esm/lib/hooks/useDarkMode.mjs +4 -4
  83. package/dist-esm/lib/hooks/useDarkMode.mjs.map +2 -2
  84. package/dist-esm/lib/hooks/useGestureEvents.mjs +171 -127
  85. package/dist-esm/lib/hooks/useGestureEvents.mjs.map +3 -3
  86. package/dist-esm/lib/utils/reparenting.mjs +2 -1
  87. package/dist-esm/lib/utils/reparenting.mjs.map +2 -2
  88. package/dist-esm/lib/utils/richText.mjs.map +2 -2
  89. package/dist-esm/version.mjs +3 -3
  90. package/dist-esm/version.mjs.map +1 -1
  91. package/editor.css +13 -33
  92. package/package.json +8 -9
  93. package/src/index.ts +14 -7
  94. package/src/lib/TldrawEditor.tsx +98 -19
  95. package/src/lib/components/default-components/CanvasShapeIndicators.tsx +3 -3
  96. package/src/lib/config/{createTLUser.ts → createTLCurrentUser.ts} +6 -6
  97. package/src/lib/config/createTLStore.ts +57 -2
  98. package/src/lib/config/defaultAssets.ts +19 -0
  99. package/src/lib/editor/Editor.ts +424 -29
  100. package/src/lib/editor/assets/AssetUtil.ts +85 -0
  101. package/src/lib/editor/managers/FontManager/FontManager.test.ts +9 -2
  102. package/src/lib/editor/managers/FontManager/FontManager.ts +1 -67
  103. package/src/lib/editor/managers/ThemeManager/ThemeManager.ts +116 -0
  104. package/src/lib/editor/managers/ThemeManager/defaultThemes.ts +605 -0
  105. package/src/lib/editor/managers/UserPreferencesManager/UserPreferencesManager.test.ts +25 -31
  106. package/src/lib/editor/managers/UserPreferencesManager/UserPreferencesManager.ts +7 -5
  107. package/src/lib/editor/shapes/ShapeUtil.ts +37 -1
  108. package/src/lib/editor/types/SvgExportContext.tsx +5 -0
  109. package/src/lib/editor/types/clipboard-types.ts +2 -1
  110. package/src/lib/editor/types/external-content.ts +1 -0
  111. package/src/lib/exports/getSvgJsx.tsx +21 -15
  112. package/src/lib/globals/environment.ts +18 -0
  113. package/src/lib/hooks/{useIsDarkMode.ts → useColorMode.ts} +9 -5
  114. package/src/lib/hooks/useCursor.ts +3 -7
  115. package/src/lib/hooks/useDarkMode.ts +4 -4
  116. package/src/lib/hooks/useGestureEvents.ts +240 -168
  117. package/src/lib/primitives/Box.test.ts +30 -0
  118. package/src/lib/primitives/geometry/Geometry2d.test.ts +21 -0
  119. package/src/lib/utils/reparenting.ts +6 -2
  120. package/src/lib/utils/richText.ts +1 -1
  121. package/src/version.ts +3 -3
  122. package/dist-cjs/lib/config/createTLUser.js.map +0 -7
  123. package/dist-cjs/lib/hooks/useIsDarkMode.js.map +0 -7
  124. package/dist-esm/lib/config/createTLUser.mjs.map +0 -7
  125. package/dist-esm/lib/hooks/useIsDarkMode.mjs +0 -15
  126. package/dist-esm/lib/hooks/useIsDarkMode.mjs.map +0 -7
@@ -50,9 +50,16 @@ import {
50
50
  TLShapePartial,
51
51
  TLStore,
52
52
  TLStoreSnapshot,
53
+ TLTheme,
54
+ TLThemeId,
55
+ TLThemes,
56
+ TLUser,
57
+ TLUserId,
53
58
  TLVideoAsset,
59
+ UserRecordType,
54
60
  createBindingId,
55
61
  createShapeId,
62
+ createUserId,
56
63
  getShapePropKeysByStyle,
57
64
  isPageId,
58
65
  isShapeId,
@@ -90,7 +97,8 @@ import {
90
97
  uniqueId,
91
98
  } from '@tldraw/utils'
92
99
  import EventEmitter from 'eventemitter3'
93
- import { TLUser, createTLUser } from '../config/createTLUser'
100
+ import { TLCurrentUser, createTLCurrentUser } from '../config/createTLCurrentUser'
101
+ import { TLAnyAssetUtilConstructor, checkAssets } from '../config/defaultAssets'
94
102
  import { TLAnyBindingUtilConstructor, checkBindings } from '../config/defaultBindings'
95
103
  import { TLAnyShapeUtilConstructor, checkShapesAndAddCore } from '../config/defaultShapes'
96
104
  import {
@@ -137,6 +145,7 @@ import { getDroppedShapesToNewParents, kickoutOccludedShapes } from '../utils/re
137
145
  import { TLTextOptions, TiptapEditor } from '../utils/richText'
138
146
  import { applyRotationToSnapshotShapes, getRotationSnapshot } from '../utils/rotation'
139
147
  import { ReadonlySharedStyleMap, SharedStyle, SharedStyleMap } from '../utils/SharedStylesMap'
148
+ import { AssetUtil } from './assets/AssetUtil'
140
149
  import { BindingOnDeleteOptions, BindingUtil } from './bindings/BindingUtil'
141
150
  import { bindingsIndex } from './derivations/bindingsIndex'
142
151
  import { notVisibleShapes } from './derivations/notVisibleShapes'
@@ -152,6 +161,7 @@ import { ScribbleManager } from './managers/ScribbleManager/ScribbleManager'
152
161
  import { SnapManager } from './managers/SnapManager/SnapManager'
153
162
  import { SpatialIndexManager } from './managers/SpatialIndexManager/SpatialIndexManager'
154
163
  import { TextManager } from './managers/TextManager/TextManager'
164
+ import { resolveThemes, ThemeManager } from './managers/ThemeManager/ThemeManager'
155
165
  import { TickManager } from './managers/TickManager/TickManager'
156
166
  import { UserPreferencesManager } from './managers/UserPreferencesManager/UserPreferencesManager'
157
167
  import {
@@ -210,18 +220,17 @@ export interface TLEditorOptions {
210
220
  */
211
221
  bindingUtils: readonly TLAnyBindingUtilConstructor[]
212
222
  /**
213
- * An array of tools to use in the editor. These will be used to handle events and manage user interactions in the editor.
223
+ * An array of asset utils to use in the editor. These will be used to handle asset-type-specific behavior.
214
224
  */
215
- tools: readonly TLStateNodeConstructor[]
225
+ assetUtils?: readonly TLAnyAssetUtilConstructor[]
216
226
  /**
217
- * Should return a containing html element which has all the styles applied to the editor. If not
218
- * given, the body element will be used.
227
+ * An array of tools to use in the editor. These will be used to handle events and manage user interactions in the editor.
219
228
  */
220
- getContainer(): HTMLElement
229
+ tools: readonly TLStateNodeConstructor[]
221
230
  /**
222
231
  * A user defined externally to replace the default user.
223
232
  */
224
- user?: TLUser
233
+ user?: TLCurrentUser
225
234
  /**
226
235
  * The editor's initial active tool (or other state node id).
227
236
  */
@@ -230,25 +239,13 @@ export interface TLEditorOptions {
230
239
  * Whether to automatically focus the editor when it mounts.
231
240
  */
232
241
  autoFocus?: boolean
233
- /**
234
- * Whether to infer dark mode from the user's system preferences. Defaults to false.
235
- */
236
- inferDarkMode?: boolean
237
- /**
238
- * Options for the editor's camera.
239
- *
240
- * @deprecated Use `options.cameraOptions` instead. This will be removed in a future release.
241
- */
242
- cameraOptions?: Partial<TLCameraOptions>
243
- options?: Partial<TldrawOptions>
244
- /**
245
- * Text options for the editor.
246
- *
247
- * @deprecated Use `options.text` instead. This prop will be removed in a future release.
248
- */
249
- textOptions?: TLTextOptions
250
242
  licenseKey?: string
251
243
  fontAssetUrls?: { [key: string]: string | undefined }
244
+ /**
245
+ * Should return a containing html element which has all the styles applied to the editor. If not
246
+ * given, the body element will be used.
247
+ */
248
+ getContainer(): HTMLElement
252
249
  /**
253
250
  * Provides a way to hide shapes.
254
251
  *
@@ -268,6 +265,41 @@ export interface TLEditorOptions {
268
265
  shape: TLShape,
269
266
  editor: Editor
270
267
  ): 'visible' | 'hidden' | 'inherit' | null | undefined
268
+ /**
269
+ * Named theme definitions for the editor. Each theme contains shared
270
+ * properties (font size, line height, stroke width) and color palettes
271
+ * for both light and dark modes.
272
+ */
273
+ themes?: Partial<TLThemes>
274
+ /**
275
+ * The id of the initially active theme. Defaults to `'default'`.
276
+ */
277
+ initialTheme?: TLThemeId
278
+ /**
279
+ * The editor's color scheme preference, controls the default color mode. Defaults to `'light'`.
280
+ *
281
+ * - `'light'` - Always use light mode.
282
+ * - `'dark'` - Always use dark mode.
283
+ * - `'system'` - Follow the OS color scheme preference.
284
+ */
285
+ colorScheme?: 'light' | 'dark' | 'system'
286
+ /**
287
+ * Additional configuration options for the tldraw editor.
288
+ */
289
+ options?: Partial<TldrawOptions>
290
+ // --- Deprecated ----
291
+ /**
292
+ * Options for the editor's camera.
293
+ *
294
+ * @deprecated Use `options.cameraOptions` instead. This will be removed in a future release.
295
+ */
296
+ cameraOptions?: Partial<TLCameraOptions>
297
+ /**
298
+ * Text options for the editor.
299
+ *
300
+ * @deprecated Use `options.text` instead. This prop will be removed in a future release.
301
+ */
302
+ textOptions?: TLTextOptions
271
303
  }
272
304
 
273
305
  /**
@@ -296,6 +328,7 @@ export class Editor extends EventEmitter<TLEventMap> {
296
328
  user,
297
329
  shapeUtils,
298
330
  bindingUtils,
331
+ assetUtils: assetUtilConstructors,
299
332
  tools,
300
333
  getContainer,
301
334
  // needs to be here for backwards compatibility with TldrawEditor
@@ -303,13 +336,15 @@ export class Editor extends EventEmitter<TLEventMap> {
303
336
  cameraOptions,
304
337
  initialState,
305
338
  autoFocus,
306
- inferDarkMode,
307
339
  options: _options,
308
340
  // needs to be here for backwards compatibility with TldrawEditor
309
341
  // eslint-disable-next-line @typescript-eslint/no-deprecated
310
342
  textOptions: _textOptions,
311
343
  getShapeVisibility,
344
+ colorScheme,
312
345
  fontAssetUrls,
346
+ themes,
347
+ initialTheme,
313
348
  }: TLEditorOptions) {
314
349
  super()
315
350
 
@@ -344,19 +379,32 @@ export class Editor extends EventEmitter<TLEventMap> {
344
379
  ...options?.camera,
345
380
  })
346
381
 
382
+ this.getContainer = getContainer
383
+
347
384
  this._textOptions = atom('text options', options?.text ?? null)
348
385
 
349
- this.user = new UserPreferencesManager(user ?? createTLUser(), inferDarkMode ?? false)
386
+ this.user = new UserPreferencesManager(user ?? createTLCurrentUser(), colorScheme ?? 'light')
350
387
  this.disposables.add(() => this.user.dispose())
351
388
 
352
- this.getContainer = getContainer
353
-
354
389
  this.textMeasure = new TextManager(this)
355
390
  this.disposables.add(() => this.textMeasure.dispose())
356
391
 
357
- this.fonts = new FontManager(this, fontAssetUrls)
392
+ this._themeManager = new ThemeManager(this, {
393
+ themes: resolveThemes(themes),
394
+ initial: initialTheme ?? 'default',
395
+ })
396
+ this.disposables.add(() => this._themeManager.dispose())
358
397
 
359
398
  this._tickManager = new TickManager(this)
399
+ this.disposables.add(() => this._tickManager.dispose())
400
+ this.disposables.add(() => {
401
+ // Reset camera state to 'idle' so the store isn't left stuck at 'moving'
402
+ // when tick events stop (e.g. React strict mode disposes while camera is moving)
403
+ this.off('tick', this._decayCameraStateTimeout)
404
+ this._setCameraState('idle')
405
+ })
406
+
407
+ this.fonts = new FontManager(this, fontAssetUrls)
360
408
 
361
409
  this.inputs = new InputsManager(this)
362
410
 
@@ -396,6 +444,17 @@ export class Editor extends EventEmitter<TLEventMap> {
396
444
  this.shapeUtils = _shapeUtils
397
445
  this.styleProps = _styleProps
398
446
 
447
+ const _shapeUtilsByAssetType = {} as Record<string, ShapeUtil<any>>
448
+ for (const Util of allShapeUtils) {
449
+ const assetTypes = Util.handledAssetTypes
450
+ if (assetTypes) {
451
+ for (const assetType of assetTypes) {
452
+ _shapeUtilsByAssetType[assetType] = _shapeUtils[Util.type]
453
+ }
454
+ }
455
+ }
456
+ this._shapeUtilsByAssetType = _shapeUtilsByAssetType
457
+
399
458
  const allBindingUtils = checkBindings(bindingUtils)
400
459
  const _bindingUtils = {} as Record<string, BindingUtil<any>>
401
460
  for (const Util of allBindingUtils) {
@@ -404,6 +463,17 @@ export class Editor extends EventEmitter<TLEventMap> {
404
463
  }
405
464
  this.bindingUtils = _bindingUtils
406
465
 
466
+ // Asset utils
467
+ if (assetUtilConstructors) {
468
+ const allAssetUtils = checkAssets(assetUtilConstructors)
469
+ const _assetUtils = {} as Record<string, AssetUtil<any>>
470
+ for (const Util of allAssetUtils) {
471
+ const util = new Util(this)
472
+ _assetUtils[Util.type] = util
473
+ }
474
+ this.assetUtils = _assetUtils
475
+ }
476
+
407
477
  // Tools.
408
478
  // Accept tools from constructor parameters which may not conflict with the root note's default or
409
479
  // "baked in" tools, select and zoom.
@@ -820,6 +890,15 @@ export class Editor extends EventEmitter<TLEventMap> {
820
890
  })
821
891
  )
822
892
  }
893
+
894
+ this.disposables.add(
895
+ react('sync current user record', () => {
896
+ const user = this.store.props.users.currentUser.get()
897
+ if (user) {
898
+ this._ensureUserRecord(user)
899
+ }
900
+ })
901
+ )
823
902
  }
824
903
 
825
904
  private readonly _getShapeVisibility?: TLEditorOptions['getShapeVisibility']
@@ -929,6 +1008,11 @@ export class Editor extends EventEmitter<TLEventMap> {
929
1008
  */
930
1009
  readonly snaps: SnapManager
931
1010
 
1011
+ /**
1012
+ * A manager for the spatial index, tracking where shapes exist on the canvas.
1013
+ *
1014
+ * @internal
1015
+ */
932
1016
  private readonly _spatialIndex: SpatialIndexManager
933
1017
 
934
1018
  /**
@@ -946,6 +1030,13 @@ export class Editor extends EventEmitter<TLEventMap> {
946
1030
  */
947
1031
  readonly user: UserPreferencesManager
948
1032
 
1033
+ /**
1034
+ * A manager for the editor's themes.
1035
+ *
1036
+ * @internal
1037
+ */
1038
+ private readonly _themeManager: ThemeManager
1039
+
949
1040
  /**
950
1041
  * A helper for measuring text.
951
1042
  *
@@ -1026,13 +1117,134 @@ export class Editor extends EventEmitter<TLEventMap> {
1026
1117
  * @public
1027
1118
  */
1028
1119
  dispose() {
1120
+ // Stop any in-progress camera animations and following before
1121
+ // running disposables, so their cleanup listeners fire first
1122
+ this.stopCameraAnimation()
1123
+ if (this.getInstanceState().followingUserId) {
1124
+ this.stopFollowingUser()
1125
+ }
1126
+
1029
1127
  this.disposables.forEach((dispose) => dispose())
1030
1128
  this.disposables.clear()
1129
+
1130
+ // Clear any open menus for this editor's context
1131
+ this.menus.clearOpenMenus()
1132
+
1031
1133
  this.store.dispose()
1032
1134
  this.isDisposed = true
1033
1135
  this.emit('dispose')
1034
1136
  }
1035
1137
 
1138
+ /* ------------------ Themes (shadowing the theme manager) ------------------ */
1139
+
1140
+ /**
1141
+ * Get the current color mode (`'light'` or `'dark'`), based on the user's dark mode preference.
1142
+ *
1143
+ * @public
1144
+ */
1145
+ getColorMode(): 'light' | 'dark' {
1146
+ return this._themeManager.getColorMode()
1147
+ }
1148
+
1149
+ /**
1150
+ * Set the color mode. Note that this is a convenience method that passes the mode to
1151
+ * `user.updateUserPreferences`, which is the source of truth for the user's color mode preference.
1152
+ *
1153
+ * @public
1154
+ */
1155
+ setColorMode(mode: 'light' | 'dark') {
1156
+ this.user.updateUserPreferences({ colorScheme: mode })
1157
+ return this
1158
+ }
1159
+
1160
+ /**
1161
+ * Get the id of the current theme.
1162
+ *
1163
+ * @public
1164
+ */
1165
+ getCurrentThemeId(): TLThemeId {
1166
+ return this._themeManager.getCurrentThemeId()
1167
+ }
1168
+
1169
+ /**
1170
+ * Get the current theme definition.
1171
+ *
1172
+ * @public
1173
+ */
1174
+ getCurrentTheme(): TLTheme {
1175
+ return this._themeManager.getCurrentTheme()
1176
+ }
1177
+
1178
+ /**
1179
+ * Set the current theme by id.
1180
+ *
1181
+ * @public
1182
+ */
1183
+ setCurrentTheme(id: TLThemeId) {
1184
+ this._themeManager.setCurrentTheme(id)
1185
+ return this
1186
+ }
1187
+
1188
+ /**
1189
+ * Get all registered theme definitions.
1190
+ *
1191
+ * @public
1192
+ */
1193
+ getThemes(): TLThemes {
1194
+ return this._themeManager.getThemes()
1195
+ }
1196
+
1197
+ /**
1198
+ * Get a single theme definition by id.
1199
+ *
1200
+ * @public
1201
+ */
1202
+ getTheme(id: TLThemeId): TLTheme | undefined {
1203
+ return this._themeManager.getTheme(id)
1204
+ }
1205
+
1206
+ /**
1207
+ * Replace all theme definitions, or update them via a callback that receives a deep copy.
1208
+ * The `'default'` theme must always be present in the result.
1209
+ *
1210
+ * @example
1211
+ * ```ts
1212
+ * // Replace all themes
1213
+ * editor.updateThemes({ default: myDefaultTheme, ocean: myOceanTheme })
1214
+ *
1215
+ * // Update via callback
1216
+ * editor.updateThemes((themes) => {
1217
+ * delete themes.ocean
1218
+ * return themes
1219
+ * })
1220
+ * ```
1221
+ *
1222
+ * @public
1223
+ */
1224
+ updateThemes(themes: TLThemes | ((themes: TLThemes) => TLThemes)) {
1225
+ this._themeManager.updateThemes(themes)
1226
+ return this
1227
+ }
1228
+
1229
+ /**
1230
+ * Register or update a single theme definition. The theme is keyed by its `id` property.
1231
+ *
1232
+ * @example
1233
+ * ```ts
1234
+ * // Override a property on the default theme
1235
+ * editor.updateTheme({ ...editor.getTheme('default')!, fontSize: 24 })
1236
+ *
1237
+ * // Register a new theme
1238
+ * editor.updateTheme({ id: 'ocean', ...myOceanTheme })
1239
+ * ```
1240
+ *
1241
+ * @public
1242
+ */
1243
+ updateTheme(theme: TLTheme) {
1244
+ this._themeManager.updateTheme(theme)
1245
+ return this
1246
+ }
1247
+
1036
1248
  /* ------------------- Shape Utils ------------------ */
1037
1249
 
1038
1250
  /**
@@ -1042,6 +1254,9 @@ export class Editor extends EventEmitter<TLEventMap> {
1042
1254
  */
1043
1255
  shapeUtils: { readonly [K in string]?: ShapeUtil<TLShape> }
1044
1256
 
1257
+ /** @internal */
1258
+ private _shapeUtilsByAssetType: { readonly [K in string]?: ShapeUtil<TLShape> } = {}
1259
+
1045
1260
  styleProps: { [key: string]: Map<StyleProp<any>, string> }
1046
1261
 
1047
1262
  /**
@@ -1084,6 +1299,18 @@ export class Editor extends EventEmitter<TLEventMap> {
1084
1299
  return hasOwnProperty(this.shapeUtils, type)
1085
1300
  }
1086
1301
 
1302
+ /**
1303
+ * Get the shape util that handles the given asset type.
1304
+ * Returns the shape util whose {@link ShapeUtil.handledAssetTypes} includes
1305
+ * the given asset type, or undefined if none matches.
1306
+ *
1307
+ * @param assetType - The asset type string.
1308
+ * @public
1309
+ */
1310
+ getShapeUtilForAssetType(assetType: string): ShapeUtil | undefined {
1311
+ return getOwnProperty(this._shapeUtilsByAssetType, assetType)
1312
+ }
1313
+
1087
1314
  /* ------------------- Binding Utils ------------------ */
1088
1315
  /**
1089
1316
  * A map of shape utility classes (TLShapeUtils) by shape type.
@@ -1119,6 +1346,56 @@ export class Editor extends EventEmitter<TLEventMap> {
1119
1346
  return bindingUtil
1120
1347
  }
1121
1348
 
1349
+ /* ------------------- Asset Utils ------------------ */
1350
+
1351
+ /**
1352
+ * A map of asset utility classes by asset type.
1353
+ *
1354
+ * @public
1355
+ */
1356
+ assetUtils: { readonly [K in string]?: AssetUtil<TLAsset> } = {}
1357
+
1358
+ /**
1359
+ * Get an asset util from an asset or asset type.
1360
+ *
1361
+ * @param arg - An asset, asset type string, or object with type.
1362
+ *
1363
+ * @public
1364
+ */
1365
+ getAssetUtil<S extends TLAsset>(asset: S | { type: S['type'] }): AssetUtil<S>
1366
+ getAssetUtil(type: string): AssetUtil
1367
+ getAssetUtil(arg: string | { type: string }) {
1368
+ const type = typeof arg === 'string' ? arg : arg.type
1369
+ const assetUtil = getOwnProperty(this.assetUtils, type)
1370
+ assert(assetUtil, `No asset util found for type "${type}"`)
1371
+ return assetUtil
1372
+ }
1373
+
1374
+ /**
1375
+ * Returns true if the editor has an asset util for the given asset type.
1376
+ *
1377
+ * @public
1378
+ */
1379
+ hasAssetUtil(arg: string | { type: string }): boolean {
1380
+ const type = typeof arg === 'string' ? arg : arg.type
1381
+ return hasOwnProperty(this.assetUtils, type)
1382
+ }
1383
+
1384
+ /**
1385
+ * Get the asset util that accepts the given MIME type.
1386
+ * Returns null if no registered asset util accepts the MIME type.
1387
+ *
1388
+ * @public
1389
+ */
1390
+ getAssetUtilForMimeType(mimeType: string): AssetUtil | null {
1391
+ for (const util of Object.values(this.assetUtils)) {
1392
+ if (util && util.acceptsMimeType(mimeType)) {
1393
+ return util
1394
+ }
1395
+ }
1396
+ return null
1397
+ }
1398
+
1122
1399
  /* --------------------- History -------------------- */
1123
1400
 
1124
1401
  /**
@@ -3972,6 +4249,94 @@ export class Editor extends EventEmitter<TLEventMap> {
3972
4249
  return this.getCollaborators().filter((c) => c.currentPageId === currentPageId)
3973
4250
  }
3974
4251
 
4252
+ // Attribution
4253
+
4254
+ /**
4255
+ * Get the current user's ID for attribution purposes.
4256
+ * Also ensures a `user:` record exists in the store for the current user.
4257
+ * Returns `null` when the user store has no current user.
4258
+ *
4259
+ * @public
4260
+ */
4261
+ getAttributionUserId(): string | null {
4262
+ const user = this.store.props.users.currentUser.get()
4263
+ if (!user) return null
4264
+ this._ensureUserRecord(user)
4265
+ return UserRecordType.parseId(user.id)
4266
+ }
4267
+
4268
+ /**
4269
+ * Ensure a user record exists in the store for the given user,
4270
+ * updating it if the data has changed.
4271
+ *
4272
+ * @internal
4273
+ */
4274
+ _ensureUserRecord(user: TLUser): void {
4275
+ const existing = this.store.get(user.id)
4276
+ if (
4277
+ existing &&
4278
+ existing.name === user.name &&
4279
+ existing.color === user.color &&
4280
+ existing.imageUrl === user.imageUrl &&
4281
+ existing.meta === user.meta
4282
+ ) {
4283
+ return
4284
+ }
4285
+ this.run(
4286
+ () => {
4287
+ this.store.put([user])
4288
+ },
4289
+ { history: 'ignore' }
4290
+ )
4291
+ }
4292
+
4293
+ /**
4294
+ * Resolve a display name for a user ID. Asks the
4295
+ * {@link @tldraw/tlschema#TLUserStore} first (the app's source of truth),
4296
+ * falling back to the `user:` record in the store.
4297
+ *
4298
+ * @public
4299
+ */
4300
+ getAttributionDisplayName(userId: string | null): string | null {
4301
+ if (!userId) return null
4302
+ return (
4303
+ this.store.props.users.resolve(userId).get()?.name ??
4304
+ this.store.get(createUserId(userId))?.name ??
4305
+ null
4306
+ )
4307
+ }
4308
+
4309
+ /**
4310
+ * Resolve a user record by ID. Asks the
4311
+ * {@link @tldraw/tlschema#TLUserStore} first (the app's source of truth),
4312
+ * falling back to the `user:` record in the store.
4313
+ *
4314
+ * @public
4315
+ */
4316
+ getAttributionUser(userId: string | null): TLUser | null {
4317
+ if (!userId) return null
4318
+ return (
4319
+ this.store.props.users.resolve(userId).get() ?? this.store.get(createUserId(userId)) ?? null
4320
+ )
4321
+ }
4322
+
4323
+ /**
4324
+ * Collect user IDs referenced by a set of shapes via shape-specific props
4325
+ * (e.g. `textFirstEditedBy` on notes).
4326
+ *
4327
+ * @internal
4328
+ */
4329
+ _getReferencedUserIds(shapes: TLShape[]): Set<string> {
4330
+ const userIds = new Set<string>()
4331
+ for (const shape of shapes) {
4332
+ const util = this.getShapeUtil(shape)
4333
+ for (const id of util.getReferencedUserIds(shape)) {
4334
+ userIds.add(id)
4335
+ }
4336
+ }
4337
+ return userIds
4338
+ }
4339
+
3975
4340
  // Following
3976
4341
 
3977
4342
  // When we are 'locked on' to a user, our camera is derived from their camera.
@@ -9157,12 +9522,23 @@ export class Editor extends EventEmitter<TLEventMap> {
9157
9522
  assets.push(asset)
9158
9523
  }
9159
9524
 
9525
+ const users: TLUser[] = []
9526
+ const seenUserIds = new Set<TLUserId>()
9527
+ for (const userId of this._getReferencedUserIds(shapes)) {
9528
+ const recordId = createUserId(userId)
9529
+ if (seenUserIds.has(recordId)) continue
9530
+ seenUserIds.add(recordId)
9531
+ const user = this.store.get(recordId)
9532
+ if (user) users.push(user)
9533
+ }
9534
+
9160
9535
  return {
9161
9536
  schema: this.store.schema.serialize(),
9162
9537
  shapes,
9163
9538
  rootShapeIds,
9164
9539
  bindings,
9165
9540
  assets,
9541
+ users,
9166
9542
  }
9167
9543
  })
9168
9544
  }
@@ -9238,6 +9614,7 @@ export class Editor extends EventEmitter<TLEventMap> {
9238
9614
  const assets: TLAsset[] = []
9239
9615
  const shapes: TLShape[] = []
9240
9616
  const bindings: TLBinding[] = []
9617
+ const users: TLUser[] = []
9241
9618
 
9242
9619
  // Let's treat the content as a store, and then migrate that store.
9243
9620
  const store: StoreSnapshot<TLRecord> = {
@@ -9247,6 +9624,7 @@ export class Editor extends EventEmitter<TLEventMap> {
9247
9624
  ...Object.fromEntries(
9248
9625
  content.bindings?.map((bindings) => [bindings.id, bindings] as const) ?? []
9249
9626
  ),
9627
+ ...Object.fromEntries(content.users?.map((user) => [user.id, user] as const) ?? []),
9250
9628
  },
9251
9629
  schema: content.schema,
9252
9630
  }
@@ -9268,6 +9646,23 @@ export class Editor extends EventEmitter<TLEventMap> {
9268
9646
  bindings.push(record)
9269
9647
  break
9270
9648
  }
9649
+ case 'user': {
9650
+ users.push(record)
9651
+ break
9652
+ }
9653
+ }
9654
+ }
9655
+
9656
+ if (users.length > 0) {
9657
+ const existingUserIds = new Set(
9658
+ this.store
9659
+ .allRecords()
9660
+ .filter((r): r is TLUser => r.typeName === 'user')
9661
+ .map((r) => r.id)
9662
+ )
9663
+ const usersToCreate = users.filter((u) => !existingUserIds.has(u.id))
9664
+ if (usersToCreate.length > 0) {
9665
+ this.store.put(usersToCreate)
9271
9666
  }
9272
9667
  }
9273
9668