@dooc-dev/dooc 0.0.19 → 0.0.20

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.
@@ -6,18 +6,16 @@ export const deferredFigureHtml = (fig, cls = "fig-frame", label) => {
6
6
  if (cls.includes("figpop-frame"))
7
7
  return escapeHtml(fig.title ?? fig.file);
8
8
  const url = `/api/figure-preview?file=${encodeURIComponent(fig.file)}${label === undefined ? "" : `&label=${encodeURIComponent(label)}`}`;
9
- return `<div class="figure-placeholder" data-figure-preview="${escapeHtml(url)}"><span>${escapeHtml(label || fig.title || fig.file)}</span><button type="button" data-i18n="Load preview">Load preview</button><span role="status"></span></div>`;
9
+ return `<div class="figure-placeholder" data-figure-preview="${escapeHtml(url)}"><span>${escapeHtml(label || fig.title || fig.file)}</span><button type="button" data-i18n="Retry" hidden>Retry</button><span role="status"></span></div>`;
10
10
  };
11
- /** Delegated so previews inserted by navigation or HMR use the same loading state. */
11
+ /** Load visible figures automatically, including previews inserted by navigation or HMR. */
12
12
  export function mountFigurePreviews(request = fetch) {
13
- document.addEventListener("click", async (event) => {
14
- const button = event.target instanceof Element
15
- ? event.target.closest("[data-figure-preview] > button")
16
- : null;
17
- const host = button?.parentElement;
18
- const url = host?.dataset.figurePreview;
19
- const status = host?.querySelector('[role="status"]');
20
- if (!button || !host || !url || !status)
13
+ const root = document.querySelector("main") ?? document.body;
14
+ const load = async (host) => {
15
+ const button = host.querySelector(":scope > button");
16
+ const url = host.dataset.figurePreview;
17
+ const status = host.querySelector('[role="status"]');
18
+ if (!button || button.disabled || !url || !status)
21
19
  return;
22
20
  const translate = (key) => window.__docsTranslate?.(key) ?? key;
23
21
  button.disabled = true;
@@ -43,12 +41,42 @@ export function mountFigurePreviews(request = fetch) {
43
41
  if (!host.isConnected)
44
42
  return;
45
43
  status.textContent = error instanceof Error ? error.message : String(error);
44
+ button.hidden = false;
46
45
  button.textContent = translate("Retry");
47
- button.dataset.i18n = "Retry";
48
46
  }
49
47
  finally {
50
48
  button.disabled = false;
51
49
  host.removeAttribute("aria-busy");
52
50
  }
51
+ };
52
+ document.addEventListener("click", (event) => {
53
+ const button = event.target instanceof Element
54
+ ? event.target.closest("[data-figure-preview] > button")
55
+ : null;
56
+ if (button?.parentElement)
57
+ void load(button.parentElement);
53
58
  });
59
+ const watched = new Set();
60
+ const visibility = new IntersectionObserver((entries) => {
61
+ for (const entry of entries) {
62
+ if (!entry.isIntersecting || !root.contains(entry.target))
63
+ continue;
64
+ visibility.unobserve(entry.target);
65
+ void load(entry.target);
66
+ }
67
+ }, { rootMargin: "200px" });
68
+ const watch = () => {
69
+ for (const host of watched)
70
+ if (!root.contains(host) || !host.dataset.figurePreview) {
71
+ visibility.unobserve(host);
72
+ watched.delete(host);
73
+ }
74
+ for (const host of root.querySelectorAll("[data-figure-preview]"))
75
+ if (!watched.has(host)) {
76
+ watched.add(host);
77
+ visibility.observe(host);
78
+ }
79
+ };
80
+ new MutationObserver(watch).observe(root, { childList: true, subtree: true });
81
+ watch();
54
82
  }
@@ -405,7 +405,6 @@
405
405
  "Today": "Today",
406
406
  "Clear": "Clear",
407
407
  "Enter a valid date (YYYY-MM-DD).": "Enter a valid date (YYYY-MM-DD).",
408
- "Load preview": "Load preview",
409
408
  "Preview unavailable": "Preview unavailable",
410
409
  "History": "History",
411
410
  "Snapshot message": "Snapshot message",
@@ -405,7 +405,6 @@
405
405
  "Today": "今日",
406
406
  "Clear": "クリア",
407
407
  "Enter a valid date (YYYY-MM-DD).": "有効な日付を入力してください(YYYY-MM-DD)。",
408
- "Load preview": "プレビューを読み込む",
409
408
  "Preview unavailable": "プレビューを読み込めません",
410
409
  "History": "履歴",
411
410
  "Snapshot message": "スナップショットの説明",
@@ -405,7 +405,6 @@
405
405
  "Today": "今天",
406
406
  "Clear": "清除",
407
407
  "Enter a valid date (YYYY-MM-DD).": "请输入有效日期(YYYY-MM-DD)。",
408
- "Load preview": "加载预览",
409
408
  "Preview unavailable": "无法加载预览",
410
409
  "History": "历史",
411
410
  "Snapshot message": "快照说明",