@tldraw/editor 5.5.4 → 5.6.0-canary.0091c91994d6
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 +166 -1
- package/dist-cjs/index.js +7 -1
- package/dist-cjs/index.js.map +2 -2
- package/dist-cjs/lib/TldrawEditor.js +18 -5
- package/dist-cjs/lib/TldrawEditor.js.map +3 -3
- package/dist-cjs/lib/components/default-components/DefaultErrorFallback.js +57 -15
- package/dist-cjs/lib/components/default-components/DefaultErrorFallback.js.map +2 -2
- package/dist-cjs/lib/config/TLI18n.js +64 -0
- package/dist-cjs/lib/config/TLI18n.js.map +7 -0
- package/dist-cjs/lib/editor/Editor.js +44 -16
- package/dist-cjs/lib/editor/Editor.js.map +2 -2
- package/dist-cjs/lib/editor/derivations/shapeIdsInCurrentPage.js +10 -3
- package/dist-cjs/lib/editor/derivations/shapeIdsInCurrentPage.js.map +2 -2
- package/dist-cjs/lib/editor/managers/EdgeScrollManager/EdgeScrollManager.js +1 -1
- package/dist-cjs/lib/editor/managers/EdgeScrollManager/EdgeScrollManager.js.map +2 -2
- package/dist-cjs/lib/editor/managers/FontManager/FontManager.js +19 -2
- package/dist-cjs/lib/editor/managers/FontManager/FontManager.js.map +2 -2
- package/dist-cjs/lib/editor/managers/ScribbleManager/ScribbleManager.js +3 -5
- package/dist-cjs/lib/editor/managers/ScribbleManager/ScribbleManager.js.map +2 -2
- package/dist-cjs/lib/editor/managers/SpatialIndexManager/SpatialIndexManager.js +7 -1
- package/dist-cjs/lib/editor/managers/SpatialIndexManager/SpatialIndexManager.js.map +2 -2
- package/dist-cjs/lib/editor/shapes/ShapeUtil.js +28 -0
- package/dist-cjs/lib/editor/shapes/ShapeUtil.js.map +2 -2
- package/dist-cjs/lib/editor/shapes/group/GroupShapeUtil.js +3 -0
- package/dist-cjs/lib/editor/shapes/group/GroupShapeUtil.js.map +2 -2
- package/dist-cjs/lib/editor/shapes/shared/resizeBox.js +21 -16
- package/dist-cjs/lib/editor/shapes/shared/resizeBox.js.map +2 -2
- package/dist-cjs/lib/hooks/useCursor.js +12 -8
- package/dist-cjs/lib/hooks/useCursor.js.map +2 -2
- package/dist-cjs/lib/hooks/useDocumentEvents.js +28 -3
- package/dist-cjs/lib/hooks/useDocumentEvents.js.map +2 -2
- package/dist-cjs/lib/license/LicenseManager.js +59 -0
- package/dist-cjs/lib/license/LicenseManager.js.map +2 -2
- package/dist-cjs/lib/utils/sync/TLLocalSyncClient.js +12 -5
- package/dist-cjs/lib/utils/sync/TLLocalSyncClient.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 +166 -1
- package/dist-esm/index.mjs +13 -1
- package/dist-esm/index.mjs.map +2 -2
- package/dist-esm/lib/TldrawEditor.mjs +18 -5
- package/dist-esm/lib/TldrawEditor.mjs.map +2 -2
- package/dist-esm/lib/components/default-components/DefaultErrorFallback.mjs +61 -16
- package/dist-esm/lib/components/default-components/DefaultErrorFallback.mjs.map +2 -2
- package/dist-esm/lib/config/TLI18n.mjs +44 -0
- package/dist-esm/lib/config/TLI18n.mjs.map +7 -0
- package/dist-esm/lib/editor/Editor.mjs +46 -17
- package/dist-esm/lib/editor/Editor.mjs.map +2 -2
- package/dist-esm/lib/editor/derivations/shapeIdsInCurrentPage.mjs +10 -3
- package/dist-esm/lib/editor/derivations/shapeIdsInCurrentPage.mjs.map +2 -2
- package/dist-esm/lib/editor/managers/EdgeScrollManager/EdgeScrollManager.mjs +1 -1
- package/dist-esm/lib/editor/managers/EdgeScrollManager/EdgeScrollManager.mjs.map +2 -2
- package/dist-esm/lib/editor/managers/FontManager/FontManager.mjs +20 -3
- package/dist-esm/lib/editor/managers/FontManager/FontManager.mjs.map +2 -2
- package/dist-esm/lib/editor/managers/ScribbleManager/ScribbleManager.mjs +3 -5
- package/dist-esm/lib/editor/managers/ScribbleManager/ScribbleManager.mjs.map +2 -2
- package/dist-esm/lib/editor/managers/SpatialIndexManager/SpatialIndexManager.mjs +7 -1
- package/dist-esm/lib/editor/managers/SpatialIndexManager/SpatialIndexManager.mjs.map +2 -2
- package/dist-esm/lib/editor/shapes/ShapeUtil.mjs +28 -0
- package/dist-esm/lib/editor/shapes/ShapeUtil.mjs.map +2 -2
- package/dist-esm/lib/editor/shapes/group/GroupShapeUtil.mjs +3 -0
- package/dist-esm/lib/editor/shapes/group/GroupShapeUtil.mjs.map +2 -2
- package/dist-esm/lib/editor/shapes/shared/resizeBox.mjs +21 -16
- package/dist-esm/lib/editor/shapes/shared/resizeBox.mjs.map +2 -2
- package/dist-esm/lib/hooks/useCursor.mjs +12 -8
- package/dist-esm/lib/hooks/useCursor.mjs.map +2 -2
- package/dist-esm/lib/hooks/useDocumentEvents.mjs +28 -3
- package/dist-esm/lib/hooks/useDocumentEvents.mjs.map +2 -2
- package/dist-esm/lib/license/LicenseManager.mjs +59 -0
- package/dist-esm/lib/license/LicenseManager.mjs.map +2 -2
- package/dist-esm/lib/utils/sync/TLLocalSyncClient.mjs +13 -6
- package/dist-esm/lib/utils/sync/TLLocalSyncClient.mjs.map +2 -2
- package/dist-esm/version.mjs +3 -3
- package/dist-esm/version.mjs.map +1 -1
- package/package.json +17 -19
- package/src/index.ts +14 -2
- package/src/lib/TldrawEditor.tsx +44 -4
- package/src/lib/components/default-components/DefaultErrorFallback.tsx +66 -18
- package/src/lib/config/TLI18n.test.ts +84 -0
- package/src/lib/config/TLI18n.ts +172 -0
- package/src/lib/editor/Editor.ts +90 -33
- package/src/lib/editor/derivations/shapeIdsInCurrentPage.ts +19 -3
- package/src/lib/editor/managers/EdgeScrollManager/EdgeScrollManager.test.ts +17 -0
- package/src/lib/editor/managers/EdgeScrollManager/EdgeScrollManager.ts +2 -2
- package/src/lib/editor/managers/FontManager/FontManager.test.ts +1 -0
- package/src/lib/editor/managers/FontManager/FontManager.ts +26 -3
- package/src/lib/editor/managers/HistoryManager/HistoryManager.test.ts +32 -0
- package/src/lib/editor/managers/ScribbleManager/ScribbleManager.ts +4 -6
- package/src/lib/editor/managers/SpatialIndexManager/SpatialIndexManager.ts +16 -7
- package/src/lib/editor/shapes/ShapeUtil.ts +30 -0
- package/src/lib/editor/shapes/group/GroupShapeUtil.tsx +4 -0
- package/src/lib/editor/shapes/shared/resizeBox.ts +21 -16
- package/src/lib/hooks/useCursor.ts +14 -7
- package/src/lib/hooks/useDocumentEvents.ts +51 -3
- package/src/lib/license/LicenseManager.test.ts +156 -0
- package/src/lib/license/LicenseManager.ts +83 -0
- package/src/lib/test/useCursor.test.tsx +36 -0
- package/src/lib/test/useDocumentEvents.test.tsx +153 -22
- package/src/lib/utils/sync/TLLocalSyncClient.test.ts +73 -0
- package/src/lib/utils/sync/TLLocalSyncClient.ts +24 -8
- package/src/version.ts +3 -3
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tldraw/editor",
|
|
3
3
|
"description": "tldraw infinite canvas SDK (editor).",
|
|
4
|
-
"version": "5.
|
|
4
|
+
"version": "5.6.0-canary.0091c91994d6",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "tldraw Inc.",
|
|
7
7
|
"email": "hello@tldraw.com"
|
|
@@ -39,27 +39,16 @@
|
|
|
39
39
|
"dist-cjs",
|
|
40
40
|
"src"
|
|
41
41
|
],
|
|
42
|
-
"scripts": {
|
|
43
|
-
"test-ci": "yarn run -T vitest run --passWithNoTests",
|
|
44
|
-
"test": "yarn run -T vitest --passWithNoTests",
|
|
45
|
-
"test-coverage": "yarn run -T vitest run --coverage --passWithNoTests",
|
|
46
|
-
"build": "yarn run -T tsx ../../internal/scripts/build-package.ts",
|
|
47
|
-
"build-api": "yarn run -T tsx ../../internal/scripts/build-api.ts",
|
|
48
|
-
"prepack": "yarn run -T tsx ../../internal/scripts/prepack.ts",
|
|
49
|
-
"postpack": "../../internal/scripts/postpack.sh",
|
|
50
|
-
"pack-tarball": "yarn pack",
|
|
51
|
-
"lint": "yarn run -T tsx ../../internal/scripts/lint.ts"
|
|
52
|
-
},
|
|
53
42
|
"dependencies": {
|
|
54
43
|
"@tiptap/core": "^3.12.1",
|
|
55
44
|
"@tiptap/pm": "^3.12.1",
|
|
56
45
|
"@tiptap/react": "^3.12.1",
|
|
57
|
-
"@tldraw/state": "5.
|
|
58
|
-
"@tldraw/state-react": "5.
|
|
59
|
-
"@tldraw/store": "5.
|
|
60
|
-
"@tldraw/tlschema": "5.
|
|
61
|
-
"@tldraw/utils": "5.
|
|
62
|
-
"@tldraw/validate": "5.
|
|
46
|
+
"@tldraw/state": "5.6.0-canary.0091c91994d6",
|
|
47
|
+
"@tldraw/state-react": "5.6.0-canary.0091c91994d6",
|
|
48
|
+
"@tldraw/store": "5.6.0-canary.0091c91994d6",
|
|
49
|
+
"@tldraw/tlschema": "5.6.0-canary.0091c91994d6",
|
|
50
|
+
"@tldraw/utils": "5.6.0-canary.0091c91994d6",
|
|
51
|
+
"@tldraw/validate": "5.6.0-canary.0091c91994d6",
|
|
63
52
|
"classnames": "^2.5.1",
|
|
64
53
|
"eventemitter3": "^4.0.7",
|
|
65
54
|
"idb": "^7.1.1",
|
|
@@ -79,7 +68,6 @@
|
|
|
79
68
|
"@types/react-dom": "^19.2.3",
|
|
80
69
|
"@types/wicg-file-system-access": "^2020.9.8",
|
|
81
70
|
"fake-indexeddb": "^4.0.2",
|
|
82
|
-
"lazyrepo": "0.0.0-alpha.27",
|
|
83
71
|
"react": "^19.2.1",
|
|
84
72
|
"react-dom": "^19.2.1",
|
|
85
73
|
"resize-observer-polyfill": "^1.5.1",
|
|
@@ -96,5 +84,15 @@
|
|
|
96
84
|
"require": "./dist-cjs/index.js"
|
|
97
85
|
},
|
|
98
86
|
"./editor.css": "./editor.css"
|
|
87
|
+
},
|
|
88
|
+
"scripts": {
|
|
89
|
+
"test-ci": "pnpm exec vitest run --passWithNoTests",
|
|
90
|
+
"test": "pnpm exec vitest --passWithNoTests",
|
|
91
|
+
"test-coverage": "pnpm exec vitest run --coverage --passWithNoTests",
|
|
92
|
+
"build": "pnpm exec tsx ../../internal/scripts/build-package.ts",
|
|
93
|
+
"build-api": "pnpm exec tsx ../../internal/scripts/build-api.ts",
|
|
94
|
+
"pack-tarball": "pnpm pack",
|
|
95
|
+
"lint": "pnpm exec tsx ../../internal/scripts/lint.ts",
|
|
96
|
+
"build-types": "tsc --build"
|
|
99
97
|
}
|
|
100
98
|
}
|
package/src/index.ts
CHANGED
|
@@ -75,6 +75,17 @@ export {
|
|
|
75
75
|
type TLEditorSnapshot,
|
|
76
76
|
type TLLoadSnapshotOptions,
|
|
77
77
|
} from './lib/config/TLEditorSnapshot'
|
|
78
|
+
export {
|
|
79
|
+
defaultI18n,
|
|
80
|
+
defineMessages,
|
|
81
|
+
getDeclaredMessages,
|
|
82
|
+
translateMessage,
|
|
83
|
+
type TLI18n,
|
|
84
|
+
type TLI18nAdapter,
|
|
85
|
+
type TLI18nMessage,
|
|
86
|
+
type TLI18nMessageInput,
|
|
87
|
+
type TLI18nValues,
|
|
88
|
+
} from './lib/config/TLI18n'
|
|
78
89
|
export {
|
|
79
90
|
createSessionStateSnapshotSignal,
|
|
80
91
|
extractSessionStateFromLegacySnapshot,
|
|
@@ -439,17 +450,18 @@ export {
|
|
|
439
450
|
} from './lib/primitives/utils'
|
|
440
451
|
export { Vec, type VecLike } from './lib/primitives/Vec'
|
|
441
452
|
export {
|
|
453
|
+
EditorI18nFallbackContext,
|
|
442
454
|
ErrorScreen,
|
|
443
455
|
LoadingScreen,
|
|
444
|
-
TldrawEditor,
|
|
445
|
-
useOnMount,
|
|
446
456
|
type LoadingScreenProps,
|
|
457
|
+
TldrawEditor,
|
|
447
458
|
type TldrawEditorBaseProps,
|
|
448
459
|
type TldrawEditorProps,
|
|
449
460
|
type TldrawEditorStoreProps,
|
|
450
461
|
type TldrawEditorWithoutStoreProps,
|
|
451
462
|
type TldrawEditorWithStoreProps,
|
|
452
463
|
type TLOnMountHandler,
|
|
464
|
+
useOnMount,
|
|
453
465
|
} from './lib/TldrawEditor'
|
|
454
466
|
export { dataUrlToFile, getDefaultCdnBaseUrl } from './lib/utils/assets'
|
|
455
467
|
export { clampToBrowserMaxCanvasSize, type CanvasMaxSize } from './lib/utils/browserCanvasMaxSize'
|
package/src/lib/TldrawEditor.tsx
CHANGED
|
@@ -32,6 +32,7 @@ import { TLAnyAssetUtilConstructor } from './config/defaultAssets'
|
|
|
32
32
|
import { TLAnyBindingUtilConstructor } from './config/defaultBindings'
|
|
33
33
|
import { TLAnyShapeUtilConstructor } from './config/defaultShapes'
|
|
34
34
|
import { TLEditorSnapshot } from './config/TLEditorSnapshot'
|
|
35
|
+
import { defaultI18n, TLI18nAdapter } from './config/TLI18n'
|
|
35
36
|
import { Editor } from './editor/Editor'
|
|
36
37
|
import { resolveThemes } from './editor/managers/ThemeManager/ThemeManager'
|
|
37
38
|
import { TLAnyOverlayUtilConstructor } from './editor/overlays/OverlayUtil'
|
|
@@ -253,6 +254,11 @@ export interface TldrawEditorBaseProps {
|
|
|
253
254
|
* The URLs for the fonts to use in the editor.
|
|
254
255
|
*/
|
|
255
256
|
assetUrls?: { fonts?: { [key: string]: string | undefined } }
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* An i18n adapter for translating strings.
|
|
260
|
+
*/
|
|
261
|
+
i18n?: TLI18nAdapter
|
|
256
262
|
}
|
|
257
263
|
|
|
258
264
|
/**
|
|
@@ -484,6 +490,23 @@ const TldrawEditorWithLoadingStore = memo(function TldrawEditorBeforeLoading({
|
|
|
484
490
|
|
|
485
491
|
const noAutoFocus = () => getGlobalDocument().location.search.includes('tldraw_preserve_focus')
|
|
486
492
|
|
|
493
|
+
/**
|
|
494
|
+
* A slot the UI fills with its own i18n, for an editor whose host passed none. Every app that
|
|
495
|
+
* composes `<TldrawEditor>` and `<TldrawUi>` by hand rather than rendering `<Tldraw>` is one: the
|
|
496
|
+
* `i18n` prop is new, so none of them pass it, and their shape names — which shapes now resolve
|
|
497
|
+
* through the editor's i18n — would have been announced in English. The UI renders inside the
|
|
498
|
+
* editor in every composition, so it can hand its translations up. An `i18n` prop still wins.
|
|
499
|
+
*
|
|
500
|
+
* `<Tldraw>` relies on this too, rather than passing a prop: the translation provider it renders
|
|
501
|
+
* above the editor has its locale fixed at mount, so an adapter read from there kept announcing
|
|
502
|
+
* shapes in the first language after the user switched.
|
|
503
|
+
*
|
|
504
|
+
* @internal
|
|
505
|
+
*/
|
|
506
|
+
export const EditorI18nFallbackContext = React.createContext<{
|
|
507
|
+
current: TLI18nAdapter | null
|
|
508
|
+
} | null>(null)
|
|
509
|
+
|
|
487
510
|
function TldrawEditorWithReadyStore({
|
|
488
511
|
onMount,
|
|
489
512
|
children,
|
|
@@ -505,6 +528,7 @@ function TldrawEditorWithReadyStore({
|
|
|
505
528
|
assetUrls,
|
|
506
529
|
themes,
|
|
507
530
|
initialTheme,
|
|
531
|
+
i18n,
|
|
508
532
|
}: Required<
|
|
509
533
|
TldrawEditorProps & {
|
|
510
534
|
store: TLStore
|
|
@@ -549,6 +573,18 @@ function TldrawEditorWithReadyStore({
|
|
|
549
573
|
}
|
|
550
574
|
}, [autoFocus, initialState, colorScheme, cameraOptions, deepLinks, themes, initialTheme])
|
|
551
575
|
|
|
576
|
+
// The adapter is called on each use rather than held as a value, so the editor can keep one
|
|
577
|
+
// stable function that reads whichever adapter is current. Listing `i18n` among the deps that
|
|
578
|
+
// recreate the editor instead tore it down — camera, selection, undo history — whenever the
|
|
579
|
+
// adapter's identity changed, which is every render for one written inline.
|
|
580
|
+
const i18nRef = useRef(i18n)
|
|
581
|
+
i18nRef.current = i18n
|
|
582
|
+
const fallbackI18nRef = useRef<TLI18nAdapter | null>(null)
|
|
583
|
+
const stableI18n = useCallback<TLI18nAdapter>(
|
|
584
|
+
() => (i18nRef.current ?? fallbackI18nRef.current ?? defaultI18n)(),
|
|
585
|
+
[]
|
|
586
|
+
)
|
|
587
|
+
|
|
552
588
|
useLayoutEffect(
|
|
553
589
|
() => {
|
|
554
590
|
const {
|
|
@@ -580,6 +616,7 @@ function TldrawEditorWithReadyStore({
|
|
|
580
616
|
fontAssetUrls: assetUrls?.fonts,
|
|
581
617
|
themes: themes,
|
|
582
618
|
initialTheme: initialTheme,
|
|
619
|
+
i18n: stableI18n,
|
|
583
620
|
})
|
|
584
621
|
editor.licenseManager = licenseManager
|
|
585
622
|
|
|
@@ -620,6 +657,7 @@ function TldrawEditorWithReadyStore({
|
|
|
620
657
|
licenseManager,
|
|
621
658
|
getShapeVisibility,
|
|
622
659
|
assetUrls,
|
|
660
|
+
stableI18n,
|
|
623
661
|
]
|
|
624
662
|
)
|
|
625
663
|
|
|
@@ -767,10 +805,12 @@ function TldrawEditorWithReadyStore({
|
|
|
767
805
|
<Crash crashingError={crashingError} />
|
|
768
806
|
) : (
|
|
769
807
|
<EditorProvider editor={editor}>
|
|
770
|
-
<
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
808
|
+
<EditorI18nFallbackContext.Provider value={fallbackI18nRef}>
|
|
809
|
+
<Layout onMount={onMount}>
|
|
810
|
+
{children ?? (Canvas ? <Canvas key={editor.contextId} /> : null)}
|
|
811
|
+
<Watermark />
|
|
812
|
+
</Layout>
|
|
813
|
+
</EditorI18nFallbackContext.Provider>
|
|
774
814
|
</EditorProvider>
|
|
775
815
|
)}
|
|
776
816
|
</OptionalErrorBoundary>
|
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import { useValue } from '@tldraw/state-react'
|
|
2
2
|
import { noop } from '@tldraw/utils'
|
|
3
3
|
import classNames from 'classnames'
|
|
4
|
-
import { ComponentType, useEffect, useLayoutEffect, useRef, useState } from 'react'
|
|
4
|
+
import { ComponentType, useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react'
|
|
5
|
+
import {
|
|
6
|
+
defineMessages,
|
|
7
|
+
translateMessage,
|
|
8
|
+
type TLI18nMessage,
|
|
9
|
+
type TLI18nValues,
|
|
10
|
+
} from '../../config/TLI18n'
|
|
5
11
|
import { Editor } from '../../editor/Editor'
|
|
6
12
|
import { getOwnerDocument } from '../../exports/domUtils'
|
|
7
13
|
import { useEditorComponents } from '../../hooks/EditorComponentsContext'
|
|
@@ -12,6 +18,50 @@ import { ErrorBoundary } from '../ErrorBoundary'
|
|
|
12
18
|
|
|
13
19
|
const BASE_ERROR_URL = 'https://github.com/tldraw/tldraw/issues/new'
|
|
14
20
|
|
|
21
|
+
const messages = defineMessages({
|
|
22
|
+
confirmResetTitle: { id: 'error.confirm-reset.title', defaultMessage: 'Are you sure?' },
|
|
23
|
+
confirmResetDescription: {
|
|
24
|
+
id: 'error.confirm-reset.description',
|
|
25
|
+
defaultMessage: 'Resetting your data will delete your drawing and cannot be undone.',
|
|
26
|
+
},
|
|
27
|
+
cancel: { id: 'error.cancel', defaultMessage: 'Cancel' },
|
|
28
|
+
resetData: { id: 'error.reset-data', defaultMessage: 'Reset data' },
|
|
29
|
+
title: { id: 'error.title', defaultMessage: 'Something went wrong' },
|
|
30
|
+
refreshHint: { id: 'error.refresh-hint', defaultMessage: 'Please refresh your browser.' },
|
|
31
|
+
resetHint: {
|
|
32
|
+
id: 'error.reset-hint',
|
|
33
|
+
defaultMessage:
|
|
34
|
+
'If the issue continues after refreshing, you may need to reset the tldraw data stored on your device.',
|
|
35
|
+
},
|
|
36
|
+
noteLabel: { id: 'error.note-label', defaultMessage: 'Note:' },
|
|
37
|
+
resetNote: {
|
|
38
|
+
id: 'error.reset-note',
|
|
39
|
+
defaultMessage: 'Resetting will erase your current project and any unsaved work.',
|
|
40
|
+
},
|
|
41
|
+
messageLabel: { id: 'error.message-label', defaultMessage: 'Message:' },
|
|
42
|
+
stackTraceLabel: { id: 'error.stack-trace-label', defaultMessage: 'Stack trace:' },
|
|
43
|
+
copy: { id: 'error.copy', defaultMessage: 'Copy' },
|
|
44
|
+
copied: { id: 'error.copied', defaultMessage: 'Copied!' },
|
|
45
|
+
showDetails: { id: 'error.show-details', defaultMessage: 'Show details' },
|
|
46
|
+
hideDetails: { id: 'error.hide-details', defaultMessage: 'Hide details' },
|
|
47
|
+
refreshPage: { id: 'error.refresh-page', defaultMessage: 'Refresh page' },
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
// This renders because something already threw, so reading the editor is done defensively the way
|
|
51
|
+
// the rest of this component reads it: English beats a second crash on the error screen.
|
|
52
|
+
function useErrorMsg(editor: Editor | undefined) {
|
|
53
|
+
return useCallback(
|
|
54
|
+
(message: TLI18nMessage, values?: TLI18nValues) => {
|
|
55
|
+
try {
|
|
56
|
+
return translateMessage(editor?.i18n(), message, values)
|
|
57
|
+
} catch {
|
|
58
|
+
return message.defaultMessage
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
[editor]
|
|
62
|
+
)
|
|
63
|
+
}
|
|
64
|
+
|
|
15
65
|
/** @public */
|
|
16
66
|
export interface TLErrorFallbackProps {
|
|
17
67
|
/** The error that was caught. */
|
|
@@ -28,6 +78,7 @@ export type TLErrorFallbackComponent = ComponentType<TLErrorFallbackProps>
|
|
|
28
78
|
|
|
29
79
|
/** @public @react */
|
|
30
80
|
export function DefaultErrorFallback({ error, editor }: TLErrorFallbackProps) {
|
|
81
|
+
const msg = useErrorMsg(editor)
|
|
31
82
|
const containerRef = useRef<HTMLDivElement>(null)
|
|
32
83
|
const [shouldShowError, setShouldShowError] = useState(process.env.NODE_ENV === 'development')
|
|
33
84
|
const [didCopy, setDidCopy] = useState(false)
|
|
@@ -168,27 +219,24 @@ My browser: ${navigator.userAgent}`
|
|
|
168
219
|
>
|
|
169
220
|
{shouldShowResetConfirmation ? (
|
|
170
221
|
<>
|
|
171
|
-
<h2>
|
|
172
|
-
<p>
|
|
222
|
+
<h2>{msg(messages.confirmResetTitle)}</h2>
|
|
223
|
+
<p>{msg(messages.confirmResetDescription)}</p>
|
|
173
224
|
<div className="tl-error-boundary__content__actions">
|
|
174
225
|
<button className="tlui-button" onClick={() => setShouldShowResetConfirmation(false)}>
|
|
175
|
-
|
|
226
|
+
{msg(messages.cancel)}
|
|
176
227
|
</button>
|
|
177
228
|
<button className="tlui-button tl-error-boundary__reset" onClick={resetLocalState}>
|
|
178
|
-
|
|
229
|
+
{msg(messages.resetData)}
|
|
179
230
|
</button>
|
|
180
231
|
</div>
|
|
181
232
|
</>
|
|
182
233
|
) : (
|
|
183
234
|
<>
|
|
184
|
-
<h2>
|
|
185
|
-
<p>
|
|
186
|
-
<p>
|
|
187
|
-
If the issue continues after refreshing, you may need to reset the tldraw data stored
|
|
188
|
-
on your device.
|
|
189
|
-
</p>
|
|
235
|
+
<h2>{msg(messages.title)}</h2>
|
|
236
|
+
<p>{msg(messages.refreshHint)}</p>
|
|
237
|
+
<p>{msg(messages.resetHint)}</p>
|
|
190
238
|
<p>
|
|
191
|
-
<strong>
|
|
239
|
+
<strong>{msg(messages.noteLabel)}</strong> {msg(messages.resetNote)}
|
|
192
240
|
</p>
|
|
193
241
|
{process.env.NODE_ENV !== 'production' && (
|
|
194
242
|
<p>
|
|
@@ -201,34 +249,34 @@ My browser: ${navigator.userAgent}`
|
|
|
201
249
|
)}
|
|
202
250
|
{shouldShowError && (
|
|
203
251
|
<>
|
|
204
|
-
|
|
252
|
+
{msg(messages.messageLabel)}
|
|
205
253
|
<h4>
|
|
206
254
|
<code>{errorMessage}</code>
|
|
207
255
|
</h4>
|
|
208
|
-
|
|
256
|
+
{msg(messages.stackTraceLabel)}
|
|
209
257
|
<div className="tl-error-boundary__content__error">
|
|
210
258
|
<pre>
|
|
211
259
|
<code>{errorStack ?? errorMessage}</code>
|
|
212
260
|
</pre>
|
|
213
261
|
<button className="tlui-button" onClick={copyError}>
|
|
214
|
-
{didCopy ?
|
|
262
|
+
{didCopy ? msg(messages.copied) : msg(messages.copy)}
|
|
215
263
|
</button>
|
|
216
264
|
</div>
|
|
217
265
|
</>
|
|
218
266
|
)}
|
|
219
267
|
<div className="tl-error-boundary__content__actions">
|
|
220
268
|
<button className="tlui-button" onClick={() => setShouldShowError(!shouldShowError)}>
|
|
221
|
-
{shouldShowError ?
|
|
269
|
+
{shouldShowError ? msg(messages.hideDetails) : msg(messages.showDetails)}
|
|
222
270
|
</button>
|
|
223
271
|
<div className="tl-error-boundary__content__actions__group">
|
|
224
272
|
<button
|
|
225
273
|
className="tlui-button tl-error-boundary__reset"
|
|
226
274
|
onClick={() => setShouldShowResetConfirmation(true)}
|
|
227
275
|
>
|
|
228
|
-
|
|
276
|
+
{msg(messages.resetData)}
|
|
229
277
|
</button>
|
|
230
278
|
<button className="tlui-button tl-error-boundary__refresh" onClick={refresh}>
|
|
231
|
-
|
|
279
|
+
{msg(messages.refreshPage)}
|
|
232
280
|
</button>
|
|
233
281
|
</div>
|
|
234
282
|
</div>
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import {
|
|
3
|
+
TLI18n,
|
|
4
|
+
defaultI18n,
|
|
5
|
+
defineMessages,
|
|
6
|
+
getDeclaredMessages,
|
|
7
|
+
translateMessage,
|
|
8
|
+
} from './TLI18n'
|
|
9
|
+
|
|
10
|
+
const messages = defineMessages({
|
|
11
|
+
somethingWentWrong: { id: 'error.title', defaultMessage: 'Something went wrong' },
|
|
12
|
+
shapesSelected: {
|
|
13
|
+
id: 'a11y.multiple-shapes',
|
|
14
|
+
defaultMessage: '{num, plural, one {# shape selected} other {# shapes selected}}',
|
|
15
|
+
},
|
|
16
|
+
// A plain placeholder, which the regex stub below can substitute. The plural above needs a
|
|
17
|
+
// real ICU formatter, so the adapter's own handling of it is covered in packages/tldraw.
|
|
18
|
+
addedFiles: { id: 'test.added-files', defaultMessage: 'Added {total} files' },
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
function i18nWith(catalog: Record<string, string>): TLI18n {
|
|
22
|
+
return {
|
|
23
|
+
locale: 'en',
|
|
24
|
+
dir: 'ltr',
|
|
25
|
+
// Mirrors the real adapter: a message the catalog doesn't have falls back to its own
|
|
26
|
+
// English, and either way the values are substituted.
|
|
27
|
+
translate: (message, values) => {
|
|
28
|
+
const text = catalog[message.id] ?? message.defaultMessage
|
|
29
|
+
return text.replace(/\{(\w+)[^}]*\}/g, (_, name) => String(values?.[name] ?? ''))
|
|
30
|
+
},
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
describe('translateMessage', () => {
|
|
35
|
+
it('uses the translation when the catalog has one', () => {
|
|
36
|
+
const i18n = i18nWith({ 'error.title': 'Etwas ist schiefgelaufen' })
|
|
37
|
+
expect(translateMessage(i18n, messages.somethingWentWrong)).toBe('Etwas ist schiefgelaufen')
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
// @tldraw/editor is usable with none of tldraw's UI, so there may be no i18n at all.
|
|
41
|
+
it('falls back to the English when there is no i18n', () => {
|
|
42
|
+
expect(translateMessage(null, messages.somethingWentWrong)).toBe('Something went wrong')
|
|
43
|
+
expect(translateMessage(undefined, messages.somethingWentWrong)).toBe('Something went wrong')
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
// A message that hasn't been through Lokalise yet is absent from every catalog. Showing the id
|
|
47
|
+
// to someone would be worse than English.
|
|
48
|
+
it('falls back to the English when the catalog has no entry', () => {
|
|
49
|
+
expect(translateMessage(i18nWith({}), messages.somethingWentWrong)).toBe('Something went wrong')
|
|
50
|
+
expect(translateMessage(defaultI18n(), messages.somethingWentWrong)).toBe(
|
|
51
|
+
'Something went wrong'
|
|
52
|
+
)
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
// The adapter gets the whole message, so it can substitute into the English too. Falling back
|
|
56
|
+
// to the raw source instead put a plural message's own ICU into an upload toast.
|
|
57
|
+
it('substitutes values into the English it falls back to', () => {
|
|
58
|
+
expect(translateMessage(i18nWith({}), messages.addedFiles, { total: 3 })).toBe('Added 3 files')
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
it('passes values through to the translation', () => {
|
|
62
|
+
const i18n = i18nWith({ 'a11y.multiple-shapes': '{num} Formen ausgewählt' })
|
|
63
|
+
expect(translateMessage(i18n, messages.shapesSelected, { num: 3 })).toBe('3 Formen ausgewählt')
|
|
64
|
+
})
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
describe('defineMessages', () => {
|
|
68
|
+
it('returns the messages unchanged', () => {
|
|
69
|
+
const declared = { a: { id: 'x', defaultMessage: 'X' } }
|
|
70
|
+
expect(defineMessages(declared)).toEqual(declared)
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
// The SDK bundles no separate English catalog; a key looked up by id resolves against this.
|
|
74
|
+
it('records the English of each message it declares', () => {
|
|
75
|
+
defineMessages({
|
|
76
|
+
a: { id: 'test.declared', defaultMessage: 'Declared' },
|
|
77
|
+
b: { defaultMessage: 'No id' },
|
|
78
|
+
})
|
|
79
|
+
expect(getDeclaredMessages()).toMatchObject({
|
|
80
|
+
'test.declared': 'Declared',
|
|
81
|
+
'No id': 'No id',
|
|
82
|
+
})
|
|
83
|
+
})
|
|
84
|
+
})
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { atom } from '@tldraw/state'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Values substituted into a message's ICU placeholders.
|
|
5
|
+
*
|
|
6
|
+
* @public
|
|
7
|
+
*/
|
|
8
|
+
export type TLI18nValues = Record<
|
|
9
|
+
string,
|
|
10
|
+
string | number | bigint | boolean | Date | null | undefined
|
|
11
|
+
>
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The editor's view of the host app's translations. Lets code with no access to React context —
|
|
15
|
+
* a {@link ShapeUtil}, say — translate a string in the user's locale.
|
|
16
|
+
*
|
|
17
|
+
* @public
|
|
18
|
+
*/
|
|
19
|
+
export interface TLI18n {
|
|
20
|
+
locale: string
|
|
21
|
+
dir: 'ltr' | 'rtl'
|
|
22
|
+
/**
|
|
23
|
+
* Translate `message` in the current locale, substituting `values` into its ICU placeholders.
|
|
24
|
+
*
|
|
25
|
+
* Takes the whole message rather than its id so an implementation can fall back to the English
|
|
26
|
+
* and still substitute into it. A message absent from the catalog — anything not yet through
|
|
27
|
+
* Lokalise — would otherwise have to come back as its own source text, placeholders and all.
|
|
28
|
+
*/
|
|
29
|
+
translate(message: TLI18nMessage, values?: TLI18nValues): string
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Supplies a {@link TLI18n}. Called on each use rather than held as a value, so the editor reads
|
|
34
|
+
* the current locale instead of pinning whichever one was active when it was constructed.
|
|
35
|
+
*
|
|
36
|
+
* @public
|
|
37
|
+
*/
|
|
38
|
+
export type TLI18nAdapter = () => TLI18n
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The i18n used when the host app supplies none: every message comes back as its own English.
|
|
42
|
+
*
|
|
43
|
+
* Placeholders are left as they are, because formatting ICU needs a formatter and this package
|
|
44
|
+
* deliberately has none — it is the floor for an editor running with no translations at all.
|
|
45
|
+
*
|
|
46
|
+
* @public
|
|
47
|
+
*/
|
|
48
|
+
export function defaultI18n(): TLI18n {
|
|
49
|
+
return {
|
|
50
|
+
locale: 'en',
|
|
51
|
+
dir: 'ltr',
|
|
52
|
+
translate(message: TLI18nMessage) {
|
|
53
|
+
return message.defaultMessage
|
|
54
|
+
},
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* One translatable message: a stable `id` naming it in the catalog, and the English it falls back
|
|
60
|
+
* to. `defaultMessage` is required, because this package's strings have to read correctly with no
|
|
61
|
+
* translations loaded at all — `@tldraw/editor` can be used without any of tldraw's UI.
|
|
62
|
+
*
|
|
63
|
+
* @public
|
|
64
|
+
*/
|
|
65
|
+
export interface TLI18nMessage {
|
|
66
|
+
id: string
|
|
67
|
+
defaultMessage: string
|
|
68
|
+
description?: string
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* A message as it's written. The id is optional because a bundler plugin may supply it:
|
|
73
|
+
* `@formatjs/unplugin` with an `idInterpolationPattern` derives one from the message's own text,
|
|
74
|
+
* which is how tldraw.com does it. Everywhere downstream takes a {@link TLI18nMessage}, whose id
|
|
75
|
+
* is required, because {@link defineMessages} fills one in.
|
|
76
|
+
*
|
|
77
|
+
* @public
|
|
78
|
+
*/
|
|
79
|
+
export type TLI18nMessageInput = Omit<TLI18nMessage, 'id'> & { id?: string }
|
|
80
|
+
|
|
81
|
+
const declaredMessages: Record<string, string> = {}
|
|
82
|
+
// Bumped per `defineMessages` call, so a reader re-renders when a chunk loaded after mount — a
|
|
83
|
+
// dynamically imported @tldraw/commenting, say — declares messages its labels look up by id.
|
|
84
|
+
const declaredVersion = atom('declaredMessagesVersion', 0)
|
|
85
|
+
let snapshot: Readonly<Record<string, string>> = {}
|
|
86
|
+
let snapshotVersion = -1
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Declares messages so the extractor can find them, next to the UI that shows them — the English
|
|
90
|
+
* then lives with the code rather than in a catalog edited by hand. Call it by this name:
|
|
91
|
+
* `formatjs extract` matches the call by name, wherever it was imported from, so a wrapper under
|
|
92
|
+
* another name extracts nothing.
|
|
93
|
+
*
|
|
94
|
+
* Write the id yourself, as the SDK's own messages do — an app overrides by id, so an id has to
|
|
95
|
+
* survive a reworded English string. Leave it out only where a bundler plugin injects one; with no
|
|
96
|
+
* plugin either the English stands in, so the message still renders as itself. An id missing from
|
|
97
|
+
* a real build is caught by `pnpm i18n-extract --check`.
|
|
98
|
+
*
|
|
99
|
+
* The type parameter is `const` so each written `id` keeps its literal type, which is what lets a
|
|
100
|
+
* declared id go where a `TLUiTranslationKey` is expected — a menu item's `label`, say — instead
|
|
101
|
+
* of widening to `string` and needing a cast.
|
|
102
|
+
*
|
|
103
|
+
* For a message rendered as an element, write it inline on the `<F>` instead; the extractor reads
|
|
104
|
+
* those props directly. This is for the ones that can't be: a label the UI takes as data, and
|
|
105
|
+
* anything translated as a string through `useTranslation`.
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
*
|
|
109
|
+
* ```ts
|
|
110
|
+
* const messages = defineMessages({
|
|
111
|
+
* copy: { id: 'action.copy', defaultMessage: 'Copy' },
|
|
112
|
+
* })
|
|
113
|
+
* ```
|
|
114
|
+
*
|
|
115
|
+
* @public
|
|
116
|
+
*/
|
|
117
|
+
export function defineMessages<const Messages extends Record<string, TLI18nMessageInput>>(
|
|
118
|
+
msgs: Messages
|
|
119
|
+
): { [K in keyof Messages]: Messages[K] & TLI18nMessage } {
|
|
120
|
+
const withIds = {} as { [K in keyof Messages]: Messages[K] & TLI18nMessage }
|
|
121
|
+
for (const key in msgs) {
|
|
122
|
+
const message = msgs[key]
|
|
123
|
+
const withId = (
|
|
124
|
+
message.id ? message : { ...message, id: message.defaultMessage }
|
|
125
|
+
) as Messages[typeof key] & TLI18nMessage
|
|
126
|
+
withIds[key] = withId
|
|
127
|
+
declaredMessages[withId.id] = withId.defaultMessage
|
|
128
|
+
}
|
|
129
|
+
declaredVersion.update((v) => v + 1)
|
|
130
|
+
return withIds
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* The English of every message declared so far, by id. This is the English catalog: building it
|
|
135
|
+
* from the declarations means it isn't bundled a second time as a generated file. A key looked up
|
|
136
|
+
* by id alone — a menu label, or an app calling `msg('action.copy')` — resolves against it.
|
|
137
|
+
*
|
|
138
|
+
* Declarations run as their modules load, so this is complete once the package has been imported.
|
|
139
|
+
* Reactive: read it inside `useValue` or a `computed` to pick up a module that loads later. The
|
|
140
|
+
* returned object changes identity only when something new is declared.
|
|
141
|
+
*
|
|
142
|
+
* @internal
|
|
143
|
+
*/
|
|
144
|
+
export function getDeclaredMessages(): Readonly<Record<string, string>> {
|
|
145
|
+
const version = declaredVersion.get()
|
|
146
|
+
if (version !== snapshotVersion) {
|
|
147
|
+
snapshot = { ...declaredMessages }
|
|
148
|
+
snapshotVersion = version
|
|
149
|
+
}
|
|
150
|
+
return snapshot
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Translates `message` through an editor's i18n, falling back to its English.
|
|
155
|
+
*
|
|
156
|
+
* Takes the editor's i18n rather than reading a context, so it works in the places this package
|
|
157
|
+
* has to keep working: a `ShapeUtil` with no hooks available, or an error fallback rendered
|
|
158
|
+
* outside the editor it's reporting on.
|
|
159
|
+
*
|
|
160
|
+
* With no i18n at all the English is returned unformatted, for the reason given on
|
|
161
|
+
* {@link defaultI18n}. Every other case goes through the adapter, which does substitute.
|
|
162
|
+
*
|
|
163
|
+
* @public
|
|
164
|
+
*/
|
|
165
|
+
export function translateMessage(
|
|
166
|
+
i18n: TLI18n | null | undefined,
|
|
167
|
+
message: TLI18nMessage,
|
|
168
|
+
values?: TLI18nValues
|
|
169
|
+
): string {
|
|
170
|
+
if (!i18n) return message.defaultMessage
|
|
171
|
+
return i18n.translate(message, values)
|
|
172
|
+
}
|