@tldraw/editor 3.8.0-canary.4569e7d7e9d9 → 3.8.0-canary.4703b6039d91

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 (69) hide show
  1. package/dist-cjs/index.d.ts +145 -13
  2. package/dist-cjs/index.js +5 -1
  3. package/dist-cjs/index.js.map +2 -2
  4. package/dist-cjs/lib/TldrawEditor.js +4 -2
  5. package/dist-cjs/lib/TldrawEditor.js.map +2 -2
  6. package/dist-cjs/lib/components/default-components/DefaultCanvas.js +1 -4
  7. package/dist-cjs/lib/components/default-components/DefaultCanvas.js.map +2 -2
  8. package/dist-cjs/lib/config/createTLStore.js +4 -2
  9. package/dist-cjs/lib/config/createTLStore.js.map +2 -2
  10. package/dist-cjs/lib/editor/Editor.js +75 -7
  11. package/dist-cjs/lib/editor/Editor.js.map +2 -2
  12. package/dist-cjs/lib/editor/shapes/ShapeUtil.js.map +2 -2
  13. package/dist-cjs/lib/editor/types/SvgExportContext.js.map +2 -2
  14. package/dist-cjs/lib/editor/types/misc-types.js.map +1 -1
  15. package/dist-cjs/lib/exports/exportToSvg.js.map +2 -2
  16. package/dist-cjs/lib/exports/getSvgAsImage.js +83 -0
  17. package/dist-cjs/lib/exports/getSvgAsImage.js.map +7 -0
  18. package/dist-cjs/lib/exports/getSvgJsx.js +16 -3
  19. package/dist-cjs/lib/exports/getSvgJsx.js.map +2 -2
  20. package/dist-cjs/lib/hooks/useLocalStore.js +1 -1
  21. package/dist-cjs/lib/hooks/useLocalStore.js.map +2 -2
  22. package/dist-cjs/lib/utils/browserCanvasMaxSize.js +75 -0
  23. package/dist-cjs/lib/utils/browserCanvasMaxSize.js.map +7 -0
  24. package/dist-cjs/lib/utils/sync/TLLocalSyncClient.js +3 -1
  25. package/dist-cjs/lib/utils/sync/TLLocalSyncClient.js.map +2 -2
  26. package/dist-cjs/version.js +3 -3
  27. package/dist-cjs/version.js.map +1 -1
  28. package/dist-esm/index.d.mts +145 -13
  29. package/dist-esm/index.mjs +5 -1
  30. package/dist-esm/index.mjs.map +2 -2
  31. package/dist-esm/lib/TldrawEditor.mjs +4 -2
  32. package/dist-esm/lib/TldrawEditor.mjs.map +2 -2
  33. package/dist-esm/lib/components/default-components/DefaultCanvas.mjs +1 -4
  34. package/dist-esm/lib/components/default-components/DefaultCanvas.mjs.map +2 -2
  35. package/dist-esm/lib/config/createTLStore.mjs +4 -2
  36. package/dist-esm/lib/config/createTLStore.mjs.map +2 -2
  37. package/dist-esm/lib/editor/Editor.mjs +75 -7
  38. package/dist-esm/lib/editor/Editor.mjs.map +2 -2
  39. package/dist-esm/lib/editor/shapes/ShapeUtil.mjs.map +2 -2
  40. package/dist-esm/lib/editor/types/SvgExportContext.mjs.map +2 -2
  41. package/dist-esm/lib/exports/exportToSvg.mjs.map +2 -2
  42. package/dist-esm/lib/exports/getSvgAsImage.mjs +63 -0
  43. package/dist-esm/lib/exports/getSvgAsImage.mjs.map +7 -0
  44. package/dist-esm/lib/exports/getSvgJsx.mjs +16 -3
  45. package/dist-esm/lib/exports/getSvgJsx.mjs.map +2 -2
  46. package/dist-esm/lib/hooks/useLocalStore.mjs +1 -1
  47. package/dist-esm/lib/hooks/useLocalStore.mjs.map +2 -2
  48. package/dist-esm/lib/utils/browserCanvasMaxSize.mjs +45 -0
  49. package/dist-esm/lib/utils/browserCanvasMaxSize.mjs.map +7 -0
  50. package/dist-esm/lib/utils/sync/TLLocalSyncClient.mjs +3 -1
  51. package/dist-esm/lib/utils/sync/TLLocalSyncClient.mjs.map +2 -2
  52. package/dist-esm/version.mjs +3 -3
  53. package/dist-esm/version.mjs.map +1 -1
  54. package/package.json +9 -7
  55. package/src/index.ts +5 -0
  56. package/src/lib/TldrawEditor.tsx +2 -0
  57. package/src/lib/components/default-components/DefaultCanvas.tsx +1 -4
  58. package/src/lib/config/createTLStore.ts +4 -2
  59. package/src/lib/editor/Editor.ts +103 -25
  60. package/src/lib/editor/shapes/ShapeUtil.ts +30 -1
  61. package/src/lib/editor/types/SvgExportContext.tsx +21 -0
  62. package/src/lib/editor/types/misc-types.ts +55 -2
  63. package/src/lib/exports/exportToSvg.tsx +2 -2
  64. package/src/lib/exports/getSvgAsImage.ts +92 -0
  65. package/src/lib/exports/getSvgJsx.tsx +17 -2
  66. package/src/lib/hooks/useLocalStore.ts +1 -1
  67. package/src/lib/utils/browserCanvasMaxSize.ts +65 -0
  68. package/src/lib/utils/sync/TLLocalSyncClient.ts +3 -1
  69. package/src/version.ts +3 -3
@@ -0,0 +1,92 @@
1
+ import { FileHelpers, Image, PngHelpers, sleep } from '@tldraw/utils'
2
+ import { tlenv } from '../globals/environment'
3
+ import { clampToBrowserMaxCanvasSize } from '../utils/browserCanvasMaxSize'
4
+ import { debugFlags } from '../utils/debug-flags'
5
+
6
+ /** @public */
7
+ export async function getSvgAsImage(
8
+ svgString: string,
9
+ options: {
10
+ type: 'png' | 'jpeg' | 'webp'
11
+ width: number
12
+ height: number
13
+ quality?: number
14
+ pixelRatio?: number
15
+ }
16
+ ) {
17
+ const { type, width, height, quality = 1, pixelRatio = 2 } = options
18
+
19
+ let [clampedWidth, clampedHeight] = await clampToBrowserMaxCanvasSize(
20
+ width * pixelRatio,
21
+ height * pixelRatio
22
+ )
23
+ clampedWidth = Math.floor(clampedWidth)
24
+ clampedHeight = Math.floor(clampedHeight)
25
+ const effectiveScale = clampedWidth / width
26
+
27
+ // usually we would use `URL.createObjectURL` here, but chrome has a bug where `blob:` URLs of
28
+ // SVGs that use <foreignObject> mark the canvas as tainted, where data: ones do not.
29
+ // https://issues.chromium.org/issues/41054640
30
+ const svgUrl = await FileHelpers.blobToDataUrl(new Blob([svgString], { type: 'image/svg+xml' }))
31
+
32
+ const canvas = await new Promise<HTMLCanvasElement | null>((resolve) => {
33
+ const image = Image()
34
+ image.crossOrigin = 'anonymous'
35
+
36
+ image.onload = async () => {
37
+ // safari will fire `onLoad` before the fonts in the SVG are
38
+ // actually loaded. just waiting around a while is brittle, but
39
+ // there doesn't seem to be any better solution for now :( see
40
+ // https://bugs.webkit.org/show_bug.cgi?id=219770
41
+ if (tlenv.isSafari) {
42
+ await sleep(250)
43
+ }
44
+
45
+ const canvas = document.createElement('canvas') as HTMLCanvasElement
46
+ const ctx = canvas.getContext('2d')!
47
+
48
+ canvas.width = clampedWidth
49
+ canvas.height = clampedHeight
50
+
51
+ ctx.imageSmoothingEnabled = true
52
+ ctx.imageSmoothingQuality = 'high'
53
+ ctx.drawImage(image, 0, 0, clampedWidth, clampedHeight)
54
+
55
+ URL.revokeObjectURL(svgUrl)
56
+
57
+ resolve(canvas)
58
+ }
59
+
60
+ image.onerror = () => {
61
+ resolve(null)
62
+ }
63
+
64
+ image.src = svgUrl
65
+ })
66
+
67
+ if (!canvas) return null
68
+
69
+ const blob = await new Promise<Blob | null>((resolve) =>
70
+ canvas.toBlob(
71
+ (blob) => {
72
+ if (!blob || debugFlags.throwToBlob.get()) {
73
+ resolve(null)
74
+ }
75
+ resolve(blob)
76
+ },
77
+ 'image/' + type,
78
+ quality
79
+ )
80
+ )
81
+
82
+ if (!blob) return null
83
+
84
+ if (type === 'png') {
85
+ const view = new DataView(await blob.arrayBuffer())
86
+ return PngHelpers.setPhysChunk(view, effectiveScale, {
87
+ type: 'image/' + type,
88
+ })
89
+ } else {
90
+ return blob
91
+ }
92
+ }
@@ -41,7 +41,7 @@ export function getSvgJsx(editor: Editor, ids: TLShapeId[], opts: TLImageExportO
41
41
  const {
42
42
  scale = 1,
43
43
  // should we include the background in the export? or is it transparent?
44
- background = false,
44
+ background = editor.getInstanceState().exportBackground,
45
45
  padding = editor.options.defaultSvgPadding,
46
46
  preserveAspectRatio,
47
47
  } = opts
@@ -102,6 +102,7 @@ export function getSvgJsx(editor: Editor, ids: TLShapeId[], opts: TLImageExportO
102
102
  editor={editor}
103
103
  preserveAspectRatio={preserveAspectRatio}
104
104
  scale={scale}
105
+ pixelRatio={opts.pixelRatio ?? null}
105
106
  bbox={bbox}
106
107
  background={background}
107
108
  singleFrameShapeId={singleFrameShapeId}
@@ -121,6 +122,7 @@ function SvgExport({
121
122
  editor,
122
123
  preserveAspectRatio,
123
124
  scale,
125
+ pixelRatio,
124
126
  bbox,
125
127
  background,
126
128
  singleFrameShapeId,
@@ -132,6 +134,7 @@ function SvgExport({
132
134
  editor: Editor
133
135
  preserveAspectRatio?: string
134
136
  scale: number
137
+ pixelRatio: number | null
135
138
  bbox: Box
136
139
  background: boolean
137
140
  singleFrameShapeId: TLShapeId | null
@@ -177,8 +180,20 @@ function SvgExport({
177
180
  isDarkMode,
178
181
  waitUntil,
179
182
  addExportDef,
183
+ scale,
184
+ pixelRatio,
185
+ async resolveAssetUrl(assetId, width) {
186
+ const asset = editor.getAsset(assetId)
187
+ if (!asset || (asset.type !== 'image' && asset.type !== 'video')) return null
188
+
189
+ return await editor.resolveAssetUrl(assetId, {
190
+ screenScale: scale * (width / asset.props.w),
191
+ shouldResolveToOriginal: pixelRatio === null,
192
+ dpr: pixelRatio ?? undefined,
193
+ })
194
+ },
180
195
  }),
181
- [isDarkMode, waitUntil, addExportDef]
196
+ [isDarkMode, waitUntil, addExportDef, scale, pixelRatio, editor]
182
197
  )
183
198
 
184
199
  const didRenderRef = useRef(false)
@@ -37,7 +37,7 @@ export function useLocalStore(
37
37
  const assets: TLAssetStore = {
38
38
  upload: async (asset, file) => {
39
39
  await client.db.storeAsset(asset.id, file)
40
- return asset.id
40
+ return { src: asset.id }
41
41
  },
42
42
  resolve: async (asset) => {
43
43
  if (!asset.props.src) return null
@@ -0,0 +1,65 @@
1
+ import canvasSize from 'canvas-size'
2
+
3
+ /** @internal */
4
+ export interface CanvasMaxSize {
5
+ maxWidth: number
6
+ maxHeight: number
7
+ maxArea: number
8
+ }
9
+
10
+ let maxSizePromise: Promise<CanvasMaxSize> | null = null
11
+
12
+ function getBrowserCanvasMaxSize() {
13
+ if (!maxSizePromise) {
14
+ maxSizePromise = calculateBrowserCanvasMaxSize()
15
+ }
16
+
17
+ return maxSizePromise
18
+ }
19
+
20
+ async function calculateBrowserCanvasMaxSize(): Promise<CanvasMaxSize> {
21
+ const maxWidth = await canvasSize.maxWidth({ usePromise: true })
22
+ const maxHeight = await canvasSize.maxHeight({ usePromise: true })
23
+ const maxArea = await canvasSize.maxArea({ usePromise: true })
24
+ return {
25
+ maxWidth: maxWidth.width,
26
+ maxHeight: maxHeight.height,
27
+ maxArea: maxArea.width * maxArea.height,
28
+ }
29
+ }
30
+
31
+ // https://github.com/jhildenbiddle/canvas-size?tab=readme-ov-file#test-results
32
+ const MAX_SAFE_CANVAS_DIMENSION = 8192
33
+ const MAX_SAFE_CANVAS_AREA = 4096 * 4096
34
+
35
+ /** @internal */
36
+ export async function clampToBrowserMaxCanvasSize(width: number, height: number) {
37
+ if (
38
+ width <= MAX_SAFE_CANVAS_DIMENSION &&
39
+ height <= MAX_SAFE_CANVAS_DIMENSION &&
40
+ width * height <= MAX_SAFE_CANVAS_AREA
41
+ ) {
42
+ return [width, height]
43
+ }
44
+
45
+ const { maxWidth, maxHeight, maxArea } = await getBrowserCanvasMaxSize()
46
+ const aspectRatio = width / height
47
+
48
+ if (width > maxWidth) {
49
+ width = maxWidth
50
+ height = width / aspectRatio
51
+ }
52
+
53
+ if (height > maxHeight) {
54
+ height = maxHeight
55
+ width = height * aspectRatio
56
+ }
57
+
58
+ if (width * height > maxArea) {
59
+ const ratio = Math.sqrt(maxArea / (width * height))
60
+ width *= ratio
61
+ height *= ratio
62
+ }
63
+
64
+ return [width, height]
65
+ }
@@ -196,7 +196,9 @@ export class TLLocalSyncClient {
196
196
  }
197
197
 
198
198
  if (sessionStateSnapshot) {
199
- loadSessionStateSnapshotIntoStore(this.store, sessionStateSnapshot)
199
+ loadSessionStateSnapshotIntoStore(this.store, sessionStateSnapshot, {
200
+ forceOverwrite: true,
201
+ })
200
202
  }
201
203
  }
202
204
 
package/src/version.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  // This file is automatically generated by internal/scripts/refresh-assets.ts.
2
2
  // Do not edit manually. Or do, I'm a comment, not a cop.
3
3
 
4
- export const version = '3.8.0-canary.4569e7d7e9d9'
4
+ export const version = '3.8.0-canary.4703b6039d91'
5
5
  export const publishDates = {
6
6
  major: '2024-09-13T14:36:29.063Z',
7
- minor: '2025-01-08T09:51:01.404Z',
8
- patch: '2025-01-08T09:51:01.404Z',
7
+ minor: '2025-01-28T10:50:55.780Z',
8
+ patch: '2025-01-28T10:50:55.780Z',
9
9
  }