@lemonriver/core 0.6.0 → 0.7.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/dist/{build-RlcGG-Ve.js → build-CKwrex8y.js} +1 -1
- package/dist/cli/bin.js +3 -3
- package/dist/{config-BJT87wE9.js → config-B1_gk0sy.js} +1641 -60
- package/dist/{dev-DR-j_xda.js → dev-DZ2Yzl2p.js} +1 -1
- package/dist/{preview-BTAhTb5B.js → preview-hMDcuHfP.js} +1 -1
- package/dist/vite/index.js +1 -1
- package/package.json +1 -1
- package/src/app/lemonriver/chrome/app-chrome.tsx +12 -1
- package/src/app/lemonriver/features.test.ts +5 -0
- package/src/app/lemonriver/features.ts +2 -1
- package/src/app/lemonriver/feedback/pending-toast.ts +7 -2
- package/src/app/lemonriver/home/deck-card.tsx +10 -1
- package/src/app/lemonriver/home/home-page.tsx +52 -1
- package/src/app/lemonriver/share/capture.ts +76 -0
- package/src/app/lemonriver/share/client.ts +162 -0
- package/src/app/lemonriver/share/codes.test.ts +49 -0
- package/src/app/lemonriver/share/codes.ts +30 -0
- package/src/app/lemonriver/share/compare.test.ts +199 -0
- package/src/app/lemonriver/share/compare.ts +367 -0
- package/src/app/lemonriver/share/drop.ts +84 -0
- package/src/app/lemonriver/share/folder.test.ts +42 -0
- package/src/app/lemonriver/share/folder.ts +29 -0
- package/src/app/lemonriver/share/freeze.ts +51 -0
- package/src/app/lemonriver/share/guide.test.ts +78 -0
- package/src/app/lemonriver/share/guide.ts +222 -0
- package/src/app/lemonriver/share/import-dialog.tsx +391 -0
- package/src/app/lemonriver/share/import-entry.tsx +106 -0
- package/src/app/lemonriver/share/model.ts +150 -0
- package/src/app/lemonriver/share/naming.test.ts +59 -0
- package/src/app/lemonriver/share/naming.ts +71 -0
- package/src/app/lemonriver/share/paths.test.ts +75 -0
- package/src/app/lemonriver/share/paths.ts +181 -0
- package/src/app/lemonriver/share/pdf.test.ts +79 -0
- package/src/app/lemonriver/share/pdf.ts +169 -0
- package/src/app/lemonriver/share/share-dialog.tsx +286 -0
- package/src/app/lemonriver/share/summary.ts +47 -0
- package/src/app/lemonriver/slide-extensions.tsx +69 -23
- package/src/app/lemonriver/templates/library-page.tsx +5 -2
- package/src/app/lemonriver/text/en.ts +67 -0
- package/src/app/lemonriver/text/es.ts +67 -0
- package/src/app/lemonriver/text/ja.ts +67 -0
- package/src/app/lemonriver/text/types.ts +78 -0
- package/src/app/lemonriver/text/zh-cn.ts +62 -0
- package/src/app/lemonriver/text/zh-tw.ts +62 -0
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
import { ChevronLeft, ChevronRight, ShieldAlert } from 'lucide-react';
|
|
2
|
+
import { useState } from 'react';
|
|
3
|
+
import { createPortal } from 'react-dom';
|
|
4
|
+
import { Button } from '@/components/ui/button';
|
|
5
|
+
import {
|
|
6
|
+
Dialog,
|
|
7
|
+
DialogContent,
|
|
8
|
+
DialogFooter,
|
|
9
|
+
DialogHeader,
|
|
10
|
+
DialogTitle,
|
|
11
|
+
} from '@/components/ui/dialog';
|
|
12
|
+
import { format, useLocale } from '@/lib/use-locale';
|
|
13
|
+
import { cn } from '@/lib/utils';
|
|
14
|
+
import { savePendingToast } from '../feedback/pending-toast';
|
|
15
|
+
import { useLrText } from '../text';
|
|
16
|
+
import { type InspectDeck, type InspectOk, importPackages, thumbUrl } from './client';
|
|
17
|
+
import type { DeckChanges, PageStatus } from './compare';
|
|
18
|
+
import { changeSummary } from './summary';
|
|
19
|
+
|
|
20
|
+
type Mode = 'copy' | 'replace';
|
|
21
|
+
|
|
22
|
+
const STATUS_DOT: Record<PageStatus, string> = {
|
|
23
|
+
same: 'bg-muted-foreground/40',
|
|
24
|
+
changed: 'bg-[var(--lr-accent)]',
|
|
25
|
+
added: 'bg-[var(--lr-success)]',
|
|
26
|
+
removed: 'bg-[var(--lr-warning)]',
|
|
27
|
+
moved: 'bg-[var(--lr-warning)]',
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
function CompareBlock({
|
|
31
|
+
changes,
|
|
32
|
+
selected,
|
|
33
|
+
expanded,
|
|
34
|
+
onToggle,
|
|
35
|
+
onSelect,
|
|
36
|
+
}: {
|
|
37
|
+
changes: DeckChanges;
|
|
38
|
+
selected: number;
|
|
39
|
+
expanded: boolean;
|
|
40
|
+
onToggle: () => void;
|
|
41
|
+
onSelect: (page: number) => void;
|
|
42
|
+
}) {
|
|
43
|
+
const lr = useLrText();
|
|
44
|
+
const locale = useLocale();
|
|
45
|
+
const listSeparator = locale.id.startsWith('zh') ? '、' : ', ';
|
|
46
|
+
const clauseSeparator = locale.id.startsWith('zh') ? ',' : ' · ';
|
|
47
|
+
const labels: Record<PageStatus, string> = {
|
|
48
|
+
same: lr.share.statusSame,
|
|
49
|
+
changed: lr.share.statusChanged,
|
|
50
|
+
added: lr.share.statusAdded,
|
|
51
|
+
removed: lr.share.statusRemoved,
|
|
52
|
+
moved: lr.share.statusMoved,
|
|
53
|
+
};
|
|
54
|
+
return (
|
|
55
|
+
<div
|
|
56
|
+
data-lr-share-compare
|
|
57
|
+
className="mt-2 rounded-[10px] border border-border bg-muted/30 px-3 py-2 text-[13px]"
|
|
58
|
+
>
|
|
59
|
+
<p className="font-semibold text-foreground">{lr.share.compareTitle}</p>
|
|
60
|
+
<p data-lr-share-compare-summary className="mt-1 leading-snug text-muted-foreground">
|
|
61
|
+
{changeSummary(changes, lr.share, listSeparator, clauseSeparator)}
|
|
62
|
+
</p>
|
|
63
|
+
{changes.readable && changes.pages.length > 0 && (
|
|
64
|
+
<>
|
|
65
|
+
<button
|
|
66
|
+
type="button"
|
|
67
|
+
aria-expanded={expanded}
|
|
68
|
+
data-lr-share-compare-toggle
|
|
69
|
+
onClick={onToggle}
|
|
70
|
+
className="mt-1.5 text-[12px] font-semibold text-[var(--lr-accent)] outline-none hover:underline focus-visible:ring-2 focus-visible:ring-ring/40"
|
|
71
|
+
>
|
|
72
|
+
{expanded ? lr.share.compareCollapse : lr.share.compareExpand}
|
|
73
|
+
</button>
|
|
74
|
+
{expanded && (
|
|
75
|
+
<ul className="mt-1.5 flex flex-col gap-0.5" data-lr-share-compare-pages>
|
|
76
|
+
{changes.pages.map((page) => (
|
|
77
|
+
<li key={`${page.n}-${page.name ?? 'inline'}`}>
|
|
78
|
+
<button
|
|
79
|
+
type="button"
|
|
80
|
+
data-lr-share-compare-page={page.n}
|
|
81
|
+
disabled={page.status === 'removed'}
|
|
82
|
+
aria-pressed={selected === page.n}
|
|
83
|
+
onClick={() => onSelect(page.n)}
|
|
84
|
+
className={cn(
|
|
85
|
+
'flex w-full items-center gap-2 rounded-[6px] px-1.5 py-1 text-left text-[12px] outline-none',
|
|
86
|
+
selected === page.n ? 'bg-[var(--lr-accent-soft)]' : 'hover:bg-muted',
|
|
87
|
+
page.status === 'removed' && 'opacity-50',
|
|
88
|
+
)}
|
|
89
|
+
>
|
|
90
|
+
<span
|
|
91
|
+
className={cn('size-1.5 shrink-0 rounded-full', STATUS_DOT[page.status])}
|
|
92
|
+
aria-hidden
|
|
93
|
+
/>
|
|
94
|
+
<span className="w-16 shrink-0 text-muted-foreground">
|
|
95
|
+
{format(lr.share.pageOf, { n: page.n })}
|
|
96
|
+
</span>
|
|
97
|
+
<span className="shrink-0 font-medium">{labels[page.status]}</span>
|
|
98
|
+
{page.name && (
|
|
99
|
+
<span className="truncate font-mono text-[11px] text-muted-foreground">
|
|
100
|
+
{page.name}
|
|
101
|
+
</span>
|
|
102
|
+
)}
|
|
103
|
+
</button>
|
|
104
|
+
</li>
|
|
105
|
+
))}
|
|
106
|
+
</ul>
|
|
107
|
+
)}
|
|
108
|
+
</>
|
|
109
|
+
)}
|
|
110
|
+
</div>
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function Thumb({
|
|
115
|
+
token,
|
|
116
|
+
deck,
|
|
117
|
+
page,
|
|
118
|
+
className,
|
|
119
|
+
onClick,
|
|
120
|
+
}: {
|
|
121
|
+
token: string;
|
|
122
|
+
deck: InspectDeck;
|
|
123
|
+
page: number;
|
|
124
|
+
className?: string;
|
|
125
|
+
onClick?: () => void;
|
|
126
|
+
}) {
|
|
127
|
+
const lr = useLrText();
|
|
128
|
+
const body =
|
|
129
|
+
deck.thumbs > 0 ? (
|
|
130
|
+
<img
|
|
131
|
+
src={thumbUrl(token, deck.slideId, page)}
|
|
132
|
+
alt=""
|
|
133
|
+
className="size-full object-cover"
|
|
134
|
+
loading="lazy"
|
|
135
|
+
/>
|
|
136
|
+
) : (
|
|
137
|
+
<span className="block size-full bg-muted" />
|
|
138
|
+
);
|
|
139
|
+
if (!onClick) return <span className={cn('block overflow-hidden', className)}>{body}</span>;
|
|
140
|
+
return (
|
|
141
|
+
<button
|
|
142
|
+
type="button"
|
|
143
|
+
onClick={onClick}
|
|
144
|
+
aria-label={format(lr.share.pageOf, { n: page })}
|
|
145
|
+
className={cn(
|
|
146
|
+
'block overflow-hidden rounded-[8px] border border-border bg-white outline-none',
|
|
147
|
+
'focus-visible:ring-2 focus-visible:ring-ring/40',
|
|
148
|
+
className,
|
|
149
|
+
)}
|
|
150
|
+
>
|
|
151
|
+
{body}
|
|
152
|
+
</button>
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** Left/right through every thumbnail of the package. */
|
|
157
|
+
function ThumbViewer({
|
|
158
|
+
token,
|
|
159
|
+
deck,
|
|
160
|
+
index,
|
|
161
|
+
onIndex,
|
|
162
|
+
onClose,
|
|
163
|
+
}: {
|
|
164
|
+
token: string;
|
|
165
|
+
deck: InspectDeck;
|
|
166
|
+
index: number;
|
|
167
|
+
onIndex: (next: number) => void;
|
|
168
|
+
onClose: () => void;
|
|
169
|
+
}) {
|
|
170
|
+
const lr = useLrText();
|
|
171
|
+
const total = Math.max(1, deck.thumbs);
|
|
172
|
+
const go = (next: number) => onIndex(Math.min(total - 1, Math.max(0, next)));
|
|
173
|
+
return createPortal(
|
|
174
|
+
<div
|
|
175
|
+
className="fixed inset-0 z-[70] flex items-center justify-center bg-foreground/60 p-6 backdrop-blur-[2px]"
|
|
176
|
+
data-lr-share-viewer
|
|
177
|
+
>
|
|
178
|
+
<button
|
|
179
|
+
type="button"
|
|
180
|
+
aria-label={lr.share.finish}
|
|
181
|
+
className="absolute inset-0 size-full cursor-default"
|
|
182
|
+
onClick={onClose}
|
|
183
|
+
/>
|
|
184
|
+
<div className="relative flex max-h-full max-w-[min(1100px,92vw)] flex-col items-center gap-3">
|
|
185
|
+
<img
|
|
186
|
+
src={thumbUrl(token, deck.slideId, index + 1)}
|
|
187
|
+
alt=""
|
|
188
|
+
className="w-auto rounded-[10px] border border-border bg-white shadow-overlay"
|
|
189
|
+
style={{ maxHeight: '72vh' }}
|
|
190
|
+
/>
|
|
191
|
+
<div className="flex items-center gap-3 rounded-full bg-card px-2 py-1 shadow-[var(--lr-shadow-2)]">
|
|
192
|
+
<button
|
|
193
|
+
type="button"
|
|
194
|
+
aria-label={lr.share.earlier}
|
|
195
|
+
disabled={index <= 0}
|
|
196
|
+
onClick={() => go(index - 1)}
|
|
197
|
+
className="flex size-8 items-center justify-center rounded-full text-muted-foreground outline-none hover:bg-muted disabled:opacity-30 focus-visible:ring-2 focus-visible:ring-ring/40"
|
|
198
|
+
>
|
|
199
|
+
<ChevronLeft className="size-4" />
|
|
200
|
+
</button>
|
|
201
|
+
<span className="min-w-[64px] text-center text-[13px] tabular-nums text-muted-foreground">
|
|
202
|
+
{index + 1} / {total}
|
|
203
|
+
</span>
|
|
204
|
+
<button
|
|
205
|
+
type="button"
|
|
206
|
+
aria-label={lr.share.later}
|
|
207
|
+
disabled={index >= total - 1}
|
|
208
|
+
onClick={() => go(index + 1)}
|
|
209
|
+
className="flex size-8 items-center justify-center rounded-full text-muted-foreground outline-none hover:bg-muted disabled:opacity-30 focus-visible:ring-2 focus-visible:ring-ring/40"
|
|
210
|
+
>
|
|
211
|
+
<ChevronRight className="size-4" />
|
|
212
|
+
</button>
|
|
213
|
+
</div>
|
|
214
|
+
</div>
|
|
215
|
+
</div>,
|
|
216
|
+
document.body,
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export function ImportDialog({
|
|
221
|
+
inspection,
|
|
222
|
+
onClose,
|
|
223
|
+
}: {
|
|
224
|
+
inspection: InspectOk;
|
|
225
|
+
onClose: () => void;
|
|
226
|
+
}) {
|
|
227
|
+
const t = useLocale();
|
|
228
|
+
const lr = useLrText();
|
|
229
|
+
const { token, decks, app } = inspection;
|
|
230
|
+
const [chosen, setChosen] = useState<Record<string, boolean>>(() =>
|
|
231
|
+
Object.fromEntries(decks.map((deck) => [deck.slideId, true])),
|
|
232
|
+
);
|
|
233
|
+
const [mode, setMode] = useState<Record<string, Mode>>(() =>
|
|
234
|
+
Object.fromEntries(decks.map((deck) => [deck.slideId, 'copy'])),
|
|
235
|
+
);
|
|
236
|
+
const [viewer, setViewer] = useState<{ slideId: string; index: number } | null>(null);
|
|
237
|
+
const [preview, setPreview] = useState<Record<string, number>>({});
|
|
238
|
+
const [expanded, setExpanded] = useState<Record<string, boolean>>({});
|
|
239
|
+
const [busy, setBusy] = useState(false);
|
|
240
|
+
const [error, setError] = useState('');
|
|
241
|
+
|
|
242
|
+
const picked = decks.filter((deck) => chosen[deck.slideId]);
|
|
243
|
+
const many = decks.length > 1;
|
|
244
|
+
const viewerDeck = viewer ? decks.find((deck) => deck.slideId === viewer.slideId) : null;
|
|
245
|
+
|
|
246
|
+
const submit = async () => {
|
|
247
|
+
if (picked.length === 0 || busy) return;
|
|
248
|
+
setBusy(true);
|
|
249
|
+
setError('');
|
|
250
|
+
try {
|
|
251
|
+
const result = await importPackages(
|
|
252
|
+
token,
|
|
253
|
+
picked.map((deck) => ({ slideId: deck.slideId, mode: mode[deck.slideId] ?? 'copy' })),
|
|
254
|
+
);
|
|
255
|
+
const first = result.imported[0];
|
|
256
|
+
const firstDeck = decks.find((deck) => deck.slideId === first?.from);
|
|
257
|
+
savePendingToast({
|
|
258
|
+
kind: 'imported',
|
|
259
|
+
slideId: first?.slideId ?? picked[0].slideId,
|
|
260
|
+
name: firstDeck?.title ?? picked[0].title,
|
|
261
|
+
});
|
|
262
|
+
window.location.reload();
|
|
263
|
+
} catch (err) {
|
|
264
|
+
setError(err instanceof Error ? err.message : String(err));
|
|
265
|
+
setBusy(false);
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
return (
|
|
270
|
+
<Dialog open onOpenChange={(next) => !next && onClose()}>
|
|
271
|
+
<DialogContent className="sm:max-w-[560px]" data-lr-share-import-dialog>
|
|
272
|
+
<DialogHeader>
|
|
273
|
+
<DialogTitle>{lr.share.importTitle}</DialogTitle>
|
|
274
|
+
</DialogHeader>
|
|
275
|
+
|
|
276
|
+
<div className="flex flex-col gap-3 overflow-y-auto pr-1" style={{ maxHeight: '46vh' }}>
|
|
277
|
+
{decks.map((deck) => (
|
|
278
|
+
<div
|
|
279
|
+
key={deck.slideId}
|
|
280
|
+
data-lr-share-deck={deck.slideId}
|
|
281
|
+
className="flex gap-3 rounded-[10px] border border-border p-2.5"
|
|
282
|
+
>
|
|
283
|
+
{many && (
|
|
284
|
+
<input
|
|
285
|
+
type="checkbox"
|
|
286
|
+
aria-label={deck.title}
|
|
287
|
+
checked={chosen[deck.slideId] ?? true}
|
|
288
|
+
onChange={(event) =>
|
|
289
|
+
setChosen((prev) => ({ ...prev, [deck.slideId]: event.target.checked }))
|
|
290
|
+
}
|
|
291
|
+
className="mt-1 size-4 shrink-0 accent-[var(--lr-accent)]"
|
|
292
|
+
/>
|
|
293
|
+
)}
|
|
294
|
+
<Thumb
|
|
295
|
+
token={token}
|
|
296
|
+
deck={deck}
|
|
297
|
+
page={preview[deck.slideId] ?? 1}
|
|
298
|
+
onClick={() =>
|
|
299
|
+
setViewer({ slideId: deck.slideId, index: (preview[deck.slideId] ?? 1) - 1 })
|
|
300
|
+
}
|
|
301
|
+
className="w-[176px] shrink-0 aspect-video"
|
|
302
|
+
/>
|
|
303
|
+
<div className="min-w-0 flex-1">
|
|
304
|
+
<p className="truncate text-[14px] font-semibold" title={deck.title}>
|
|
305
|
+
{deck.title}
|
|
306
|
+
</p>
|
|
307
|
+
<p className="mt-0.5 text-[12px] text-muted-foreground">
|
|
308
|
+
{deck.pages !== null ? format(lr.share.pageCount, { n: deck.pages }) : ''}
|
|
309
|
+
{deck.pages !== null ? ' · ' : ''}
|
|
310
|
+
{format(lr.share.madeWith, { version: app.version })}
|
|
311
|
+
</p>
|
|
312
|
+
{deck.existing && deck.changes && (
|
|
313
|
+
<CompareBlock
|
|
314
|
+
changes={deck.changes}
|
|
315
|
+
selected={preview[deck.slideId] ?? 1}
|
|
316
|
+
expanded={expanded[deck.slideId] ?? false}
|
|
317
|
+
onToggle={() =>
|
|
318
|
+
setExpanded((prev) => ({ ...prev, [deck.slideId]: !prev[deck.slideId] }))
|
|
319
|
+
}
|
|
320
|
+
onSelect={(page) => setPreview((prev) => ({ ...prev, [deck.slideId]: page }))}
|
|
321
|
+
/>
|
|
322
|
+
)}
|
|
323
|
+
{deck.existing && (
|
|
324
|
+
<div className="mt-2 flex flex-col gap-1.5 text-[13px]">
|
|
325
|
+
<label className="flex items-center gap-2">
|
|
326
|
+
<input
|
|
327
|
+
type="radio"
|
|
328
|
+
name={`lr-import-mode-${deck.slideId}`}
|
|
329
|
+
checked={(mode[deck.slideId] ?? 'copy') === 'copy'}
|
|
330
|
+
onChange={() => setMode((prev) => ({ ...prev, [deck.slideId]: 'copy' }))}
|
|
331
|
+
className="size-3.5 accent-[var(--lr-accent)]"
|
|
332
|
+
/>
|
|
333
|
+
{lr.share.keepBoth}
|
|
334
|
+
</label>
|
|
335
|
+
<label className="flex items-center gap-2">
|
|
336
|
+
<input
|
|
337
|
+
type="radio"
|
|
338
|
+
name={`lr-import-mode-${deck.slideId}`}
|
|
339
|
+
checked={mode[deck.slideId] === 'replace'}
|
|
340
|
+
onChange={() => setMode((prev) => ({ ...prev, [deck.slideId]: 'replace' }))}
|
|
341
|
+
className="size-3.5 accent-[var(--lr-accent)]"
|
|
342
|
+
/>
|
|
343
|
+
{format(lr.share.replaceExisting, { title: deck.existing.title })}
|
|
344
|
+
</label>
|
|
345
|
+
</div>
|
|
346
|
+
)}
|
|
347
|
+
</div>
|
|
348
|
+
</div>
|
|
349
|
+
))}
|
|
350
|
+
</div>
|
|
351
|
+
|
|
352
|
+
<div
|
|
353
|
+
data-lr-share-warning
|
|
354
|
+
className="flex items-start gap-2.5 rounded-[10px] border border-[var(--lr-warn-border,var(--lr-accent))] bg-[var(--lr-accent-soft)] px-3 py-2.5 text-[13px] font-semibold leading-snug"
|
|
355
|
+
>
|
|
356
|
+
<ShieldAlert className="mt-0.5 size-4 shrink-0" aria-hidden />
|
|
357
|
+
<span>{lr.share.securityNotice}</span>
|
|
358
|
+
</div>
|
|
359
|
+
|
|
360
|
+
{error !== '' && <p className="text-[13px] text-destructive">{error}</p>}
|
|
361
|
+
|
|
362
|
+
<DialogFooter>
|
|
363
|
+
<Button type="button" variant="outline" size="sm" onClick={onClose} disabled={busy}>
|
|
364
|
+
{t.common.cancel}
|
|
365
|
+
</Button>
|
|
366
|
+
<Button
|
|
367
|
+
type="button"
|
|
368
|
+
size="sm"
|
|
369
|
+
onClick={() => void submit()}
|
|
370
|
+
disabled={busy || picked.length === 0}
|
|
371
|
+
>
|
|
372
|
+
{lr.share.importAction}
|
|
373
|
+
</Button>
|
|
374
|
+
</DialogFooter>
|
|
375
|
+
</DialogContent>
|
|
376
|
+
|
|
377
|
+
{viewer && viewerDeck && (
|
|
378
|
+
<ThumbViewer
|
|
379
|
+
token={token}
|
|
380
|
+
deck={viewerDeck}
|
|
381
|
+
index={viewer.index}
|
|
382
|
+
onIndex={(index) => {
|
|
383
|
+
setViewer({ slideId: viewerDeck.slideId, index });
|
|
384
|
+
setPreview((prev) => ({ ...prev, [viewerDeck.slideId]: index + 1 }));
|
|
385
|
+
}}
|
|
386
|
+
onClose={() => setViewer(null)}
|
|
387
|
+
/>
|
|
388
|
+
)}
|
|
389
|
+
</Dialog>
|
|
390
|
+
);
|
|
391
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { Import } from 'lucide-react';
|
|
2
|
+
import { type DragEvent, type ReactNode, useRef, useState } from 'react';
|
|
3
|
+
import { useLrText } from '../text';
|
|
4
|
+
import { readDropped } from './drop';
|
|
5
|
+
|
|
6
|
+
function hasFiles(event: DragEvent): boolean {
|
|
7
|
+
return Array.from(event.dataTransfer?.types ?? []).includes('Files');
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** The home page's Import button: a zip picker wearing the same clothes as the toolbar. */
|
|
11
|
+
export function ImportButton({
|
|
12
|
+
onBytes,
|
|
13
|
+
disabled,
|
|
14
|
+
}: {
|
|
15
|
+
onBytes: (bytes: Uint8Array) => void;
|
|
16
|
+
disabled?: boolean;
|
|
17
|
+
}) {
|
|
18
|
+
const lr = useLrText();
|
|
19
|
+
const inputRef = useRef<HTMLInputElement | null>(null);
|
|
20
|
+
return (
|
|
21
|
+
<>
|
|
22
|
+
<button
|
|
23
|
+
type="button"
|
|
24
|
+
data-lr-share-import
|
|
25
|
+
disabled={disabled}
|
|
26
|
+
onClick={() => inputRef.current?.click()}
|
|
27
|
+
className="flex h-9 shrink-0 items-center gap-1.5 whitespace-nowrap rounded-full border border-border bg-card px-3 text-[14px] font-medium text-foreground outline-none transition-colors duration-150 hover:bg-muted focus-visible:ring-2 focus-visible:ring-ring/40 disabled:opacity-60"
|
|
28
|
+
>
|
|
29
|
+
<Import className="size-4 text-muted-foreground" aria-hidden />
|
|
30
|
+
{lr.share.importAction}
|
|
31
|
+
</button>
|
|
32
|
+
<input
|
|
33
|
+
ref={inputRef}
|
|
34
|
+
type="file"
|
|
35
|
+
accept=".zip,application/zip"
|
|
36
|
+
className="hidden"
|
|
37
|
+
onChange={(event) => {
|
|
38
|
+
const file = event.target.files?.[0];
|
|
39
|
+
event.target.value = '';
|
|
40
|
+
if (!file) return;
|
|
41
|
+
void file.arrayBuffer().then((buffer) => onBytes(new Uint8Array(buffer)));
|
|
42
|
+
}}
|
|
43
|
+
/>
|
|
44
|
+
</>
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The home page accepts a package anywhere: the zip itself, or the folder
|
|
50
|
+
* Safari left behind after unpacking it. Spread `handlers` on the page element
|
|
51
|
+
* and render `hint` inside it.
|
|
52
|
+
*/
|
|
53
|
+
export function useShareDrop({
|
|
54
|
+
onBytes,
|
|
55
|
+
onUnknown,
|
|
56
|
+
}: {
|
|
57
|
+
onBytes: (bytes: Uint8Array) => void;
|
|
58
|
+
onUnknown: () => void;
|
|
59
|
+
}): { handlers: Record<string, unknown>; hint: ReactNode } {
|
|
60
|
+
const lr = useLrText();
|
|
61
|
+
const [active, setActive] = useState(false);
|
|
62
|
+
const depth = useRef(0);
|
|
63
|
+
|
|
64
|
+
const handlers = {
|
|
65
|
+
'data-lr-share-drop': '',
|
|
66
|
+
onDragEnter: (event: DragEvent) => {
|
|
67
|
+
if (!hasFiles(event)) return;
|
|
68
|
+
event.preventDefault();
|
|
69
|
+
depth.current += 1;
|
|
70
|
+
setActive(true);
|
|
71
|
+
},
|
|
72
|
+
onDragOver: (event: DragEvent) => {
|
|
73
|
+
if (!hasFiles(event)) return;
|
|
74
|
+
event.preventDefault();
|
|
75
|
+
event.dataTransfer.dropEffect = 'copy';
|
|
76
|
+
},
|
|
77
|
+
onDragLeave: () => {
|
|
78
|
+
depth.current = Math.max(0, depth.current - 1);
|
|
79
|
+
if (depth.current === 0) setActive(false);
|
|
80
|
+
},
|
|
81
|
+
onDrop: (event: DragEvent) => {
|
|
82
|
+
if (!hasFiles(event)) return;
|
|
83
|
+
event.preventDefault();
|
|
84
|
+
depth.current = 0;
|
|
85
|
+
setActive(false);
|
|
86
|
+
const transfer = event.dataTransfer;
|
|
87
|
+
void readDropped(transfer).then((dropped) => {
|
|
88
|
+
if (dropped) onBytes(dropped.bytes);
|
|
89
|
+
else onUnknown();
|
|
90
|
+
});
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const hint = active ? (
|
|
95
|
+
<div
|
|
96
|
+
data-lr-share-drop-hint
|
|
97
|
+
className="pointer-events-none fixed inset-0 z-50 flex items-center justify-center bg-foreground/20 backdrop-blur-[2px]"
|
|
98
|
+
>
|
|
99
|
+
<div className="rounded-[16px] border-[2px] border-dashed border-[var(--lr-focus)] bg-card px-10 py-8 text-[18px] font-semibold text-foreground shadow-overlay">
|
|
100
|
+
{lr.share.dropHint}
|
|
101
|
+
</div>
|
|
102
|
+
</div>
|
|
103
|
+
) : null;
|
|
104
|
+
|
|
105
|
+
return { handlers, hint };
|
|
106
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/** The parts of a share package both ends must agree on. */
|
|
2
|
+
|
|
3
|
+
export const SHARE_FORMAT = 'lemonriver';
|
|
4
|
+
export const SHARE_VERSION = 1;
|
|
5
|
+
export const SHARE_ROOT = 'LemonRiver-files';
|
|
6
|
+
export const SHARE_MANIFEST = 'manifest.json';
|
|
7
|
+
export const SHARE_APP_NAME = 'LemonRiver';
|
|
8
|
+
|
|
9
|
+
export type ShareKind = 'share' | 'backup';
|
|
10
|
+
|
|
11
|
+
export type ShareDeckEntry = {
|
|
12
|
+
slideId: string;
|
|
13
|
+
title: string;
|
|
14
|
+
pages: number | null;
|
|
15
|
+
shareId: string;
|
|
16
|
+
theme: string | null;
|
|
17
|
+
thumbs: number;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export type ShareManifest = {
|
|
21
|
+
format: typeof SHARE_FORMAT;
|
|
22
|
+
version: number;
|
|
23
|
+
kind: ShareKind;
|
|
24
|
+
app: { name: string; version: string };
|
|
25
|
+
createdAt: number;
|
|
26
|
+
decks: ShareDeckEntry[];
|
|
27
|
+
files: Record<string, string>;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export type ShareCode =
|
|
31
|
+
| 'not-a-package'
|
|
32
|
+
| 'bad-manifest'
|
|
33
|
+
| 'newer-version'
|
|
34
|
+
| 'bad-path'
|
|
35
|
+
| 'blocked-file'
|
|
36
|
+
| 'checksum'
|
|
37
|
+
| 'missing-file'
|
|
38
|
+
| 'extra-file'
|
|
39
|
+
| 'too-large'
|
|
40
|
+
| 'too-many-files';
|
|
41
|
+
|
|
42
|
+
export type ManifestRead =
|
|
43
|
+
| { ok: true; manifest: ShareManifest }
|
|
44
|
+
| { ok: false; code: ShareCode; detail: string; appVersion?: string };
|
|
45
|
+
|
|
46
|
+
const KIND: readonly ShareKind[] = ['share', 'backup'];
|
|
47
|
+
|
|
48
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
49
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function readDeck(raw: unknown): ShareDeckEntry | null {
|
|
53
|
+
if (!isRecord(raw)) return null;
|
|
54
|
+
const { slideId, title, shareId } = raw;
|
|
55
|
+
if (typeof slideId !== 'string' || slideId === '') return null;
|
|
56
|
+
if (typeof title !== 'string') return null;
|
|
57
|
+
if (typeof shareId !== 'string' || shareId === '') return null;
|
|
58
|
+
const pages = typeof raw.pages === 'number' && raw.pages >= 0 ? Math.round(raw.pages) : null;
|
|
59
|
+
const thumbs = typeof raw.thumbs === 'number' && raw.thumbs >= 0 ? Math.round(raw.thumbs) : 0;
|
|
60
|
+
return {
|
|
61
|
+
slideId,
|
|
62
|
+
title,
|
|
63
|
+
pages,
|
|
64
|
+
shareId,
|
|
65
|
+
theme: typeof raw.theme === 'string' && raw.theme !== '' ? raw.theme : null,
|
|
66
|
+
thumbs,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Reads a manifest without trusting any of it. A version above ours is its own
|
|
72
|
+
* answer, because the package may still be perfectly well formed.
|
|
73
|
+
*/
|
|
74
|
+
export function sanitizeManifest(raw: unknown): ManifestRead {
|
|
75
|
+
if (!isRecord(raw))
|
|
76
|
+
return { ok: false, code: 'bad-manifest', detail: 'manifest is not an object' };
|
|
77
|
+
if (raw.format !== SHARE_FORMAT) {
|
|
78
|
+
return { ok: false, code: 'not-a-package', detail: 'this zip is not a LemonRiver package' };
|
|
79
|
+
}
|
|
80
|
+
const version = raw.version;
|
|
81
|
+
if (typeof version !== 'number' || !Number.isInteger(version) || version < 1) {
|
|
82
|
+
return { ok: false, code: 'bad-manifest', detail: 'manifest has no usable version' };
|
|
83
|
+
}
|
|
84
|
+
const app = isRecord(raw.app) ? raw.app : {};
|
|
85
|
+
const appVersion = typeof app.version === 'string' ? app.version : '';
|
|
86
|
+
if (version > SHARE_VERSION) {
|
|
87
|
+
return {
|
|
88
|
+
ok: false,
|
|
89
|
+
code: 'newer-version',
|
|
90
|
+
detail: `made by a newer LemonRiver (${appVersion || 'unknown version'})`,
|
|
91
|
+
appVersion,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
const kind = raw.kind;
|
|
95
|
+
if (typeof kind !== 'string' || !KIND.includes(kind as ShareKind)) {
|
|
96
|
+
return { ok: false, code: 'bad-manifest', detail: 'manifest has no usable kind' };
|
|
97
|
+
}
|
|
98
|
+
if (!Array.isArray(raw.decks) || raw.decks.length === 0) {
|
|
99
|
+
return { ok: false, code: 'bad-manifest', detail: 'manifest lists no decks' };
|
|
100
|
+
}
|
|
101
|
+
const decks: ShareDeckEntry[] = [];
|
|
102
|
+
for (const entry of raw.decks) {
|
|
103
|
+
const deck = readDeck(entry);
|
|
104
|
+
if (!deck)
|
|
105
|
+
return { ok: false, code: 'bad-manifest', detail: 'manifest has a broken deck entry' };
|
|
106
|
+
decks.push(deck);
|
|
107
|
+
}
|
|
108
|
+
if (!isRecord(raw.files)) {
|
|
109
|
+
return { ok: false, code: 'bad-manifest', detail: 'manifest lists no files' };
|
|
110
|
+
}
|
|
111
|
+
const files: Record<string, string> = {};
|
|
112
|
+
for (const [file, digest] of Object.entries(raw.files)) {
|
|
113
|
+
if (typeof digest !== 'string' || digest === '') {
|
|
114
|
+
return { ok: false, code: 'bad-manifest', detail: `no checksum for ${file}` };
|
|
115
|
+
}
|
|
116
|
+
files[file] = digest;
|
|
117
|
+
}
|
|
118
|
+
const createdAt =
|
|
119
|
+
typeof raw.createdAt === 'number' && Number.isFinite(raw.createdAt) ? raw.createdAt : 0;
|
|
120
|
+
return {
|
|
121
|
+
ok: true,
|
|
122
|
+
manifest: {
|
|
123
|
+
format: SHARE_FORMAT,
|
|
124
|
+
version,
|
|
125
|
+
kind: kind as ShareKind,
|
|
126
|
+
app: { name: typeof app.name === 'string' ? app.name : SHARE_APP_NAME, version: appVersion },
|
|
127
|
+
createdAt,
|
|
128
|
+
decks,
|
|
129
|
+
files,
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** The keys a board record may not carry into a package or back out of one. */
|
|
135
|
+
export const BOARD_STRIP_KEYS = [
|
|
136
|
+
'applyBackup',
|
|
137
|
+
'applyColumns',
|
|
138
|
+
'applyColumnsBackup',
|
|
139
|
+
'applying',
|
|
140
|
+
'lastApply',
|
|
141
|
+
] as const;
|
|
142
|
+
|
|
143
|
+
export function stripBoardRecord(record: Record<string, unknown>): Record<string, unknown> {
|
|
144
|
+
const kept: Record<string, unknown> = {};
|
|
145
|
+
for (const [key, value] of Object.entries(record)) {
|
|
146
|
+
if ((BOARD_STRIP_KEYS as readonly string[]).includes(key)) continue;
|
|
147
|
+
kept[key] = value;
|
|
148
|
+
}
|
|
149
|
+
return kept;
|
|
150
|
+
}
|