@astra-spec/ui 0.0.2 → 0.0.4
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/README.md +231 -20
- package/TOKENS.md +10 -3
- package/components.css +2 -0
- package/dist/blocks/decisions-list.d.ts +0 -1
- package/dist/blocks/decisions-list.d.ts.map +1 -1
- package/dist/blocks/decisions-list.js +1 -5
- package/dist/blocks/decisions-list.js.map +1 -1
- package/dist/blocks/index.d.ts +3 -3
- package/dist/blocks/index.d.ts.map +1 -1
- package/dist/blocks/index.js +2 -2
- package/dist/blocks/index.js.map +1 -1
- package/dist/blocks/inputs-list.js +1 -1
- package/dist/blocks/inputs-list.js.map +1 -1
- package/dist/blocks/outputs-list.d.ts +2 -2
- package/dist/blocks/outputs-list.d.ts.map +1 -1
- package/dist/blocks/outputs-list.js +15 -19
- package/dist/blocks/outputs-list.js.map +1 -1
- package/dist/blocks/section.d.ts +1 -4
- package/dist/blocks/section.d.ts.map +1 -1
- package/dist/blocks/section.js +1 -12
- package/dist/blocks/section.js.map +1 -1
- package/dist/components/artifact-preview.d.ts +2 -54
- package/dist/components/artifact-preview.d.ts.map +1 -1
- package/dist/components/artifact-preview.js +2 -107
- package/dist/components/artifact-preview.js.map +1 -1
- package/dist/components/decision-detail.d.ts +1 -1
- package/dist/components/decision-detail.d.ts.map +1 -1
- package/dist/components/decision-detail.js.map +1 -1
- package/dist/components/finding-detail.d.ts +2 -2
- package/dist/components/finding-detail.d.ts.map +1 -1
- package/dist/components/finding-detail.js.map +1 -1
- package/dist/components/index.d.ts +11 -13
- package/dist/components/index.d.ts.map +1 -1
- package/dist/components/index.js +7 -8
- package/dist/components/index.js.map +1 -1
- package/dist/components/input-detail.d.ts +1 -2
- package/dist/components/input-detail.d.ts.map +1 -1
- package/dist/components/input-detail.js +0 -3
- package/dist/components/input-detail.js.map +1 -1
- package/dist/components/insight-detail.d.ts +2 -8
- package/dist/components/insight-detail.d.ts.map +1 -1
- package/dist/components/insight-detail.js +5 -11
- package/dist/components/insight-detail.js.map +1 -1
- package/dist/components/insight-trigger.d.ts +1 -1
- package/dist/components/insight-trigger.d.ts.map +1 -1
- package/dist/components/insight-trigger.js.map +1 -1
- package/dist/components/output-detail.d.ts +2 -7
- package/dist/components/output-detail.d.ts.map +1 -1
- package/dist/components/output-detail.js +3 -30
- package/dist/components/output-detail.js.map +1 -1
- package/dist/components/output-dialog.d.ts +2 -2
- package/dist/components/output-dialog.d.ts.map +1 -1
- package/dist/components/output-dialog.js +2 -1
- package/dist/components/output-dialog.js.map +1 -1
- package/dist/components/paper-detail.d.ts +14 -14
- package/dist/components/paper-detail.d.ts.map +1 -1
- package/dist/components/paper-detail.js +49 -29
- package/dist/components/paper-detail.js.map +1 -1
- package/dist/components/paper-pdf-viewer.d.ts +19 -0
- package/dist/components/paper-pdf-viewer.d.ts.map +1 -0
- package/dist/components/paper-pdf-viewer.js +250 -0
- package/dist/components/paper-pdf-viewer.js.map +1 -0
- package/dist/components/record-dialog.d.ts +7 -5
- package/dist/components/record-dialog.d.ts.map +1 -1
- package/dist/components/record-dialog.js +7 -5
- package/dist/components/record-dialog.js.map +1 -1
- package/dist/components/record-preview.d.ts +61 -0
- package/dist/components/record-preview.d.ts.map +1 -0
- package/dist/components/record-preview.js +177 -0
- package/dist/components/record-preview.js.map +1 -0
- package/dist/components/relation-items.d.ts +1 -1
- package/dist/components/relation-items.d.ts.map +1 -1
- package/dist/components/relation-items.js +10 -3
- package/dist/components/relation-items.js.map +1 -1
- package/dist/{components/use-detail-stack.d.ts → lib/detail-stack.d.ts} +17 -2
- package/dist/lib/detail-stack.d.ts.map +1 -0
- package/dist/{components/use-detail-stack.js → lib/detail-stack.js} +15 -2
- package/dist/lib/detail-stack.js.map +1 -0
- package/dist/lib/index.d.ts +14 -0
- package/dist/lib/index.d.ts.map +1 -0
- package/dist/lib/index.js +9 -0
- package/dist/lib/index.js.map +1 -0
- package/dist/lib/labels.d.ts +32 -0
- package/dist/lib/labels.d.ts.map +1 -1
- package/dist/lib/labels.js +32 -0
- package/dist/lib/labels.js.map +1 -1
- package/dist/lib/pdf-quote.d.ts +47 -0
- package/dist/lib/pdf-quote.d.ts.map +1 -0
- package/dist/lib/pdf-quote.js +154 -0
- package/dist/lib/pdf-quote.js.map +1 -0
- package/dist/lib/pdf-runtime.d.ts +102 -0
- package/dist/lib/pdf-runtime.d.ts.map +1 -0
- package/dist/lib/pdf-runtime.js +48 -0
- package/dist/lib/pdf-runtime.js.map +1 -0
- package/dist/lib/preview-data.d.ts +55 -0
- package/dist/lib/preview-data.d.ts.map +1 -0
- package/dist/lib/preview-data.js +108 -0
- package/dist/lib/preview-data.js.map +1 -0
- package/dist/lib/prose.d.ts +37 -0
- package/dist/lib/prose.d.ts.map +1 -0
- package/dist/lib/prose.js +60 -0
- package/dist/lib/prose.js.map +1 -0
- package/dist/lib/use-isomorphic-layout-effect.d.ts +4 -0
- package/dist/lib/use-isomorphic-layout-effect.d.ts.map +1 -0
- package/dist/lib/use-isomorphic-layout-effect.js +4 -0
- package/dist/lib/use-isomorphic-layout-effect.js.map +1 -0
- package/dist/lib/use-output-expanded.d.ts +7 -0
- package/dist/lib/use-output-expanded.d.ts.map +1 -0
- package/dist/lib/use-output-expanded.js +29 -0
- package/dist/lib/use-output-expanded.js.map +1 -0
- package/dist/model/index.d.ts +5 -3
- package/dist/model/index.d.ts.map +1 -1
- package/dist/model/index.js +3 -2
- package/dist/model/index.js.map +1 -1
- package/dist/{primitives → model}/kind.d.ts +3 -0
- package/dist/model/kind.d.ts.map +1 -0
- package/dist/model/kind.js +25 -0
- package/dist/model/kind.js.map +1 -0
- package/dist/model/papers.d.ts +6 -6
- package/dist/model/papers.d.ts.map +1 -1
- package/dist/model/papers.js +8 -0
- package/dist/model/papers.js.map +1 -1
- package/dist/model/records.d.ts +5 -1
- package/dist/model/records.d.ts.map +1 -1
- package/dist/model/records.js +9 -0
- package/dist/model/records.js.map +1 -1
- package/dist/primitives/badge.d.ts +1 -1
- package/dist/primitives/badge.d.ts.map +1 -1
- package/dist/primitives/button.js +1 -1
- package/dist/primitives/button.js.map +1 -1
- package/dist/primitives/dialog.d.ts +1 -1
- package/dist/primitives/dialog.d.ts.map +1 -1
- package/dist/primitives/dialog.js +3 -3
- package/dist/primitives/dialog.js.map +1 -1
- package/dist/primitives/index.d.ts +6 -9
- package/dist/primitives/index.d.ts.map +1 -1
- package/dist/primitives/index.js +3 -5
- package/dist/primitives/index.js.map +1 -1
- package/dist/primitives/preview-popover.d.ts +30 -0
- package/dist/primitives/preview-popover.d.ts.map +1 -0
- package/dist/primitives/preview-popover.js +132 -0
- package/dist/primitives/preview-popover.js.map +1 -0
- package/dist/primitives/prose.d.ts +1 -29
- package/dist/primitives/prose.d.ts.map +1 -1
- package/dist/primitives/prose.js +2 -50
- package/dist/primitives/prose.js.map +1 -1
- package/dist/primitives/record-list.d.ts +1 -1
- package/dist/primitives/record-list.d.ts.map +1 -1
- package/dist/primitives/record-list.js +1 -1
- package/dist/primitives/record-list.js.map +1 -1
- package/dist/primitives/relation-list.d.ts +1 -1
- package/dist/primitives/relation-list.d.ts.map +1 -1
- package/dist/primitives/relation-list.js +1 -1
- package/dist/primitives/relation-list.js.map +1 -1
- package/dist/primitives/slot.d.ts.map +1 -0
- package/dist/{lib → primitives}/slot.js +1 -1
- package/dist/primitives/slot.js.map +1 -0
- package/dist/primitives/surface-header.d.ts +1 -1
- package/dist/primitives/surface-header.d.ts.map +1 -1
- package/dist/views/inventory.d.ts +7 -5
- package/dist/views/inventory.d.ts.map +1 -1
- package/dist/views/inventory.js +5 -4
- package/dist/views/inventory.js.map +1 -1
- package/package.json +12 -1
- package/primitives.css +1 -0
- package/src/blocks/decisions-list.tsx +1 -6
- package/src/blocks/index.ts +2 -3
- package/src/blocks/inputs-list.tsx +1 -1
- package/src/blocks/outputs-list.tsx +53 -30
- package/src/blocks/section.tsx +1 -14
- package/src/components/artifact-preview.tsx +6 -161
- package/src/components/decision-detail.tsx +2 -1
- package/src/components/finding-detail.tsx +3 -2
- package/src/components/index.ts +19 -29
- package/src/components/input-detail.tsx +2 -5
- package/src/components/insight-detail.tsx +20 -35
- package/src/components/insight-trigger.tsx +2 -1
- package/src/components/output-detail.tsx +17 -38
- package/src/components/output-dialog.tsx +4 -3
- package/src/components/paper-detail.tsx +121 -66
- package/src/components/paper-pdf-viewer.tsx +319 -0
- package/src/components/record-dialog.tsx +15 -10
- package/src/components/record-preview.tsx +762 -0
- package/src/components/relation-items.ts +15 -5
- package/src/{components/use-detail-stack.ts → lib/detail-stack.ts} +24 -1
- package/src/lib/index.ts +22 -0
- package/src/lib/labels.tsx +67 -0
- package/src/lib/pdf-quote.ts +189 -0
- package/src/lib/pdf-runtime.ts +131 -0
- package/src/lib/preview-data.ts +155 -0
- package/src/lib/prose.tsx +87 -0
- package/src/lib/use-isomorphic-layout-effect.ts +4 -0
- package/src/lib/use-output-expanded.ts +27 -0
- package/src/model/index.ts +4 -2
- package/src/{primitives → model}/kind.ts +14 -0
- package/src/model/papers.ts +9 -7
- package/src/model/records.ts +12 -0
- package/src/primitives/badge.tsx +1 -1
- package/src/primitives/button.tsx +1 -1
- package/src/primitives/dialog.tsx +3 -5
- package/src/primitives/index.ts +9 -9
- package/src/primitives/preview-popover.tsx +278 -0
- package/src/primitives/prose.tsx +1 -63
- package/src/primitives/record-list.tsx +1 -1
- package/src/primitives/relation-list.tsx +1 -1
- package/src/{lib → primitives}/slot.tsx +1 -1
- package/src/primitives/surface-header.tsx +1 -1
- package/src/views/inventory.tsx +12 -8
- package/styles/blocks/analysis-tree.css +2 -2
- package/styles/blocks/decisions-list.css +4 -4
- package/styles/blocks/outputs-list.css +246 -19
- package/styles/blocks/papers-list.css +2 -2
- package/styles/components/artifact-preview.css +6 -6
- package/styles/components/decision-detail.css +2 -2
- package/styles/components/evidence.css +2 -2
- package/styles/components/input-detail.css +4 -4
- package/styles/components/insight-detail.css +56 -94
- package/styles/components/insight-list.css +3 -3
- package/styles/components/insight-trigger.css +1 -4
- package/styles/components/output-detail.css +25 -140
- package/styles/components/paper-detail.css +76 -95
- package/styles/components/paper-doi.css +3 -12
- package/styles/components/paper-pdf-viewer.css +120 -0
- package/styles/components/record-preview.css +316 -0
- package/styles/primitives/badge.css +1 -1
- package/styles/primitives/count-heading.css +2 -2
- package/styles/primitives/detail-layout.css +5 -5
- package/styles/primitives/dialog.css +9 -9
- package/styles/primitives/preview-popover.css +76 -0
- package/styles/primitives/record-list.css +17 -17
- package/styles/primitives/relation-list.css +6 -6
- package/styles/primitives/surface-header.css +5 -5
- package/styles/tokens.css +16 -3
- package/styles/views/inventory.css +3 -3
- package/dist/components/detail-entry.d.ts +0 -15
- package/dist/components/detail-entry.d.ts.map +0 -1
- package/dist/components/detail-entry.js +0 -14
- package/dist/components/detail-entry.js.map +0 -1
- package/dist/components/use-detail-stack.d.ts.map +0 -1
- package/dist/components/use-detail-stack.js.map +0 -1
- package/dist/lib/slot.d.ts.map +0 -1
- package/dist/lib/slot.js.map +0 -1
- package/dist/primitives/kind.d.ts.map +0 -1
- package/dist/primitives/kind.js +0 -14
- package/dist/primitives/kind.js.map +0 -1
- package/src/components/detail-entry.ts +0 -19
- /package/dist/{lib → primitives}/slot.d.ts +0 -0
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
import { forwardRef, useCallback, useEffect, useRef, useState, type HTMLAttributes } from 'react';
|
|
2
|
+
import { cn } from '../lib/cn.js';
|
|
3
|
+
import { useLabels } from '../lib/labels.js';
|
|
4
|
+
import { useIsomorphicLayoutEffect } from '../lib/use-isomorphic-layout-effect.js';
|
|
5
|
+
import {
|
|
6
|
+
highlightMatch,
|
|
7
|
+
locateQuote,
|
|
8
|
+
pageText,
|
|
9
|
+
type PdfPassage,
|
|
10
|
+
type PdfPassageLocation,
|
|
11
|
+
type PdfTextCache,
|
|
12
|
+
} from '../lib/pdf-quote.js';
|
|
13
|
+
import type { PdfDocument, PdfJs, PdfJsLoader, PdfLoadingTask, PdfRenderTask, PdfTextLayer } from '../lib/pdf-runtime.js';
|
|
14
|
+
|
|
15
|
+
export type { PdfPassage } from '../lib/pdf-quote.js';
|
|
16
|
+
|
|
17
|
+
export type PdfLoadState = 'loading' | 'ready' | 'error';
|
|
18
|
+
|
|
19
|
+
export interface PaperPdfViewerProps extends Omit<HTMLAttributes<HTMLDivElement>, 'children'> {
|
|
20
|
+
pdfUrl: string;
|
|
21
|
+
/** Names the viewer for assistive technology. */
|
|
22
|
+
title: string;
|
|
23
|
+
/** Called once per document; a later identity is ignored, so an inline function is fine. */
|
|
24
|
+
loadPdfJs: PdfJsLoader;
|
|
25
|
+
/** Scrolls to and highlights a passage once the document is readable; a new `key` repeats the request. */
|
|
26
|
+
passage?: PdfPassage | undefined;
|
|
27
|
+
/** Reports loading, readiness, or failure with its reason. The latest callback is used; identity is not tracked. */
|
|
28
|
+
onLoadStateChange?: ((state: PdfLoadState, error?: unknown) => void) | undefined;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
interface Loaded {
|
|
32
|
+
pdf: PdfDocument;
|
|
33
|
+
runtime: PdfJs;
|
|
34
|
+
text: PdfTextCache;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface Focus extends PdfPassageLocation {
|
|
38
|
+
key: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** iOS Safari's canvas area limit (4096 x 4096); CSS scales a smaller backing store up to the page shell. */
|
|
42
|
+
const MAX_CANVAS_PIXELS = 16_777_216;
|
|
43
|
+
const ZOOM_MIN = 0.5;
|
|
44
|
+
const ZOOM_MAX = 2;
|
|
45
|
+
const ZOOM_STEP = 0.25;
|
|
46
|
+
|
|
47
|
+
function Page({ loaded, number, width, zoom, focus }: {
|
|
48
|
+
loaded: Loaded;
|
|
49
|
+
number: number;
|
|
50
|
+
width: number;
|
|
51
|
+
zoom: number;
|
|
52
|
+
focus: Focus | undefined;
|
|
53
|
+
}) {
|
|
54
|
+
const { pdf: labels } = useLabels();
|
|
55
|
+
const shellRef = useRef<HTMLDivElement>(null);
|
|
56
|
+
const canvasRef = useRef<HTMLCanvasElement>(null);
|
|
57
|
+
const textRef = useRef<HTMLDivElement>(null);
|
|
58
|
+
const [nearby, setNearby] = useState(number === 1);
|
|
59
|
+
const [aspectRatio, setAspectRatio] = useState(612 / 792);
|
|
60
|
+
const [failed, setFailed] = useState(false);
|
|
61
|
+
// The rendered text layer lives in a ref so the render effect's cleanup can
|
|
62
|
+
// drop it synchronously; the version re-runs the highlight effect once a
|
|
63
|
+
// render attempt has settled, so a locate request never lands on detached
|
|
64
|
+
// spans and still brings a page without a text layer into view.
|
|
65
|
+
const layerRef = useRef<PdfTextLayer | undefined>(undefined);
|
|
66
|
+
const settledRef = useRef(false);
|
|
67
|
+
const [renderVersion, setRenderVersion] = useState(0);
|
|
68
|
+
const scrolledRequest = useRef<string | undefined>(undefined);
|
|
69
|
+
const active = nearby || focus !== undefined;
|
|
70
|
+
|
|
71
|
+
useEffect(() => {
|
|
72
|
+
const shell = shellRef.current;
|
|
73
|
+
if (!shell) return;
|
|
74
|
+
const observer = new IntersectionObserver(
|
|
75
|
+
(entries) => { setNearby(entries.some((entry) => entry.isIntersecting)); },
|
|
76
|
+
{ root: shell.parentElement, rootMargin: '600px' },
|
|
77
|
+
);
|
|
78
|
+
observer.observe(shell);
|
|
79
|
+
return () => { observer.disconnect(); };
|
|
80
|
+
}, []);
|
|
81
|
+
|
|
82
|
+
// Shells keep their geometry whether or not they are rasterized, so mixed page sizes scroll correctly.
|
|
83
|
+
useEffect(() => {
|
|
84
|
+
let disposed = false;
|
|
85
|
+
void loaded.pdf.getPage(number).then((page) => {
|
|
86
|
+
if (disposed) return;
|
|
87
|
+
const viewport = page.getViewport({ scale: 1 });
|
|
88
|
+
setAspectRatio(viewport.width / viewport.height);
|
|
89
|
+
}).catch(() => { /* Rendering reports errors for pages the reader visits. */ });
|
|
90
|
+
return () => { disposed = true; };
|
|
91
|
+
}, [loaded, number]);
|
|
92
|
+
|
|
93
|
+
useEffect(() => {
|
|
94
|
+
if (!active || width === 0) return;
|
|
95
|
+
const canvas = canvasRef.current;
|
|
96
|
+
const text = textRef.current;
|
|
97
|
+
if (!canvas || !text) return;
|
|
98
|
+
let disposed = false;
|
|
99
|
+
let task: PdfRenderTask | undefined;
|
|
100
|
+
let layer: PdfTextLayer | undefined;
|
|
101
|
+
setFailed(false);
|
|
102
|
+
void (async () => {
|
|
103
|
+
let rasterized = false;
|
|
104
|
+
try {
|
|
105
|
+
const page = await loaded.pdf.getPage(number);
|
|
106
|
+
if (disposed) return;
|
|
107
|
+
const base = page.getViewport({ scale: 1 });
|
|
108
|
+
const viewport = page.getViewport({ scale: width * zoom / base.width });
|
|
109
|
+
const context = canvas.getContext('2d');
|
|
110
|
+
if (!context) throw new Error('Canvas rendering is unavailable.');
|
|
111
|
+
const ratio = Math.min(
|
|
112
|
+
window.devicePixelRatio || 1,
|
|
113
|
+
2,
|
|
114
|
+
Math.sqrt(MAX_CANVAS_PIXELS / (viewport.width * viewport.height)),
|
|
115
|
+
);
|
|
116
|
+
// Rounded down, so the backing store never exceeds the cap the ratio was derived from.
|
|
117
|
+
canvas.width = Math.floor(viewport.width * ratio);
|
|
118
|
+
canvas.height = Math.floor(viewport.height * ratio);
|
|
119
|
+
text.replaceChildren();
|
|
120
|
+
// pdf.js 4.8 sizes the text layer from --scale-factor; later text layers read --total-scale-factor.
|
|
121
|
+
text.style.setProperty('--scale-factor', String(viewport.scale));
|
|
122
|
+
text.style.setProperty('--total-scale-factor', String(viewport.scale));
|
|
123
|
+
task = page.render({ canvasContext: context, viewport, transform: [ratio, 0, 0, ratio, 0, 0] });
|
|
124
|
+
await task.promise;
|
|
125
|
+
task = undefined;
|
|
126
|
+
rasterized = true;
|
|
127
|
+
if (disposed) return;
|
|
128
|
+
const content = await pageText(loaded.pdf, number, loaded.text);
|
|
129
|
+
if (disposed) return;
|
|
130
|
+
layer = new loaded.runtime.TextLayer({ textContentSource: content, container: text, viewport });
|
|
131
|
+
await layer.render();
|
|
132
|
+
const rendered = layer;
|
|
133
|
+
layer = undefined;
|
|
134
|
+
if (disposed) return;
|
|
135
|
+
layerRef.current = rendered;
|
|
136
|
+
} catch {
|
|
137
|
+
// A page without extractable text still shows its canvas.
|
|
138
|
+
if (!disposed && !rasterized) setFailed(true);
|
|
139
|
+
}
|
|
140
|
+
if (disposed) return;
|
|
141
|
+
settledRef.current = true;
|
|
142
|
+
setRenderVersion((version) => version + 1);
|
|
143
|
+
})();
|
|
144
|
+
return () => {
|
|
145
|
+
disposed = true;
|
|
146
|
+
layerRef.current = undefined;
|
|
147
|
+
settledRef.current = false;
|
|
148
|
+
// Only work still in flight is cancelled; settled tasks were released above.
|
|
149
|
+
task?.cancel();
|
|
150
|
+
layer?.cancel();
|
|
151
|
+
canvas.width = 0;
|
|
152
|
+
canvas.height = 0;
|
|
153
|
+
text.replaceChildren();
|
|
154
|
+
};
|
|
155
|
+
}, [active, loaded, number, width, zoom]);
|
|
156
|
+
|
|
157
|
+
useEffect(() => {
|
|
158
|
+
// Clear the previous request even when the next one lands on the same page.
|
|
159
|
+
if (!focus) scrolledRequest.current = undefined;
|
|
160
|
+
if (!active || !settledRef.current) return;
|
|
161
|
+
const layer = layerRef.current;
|
|
162
|
+
// pdf.js keeps a span for every item, attached only when the item has text.
|
|
163
|
+
layer?.textDivs.forEach((div, index) => { div.textContent = layer.textContentItemsStr[index] ?? ''; });
|
|
164
|
+
if (!focus) return;
|
|
165
|
+
const mark = layer && focus.match ? highlightMatch(layer.textContentItemsStr, layer.textDivs, focus.match) : undefined;
|
|
166
|
+
// Zoom and resize rebuild the marks without taking the reader back to a
|
|
167
|
+
// passage they have already located and scrolled away from.
|
|
168
|
+
if (scrolledRequest.current !== focus.key) {
|
|
169
|
+
(mark ?? shellRef.current)?.scrollIntoView({ block: 'center' });
|
|
170
|
+
scrolledRequest.current = focus.key;
|
|
171
|
+
}
|
|
172
|
+
}, [active, renderVersion, focus]);
|
|
173
|
+
|
|
174
|
+
return (
|
|
175
|
+
<div ref={shellRef} className="astra-paper-pdf__page" data-page={number} role="group"
|
|
176
|
+
aria-label={labels.page(number)} style={{ width: width * zoom, aspectRatio }}>
|
|
177
|
+
<canvas ref={canvasRef} aria-hidden="true" />
|
|
178
|
+
<div ref={textRef} className="astra-paper-pdf__text" />
|
|
179
|
+
{failed ? <p role="alert">{labels.pageError(number)}</p> : null}
|
|
180
|
+
</div>
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** Continuous PDF reading and passage navigation; the host supplies pdf.js through `loadPdfJs`. */
|
|
185
|
+
export const PaperPdfViewer = forwardRef<HTMLDivElement, PaperPdfViewerProps>(function PaperPdfViewer(props, ref) {
|
|
186
|
+
// A new URL is a new document: geometry, text cache and requests start over.
|
|
187
|
+
return <DocumentViewer key={props.pdfUrl} {...props} ref={ref} />;
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
const DocumentViewer = forwardRef<HTMLDivElement, PaperPdfViewerProps>(function DocumentViewer({
|
|
191
|
+
pdfUrl, title, loadPdfJs, passage, onLoadStateChange, className, ...props
|
|
192
|
+
}, ref) {
|
|
193
|
+
const { pdf: labels } = useLabels();
|
|
194
|
+
// One load per mounted document, with the loader present at mount.
|
|
195
|
+
const [loader] = useState(() => loadPdfJs);
|
|
196
|
+
const [result, setResult] = useState<{ status: 'ready'; loaded: Loaded } | { status: 'error'; error: unknown }>();
|
|
197
|
+
const loaded = result?.status === 'ready' ? result.loaded : undefined;
|
|
198
|
+
const [located, setLocated] = useState<{ request: PdfPassage; focus: Focus | undefined }>();
|
|
199
|
+
const [zoom, setZoom] = useState(1);
|
|
200
|
+
const [width, setWidth] = useState(0);
|
|
201
|
+
const scrollRef = useRef<HTMLDivElement>(null);
|
|
202
|
+
const anchorRef = useRef<{ shell: HTMLElement; fraction: number } | undefined>(undefined);
|
|
203
|
+
const report = useRef(onLoadStateChange);
|
|
204
|
+
useEffect(() => { report.current = onLoadStateChange; });
|
|
205
|
+
|
|
206
|
+
const rememberPosition = useCallback(() => {
|
|
207
|
+
const scroll = scrollRef.current;
|
|
208
|
+
if (!scroll) return;
|
|
209
|
+
const top = scroll.getBoundingClientRect().top;
|
|
210
|
+
for (const shell of scroll.querySelectorAll<HTMLElement>('[data-page]')) {
|
|
211
|
+
const bounds = shell.getBoundingClientRect();
|
|
212
|
+
if (bounds.bottom > top && bounds.height > 0) {
|
|
213
|
+
anchorRef.current = { shell, fraction: (top - bounds.top) / bounds.height };
|
|
214
|
+
break;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}, []);
|
|
218
|
+
|
|
219
|
+
// Before paint, so a zoom step never flashes the page at the wrong offset.
|
|
220
|
+
useIsomorphicLayoutEffect(() => {
|
|
221
|
+
const scroll = scrollRef.current;
|
|
222
|
+
const anchor = anchorRef.current;
|
|
223
|
+
if (scroll && anchor) {
|
|
224
|
+
const bounds = anchor.shell.getBoundingClientRect();
|
|
225
|
+
scroll.scrollTop += bounds.top - scroll.getBoundingClientRect().top + bounds.height * anchor.fraction;
|
|
226
|
+
anchorRef.current = undefined;
|
|
227
|
+
}
|
|
228
|
+
}, [zoom, width]);
|
|
229
|
+
|
|
230
|
+
useEffect(() => {
|
|
231
|
+
const node = scrollRef.current;
|
|
232
|
+
if (!node) return;
|
|
233
|
+
const resize = () => { rememberPosition(); setWidth(Math.max(100, node.clientWidth - 32)); };
|
|
234
|
+
resize();
|
|
235
|
+
const observer = new ResizeObserver(resize);
|
|
236
|
+
observer.observe(node);
|
|
237
|
+
return () => { observer.disconnect(); };
|
|
238
|
+
}, [rememberPosition]);
|
|
239
|
+
|
|
240
|
+
useEffect(() => {
|
|
241
|
+
let disposed = false;
|
|
242
|
+
let task: PdfLoadingTask | undefined;
|
|
243
|
+
const release = () => {
|
|
244
|
+
const current = task;
|
|
245
|
+
task = undefined;
|
|
246
|
+
void current?.destroy().catch(() => { /* The document is already unavailable. */ });
|
|
247
|
+
};
|
|
248
|
+
void (async () => {
|
|
249
|
+
try {
|
|
250
|
+
const runtime = await loader();
|
|
251
|
+
if (disposed) return;
|
|
252
|
+
task = runtime.getDocument({ url: pdfUrl });
|
|
253
|
+
const pdf = await task.promise;
|
|
254
|
+
if (!disposed) setResult({ status: 'ready', loaded: { pdf, runtime, text: new Map() } });
|
|
255
|
+
} catch (error) {
|
|
256
|
+
release();
|
|
257
|
+
if (!disposed) setResult({ status: 'error', error });
|
|
258
|
+
}
|
|
259
|
+
})();
|
|
260
|
+
return () => {
|
|
261
|
+
disposed = true;
|
|
262
|
+
release();
|
|
263
|
+
};
|
|
264
|
+
}, [loader, pdfUrl]);
|
|
265
|
+
|
|
266
|
+
useEffect(() => {
|
|
267
|
+
report.current?.(result?.status ?? 'loading', result?.status === 'error' ? result.error : undefined);
|
|
268
|
+
}, [result]);
|
|
269
|
+
|
|
270
|
+
const key = passage?.key;
|
|
271
|
+
const quote = passage?.quote;
|
|
272
|
+
const page = passage?.page;
|
|
273
|
+
useEffect(() => {
|
|
274
|
+
if (!loaded || key === undefined) return;
|
|
275
|
+
const abort = new AbortController();
|
|
276
|
+
void locateQuote(loaded.pdf, { quote, page }, loaded.text, abort.signal).then((location) => {
|
|
277
|
+
if (!abort.signal.aborted) setLocated({ request: { key, quote, page }, focus: location && { ...location, key } });
|
|
278
|
+
});
|
|
279
|
+
return () => { abort.abort(); };
|
|
280
|
+
}, [loaded, key, quote, page]);
|
|
281
|
+
// A result answers one request; an edited quote or page under the same key is a new search.
|
|
282
|
+
const current = located && located.request.key === key && located.request.quote === quote && located.request.page === page
|
|
283
|
+
? located
|
|
284
|
+
: undefined;
|
|
285
|
+
const focus = current?.focus;
|
|
286
|
+
|
|
287
|
+
const status = result?.status === 'error' ? labels.loadError
|
|
288
|
+
: !loaded ? labels.loading
|
|
289
|
+
: key === undefined ? labels.pageCount(loaded.pdf.numPages)
|
|
290
|
+
: !current ? labels.searching
|
|
291
|
+
: !focus ? labels.quoteNotFound
|
|
292
|
+
: focus.match
|
|
293
|
+
? (focus.match.complete ? labels.quoteHighlighted : labels.partialQuoteHighlighted)(focus.page, loaded.pdf.numPages)
|
|
294
|
+
: labels.citedPageFallback(focus.page, loaded.pdf.numPages);
|
|
295
|
+
|
|
296
|
+
const zoomBy = (step: number) => { rememberPosition(); setZoom((value) => value + step); };
|
|
297
|
+
|
|
298
|
+
return (
|
|
299
|
+
<div data-slot="paper-pdf-viewer" role="group" aria-label={labels.viewer(title)} {...props} ref={ref}
|
|
300
|
+
className={cn('astra-paper-pdf', className)}>
|
|
301
|
+
<div className="astra-paper-pdf__toolbar">
|
|
302
|
+
<span role="status">{status}</span>
|
|
303
|
+
<button type="button" disabled={!loaded || zoom <= ZOOM_MIN} aria-label={labels.zoomOut}
|
|
304
|
+
onClick={() => { zoomBy(-ZOOM_STEP); }}>−</button>
|
|
305
|
+
<span>{labels.zoomLevel(Math.round(zoom * 100))}</span>
|
|
306
|
+
<button type="button" disabled={!loaded || zoom >= ZOOM_MAX} aria-label={labels.zoomIn}
|
|
307
|
+
onClick={() => { zoomBy(ZOOM_STEP); }}>+</button>
|
|
308
|
+
</div>
|
|
309
|
+
{/* Keyboard focus lets readers scroll the PDF with arrows and Page Up/Down. */}
|
|
310
|
+
{/* eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex */}
|
|
311
|
+
<div ref={scrollRef} className="astra-paper-pdf__scroll" role="region" tabIndex={0} aria-label={labels.pages}>
|
|
312
|
+
{loaded ? Array.from({ length: loaded.pdf.numPages }, (_, index) => (
|
|
313
|
+
<Page key={index} loaded={loaded} number={index + 1} width={width} zoom={zoom}
|
|
314
|
+
focus={focus?.page === index + 1 ? focus : undefined} />
|
|
315
|
+
)) : null}
|
|
316
|
+
</div>
|
|
317
|
+
</div>
|
|
318
|
+
);
|
|
319
|
+
});
|
|
@@ -15,15 +15,18 @@ import { decisionInsights, findingEvidence, informedDecisions, outputRelations }
|
|
|
15
15
|
import { useLabels } from '../lib/labels.js';
|
|
16
16
|
import type { ArtifactRenderer } from './artifact-preview.js';
|
|
17
17
|
import { DetailDialog, type DetailDialogProps, type DialogLayout } from '../primitives/dialog.js';
|
|
18
|
-
import type { SurfaceKind } from '../
|
|
19
|
-
import type { TextRenderer } from '../
|
|
18
|
+
import type { SurfaceKind } from '../model/kind.js';
|
|
19
|
+
import type { TextRenderer } from '../lib/prose.js';
|
|
20
20
|
import { DecisionDetail } from './decision-detail.js';
|
|
21
|
-
import type { DetailEntry } from '
|
|
21
|
+
import type { DetailEntry } from '../lib/detail-stack.js';
|
|
22
22
|
import { FindingDetail } from './finding-detail.js';
|
|
23
23
|
import { InputDetail } from './input-detail.js';
|
|
24
|
-
import { InsightDetail
|
|
25
|
-
import {
|
|
26
|
-
import {
|
|
24
|
+
import { InsightDetail } from './insight-detail.js';
|
|
25
|
+
import { primaryLiteratureEvidence } from '../model/papers.js';
|
|
26
|
+
import { OutputDetail, OutputDialogActions } from './output-detail.js';
|
|
27
|
+
import { useOutputExpanded } from '../lib/use-output-expanded.js';
|
|
28
|
+
import { PaperDetail, PaperDialogActions, type OpenPaperFileHandler } from './paper-detail.js';
|
|
29
|
+
import type { PdfJsLoader } from '../lib/pdf-runtime.js';
|
|
27
30
|
|
|
28
31
|
export interface RecordDialogProps extends Pick<DetailDialogProps, 'mode' | 'backText' | 'className' | 'onBack' | 'onClose'> {
|
|
29
32
|
entry: DetailEntry;
|
|
@@ -34,7 +37,8 @@ export interface RecordDialogProps extends Pick<DetailDialogProps, 'mode' | 'bac
|
|
|
34
37
|
paperMetadata?: InventoryPaperMetadataMap | undefined;
|
|
35
38
|
renderArtifact?: ArtifactRenderer | undefined;
|
|
36
39
|
renderText?: TextRenderer | undefined;
|
|
37
|
-
|
|
40
|
+
loadPdfJs?: PdfJsLoader | undefined;
|
|
41
|
+
onOpenPaperFile?: OpenPaperFileHandler | undefined;
|
|
38
42
|
onOpenArtifact?: ((output: ResolvedOutput) => void | Promise<void>) | undefined;
|
|
39
43
|
onFetchPaper?: ((doi: string) => void) | undefined;
|
|
40
44
|
/** Navigation to another record from within the detail (drill-down). */
|
|
@@ -66,7 +70,8 @@ export function RecordDialog({
|
|
|
66
70
|
paperMetadata = {},
|
|
67
71
|
renderArtifact,
|
|
68
72
|
renderText,
|
|
69
|
-
|
|
73
|
+
loadPdfJs,
|
|
74
|
+
onOpenPaperFile,
|
|
70
75
|
onOpenArtifact,
|
|
71
76
|
onFetchPaper,
|
|
72
77
|
onOpenRecord,
|
|
@@ -101,14 +106,14 @@ export function RecordDialog({
|
|
|
101
106
|
kindLabel: labels.kinds.paper,
|
|
102
107
|
title: paper.title,
|
|
103
108
|
layout: 'reader',
|
|
104
|
-
actions: <PaperDialogActions record={paper} />,
|
|
109
|
+
actions: <PaperDialogActions record={paper} onOpenPaperFile={onOpenPaperFile} />,
|
|
105
110
|
body: (
|
|
106
111
|
<PaperDetail
|
|
107
112
|
record={paper}
|
|
108
113
|
metadata={paperMetadataFor(paper.doi, paperMetadata)}
|
|
109
114
|
focusInsight={isInsight(focusRecord) ? focusRecord : undefined}
|
|
110
115
|
renderText={renderText}
|
|
111
|
-
|
|
116
|
+
loadPdfJs={loadPdfJs}
|
|
112
117
|
onFetchPaper={onFetchPaper}
|
|
113
118
|
onOpenInsight={openInsight}
|
|
114
119
|
onOpenDecision={openDecision}
|