@loupekit/sdk 0.4.0 → 0.4.1

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.
@@ -1962,10 +1962,18 @@ var Loupe = (() => {
1962
1962
  }
1963
1963
  return { html, styles };
1964
1964
  }
1965
+ var CAPTURE_TIMEOUT = 6e3;
1966
+ function withTimeout(p, ms) {
1967
+ return Promise.race([p, new Promise((resolve) => setTimeout(() => resolve(void 0), ms))]);
1968
+ }
1965
1969
  async function fontsReady() {
1966
1970
  try {
1967
1971
  const fonts = document.fonts;
1968
- if (fonts?.ready) await fonts.ready;
1972
+ if (!fonts?.ready) return;
1973
+ await Promise.race([
1974
+ fonts.ready,
1975
+ new Promise((resolve) => setTimeout(resolve, 800))
1976
+ ]);
1969
1977
  } catch {
1970
1978
  }
1971
1979
  }
@@ -1978,14 +1986,15 @@ var Loupe = (() => {
1978
1986
  async function captureScreenshot(el2) {
1979
1987
  try {
1980
1988
  await fontsReady();
1981
- return await domToPng(el2, {
1982
- scale: Math.min(window.devicePixelRatio || 1, 2),
1983
- backgroundColor: getComputedStyle(document.body).backgroundColor || "#ffffff",
1984
- // Give cross-origin font/asset fetches time to embed (default is short) so
1985
- // the capture matches the page instead of falling back to system fonts.
1986
- timeout: 3e4,
1987
- filter: captureFilter
1988
- });
1989
+ return await withTimeout(
1990
+ domToPng(el2, {
1991
+ scale: Math.min(window.devicePixelRatio || 1, 2),
1992
+ backgroundColor: getComputedStyle(document.body).backgroundColor || "#ffffff",
1993
+ timeout: CAPTURE_TIMEOUT,
1994
+ filter: captureFilter
1995
+ }),
1996
+ CAPTURE_TIMEOUT + 2e3
1997
+ );
1989
1998
  } catch (err) {
1990
1999
  console.warn("[loupe] screenshot capture failed", err);
1991
2000
  return void 0;
@@ -1997,12 +2006,16 @@ var Loupe = (() => {
1997
2006
  const scale = Math.min(window.devicePixelRatio || 1, 2);
1998
2007
  const container = regionContainer(rect);
1999
2008
  const origin = container.getBoundingClientRect();
2000
- const full = await domToPng(container, {
2001
- scale,
2002
- backgroundColor: getComputedStyle(document.body).backgroundColor || "#ffffff",
2003
- timeout: 3e4,
2004
- filter: captureFilter
2005
- });
2009
+ const full = await withTimeout(
2010
+ domToPng(container, {
2011
+ scale,
2012
+ backgroundColor: getComputedStyle(document.body).backgroundColor || "#ffffff",
2013
+ timeout: CAPTURE_TIMEOUT,
2014
+ filter: captureFilter
2015
+ }),
2016
+ CAPTURE_TIMEOUT + 2e3
2017
+ );
2018
+ if (!full) return void 0;
2006
2019
  const redact = Array.from(document.querySelectorAll("[data-loupe-redact]")).map(
2007
2020
  (n) => n.getBoundingClientRect()
2008
2021
  );
@@ -2432,11 +2445,15 @@ var Loupe = (() => {
2432
2445
  };
2433
2446
  }
2434
2447
  }
2435
- const capture = this.cfg.captureRegion ?? captureRegionScreenshot;
2436
- const screenshot = await capture(vp);
2437
2448
  this.selbox.style.display = "none";
2438
2449
  const region = { x: vp.x + window.scrollX, y: vp.y + window.scrollY, w: vp.w, h: vp.h, rel };
2439
- this.openComposer({ kind: "region", region, element: centerEl, screenshot }, vp.x + vp.w, vp.y);
2450
+ const target = { kind: "region", region, element: centerEl };
2451
+ this.openComposer(target, vp.x + vp.w, vp.y);
2452
+ const capture = this.cfg.captureRegion ?? captureRegionScreenshot;
2453
+ this.pendingShot = capture(vp);
2454
+ void this.pendingShot.then((shot) => {
2455
+ if (shot && this.pending === target) target.screenshot = shot;
2456
+ }).catch(() => void 0);
2440
2457
  }
2441
2458
  /** elementFromPoint, ignoring our own UI. */
2442
2459
  pick(x, y) {
@@ -2485,8 +2502,7 @@ var Loupe = (() => {
2485
2502
  const chk = el("label", "chk");
2486
2503
  const box = document.createElement("input");
2487
2504
  box.type = "checkbox";
2488
- box.checked = target.kind === "region" ? !!target.screenshot : true;
2489
- if (target.kind === "region" && !target.screenshot) box.disabled = true;
2505
+ box.checked = true;
2490
2506
  chk.append(box, document.createTextNode("Attach screenshot"));
2491
2507
  const btns = el("div", "btns");
2492
2508
  const cancel = el("button", "ghost", "Cancel");
@@ -2509,6 +2525,7 @@ var Loupe = (() => {
2509
2525
  closeComposer() {
2510
2526
  this.composer.style.display = "none";
2511
2527
  this.pending = null;
2528
+ this.pendingShot = void 0;
2512
2529
  }
2513
2530
  async submit(target, body, withShot) {
2514
2531
  if (!body) return;
@@ -2529,7 +2546,7 @@ var Loupe = (() => {
2529
2546
  offset = this.targetOffset;
2530
2547
  anchoredEl = target.element;
2531
2548
  } else {
2532
- screenshot = withShot ? target.screenshot : void 0;
2549
+ screenshot = withShot ? target.screenshot ?? await this.pendingShot : void 0;
2533
2550
  region = target.region;
2534
2551
  offset = { x: 0, y: 0 };
2535
2552
  if (target.element) {