docgen-utils 1.0.30 → 1.0.32

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
@@ -71192,24 +71192,38 @@ ${generateStylesCss(styleMap, themeFonts)}
71192
71192
  return null;
71193
71193
  return { x, y, w, h };
71194
71194
  }
71195
- async function getImageSource(src) {
71196
- if (src.startsWith("data:")) {
71197
- return { data: src };
71198
- } else {
71199
- try {
71200
- const dataUrl = await fetchImageAsDataUrl(src);
71201
- return { data: dataUrl };
71202
- } catch (e) {
71203
- const message = e instanceof Error ? e.message : String(e);
71204
- console.warn(`Could not fetch image ${src}: ${message}`);
71205
- return null;
71195
+ async function addElementsToSlide(elements, slide, pres) {
71196
+ const imageUrls = /* @__PURE__ */ new Set();
71197
+ for (const el of elements) {
71198
+ if (el.type === "image" || el.type === "backgroundImage" || el.type === "slideBackgroundImage") {
71199
+ if (el.src && !el.src.startsWith("data:")) {
71200
+ imageUrls.add(el.src);
71201
+ }
71206
71202
  }
71207
71203
  }
71208
- }
71209
- async function addElementsToSlide(elements, slide, pres) {
71204
+ const imageCache = /* @__PURE__ */ new Map();
71205
+ if (imageUrls.size > 0) {
71206
+ const fetchPromises = Array.from(imageUrls).map(async (url) => {
71207
+ try {
71208
+ const dataUrl = await fetchImageAsDataUrl(url);
71209
+ imageCache.set(url, { data: dataUrl });
71210
+ } catch (e) {
71211
+ const message = e instanceof Error ? e.message : String(e);
71212
+ console.warn(`Could not fetch image ${url}: ${message}`);
71213
+ imageCache.set(url, null);
71214
+ }
71215
+ });
71216
+ await Promise.all(fetchPromises);
71217
+ }
71218
+ const getCachedImageSource = (src) => {
71219
+ if (src.startsWith("data:")) {
71220
+ return { data: src };
71221
+ }
71222
+ return imageCache.get(src) ?? null;
71223
+ };
71210
71224
  for (const el of elements) {
71211
71225
  if (el.type === "slideBackgroundImage") {
71212
- const imgSrc = await getImageSource(el.src);
71226
+ const imgSrc = getCachedImageSource(el.src);
71213
71227
  if (!imgSrc) {
71214
71228
  console.warn(`Skipping slide background image (CORS failure): ${el.src}`);
71215
71229
  continue;
@@ -71255,7 +71269,7 @@ ${generateStylesCss(styleMap, themeFonts)}
71255
71269
  el.position.h = clipped.h;
71256
71270
  }
71257
71271
  if (el.type === "image") {
71258
- const imgSrc = await getImageSource(el.src);
71272
+ const imgSrc = getCachedImageSource(el.src);
71259
71273
  if (!imgSrc) {
71260
71274
  console.warn(`Skipping image (CORS failure): ${el.src}`);
71261
71275
  continue;
@@ -71288,7 +71302,7 @@ ${generateStylesCss(styleMap, themeFonts)}
71288
71302
  imageOptions.transparency = el.transparency;
71289
71303
  slide.addImage(imageOptions);
71290
71304
  } else if (el.type === "backgroundImage") {
71291
- const imgSrc = await getImageSource(el.src);
71305
+ const imgSrc = getCachedImageSource(el.src);
71292
71306
  if (!imgSrc) {
71293
71307
  console.warn(`Skipping background image (CORS failure): ${el.src}`);
71294
71308
  continue;
@@ -71705,16 +71719,17 @@ ${generateStylesCss(styleMap, themeFonts)}
71705
71719
  resolve();
71706
71720
  };
71707
71721
  iframe.onload = resolveOnce;
71708
- setTimeout(resolveOnce, 2e3);
71722
+ setTimeout(resolveOnce, 1e3);
71709
71723
  });
71710
71724
  const doc = iframe.contentDocument || iframe.contentWindow.document;
71711
71725
  const iframeWin = iframe.contentWindow;
71726
+ const waitPromises = [];
71712
71727
  if (iframeWin && iframeWin.document.fonts && iframeWin.document.fonts.ready) {
71713
- await Promise.race([
71714
- iframeWin.document.fonts.ready,
71715
- new Promise((r) => setTimeout(r, 3e3))
71716
- // 3s timeout for slow CDN
71717
- ]);
71728
+ waitPromises.push(Promise.race([
71729
+ iframeWin.document.fonts.ready.then(() => {
71730
+ }),
71731
+ new Promise((r) => setTimeout(r, 500))
71732
+ ]));
71718
71733
  }
71719
71734
  const images = doc.querySelectorAll("img");
71720
71735
  if (images.length > 0) {
@@ -71727,12 +71742,13 @@ ${generateStylesCss(styleMap, themeFonts)}
71727
71742
  img.onerror = () => resolve();
71728
71743
  });
71729
71744
  });
71730
- await Promise.race([
71731
- Promise.all(imagePromises),
71732
- new Promise((r) => setTimeout(r, 5e3))
71733
- // 5s timeout for slow images
71734
- ]);
71745
+ waitPromises.push(Promise.race([
71746
+ Promise.all(imagePromises).then(() => {
71747
+ }),
71748
+ new Promise((r) => setTimeout(r, 500))
71749
+ ]));
71735
71750
  }
71751
+ await Promise.all(waitPromises);
71736
71752
  return { iframe, doc };
71737
71753
  }
71738
71754
  async function addSlideFromHtml(source, pres, options = {}) {