@bevel-software/platform-core-frontend 0.11.2 → 0.12.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.
- package/THIRD-PARTY-NOTICES.md +670 -395
- package/package.json +6 -2
- package/src/lib/__tests__/api.test.ts +95 -0
- package/src/lib/api.ts +30 -2
- package/src/lib/clipboard.ts +25 -0
- package/src/modules/access/__tests__/useFileAccess.test.tsx +47 -0
- package/src/modules/access/hooks/useFileAccess.ts +23 -5
- package/src/modules/git/__tests__/GitSyncFailedBanner.test.tsx +181 -0
- package/src/modules/git/components/GitSyncFailedBanner.tsx +106 -0
- package/src/modules/library/utils/clipboard.ts +4 -20
- package/src/modules/secrets-vault/components/SecretsPage.tsx +313 -313
- package/src/modules/secrets-vault/components/__tests__/SecretsPage.test.tsx +82 -82
- package/src/modules/workspace/components/FileExplorer.tsx +6 -14
- package/src/modules/workspace/components/FileViewer.tsx +20 -4
- package/src/modules/workspace/components/KbPageHeader.tsx +1 -1
- package/src/modules/workspace/components/__tests__/FileExplorer.test.tsx +3 -1
- package/src/modules/workspace/components/__tests__/FileViewer.test.tsx +17 -0
- package/src/modules/workspace/components/__tests__/KbPageHeader.test.tsx +15 -0
- package/src/modules/workspace/components/renderers/DocxRenderer.tsx +11 -2
- package/src/modules/workspace/components/renderers/DownloadFileButton.tsx +88 -0
- package/src/modules/workspace/components/renderers/EmailRenderer.tsx +309 -0
- package/src/modules/workspace/components/renderers/LegacyOfficeRenderer.tsx +60 -0
- package/src/modules/workspace/components/renderers/PdfRenderer.tsx +261 -33
- package/src/modules/workspace/components/renderers/PptxRenderer.tsx +184 -0
- package/src/modules/workspace/components/renderers/XlsxRenderer.tsx +195 -45
- package/src/modules/workspace/components/renderers/__tests__/DownloadFileButton.test.tsx +48 -0
- package/src/modules/workspace/components/renderers/__tests__/EmailRenderer.test.tsx +176 -0
- package/src/modules/workspace/components/renderers/__tests__/PdfRenderer.test.tsx +214 -76
- package/src/modules/workspace/components/renderers/__tests__/PptxRenderer.test.tsx +125 -0
- package/src/modules/workspace/components/renderers/__tests__/XlsxRenderer.test.tsx +245 -0
- package/src/modules/workspace/components/renderers/__tests__/emailBody.test.ts +117 -0
- package/src/modules/workspace/components/renderers/__tests__/emailMessage.test.ts +404 -0
- package/src/modules/workspace/components/renderers/__tests__/pptxOutline.test.ts +448 -0
- package/src/modules/workspace/components/renderers/__tests__/readBodyCapped.test.ts +117 -0
- package/src/modules/workspace/components/renderers/__tests__/rendererLayout.test.ts +73 -7
- package/src/modules/workspace/components/renderers/__tests__/rendererSelection.test.tsx +107 -0
- package/src/modules/workspace/components/renderers/downloadFile.ts +33 -0
- package/src/modules/workspace/components/renderers/emailBody.ts +168 -0
- package/src/modules/workspace/components/renderers/emailMessage.ts +223 -0
- package/src/modules/workspace/components/renderers/emlMessage.ts +91 -0
- package/src/modules/workspace/components/renderers/htmlSandbox.ts +9 -2
- package/src/modules/workspace/components/renderers/index.ts +82 -6
- package/src/modules/workspace/components/renderers/lazyRenderer.tsx +9 -2
- package/src/modules/workspace/components/renderers/msgMessage.ts +219 -0
- package/src/modules/workspace/components/renderers/pptxOutline.ts +317 -0
- package/src/modules/workspace/components/renderers/readBodyCapped.ts +97 -0
- package/src/modules/workspace/components/renderers/xmlReading.ts +0 -0
- package/src/url-suffix-imports.d.ts +12 -0
|
@@ -1,81 +1,309 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
2
|
+
import { ChevronLeft, ChevronRight } from 'lucide-react';
|
|
3
|
+
import { getDocument, GlobalWorkerOptions } from 'pdfjs-dist';
|
|
4
|
+
import type { PDFDocumentLoadingTask, PDFDocumentProxy, PDFPageProxy } from 'pdfjs-dist';
|
|
5
|
+
import pdfWorkerUrl from 'pdfjs-dist/build/pdf.worker.min.mjs?url';
|
|
6
|
+
import { Button } from '../../../../shared/components';
|
|
2
7
|
import { useWorkspace } from '../../state/workspace.context';
|
|
3
8
|
import { authFetch } from '../../../../lib/api';
|
|
9
|
+
import { DownloadFileButton } from './DownloadFileButton';
|
|
4
10
|
import type { FileRendererProps } from './types';
|
|
5
11
|
|
|
12
|
+
// pdf.js parses on a worker thread; the `?url` import hands Vite the worker
|
|
13
|
+
// file to emit as an asset and gives us its URL to point the library at.
|
|
14
|
+
// Module-level (not per-mount): the worker location is process-wide state.
|
|
15
|
+
GlobalWorkerOptions.workerSrc = pdfWorkerUrl;
|
|
16
|
+
|
|
6
17
|
/**
|
|
7
|
-
* Inline PDF viewer
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
18
|
+
* Inline PDF viewer on `pdfjs-dist` — page-by-page canvas rendering with a
|
|
19
|
+
* Prev/Next pager, fit-to-width scaling, and a Download affordance.
|
|
20
|
+
*
|
|
21
|
+
* This replaced the old `<iframe src={blobUrl}>` handoff to the browser's
|
|
22
|
+
* built-in viewer: that one drew its own chrome (toolbar, sidebar, its own
|
|
23
|
+
* dark theme) inside our pane and rendered nothing at all where the built-in
|
|
24
|
+
* viewer is disabled or absent (hardened browsers, some WebViews). Rendering
|
|
25
|
+
* the pages ourselves costs a lazy-loaded chunk (see `renderers/index.ts` —
|
|
26
|
+
* pdf.js is why this renderer is code-split) and buys a viewer that is the
|
|
27
|
+
* same surface everywhere.
|
|
28
|
+
*
|
|
29
|
+
* Rendering is one page at a time, on purpose: decks and reports in a KB run
|
|
30
|
+
* to hundreds of pages, and rasterizing all of them up front is exactly the
|
|
31
|
+
* memory profile an embedded viewer must not have. The pager is the honest
|
|
32
|
+
* shape of that decision.
|
|
33
|
+
*
|
|
34
|
+
* View-only: there is no edit mode for PDFs. The renderer ignores
|
|
35
|
+
* `onSave` / `onValueChange` / `readOnly`.
|
|
13
36
|
*/
|
|
14
37
|
export function PdfRenderer({ filePath }: FileRendererProps) {
|
|
15
38
|
const { workspaceId } = useWorkspace();
|
|
16
|
-
const [
|
|
39
|
+
const [doc, setDoc] = useState<PDFDocumentProxy | null>(null);
|
|
40
|
+
const [pageNumber, setPageNumber] = useState(1);
|
|
17
41
|
const [error, setError] = useState<string | null>(null);
|
|
42
|
+
const containerRef = useRef<HTMLDivElement | null>(null);
|
|
43
|
+
const canvasRef = useRef<HTMLCanvasElement | null>(null);
|
|
44
|
+
// The page whose render last COMPLETED. Its cached resources are released
|
|
45
|
+
// only after the NEXT page's render settles — never mid-render (pdf.js
|
|
46
|
+
// corrupts a render whose page is cleaned up under it).
|
|
47
|
+
// The loading task, kept in a ref: it owns the worker-side document, and the
|
|
48
|
+
// page-render effect must be able to release it when it gives up for good.
|
|
49
|
+
const loadingTaskRef = useRef<PDFDocumentLoadingTask | null>(null);
|
|
50
|
+
|
|
51
|
+
/** Tear down the worker-side document and forget it. Safe to call twice. */
|
|
52
|
+
const releaseDoc = useCallback(() => {
|
|
53
|
+
loadingTaskRef.current?.destroy().catch(() => {});
|
|
54
|
+
loadingTaskRef.current = null;
|
|
55
|
+
setDoc(null);
|
|
56
|
+
}, []);
|
|
57
|
+
|
|
58
|
+
const renderedPageRef = useRef<PDFPageProxy | null>(null);
|
|
59
|
+
// Fit-to-width: the page is scaled so its width matches the container's.
|
|
60
|
+
// 0 = not measured yet; the render effect waits for a real measurement so
|
|
61
|
+
// the first paint is already at the right size instead of flashing 1:1.
|
|
62
|
+
const [width, setWidth] = useState(0);
|
|
18
63
|
|
|
19
64
|
useEffect(() => {
|
|
20
65
|
// Reset stale state from a prior filePath / workspaceId before starting
|
|
21
|
-
// the new
|
|
22
|
-
//
|
|
23
|
-
|
|
24
|
-
|
|
66
|
+
// the new load — including teardown (workspaceId → null). The rendered-
|
|
67
|
+
// page ref dies with its document (task.destroy frees the pages).
|
|
68
|
+
setDoc(null);
|
|
69
|
+
setPageNumber(1);
|
|
25
70
|
setError(null);
|
|
26
|
-
|
|
71
|
+
renderedPageRef.current = null;
|
|
27
72
|
|
|
28
73
|
if (!workspaceId) return;
|
|
29
74
|
|
|
30
|
-
let
|
|
31
|
-
let
|
|
75
|
+
let cancelled = false;
|
|
76
|
+
let loadingTask: PDFDocumentLoadingTask | null = null;
|
|
32
77
|
(async () => {
|
|
33
78
|
try {
|
|
34
79
|
const res = await authFetch(
|
|
35
80
|
`/api/workspace/${workspaceId}/file/raw?path=${encodeURIComponent(filePath)}`,
|
|
36
81
|
);
|
|
37
|
-
if (
|
|
82
|
+
if (cancelled) return;
|
|
38
83
|
if (!res.ok) {
|
|
39
84
|
setError(`Failed to load PDF (HTTP ${res.status})`);
|
|
40
85
|
return;
|
|
41
86
|
}
|
|
42
|
-
const
|
|
43
|
-
if (
|
|
44
|
-
|
|
45
|
-
|
|
87
|
+
const buffer = await res.arrayBuffer();
|
|
88
|
+
if (cancelled) return;
|
|
89
|
+
let loadedDoc: PDFDocumentProxy;
|
|
90
|
+
try {
|
|
91
|
+
// The TASK is retained (not just the resolved doc) so cleanup can
|
|
92
|
+
// tear it down in every state: still loading, resolved, or a doc
|
|
93
|
+
// that resolves only AFTER cancellation.
|
|
94
|
+
loadingTask = getDocument({ data: new Uint8Array(buffer) });
|
|
95
|
+
loadingTaskRef.current = loadingTask;
|
|
96
|
+
loadedDoc = await loadingTask.promise;
|
|
97
|
+
} catch (parseErr) {
|
|
98
|
+
// A destroyed task rejects its promise — that is this effect's own
|
|
99
|
+
// cleanup, not a parse failure to report.
|
|
100
|
+
if (cancelled) return;
|
|
101
|
+
// A REJECTED task still holds worker-side state. Tear it down now —
|
|
102
|
+
// the error view may stay mounted indefinitely — and null the ref so
|
|
103
|
+
// the effect cleanup doesn't destroy it a second time.
|
|
104
|
+
loadingTask?.destroy().catch(() => {});
|
|
105
|
+
loadingTaskRef.current = null;
|
|
106
|
+
loadingTask = null;
|
|
107
|
+
// The bytes arrived but pdf.js rejected them — a renamed file, a
|
|
108
|
+
// truncated upload. Distinct copy from the transport failure above.
|
|
109
|
+
console.warn('[PdfRenderer] parse failed:', parseErr);
|
|
110
|
+
setError('This file could not be parsed as a PDF.');
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
// Resolved after cancellation: cleanup already destroyed the task
|
|
114
|
+
// (which destroys the document with it) — just don't publish it.
|
|
115
|
+
if (cancelled) return;
|
|
116
|
+
setDoc(loadedDoc);
|
|
46
117
|
} catch (e) {
|
|
47
|
-
if (
|
|
118
|
+
if (cancelled) return;
|
|
48
119
|
setError(e instanceof Error ? e.message : String(e));
|
|
49
120
|
}
|
|
50
121
|
})();
|
|
51
122
|
|
|
52
123
|
return () => {
|
|
53
|
-
|
|
54
|
-
|
|
124
|
+
cancelled = true;
|
|
125
|
+
// Destroying the loading task frees the worker-side document and every
|
|
126
|
+
// cached page, whether the load is mid-flight or long resolved.
|
|
127
|
+
loadingTask?.destroy().catch(() => {});
|
|
128
|
+
loadingTaskRef.current = null;
|
|
55
129
|
};
|
|
56
130
|
}, [workspaceId, filePath]);
|
|
57
131
|
|
|
132
|
+
// Track the page column's width so the canvas re-renders to fit it. The
|
|
133
|
+
// container only mounts once the document is loaded, hence the `doc` dep.
|
|
134
|
+
useEffect(() => {
|
|
135
|
+
const el = containerRef.current;
|
|
136
|
+
if (!el) return;
|
|
137
|
+
const measure = () => setWidth(el.clientWidth);
|
|
138
|
+
measure();
|
|
139
|
+
// happy-dom (tests) has no ResizeObserver; the initial measure suffices.
|
|
140
|
+
if (typeof ResizeObserver === 'undefined') return;
|
|
141
|
+
const ro = new ResizeObserver(measure);
|
|
142
|
+
ro.observe(el);
|
|
143
|
+
return () => ro.disconnect();
|
|
144
|
+
}, [doc]);
|
|
145
|
+
|
|
146
|
+
// The render task most recently STARTED, surviving across effect runs: a
|
|
147
|
+
// canvas can only host one render at a time, and pdf.js rejects a second
|
|
148
|
+
// render() while the previous one — cancelled or not — has yet to settle.
|
|
149
|
+
// Each effect run parks on this before it draws.
|
|
150
|
+
const renderTaskRef = useRef<{ promise: Promise<unknown>; cancel(): void } | null>(null);
|
|
151
|
+
|
|
152
|
+
// Render the current page into the canvas whenever the page or the width
|
|
153
|
+
// changes. Cancellation matters twice over: a rapid Next-Next leaves the
|
|
154
|
+
// first render mid-flight (pdf.js throws RenderingCancelledException into
|
|
155
|
+
// the awaited promise), and a canvas can only host one render at a time.
|
|
156
|
+
useEffect(() => {
|
|
157
|
+
if (!doc || width <= 0) return;
|
|
158
|
+
// The loading task `doc` belongs to, captured now: if this render loses a
|
|
159
|
+
// race with a file switch (the load effect destroys the task — and the doc
|
|
160
|
+
// under it — before the null doc flushes), the failure path below must
|
|
161
|
+
// neither report an error over the NEW file nor release the new file's
|
|
162
|
+
// loading task.
|
|
163
|
+
const task = loadingTaskRef.current;
|
|
164
|
+
let cancelled = false;
|
|
165
|
+
let page: PDFPageProxy | null = null;
|
|
166
|
+
let renderTask: { promise: Promise<unknown>; cancel(): void } | null = null;
|
|
167
|
+
// Cancelled AFTER its render task settled: this page will never be the
|
|
168
|
+
// displayed one, so its resources go now — never the on-screen page's,
|
|
169
|
+
// and never mid-render (any superseding effect is still parked on this
|
|
170
|
+
// task's promise, so no render is active on the page).
|
|
171
|
+
const releaseCancelledPage = () => {
|
|
172
|
+
if (renderTask && page && page !== renderedPageRef.current) page.cleanup();
|
|
173
|
+
};
|
|
174
|
+
(async () => {
|
|
175
|
+
try {
|
|
176
|
+
// Wait out the previous render (its own cleanup has already requested
|
|
177
|
+
// cancellation): starting a second render on the shared canvas before
|
|
178
|
+
// the first settles is the concurrent use pdf.js rejects.
|
|
179
|
+
const priorTask = renderTaskRef.current;
|
|
180
|
+
if (priorTask) await priorTask.promise.catch(() => {});
|
|
181
|
+
if (cancelled) return;
|
|
182
|
+
page = await doc.getPage(pageNumber);
|
|
183
|
+
if (cancelled) return;
|
|
184
|
+
const canvas = canvasRef.current;
|
|
185
|
+
if (!canvas) return;
|
|
186
|
+
const base = page.getViewport({ scale: 1 });
|
|
187
|
+
const cssScale = width / base.width;
|
|
188
|
+
// Render at device resolution, display at CSS size — otherwise text
|
|
189
|
+
// is visibly blurry on any HiDPI screen.
|
|
190
|
+
const dpr = window.devicePixelRatio || 1;
|
|
191
|
+
const viewport = page.getViewport({ scale: cssScale * dpr });
|
|
192
|
+
canvas.width = Math.floor(viewport.width);
|
|
193
|
+
canvas.height = Math.floor(viewport.height);
|
|
194
|
+
canvas.style.width = `${Math.floor(viewport.width / dpr)}px`;
|
|
195
|
+
canvas.style.height = `${Math.floor(viewport.height / dpr)}px`;
|
|
196
|
+
// v5 API: hand over the canvas itself; pdf.js takes the 2D context.
|
|
197
|
+
const startedTask: { promise: Promise<unknown>; cancel(): void } = page.render({
|
|
198
|
+
canvas,
|
|
199
|
+
viewport,
|
|
200
|
+
});
|
|
201
|
+
renderTask = startedTask;
|
|
202
|
+
renderTaskRef.current = startedTask;
|
|
203
|
+
await startedTask.promise;
|
|
204
|
+
if (cancelled) {
|
|
205
|
+
releaseCancelledPage();
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
// THIS page's render has settled, so the PRIOR page's cached
|
|
209
|
+
// resources (fonts, operator lists) can be released. Sequenced here
|
|
210
|
+
// — after the render task resolves — because cleanup() during an
|
|
211
|
+
// active render is the one forbidden call.
|
|
212
|
+
const prior = renderedPageRef.current;
|
|
213
|
+
if (prior && prior !== page) prior.cleanup();
|
|
214
|
+
renderedPageRef.current = page;
|
|
215
|
+
} catch (err) {
|
|
216
|
+
// A cancelled render is this effect's own cleanup, not a failure.
|
|
217
|
+
if (cancelled || (err as Error | null)?.name === 'RenderingCancelledException') {
|
|
218
|
+
releaseCancelledPage();
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
// Superseded by a file switch mid-render: the load effect owns that
|
|
222
|
+
// teardown (this failure is its fallout), and `loadingTaskRef` may by
|
|
223
|
+
// now hold the NEW file's task — which must not be destroyed here.
|
|
224
|
+
if (task === null || loadingTaskRef.current !== task) return;
|
|
225
|
+
console.error('[PdfRenderer] page render failed:', err);
|
|
226
|
+
// The error view is terminal, so nothing will read `doc` again: release it
|
|
227
|
+
// (and the loading task it retains) rather than leaving worker-side PDF
|
|
228
|
+
// resources held for the lifetime of the view.
|
|
229
|
+
setError(`Could not render page ${pageNumber}.`);
|
|
230
|
+
releaseDoc();
|
|
231
|
+
}
|
|
232
|
+
})();
|
|
233
|
+
return () => {
|
|
234
|
+
cancelled = true;
|
|
235
|
+
renderTask?.cancel();
|
|
236
|
+
};
|
|
237
|
+
}, [doc, pageNumber, width]);
|
|
238
|
+
|
|
239
|
+
const goTo = useCallback(
|
|
240
|
+
(delta: number) => {
|
|
241
|
+
setPageNumber((current) => {
|
|
242
|
+
const total = doc?.numPages ?? 1;
|
|
243
|
+
return Math.min(total, Math.max(1, current + delta));
|
|
244
|
+
});
|
|
245
|
+
},
|
|
246
|
+
[doc],
|
|
247
|
+
);
|
|
248
|
+
|
|
58
249
|
if (error) {
|
|
59
250
|
return (
|
|
60
|
-
<div className="flex items-center justify-center
|
|
61
|
-
{error}
|
|
251
|
+
<div className="flex h-full flex-col items-center justify-center gap-3 p-6 text-center">
|
|
252
|
+
<p className="text-ui text-danger">{error}</p>
|
|
253
|
+
{/* The rendering failed; the bytes may still be perfectly good to
|
|
254
|
+
someone with a desktop viewer. */}
|
|
255
|
+
<DownloadFileButton filePath={filePath} />
|
|
62
256
|
</div>
|
|
63
257
|
);
|
|
64
258
|
}
|
|
65
259
|
|
|
66
|
-
if (!
|
|
260
|
+
if (!doc) {
|
|
67
261
|
return (
|
|
68
|
-
<div className="flex items-center justify-center
|
|
69
|
-
Loading PDF
|
|
262
|
+
<div className="flex h-full items-center justify-center text-ui text-ink-muted">
|
|
263
|
+
Loading PDF…
|
|
70
264
|
</div>
|
|
71
265
|
);
|
|
72
266
|
}
|
|
73
267
|
|
|
74
268
|
return (
|
|
75
|
-
<
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
269
|
+
<div className="flex h-full min-h-0 flex-col">
|
|
270
|
+
<div className="flex shrink-0 items-center gap-2 border-b border-line pb-2">
|
|
271
|
+
<Button
|
|
272
|
+
variant="quiet"
|
|
273
|
+
size="tiny"
|
|
274
|
+
leadingIcon={<ChevronLeft size={13} />}
|
|
275
|
+
onClick={() => goTo(-1)}
|
|
276
|
+
disabled={pageNumber <= 1}
|
|
277
|
+
title="Previous page"
|
|
278
|
+
>
|
|
279
|
+
Prev
|
|
280
|
+
</Button>
|
|
281
|
+
<span aria-live="polite" className="text-detail text-ink-muted whitespace-nowrap">
|
|
282
|
+
Page {pageNumber} of {doc.numPages}
|
|
283
|
+
</span>
|
|
284
|
+
<Button
|
|
285
|
+
variant="quiet"
|
|
286
|
+
size="tiny"
|
|
287
|
+
onClick={() => goTo(1)}
|
|
288
|
+
disabled={pageNumber >= doc.numPages}
|
|
289
|
+
title="Next page"
|
|
290
|
+
>
|
|
291
|
+
Next
|
|
292
|
+
<ChevronRight size={13} />
|
|
293
|
+
</Button>
|
|
294
|
+
<span className="flex-1" />
|
|
295
|
+
<DownloadFileButton filePath={filePath} />
|
|
296
|
+
</div>
|
|
297
|
+
{/* The measured column. Fit-to-width means horizontal scrolling never
|
|
298
|
+
happens; tall pages scroll vertically inside this pane. */}
|
|
299
|
+
<div ref={containerRef} className="min-h-0 flex-1 overflow-y-auto overflow-x-hidden pt-3">
|
|
300
|
+
<canvas
|
|
301
|
+
ref={canvasRef}
|
|
302
|
+
role="img"
|
|
303
|
+
aria-label={`${filePath}, page ${pageNumber} of ${doc.numPages}`}
|
|
304
|
+
className="block rounded-xs border border-line bg-white"
|
|
305
|
+
/>
|
|
306
|
+
</div>
|
|
307
|
+
</div>
|
|
80
308
|
);
|
|
81
309
|
}
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { useEffect, useState } from 'react';
|
|
2
|
+
import { Presentation } from 'lucide-react';
|
|
3
|
+
import { useWorkspace } from '../../state/workspace.context';
|
|
4
|
+
import { authFetch } from '../../../../lib/api';
|
|
5
|
+
import { DownloadFileButton } from './DownloadFileButton';
|
|
6
|
+
import { extractPptxOutline, type PptxSlide } from './pptxOutline';
|
|
7
|
+
import { readBodyCapped } from './readBodyCapped';
|
|
8
|
+
import type { FileRendererProps } from './types';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The honest `.pptx` view: a text outline, not a slide facsimile.
|
|
12
|
+
*
|
|
13
|
+
* Browser-side pptx RENDERERS were evaluated and rejected — every available
|
|
14
|
+
* one draws a wrong-enough version of the deck (fonts substituted, SmartArt
|
|
15
|
+
* and charts dropped, positioning approximate) that the result misleads more
|
|
16
|
+
* than it informs. So this viewer does what the backend's text extractor does
|
|
17
|
+
* for agents: slides as sections, each paragraph a line, speaker notes
|
|
18
|
+
* attached — and says so, with the original file one click away. The outline
|
|
19
|
+
* IS the same story `read_file` tells an agent about this deck (see
|
|
20
|
+
* `pptxOutline.ts`), which keeps human and agent conversations about "slide
|
|
21
|
+
* 4" pointing at the same thing.
|
|
22
|
+
*
|
|
23
|
+
* Text only, rendered as React text nodes — nothing from the document is ever
|
|
24
|
+
* interpreted as HTML, so there is no sanitization question here.
|
|
25
|
+
*
|
|
26
|
+
* View-only: there is no edit mode for binary office formats. The renderer
|
|
27
|
+
* ignores `onSave` / `onValueChange` / `readOnly`.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Compressed-archive bound. The parser caps what it DECOMPRESSES (see
|
|
32
|
+
* `pptxOutline.ts`), but those caps only run after the whole response is in
|
|
33
|
+
* memory — so the download itself is bounded too, or an oversized upload
|
|
34
|
+
* could exhaust the tab before the parser ever saw a byte. Matches the deck's
|
|
35
|
+
* 200 MB aggregate inflation budget; a media-heavy deck past it is
|
|
36
|
+
* download-only.
|
|
37
|
+
*/
|
|
38
|
+
const MAX_ARCHIVE_BYTES = 200 * 1024 * 1024;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* DOM bound per slide. The parser caps BYTES, not element counts — a crafted
|
|
42
|
+
* slide can spell millions of one-character paragraphs inside its 50 MB, and
|
|
43
|
+
* every rendered line is a DOM node on the reader's main thread. Far above
|
|
44
|
+
* any real slide (tens of lines); past it the outline truncates with a
|
|
45
|
+
* notice.
|
|
46
|
+
*/
|
|
47
|
+
const MAX_LINES_PER_SLIDE = 500;
|
|
48
|
+
|
|
49
|
+
export function PptxRenderer({ filePath }: FileRendererProps) {
|
|
50
|
+
const { workspaceId } = useWorkspace();
|
|
51
|
+
const [slides, setSlides] = useState<PptxSlide[] | null>(null);
|
|
52
|
+
const [error, setError] = useState<string | null>(null);
|
|
53
|
+
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
setSlides(null);
|
|
56
|
+
setError(null);
|
|
57
|
+
if (!workspaceId) return;
|
|
58
|
+
|
|
59
|
+
let cancelled = false;
|
|
60
|
+
// `cancelled` only stops this effect from PUBLISHING; the request itself
|
|
61
|
+
// ran to completion, so switching files left the old deck downloading and
|
|
62
|
+
// buffering in full. The abort ends it at the transport.
|
|
63
|
+
const abort = new AbortController();
|
|
64
|
+
(async () => {
|
|
65
|
+
try {
|
|
66
|
+
const res = await authFetch(
|
|
67
|
+
`/api/workspace/${workspaceId}/file/raw?path=${encodeURIComponent(filePath)}`,
|
|
68
|
+
{ signal: abort.signal },
|
|
69
|
+
);
|
|
70
|
+
if (cancelled) return;
|
|
71
|
+
if (!res.ok) {
|
|
72
|
+
setError(`Failed to load presentation (HTTP ${res.status})`);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
// The parser's decompression caps only run once the whole response is
|
|
76
|
+
// in memory, so the transfer is bounded first: an over-cap
|
|
77
|
+
// Content-Length is refused (and the request aborted) without a byte
|
|
78
|
+
// buffered, an undeclared body abandoned the moment it crosses the cap.
|
|
79
|
+
const buffer = await readBodyCapped(res, MAX_ARCHIVE_BYTES, abort);
|
|
80
|
+
if (cancelled) return;
|
|
81
|
+
if (buffer === null) {
|
|
82
|
+
setError('This presentation is too large to preview — download it to view the slides.');
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
const outline = await extractPptxOutline(buffer);
|
|
86
|
+
if (cancelled) return;
|
|
87
|
+
setSlides(outline);
|
|
88
|
+
} catch (e) {
|
|
89
|
+
if (cancelled) return;
|
|
90
|
+
// `extractPptxOutline` throws "could not be parsed as a .pptx (…)".
|
|
91
|
+
setError(`This file ${e instanceof Error ? e.message : String(e)}`);
|
|
92
|
+
}
|
|
93
|
+
})();
|
|
94
|
+
|
|
95
|
+
return () => {
|
|
96
|
+
cancelled = true;
|
|
97
|
+
abort.abort();
|
|
98
|
+
};
|
|
99
|
+
}, [workspaceId, filePath]);
|
|
100
|
+
|
|
101
|
+
if (error) {
|
|
102
|
+
return (
|
|
103
|
+
<div className="flex min-h-40 flex-col items-center justify-center gap-3 p-6 text-center">
|
|
104
|
+
<p className="text-ui text-danger">{error}</p>
|
|
105
|
+
<DownloadFileButton filePath={filePath} />
|
|
106
|
+
</div>
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (slides === null) {
|
|
111
|
+
return (
|
|
112
|
+
<div className="flex min-h-40 items-center justify-center text-ui text-ink-muted">
|
|
113
|
+
Loading presentation…
|
|
114
|
+
</div>
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return (
|
|
119
|
+
// No scroller of its own: the outline is a document, so it sits in
|
|
120
|
+
// `KbDocumentShell`'s prose column and the column scrolls.
|
|
121
|
+
<div className="min-w-0">
|
|
122
|
+
{/* What this view is, and the way to the real thing — up front, not in
|
|
123
|
+
a footnote: the one thing a reader must not believe is that the deck
|
|
124
|
+
IS this outline. */}
|
|
125
|
+
<div className="mb-4 flex items-center gap-3 rounded-sm bg-sunken px-3 py-2">
|
|
126
|
+
<Presentation size={15} className="shrink-0 text-ink-faint" aria-hidden />
|
|
127
|
+
<p className="min-w-0 flex-1 text-detail text-ink-muted">
|
|
128
|
+
Text outline of the presentation — layout, images and formatting are not shown.
|
|
129
|
+
Download the file to see the full slides.
|
|
130
|
+
</p>
|
|
131
|
+
<DownloadFileButton filePath={filePath} size="sm" />
|
|
132
|
+
</div>
|
|
133
|
+
{slides.map((slide) => (
|
|
134
|
+
<section key={slide.number} className="mb-6">
|
|
135
|
+
<h2 className="mb-2 border-b border-line pb-1 text-ui font-semibold text-ink">
|
|
136
|
+
Slide {slide.number}
|
|
137
|
+
</h2>
|
|
138
|
+
{slide.paragraphs.length === 0 && slide.notes.length === 0 ? (
|
|
139
|
+
<p className="text-detail italic text-ink-faint">No text on this slide.</p>
|
|
140
|
+
) : (
|
|
141
|
+
<>
|
|
142
|
+
{slide.paragraphs.slice(0, MAX_LINES_PER_SLIDE).map((text, i) => (
|
|
143
|
+
// `whitespace-pre-wrap`: the extraction keeps the deck's own runs of
|
|
144
|
+
// spaces, and collapsing them here made the outline disagree with what
|
|
145
|
+
// `read_file` returns. `break-words` keeps a long token from widening
|
|
146
|
+
// the page.
|
|
147
|
+
<p key={i} className="mb-1 whitespace-pre-wrap break-words text-ui text-ink">
|
|
148
|
+
{text}
|
|
149
|
+
</p>
|
|
150
|
+
))}
|
|
151
|
+
{slide.paragraphs.length > MAX_LINES_PER_SLIDE && (
|
|
152
|
+
<p className="mb-1 text-detail italic text-ink-faint">
|
|
153
|
+
… {slide.paragraphs.length - MAX_LINES_PER_SLIDE} more paragraphs not shown —
|
|
154
|
+
download the file to read them.
|
|
155
|
+
</p>
|
|
156
|
+
)}
|
|
157
|
+
</>
|
|
158
|
+
)}
|
|
159
|
+
{slide.notes.length > 0 && (
|
|
160
|
+
<div className="mt-2 border-l-2 border-line pl-3">
|
|
161
|
+
<div className="mb-1 text-meta font-medium uppercase tracking-wide text-ink-faint">
|
|
162
|
+
Notes
|
|
163
|
+
</div>
|
|
164
|
+
{slide.notes.slice(0, MAX_LINES_PER_SLIDE).map((text, i) => (
|
|
165
|
+
// Same whitespace/wrapping contract as the slide paragraphs above:
|
|
166
|
+
// notes keep their runs of spaces too, and a long token must not
|
|
167
|
+
// widen the prose column.
|
|
168
|
+
<p key={i} className="mb-1 whitespace-pre-wrap break-words text-detail text-ink-muted">
|
|
169
|
+
{text}
|
|
170
|
+
</p>
|
|
171
|
+
))}
|
|
172
|
+
{slide.notes.length > MAX_LINES_PER_SLIDE && (
|
|
173
|
+
<p className="mb-1 text-detail italic text-ink-faint">
|
|
174
|
+
… {slide.notes.length - MAX_LINES_PER_SLIDE} more note paragraphs not shown —
|
|
175
|
+
download the file to read them.
|
|
176
|
+
</p>
|
|
177
|
+
)}
|
|
178
|
+
</div>
|
|
179
|
+
)}
|
|
180
|
+
</section>
|
|
181
|
+
))}
|
|
182
|
+
</div>
|
|
183
|
+
);
|
|
184
|
+
}
|