@ruckc/truss-pdf 0.0.0-stage → 1.68.0
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 +122 -2
- package/dist/PdfDialog.svelte +72 -0
- package/dist/PdfDialog.svelte.d.ts +15 -0
- package/dist/PdfReview.svelte +174 -0
- package/dist/PdfReview.svelte.d.ts +120 -0
- package/dist/PdfViewer.svelte +1184 -0
- package/dist/PdfViewer.svelte.d.ts +92 -0
- package/dist/ReviewPanel.svelte +591 -0
- package/dist/ReviewPanel.svelte.d.ts +126 -0
- package/dist/assets.d.ts +9 -0
- package/dist/assets.js +29 -0
- package/dist/bidi.d.ts +2 -0
- package/dist/bidi.js +9 -0
- package/dist/cssvars.d.ts +12 -0
- package/dist/cssvars.js +32 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +7 -0
- package/dist/labels.d.ts +6 -0
- package/dist/labels.js +23 -0
- package/dist/pages.d.ts +54 -0
- package/dist/pages.js +171 -0
- package/dist/paging.d.ts +18 -0
- package/dist/paging.js +110 -0
- package/dist/pdfjs.d.ts +40 -0
- package/dist/pdfjs.js +49 -0
- package/dist/quote-match.d.ts +30 -0
- package/dist/quote-match.js +145 -0
- package/dist/types.d.ts +142 -0
- package/dist/types.js +1 -0
- package/package.json +54 -5
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
export type PdfViewerLabels = {
|
|
2
|
+
/** Accessible name of the page area when no `title` is given. */
|
|
3
|
+
document: string;
|
|
4
|
+
/** Accessible name of the toolbar. */
|
|
5
|
+
toolbar: string;
|
|
6
|
+
previous: string;
|
|
7
|
+
next: string;
|
|
8
|
+
/** Accessible name of the page number box. */
|
|
9
|
+
pageNumber: string;
|
|
10
|
+
/** After the page number box. */
|
|
11
|
+
of: (total: number) => string;
|
|
12
|
+
/** Announced on a page turn, and part of the page area's name. */
|
|
13
|
+
pageStatus: (page: number, total: number) => string;
|
|
14
|
+
/** Accessible name of the zoom switch. */
|
|
15
|
+
zoom: string;
|
|
16
|
+
fitPage: string;
|
|
17
|
+
fitWidth: string;
|
|
18
|
+
loading: string;
|
|
19
|
+
rendering: string;
|
|
20
|
+
loadFailed: string;
|
|
21
|
+
renderFailed: string;
|
|
22
|
+
retry: string;
|
|
23
|
+
/** The draw-a-box tool (shown with `oncreate`). */
|
|
24
|
+
drawBox: string;
|
|
25
|
+
/** Creates an annotation from the selected text (shown with `oncreate`). */
|
|
26
|
+
annotateSelection: string;
|
|
27
|
+
/** Accessible name of an annotation without a label (its 1-based position on the page). */
|
|
28
|
+
annotation: (n: number) => string;
|
|
29
|
+
quoteFound: string;
|
|
30
|
+
quotePartial: string;
|
|
31
|
+
quoteMissing: string;
|
|
32
|
+
regionFound: string;
|
|
33
|
+
regionPartial: string;
|
|
34
|
+
regionMissing: string;
|
|
35
|
+
/** Accessible name of a link of the document to a web address. */
|
|
36
|
+
link: (url: string) => string;
|
|
37
|
+
/** Accessible name of a link of the document to one of its pages. */
|
|
38
|
+
goToPage: (page: number) => string;
|
|
39
|
+
};
|
|
40
|
+
import { type Snippet } from 'svelte';
|
|
41
|
+
import type { HTMLAttributes } from 'svelte/elements';
|
|
42
|
+
import type { PdfAnnotation, PdfCreateDetail, PdfGoToResult, PdfSource, PdfTarget } from './types.js';
|
|
43
|
+
type Mode = 'page' | 'fit-width';
|
|
44
|
+
type $$ComponentProps = Omit<HTMLAttributes<HTMLDivElement>, 'children' | 'class' | 'title'> & {
|
|
45
|
+
/** The document: a URL, or its bytes (`ArrayBuffer` or `Uint8Array`, copied before pdf.js takes them). Nothing loads until this is set. */
|
|
46
|
+
src?: PdfSource | null;
|
|
47
|
+
/** The 1-based page on screen. Bindable. */
|
|
48
|
+
page?: number;
|
|
49
|
+
/** The document's page count once loaded (0 before). Bindable, read-only. */
|
|
50
|
+
pageCount?: number;
|
|
51
|
+
/** `page` fits the whole page; `fit-width` fits the width and scrolls a tall page. Bindable. */
|
|
52
|
+
mode?: Mode;
|
|
53
|
+
/** Overlays drawn over the pages (never written into the PDF). Update the array to change them; selection is kept by id. */
|
|
54
|
+
annotations?: PdfAnnotation[];
|
|
55
|
+
/** The id of the emphasized annotation. Bindable: a click or Enter on an annotation sets it. */
|
|
56
|
+
selected?: string | null;
|
|
57
|
+
/** Called with an annotation's id when the user selects it (click, Enter or Space). */
|
|
58
|
+
onselect?: (id: string) => void;
|
|
59
|
+
/** Turns on creating annotations: a draw-a-box tool and an "annotate the selected text" button. Called with what was drawn or selected. */
|
|
60
|
+
oncreate?: (detail: PdfCreateDetail) => void;
|
|
61
|
+
/** Where to go: applied when the document has loaded, and again when `target` or `targetKey` changes. */
|
|
62
|
+
target?: PdfTarget | null;
|
|
63
|
+
/** Change it to apply the same `target` again (e.g. a counter). */
|
|
64
|
+
targetKey?: unknown;
|
|
65
|
+
/** Called with the outcome of each `target` (and `goTo`), so the host can say "quote not found on page 4". */
|
|
66
|
+
ontarget?: (result: PdfGoToResult, target: PdfTarget) => void;
|
|
67
|
+
/**
|
|
68
|
+
* Called when the user activates a link of the document to a web address, instead of opening it, so the host can ask first. Without it,
|
|
69
|
+
* http(s) and mailto links open in a new tab with `rel="noopener noreferrer"`; other schemes are never clickable.
|
|
70
|
+
*/
|
|
71
|
+
onlink?: (url: string) => void;
|
|
72
|
+
/** The document's name, for the page area's accessible name (bidi-isolated). */
|
|
73
|
+
title?: string;
|
|
74
|
+
/** CSS height (px when a number). Default: 32rem; `100%` fills a sized parent. */
|
|
75
|
+
height?: number | string;
|
|
76
|
+
/** Show the toolbar (paging, page number, zoom, create tools). */
|
|
77
|
+
toolbar?: boolean;
|
|
78
|
+
/** Extra controls at the end of the toolbar (a download button, a close button). */
|
|
79
|
+
actions?: Snippet;
|
|
80
|
+
labels?: Partial<PdfViewerLabels>;
|
|
81
|
+
class?: string;
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* An inline PDF viewer built on pdf.js, with hardened defaults for untrusted documents: one page at a time, fitted to the page or to the
|
|
85
|
+
* width, turned by whole pages (buttons, PgUp/PgDn, arrows, Home/End, wheel, trackpad and swipe), a selectable text layer, jumps to a page,
|
|
86
|
+
* region, quote or destination, and host-supplied annotation overlays. Adapted from EvalBot's DocumentViewer.
|
|
87
|
+
*/
|
|
88
|
+
declare const PdfViewer: import("svelte").Component<$$ComponentProps, {
|
|
89
|
+
goTo: (t: PdfTarget) => Promise<PdfGoToResult>;
|
|
90
|
+
}, "selected" | "mode" | "page" | "pageCount">;
|
|
91
|
+
type PdfViewer = ReturnType<typeof PdfViewer>;
|
|
92
|
+
export default PdfViewer;
|
|
@@ -0,0 +1,591 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
@component
|
|
3
|
+
A review list for a document: items grouped by page (and a "Document" group for the rest), filter segments with counts, previous / next
|
|
4
|
+
with an "n of m" indicator and the `j`/`k` and `[`/`]` keys, host-defined actions with an optional confirmation and note, auto-advance when
|
|
5
|
+
an action takes the item out of the filter, a detail snippet for the selected item and optional bulk selection. It drives a `PdfViewer`
|
|
6
|
+
through `viewer` (its `goTo`) and `bind:selected` (the annotation shown as selected); `PdfReview` lays the two out side by side.
|
|
7
|
+
-->
|
|
8
|
+
<script module lang="ts">
|
|
9
|
+
export type ReviewPanelLabels = {
|
|
10
|
+
/** Accessible name of the panel. */
|
|
11
|
+
panel: string;
|
|
12
|
+
/** Accessible name of the filter segments. */
|
|
13
|
+
filters: string;
|
|
14
|
+
/** The default filter, when the host defines none. */
|
|
15
|
+
all: string;
|
|
16
|
+
/** A filter segment: its label and the number of items it matches. */
|
|
17
|
+
filterOption: (label: string, count: number) => string;
|
|
18
|
+
/** Accessible name of the item list. */
|
|
19
|
+
list: string;
|
|
20
|
+
/** Heading of the items without a page. */
|
|
21
|
+
document: string;
|
|
22
|
+
/** Heading of a page's items. */
|
|
23
|
+
page: (page: number) => string;
|
|
24
|
+
/** Accessible name of the previous / next group. */
|
|
25
|
+
navigation: string;
|
|
26
|
+
previous: string;
|
|
27
|
+
next: string;
|
|
28
|
+
/** The indicator while an item is selected. */
|
|
29
|
+
position: (n: number, total: number) => string;
|
|
30
|
+
/** The indicator while none is. */
|
|
31
|
+
count: (total: number) => string;
|
|
32
|
+
/** Announced when an item is selected; `title` is bidi-isolated. */
|
|
33
|
+
announce: (n: number, total: number, title: string) => string;
|
|
34
|
+
/** Accessible name of the selected item's actions. */
|
|
35
|
+
actions: string;
|
|
36
|
+
/** Announced when an action has run (a sentence: the next item may follow it); `target` is the item's title or the bulk count. */
|
|
37
|
+
done: (action: string, target: string) => string;
|
|
38
|
+
/** The toast (and announcement) when an action fails. */
|
|
39
|
+
failed: (action: string, error: string) => string;
|
|
40
|
+
/** Announced when the last item of the view has gone. */
|
|
41
|
+
cleared: string;
|
|
42
|
+
/** Shown without any items. */
|
|
43
|
+
empty: string;
|
|
44
|
+
/** Shown when the filter matches no item. */
|
|
45
|
+
emptyFilter: string;
|
|
46
|
+
/** The dialog's dismissing button. */
|
|
47
|
+
cancel: string;
|
|
48
|
+
/** The note field of a confirmation. */
|
|
49
|
+
note: string;
|
|
50
|
+
/** Accessible name of an item's bulk checkbox; `title` is bidi-isolated. */
|
|
51
|
+
check: (title: string) => string;
|
|
52
|
+
/** Accessible name of the check-all box. */
|
|
53
|
+
checkAll: string;
|
|
54
|
+
/** The bulk bar's count. */
|
|
55
|
+
checked: (n: number) => string;
|
|
56
|
+
/** Accessible name of the bulk bar. */
|
|
57
|
+
bulk: string;
|
|
58
|
+
/** Clears the bulk selection. */
|
|
59
|
+
uncheck: string;
|
|
60
|
+
};
|
|
61
|
+
const DEFAULTS: ReviewPanelLabels = {
|
|
62
|
+
panel: 'Review',
|
|
63
|
+
filters: 'Filter',
|
|
64
|
+
all: 'All',
|
|
65
|
+
filterOption: (label, count) => `${label} (${count})`,
|
|
66
|
+
list: 'Items',
|
|
67
|
+
document: 'Document',
|
|
68
|
+
page: (page) => `Page ${page}`,
|
|
69
|
+
navigation: 'Review navigation',
|
|
70
|
+
previous: 'Previous item',
|
|
71
|
+
next: 'Next item',
|
|
72
|
+
position: (n, total) => `${n} of ${total}`,
|
|
73
|
+
count: (total) => `${total} ${total === 1 ? 'item' : 'items'}`,
|
|
74
|
+
announce: (n, total, title) => `${n} of ${total}: ${title}`,
|
|
75
|
+
actions: 'Actions',
|
|
76
|
+
done: (action, target) => `${action}: ${target}.`,
|
|
77
|
+
failed: (action, error) => `${action} failed: ${error}`,
|
|
78
|
+
cleared: 'No items left in this view',
|
|
79
|
+
empty: 'Nothing to review',
|
|
80
|
+
emptyFilter: 'No items match this filter',
|
|
81
|
+
cancel: 'Cancel',
|
|
82
|
+
note: 'Note (optional)',
|
|
83
|
+
check: (title) => `Select ${title}`,
|
|
84
|
+
checkAll: 'Select all',
|
|
85
|
+
checked: (n) => `${n} selected`,
|
|
86
|
+
bulk: 'Bulk actions',
|
|
87
|
+
uncheck: 'Clear selection'
|
|
88
|
+
};
|
|
89
|
+
</script>
|
|
90
|
+
|
|
91
|
+
<script lang="ts" generics="T extends ReviewItem">
|
|
92
|
+
import { tick, untrack, type Snippet } from 'svelte';
|
|
93
|
+
import type { HTMLAttributes } from 'svelte/elements';
|
|
94
|
+
import { Button, Checkbox, Dialog, EmptyState, Kbd, SegmentedControl, Tag, Textarea, toasts, matches, ariaKeyShortcuts } from '@ruckc/truss-ui';
|
|
95
|
+
import { useLabels } from './labels.js';
|
|
96
|
+
import { isolate } from './bidi.js';
|
|
97
|
+
import type { PdfGoToResult, PdfTarget, ReviewAction, ReviewActionDetail, ReviewConfirm, ReviewFilter, ReviewItem, ReviewStatuses, ReviewViewer } from './types.js';
|
|
98
|
+
|
|
99
|
+
let {
|
|
100
|
+
items,
|
|
101
|
+
filters,
|
|
102
|
+
filter = $bindable(),
|
|
103
|
+
current = $bindable(null),
|
|
104
|
+
selected = $bindable(null),
|
|
105
|
+
viewer,
|
|
106
|
+
actions = [],
|
|
107
|
+
onaction,
|
|
108
|
+
onbulkaction,
|
|
109
|
+
onnavigate,
|
|
110
|
+
statuses = {},
|
|
111
|
+
bulk = false,
|
|
112
|
+
checked = $bindable([]),
|
|
113
|
+
detail,
|
|
114
|
+
keys = true,
|
|
115
|
+
labels,
|
|
116
|
+
class: klass,
|
|
117
|
+
...rest
|
|
118
|
+
}: Omit<HTMLAttributes<HTMLDivElement>, 'children' | 'class'> & {
|
|
119
|
+
/** What to review. Update the array (e.g. a new status after an action): the selection follows the ids. */
|
|
120
|
+
items: T[];
|
|
121
|
+
/** The filter segments, each with its count. Default: one "All" segment. */
|
|
122
|
+
filters?: ReviewFilter<T>[];
|
|
123
|
+
/** The id of the active filter. Bindable. Default: the first. */
|
|
124
|
+
filter?: string;
|
|
125
|
+
/** The id of the selected item. Bindable: setting it moves the viewer there. */
|
|
126
|
+
current?: string | null;
|
|
127
|
+
/** The id of the annotation shown as selected. Bindable: bind it to the viewer's `selected` too, so a click on an annotation selects its item. */
|
|
128
|
+
selected?: string | null;
|
|
129
|
+
/** The viewer to move (`PdfViewer`, `PdfDialog`, or anything with its `goTo`). */
|
|
130
|
+
viewer?: ReviewViewer;
|
|
131
|
+
/** The actions offered on the selected item. */
|
|
132
|
+
actions?: ReviewAction<T>[];
|
|
133
|
+
/** Runs an action. May return a promise: the panel is busy until it settles, and a rejection shows a toast. Then update `items`. */
|
|
134
|
+
onaction?: (action: string, item: T, detail: ReviewActionDetail) => unknown;
|
|
135
|
+
/** Runs a bulk action on the checked items it applies to (with `bulk`). */
|
|
136
|
+
onbulkaction?: (action: string, items: T[], detail: ReviewActionDetail) => unknown;
|
|
137
|
+
/** Called after each move with the item and the outcome of the viewer's `goTo`. */
|
|
138
|
+
onnavigate?: (item: T, result: PdfGoToResult) => void;
|
|
139
|
+
/** Label and tone of each status. */
|
|
140
|
+
statuses?: ReviewStatuses;
|
|
141
|
+
/** Checkboxes on the items and a bulk bar for the actions marked `bulk`. */
|
|
142
|
+
bulk?: boolean;
|
|
143
|
+
/** The ids of the checked items. Bindable. */
|
|
144
|
+
checked?: string[];
|
|
145
|
+
/** Host content for the selected item (an excerpt, notes, an analysis). */
|
|
146
|
+
detail?: Snippet<[T]>;
|
|
147
|
+
/** Handle the keys pressed inside the panel. `PdfReview` turns it off and passes the keys of the whole review (`handleKey`). */
|
|
148
|
+
keys?: boolean;
|
|
149
|
+
labels?: Partial<ReviewPanelLabels>;
|
|
150
|
+
class?: string;
|
|
151
|
+
} = $props();
|
|
152
|
+
|
|
153
|
+
const L = useLabels('ReviewPanel', DEFAULTS, () => labels);
|
|
154
|
+
const uid = $props.id();
|
|
155
|
+
|
|
156
|
+
// ───── filters and the list (its order is the navigation order: the Document group, then the pages)
|
|
157
|
+
const fs = $derived<ReviewFilter<T>[]>(filters?.length ? filters : [{ id: 'all', label: L.all, match: () => true }]);
|
|
158
|
+
const active = $derived(fs.find((f) => f.id === filter) ?? fs[0]);
|
|
159
|
+
const counts = $derived(fs.map((f) => items.filter(f.match).length));
|
|
160
|
+
const pageOf = (it: ReviewItem) => (Number.isInteger(it.page) && it.page! > 0 ? it.page! : 0);
|
|
161
|
+
const groups = $derived.by(() => {
|
|
162
|
+
const by = new Map<number, T[]>();
|
|
163
|
+
for (const it of items) if (active.match(it)) by.set(pageOf(it), [...(by.get(pageOf(it)) ?? []), it]);
|
|
164
|
+
return [...by].sort((a, b) => a[0] - b[0]).map(([page, list]) => ({ page, list }));
|
|
165
|
+
});
|
|
166
|
+
const order = $derived(groups.flatMap((g) => g.list));
|
|
167
|
+
const index = $derived(order.findIndex((it) => it.id === current));
|
|
168
|
+
const item = $derived(order[index]);
|
|
169
|
+
const offered = $derived(item ? actions.filter((a) => !a.applies || a.applies(item)) : []);
|
|
170
|
+
|
|
171
|
+
let root = $state<HTMLDivElement>();
|
|
172
|
+
let listEl = $state<HTMLDivElement>();
|
|
173
|
+
let live = $state('');
|
|
174
|
+
/** Where the selected item was in the list, so an item that leaves it is replaced by the one that takes its place. */
|
|
175
|
+
let pos = 0;
|
|
176
|
+
|
|
177
|
+
function moveTo(it: T | undefined) {
|
|
178
|
+
if (!it) {
|
|
179
|
+
current = null;
|
|
180
|
+
selected = null;
|
|
181
|
+
live = L.cleared;
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
pos = order.indexOf(it);
|
|
185
|
+
current = it.id;
|
|
186
|
+
}
|
|
187
|
+
const step = (d: number) => moveTo(order[Math.min(Math.max(index + d, 0), order.length - 1)]);
|
|
188
|
+
/** Selects the next item in the list. */
|
|
189
|
+
export const next = () => step(1);
|
|
190
|
+
/** Selects the previous item in the list. */
|
|
191
|
+
export const previous = () => step(-1);
|
|
192
|
+
|
|
193
|
+
function pickFilter(id: string) {
|
|
194
|
+
filter = id;
|
|
195
|
+
if (index < 0) moveTo(order[0]);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// auto-advance: when the selected item leaves the view (an action changed its status), the next one takes its place, else the previous
|
|
199
|
+
$effect(() => {
|
|
200
|
+
const list = order;
|
|
201
|
+
const id = untrack(() => current);
|
|
202
|
+
if (id === null) return;
|
|
203
|
+
const i = list.findIndex((it) => it.id === id);
|
|
204
|
+
if (i >= 0) return void (pos = i);
|
|
205
|
+
untrack(() => moveTo(list[Math.min(pos, list.length - 1)]));
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
// a new selection (from the list, the keys, auto-advance or the host) moves the viewer there and is announced
|
|
209
|
+
let shown: string | null = null;
|
|
210
|
+
let shownOn: ReviewViewer | undefined;
|
|
211
|
+
let fromViewer = false;
|
|
212
|
+
$effect(() => {
|
|
213
|
+
const id = current;
|
|
214
|
+
const v = viewer;
|
|
215
|
+
if (id === shown && v === shownOn) return;
|
|
216
|
+
shown = id;
|
|
217
|
+
shownOn = v;
|
|
218
|
+
untrack(() => {
|
|
219
|
+
const it = items.find((x) => x.id === id);
|
|
220
|
+
if (!it) return;
|
|
221
|
+
const n = order.indexOf(it) + 1;
|
|
222
|
+
live = L.announce(n, order.length, isolate(it.title));
|
|
223
|
+
void tick().then(() => listEl?.querySelector('[aria-current="true"]')?.scrollIntoView({ block: 'nearest' }));
|
|
224
|
+
if (fromViewer) return void (fromViewer = false);
|
|
225
|
+
selected = it.annotation ?? null;
|
|
226
|
+
const t: PdfTarget | null = it.annotation ? { annotation: it.annotation } : (it.target ?? (pageOf(it) ? { page: pageOf(it) } : null));
|
|
227
|
+
if (t && v) void v.goTo(t).then((r) => onnavigate?.(it, r));
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
// an annotation selected in the viewer (a click) selects its item
|
|
232
|
+
$effect(() => {
|
|
233
|
+
const a = selected;
|
|
234
|
+
untrack(() => {
|
|
235
|
+
const it = order.find((x) => x.annotation === a);
|
|
236
|
+
if (!a || !it || it.id === current) return;
|
|
237
|
+
fromViewer = true;
|
|
238
|
+
moveTo(it);
|
|
239
|
+
});
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
// ───── actions
|
|
243
|
+
let busy = $state<string | null>(null);
|
|
244
|
+
type Asking = ReviewConfirm & { action: ReviewAction<T>; value: string };
|
|
245
|
+
/** The last confirmation asked (kept while its dialog closes; the dialog is mounted from the first one on). */
|
|
246
|
+
let asking = $state<Asking | null>(null);
|
|
247
|
+
let open = $state(false);
|
|
248
|
+
/** Answers the confirmation on screen; null once it is answered. */
|
|
249
|
+
let settle: ((note: string | null) => void) | null = null;
|
|
250
|
+
const noteSpec = $derived(typeof asking?.note === 'object' ? asking.note : {});
|
|
251
|
+
|
|
252
|
+
function ask(spec: ReviewConfirm, action: ReviewAction<T>): Promise<string | null> {
|
|
253
|
+
return new Promise((resolve) => {
|
|
254
|
+
asking = { ...spec, action, value: (typeof spec.note === 'object' && spec.note.value) || '' };
|
|
255
|
+
settle = resolve;
|
|
256
|
+
open = true;
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
// the buttons, and any other close (Escape, the close button, a backdrop click) as a cancel; the dialog's own `close` follows a button
|
|
260
|
+
function answer(ok: boolean) {
|
|
261
|
+
const r = settle;
|
|
262
|
+
settle = null;
|
|
263
|
+
open = false;
|
|
264
|
+
r?.(ok ? asking!.value : null);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
const message = (e: unknown) => (e instanceof Error ? e.message : String(e));
|
|
268
|
+
|
|
269
|
+
// the keys and the buttons are off while an action runs, so one action runs at a time
|
|
270
|
+
async function run(action: ReviewAction<T>, targets: T[], many: boolean) {
|
|
271
|
+
const spec = many ? action.confirmBulk?.(targets) : action.confirm?.(targets[0]);
|
|
272
|
+
let note: string | undefined;
|
|
273
|
+
if (spec) {
|
|
274
|
+
const answered = await ask(spec, action);
|
|
275
|
+
if (answered === null) return;
|
|
276
|
+
note = spec.note ? answered : undefined;
|
|
277
|
+
}
|
|
278
|
+
busy = action.id;
|
|
279
|
+
const was = current;
|
|
280
|
+
try {
|
|
281
|
+
await (many ? onbulkaction?.(action.id, targets, { note }) : onaction?.(action.id, targets[0], { note }));
|
|
282
|
+
await tick();
|
|
283
|
+
// the outcome, then the item that took the place of this one (when the host's update moved the selection)
|
|
284
|
+
const done = L.done(action.label, many ? L.checked(targets.length) : isolate(targets[0].title));
|
|
285
|
+
live = current === was ? done : `${done} ${live}`;
|
|
286
|
+
if (many) checked = [];
|
|
287
|
+
} catch (e) {
|
|
288
|
+
live = L.failed(action.label, message(e));
|
|
289
|
+
toasts.danger(live);
|
|
290
|
+
} finally {
|
|
291
|
+
busy = null;
|
|
292
|
+
}
|
|
293
|
+
// the button pressed may be gone with its item: keep the keys working inside the panel
|
|
294
|
+
await tick();
|
|
295
|
+
if (!root!.contains(document.activeElement)) (listEl?.querySelector<HTMLElement>('[aria-current="true"]') ?? root)?.focus();
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// ───── bulk selection
|
|
299
|
+
const checkedItems = $derived(order.filter((it) => checked.includes(it.id)));
|
|
300
|
+
const bulkActions = $derived(actions.filter((a) => a.bulk).map((a) => ({ a, targets: checkedItems.filter((it) => !a.applies || a.applies(it)) })));
|
|
301
|
+
function check(id: string, on: boolean) {
|
|
302
|
+
checked = on ? [...checked, id] : checked.filter((x) => x !== id);
|
|
303
|
+
}
|
|
304
|
+
function checkAll(on: boolean) {
|
|
305
|
+
checked = on ? order.map((it) => it.id) : [];
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// ───── keys
|
|
309
|
+
const typing = (t: EventTarget | null) =>
|
|
310
|
+
t instanceof HTMLElement && (t.isContentEditable || !!t.closest('textarea, select, dialog, [role="textbox"]') || (t instanceof HTMLInputElement && !['checkbox', 'radio'].includes(t.type)));
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Handles a key of the review: `j` or `]` selects the next item, `k` or `[` the previous one, and an action's `shortcut` runs it.
|
|
314
|
+
* Keys typed in a text field, a contenteditable or a dialog are left alone. Returns whether the key was used (it is then `preventDefault`ed).
|
|
315
|
+
*/
|
|
316
|
+
export function handleKey(e: KeyboardEvent): boolean {
|
|
317
|
+
if (e.defaultPrevented || busy || typing(e.target)) return false;
|
|
318
|
+
const d = matches(e, 'j') || matches(e, ']') ? 1 : matches(e, 'k') || matches(e, '[') ? -1 : 0;
|
|
319
|
+
const a = offered.find((x) => x.shortcut && matches(e, x.shortcut));
|
|
320
|
+
if (d) step(d);
|
|
321
|
+
else if (a) void run(a, [item], false);
|
|
322
|
+
else return false;
|
|
323
|
+
e.preventDefault();
|
|
324
|
+
return true;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
const statusOf = (s: string) => statuses[s] ?? { label: s, tone: 'neutral' as const };
|
|
328
|
+
const intentOf = (t: string | undefined) => (t === 'danger' || t === 'warning' ? t : 'primary');
|
|
329
|
+
</script>
|
|
330
|
+
|
|
331
|
+
<!-- svelte-ignore a11y_no_static_element_interactions -->
|
|
332
|
+
<div
|
|
333
|
+
{...rest}
|
|
334
|
+
bind:this={root}
|
|
335
|
+
class={['tr-review', klass]}
|
|
336
|
+
role="region"
|
|
337
|
+
aria-label={L.panel}
|
|
338
|
+
aria-busy={!!busy || undefined}
|
|
339
|
+
tabindex="-1"
|
|
340
|
+
onkeydown={(e) => keys && handleKey(e)}
|
|
341
|
+
>
|
|
342
|
+
<div class="head">
|
|
343
|
+
<SegmentedControl size="sm" label={L.filters} value={active.id} onchange={pickFilter} options={fs.map((f, i) => ({ value: f.id, label: L.filterOption(f.label, counts[i]) }))} />
|
|
344
|
+
<div class="nav" role="group" aria-label={L.navigation}>
|
|
345
|
+
<Button size="sm" variant="outlined" icon="chevron-up" aria-label={L.previous} aria-keyshortcuts="K [" disabled={!order.length || index === 0} onclick={previous} />
|
|
346
|
+
<span class="pos">{index >= 0 ? L.position(index + 1, order.length) : L.count(order.length)}</span>
|
|
347
|
+
<Button size="sm" variant="outlined" icon="chevron-down" aria-label={L.next} aria-keyshortcuts="J ]" disabled={!order.length || index === order.length - 1} onclick={next} />
|
|
348
|
+
</div>
|
|
349
|
+
</div>
|
|
350
|
+
|
|
351
|
+
{#if bulk && order.length}
|
|
352
|
+
<div class="bulk" role="group" aria-label={L.bulk}>
|
|
353
|
+
<Checkbox aria-label={L.checkAll} checked={checkedItems.length === order.length} indeterminate={checkedItems.length > 0 && checkedItems.length < order.length} oncheckedchange={checkAll} />
|
|
354
|
+
<span class="pos">{L.checked(checkedItems.length)}</span>
|
|
355
|
+
{#if checkedItems.length}
|
|
356
|
+
{#each bulkActions as { a, targets } (a.id)}
|
|
357
|
+
<Button size="sm" variant="tonal" intent={a.tone ?? 'primary'} disabled={!targets.length || !!busy} loading={busy === a.id} onclick={() => run(a, targets, true)}>{a.label}</Button>
|
|
358
|
+
{/each}
|
|
359
|
+
<Button size="sm" variant="minimal" onclick={() => checkAll(false)}>{L.uncheck}</Button>
|
|
360
|
+
{/if}
|
|
361
|
+
</div>
|
|
362
|
+
{/if}
|
|
363
|
+
|
|
364
|
+
<div class="list" bind:this={listEl}>
|
|
365
|
+
{#if !order.length}
|
|
366
|
+
<EmptyState icon="check" title={items.length ? L.emptyFilter : L.empty} />
|
|
367
|
+
{/if}
|
|
368
|
+
{#each groups as g (g.page)}
|
|
369
|
+
<div class="group">
|
|
370
|
+
<h3 id="{uid}-g{g.page}">{g.page ? L.page(g.page) : L.document}</h3>
|
|
371
|
+
<ul aria-labelledby="{uid}-g{g.page}">
|
|
372
|
+
{#each g.list as it (it.id)}
|
|
373
|
+
{@const st = statusOf(it.status)}
|
|
374
|
+
<li data-tone={it.tone ?? 'warning'} data-item-id={it.id}>
|
|
375
|
+
{#if bulk}
|
|
376
|
+
<Checkbox aria-label={L.check(isolate(it.title))} checked={checked.includes(it.id)} oncheckedchange={(on) => check(it.id, on)} />
|
|
377
|
+
{/if}
|
|
378
|
+
<button type="button" class="item" aria-current={it.id === current || undefined} onclick={() => moveTo(it)}>
|
|
379
|
+
<span class="dot" aria-hidden="true"></span>
|
|
380
|
+
<span class="text">
|
|
381
|
+
<span class="title"><bdi>{it.title}</bdi></span>
|
|
382
|
+
{#if it.description}<span class="desc"><bdi>{it.description}</bdi></span>{/if}
|
|
383
|
+
</span>
|
|
384
|
+
<Tag size="sm" intent={st.tone ?? 'neutral'}><bdi>{st.label}</bdi></Tag>
|
|
385
|
+
</button>
|
|
386
|
+
</li>
|
|
387
|
+
{/each}
|
|
388
|
+
</ul>
|
|
389
|
+
</div>
|
|
390
|
+
{/each}
|
|
391
|
+
</div>
|
|
392
|
+
|
|
393
|
+
{#if item}
|
|
394
|
+
<div class="current">
|
|
395
|
+
{#if offered.length}
|
|
396
|
+
<div class="acts" role="group" aria-label={L.actions}>
|
|
397
|
+
{#each offered as a (a.id)}
|
|
398
|
+
<Button size="sm" variant={a.tone === 'neutral' ? 'outlined' : 'tonal'} intent={a.tone ?? 'primary'} disabled={!!busy} loading={busy === a.id} aria-keyshortcuts={a.shortcut ? ariaKeyShortcuts(a.shortcut) : undefined} onclick={() => run(a, [item], false)}>
|
|
399
|
+
{a.label}{#if a.shortcut}<span class="kbd" aria-hidden="true"><Kbd keys={a.shortcut} /></span>{/if}
|
|
400
|
+
</Button>
|
|
401
|
+
{/each}
|
|
402
|
+
</div>
|
|
403
|
+
{/if}
|
|
404
|
+
{#if detail}<div class="detail">{@render detail(item)}</div>{/if}
|
|
405
|
+
</div>
|
|
406
|
+
{/if}
|
|
407
|
+
|
|
408
|
+
<div class="sr" role="status" aria-live="polite" aria-atomic="true">{live}</div>
|
|
409
|
+
</div>
|
|
410
|
+
|
|
411
|
+
{#if asking}
|
|
412
|
+
<Dialog bind:open title={asking.title} alert size="sm" onclose={() => answer(false)} class="tr-review-confirm">
|
|
413
|
+
{#if asking.message}<p class="msg">{asking.message}</p>{/if}
|
|
414
|
+
{#if asking.excerpt}<blockquote class="excerpt"><bdi>{asking.excerpt}</bdi></blockquote>{/if}
|
|
415
|
+
{#if asking.note}
|
|
416
|
+
<Textarea label={noteSpec.label ?? L.note} placeholder={noteSpec.placeholder} rows={3} bind:value={asking.value} />
|
|
417
|
+
{/if}
|
|
418
|
+
{#snippet footer()}
|
|
419
|
+
<Button variant="minimal" onclick={() => answer(false)}>{L.cancel}</Button>
|
|
420
|
+
<Button intent={asking!.tone ?? intentOf(asking!.action.tone)} disabled={!!noteSpec.required && !asking!.value.trim()} onclick={() => answer(true)}>{asking!.confirmLabel ?? asking!.action.label}</Button>
|
|
421
|
+
{/snippet}
|
|
422
|
+
</Dialog>
|
|
423
|
+
{/if}
|
|
424
|
+
|
|
425
|
+
<style>
|
|
426
|
+
.tr-review {
|
|
427
|
+
display: flex;
|
|
428
|
+
flex-direction: column;
|
|
429
|
+
min-height: 0;
|
|
430
|
+
height: 100%;
|
|
431
|
+
background: var(--tr-surface);
|
|
432
|
+
color: var(--tr-on-surface);
|
|
433
|
+
font: var(--tr-text-md) var(--tr-font-sans);
|
|
434
|
+
outline: none;
|
|
435
|
+
}
|
|
436
|
+
.head {
|
|
437
|
+
display: flex;
|
|
438
|
+
flex-wrap: wrap;
|
|
439
|
+
align-items: center;
|
|
440
|
+
justify-content: space-between;
|
|
441
|
+
gap: var(--tr-space-2, 8px);
|
|
442
|
+
padding: var(--tr-space-2, 8px) var(--tr-space-3, 12px);
|
|
443
|
+
border-bottom: 1px solid var(--tr-outline-variant);
|
|
444
|
+
background: var(--tr-surface-container-low);
|
|
445
|
+
}
|
|
446
|
+
.nav,
|
|
447
|
+
.bulk,
|
|
448
|
+
.acts {
|
|
449
|
+
display: flex;
|
|
450
|
+
flex-wrap: wrap;
|
|
451
|
+
align-items: center;
|
|
452
|
+
gap: var(--tr-space-1, 4px) var(--tr-space-2, 8px);
|
|
453
|
+
}
|
|
454
|
+
.pos {
|
|
455
|
+
font-size: var(--tr-text-sm);
|
|
456
|
+
font-variant-numeric: tabular-nums;
|
|
457
|
+
color: var(--tr-on-surface-variant);
|
|
458
|
+
white-space: nowrap;
|
|
459
|
+
}
|
|
460
|
+
.bulk {
|
|
461
|
+
padding: var(--tr-space-1, 4px) var(--tr-space-3, 12px);
|
|
462
|
+
border-bottom: 1px solid var(--tr-outline-variant);
|
|
463
|
+
}
|
|
464
|
+
.list {
|
|
465
|
+
flex: 1;
|
|
466
|
+
min-height: 6rem;
|
|
467
|
+
overflow-y: auto;
|
|
468
|
+
padding: var(--tr-space-1, 4px) 0;
|
|
469
|
+
}
|
|
470
|
+
.group h3 {
|
|
471
|
+
margin: 0;
|
|
472
|
+
padding: var(--tr-space-2, 8px) var(--tr-space-3, 12px) var(--tr-space-1, 4px);
|
|
473
|
+
font: 600 var(--tr-text-xs, 0.75rem) var(--tr-font-sans);
|
|
474
|
+
letter-spacing: 0.04em;
|
|
475
|
+
text-transform: uppercase;
|
|
476
|
+
color: var(--tr-on-surface-variant);
|
|
477
|
+
}
|
|
478
|
+
ul {
|
|
479
|
+
list-style: none;
|
|
480
|
+
margin: 0;
|
|
481
|
+
padding: 0;
|
|
482
|
+
}
|
|
483
|
+
li {
|
|
484
|
+
--c: var(--tr-warning);
|
|
485
|
+
display: flex;
|
|
486
|
+
align-items: center;
|
|
487
|
+
gap: var(--tr-space-1, 4px);
|
|
488
|
+
padding: 0 var(--tr-space-2, 8px) 0 var(--tr-space-3, 12px);
|
|
489
|
+
}
|
|
490
|
+
li[data-tone='danger'] {
|
|
491
|
+
--c: var(--tr-danger);
|
|
492
|
+
}
|
|
493
|
+
li[data-tone='success'] {
|
|
494
|
+
--c: var(--tr-success);
|
|
495
|
+
}
|
|
496
|
+
li[data-tone='primary'] {
|
|
497
|
+
--c: var(--tr-primary);
|
|
498
|
+
}
|
|
499
|
+
li[data-tone='neutral'] {
|
|
500
|
+
--c: var(--tr-outline);
|
|
501
|
+
}
|
|
502
|
+
.item {
|
|
503
|
+
flex: 1;
|
|
504
|
+
min-width: 0;
|
|
505
|
+
display: flex;
|
|
506
|
+
align-items: center;
|
|
507
|
+
gap: var(--tr-space-2, 8px);
|
|
508
|
+
min-height: 36px;
|
|
509
|
+
padding: var(--tr-space-1, 4px) var(--tr-space-2, 8px);
|
|
510
|
+
border: 0;
|
|
511
|
+
border-radius: var(--tr-radius-sm, 4px);
|
|
512
|
+
background: transparent;
|
|
513
|
+
color: inherit;
|
|
514
|
+
font: inherit;
|
|
515
|
+
text-align: start;
|
|
516
|
+
cursor: pointer;
|
|
517
|
+
}
|
|
518
|
+
.item:hover {
|
|
519
|
+
background: var(--tr-surface-container);
|
|
520
|
+
}
|
|
521
|
+
.item[aria-current='true'] {
|
|
522
|
+
background: var(--tr-surface-container-high);
|
|
523
|
+
box-shadow: inset 3px 0 0 var(--tr-primary);
|
|
524
|
+
}
|
|
525
|
+
.item:focus-visible {
|
|
526
|
+
outline: none;
|
|
527
|
+
box-shadow: var(--tr-focus-ring);
|
|
528
|
+
}
|
|
529
|
+
.dot {
|
|
530
|
+
flex: none;
|
|
531
|
+
width: 8px;
|
|
532
|
+
height: 8px;
|
|
533
|
+
border-radius: 50%;
|
|
534
|
+
background: var(--c);
|
|
535
|
+
}
|
|
536
|
+
.text {
|
|
537
|
+
flex: 1;
|
|
538
|
+
min-width: 0;
|
|
539
|
+
display: flex;
|
|
540
|
+
flex-direction: column;
|
|
541
|
+
}
|
|
542
|
+
.title,
|
|
543
|
+
.desc {
|
|
544
|
+
overflow: hidden;
|
|
545
|
+
text-overflow: ellipsis;
|
|
546
|
+
white-space: nowrap;
|
|
547
|
+
}
|
|
548
|
+
.desc {
|
|
549
|
+
font-size: var(--tr-text-sm);
|
|
550
|
+
color: var(--tr-on-surface-variant);
|
|
551
|
+
}
|
|
552
|
+
.current {
|
|
553
|
+
display: flex;
|
|
554
|
+
flex-direction: column;
|
|
555
|
+
gap: var(--tr-space-2, 8px);
|
|
556
|
+
max-height: 50%;
|
|
557
|
+
overflow-y: auto;
|
|
558
|
+
padding: var(--tr-space-2, 8px) var(--tr-space-3, 12px);
|
|
559
|
+
border-top: 1px solid var(--tr-outline-variant);
|
|
560
|
+
background: var(--tr-surface-container-low);
|
|
561
|
+
}
|
|
562
|
+
.kbd {
|
|
563
|
+
margin-inline-start: 6px;
|
|
564
|
+
opacity: 0.8;
|
|
565
|
+
}
|
|
566
|
+
.detail {
|
|
567
|
+
font-size: var(--tr-text-sm);
|
|
568
|
+
min-width: 0;
|
|
569
|
+
}
|
|
570
|
+
.sr {
|
|
571
|
+
position: absolute;
|
|
572
|
+
width: 1px;
|
|
573
|
+
height: 1px;
|
|
574
|
+
margin: -1px;
|
|
575
|
+
overflow: hidden;
|
|
576
|
+
clip-path: inset(50%);
|
|
577
|
+
white-space: nowrap;
|
|
578
|
+
}
|
|
579
|
+
.msg {
|
|
580
|
+
margin: 0 0 var(--tr-space-2, 8px);
|
|
581
|
+
}
|
|
582
|
+
.excerpt {
|
|
583
|
+
margin: 0 0 var(--tr-space-3, 12px);
|
|
584
|
+
padding: var(--tr-space-2, 8px) var(--tr-space-3, 12px);
|
|
585
|
+
border-inline-start: 3px solid var(--tr-outline);
|
|
586
|
+
background: var(--tr-surface-container);
|
|
587
|
+
border-radius: var(--tr-radius-sm, 4px);
|
|
588
|
+
white-space: pre-wrap;
|
|
589
|
+
overflow-wrap: anywhere;
|
|
590
|
+
}
|
|
591
|
+
</style>
|