docgen-utils 1.0.31 → 1.0.33

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/bundle.js CHANGED
@@ -71359,26 +71359,40 @@ ${generateStylesCss(styleMap, themeFonts)}
71359
71359
  return null;
71360
71360
  return { x, y, w, h };
71361
71361
  }
71362
- async function getImageSource(src) {
71363
- if (src.startsWith("data:")) {
71364
- return { data: src };
71365
- } else {
71366
- try {
71367
- const dataUrl = await fetchImageAsDataUrl(src);
71368
- return { data: dataUrl };
71369
- } catch (e) {
71370
- const message = e instanceof Error ? e.message : String(e);
71371
- console.warn(`Could not fetch image ${src}: ${message}`);
71372
- return null;
71362
+ async function addElementsToSlide(elements, slide, pres) {
71363
+ const imageUrls = /* @__PURE__ */ new Set();
71364
+ for (const el of elements) {
71365
+ if (el.type === "image" || el.type === "backgroundImage" || el.type === "slideBackgroundImage") {
71366
+ if (el.src && !el.src.startsWith("data:")) {
71367
+ imageUrls.add(el.src);
71368
+ }
71373
71369
  }
71374
71370
  }
71375
- }
71376
- async function addElementsToSlide(elements, slide, pres) {
71371
+ const imageCache = /* @__PURE__ */ new Map();
71372
+ if (imageUrls.size > 0) {
71373
+ const fetchPromises = Array.from(imageUrls).map(async (url) => {
71374
+ try {
71375
+ const dataUrl = await fetchImageAsDataUrl(url);
71376
+ imageCache.set(url, { data: dataUrl });
71377
+ } catch (e) {
71378
+ const message = e instanceof Error ? e.message : String(e);
71379
+ console.warn(`Could not fetch image ${url}: ${message}`);
71380
+ imageCache.set(url, null);
71381
+ }
71382
+ });
71383
+ await Promise.all(fetchPromises);
71384
+ }
71385
+ const getCachedImageSource = (src) => {
71386
+ if (src.startsWith("data:")) {
71387
+ return { data: src };
71388
+ }
71389
+ return imageCache.get(src) ?? null;
71390
+ };
71377
71391
  for (const el of elements) {
71378
71392
  if (el.type === "slideBackgroundImage") {
71379
- const imgSrc = await getImageSource(el.src);
71393
+ const imgSrc = getCachedImageSource(el.src);
71380
71394
  if (!imgSrc) {
71381
- console.warn(`Skipping slide background image (CORS failure): ${el.src}`);
71395
+ console.warn(`Skipping slide background image (fetch failed): ${el.src}`);
71382
71396
  continue;
71383
71397
  }
71384
71398
  const imageOptions = {
@@ -71422,9 +71436,9 @@ ${generateStylesCss(styleMap, themeFonts)}
71422
71436
  el.position.h = clipped.h;
71423
71437
  }
71424
71438
  if (el.type === "image") {
71425
- const imgSrc = await getImageSource(el.src);
71439
+ const imgSrc = getCachedImageSource(el.src);
71426
71440
  if (!imgSrc) {
71427
- console.warn(`Skipping image (CORS failure): ${el.src}`);
71441
+ console.warn(`Skipping image (fetch failed): ${el.src}`);
71428
71442
  continue;
71429
71443
  }
71430
71444
  const imageOptions = {
@@ -71455,9 +71469,9 @@ ${generateStylesCss(styleMap, themeFonts)}
71455
71469
  imageOptions.transparency = el.transparency;
71456
71470
  slide.addImage(imageOptions);
71457
71471
  } else if (el.type === "backgroundImage") {
71458
- const imgSrc = await getImageSource(el.src);
71472
+ const imgSrc = getCachedImageSource(el.src);
71459
71473
  if (!imgSrc) {
71460
- console.warn(`Skipping background image (CORS failure): ${el.src}`);
71474
+ console.warn(`Skipping background image (fetch failed): ${el.src}`);
71461
71475
  continue;
71462
71476
  }
71463
71477
  const imageOptions = {
@@ -71872,16 +71886,17 @@ ${generateStylesCss(styleMap, themeFonts)}
71872
71886
  resolve();
71873
71887
  };
71874
71888
  iframe.onload = resolveOnce;
71875
- setTimeout(resolveOnce, 2e3);
71889
+ setTimeout(resolveOnce, 1e3);
71876
71890
  });
71877
71891
  const doc = iframe.contentDocument || iframe.contentWindow.document;
71878
71892
  const iframeWin = iframe.contentWindow;
71893
+ const waitPromises = [];
71879
71894
  if (iframeWin && iframeWin.document.fonts && iframeWin.document.fonts.ready) {
71880
- await Promise.race([
71881
- iframeWin.document.fonts.ready,
71882
- new Promise((r) => setTimeout(r, 3e3))
71883
- // 3s timeout for slow CDN
71884
- ]);
71895
+ waitPromises.push(Promise.race([
71896
+ iframeWin.document.fonts.ready.then(() => {
71897
+ }),
71898
+ new Promise((r) => setTimeout(r, 500))
71899
+ ]));
71885
71900
  }
71886
71901
  const images = doc.querySelectorAll("img");
71887
71902
  if (images.length > 0) {
@@ -71894,12 +71909,13 @@ ${generateStylesCss(styleMap, themeFonts)}
71894
71909
  img.onerror = () => resolve();
71895
71910
  });
71896
71911
  });
71897
- await Promise.race([
71898
- Promise.all(imagePromises),
71899
- new Promise((r) => setTimeout(r, 5e3))
71900
- // 5s timeout for slow images
71901
- ]);
71912
+ waitPromises.push(Promise.race([
71913
+ Promise.all(imagePromises).then(() => {
71914
+ }),
71915
+ new Promise((r) => setTimeout(r, 500))
71916
+ ]));
71902
71917
  }
71918
+ await Promise.all(waitPromises);
71903
71919
  return { iframe, doc };
71904
71920
  }
71905
71921
  async function addSlideFromHtml(source, pres, options = {}) {