@tldraw/utils 4.5.3 → 4.6.0-canary.00a8c03b5687
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 +8 -4
- package/dist-cjs/index.js +1 -1
- package/dist-cjs/lib/PerformanceTracker.js +1 -1
- package/dist-cjs/lib/PerformanceTracker.js.map +1 -1
- package/dist-cjs/lib/bind.js.map +1 -1
- package/dist-cjs/lib/debounce.js.map +2 -2
- package/dist-cjs/lib/media/media.js +18 -10
- package/dist-cjs/lib/media/media.js.map +2 -2
- package/dist-cjs/lib/network.js.map +1 -1
- package/dist-cjs/lib/retry.js.map +2 -2
- package/dist-cjs/lib/storage.js.map +1 -1
- package/dist-cjs/lib/timers.js.map +1 -1
- package/dist-cjs/lib/version.js.map +1 -1
- package/dist-esm/index.d.mts +8 -4
- package/dist-esm/index.mjs +1 -1
- package/dist-esm/lib/PerformanceTracker.mjs +1 -1
- package/dist-esm/lib/PerformanceTracker.mjs.map +1 -1
- package/dist-esm/lib/bind.mjs.map +1 -1
- package/dist-esm/lib/debounce.mjs.map +2 -2
- package/dist-esm/lib/media/media.mjs +18 -10
- package/dist-esm/lib/media/media.mjs.map +2 -2
- package/dist-esm/lib/network.mjs.map +1 -1
- package/dist-esm/lib/retry.mjs.map +2 -2
- package/dist-esm/lib/storage.mjs.map +1 -1
- package/dist-esm/lib/timers.mjs.map +1 -1
- package/dist-esm/lib/version.mjs.map +1 -1
- package/package.json +2 -2
- package/src/lib/PerformanceTracker.ts +1 -1
- package/src/lib/bind.ts +1 -1
- package/src/lib/debounce.ts +1 -0
- package/src/lib/media/media.ts +24 -10
- package/src/lib/network.ts +2 -2
- package/src/lib/retry.ts +1 -0
- package/src/lib/storage.test.ts +1 -1
- package/src/lib/storage.tsx +1 -1
- package/src/lib/timers.ts +1 -1
- package/src/lib/version.ts +1 -1
package/src/lib/media/media.ts
CHANGED
|
@@ -133,6 +133,7 @@ export class MediaHelpers {
|
|
|
133
133
|
* Load a video element from a URL with cross-origin support.
|
|
134
134
|
*
|
|
135
135
|
* @param src - The URL of the video to load
|
|
136
|
+
* @param doc - Optional document to create the video element in
|
|
136
137
|
* @returns Promise that resolves to the loaded HTMLVideoElement
|
|
137
138
|
* @example
|
|
138
139
|
* ```ts
|
|
@@ -141,9 +142,10 @@ export class MediaHelpers {
|
|
|
141
142
|
* ```
|
|
142
143
|
* @public
|
|
143
144
|
*/
|
|
144
|
-
static loadVideo(src: string): Promise<HTMLVideoElement> {
|
|
145
|
+
static loadVideo(src: string, doc?: Document): Promise<HTMLVideoElement> {
|
|
145
146
|
return new Promise((resolve, reject) => {
|
|
146
|
-
|
|
147
|
+
// eslint-disable-next-line no-restricted-globals
|
|
148
|
+
const video = (doc ?? document).createElement('video')
|
|
147
149
|
video.onloadeddata = () => resolve(video)
|
|
148
150
|
video.onerror = (e) => {
|
|
149
151
|
console.error(e)
|
|
@@ -185,7 +187,8 @@ export class MediaHelpers {
|
|
|
185
187
|
}
|
|
186
188
|
|
|
187
189
|
if (video.readyState >= video.HAVE_CURRENT_DATA) {
|
|
188
|
-
|
|
190
|
+
// eslint-disable-next-line no-restricted-globals
|
|
191
|
+
const canvas = (video.ownerDocument ?? document).createElement('canvas')
|
|
189
192
|
canvas.width = video.videoWidth
|
|
190
193
|
canvas.height = video.videoHeight
|
|
191
194
|
const ctx = canvas.getContext('2d')
|
|
@@ -228,6 +231,7 @@ export class MediaHelpers {
|
|
|
228
231
|
* Load an image from a URL and get its dimensions along with the image element.
|
|
229
232
|
*
|
|
230
233
|
* @param src - The URL of the image to load
|
|
234
|
+
* @param doc - Optional document to use for DOM operations (e.g. measuring SVG dimensions)
|
|
231
235
|
* @returns Promise that resolves to an object with width, height, and the image element
|
|
232
236
|
* @example
|
|
233
237
|
* ```ts
|
|
@@ -239,7 +243,8 @@ export class MediaHelpers {
|
|
|
239
243
|
* @public
|
|
240
244
|
*/
|
|
241
245
|
static getImageAndDimensions(
|
|
242
|
-
src: string
|
|
246
|
+
src: string,
|
|
247
|
+
doc?: Document
|
|
243
248
|
): Promise<{ w: number; h: number; image: HTMLImageElement }> {
|
|
244
249
|
return new Promise((resolve, reject) => {
|
|
245
250
|
const img = Image()
|
|
@@ -253,12 +258,14 @@ export class MediaHelpers {
|
|
|
253
258
|
} else {
|
|
254
259
|
// Sigh, Firefox doesn't have naturalWidth or naturalHeight for SVGs. :-/
|
|
255
260
|
// We have to attach to dom and use clientWidth/clientHeight.
|
|
256
|
-
|
|
261
|
+
// eslint-disable-next-line no-restricted-globals
|
|
262
|
+
const body = (doc ?? document).body
|
|
263
|
+
body.appendChild(img)
|
|
257
264
|
dimensions = {
|
|
258
265
|
w: img.clientWidth,
|
|
259
266
|
h: img.clientHeight,
|
|
260
267
|
}
|
|
261
|
-
|
|
268
|
+
body.removeChild(img)
|
|
262
269
|
}
|
|
263
270
|
resolve({ ...dimensions, image: img })
|
|
264
271
|
}
|
|
@@ -280,6 +287,7 @@ export class MediaHelpers {
|
|
|
280
287
|
* Get the size of a video blob
|
|
281
288
|
*
|
|
282
289
|
* @param blob - A Blob containing the video
|
|
290
|
+
* @param doc - Optional document to create elements in
|
|
283
291
|
* @returns Promise that resolves to an object with width and height properties
|
|
284
292
|
* @example
|
|
285
293
|
* ```ts
|
|
@@ -289,9 +297,9 @@ export class MediaHelpers {
|
|
|
289
297
|
* ```
|
|
290
298
|
* @public
|
|
291
299
|
*/
|
|
292
|
-
static async getVideoSize(blob: Blob): Promise<{ w: number; h: number }> {
|
|
300
|
+
static async getVideoSize(blob: Blob, doc?: Document): Promise<{ w: number; h: number }> {
|
|
293
301
|
return MediaHelpers.usingObjectURL(blob, async (url) => {
|
|
294
|
-
const video = await MediaHelpers.loadVideo(url)
|
|
302
|
+
const video = await MediaHelpers.loadVideo(url, doc)
|
|
295
303
|
return { w: video.videoWidth, h: video.videoHeight }
|
|
296
304
|
})
|
|
297
305
|
}
|
|
@@ -300,6 +308,7 @@ export class MediaHelpers {
|
|
|
300
308
|
* Get the size of an image blob
|
|
301
309
|
*
|
|
302
310
|
* @param blob - A Blob containing the image
|
|
311
|
+
* @param doc - Optional document to use for DOM operations
|
|
303
312
|
* @returns Promise that resolves to an object with width and height properties
|
|
304
313
|
* @example
|
|
305
314
|
* ```ts
|
|
@@ -309,8 +318,13 @@ export class MediaHelpers {
|
|
|
309
318
|
* ```
|
|
310
319
|
* @public
|
|
311
320
|
*/
|
|
312
|
-
static async getImageSize(
|
|
313
|
-
|
|
321
|
+
static async getImageSize(
|
|
322
|
+
blob: Blob,
|
|
323
|
+
doc?: Document
|
|
324
|
+
): Promise<{ w: number; h: number; pixelRatio: number }> {
|
|
325
|
+
const { w, h } = await MediaHelpers.usingObjectURL(blob, (url) =>
|
|
326
|
+
MediaHelpers.getImageAndDimensions(url, doc)
|
|
327
|
+
)
|
|
314
328
|
|
|
315
329
|
try {
|
|
316
330
|
if (blob.type === 'image/png') {
|
package/src/lib/network.ts
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* @internal
|
|
8
8
|
*/
|
|
9
9
|
export async function fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response> {
|
|
10
|
-
// eslint-disable-next-line no-restricted-properties
|
|
10
|
+
// eslint-disable-next-line tldraw/no-restricted-properties
|
|
11
11
|
return window.fetch(input, {
|
|
12
12
|
// We want to make sure that the referrer is not sent to other domains.
|
|
13
13
|
referrerPolicy: 'strict-origin-when-cross-origin',
|
|
@@ -25,7 +25,7 @@ export async function fetch(input: RequestInfo | URL, init?: RequestInit): Promi
|
|
|
25
25
|
* @internal
|
|
26
26
|
*/
|
|
27
27
|
export const Image = (width?: number, height?: number) => {
|
|
28
|
-
// eslint-disable-next-line no-restricted-properties
|
|
28
|
+
// eslint-disable-next-line tldraw/no-restricted-properties
|
|
29
29
|
const img = new window.Image(width, height)
|
|
30
30
|
img.referrerPolicy = 'strict-origin-when-cross-origin'
|
|
31
31
|
return img
|
package/src/lib/retry.ts
CHANGED
package/src/lib/storage.test.ts
CHANGED
package/src/lib/storage.tsx
CHANGED
package/src/lib/timers.ts
CHANGED
package/src/lib/version.ts
CHANGED
|
@@ -12,7 +12,7 @@ interface TldrawLibraryVersionInfo {
|
|
|
12
12
|
|
|
13
13
|
const TLDRAW_LIBRARY_VERSION_KEY = '__TLDRAW_LIBRARY_VERSIONS__' as const
|
|
14
14
|
|
|
15
|
-
// eslint-disable-next-line @typescript-eslint/prefer-namespace-keyword, @typescript-eslint/no-namespace
|
|
15
|
+
// eslint-disable-next-line @typescript-eslint/prefer-namespace-keyword, @typescript-eslint/no-namespace, no-shadow-restricted-names
|
|
16
16
|
declare module globalThis {
|
|
17
17
|
export const __TLDRAW_LIBRARY_VERSIONS__: TldrawLibraryVersionInfo
|
|
18
18
|
}
|