@adoptai/genui-components 0.1.67 → 0.1.68

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.
@@ -101,13 +101,74 @@ var init_ThemeContext = __esm({
101
101
  GenUIThemeContext = createContext(DEFAULT_TOKENS);
102
102
  }
103
103
  });
104
+ function resolvePdfWorkerSrc(pdfjs2) {
105
+ const override = typeof window !== "undefined" ? window.__GENUI_PDF_WORKER_SRC__ : void 0;
106
+ const current = pdfjs2.GlobalWorkerOptions.workerSrc;
107
+ const isConfigured = !!current && current !== "pdf.worker.mjs";
108
+ if (override) {
109
+ pdfjs2.GlobalWorkerOptions.workerSrc = override;
110
+ } else if (!isConfigured) {
111
+ pdfjs2.GlobalWorkerOptions.workerSrc = `https://cdn.jsdelivr.net/npm/pdfjs-dist@${pdfjs2.version}/build/pdf.worker.min.mjs`;
112
+ }
113
+ return pdfjs2.GlobalWorkerOptions.workerSrc;
114
+ }
115
+ function probePdfWorker() {
116
+ if (probe) return probe;
117
+ probe = (async () => {
118
+ if (typeof window === "undefined") return false;
119
+ try {
120
+ const { pdfjs: pdfjs2 } = await import('react-pdf');
121
+ resolvePdfWorkerSrc(pdfjs2);
122
+ const task = pdfjs2.getDocument({ data: new TextEncoder().encode(TINY_PDF) });
123
+ const doc = await Promise.race([
124
+ task.promise,
125
+ new Promise(
126
+ (_, reject) => setTimeout(() => reject(new Error("pdf worker probe timed out")), PROBE_TIMEOUT_MS)
127
+ )
128
+ ]);
129
+ await doc.destroy();
130
+ return true;
131
+ } catch (e) {
132
+ probe = null;
133
+ return false;
134
+ }
135
+ })();
136
+ return probe;
137
+ }
138
+ function usePdfWorkerReady(enabled) {
139
+ const [ready, setReady] = useState(null);
140
+ useEffect(() => {
141
+ if (!enabled) return;
142
+ let cancelled = false;
143
+ probePdfWorker().then((ok) => {
144
+ if (!cancelled) setReady(ok);
145
+ });
146
+ return () => {
147
+ cancelled = true;
148
+ };
149
+ }, [enabled]);
150
+ return enabled ? ready : null;
151
+ }
152
+ var TINY_PDF, PROBE_TIMEOUT_MS, probe;
153
+ var init_pdfWorkerProbe = __esm({
154
+ "src/shared/viewers/pdfWorkerProbe.ts"() {
155
+ "use client";
156
+ TINY_PDF = `%PDF-1.1
157
+ 1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj
158
+ 2 0 obj<</Type/Pages/Kids[3 0 R]/Count 1>>endobj
159
+ 3 0 obj<</Type/Page/Parent 2 0 R/MediaBox[0 0 3 3]>>endobj
160
+ trailer<</Root 1 0 R>>`;
161
+ PROBE_TIMEOUT_MS = 5e3;
162
+ probe = null;
163
+ }
164
+ });
104
165
 
105
166
  // src/composites/document-preview/PdfViewer.tsx
106
167
  var PdfViewer_exports = {};
107
168
  __export(PdfViewer_exports, {
108
169
  PdfViewer: () => PdfViewer
109
170
  });
110
- function PdfViewer({ url, base64, onLoadSuccess }) {
171
+ function PdfViewer({ url, base64, onLoadSuccess, onLoadError }) {
111
172
  const { BORDER: BORDER2, MUTED: MUTED2, SURFACE: SURFACE2 } = useTheme();
112
173
  const [numPages, setNumPages] = useState(0);
113
174
  const [currentPage, setCurrentPage] = useState(1);
@@ -187,23 +248,34 @@ function PdfViewer({ url, base64, onLoadSuccess }) {
187
248
  ] }),
188
249
  /* @__PURE__ */ jsxs("div", { style: { overflow: "auto", width: "100%", display: "flex", justifyContent: "center", padding: "16px" }, children: [
189
250
  loading && /* @__PURE__ */ jsx("div", { style: { display: "flex", alignItems: "center", justifyContent: "center", height: "200px", color: MUTED2, fontSize: "13px" }, children: "Loading PDF..." }),
190
- /* @__PURE__ */ jsx(Document, { file: base64 ? `data:application/pdf;base64,${base64}` : url, onLoadSuccess: handleLoad, loading: "", children: /* @__PURE__ */ jsx(
191
- Page,
251
+ /* @__PURE__ */ jsx(
252
+ Document,
192
253
  {
193
- pageNumber: currentPage,
194
- width: 540,
195
- renderTextLayer: true,
196
- renderAnnotationLayer: true
254
+ file: base64 ? `data:application/pdf;base64,${base64}` : url,
255
+ onLoadSuccess: handleLoad,
256
+ onLoadError,
257
+ loading: "",
258
+ error: "",
259
+ children: /* @__PURE__ */ jsx(
260
+ Page,
261
+ {
262
+ pageNumber: currentPage,
263
+ width: 540,
264
+ renderTextLayer: true,
265
+ renderAnnotationLayer: true
266
+ }
267
+ )
197
268
  }
198
- ) })
269
+ )
199
270
  ] })
200
271
  ] });
201
272
  }
202
273
  var init_PdfViewer = __esm({
203
274
  "src/composites/document-preview/PdfViewer.tsx"() {
204
275
  "use client";
276
+ init_pdfWorkerProbe();
205
277
  init_ThemeContext();
206
- pdfjs.GlobalWorkerOptions.workerSrc = `https://unpkg.com/pdfjs-dist@${pdfjs.version}/build/pdf.worker.min.mjs`;
278
+ resolvePdfWorkerSrc(pdfjs);
207
279
  }
208
280
  });
209
281
 
@@ -927,6 +999,7 @@ function useFullscreen(ref) {
927
999
  // src/composites/document-preview/resolver.tsx
928
1000
  init_theme();
929
1001
  init_ThemeContext();
1002
+ init_pdfWorkerProbe();
930
1003
  var PdfViewer2 = React5.lazy(
931
1004
  () => Promise.resolve().then(() => (init_PdfViewer(), PdfViewer_exports)).then((m) => ({ default: m.PdfViewer }))
932
1005
  );
@@ -990,8 +1063,14 @@ function DocumentPreviewResolver(p) {
990
1063
  const totalPages = ((_c = p.pages) != null ? _c : []).length;
991
1064
  const page = p.pages[currentPage];
992
1065
  const pageHighlights = ((_d = p.highlights) != null ? _d : []).filter((h) => h.pageIndex === currentPage);
1066
+ const [pdfFailed, setPdfFailed] = useState(false);
1067
+ const pdfWorkerReady = usePdfWorkerReady(hasRealFile && isPdf);
1068
+ const pdfReady = pdfFailed ? false : pdfWorkerReady;
1069
+ const showPdfViewer = hasRealFile && isPdf && pdfReady === true;
1070
+ const pdfPending = hasRealFile && isPdf && pdfReady === null;
993
1071
  const handlePdfLoad = useCallback(() => {
994
1072
  }, []);
1073
+ const handlePdfError = useCallback(() => setPdfFailed(true), []);
995
1074
  const handleExcelLoad = useCallback(() => {
996
1075
  }, []);
997
1076
  return /* @__PURE__ */ jsx(ComponentActions, { filename: (_e = p.title) != null ? _e : "document-preview", children: /* @__PURE__ */ jsxs(
@@ -1085,7 +1164,7 @@ function DocumentPreviewResolver(p) {
1085
1164
  children: pg.label
1086
1165
  },
1087
1166
  i
1088
- )) }) : !hasRealFile && totalPages > 1 && /* @__PURE__ */ jsxs("div", { style: { display: "flex", alignItems: "center", gap: "8px", padding: "6px 14px", borderLeft: `1px solid ${BORDER2}`, borderRight: `1px solid ${BORDER2}`, background: SURFACE2 }, children: [
1167
+ )) }) : (!hasRealFile || pdfReady === false) && totalPages > 1 && /* @__PURE__ */ jsxs("div", { style: { display: "flex", alignItems: "center", gap: "8px", padding: "6px 14px", borderLeft: `1px solid ${BORDER2}`, borderRight: `1px solid ${BORDER2}`, background: SURFACE2 }, children: [
1089
1168
  /* @__PURE__ */ jsx("button", { onClick: () => setCurrentPage(Math.max(0, currentPage - 1)), disabled: currentPage === 0, style: { border: `1px solid ${BORDER2}`, borderRadius: "4px", padding: "2px 8px", background: SURFACE2, cursor: "pointer", fontSize: "12px", color: currentPage === 0 ? BORDER2 : MUTED2 }, children: "\u2190" }),
1090
1169
  /* @__PURE__ */ jsxs("span", { style: { fontSize: "12px", color: MUTED2 }, children: [
1091
1170
  currentPage + 1,
@@ -1105,7 +1184,7 @@ function DocumentPreviewResolver(p) {
1105
1184
  display: "flex",
1106
1185
  flexDirection: "column"
1107
1186
  }, children: [
1108
- hasRealFile && isPdf ? /* @__PURE__ */ jsx(Suspense, { fallback: null, children: /* @__PURE__ */ jsx(PdfViewer2, { url: (_q = p.source) == null ? void 0 : _q.url, base64: (_r = p.source) == null ? void 0 : _r.base64, onLoadSuccess: handlePdfLoad }) }) : hasRealFile && isExcel ? /* @__PURE__ */ jsx(Suspense, { fallback: null, children: /* @__PURE__ */ jsx(ExcelViewer2, { url: (_s = p.source) == null ? void 0 : _s.url, base64: (_t = p.source) == null ? void 0 : _t.base64, onLoadSuccess: handleExcelLoad }) }) : (page == null ? void 0 : page.imageUrl) ? /* @__PURE__ */ jsx("div", { style: { padding: "20px" }, children: /* @__PURE__ */ jsx("img", { src: page.imageUrl, alt: page.label, style: { maxWidth: "100%", borderRadius: "4px" } }) }) : (page == null ? void 0 : page.content) ? /* @__PURE__ */ jsx("div", { style: { fontFamily: "var(--font-sans, system-ui)", fontSize: "13px", color: "var(--foreground)", lineHeight: 1.6, margin: 0, padding: "20px" }, children: /* @__PURE__ */ jsx(Markdown, { content: page.content }) }) : /* @__PURE__ */ jsxs("div", { style: { display: "flex", alignItems: "center", justifyContent: "center", height: "120px", color: MUTED2, fontSize: "13px" }, children: [
1187
+ showPdfViewer ? /* @__PURE__ */ jsx(Suspense, { fallback: null, children: /* @__PURE__ */ jsx(PdfViewer2, { url: (_q = p.source) == null ? void 0 : _q.url, base64: (_r = p.source) == null ? void 0 : _r.base64, onLoadSuccess: handlePdfLoad, onLoadError: handlePdfError }) }) : pdfPending ? /* @__PURE__ */ jsx("div", { style: { display: "flex", alignItems: "center", justifyContent: "center", height: "200px", color: MUTED2, fontSize: "13px" }, children: "Loading preview\u2026" }) : hasRealFile && isExcel ? /* @__PURE__ */ jsx(Suspense, { fallback: null, children: /* @__PURE__ */ jsx(ExcelViewer2, { url: (_s = p.source) == null ? void 0 : _s.url, base64: (_t = p.source) == null ? void 0 : _t.base64, onLoadSuccess: handleExcelLoad }) }) : (page == null ? void 0 : page.imageUrl) ? /* @__PURE__ */ jsx("div", { style: { padding: "20px" }, children: /* @__PURE__ */ jsx("img", { src: page.imageUrl, alt: page.label, style: { maxWidth: "100%", borderRadius: "4px" } }) }) : (page == null ? void 0 : page.content) ? /* @__PURE__ */ jsx("div", { style: { fontFamily: "var(--font-sans, system-ui)", fontSize: "13px", color: "var(--foreground)", lineHeight: 1.6, margin: 0, padding: "20px" }, children: /* @__PURE__ */ jsx(Markdown, { content: page.content }) }) : /* @__PURE__ */ jsxs("div", { style: { display: "flex", alignItems: "center", justifyContent: "center", height: "120px", color: MUTED2, fontSize: "13px" }, children: [
1109
1188
  "No content available for ",
1110
1189
  (_u = page == null ? void 0 : page.label) != null ? _u : "this page"
1111
1190
  ] }),