@astra-spec/ui 0.0.3 → 0.0.5
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 +173 -20
- package/TOKENS.md +17 -8
- package/components.css +1 -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/findings-list.d.ts.map +1 -1
- package/dist/blocks/findings-list.js +2 -1
- package/dist/blocks/findings-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 +3 -13
- 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 +9 -13
- package/dist/components/index.d.ts.map +1 -1
- package/dist/components/index.js +6 -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 +3 -2
- 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 +2 -2
- package/dist/components/record-preview.d.ts.map +1 -1
- package/dist/components/record-preview.js +9 -8
- package/dist/components/record-preview.js.map +1 -1
- 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 +20 -0
- package/dist/lib/labels.d.ts.map +1 -1
- package/dist/lib/labels.js +20 -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/model/kind.d.ts +9 -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 +8 -9
- package/dist/primitives/index.d.ts.map +1 -1
- package/dist/primitives/index.js +4 -5
- package/dist/primitives/index.js.map +1 -1
- package/dist/primitives/inline-reference.d.ts +9 -0
- package/dist/primitives/inline-reference.d.ts.map +1 -0
- package/dist/primitives/inline-reference.js +14 -0
- package/dist/primitives/inline-reference.js.map +1 -0
- package/dist/primitives/kind-glyph.d.ts +8 -0
- package/dist/primitives/kind-glyph.d.ts.map +1 -0
- package/dist/primitives/kind-glyph.js +9 -0
- package/dist/primitives/kind-glyph.js.map +1 -0
- package/dist/primitives/preview-popover.d.ts +2 -2
- package/dist/primitives/preview-popover.d.ts.map +1 -1
- package/dist/primitives/preview-popover.js +1 -1
- package/dist/primitives/preview-popover.js.map +1 -1
- package/dist/primitives/prose.d.ts +1 -35
- package/dist/primitives/prose.d.ts.map +1 -1
- package/dist/primitives/prose.js +2 -58
- 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 +3 -2
- 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 +3 -2
- 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/isolate.css +7 -0
- package/package.json +15 -3
- package/primitives.css +2 -0
- package/src/blocks/decisions-list.tsx +1 -6
- package/src/blocks/findings-list.tsx +2 -1
- 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 +3 -15
- 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 +10 -30
- package/src/components/input-detail.tsx +2 -5
- package/src/components/insight-detail.tsx +20 -35
- package/src/components/insight-trigger.tsx +5 -3
- 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 +12 -17
- 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 +40 -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/model/kind.ts +33 -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 +8 -17
- package/src/primitives/inline-reference.tsx +25 -0
- package/src/primitives/kind-glyph.tsx +24 -0
- package/src/primitives/preview-popover.tsx +3 -3
- package/src/primitives/prose.tsx +1 -86
- package/src/primitives/record-list.tsx +3 -4
- package/src/primitives/relation-list.tsx +4 -7
- 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 +6 -6
- package/styles/blocks/decisions-list.css +10 -10
- package/styles/blocks/outputs-list.css +272 -45
- package/styles/blocks/papers-list.css +14 -14
- package/styles/blocks/records.css +13 -13
- package/styles/components/artifact-preview.css +21 -21
- package/styles/components/decision-detail.css +23 -23
- package/styles/components/evidence.css +4 -9
- package/styles/components/finding-detail.css +11 -11
- package/styles/components/input-detail.css +11 -11
- package/styles/components/insight-detail.css +57 -95
- package/styles/components/insight-list.css +9 -9
- package/styles/components/insight-trigger.css +5 -8
- package/styles/components/output-detail.css +74 -189
- package/styles/components/paper-detail.css +90 -109
- package/styles/components/paper-doi.css +5 -14
- package/styles/components/paper-pdf-viewer.css +120 -0
- package/styles/components/record-preview.css +444 -268
- package/styles/primitives/badge.css +2 -2
- package/styles/primitives/button.css +25 -25
- package/styles/primitives/count-heading.css +5 -5
- package/styles/primitives/detail-layout.css +25 -25
- package/styles/primitives/dialog.css +86 -86
- package/styles/primitives/empty-state.css +1 -1
- package/styles/primitives/inline-reference.css +37 -0
- package/styles/primitives/kind-glyph.css +31 -0
- package/styles/primitives/kind.css +6 -0
- package/styles/primitives/preview-popover.css +6 -11
- package/styles/primitives/prose.css +1 -1
- package/styles/primitives/record-list.css +46 -62
- package/styles/primitives/relation-list.css +20 -42
- package/styles/primitives/surface-header.css +20 -16
- package/styles/tokens.css +26 -8
- package/styles/views/inventory.css +27 -29
- 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 +0 -6
- 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/src/primitives/kind.ts +0 -19
- /package/dist/{lib → primitives}/slot.d.ts +0 -0
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import type { PdfDocument, PdfTextContent } from './pdf-runtime.js';
|
|
2
|
+
|
|
3
|
+
/** A passage to locate in a PDF: its quoted text and/or the cited one-based physical page. */
|
|
4
|
+
export interface PdfPassage {
|
|
5
|
+
/** Distinguishes requests; repeat a passage under a new key to scroll to it again. */
|
|
6
|
+
key: string;
|
|
7
|
+
quote?: string | undefined;
|
|
8
|
+
page?: number | undefined;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** The page a passage was found on; `match` is absent for a cited-page fallback. */
|
|
12
|
+
export interface PdfPassageLocation {
|
|
13
|
+
page: number;
|
|
14
|
+
match?: QuoteMatch | undefined;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface MatchOrigin {
|
|
18
|
+
itemIndex: number;
|
|
19
|
+
rawStart: number;
|
|
20
|
+
rawEnd: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Where a quote sits in a page's text runs: offsets into the folded text, each mapped back to its raw run. */
|
|
24
|
+
export interface QuoteMatch {
|
|
25
|
+
origin: MatchOrigin[];
|
|
26
|
+
start: number;
|
|
27
|
+
length: number;
|
|
28
|
+
/** False when only a shortened prefix of the quote was found. */
|
|
29
|
+
complete: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Text content per page of one open document, shared by the text layers and
|
|
34
|
+
* passage search so each page is extracted once. There is no timeout: an
|
|
35
|
+
* extraction that never settles means the worker is gone, and rendering has
|
|
36
|
+
* stopped with it.
|
|
37
|
+
*/
|
|
38
|
+
export type PdfTextCache = Map<number, Promise<PdfTextContent>>;
|
|
39
|
+
|
|
40
|
+
export function pageText(pdf: PdfDocument, page: number, cache: PdfTextCache): Promise<PdfTextContent> {
|
|
41
|
+
const cached = cache.get(page);
|
|
42
|
+
if (cached) return cached;
|
|
43
|
+
const attempt = pdf.getPage(page).then((value) => value.getTextContent());
|
|
44
|
+
cache.set(page, attempt);
|
|
45
|
+
// A failed extraction is forgotten so a later request can retry it.
|
|
46
|
+
void attempt.catch(() => cache.delete(page));
|
|
47
|
+
return attempt;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function textStrings(content: PdfTextContent): string[] {
|
|
51
|
+
return content.items.flatMap((item) => (
|
|
52
|
+
typeof item === 'object' && item !== null && 'str' in item && typeof item.str === 'string'
|
|
53
|
+
? [item.str]
|
|
54
|
+
: []
|
|
55
|
+
));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const DASHES = '-‐‑‒–—―';
|
|
59
|
+
const SINGLE_QUOTES = '‘’‚‛';
|
|
60
|
+
const DOUBLE_QUOTES = '“”„‟';
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The comparable form of one code point: compatibility forms (ligatures, full
|
|
64
|
+
* width) decompose; accents and other marks, dashes, whitespace and invisible
|
|
65
|
+
* format characters (soft hyphens, zero-width spaces and joiners, invisible
|
|
66
|
+
* operators) vanish; case folds. Extracted text then matches an authored quote
|
|
67
|
+
* whether the PDF kept, decomposed or lost its diacritics.
|
|
68
|
+
*/
|
|
69
|
+
function fold(codePoint: string): string {
|
|
70
|
+
let folded = '';
|
|
71
|
+
for (const part of codePoint.normalize('NFKD')) {
|
|
72
|
+
if (/[\s\p{M}\p{Cf}]/u.test(part) || DASHES.includes(part)) continue;
|
|
73
|
+
folded += SINGLE_QUOTES.includes(part) ? "'" : DOUBLE_QUOTES.includes(part) ? '"' : part.toLowerCase();
|
|
74
|
+
}
|
|
75
|
+
return folded;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function foldWords(quote: string): string[] {
|
|
79
|
+
const words: string[] = [];
|
|
80
|
+
let word = '';
|
|
81
|
+
for (const codePoint of quote) {
|
|
82
|
+
if (/\s/u.test(codePoint)) {
|
|
83
|
+
if (word) words.push(word);
|
|
84
|
+
word = '';
|
|
85
|
+
} else {
|
|
86
|
+
word += fold(codePoint);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (word) words.push(word);
|
|
90
|
+
return words;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Finds a quote in a page's text runs, keeping raw offsets for highlighting.
|
|
95
|
+
* The whole quote wins; otherwise the longest word prefix of at least 16
|
|
96
|
+
* characters is reported as a partial match.
|
|
97
|
+
*/
|
|
98
|
+
export function findQuoteMatch(strings: string[], quote: string): QuoteMatch | undefined {
|
|
99
|
+
let flat = '';
|
|
100
|
+
const origin: MatchOrigin[] = [];
|
|
101
|
+
strings.forEach((raw, itemIndex) => {
|
|
102
|
+
let rawStart = 0;
|
|
103
|
+
for (const codePoint of raw) {
|
|
104
|
+
const rawEnd = rawStart + codePoint.length;
|
|
105
|
+
const folded = fold(codePoint);
|
|
106
|
+
flat += folded;
|
|
107
|
+
origin.push(...Array.from({ length: folded.length }, () => ({ itemIndex, rawStart, rawEnd })));
|
|
108
|
+
// A combining mark stays with its base, so a highlight boundary never splits the glyph.
|
|
109
|
+
const base = origin.at(-1);
|
|
110
|
+
if (!folded && base?.itemIndex === itemIndex && /\p{M}/u.test(codePoint)) base.rawEnd = rawEnd;
|
|
111
|
+
rawStart = rawEnd;
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
const words = foldWords(quote);
|
|
116
|
+
const full = words.join('');
|
|
117
|
+
if (!full) return undefined;
|
|
118
|
+
const probes = [full];
|
|
119
|
+
for (let count = words.length - 1; count > 0; count -= 1) {
|
|
120
|
+
const probe = words.slice(0, count).join('');
|
|
121
|
+
if (probe.length < 16) break;
|
|
122
|
+
probes.push(probe);
|
|
123
|
+
}
|
|
124
|
+
for (const probe of probes) {
|
|
125
|
+
const start = flat.indexOf(probe);
|
|
126
|
+
if (start !== -1) return { origin, start, length: probe.length, complete: probe === full };
|
|
127
|
+
}
|
|
128
|
+
return undefined;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Wraps the matched characters of a rendered text layer in `<mark>` elements and returns the first. */
|
|
132
|
+
export function highlightMatch(strings: string[], textDivs: HTMLElement[], match: QuoteMatch): HTMLElement | undefined {
|
|
133
|
+
const ranges = new Map<number, { rawStart: number; rawEnd: number }>();
|
|
134
|
+
for (let index = match.start; index < match.start + match.length; index += 1) {
|
|
135
|
+
const item = match.origin[index];
|
|
136
|
+
if (!item) continue;
|
|
137
|
+
const range = ranges.get(item.itemIndex);
|
|
138
|
+
if (range) {
|
|
139
|
+
range.rawStart = Math.min(range.rawStart, item.rawStart);
|
|
140
|
+
range.rawEnd = Math.max(range.rawEnd, item.rawEnd);
|
|
141
|
+
} else {
|
|
142
|
+
ranges.set(item.itemIndex, { rawStart: item.rawStart, rawEnd: item.rawEnd });
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
let first: HTMLElement | undefined;
|
|
146
|
+
for (const [itemIndex, range] of ranges) {
|
|
147
|
+
const textDiv = textDivs[itemIndex];
|
|
148
|
+
if (!textDiv) continue;
|
|
149
|
+
const raw = strings[itemIndex] ?? '';
|
|
150
|
+
const mark = document.createElement('mark');
|
|
151
|
+
mark.className = 'astra-paper-pdf__match';
|
|
152
|
+
mark.textContent = raw.slice(range.rawStart, range.rawEnd);
|
|
153
|
+
textDiv.replaceChildren(raw.slice(0, range.rawStart), mark, raw.slice(range.rawEnd));
|
|
154
|
+
first ??= mark;
|
|
155
|
+
}
|
|
156
|
+
return first;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Cited page first; a complete match anywhere outranks a partial one; a valid cited page is the last resort. */
|
|
160
|
+
export async function locateQuote(
|
|
161
|
+
pdf: PdfDocument,
|
|
162
|
+
passage: Pick<PdfPassage, 'quote' | 'page'>,
|
|
163
|
+
cache: PdfTextCache,
|
|
164
|
+
signal: AbortSignal,
|
|
165
|
+
): Promise<PdfPassageLocation | undefined> {
|
|
166
|
+
const { quote, page: cited } = passage;
|
|
167
|
+
const fallback = cited !== undefined && Number.isInteger(cited) && cited >= 1 && cited <= pdf.numPages
|
|
168
|
+
? { page: cited }
|
|
169
|
+
: undefined;
|
|
170
|
+
if (!quote) return fallback;
|
|
171
|
+
const order = [...new Set([
|
|
172
|
+
...(fallback ? [fallback.page] : []),
|
|
173
|
+
...Array.from({ length: pdf.numPages }, (_, index) => index + 1),
|
|
174
|
+
])];
|
|
175
|
+
let partial: PdfPassageLocation | undefined;
|
|
176
|
+
for (const page of order) {
|
|
177
|
+
if (signal.aborted) return undefined;
|
|
178
|
+
try {
|
|
179
|
+
const strings = textStrings(await pageText(pdf, page, cache));
|
|
180
|
+
if (signal.aborted) return undefined;
|
|
181
|
+
const match = findQuoteMatch(strings, quote);
|
|
182
|
+
if (match?.complete) return { page, match };
|
|
183
|
+
if (match) partial ??= { page, match };
|
|
184
|
+
} catch {
|
|
185
|
+
// An unreadable page must not stop the search through the rest of the paper.
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return partial ?? fallback;
|
|
189
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The pdf.js surface the viewer uses, as structural types. The `pdfjs-dist`
|
|
3
|
+
* module satisfies `PdfJs`: a host's `loadPdfJs` imports the module, points
|
|
4
|
+
* `GlobalWorkerOptions.workerSrc` at the matching worker script and returns
|
|
5
|
+
* the module. Nothing here imports pdf.js or React; the components layer
|
|
6
|
+
* re-exports what hosts need.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** Always the object `getViewport()` returned; pdf.js reads more of it than these fields. Never construct one. */
|
|
10
|
+
export interface PdfViewport {
|
|
11
|
+
width: number;
|
|
12
|
+
height: number;
|
|
13
|
+
scale: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** A `getTextContent()` result. Only `str` items are read; the object goes back to pdf.js unchanged. */
|
|
17
|
+
export interface PdfTextContent {
|
|
18
|
+
items: unknown[];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface PdfRenderTask {
|
|
22
|
+
promise: Promise<void>;
|
|
23
|
+
cancel(): void;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface PdfPage {
|
|
27
|
+
getViewport(options: { scale: number }): PdfViewport;
|
|
28
|
+
getTextContent(): Promise<PdfTextContent>;
|
|
29
|
+
render(options: {
|
|
30
|
+
canvasContext: CanvasRenderingContext2D;
|
|
31
|
+
viewport: PdfViewport;
|
|
32
|
+
transform?: number[] | undefined;
|
|
33
|
+
}): PdfRenderTask;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface PdfDocument {
|
|
37
|
+
numPages: number;
|
|
38
|
+
getPage(page: number): Promise<PdfPage>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface PdfLoadingTask {
|
|
42
|
+
/** May stay pending forever once `destroy()` has been called; never await it after that. */
|
|
43
|
+
promise: Promise<PdfDocument>;
|
|
44
|
+
/** Aborts loading and releases the document together with the worker pdf.js started for it. */
|
|
45
|
+
destroy(): Promise<void>;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface PdfTextLayerOptions {
|
|
49
|
+
/** The `getTextContent()` result, passed back as pdf.js produced it. */
|
|
50
|
+
textContentSource: object;
|
|
51
|
+
container: HTMLElement;
|
|
52
|
+
viewport: PdfViewport;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* pdf.js's `TextLayer`. An ambient class rather than a construct signature
|
|
57
|
+
* because TypeScript relates class constructor parameters bivariantly, which
|
|
58
|
+
* lets the real class (whose constructor takes pdf.js's own wider types)
|
|
59
|
+
* satisfy `PdfJs`. Only its types are exported: the class has no runtime
|
|
60
|
+
* binding.
|
|
61
|
+
*/
|
|
62
|
+
declare class PdfTextLayerClass {
|
|
63
|
+
constructor(options: PdfTextLayerOptions);
|
|
64
|
+
readonly textDivs: HTMLElement[];
|
|
65
|
+
readonly textContentItemsStr: string[];
|
|
66
|
+
render(): Promise<void>;
|
|
67
|
+
cancel(): void;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export type PdfTextLayer = PdfTextLayerClass;
|
|
71
|
+
export type PdfTextLayerConstructor = typeof PdfTextLayerClass;
|
|
72
|
+
|
|
73
|
+
/** What `loadPdfJs` resolves to: the `pdfjs-dist` module, or anything shaped like it. */
|
|
74
|
+
export interface PdfJs {
|
|
75
|
+
getDocument(options: { url: string }): PdfLoadingTask;
|
|
76
|
+
TextLayer: PdfTextLayerConstructor;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Imports pdf.js in the browser and returns it with `GlobalWorkerOptions.workerSrc`
|
|
81
|
+
* set to the matching worker script; pdf.js then starts one worker per document
|
|
82
|
+
* and terminates it with the document. Import inside the callback so server
|
|
83
|
+
* rendering never touches browser APIs.
|
|
84
|
+
*/
|
|
85
|
+
export type PdfJsLoader = () => Promise<PdfJs>;
|
|
86
|
+
|
|
87
|
+
/** The extra pdf.js exports `pdfJsWithWorker` needs. */
|
|
88
|
+
export interface PdfJsWorkerModule extends PdfJs {
|
|
89
|
+
/** pdf.js only accepts its own worker objects; the helper never inspects one. */
|
|
90
|
+
getDocument(options: { url: string; worker?: never }): PdfLoadingTask;
|
|
91
|
+
PDFWorker: { fromPort(params: { port: Worker }): { destroy(): void } };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* For bundlers that emit a worker only through `new Worker(new URL(...))`:
|
|
96
|
+
* starts one worker per document with `createWorker`, lets it fail loading
|
|
97
|
+
* instead of hanging when the script cannot start, and terminates it when the
|
|
98
|
+
* viewer releases the document.
|
|
99
|
+
*/
|
|
100
|
+
export function pdfJsWithWorker(pdfjs: PdfJsWorkerModule, createWorker: () => Worker): PdfJs {
|
|
101
|
+
return {
|
|
102
|
+
TextLayer: pdfjs.TextLayer,
|
|
103
|
+
getDocument(options) {
|
|
104
|
+
const port = createWorker();
|
|
105
|
+
const worker = pdfjs.PDFWorker.fromPort({ port });
|
|
106
|
+
let task: PdfLoadingTask;
|
|
107
|
+
try {
|
|
108
|
+
task = pdfjs.getDocument({ ...options, worker: worker as never });
|
|
109
|
+
} catch (error) {
|
|
110
|
+
// pdf.js rejects some options synchronously (an unparsable URL, say); the worker must not outlive that.
|
|
111
|
+
worker.destroy();
|
|
112
|
+
port.terminate();
|
|
113
|
+
throw error;
|
|
114
|
+
}
|
|
115
|
+
const startup = new Promise<never>((_, reject) => {
|
|
116
|
+
port.addEventListener('error', () => { reject(new Error('The PDF worker could not start.')); });
|
|
117
|
+
});
|
|
118
|
+
return {
|
|
119
|
+
promise: Promise.race([task.promise, startup]),
|
|
120
|
+
async destroy() {
|
|
121
|
+
try {
|
|
122
|
+
await task.destroy();
|
|
123
|
+
} finally {
|
|
124
|
+
worker.destroy();
|
|
125
|
+
port.terminate();
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/** Host-safe artifact preview data, and pure builders (no I/O) hosts use to produce it from data they already loaded. */
|
|
2
|
+
|
|
3
|
+
export interface TablePreviewData {
|
|
4
|
+
kind: 'table';
|
|
5
|
+
headers: string[];
|
|
6
|
+
rows: (string | number | boolean | null)[][];
|
|
7
|
+
/** Exact number of rows in the source. Leave unset when the source was sampled and the total is unknown. */
|
|
8
|
+
totalRows?: number | undefined;
|
|
9
|
+
totalColumns?: number | undefined;
|
|
10
|
+
/** The preview does not show everything: rows or columns were cut, or the source itself was a sample. */
|
|
11
|
+
truncated?: boolean | undefined;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface ImagePreviewData {
|
|
15
|
+
kind: 'image';
|
|
16
|
+
url: string;
|
|
17
|
+
alt?: string | undefined;
|
|
18
|
+
width?: number | undefined;
|
|
19
|
+
height?: number | undefined;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface MetricPreviewData {
|
|
23
|
+
kind: 'metric';
|
|
24
|
+
value: number | string;
|
|
25
|
+
uncertainty?: number | string | undefined;
|
|
26
|
+
unit?: string | undefined;
|
|
27
|
+
label?: string | undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface TextPreviewData {
|
|
31
|
+
kind: 'text';
|
|
32
|
+
text: string;
|
|
33
|
+
language?: string | undefined;
|
|
34
|
+
truncated?: boolean | undefined;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface LoadingPreviewData {
|
|
38
|
+
kind: 'loading';
|
|
39
|
+
message?: string | undefined;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface UnavailablePreviewData {
|
|
43
|
+
kind: 'unavailable';
|
|
44
|
+
reason?: string | undefined;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Host-safe data that a portable component can render without reading a file. */
|
|
48
|
+
export type ArtifactPreviewData =
|
|
49
|
+
| TablePreviewData
|
|
50
|
+
| ImagePreviewData
|
|
51
|
+
| MetricPreviewData
|
|
52
|
+
| TextPreviewData
|
|
53
|
+
| LoadingPreviewData
|
|
54
|
+
| UnavailablePreviewData;
|
|
55
|
+
|
|
56
|
+
export interface DelimitedPreviewOptions {
|
|
57
|
+
delimiter?: string | undefined;
|
|
58
|
+
maxRows?: number | undefined;
|
|
59
|
+
maxColumns?: number | undefined;
|
|
60
|
+
/** Set when the source text was itself cut short by the host. */
|
|
61
|
+
sourceTruncated?: boolean | undefined;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** RFC 4180-style rows: quoted cells may hold delimiters, doubled quotes, and line breaks. Blank rows are dropped. */
|
|
65
|
+
function parseDelimited(text: string, delimiter: string): { rows: string[][]; unterminated: boolean } {
|
|
66
|
+
const rows: string[][] = [];
|
|
67
|
+
let row: string[] = [];
|
|
68
|
+
let cell = '';
|
|
69
|
+
let quoted = false;
|
|
70
|
+
const endCell = () => { row.push(cell.trim()); cell = ''; };
|
|
71
|
+
const endRow = () => {
|
|
72
|
+
endCell();
|
|
73
|
+
if (row.some((value) => value.length > 0)) rows.push(row);
|
|
74
|
+
row = [];
|
|
75
|
+
};
|
|
76
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
77
|
+
const character = text.charAt(index);
|
|
78
|
+
if (quoted) {
|
|
79
|
+
if (character !== '"') cell += character;
|
|
80
|
+
else if (text.charAt(index + 1) === '"') { cell += '"'; index += 1; } else quoted = false;
|
|
81
|
+
} else if (character === '"') quoted = true;
|
|
82
|
+
else if (character === delimiter) endCell();
|
|
83
|
+
else if (character === '\n' || character === '\r') {
|
|
84
|
+
if (character === '\r' && text.charAt(index + 1) === '\n') index += 1;
|
|
85
|
+
endRow();
|
|
86
|
+
} else cell += character;
|
|
87
|
+
}
|
|
88
|
+
endRow();
|
|
89
|
+
return { rows, unterminated: quoted };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Turns delimited text (CSV, TSV) into table preview data. */
|
|
93
|
+
export function tablePreviewFromDelimited(text: string, options: DelimitedPreviewOptions = {}): TablePreviewData {
|
|
94
|
+
const delimiter = options.delimiter ?? ',';
|
|
95
|
+
const maxRows = options.maxRows ?? 30;
|
|
96
|
+
const maxColumns = options.maxColumns ?? 30;
|
|
97
|
+
const parsed = parseDelimited(text, delimiter);
|
|
98
|
+
const [allHeaders = [], ...records] = parsed.rows;
|
|
99
|
+
// A byte-limited sample may end inside a record (or inside a quoted cell);
|
|
100
|
+
// that record is dropped, and the total row count is unknown.
|
|
101
|
+
const sampled = Boolean(options.sourceTruncated);
|
|
102
|
+
const lastComplete = /\r?\n$/.test(text) && !parsed.unterminated;
|
|
103
|
+
const body = sampled && !lastComplete ? records.slice(0, -1) : records;
|
|
104
|
+
const headers = allHeaders.slice(0, maxColumns);
|
|
105
|
+
const rows = body.slice(0, maxRows).map((cells) => cells.slice(0, maxColumns));
|
|
106
|
+
return {
|
|
107
|
+
kind: 'table',
|
|
108
|
+
headers,
|
|
109
|
+
rows,
|
|
110
|
+
...(sampled ? {} : { totalRows: body.length }),
|
|
111
|
+
totalColumns: allHeaders.length,
|
|
112
|
+
truncated: sampled || body.length > maxRows || allHeaders.length > maxColumns,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Turns an array of flat objects (e.g. parsed JSON records) into table preview data. */
|
|
117
|
+
export function tablePreviewFromRows(
|
|
118
|
+
records: readonly Record<string, unknown>[],
|
|
119
|
+
options: Pick<DelimitedPreviewOptions, 'maxRows' | 'maxColumns' | 'sourceTruncated'> = {},
|
|
120
|
+
): TablePreviewData {
|
|
121
|
+
const maxRows = options.maxRows ?? 30;
|
|
122
|
+
const maxColumns = options.maxColumns ?? 30;
|
|
123
|
+
const allHeaders = [...new Set(records.flatMap((record) => Object.keys(record)))];
|
|
124
|
+
const headers = allHeaders.slice(0, maxColumns);
|
|
125
|
+
const rows = records.slice(0, maxRows).map((record) => headers.map((header) => {
|
|
126
|
+
const value = record[header];
|
|
127
|
+
if (value == null) return null;
|
|
128
|
+
if (typeof value === 'number' || typeof value === 'boolean' || typeof value === 'string') return value;
|
|
129
|
+
return JSON.stringify(value);
|
|
130
|
+
}));
|
|
131
|
+
return {
|
|
132
|
+
kind: 'table',
|
|
133
|
+
headers,
|
|
134
|
+
rows,
|
|
135
|
+
totalRows: records.length,
|
|
136
|
+
totalColumns: allHeaders.length,
|
|
137
|
+
truncated: Boolean(options.sourceTruncated) || records.length > maxRows || allHeaders.length > maxColumns,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Reads a metric from a scalar or a `{ value, uncertainty?, unit?, label? }` object; `undefined` when the shape is not a metric. */
|
|
142
|
+
export function metricPreviewFromJson(value: unknown): MetricPreviewData | undefined {
|
|
143
|
+
if (typeof value === 'number' || typeof value === 'string') return { kind: 'metric', value };
|
|
144
|
+
if (value && typeof value === 'object' && 'value' in value) {
|
|
145
|
+
const record = value as Record<string, unknown>;
|
|
146
|
+
const metric = record.value;
|
|
147
|
+
if (typeof metric !== 'number' && typeof metric !== 'string') return undefined;
|
|
148
|
+
const result: MetricPreviewData = { kind: 'metric', value: metric };
|
|
149
|
+
if (typeof record.uncertainty === 'number' || typeof record.uncertainty === 'string') result.uncertainty = record.uncertainty;
|
|
150
|
+
if (typeof record.unit === 'string') result.unit = record.unit;
|
|
151
|
+
if (typeof record.label === 'string') result.label = record.label;
|
|
152
|
+
return result;
|
|
153
|
+
}
|
|
154
|
+
return undefined;
|
|
155
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/** Authored-prose machinery: the token parser and the built-in KaTeX renderer behind the `Prose` element. */
|
|
2
|
+
import { Fragment, type ReactNode } from 'react';
|
|
3
|
+
import { renderToString } from 'katex';
|
|
4
|
+
|
|
5
|
+
export type ProseField = 'claim' | 'rationale' | 'description' | 'notes' | 'quote';
|
|
6
|
+
|
|
7
|
+
export interface ProseContext {
|
|
8
|
+
/** Which authored field is being rendered, when known. */
|
|
9
|
+
field?: ProseField | undefined;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Host-owned rendering for authored prose (Markdown, citations, ...); replaces the built-in rendering. */
|
|
13
|
+
export type TextRenderer = (text: string, context: ProseContext) => ReactNode;
|
|
14
|
+
|
|
15
|
+
export type ProseToken =
|
|
16
|
+
| { type: 'text'; value: string }
|
|
17
|
+
| { type: 'inlineCode'; value: string }
|
|
18
|
+
| { type: 'inlineMath'; value: string }
|
|
19
|
+
| { type: 'math'; value: string };
|
|
20
|
+
|
|
21
|
+
/** String macro expansions accepted by the built-in KaTeX renderer. */
|
|
22
|
+
export type ProseMathMacros = Readonly<Record<string, string>>;
|
|
23
|
+
|
|
24
|
+
export interface ProseRenderOptions {
|
|
25
|
+
/** Host-normalized math macros, such as `{ '\\vect': '\\mathbf{#1}' }`. */
|
|
26
|
+
macros?: ProseMathMacros | undefined;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Balanced tokens: display math first, then `code`, then inline math. */
|
|
30
|
+
const PROSE_TOKEN = /(\$\$[\s\S]+?\$\$|`[^`\n]+`|\$[^$\n]+\$)/g;
|
|
31
|
+
|
|
32
|
+
/** Splits authored prose into text, inline `code`, `$inline$` and `$$display$$` math. */
|
|
33
|
+
export function parseProse(text: string): ProseToken[] {
|
|
34
|
+
const nodes: ProseToken[] = [];
|
|
35
|
+
let last = 0;
|
|
36
|
+
for (const match of text.matchAll(PROSE_TOKEN)) {
|
|
37
|
+
const index = match.index;
|
|
38
|
+
if (index > last) nodes.push({ type: 'text', value: text.slice(last, index) });
|
|
39
|
+
const token = match[0];
|
|
40
|
+
if (token.startsWith('$$')) nodes.push({ type: 'math', value: token.slice(2, -2).trim() });
|
|
41
|
+
else if (token.startsWith('`')) nodes.push({ type: 'inlineCode', value: token.slice(1, -1) });
|
|
42
|
+
else nodes.push({ type: 'inlineMath', value: token.slice(1, -1) });
|
|
43
|
+
last = index + token.length;
|
|
44
|
+
}
|
|
45
|
+
if (last < text.length) nodes.push({ type: 'text', value: text.slice(last) });
|
|
46
|
+
return nodes;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function renderMath(
|
|
50
|
+
value: string,
|
|
51
|
+
displayMode: boolean,
|
|
52
|
+
macros?: ProseMathMacros,
|
|
53
|
+
): string {
|
|
54
|
+
return renderToString(value, {
|
|
55
|
+
displayMode,
|
|
56
|
+
output: 'htmlAndMathml',
|
|
57
|
+
strict: 'ignore',
|
|
58
|
+
throwOnError: false,
|
|
59
|
+
// KaTeX may mutate its macro object for global definitions. Keep a host's
|
|
60
|
+
// shared configuration immutable across independent prose fields.
|
|
61
|
+
...(macros ? { macros: { ...macros } } : {}),
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The built-in rendering: inline `code`, and `$inline$` / `$$display$$` math
|
|
67
|
+
* typeset with KaTeX (load `katex/dist/katex.css`; the stylesheet bundles
|
|
68
|
+
* import it). Text without those markers is returned as is.
|
|
69
|
+
*/
|
|
70
|
+
export function renderProse(
|
|
71
|
+
text: string,
|
|
72
|
+
options: ProseRenderOptions = {},
|
|
73
|
+
): ReactNode {
|
|
74
|
+
if (!/[`$]/.test(text)) return text;
|
|
75
|
+
return parseProse(text).map((node, index) => {
|
|
76
|
+
switch (node.type) {
|
|
77
|
+
case 'inlineCode':
|
|
78
|
+
return <code key={index}>{node.value}</code>;
|
|
79
|
+
case 'inlineMath':
|
|
80
|
+
return <span key={index} className="astra-prose__inline-math" dangerouslySetInnerHTML={{ __html: renderMath(node.value, false, options.macros) }} />;
|
|
81
|
+
case 'math':
|
|
82
|
+
return <div key={index} className="astra-prose__display-math" dangerouslySetInnerHTML={{ __html: renderMath(node.value, true, options.macros) }} />;
|
|
83
|
+
default:
|
|
84
|
+
return <Fragment key={index}>{node.value}</Fragment>;
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
2
|
+
import type { ResolvedOutput } from '@astra-spec/sdk';
|
|
3
|
+
|
|
4
|
+
/** Full-screen state for an output, reset whenever the output changes (a controlled host is asked to reset). */
|
|
5
|
+
export function useOutputExpanded(output: ResolvedOutput, controlled?: { expanded?: boolean | undefined; onExpandedChange?: ((next: boolean) => void) | undefined }) {
|
|
6
|
+
const [internal, setInternal] = useState(false);
|
|
7
|
+
const [lastPath, setLastPath] = useState(output.canonicalPath);
|
|
8
|
+
if (lastPath !== output.canonicalPath) {
|
|
9
|
+
setLastPath(output.canonicalPath);
|
|
10
|
+
setInternal(false);
|
|
11
|
+
}
|
|
12
|
+
const isControlled = controlled?.expanded !== undefined;
|
|
13
|
+
const expanded = isControlled ? Boolean(controlled.expanded) : internal;
|
|
14
|
+
const onChange = controlled?.onExpandedChange;
|
|
15
|
+
const seenPath = useRef(output.canonicalPath);
|
|
16
|
+
const controlledExpanded = isControlled && Boolean(controlled.expanded);
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
if (seenPath.current === output.canonicalPath) return;
|
|
19
|
+
seenPath.current = output.canonicalPath;
|
|
20
|
+
if (controlledExpanded) onChange?.(false);
|
|
21
|
+
}, [output.canonicalPath, controlledExpanded, onChange]);
|
|
22
|
+
const setExpanded = useCallback((next: boolean) => {
|
|
23
|
+
if (!isControlled) setInternal(next);
|
|
24
|
+
onChange?.(next);
|
|
25
|
+
}, [isControlled, onChange]);
|
|
26
|
+
return [expanded, setExpanded] as const;
|
|
27
|
+
}
|
package/src/model/index.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
/** Model: pure, React-free derivations over the SDK's resolved analysis. */
|
|
2
|
-
export { analysisTitle, countLabel, isInsight, isVisualOutput, recordTitle, selectedOptionLabel } from './records.js';
|
|
2
|
+
export { analysisTitle, countLabel, decisionTagLabel, inputSourceLabel, isInsight, isVisualOutput, recordTitle, selectedOptionLabel } from './records.js';
|
|
3
|
+
export { sectionKind, surfaceGlyph } from './kind.js';
|
|
4
|
+
export type { InventorySectionId, SurfaceKind } from './kind.js';
|
|
3
5
|
export { locateRecord } from './locate-record.js';
|
|
4
6
|
export type { LocatedRecord } from './locate-record.js';
|
|
5
7
|
export {
|
|
@@ -21,12 +23,12 @@ export {
|
|
|
21
23
|
paperEvidence,
|
|
22
24
|
paperForDoi,
|
|
23
25
|
paperMetadataFor,
|
|
26
|
+
primaryLiteratureEvidence,
|
|
24
27
|
} from './papers.js';
|
|
25
28
|
export type {
|
|
26
29
|
InventoryPaper,
|
|
27
30
|
InventoryPaperMetadata,
|
|
28
31
|
InventoryPaperMetadataMap,
|
|
29
32
|
PaperFetchStatus,
|
|
30
|
-
PaperFocusEvidence,
|
|
31
33
|
} from './papers.js';
|
|
32
34
|
export { doiHref } from './doi.js';
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { ResolvedRecord } from '@astra-spec/sdk';
|
|
2
|
+
|
|
3
|
+
/** Record kinds plus the two non-record surfaces the UI colours. */
|
|
4
|
+
export type SurfaceKind = ResolvedRecord['kind'] | 'analysis' | 'paper';
|
|
5
|
+
|
|
6
|
+
const SURFACE_GLYPHS: Record<SurfaceKind, string> = {
|
|
7
|
+
analysis: '◐',
|
|
8
|
+
input: '▤',
|
|
9
|
+
decision: '◇',
|
|
10
|
+
output: '◆',
|
|
11
|
+
finding: '●',
|
|
12
|
+
prior_insight: '◈',
|
|
13
|
+
paper: '📄',
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/** Plain-text kind mark. Use KindGlyph for consistent, themed UI rendering. */
|
|
17
|
+
export function surfaceGlyph(kind: SurfaceKind): string {
|
|
18
|
+
return SURFACE_GLYPHS[kind];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type InventorySectionId = 'outputs' | 'decisions' | 'inputs' | 'findings' | 'prior_insights' | 'papers';
|
|
22
|
+
|
|
23
|
+
/** The record kind each inventory section lists; drives the outline glyph and colour. */
|
|
24
|
+
export function sectionKind(section: InventorySectionId): SurfaceKind {
|
|
25
|
+
switch (section) {
|
|
26
|
+
case 'outputs': return 'output';
|
|
27
|
+
case 'decisions': return 'decision';
|
|
28
|
+
case 'inputs': return 'input';
|
|
29
|
+
case 'findings': return 'finding';
|
|
30
|
+
case 'prior_insights': return 'prior_insight';
|
|
31
|
+
case 'papers': return 'paper';
|
|
32
|
+
}
|
|
33
|
+
}
|
package/src/model/papers.ts
CHANGED
|
@@ -32,13 +32,6 @@ export interface InventoryPaperMetadata {
|
|
|
32
32
|
|
|
33
33
|
export type InventoryPaperMetadataMap = Readonly<Record<string, InventoryPaperMetadata>>;
|
|
34
34
|
|
|
35
|
-
export interface PaperFocusEvidence {
|
|
36
|
-
/** Changes on every locate request, including a repeat of the same passage, so a host can react to each one. */
|
|
37
|
-
key: string;
|
|
38
|
-
insight: ResolvedInsight;
|
|
39
|
-
evidence: ResolvedEvidence;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
35
|
/** Looks up metadata by DOI, whichever spelling the host used as the key. */
|
|
43
36
|
export function paperMetadataFor(doi: string, metadata: InventoryPaperMetadataMap): InventoryPaperMetadata | undefined {
|
|
44
37
|
const direct = metadata[normalizeDoi(doi)] ?? metadata[doi];
|
|
@@ -144,3 +137,12 @@ export function findPaper(papers: readonly InventoryPaper[], doi: string): Inven
|
|
|
144
137
|
const key = normalizeDoi(doi);
|
|
145
138
|
return papers.find((paper) => normalizeDoi(paper.doi) === key);
|
|
146
139
|
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* The literature evidence an insight is presented against: the first entry
|
|
143
|
+
* with a DOI. A quote without a DOI names no paper to open, so it is skipped;
|
|
144
|
+
* the passage shown and the paper opened always belong to this one entry.
|
|
145
|
+
*/
|
|
146
|
+
export function primaryLiteratureEvidence(insight: ResolvedInsight) {
|
|
147
|
+
return insight.evidence.find((evidence) => Boolean(evidence.doi));
|
|
148
|
+
}
|