@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.
Files changed (37) hide show
  1. package/dist-cjs/index.d.ts +8 -4
  2. package/dist-cjs/index.js +1 -1
  3. package/dist-cjs/lib/PerformanceTracker.js +1 -1
  4. package/dist-cjs/lib/PerformanceTracker.js.map +1 -1
  5. package/dist-cjs/lib/bind.js.map +1 -1
  6. package/dist-cjs/lib/debounce.js.map +2 -2
  7. package/dist-cjs/lib/media/media.js +18 -10
  8. package/dist-cjs/lib/media/media.js.map +2 -2
  9. package/dist-cjs/lib/network.js.map +1 -1
  10. package/dist-cjs/lib/retry.js.map +2 -2
  11. package/dist-cjs/lib/storage.js.map +1 -1
  12. package/dist-cjs/lib/timers.js.map +1 -1
  13. package/dist-cjs/lib/version.js.map +1 -1
  14. package/dist-esm/index.d.mts +8 -4
  15. package/dist-esm/index.mjs +1 -1
  16. package/dist-esm/lib/PerformanceTracker.mjs +1 -1
  17. package/dist-esm/lib/PerformanceTracker.mjs.map +1 -1
  18. package/dist-esm/lib/bind.mjs.map +1 -1
  19. package/dist-esm/lib/debounce.mjs.map +2 -2
  20. package/dist-esm/lib/media/media.mjs +18 -10
  21. package/dist-esm/lib/media/media.mjs.map +2 -2
  22. package/dist-esm/lib/network.mjs.map +1 -1
  23. package/dist-esm/lib/retry.mjs.map +2 -2
  24. package/dist-esm/lib/storage.mjs.map +1 -1
  25. package/dist-esm/lib/timers.mjs.map +1 -1
  26. package/dist-esm/lib/version.mjs.map +1 -1
  27. package/package.json +2 -2
  28. package/src/lib/PerformanceTracker.ts +1 -1
  29. package/src/lib/bind.ts +1 -1
  30. package/src/lib/debounce.ts +1 -0
  31. package/src/lib/media/media.ts +24 -10
  32. package/src/lib/network.ts +2 -2
  33. package/src/lib/retry.ts +1 -0
  34. package/src/lib/storage.test.ts +1 -1
  35. package/src/lib/storage.tsx +1 -1
  36. package/src/lib/timers.ts +1 -1
  37. package/src/lib/version.ts +1 -1
@@ -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
- const video = document.createElement('video')
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
- const canvas = document.createElement('canvas')
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
- document.body.appendChild(img)
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
- document.body.removeChild(img)
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(blob: Blob): Promise<{ w: number; h: number; pixelRatio: number }> {
313
- const { w, h } = await MediaHelpers.usingObjectURL(blob, MediaHelpers.getImageAndDimensions)
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') {
@@ -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
@@ -72,5 +72,6 @@ export async function retry<T>(
72
72
  await sleep(waitDuration)
73
73
  }
74
74
  }
75
+ // eslint-disable-next-line no-throw-literal
75
76
  throw error
76
77
  }
@@ -1,4 +1,4 @@
1
- /* eslint-disable no-restricted-syntax */
1
+ /* eslint-disable tldraw/no-direct-storage */
2
2
 
3
3
  import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
4
4
  import {
@@ -1,4 +1,4 @@
1
- /* eslint-disable no-restricted-syntax */
1
+ /* eslint-disable tldraw/no-direct-storage */
2
2
 
3
3
  /**
4
4
  * Get a value from local storage.
package/src/lib/timers.ts CHANGED
@@ -1,4 +1,4 @@
1
- /* eslint-disable no-restricted-properties */
1
+ /* eslint-disable tldraw/no-restricted-properties */
2
2
 
3
3
  /**
4
4
  * A utility class for managing timeouts, intervals, and animation frames with context-based organization and automatic cleanup.
@@ -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
  }