@tldraw/editor 4.6.0-next.793daa9fe264 → 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.
- package/dist-cjs/index.d.ts +376 -98
- package/dist-cjs/index.js +12 -3
- package/dist-cjs/index.js.map +3 -3
- package/dist-cjs/lib/TldrawEditor.js +55 -12
- package/dist-cjs/lib/TldrawEditor.js.map +3 -3
- package/dist-cjs/lib/components/default-components/CanvasShapeIndicators.js +3 -3
- package/dist-cjs/lib/components/default-components/CanvasShapeIndicators.js.map +2 -2
- package/dist-cjs/lib/config/createTLStore.js +7 -0
- package/dist-cjs/lib/config/createTLStore.js.map +2 -2
- package/dist-cjs/lib/config/defaultAssets.js +36 -0
- package/dist-cjs/lib/config/defaultAssets.js.map +7 -0
- package/dist-cjs/lib/editor/Editor.js +200 -5
- package/dist-cjs/lib/editor/Editor.js.map +2 -2
- package/dist-cjs/lib/editor/assets/AssetUtil.js +66 -0
- package/dist-cjs/lib/editor/assets/AssetUtil.js.map +7 -0
- package/dist-cjs/lib/editor/managers/FontManager/FontManager.js.map +2 -2
- package/dist-cjs/lib/editor/managers/ThemeManager/ThemeManager.js +106 -0
- package/dist-cjs/lib/editor/managers/ThemeManager/ThemeManager.js.map +7 -0
- package/dist-cjs/lib/editor/managers/ThemeManager/defaultThemes.js +586 -0
- package/dist-cjs/lib/editor/managers/ThemeManager/defaultThemes.js.map +7 -0
- package/dist-cjs/lib/editor/managers/UserPreferencesManager/UserPreferencesManager.js +6 -4
- package/dist-cjs/lib/editor/managers/UserPreferencesManager/UserPreferencesManager.js.map +2 -2
- package/dist-cjs/lib/editor/shapes/ShapeUtil.js +9 -0
- package/dist-cjs/lib/editor/shapes/ShapeUtil.js.map +2 -2
- package/dist-cjs/lib/editor/types/SvgExportContext.js.map +2 -2
- package/dist-cjs/lib/editor/types/external-content.js.map +1 -1
- package/dist-cjs/lib/exports/getSvgJsx.js +12 -7
- package/dist-cjs/lib/exports/getSvgJsx.js.map +2 -2
- package/dist-cjs/lib/globals/environment.js +18 -1
- package/dist-cjs/lib/globals/environment.js.map +2 -2
- package/dist-cjs/lib/hooks/{useIsDarkMode.js → useColorMode.js} +14 -10
- package/dist-cjs/lib/hooks/useColorMode.js.map +7 -0
- package/dist-cjs/lib/hooks/useCursor.js +3 -7
- package/dist-cjs/lib/hooks/useCursor.js.map +2 -2
- package/dist-cjs/lib/hooks/useDarkMode.js +4 -4
- package/dist-cjs/lib/hooks/useDarkMode.js.map +2 -2
- package/dist-cjs/lib/utils/reparenting.js +2 -1
- package/dist-cjs/lib/utils/reparenting.js.map +2 -2
- package/dist-cjs/lib/utils/richText.js.map +2 -2
- package/dist-cjs/version.js +3 -3
- package/dist-cjs/version.js.map +1 -1
- package/dist-esm/index.d.mts +376 -98
- package/dist-esm/index.mjs +13 -6
- package/dist-esm/index.mjs.map +2 -2
- package/dist-esm/lib/TldrawEditor.mjs +58 -12
- package/dist-esm/lib/TldrawEditor.mjs.map +3 -3
- package/dist-esm/lib/components/default-components/CanvasShapeIndicators.mjs +3 -3
- package/dist-esm/lib/components/default-components/CanvasShapeIndicators.mjs.map +2 -2
- package/dist-esm/lib/config/createTLStore.mjs +10 -1
- package/dist-esm/lib/config/createTLStore.mjs.map +2 -2
- package/dist-esm/lib/config/defaultAssets.mjs +16 -0
- package/dist-esm/lib/config/defaultAssets.mjs.map +7 -0
- package/dist-esm/lib/editor/Editor.mjs +200 -5
- package/dist-esm/lib/editor/Editor.mjs.map +2 -2
- package/dist-esm/lib/editor/assets/AssetUtil.mjs +46 -0
- package/dist-esm/lib/editor/assets/AssetUtil.mjs.map +7 -0
- package/dist-esm/lib/editor/managers/FontManager/FontManager.mjs.map +2 -2
- package/dist-esm/lib/editor/managers/ThemeManager/ThemeManager.mjs +88 -0
- package/dist-esm/lib/editor/managers/ThemeManager/ThemeManager.mjs.map +7 -0
- package/dist-esm/lib/editor/managers/ThemeManager/defaultThemes.mjs +568 -0
- package/dist-esm/lib/editor/managers/ThemeManager/defaultThemes.mjs.map +7 -0
- package/dist-esm/lib/editor/managers/UserPreferencesManager/UserPreferencesManager.mjs +6 -4
- package/dist-esm/lib/editor/managers/UserPreferencesManager/UserPreferencesManager.mjs.map +2 -2
- package/dist-esm/lib/editor/shapes/ShapeUtil.mjs +9 -0
- package/dist-esm/lib/editor/shapes/ShapeUtil.mjs.map +2 -2
- package/dist-esm/lib/editor/types/SvgExportContext.mjs.map +2 -2
- package/dist-esm/lib/exports/getSvgJsx.mjs +12 -10
- package/dist-esm/lib/exports/getSvgJsx.mjs.map +2 -2
- package/dist-esm/lib/globals/environment.mjs +18 -1
- package/dist-esm/lib/globals/environment.mjs.map +2 -2
- package/dist-esm/lib/hooks/useColorMode.mjs +19 -0
- package/dist-esm/lib/hooks/useColorMode.mjs.map +7 -0
- package/dist-esm/lib/hooks/useCursor.mjs +3 -7
- package/dist-esm/lib/hooks/useCursor.mjs.map +2 -2
- package/dist-esm/lib/hooks/useDarkMode.mjs +4 -4
- package/dist-esm/lib/hooks/useDarkMode.mjs.map +2 -2
- package/dist-esm/lib/utils/reparenting.mjs +2 -1
- package/dist-esm/lib/utils/reparenting.mjs.map +2 -2
- package/dist-esm/lib/utils/richText.mjs.map +2 -2
- package/dist-esm/version.mjs +3 -3
- package/dist-esm/version.mjs.map +1 -1
- package/editor.css +0 -33
- package/package.json +7 -7
- package/src/index.ts +8 -6
- package/src/lib/TldrawEditor.tsx +90 -13
- package/src/lib/components/default-components/CanvasShapeIndicators.tsx +3 -3
- package/src/lib/config/createTLStore.ts +22 -1
- package/src/lib/config/defaultAssets.ts +19 -0
- package/src/lib/editor/Editor.ts +285 -27
- package/src/lib/editor/assets/AssetUtil.ts +85 -0
- package/src/lib/editor/managers/FontManager/FontManager.test.ts +9 -2
- package/src/lib/editor/managers/FontManager/FontManager.ts +1 -67
- package/src/lib/editor/managers/ThemeManager/ThemeManager.ts +116 -0
- package/src/lib/editor/managers/ThemeManager/defaultThemes.ts +605 -0
- package/src/lib/editor/managers/UserPreferencesManager/UserPreferencesManager.test.ts +23 -29
- package/src/lib/editor/managers/UserPreferencesManager/UserPreferencesManager.ts +5 -3
- package/src/lib/editor/shapes/ShapeUtil.ts +26 -1
- package/src/lib/editor/types/SvgExportContext.tsx +5 -0
- package/src/lib/editor/types/external-content.ts +1 -0
- package/src/lib/exports/getSvgJsx.tsx +21 -15
- package/src/lib/globals/environment.ts +18 -0
- package/src/lib/hooks/{useIsDarkMode.ts → useColorMode.ts} +9 -5
- package/src/lib/hooks/useCursor.ts +3 -7
- package/src/lib/hooks/useDarkMode.ts +4 -4
- package/src/lib/utils/reparenting.ts +6 -2
- package/src/lib/utils/richText.ts +1 -1
- package/src/version.ts +3 -3
- package/dist-cjs/lib/hooks/useIsDarkMode.js.map +0 -7
- package/dist-esm/lib/hooks/useIsDarkMode.mjs +0 -15
- package/dist-esm/lib/hooks/useIsDarkMode.mjs.map +0 -7
package/src/lib/TldrawEditor.tsx
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
import { MigrationSequence, Store } from '@tldraw/store'
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
TLShape,
|
|
4
|
+
TLStore,
|
|
5
|
+
TLStoreSnapshot,
|
|
6
|
+
TLTheme,
|
|
7
|
+
TLThemeId,
|
|
8
|
+
TLThemes,
|
|
9
|
+
registerColorsFromThemes,
|
|
10
|
+
registerFontsFromThemes,
|
|
11
|
+
} from '@tldraw/tlschema'
|
|
3
12
|
import { annotateError, Required } from '@tldraw/utils'
|
|
4
13
|
import classNames from 'classnames'
|
|
5
14
|
import React, {
|
|
@@ -18,10 +27,12 @@ import { DefaultErrorFallback } from './components/default-components/DefaultErr
|
|
|
18
27
|
import { OptionalErrorBoundary } from './components/ErrorBoundary'
|
|
19
28
|
import { createTLCurrentUser, TLCurrentUser } from './config/createTLCurrentUser'
|
|
20
29
|
import { TLStoreBaseOptions } from './config/createTLStore'
|
|
30
|
+
import { TLAnyAssetUtilConstructor } from './config/defaultAssets'
|
|
21
31
|
import { TLAnyBindingUtilConstructor } from './config/defaultBindings'
|
|
22
32
|
import { TLAnyShapeUtilConstructor } from './config/defaultShapes'
|
|
23
33
|
import { TLEditorSnapshot } from './config/TLEditorSnapshot'
|
|
24
34
|
import { Editor } from './editor/Editor'
|
|
35
|
+
import { resolveThemes } from './editor/managers/ThemeManager/ThemeManager'
|
|
25
36
|
import { TLStateNodeConstructor } from './editor/tools/StateNode'
|
|
26
37
|
import { TLCameraOptions } from './editor/types/misc-types'
|
|
27
38
|
import { useEditorComponents } from './hooks/EditorComponentsContext'
|
|
@@ -121,6 +132,11 @@ export interface TldrawEditorBaseProps {
|
|
|
121
132
|
*/
|
|
122
133
|
bindingUtils?: readonly TLAnyBindingUtilConstructor[]
|
|
123
134
|
|
|
135
|
+
/**
|
|
136
|
+
* An array of asset utils to use in the editor.
|
|
137
|
+
*/
|
|
138
|
+
assetUtils?: readonly TLAnyAssetUtilConstructor[]
|
|
139
|
+
|
|
124
140
|
/**
|
|
125
141
|
* An array of tools to add to the editor's state chart.
|
|
126
142
|
*/
|
|
@@ -157,9 +173,23 @@ export interface TldrawEditorBaseProps {
|
|
|
157
173
|
user?: TLCurrentUser
|
|
158
174
|
|
|
159
175
|
/**
|
|
160
|
-
*
|
|
176
|
+
* The editor's color scheme. Defaults to `'light'`.
|
|
177
|
+
*
|
|
178
|
+
* - `'light'` - Always use light mode.
|
|
179
|
+
* - `'dark'` - Always use dark mode.
|
|
180
|
+
* - `'system'` - Follow the OS color scheme preference.
|
|
161
181
|
*/
|
|
162
|
-
|
|
182
|
+
colorScheme?: 'light' | 'dark' | 'system'
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Named themes for the editor.
|
|
186
|
+
*/
|
|
187
|
+
themes?: Partial<TLThemes>
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* The id of the initially active theme. Defaults to `'default'`.
|
|
191
|
+
*/
|
|
192
|
+
initialTheme?: TLThemeId
|
|
163
193
|
|
|
164
194
|
/**
|
|
165
195
|
* Camera options for the editor.
|
|
@@ -242,6 +272,7 @@ declare global {
|
|
|
242
272
|
|
|
243
273
|
const EMPTY_SHAPE_UTILS_ARRAY = [] as const
|
|
244
274
|
const EMPTY_BINDING_UTILS_ARRAY = [] as const
|
|
275
|
+
const EMPTY_ASSET_UTILS_ARRAY = [] as const
|
|
245
276
|
const EMPTY_TOOLS_ARRAY = [] as const
|
|
246
277
|
/** @internal */
|
|
247
278
|
export const TL_CONTAINER_CLASS = 'tl-container'
|
|
@@ -259,6 +290,13 @@ export const TldrawEditor = memo(function TldrawEditor({
|
|
|
259
290
|
deepLinks: _deepLinks,
|
|
260
291
|
...rest
|
|
261
292
|
}: TldrawEditorProps) {
|
|
293
|
+
// Register custom colors and fonts before effects run. For external stores,
|
|
294
|
+
// users should also pass themes to createTLStore so they are
|
|
295
|
+
// registered before data is loaded into the store.
|
|
296
|
+
const resolvedThemes = resolveThemes(rest.themes)
|
|
297
|
+
registerColorsFromThemes(resolvedThemes)
|
|
298
|
+
registerFontsFromThemes(resolvedThemes)
|
|
299
|
+
|
|
262
300
|
const [container, setContainer] = useState<HTMLElement | null>(null)
|
|
263
301
|
const user = useMemo(() => _user ?? createTLCurrentUser(), [_user])
|
|
264
302
|
|
|
@@ -285,6 +323,7 @@ export const TldrawEditor = memo(function TldrawEditor({
|
|
|
285
323
|
...rest,
|
|
286
324
|
shapeUtils: rest.shapeUtils ?? EMPTY_SHAPE_UTILS_ARRAY,
|
|
287
325
|
bindingUtils: rest.bindingUtils ?? EMPTY_BINDING_UTILS_ARRAY,
|
|
326
|
+
assetUtils: rest.assetUtils ?? EMPTY_ASSET_UTILS_ARRAY,
|
|
288
327
|
tools: rest.tools ?? EMPTY_TOOLS_ARRAY,
|
|
289
328
|
components,
|
|
290
329
|
options: useShallowObjectIdentity(mergedOptions),
|
|
@@ -332,7 +371,7 @@ export const TldrawEditor = memo(function TldrawEditor({
|
|
|
332
371
|
function TldrawEditorWithOwnStore(
|
|
333
372
|
props: Required<
|
|
334
373
|
TldrawEditorProps & { store: undefined; user: TLCurrentUser },
|
|
335
|
-
'shapeUtils' | 'bindingUtils' | 'tools'
|
|
374
|
+
'shapeUtils' | 'bindingUtils' | 'assetUtils' | 'tools'
|
|
336
375
|
>
|
|
337
376
|
) {
|
|
338
377
|
const {
|
|
@@ -341,17 +380,20 @@ function TldrawEditorWithOwnStore(
|
|
|
341
380
|
initialData,
|
|
342
381
|
shapeUtils,
|
|
343
382
|
bindingUtils,
|
|
383
|
+
assetUtils,
|
|
344
384
|
persistenceKey,
|
|
345
385
|
sessionId,
|
|
346
386
|
user,
|
|
347
387
|
assets,
|
|
348
388
|
users,
|
|
349
389
|
migrations,
|
|
390
|
+
themes,
|
|
350
391
|
} = props
|
|
351
392
|
|
|
352
393
|
const syncedStore = useLocalStore({
|
|
353
394
|
shapeUtils,
|
|
354
395
|
bindingUtils,
|
|
396
|
+
assetUtils,
|
|
355
397
|
initialData,
|
|
356
398
|
persistenceKey,
|
|
357
399
|
sessionId,
|
|
@@ -360,6 +402,7 @@ function TldrawEditorWithOwnStore(
|
|
|
360
402
|
assets,
|
|
361
403
|
users,
|
|
362
404
|
migrations,
|
|
405
|
+
themes,
|
|
363
406
|
})
|
|
364
407
|
|
|
365
408
|
return <TldrawEditorWithLoadingStore {...props} store={syncedStore} user={user} />
|
|
@@ -371,7 +414,7 @@ const TldrawEditorWithLoadingStore = memo(function TldrawEditorBeforeLoading({
|
|
|
371
414
|
...rest
|
|
372
415
|
}: Required<
|
|
373
416
|
TldrawEditorProps & { store: TLStoreWithStatus; user: TLCurrentUser },
|
|
374
|
-
'shapeUtils' | 'bindingUtils' | 'tools'
|
|
417
|
+
'shapeUtils' | 'bindingUtils' | 'assetUtils' | 'tools'
|
|
375
418
|
>) {
|
|
376
419
|
const container = useContainer()
|
|
377
420
|
|
|
@@ -417,22 +460,25 @@ function TldrawEditorWithReadyStore({
|
|
|
417
460
|
tools,
|
|
418
461
|
shapeUtils,
|
|
419
462
|
bindingUtils,
|
|
463
|
+
assetUtils,
|
|
420
464
|
user,
|
|
421
465
|
initialState,
|
|
422
466
|
autoFocus = true,
|
|
423
|
-
inferDarkMode,
|
|
424
467
|
// eslint-disable-next-line @typescript-eslint/no-deprecated
|
|
425
468
|
cameraOptions,
|
|
426
469
|
options,
|
|
427
470
|
licenseKey,
|
|
428
471
|
getShapeVisibility,
|
|
472
|
+
colorScheme,
|
|
429
473
|
assetUrls,
|
|
474
|
+
themes,
|
|
475
|
+
initialTheme,
|
|
430
476
|
}: Required<
|
|
431
477
|
TldrawEditorProps & {
|
|
432
478
|
store: TLStore
|
|
433
479
|
user: TLCurrentUser
|
|
434
480
|
},
|
|
435
|
-
'shapeUtils' | 'bindingUtils' | 'tools'
|
|
481
|
+
'shapeUtils' | 'bindingUtils' | 'assetUtils' | 'tools'
|
|
436
482
|
>) {
|
|
437
483
|
const { ErrorFallback } = useEditorComponents()
|
|
438
484
|
const container = useContainer()
|
|
@@ -448,44 +494,58 @@ function TldrawEditorWithReadyStore({
|
|
|
448
494
|
const editorOptionsRef = useRef({
|
|
449
495
|
// for these, it's because they're only used when the editor first mounts:
|
|
450
496
|
autoFocus: autoFocus && !noAutoFocus(),
|
|
451
|
-
inferDarkMode,
|
|
452
497
|
initialState,
|
|
498
|
+
colorScheme,
|
|
453
499
|
|
|
454
500
|
// for these, it's because we keep them up to date in a separate effect:
|
|
455
501
|
cameraOptions,
|
|
456
502
|
deepLinks,
|
|
503
|
+
themes,
|
|
504
|
+
initialTheme,
|
|
457
505
|
})
|
|
458
506
|
|
|
459
507
|
useLayoutEffect(() => {
|
|
460
508
|
editorOptionsRef.current = {
|
|
461
509
|
autoFocus: autoFocus && !noAutoFocus(),
|
|
462
|
-
inferDarkMode,
|
|
463
510
|
initialState,
|
|
511
|
+
colorScheme,
|
|
464
512
|
cameraOptions,
|
|
465
513
|
deepLinks,
|
|
514
|
+
themes,
|
|
515
|
+
initialTheme,
|
|
466
516
|
}
|
|
467
|
-
}, [autoFocus,
|
|
517
|
+
}, [autoFocus, initialState, colorScheme, cameraOptions, deepLinks, themes, initialTheme])
|
|
468
518
|
|
|
469
519
|
useLayoutEffect(
|
|
470
520
|
() => {
|
|
471
|
-
const {
|
|
472
|
-
|
|
521
|
+
const {
|
|
522
|
+
autoFocus,
|
|
523
|
+
initialState,
|
|
524
|
+
colorScheme: initColorScheme,
|
|
525
|
+
cameraOptions,
|
|
526
|
+
deepLinks,
|
|
527
|
+
themes,
|
|
528
|
+
initialTheme,
|
|
529
|
+
} = editorOptionsRef.current
|
|
473
530
|
const editor = new Editor({
|
|
474
531
|
store,
|
|
475
532
|
shapeUtils,
|
|
476
533
|
bindingUtils,
|
|
534
|
+
assetUtils,
|
|
477
535
|
tools,
|
|
478
536
|
getContainer: () => container,
|
|
479
537
|
user,
|
|
480
538
|
initialState,
|
|
481
539
|
// we should check for some kind of query parameter that turns off autofocus
|
|
482
540
|
autoFocus,
|
|
483
|
-
inferDarkMode,
|
|
484
541
|
cameraOptions,
|
|
485
542
|
options,
|
|
486
543
|
licenseKey,
|
|
487
544
|
getShapeVisibility,
|
|
545
|
+
colorScheme: initColorScheme,
|
|
488
546
|
fontAssetUrls: assetUrls?.fonts,
|
|
547
|
+
themes: themes,
|
|
548
|
+
initialTheme: initialTheme,
|
|
489
549
|
})
|
|
490
550
|
|
|
491
551
|
editor.updateViewportScreenBounds(canvasRef.current ?? container)
|
|
@@ -510,7 +570,9 @@ function TldrawEditorWithReadyStore({
|
|
|
510
570
|
},
|
|
511
571
|
// if any of these change, we need to recreate the editor.
|
|
512
572
|
[
|
|
573
|
+
assetUtils,
|
|
513
574
|
bindingUtils,
|
|
575
|
+
colorScheme,
|
|
514
576
|
container,
|
|
515
577
|
options,
|
|
516
578
|
shapeUtils,
|
|
@@ -539,6 +601,21 @@ function TldrawEditorWithReadyStore({
|
|
|
539
601
|
}
|
|
540
602
|
}, [editor, cameraOptions, options?.camera])
|
|
541
603
|
|
|
604
|
+
// keep the editor up to date with the latest theme definitions
|
|
605
|
+
useLayoutEffect(() => {
|
|
606
|
+
if (editor && themes) {
|
|
607
|
+
for (const def of Object.values(themes) as TLTheme[]) {
|
|
608
|
+
editor.updateTheme(def)
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
}, [editor, themes])
|
|
612
|
+
|
|
613
|
+
useLayoutEffect(() => {
|
|
614
|
+
if (editor && initialTheme) {
|
|
615
|
+
editor.setCurrentTheme(initialTheme)
|
|
616
|
+
}
|
|
617
|
+
}, [editor, initialTheme])
|
|
618
|
+
|
|
542
619
|
const crashingError = useSyncExternalStore(
|
|
543
620
|
useCallback(
|
|
544
621
|
(onStoreChange) => {
|
|
@@ -6,8 +6,8 @@ import { memo, useEffect, useRef } from 'react'
|
|
|
6
6
|
import { Editor } from '../../editor/Editor'
|
|
7
7
|
import { TLIndicatorPath } from '../../editor/shapes/ShapeUtil'
|
|
8
8
|
import { getComputedStyle } from '../../exports/domUtils'
|
|
9
|
+
import { useColorMode } from '../../hooks/useColorMode'
|
|
9
10
|
import { useEditor } from '../../hooks/useEditor'
|
|
10
|
-
import { useIsDarkMode } from '../../hooks/useIsDarkMode'
|
|
11
11
|
import { useActivePeerIds$ } from '../../hooks/usePeerIds'
|
|
12
12
|
|
|
13
13
|
interface CollaboratorIndicatorData {
|
|
@@ -133,14 +133,14 @@ export const CanvasShapeIndicators = memo(function CanvasShapeIndicators() {
|
|
|
133
133
|
|
|
134
134
|
// Cache the selected color to avoid getComputedStyle on every render
|
|
135
135
|
const rSelectedColor = useRef<string | null>(null)
|
|
136
|
-
const
|
|
136
|
+
const colorMode = useColorMode()
|
|
137
137
|
|
|
138
138
|
useEffect(() => {
|
|
139
139
|
const timer = editor.timers.setTimeout(() => {
|
|
140
140
|
rSelectedColor.current = null
|
|
141
141
|
}, 0)
|
|
142
142
|
return () => clearTimeout(timer)
|
|
143
|
-
}, [
|
|
143
|
+
}, [colorMode, editor])
|
|
144
144
|
|
|
145
145
|
// Get active peer IDs (already handles time-based state transitions)
|
|
146
146
|
const activePeerIds$ = useActivePeerIds$()
|
|
@@ -8,15 +8,20 @@ import {
|
|
|
8
8
|
TLStore,
|
|
9
9
|
TLStoreProps,
|
|
10
10
|
TLStoreSnapshot,
|
|
11
|
+
TLThemes,
|
|
11
12
|
TLUser,
|
|
12
13
|
TLUserStore,
|
|
13
14
|
UserRecordType,
|
|
14
15
|
createCachedUserResolve,
|
|
15
16
|
createTLSchema,
|
|
16
17
|
createUserId,
|
|
18
|
+
registerColorsFromThemes,
|
|
19
|
+
registerFontsFromThemes,
|
|
17
20
|
} from '@tldraw/tlschema'
|
|
18
21
|
import { FileHelpers, assert } from '@tldraw/utils'
|
|
19
22
|
import { Editor } from '../editor/Editor'
|
|
23
|
+
import { resolveThemes } from '../editor/managers/ThemeManager/ThemeManager'
|
|
24
|
+
import { TLAnyAssetUtilConstructor, checkAssets } from './defaultAssets'
|
|
20
25
|
import { TLAnyBindingUtilConstructor, checkBindings } from './defaultBindings'
|
|
21
26
|
import { TLAnyShapeUtilConstructor, checkShapesAndAddCore } from './defaultShapes'
|
|
22
27
|
import { TLEditorSnapshot, loadSnapshot } from './TLEditorSnapshot'
|
|
@@ -36,6 +41,13 @@ export interface TLStoreBaseOptions {
|
|
|
36
41
|
/** How should this store upload & resolve assets? */
|
|
37
42
|
assets?: TLAssetStore
|
|
38
43
|
|
|
44
|
+
/**
|
|
45
|
+
* Named theme definitions. When provided, custom color names are automatically
|
|
46
|
+
* registered before the store is constructed so persisted data with those
|
|
47
|
+
* colors passes validation on load.
|
|
48
|
+
*/
|
|
49
|
+
themes?: Partial<TLThemes>
|
|
50
|
+
|
|
39
51
|
/** How should this store resolve users for attribution? */
|
|
40
52
|
users?: TLUserStore
|
|
41
53
|
|
|
@@ -50,8 +62,9 @@ export type TLStoreSchemaOptions =
|
|
|
50
62
|
}
|
|
51
63
|
| {
|
|
52
64
|
shapeUtils?: readonly TLAnyShapeUtilConstructor[]
|
|
53
|
-
migrations?: readonly MigrationSequence[]
|
|
54
65
|
bindingUtils?: readonly TLAnyBindingUtilConstructor[]
|
|
66
|
+
assetUtils?: readonly TLAnyAssetUtilConstructor[]
|
|
67
|
+
migrations?: readonly MigrationSequence[]
|
|
55
68
|
records?: Record<string, CustomRecordInfo>
|
|
56
69
|
}
|
|
57
70
|
|
|
@@ -114,6 +127,10 @@ export function createTLSchemaFromUtils(
|
|
|
114
127
|
'bindingUtils' in opts && opts.bindingUtils
|
|
115
128
|
? utilsToMap(checkBindings(opts.bindingUtils))
|
|
116
129
|
: undefined,
|
|
130
|
+
assets:
|
|
131
|
+
'assetUtils' in opts && opts.assetUtils
|
|
132
|
+
? utilsToMap(checkAssets(opts.assetUtils))
|
|
133
|
+
: undefined,
|
|
117
134
|
records: 'records' in opts ? opts.records : undefined,
|
|
118
135
|
migrations: 'migrations' in opts ? opts.migrations : undefined,
|
|
119
136
|
})
|
|
@@ -134,8 +151,12 @@ export function createTLStore({
|
|
|
134
151
|
users = defaultUserStore,
|
|
135
152
|
onMount,
|
|
136
153
|
collaboration,
|
|
154
|
+
themes,
|
|
137
155
|
...rest
|
|
138
156
|
}: TLStoreOptions = {}): TLStore {
|
|
157
|
+
const resolvedThemes = resolveThemes(themes)
|
|
158
|
+
registerColorsFromThemes(resolvedThemes)
|
|
159
|
+
registerFontsFromThemes(resolvedThemes)
|
|
139
160
|
const schema = createTLSchemaFromUtils(rest)
|
|
140
161
|
|
|
141
162
|
const store = new Store({
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { TLAssetUtilConstructor } from '../editor/assets/AssetUtil'
|
|
2
|
+
|
|
3
|
+
/** @public */
|
|
4
|
+
export type TLAnyAssetUtilConstructor = TLAssetUtilConstructor<any>
|
|
5
|
+
|
|
6
|
+
export function checkAssets(customAssets: readonly TLAnyAssetUtilConstructor[]) {
|
|
7
|
+
const assets = [] as TLAnyAssetUtilConstructor[]
|
|
8
|
+
|
|
9
|
+
const addedCustomAssetTypes = new Set<string>()
|
|
10
|
+
for (const customAsset of customAssets) {
|
|
11
|
+
if (addedCustomAssetTypes.has(customAsset.type)) {
|
|
12
|
+
throw new Error(`Asset type "${customAsset.type}" is defined more than once`)
|
|
13
|
+
}
|
|
14
|
+
assets.push(customAsset)
|
|
15
|
+
addedCustomAssetTypes.add(customAsset.type)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return assets
|
|
19
|
+
}
|