@tldraw/editor 3.8.0-canary.c83a0189a824 → 3.8.0-canary.c860b352eedb
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 +108 -12
- package/dist-cjs/index.js +5 -1
- package/dist-cjs/index.js.map +2 -2
- package/dist-cjs/lib/components/default-components/DefaultCanvas.js +1 -4
- package/dist-cjs/lib/components/default-components/DefaultCanvas.js.map +2 -2
- package/dist-cjs/lib/config/createTLStore.js +4 -2
- package/dist-cjs/lib/config/createTLStore.js.map +2 -2
- package/dist-cjs/lib/editor/Editor.js +70 -6
- package/dist-cjs/lib/editor/Editor.js.map +2 -2
- package/dist-cjs/lib/editor/types/SvgExportContext.js.map +2 -2
- package/dist-cjs/lib/editor/types/misc-types.js.map +1 -1
- package/dist-cjs/lib/exports/exportToSvg.js.map +2 -2
- package/dist-cjs/lib/exports/getSvgAsImage.js +83 -0
- package/dist-cjs/lib/exports/getSvgAsImage.js.map +7 -0
- package/dist-cjs/lib/exports/getSvgJsx.js +16 -3
- package/dist-cjs/lib/exports/getSvgJsx.js.map +2 -2
- package/dist-cjs/lib/hooks/useLocalStore.js +1 -1
- package/dist-cjs/lib/hooks/useLocalStore.js.map +2 -2
- package/dist-cjs/lib/utils/browserCanvasMaxSize.js +75 -0
- package/dist-cjs/lib/utils/browserCanvasMaxSize.js.map +7 -0
- package/dist-cjs/lib/utils/sync/TLLocalSyncClient.js +3 -1
- 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 +108 -12
- package/dist-esm/index.mjs +5 -1
- package/dist-esm/index.mjs.map +2 -2
- package/dist-esm/lib/components/default-components/DefaultCanvas.mjs +1 -4
- package/dist-esm/lib/components/default-components/DefaultCanvas.mjs.map +2 -2
- package/dist-esm/lib/config/createTLStore.mjs +4 -2
- package/dist-esm/lib/config/createTLStore.mjs.map +2 -2
- package/dist-esm/lib/editor/Editor.mjs +70 -6
- package/dist-esm/lib/editor/Editor.mjs.map +2 -2
- package/dist-esm/lib/editor/types/SvgExportContext.mjs.map +2 -2
- package/dist-esm/lib/exports/exportToSvg.mjs.map +2 -2
- package/dist-esm/lib/exports/getSvgAsImage.mjs +63 -0
- package/dist-esm/lib/exports/getSvgAsImage.mjs.map +7 -0
- package/dist-esm/lib/exports/getSvgJsx.mjs +16 -3
- package/dist-esm/lib/exports/getSvgJsx.mjs.map +2 -2
- package/dist-esm/lib/hooks/useLocalStore.mjs +1 -1
- package/dist-esm/lib/hooks/useLocalStore.mjs.map +2 -2
- package/dist-esm/lib/utils/browserCanvasMaxSize.mjs +45 -0
- package/dist-esm/lib/utils/browserCanvasMaxSize.mjs.map +7 -0
- package/dist-esm/lib/utils/sync/TLLocalSyncClient.mjs +3 -1
- 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 +9 -7
- package/src/index.ts +4 -0
- package/src/lib/components/default-components/DefaultCanvas.tsx +1 -4
- package/src/lib/config/createTLStore.ts +4 -2
- package/src/lib/editor/Editor.ts +86 -23
- package/src/lib/editor/types/SvgExportContext.tsx +21 -0
- package/src/lib/editor/types/misc-types.ts +55 -2
- package/src/lib/exports/exportToSvg.tsx +2 -2
- package/src/lib/exports/getSvgAsImage.ts +92 -0
- package/src/lib/exports/getSvgJsx.tsx +17 -2
- package/src/lib/hooks/useLocalStore.ts +1 -1
- package/src/lib/utils/browserCanvasMaxSize.ts +65 -0
- package/src/lib/utils/sync/TLLocalSyncClient.ts +3 -1
- package/src/version.ts +3 -3
|
@@ -61,7 +61,9 @@ const defaultAssetResolve: NonNullable<TLAssetStore['resolve']> = (asset) => ass
|
|
|
61
61
|
|
|
62
62
|
/** @public */
|
|
63
63
|
export const inlineBase64AssetStore: TLAssetStore = {
|
|
64
|
-
upload: (_, file) =>
|
|
64
|
+
upload: async (_, file) => {
|
|
65
|
+
return { src: await FileHelpers.blobToDataUrl(file) }
|
|
66
|
+
},
|
|
65
67
|
}
|
|
66
68
|
|
|
67
69
|
/**
|
|
@@ -128,7 +130,7 @@ export function createTLStore({
|
|
|
128
130
|
|
|
129
131
|
if (rest.snapshot) {
|
|
130
132
|
if (initialData) throw new Error('Cannot provide both initialData and snapshot')
|
|
131
|
-
loadSnapshot(store, rest.snapshot)
|
|
133
|
+
loadSnapshot(store, rest.snapshot, { forceOverwriteSessionState: true })
|
|
132
134
|
}
|
|
133
135
|
|
|
134
136
|
return store
|
package/src/lib/editor/Editor.ts
CHANGED
|
@@ -104,6 +104,7 @@ import {
|
|
|
104
104
|
ZOOM_TO_FIT_PADDING,
|
|
105
105
|
} from '../constants'
|
|
106
106
|
import { exportToSvg } from '../exports/exportToSvg'
|
|
107
|
+
import { getSvgAsImage } from '../exports/getSvgAsImage'
|
|
107
108
|
import { tlenv } from '../globals/environment'
|
|
108
109
|
import { tlmenus } from '../globals/menus'
|
|
109
110
|
import { tltime } from '../globals/time'
|
|
@@ -162,6 +163,7 @@ import {
|
|
|
162
163
|
TLCameraMoveOptions,
|
|
163
164
|
TLCameraOptions,
|
|
164
165
|
TLImageExportOptions,
|
|
166
|
+
TLSvgExportOptions,
|
|
165
167
|
} from './types/misc-types'
|
|
166
168
|
import { TLResizeHandle } from './types/selection-types'
|
|
167
169
|
|
|
@@ -1218,18 +1220,7 @@ export class Editor extends EventEmitter<TLEventMap> {
|
|
|
1218
1220
|
}
|
|
1219
1221
|
|
|
1220
1222
|
/** @internal */
|
|
1221
|
-
createErrorAnnotations(
|
|
1222
|
-
origin: string,
|
|
1223
|
-
willCrashApp: boolean | 'unknown'
|
|
1224
|
-
): {
|
|
1225
|
-
tags: { origin: string; willCrashApp: boolean | 'unknown' }
|
|
1226
|
-
extras: {
|
|
1227
|
-
activeStateNode?: string
|
|
1228
|
-
selectedShapes?: TLUnknownShape[]
|
|
1229
|
-
editingShape?: TLUnknownShape
|
|
1230
|
-
inputs?: Record<string, unknown>
|
|
1231
|
-
}
|
|
1232
|
-
} {
|
|
1223
|
+
createErrorAnnotations(origin: string, willCrashApp: boolean | 'unknown') {
|
|
1233
1224
|
try {
|
|
1234
1225
|
const editingShapeId = this.getEditingShapeId()
|
|
1235
1226
|
return {
|
|
@@ -1239,9 +1230,20 @@ export class Editor extends EventEmitter<TLEventMap> {
|
|
|
1239
1230
|
},
|
|
1240
1231
|
extras: {
|
|
1241
1232
|
activeStateNode: this.root.getPath(),
|
|
1242
|
-
selectedShapes: this.getSelectedShapes()
|
|
1233
|
+
selectedShapes: this.getSelectedShapes().map((s) => {
|
|
1234
|
+
const { props, ...rest } = s
|
|
1235
|
+
const { text: _text, richText: _richText, ...restProps } = props as any
|
|
1236
|
+
return {
|
|
1237
|
+
...rest,
|
|
1238
|
+
props: restProps,
|
|
1239
|
+
}
|
|
1240
|
+
}),
|
|
1241
|
+
selectionCount: this.getSelectedShapes().length,
|
|
1243
1242
|
editingShape: editingShapeId ? this.getShape(editingShapeId) : undefined,
|
|
1244
1243
|
inputs: this.inputs,
|
|
1244
|
+
pageState: this.getCurrentPageState(),
|
|
1245
|
+
instanceState: this.getInstanceState(),
|
|
1246
|
+
collaboratorCount: this.getCollaboratorsOnCurrentPage().length,
|
|
1245
1247
|
},
|
|
1246
1248
|
}
|
|
1247
1249
|
} catch {
|
|
@@ -4145,20 +4147,24 @@ export class Editor extends EventEmitter<TLEventMap> {
|
|
|
4145
4147
|
context: {
|
|
4146
4148
|
screenScale?: number
|
|
4147
4149
|
shouldResolveToOriginal?: boolean
|
|
4150
|
+
dpr?: number
|
|
4148
4151
|
}
|
|
4149
4152
|
): Promise<string | null> {
|
|
4150
4153
|
if (!assetId) return null
|
|
4151
4154
|
const asset = this.getAsset(assetId)
|
|
4152
4155
|
if (!asset) return null
|
|
4153
4156
|
|
|
4154
|
-
const {
|
|
4157
|
+
const {
|
|
4158
|
+
screenScale = 1,
|
|
4159
|
+
shouldResolveToOriginal = false,
|
|
4160
|
+
dpr = this.getInstanceState().devicePixelRatio,
|
|
4161
|
+
} = context
|
|
4155
4162
|
|
|
4156
4163
|
// We only look at the zoom level at powers of 2.
|
|
4157
4164
|
const zoomStepFunction = (zoom: number) => Math.pow(2, Math.ceil(Math.log2(zoom)))
|
|
4158
|
-
const steppedScreenScale =
|
|
4165
|
+
const steppedScreenScale = zoomStepFunction(screenScale)
|
|
4159
4166
|
const networkEffectiveType: string | null =
|
|
4160
4167
|
'connection' in navigator ? (navigator as any).connection.effectiveType : null
|
|
4161
|
-
const dpr = this.getInstanceState().devicePixelRatio
|
|
4162
4168
|
|
|
4163
4169
|
return await this.store.props.assets.resolve(asset, {
|
|
4164
4170
|
screenScale: screenScale || 1,
|
|
@@ -4172,7 +4178,11 @@ export class Editor extends EventEmitter<TLEventMap> {
|
|
|
4172
4178
|
* Upload an asset to the store's asset service, returning a URL that can be used to resolve the
|
|
4173
4179
|
* asset.
|
|
4174
4180
|
*/
|
|
4175
|
-
async uploadAsset(
|
|
4181
|
+
async uploadAsset(
|
|
4182
|
+
asset: TLAsset,
|
|
4183
|
+
file: File,
|
|
4184
|
+
abortSignal?: AbortSignal
|
|
4185
|
+
): Promise<{ src: string; meta?: JsonObject }> {
|
|
4176
4186
|
return await this.store.props.assets.upload(asset, file, abortSignal)
|
|
4177
4187
|
}
|
|
4178
4188
|
|
|
@@ -8564,11 +8574,13 @@ export class Editor extends EventEmitter<TLEventMap> {
|
|
|
8564
8574
|
*
|
|
8565
8575
|
* @public
|
|
8566
8576
|
*/
|
|
8567
|
-
async getSvgElement(shapes: TLShapeId[] | TLShape[], opts:
|
|
8577
|
+
async getSvgElement(shapes: TLShapeId[] | TLShape[], opts: TLSvgExportOptions = {}) {
|
|
8568
8578
|
const ids =
|
|
8569
|
-
|
|
8570
|
-
? (
|
|
8571
|
-
:
|
|
8579
|
+
shapes.length === 0
|
|
8580
|
+
? this.getCurrentPageShapeIdsSorted()
|
|
8581
|
+
: typeof shapes[0] === 'string'
|
|
8582
|
+
? (shapes as TLShapeId[])
|
|
8583
|
+
: (shapes as TLShape[]).map((s) => s.id)
|
|
8572
8584
|
|
|
8573
8585
|
if (ids.length === 0) return undefined
|
|
8574
8586
|
|
|
@@ -8585,7 +8597,7 @@ export class Editor extends EventEmitter<TLEventMap> {
|
|
|
8585
8597
|
*
|
|
8586
8598
|
* @public
|
|
8587
8599
|
*/
|
|
8588
|
-
async getSvgString(shapes: TLShapeId[] | TLShape[], opts:
|
|
8600
|
+
async getSvgString(shapes: TLShapeId[] | TLShape[], opts: TLSvgExportOptions = {}) {
|
|
8589
8601
|
const result = await this.getSvgElement(shapes, opts)
|
|
8590
8602
|
if (!result) return undefined
|
|
8591
8603
|
|
|
@@ -8598,12 +8610,63 @@ export class Editor extends EventEmitter<TLEventMap> {
|
|
|
8598
8610
|
}
|
|
8599
8611
|
|
|
8600
8612
|
/** @deprecated Use {@link Editor.getSvgString} or {@link Editor.getSvgElement} instead. */
|
|
8601
|
-
async getSvg(shapes: TLShapeId[] | TLShape[], opts:
|
|
8613
|
+
async getSvg(shapes: TLShapeId[] | TLShape[], opts: TLSvgExportOptions = {}) {
|
|
8602
8614
|
const result = await this.getSvgElement(shapes, opts)
|
|
8603
8615
|
if (!result) return undefined
|
|
8604
8616
|
return result.svg
|
|
8605
8617
|
}
|
|
8606
8618
|
|
|
8619
|
+
/**
|
|
8620
|
+
* Get an exported image of the given shapes.
|
|
8621
|
+
*
|
|
8622
|
+
* @param shapes - The shapes (or shape ids) to export.
|
|
8623
|
+
* @param opts - Options for the export.
|
|
8624
|
+
*
|
|
8625
|
+
* @returns A blob of the image.
|
|
8626
|
+
* @public
|
|
8627
|
+
*/
|
|
8628
|
+
async toImage(shapes: TLShapeId[] | TLShape[], opts: TLImageExportOptions = {}) {
|
|
8629
|
+
const withDefaults = {
|
|
8630
|
+
format: 'png',
|
|
8631
|
+
scale: 1,
|
|
8632
|
+
pixelRatio: opts.format === 'svg' ? undefined : 2,
|
|
8633
|
+
...opts,
|
|
8634
|
+
} satisfies TLImageExportOptions
|
|
8635
|
+
const result = await this.getSvgString(shapes, withDefaults)
|
|
8636
|
+
if (!result) throw new Error('Could not create SVG')
|
|
8637
|
+
|
|
8638
|
+
switch (withDefaults.format) {
|
|
8639
|
+
case 'svg':
|
|
8640
|
+
return {
|
|
8641
|
+
blob: new Blob([result.svg], { type: 'text/plain' }),
|
|
8642
|
+
width: result.width,
|
|
8643
|
+
height: result.height,
|
|
8644
|
+
}
|
|
8645
|
+
case 'jpeg':
|
|
8646
|
+
case 'png':
|
|
8647
|
+
case 'webp': {
|
|
8648
|
+
const blob = await getSvgAsImage(result.svg, {
|
|
8649
|
+
type: withDefaults.format,
|
|
8650
|
+
quality: withDefaults.quality,
|
|
8651
|
+
pixelRatio: withDefaults.pixelRatio,
|
|
8652
|
+
width: result.width,
|
|
8653
|
+
height: result.height,
|
|
8654
|
+
})
|
|
8655
|
+
if (!blob) {
|
|
8656
|
+
throw new Error('Could not construct image.')
|
|
8657
|
+
}
|
|
8658
|
+
return {
|
|
8659
|
+
blob,
|
|
8660
|
+
width: result.width,
|
|
8661
|
+
height: result.height,
|
|
8662
|
+
}
|
|
8663
|
+
}
|
|
8664
|
+
default: {
|
|
8665
|
+
exhaustiveSwitchError(withDefaults.format)
|
|
8666
|
+
}
|
|
8667
|
+
}
|
|
8668
|
+
}
|
|
8669
|
+
|
|
8607
8670
|
/* --------------------- Events --------------------- */
|
|
8608
8671
|
|
|
8609
8672
|
/**
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { TLAssetId } from '@tldraw/tlschema'
|
|
1
2
|
import { promiseWithResolve } from '@tldraw/utils'
|
|
2
3
|
import { ReactElement, ReactNode, createContext, useContext, useEffect, useState } from 'react'
|
|
3
4
|
import { ContainerProvider } from '../../hooks/useContainer'
|
|
@@ -29,10 +30,30 @@ export interface SvgExportContext {
|
|
|
29
30
|
*/
|
|
30
31
|
waitUntil(promise: Promise<void>): void
|
|
31
32
|
|
|
33
|
+
/**
|
|
34
|
+
* Resolve an asset URL in the context of this export. Supply the asset ID and the width in
|
|
35
|
+
* shape-pixels it'll be displayed at, and this will resolve the asset according to the export
|
|
36
|
+
* options.
|
|
37
|
+
*/
|
|
38
|
+
resolveAssetUrl(assetId: TLAssetId, width: number): Promise<string | null>
|
|
39
|
+
|
|
32
40
|
/**
|
|
33
41
|
* Whether the export should be in dark mode.
|
|
34
42
|
*/
|
|
35
43
|
readonly isDarkMode: boolean
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The scale of the export - how much CSS pixels will be scaled up/down by.
|
|
47
|
+
*/
|
|
48
|
+
readonly scale: number
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Use this value to optionally downscale images in the export. If we're exporting directly to
|
|
52
|
+
* an SVG, this will usually be null, and you shouldn't downscale images. If the export is to a
|
|
53
|
+
* raster format like PNG, this will be the number of raster pixels in the resulting bitmap per
|
|
54
|
+
* CSS pixel in the resulting SVG.
|
|
55
|
+
*/
|
|
56
|
+
readonly pixelRatio: number | null
|
|
36
57
|
}
|
|
37
58
|
|
|
38
59
|
const Context = createContext<SvgExportContext | null>(null)
|
|
@@ -8,17 +8,70 @@ export type RequiredKeys<T, K extends keyof T> = Required<Pick<T, K>> & Omit<T,
|
|
|
8
8
|
export type OptionalKeys<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>
|
|
9
9
|
|
|
10
10
|
/** @public */
|
|
11
|
-
export
|
|
11
|
+
export type TLExportType = 'svg' | 'png' | 'jpeg' | 'webp'
|
|
12
|
+
|
|
13
|
+
/** @public */
|
|
14
|
+
export interface TLSvgExportOptions {
|
|
15
|
+
/**
|
|
16
|
+
* The bounding box, in page coordinates, of the area being exported.
|
|
17
|
+
*/
|
|
12
18
|
bounds?: Box
|
|
19
|
+
/**
|
|
20
|
+
* The logical scale of the export. This scales the resulting size of the SVG being generated.
|
|
21
|
+
*/
|
|
13
22
|
scale?: number
|
|
14
|
-
|
|
23
|
+
/**
|
|
24
|
+
* When exporting an SVG, the expected pixel ratio of the export will be passed in to
|
|
25
|
+
* {@link @tldraw/tlschema#TLAssetStore.resolve} as the `dpr` property, so that assets can be
|
|
26
|
+
* downscaled to the appropriate resolution.
|
|
27
|
+
*
|
|
28
|
+
* When exporting to a bitmap image format, the size of the resulting image will be multiplied
|
|
29
|
+
* by this number.
|
|
30
|
+
*
|
|
31
|
+
* For SVG exports, this defaults to undefined - which means we'll request original-quality
|
|
32
|
+
* assets. For bitmap exports, this defaults to 2.
|
|
33
|
+
*/
|
|
15
34
|
pixelRatio?: number
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Should the background color be included in the export? If false, the generated image will be
|
|
38
|
+
* transparent (if exporting to a format that supports transparency).
|
|
39
|
+
*/
|
|
16
40
|
background?: boolean
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* How much padding to include around the bounds of exports? Defaults to 32px.
|
|
44
|
+
*/
|
|
17
45
|
padding?: number
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Should the export be rendered in dark mode (true) or light mode (false)? Defaults to the
|
|
49
|
+
* current instance's dark mode setting.
|
|
50
|
+
*/
|
|
18
51
|
darkMode?: boolean
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The
|
|
55
|
+
* {@link https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/preserveAspectRatio | `preserveAspectRatio` }
|
|
56
|
+
* attribute of the SVG element.
|
|
57
|
+
*/
|
|
19
58
|
preserveAspectRatio?: React.SVGAttributes<SVGSVGElement>['preserveAspectRatio']
|
|
20
59
|
}
|
|
21
60
|
|
|
61
|
+
/** @public */
|
|
62
|
+
export interface TLImageExportOptions extends TLSvgExportOptions {
|
|
63
|
+
/**
|
|
64
|
+
* If the export is being converted to a lossy bitmap format (e.g. jpeg), this is the quality of
|
|
65
|
+
* the export. This is a number between 0 and 1.
|
|
66
|
+
*/
|
|
67
|
+
quality?: number
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The format to export as. Defaults to 'png'.
|
|
71
|
+
*/
|
|
72
|
+
format?: TLExportType
|
|
73
|
+
}
|
|
74
|
+
|
|
22
75
|
/**
|
|
23
76
|
* @public
|
|
24
77
|
* @deprecated use {@link TLImageExportOptions} instead
|
|
@@ -3,7 +3,7 @@ import { assert } from '@tldraw/utils'
|
|
|
3
3
|
import { flushSync } from 'react-dom'
|
|
4
4
|
import { createRoot } from 'react-dom/client'
|
|
5
5
|
import { Editor } from '../editor/Editor'
|
|
6
|
-
import {
|
|
6
|
+
import { TLSvgExportOptions } from '../editor/types/misc-types'
|
|
7
7
|
import { StyleEmbedder } from './StyleEmbedder'
|
|
8
8
|
import { embedMedia } from './embedMedia'
|
|
9
9
|
import { getSvgJsx } from './getSvgJsx'
|
|
@@ -13,7 +13,7 @@ let idCounter = 1
|
|
|
13
13
|
export async function exportToSvg(
|
|
14
14
|
editor: Editor,
|
|
15
15
|
shapeIds: TLShapeId[],
|
|
16
|
-
opts:
|
|
16
|
+
opts: TLSvgExportOptions = {}
|
|
17
17
|
) {
|
|
18
18
|
// when rendering to SVG, we start by creating a JSX representation of the SVG that we can
|
|
19
19
|
// render with react. Hopefully elements will have a `toSvg` method that renders them to SVG,
|
|
@@ -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 =
|
|
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.
|
|
4
|
+
export const version = '3.8.0-canary.c860b352eedb'
|
|
5
5
|
export const publishDates = {
|
|
6
6
|
major: '2024-09-13T14:36:29.063Z',
|
|
7
|
-
minor: '2025-01-
|
|
8
|
-
patch: '2025-01-
|
|
7
|
+
minor: '2025-01-22T13:36:17.024Z',
|
|
8
|
+
patch: '2025-01-22T13:36:17.024Z',
|
|
9
9
|
}
|