@tldraw/editor 4.6.0-next.d15997ff5a4b → 4.6.0-next.e0f85e824397

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
@@ -44,7 +44,8 @@ var import_store = require("@tldraw/store");
44
44
  var import_tlschema = require("@tldraw/tlschema");
45
45
  var import_utils = require("@tldraw/utils");
46
46
  var import_eventemitter3 = __toESM(require("eventemitter3"), 1);
47
- var import_createTLUser = require("../config/createTLUser");
47
+ var import_createTLCurrentUser = require("../config/createTLCurrentUser");
48
+ var import_defaultAssets = require("../config/defaultAssets");
48
49
  var import_defaultBindings = require("../config/defaultBindings");
49
50
  var import_defaultShapes = require("../config/defaultShapes");
50
51
  var import_TLEditorSnapshot = require("../config/TLEditorSnapshot");
@@ -85,6 +86,7 @@ var import_ScribbleManager = require("./managers/ScribbleManager/ScribbleManager
85
86
  var import_SnapManager = require("./managers/SnapManager/SnapManager");
86
87
  var import_SpatialIndexManager = require("./managers/SpatialIndexManager/SpatialIndexManager");
87
88
  var import_TextManager = require("./managers/TextManager/TextManager");
89
+ var import_ThemeManager = require("./managers/ThemeManager/ThemeManager");
88
90
  var import_TickManager = require("./managers/TickManager/TickManager");
89
91
  var import_UserPreferencesManager = require("./managers/UserPreferencesManager/UserPreferencesManager");
90
92
  var import_RootState = require("./tools/RootState");
@@ -95,6 +97,7 @@ class Editor extends import_eventemitter3.default {
95
97
  user,
96
98
  shapeUtils,
97
99
  bindingUtils,
100
+ assetUtils: assetUtilConstructors,
98
101
  tools,
99
102
  getContainer,
100
103
  // needs to be here for backwards compatibility with TldrawEditor
@@ -102,13 +105,15 @@ class Editor extends import_eventemitter3.default {
102
105
  cameraOptions,
103
106
  initialState,
104
107
  autoFocus,
105
- inferDarkMode,
106
108
  options: _options,
107
109
  // needs to be here for backwards compatibility with TldrawEditor
108
110
  // eslint-disable-next-line @typescript-eslint/no-deprecated
109
111
  textOptions: _textOptions,
110
112
  getShapeVisibility,
111
- fontAssetUrls
113
+ colorScheme,
114
+ fontAssetUrls,
115
+ themes,
116
+ initialTheme
112
117
  }) {
113
118
  super();
114
119
  this._getShapeVisibility = getShapeVisibility;
@@ -131,14 +136,24 @@ class Editor extends import_eventemitter3.default {
131
136
  ...cameraOptions,
132
137
  ...options?.camera
133
138
  });
139
+ this.getContainer = getContainer;
134
140
  this._textOptions = (0, import_state.atom)("text options", options?.text ?? null);
135
- this.user = new import_UserPreferencesManager.UserPreferencesManager(user ?? (0, import_createTLUser.createTLUser)(), inferDarkMode ?? false);
141
+ this.user = new import_UserPreferencesManager.UserPreferencesManager(user ?? (0, import_createTLCurrentUser.createTLCurrentUser)(), colorScheme ?? "light");
136
142
  this.disposables.add(() => this.user.dispose());
137
- this.getContainer = getContainer;
138
143
  this.textMeasure = new import_TextManager.TextManager(this);
139
144
  this.disposables.add(() => this.textMeasure.dispose());
140
- this.fonts = new import_FontManager.FontManager(this, fontAssetUrls);
145
+ this._themeManager = new import_ThemeManager.ThemeManager(this, {
146
+ themes: (0, import_ThemeManager.resolveThemes)(themes),
147
+ initial: initialTheme ?? "default"
148
+ });
149
+ this.disposables.add(() => this._themeManager.dispose());
141
150
  this._tickManager = new import_TickManager.TickManager(this);
151
+ this.disposables.add(() => this._tickManager.dispose());
152
+ this.disposables.add(() => {
153
+ this.off("tick", this._decayCameraStateTimeout);
154
+ this._setCameraState("idle");
155
+ });
156
+ this.fonts = new import_FontManager.FontManager(this, fontAssetUrls);
142
157
  this.inputs = new import_InputsManager.InputsManager(this);
143
158
  class NewRoot extends import_RootState.RootState {
144
159
  static initial = initialState ?? "";
@@ -167,6 +182,16 @@ class Editor extends import_eventemitter3.default {
167
182
  }
168
183
  this.shapeUtils = _shapeUtils;
169
184
  this.styleProps = _styleProps;
185
+ const _shapeUtilsByAssetType = {};
186
+ for (const Util of allShapeUtils) {
187
+ const assetTypes = Util.handledAssetTypes;
188
+ if (assetTypes) {
189
+ for (const assetType of assetTypes) {
190
+ _shapeUtilsByAssetType[assetType] = _shapeUtils[Util.type];
191
+ }
192
+ }
193
+ }
194
+ this._shapeUtilsByAssetType = _shapeUtilsByAssetType;
170
195
  const allBindingUtils = (0, import_defaultBindings.checkBindings)(bindingUtils);
171
196
  const _bindingUtils = {};
172
197
  for (const Util of allBindingUtils) {
@@ -174,6 +199,15 @@ class Editor extends import_eventemitter3.default {
174
199
  _bindingUtils[Util.type] = util;
175
200
  }
176
201
  this.bindingUtils = _bindingUtils;
202
+ if (assetUtilConstructors) {
203
+ const allAssetUtils = (0, import_defaultAssets.checkAssets)(assetUtilConstructors);
204
+ const _assetUtils = {};
205
+ for (const Util of allAssetUtils) {
206
+ const util = new Util(this);
207
+ _assetUtils[Util.type] = util;
208
+ }
209
+ this.assetUtils = _assetUtils;
210
+ }
177
211
  for (const Tool of [...tools]) {
178
212
  if ((0, import_utils.hasOwnProperty)(this.root.children, Tool.id)) {
179
213
  throw Error(`Can't override tool with id "${Tool.id}"`);
@@ -515,6 +549,14 @@ class Editor extends import_eventemitter3.default {
515
549
  })
516
550
  );
517
551
  }
552
+ this.disposables.add(
553
+ (0, import_state.react)("sync current user record", () => {
554
+ const user2 = this.store.props.users.currentUser.get();
555
+ if (user2) {
556
+ this._ensureUserRecord(user2);
557
+ }
558
+ })
559
+ );
518
560
  }
519
561
  _getShapeVisibility;
520
562
  getIsShapeHiddenCache() {
@@ -606,6 +648,11 @@ class Editor extends import_eventemitter3.default {
606
648
  * @public
607
649
  */
608
650
  snaps;
651
+ /**
652
+ * A manager for the spatial index, tracking where shapes exist on the canvas.
653
+ *
654
+ * @internal
655
+ */
609
656
  _spatialIndex;
610
657
  /**
611
658
  * A manager for the any asynchronous events and making sure they're
@@ -620,6 +667,12 @@ class Editor extends import_eventemitter3.default {
620
667
  * @public
621
668
  */
622
669
  user;
670
+ /**
671
+ * A manager for the editor's themes.
672
+ *
673
+ * @internal
674
+ */
675
+ _themeManager;
623
676
  /**
624
677
  * A helper for measuring text.
625
678
  *
@@ -691,12 +744,117 @@ class Editor extends import_eventemitter3.default {
691
744
  * @public
692
745
  */
693
746
  dispose() {
747
+ this.stopCameraAnimation();
748
+ if (this.getInstanceState().followingUserId) {
749
+ this.stopFollowingUser();
750
+ }
694
751
  this.disposables.forEach((dispose) => dispose());
695
752
  this.disposables.clear();
753
+ this.menus.clearOpenMenus();
696
754
  this.store.dispose();
697
755
  this.isDisposed = true;
698
756
  this.emit("dispose");
699
757
  }
758
+ /* ------------------ Themes (shadowing the theme manager) ------------------ */
759
+ /**
760
+ * Get the current color mode (`'light'` or `'dark'`), based on the user's dark mode preference.
761
+ *
762
+ * @public
763
+ */
764
+ getColorMode() {
765
+ return this._themeManager.getColorMode();
766
+ }
767
+ /**
768
+ * Set the color mode. Note that this is a convenience method that passes the mode to
769
+ * `user.updateUserPreferences`, which is the source of truth for the user's color mode preference.
770
+ *
771
+ * @public
772
+ */
773
+ setColorMode(mode) {
774
+ this.user.updateUserPreferences({ colorScheme: mode });
775
+ return this;
776
+ }
777
+ /**
778
+ * Get the id of the current theme.
779
+ *
780
+ * @public
781
+ */
782
+ getCurrentThemeId() {
783
+ return this._themeManager.getCurrentThemeId();
784
+ }
785
+ /**
786
+ * Get the current theme definition.
787
+ *
788
+ * @public
789
+ */
790
+ getCurrentTheme() {
791
+ return this._themeManager.getCurrentTheme();
792
+ }
793
+ /**
794
+ * Set the current theme by id.
795
+ *
796
+ * @public
797
+ */
798
+ setCurrentTheme(id) {
799
+ this._themeManager.setCurrentTheme(id);
800
+ return this;
801
+ }
802
+ /**
803
+ * Get all registered theme definitions.
804
+ *
805
+ * @public
806
+ */
807
+ getThemes() {
808
+ return this._themeManager.getThemes();
809
+ }
810
+ /**
811
+ * Get a single theme definition by id.
812
+ *
813
+ * @public
814
+ */
815
+ getTheme(id) {
816
+ return this._themeManager.getTheme(id);
817
+ }
818
+ /**
819
+ * Replace all theme definitions, or update them via a callback that receives a deep copy.
820
+ * The `'default'` theme must always be present in the result.
821
+ *
822
+ * @example
823
+ * ```ts
824
+ * // Replace all themes
825
+ * editor.updateThemes({ default: myDefaultTheme, ocean: myOceanTheme })
826
+ *
827
+ * // Update via callback
828
+ * editor.updateThemes((themes) => {
829
+ * delete themes.ocean
830
+ * return themes
831
+ * })
832
+ * ```
833
+ *
834
+ * @public
835
+ */
836
+ updateThemes(themes) {
837
+ this._themeManager.updateThemes(themes);
838
+ return this;
839
+ }
840
+ /**
841
+ * Register or update a single theme definition. The theme is keyed by its `id` property.
842
+ *
843
+ * @example
844
+ * ```ts
845
+ * // Override a property on the default theme
846
+ * editor.updateTheme({ ...editor.getTheme('default')!, fontSize: 24 })
847
+ *
848
+ * // Register a new theme
849
+ * editor.updateTheme({ id: 'ocean', ...myOceanTheme })
850
+ * ```
851
+ *
852
+ * @public
853
+ */
854
+ updateTheme(theme) {
855
+ this._themeManager.updateTheme(theme);
856
+ return this;
857
+ }
700
858
  /* ------------------- Shape Utils ------------------ */
701
859
  /**
702
860
  * A map of shape utility classes (TLShapeUtils) by shape type.
@@ -704,6 +862,8 @@ class Editor extends import_eventemitter3.default {
704
862
  * @public
705
863
  */
706
864
  shapeUtils;
865
+ /** @internal */
866
+ _shapeUtilsByAssetType = {};
707
867
  styleProps;
708
868
  getShapeUtil(arg) {
709
869
  const type = typeof arg === "string" ? arg : arg.type;
@@ -715,6 +875,17 @@ class Editor extends import_eventemitter3.default {
715
875
  const type = typeof arg === "string" ? arg : arg.type;
716
876
  return (0, import_utils.hasOwnProperty)(this.shapeUtils, type);
717
877
  }
878
+ /**
879
+ * Get the shape util that handles the given asset type.
880
+ * Returns the shape util whose {@link ShapeUtil.handledAssetTypes} includes
881
+ * the given asset type, or undefined if none matches.
882
+ *
883
+ * @param assetType - The asset type string.
884
+ * @public
885
+ */
886
+ getShapeUtilForAssetType(assetType) {
887
+ return (0, import_utils.getOwnProperty)(this._shapeUtilsByAssetType, assetType);
888
+ }
718
889
  /* ------------------- Binding Utils ------------------ */
719
890
  /**
720
891
  * A map of shape utility classes (TLShapeUtils) by shape type.
@@ -728,6 +899,42 @@ class Editor extends import_eventemitter3.default {
728
899
  (0, import_utils.assert)(bindingUtil, `No binding util found for type "${type}"`);
729
900
  return bindingUtil;
730
901
  }
902
+ /* ------------------- Asset Utils ------------------ */
903
+ /**
904
+ * A map of asset utility classes by asset type.
905
+ *
906
+ * @public
907
+ */
908
+ assetUtils = {};
909
+ getAssetUtil(arg) {
910
+ const type = typeof arg === "string" ? arg : arg.type;
911
+ const assetUtil = (0, import_utils.getOwnProperty)(this.assetUtils, type);
912
+ (0, import_utils.assert)(assetUtil, `No asset util found for type "${type}"`);
913
+ return assetUtil;
914
+ }
915
+ /**
916
+ * Returns true if the editor has an asset util for the given asset type.
917
+ *
918
+ * @public
919
+ */
920
+ hasAssetUtil(arg) {
921
+ const type = typeof arg === "string" ? arg : arg.type;
922
+ return (0, import_utils.hasOwnProperty)(this.assetUtils, type);
923
+ }
924
+ /**
925
+ * Get the asset util that accepts the given MIME type.
926
+ * Returns null if no registered asset util accepts the MIME type.
927
+ *
928
+ * @public
929
+ */
930
+ getAssetUtilForMimeType(mimeType) {
931
+ for (const util of Object.values(this.assetUtils)) {
932
+ if (util && util.acceptsMimeType(mimeType)) {
933
+ return util;
934
+ }
935
+ }
936
+ return null;
937
+ }
731
938
  /* --------------------- History -------------------- */
732
939
  /**
733
940
  * A manager for the editor's history.
@@ -2821,6 +3028,76 @@ class Editor extends import_eventemitter3.default {
2821
3028
  const currentPageId = this.getCurrentPageId();
2822
3029
  return this.getCollaborators().filter((c) => c.currentPageId === currentPageId);
2823
3030
  }
3031
+ // Attribution
3032
+ /**
3033
+ * Get the current user's ID for attribution purposes.
3034
+ * Also ensures a `user:` record exists in the store for the current user.
3035
+ * Returns `null` when the user store has no current user.
3036
+ *
3037
+ * @public
3038
+ */
3039
+ getAttributionUserId() {
3040
+ const user = this.store.props.users.currentUser.get();
3041
+ if (!user) return null;
3042
+ this._ensureUserRecord(user);
3043
+ return import_tlschema.UserRecordType.parseId(user.id);
3044
+ }
3045
+ /**
3046
+ * Ensure a user record exists in the store for the given user,
3047
+ * updating it if the data has changed.
3048
+ *
3049
+ * @internal
3050
+ */
3051
+ _ensureUserRecord(user) {
3052
+ const existing = this.store.get(user.id);
3053
+ if (existing && existing.name === user.name && existing.color === user.color && existing.imageUrl === user.imageUrl && existing.meta === user.meta) {
3054
+ return;
3055
+ }
3056
+ this.run(
3057
+ () => {
3058
+ this.store.put([user]);
3059
+ },
3060
+ { history: "ignore" }
3061
+ );
3062
+ }
3063
+ /**
3064
+ * Resolve a display name for a user ID. Asks the
3065
+ * {@link @tldraw/tlschema#TLUserStore} first (the app's source of truth),
3066
+ * falling back to the `user:` record in the store.
3067
+ *
3068
+ * @public
3069
+ */
3070
+ getAttributionDisplayName(userId) {
3071
+ if (!userId) return null;
3072
+ return this.store.props.users.resolve(userId).get()?.name ?? this.store.get((0, import_tlschema.createUserId)(userId))?.name ?? null;
3073
+ }
3074
+ /**
3075
+ * Resolve a user record by ID. Asks the
3076
+ * {@link @tldraw/tlschema#TLUserStore} first (the app's source of truth),
3077
+ * falling back to the `user:` record in the store.
3078
+ *
3079
+ * @public
3080
+ */
3081
+ getAttributionUser(userId) {
3082
+ if (!userId) return null;
3083
+ return this.store.props.users.resolve(userId).get() ?? this.store.get((0, import_tlschema.createUserId)(userId)) ?? null;
3084
+ }
3085
+ /**
3086
+ * Collect user IDs referenced by a set of shapes via shape-specific props
3087
+ * (e.g. `textFirstEditedBy` on notes).
3088
+ *
3089
+ * @internal
3090
+ */
3091
+ _getReferencedUserIds(shapes) {
3092
+ const userIds = /* @__PURE__ */ new Set();
3093
+ for (const shape of shapes) {
3094
+ const util = this.getShapeUtil(shape);
3095
+ for (const id of util.getReferencedUserIds(shape)) {
3096
+ userIds.add(id);
3097
+ }
3098
+ }
3099
+ return userIds;
3100
+ }
2824
3101
  // Following
2825
3102
  // When we are 'locked on' to a user, our camera is derived from their camera.
2826
3103
  _isLockedOnFollowingUser = (0, import_state.atom)("isLockedOnFollowingUser", false);
@@ -6534,12 +6811,22 @@ class Editor extends import_eventemitter3.default {
6534
6811
  if (!asset) continue;
6535
6812
  assets.push(asset);
6536
6813
  }
6814
+ const users = [];
6815
+ const seenUserIds = /* @__PURE__ */ new Set();
6816
+ for (const userId of this._getReferencedUserIds(shapes2)) {
6817
+ const recordId = (0, import_tlschema.createUserId)(userId);
6818
+ if (seenUserIds.has(recordId)) continue;
6819
+ seenUserIds.add(recordId);
6820
+ const user = this.store.get(recordId);
6821
+ if (user) users.push(user);
6822
+ }
6537
6823
  return {
6538
6824
  schema: this.store.schema.serialize(),
6539
6825
  shapes: shapes2,
6540
6826
  rootShapeIds,
6541
6827
  bindings,
6542
- assets
6828
+ assets,
6829
+ users
6543
6830
  };
6544
6831
  });
6545
6832
  }
@@ -6589,13 +6876,15 @@ class Editor extends import_eventemitter3.default {
6589
6876
  const assets = [];
6590
6877
  const shapes = [];
6591
6878
  const bindings = [];
6879
+ const users = [];
6592
6880
  const store = {
6593
6881
  store: {
6594
6882
  ...Object.fromEntries(content.assets.map((asset) => [asset.id, asset])),
6595
6883
  ...Object.fromEntries(content.shapes.map((shape) => [shape.id, shape])),
6596
6884
  ...Object.fromEntries(
6597
6885
  content.bindings?.map((bindings2) => [bindings2.id, bindings2]) ?? []
6598
- )
6886
+ ),
6887
+ ...Object.fromEntries(content.users?.map((user) => [user.id, user]) ?? [])
6599
6888
  },
6600
6889
  schema: content.schema
6601
6890
  };
@@ -6617,6 +6906,19 @@ class Editor extends import_eventemitter3.default {
6617
6906
  bindings.push(record);
6618
6907
  break;
6619
6908
  }
6909
+ case "user": {
6910
+ users.push(record);
6911
+ break;
6912
+ }
6913
+ }
6914
+ }
6915
+ if (users.length > 0) {
6916
+ const existingUserIds = new Set(
6917
+ this.store.allRecords().filter((r) => r.typeName === "user").map((r) => r.id)
6918
+ );
6919
+ const usersToCreate = users.filter((u) => !existingUserIds.has(u.id));
6920
+ if (usersToCreate.length > 0) {
6921
+ this.store.put(usersToCreate);
6620
6922
  }
6621
6923
  }
6622
6924
  const shapeIdMap = new Map(