@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
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { V as PKG_NAMES } from "./update-CMRxtTc9.js";
|
|
2
2
|
import { a as printUrls, i as printShortcutsHint, n as formatError, o as shortcutsEnabled, r as printHeader, s as startupDuration, t as createCliLogger } from "./cli/bin.js";
|
|
3
|
-
import { n as DEV_SUPERVISED_ENV, t as createViteConfig } from "./config-
|
|
3
|
+
import { n as DEV_SUPERVISED_ENV, t as createViteConfig } from "./config-B1_gk0sy.js";
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
import { existsSync } from "node:fs";
|
|
6
6
|
import { fork } from "node:child_process";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { a as printUrls, i as printShortcutsHint, o as shortcutsEnabled, r as printHeader, s as startupDuration, t as createCliLogger } from "./cli/bin.js";
|
|
2
|
-
import { t as createViteConfig } from "./config-
|
|
2
|
+
import { t as createViteConfig } from "./config-B1_gk0sy.js";
|
|
3
3
|
import { mergeConfig, preview as preview$1 } from "vite";
|
|
4
4
|
//#region src/cli/preview.ts
|
|
5
5
|
async function preview(opts = {}) {
|
package/dist/vite/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { t as createViteConfig } from "../config-
|
|
1
|
+
import { t as createViteConfig } from "../config-B1_gk0sy.js";
|
|
2
2
|
export { createViteConfig };
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useEffect } from 'react';
|
|
2
|
-
import { useLocation } from 'react-router-dom';
|
|
2
|
+
import { useLocation, useNavigate } from 'react-router-dom';
|
|
3
3
|
import { toast } from 'sonner';
|
|
4
4
|
import { format } from '@/lib/use-locale';
|
|
5
5
|
import { OfflineBanner } from '../feedback/offline-banner';
|
|
@@ -52,10 +52,21 @@ function useDecksChangedReload() {
|
|
|
52
52
|
|
|
53
53
|
function usePendingToast() {
|
|
54
54
|
const lr = useLrText();
|
|
55
|
+
const navigate = useNavigate();
|
|
55
56
|
// biome-ignore lint/correctness/useExhaustiveDependencies: shows once per page load
|
|
56
57
|
useEffect(() => {
|
|
57
58
|
const pending = takePendingToast();
|
|
58
59
|
if (!pending) return;
|
|
60
|
+
if (pending.kind === 'imported') {
|
|
61
|
+
toast.success(format(lr.share.importedToast, { title: pending.name }), {
|
|
62
|
+
duration: 8000,
|
|
63
|
+
action: {
|
|
64
|
+
label: lr.share.openAction,
|
|
65
|
+
onClick: () => navigate(`/s/${encodeURIComponent(pending.slideId)}`),
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
59
70
|
if (pending.kind === 'restored') {
|
|
60
71
|
toast.success(format(lr.home.toastRestored, { name: pending.name }));
|
|
61
72
|
return;
|
|
@@ -36,4 +36,9 @@ describe('isFeatureEnabled', () => {
|
|
|
36
36
|
expect(isFeatureEnabled('templates', { dev: true })).toBe(true);
|
|
37
37
|
expect(isFeatureEnabled('templates', { dev: false })).toBe(false);
|
|
38
38
|
});
|
|
39
|
+
|
|
40
|
+
it('keeps sharing to dev, where the pack and import endpoints run', () => {
|
|
41
|
+
expect(isFeatureEnabled('share', { dev: true })).toBe(true);
|
|
42
|
+
expect(isFeatureEnabled('share', { dev: false })).toBe(false);
|
|
43
|
+
});
|
|
39
44
|
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export type Tier = 'free' | 'pro';
|
|
2
|
-
export type FeatureId = 'board' | 'trash' | 'settings' | 'outline' | 'templates';
|
|
2
|
+
export type FeatureId = 'board' | 'trash' | 'settings' | 'outline' | 'templates' | 'share';
|
|
3
3
|
|
|
4
4
|
export type Feature = { id: FeatureId; tier: Tier; devOnly: boolean };
|
|
5
5
|
|
|
@@ -9,6 +9,7 @@ export const FEATURES: readonly Feature[] = [
|
|
|
9
9
|
{ id: 'settings', tier: 'free', devOnly: false },
|
|
10
10
|
{ id: 'outline', tier: 'free', devOnly: true },
|
|
11
11
|
{ id: 'templates', tier: 'free', devOnly: true },
|
|
12
|
+
{ id: 'share', tier: 'free', devOnly: true },
|
|
12
13
|
];
|
|
13
14
|
|
|
14
15
|
/** The single place a paid tier will hook into later; today every tier is unlocked. */
|
|
@@ -5,7 +5,11 @@ const KEY = 'lemonriver:pending-toast';
|
|
|
5
5
|
* reloads every client, and the dev server's own reload can land before the
|
|
6
6
|
* request's response does, so the toast is saved before the request is sent.
|
|
7
7
|
*/
|
|
8
|
-
export type PendingToast = {
|
|
8
|
+
export type PendingToast = {
|
|
9
|
+
kind: 'trashed' | 'restored' | 'imported';
|
|
10
|
+
slideId: string;
|
|
11
|
+
name: string;
|
|
12
|
+
};
|
|
9
13
|
|
|
10
14
|
export function savePendingToast(toast: PendingToast, storage: Storage = sessionStorage): void {
|
|
11
15
|
try {
|
|
@@ -30,7 +34,8 @@ export function takePendingToast(storage: Storage = sessionStorage): PendingToas
|
|
|
30
34
|
if (!raw) return null;
|
|
31
35
|
try {
|
|
32
36
|
const value = JSON.parse(raw) as Record<string, unknown>;
|
|
33
|
-
if (value.kind !== 'trashed' && value.kind !== 'restored'
|
|
37
|
+
if (value.kind !== 'trashed' && value.kind !== 'restored' && value.kind !== 'imported')
|
|
38
|
+
return null;
|
|
34
39
|
if (typeof value.slideId !== 'string' || typeof value.name !== 'string') return null;
|
|
35
40
|
return { kind: value.kind, slideId: value.slideId, name: value.name };
|
|
36
41
|
} catch {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Copy, FolderInput, MoreHorizontal, Palette, Pencil, Trash2 } from 'lucide-react';
|
|
1
|
+
import { Copy, FolderInput, MoreHorizontal, Palette, Pencil, Share2, Trash2 } from 'lucide-react';
|
|
2
2
|
import { useEffect, useState } from 'react';
|
|
3
3
|
import { Link } from 'react-router-dom';
|
|
4
4
|
import {
|
|
@@ -16,11 +16,13 @@ import { SlidePageProvider } from '../../lib/page-context';
|
|
|
16
16
|
import type { SlideModule } from '../../lib/sdk';
|
|
17
17
|
import { loadSlide } from '../../lib/slides';
|
|
18
18
|
import { Mascot } from '../brand/mascot';
|
|
19
|
+
import { isFeatureEnabled } from '../features';
|
|
19
20
|
import { useLrText } from '../text';
|
|
20
21
|
import { relativeTime } from './time';
|
|
21
22
|
import { useDelayed } from './use-delayed';
|
|
22
23
|
|
|
23
24
|
export type DeckCardActions = {
|
|
25
|
+
onShare: () => void;
|
|
24
26
|
onRename: () => void;
|
|
25
27
|
onDuplicate: () => void;
|
|
26
28
|
onMove: () => void;
|
|
@@ -156,6 +158,7 @@ export function DeckCard({
|
|
|
156
158
|
function DeckMenu({ actions }: { actions: DeckCardActions }) {
|
|
157
159
|
const t = useLocale();
|
|
158
160
|
const lr = useLrText();
|
|
161
|
+
const shareOn = isFeatureEnabled('share');
|
|
159
162
|
return (
|
|
160
163
|
<DropdownMenu>
|
|
161
164
|
<DropdownMenuTrigger
|
|
@@ -170,6 +173,12 @@ function DeckMenu({ actions }: { actions: DeckCardActions }) {
|
|
|
170
173
|
}
|
|
171
174
|
/>
|
|
172
175
|
<DropdownMenuContent align="end" className="min-w-[168px]">
|
|
176
|
+
{shareOn && (
|
|
177
|
+
<DropdownMenuItem onClick={actions.onShare}>
|
|
178
|
+
<Share2 />
|
|
179
|
+
{lr.share.menuItem}
|
|
180
|
+
</DropdownMenuItem>
|
|
181
|
+
)}
|
|
173
182
|
<DropdownMenuItem onClick={actions.onRename}>
|
|
174
183
|
<Pencil />
|
|
175
184
|
{t.common.rename}
|
|
@@ -19,6 +19,11 @@ import type { HomeOutletContext } from '../../routes/home-shell';
|
|
|
19
19
|
import { FilterPill } from '../chrome/filter-pill';
|
|
20
20
|
import { isFeatureEnabled } from '../features';
|
|
21
21
|
import { LemonEmpty } from '../feedback/lemon-empty';
|
|
22
|
+
import { type InspectOk, inspectPackage } from '../share/client';
|
|
23
|
+
import { shareErrorText } from '../share/codes';
|
|
24
|
+
import { ImportDialog } from '../share/import-dialog';
|
|
25
|
+
import { ImportButton, useShareDrop } from '../share/import-entry';
|
|
26
|
+
import { ShareDialog } from '../share/share-dialog';
|
|
22
27
|
import { OCCASIONS } from '../templates/model';
|
|
23
28
|
import { useTemplateLibrary } from '../templates/use-library';
|
|
24
29
|
import { useLrText } from '../text';
|
|
@@ -66,6 +71,9 @@ export function LemonRiverHome() {
|
|
|
66
71
|
const [occasion, setOccasion] = useState<string | null>(null);
|
|
67
72
|
const [sortKey, setSortKey] = useSortPref();
|
|
68
73
|
const [dialog, setDialog] = useState<Dialog>(null);
|
|
74
|
+
const [sharing, setSharing] = useState<{ slideId: string; title: string } | null>(null);
|
|
75
|
+
const [inspection, setInspection] = useState<InspectOk | null>(null);
|
|
76
|
+
const [inspecting, setInspecting] = useState(false);
|
|
69
77
|
|
|
70
78
|
const isAll = selectedId === ALL_SLIDES_ID;
|
|
71
79
|
const isDraft = selectedId === DRAFT_ID;
|
|
@@ -118,11 +126,35 @@ export function LemonRiverHome() {
|
|
|
118
126
|
}
|
|
119
127
|
};
|
|
120
128
|
|
|
129
|
+
const inspectBytes = async (bytes: Uint8Array) => {
|
|
130
|
+
if (inspecting) return;
|
|
131
|
+
setInspecting(true);
|
|
132
|
+
try {
|
|
133
|
+
const result = await inspectPackage(bytes);
|
|
134
|
+
if (result.ok) setInspection(result);
|
|
135
|
+
else
|
|
136
|
+
toast.error(
|
|
137
|
+
shareErrorText(lr.share.errors, result.code, result.appVersion, lr.share.unknownVersion),
|
|
138
|
+
);
|
|
139
|
+
} catch {
|
|
140
|
+
toast.error(lr.share.errors.notAPackage);
|
|
141
|
+
} finally {
|
|
142
|
+
setInspecting(false);
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
const { handlers: dropHandlers, hint: dropHint } = useShareDrop({
|
|
147
|
+
onBytes: (bytes) => void inspectBytes(bytes),
|
|
148
|
+
onUnknown: () => toast.error(lr.share.notAPackage),
|
|
149
|
+
});
|
|
150
|
+
|
|
121
151
|
const current = dialog ? dialog.id : null;
|
|
122
152
|
const trashOn = isFeatureEnabled('trash');
|
|
153
|
+
const shareOn = isFeatureEnabled('share');
|
|
123
154
|
|
|
124
155
|
return (
|
|
125
|
-
<div data-lr-home>
|
|
156
|
+
<div data-lr-home {...(shareOn ? dropHandlers : {})}>
|
|
157
|
+
{shareOn && dropHint}
|
|
126
158
|
{import.meta.env.DEV && isAll && (
|
|
127
159
|
<>
|
|
128
160
|
<header className="mb-6">
|
|
@@ -154,6 +186,9 @@ export function LemonRiverHome() {
|
|
|
154
186
|
</span>
|
|
155
187
|
)}
|
|
156
188
|
<div className="ml-auto flex w-full items-center gap-2 md:w-auto">
|
|
189
|
+
{shareOn && (
|
|
190
|
+
<ImportButton onBytes={(bytes) => void inspectBytes(bytes)} disabled={inspecting} />
|
|
191
|
+
)}
|
|
157
192
|
<SortControl value={sortKey} onChange={setSortKey} />
|
|
158
193
|
<SearchInput value={query} onChange={setQuery} />
|
|
159
194
|
</div>
|
|
@@ -225,6 +260,7 @@ export function LemonRiverHome() {
|
|
|
225
260
|
templateName={templateNameOf(id)}
|
|
226
261
|
onTitleResolved={ctx.reportTitle}
|
|
227
262
|
actions={{
|
|
263
|
+
onShare: () => setSharing({ slideId: id, title: titleOf(id) }),
|
|
228
264
|
onRename: () => setDialog({ kind: 'rename', id }),
|
|
229
265
|
onDuplicate: () => void duplicate(id),
|
|
230
266
|
onMove: () => setDialog({ kind: 'move', id }),
|
|
@@ -269,6 +305,21 @@ export function LemonRiverHome() {
|
|
|
269
305
|
setDialog(null);
|
|
270
306
|
}}
|
|
271
307
|
/>
|
|
308
|
+
{shareOn && (
|
|
309
|
+
<>
|
|
310
|
+
<ShareDialog
|
|
311
|
+
open={sharing !== null}
|
|
312
|
+
slideId={sharing?.slideId ?? ''}
|
|
313
|
+
title={sharing?.title ?? ''}
|
|
314
|
+
onOpenChange={(open) => {
|
|
315
|
+
if (!open) setSharing(null);
|
|
316
|
+
}}
|
|
317
|
+
/>
|
|
318
|
+
{inspection && (
|
|
319
|
+
<ImportDialog inspection={inspection} onClose={() => setInspection(null)} />
|
|
320
|
+
)}
|
|
321
|
+
</>
|
|
322
|
+
)}
|
|
272
323
|
</div>
|
|
273
324
|
);
|
|
274
325
|
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { CANVAS_HEIGHT, CANVAS_WIDTH, type SlideModule } from '../../lib/sdk';
|
|
2
|
+
import { canvasJpeg, canvasThumb, freezeForCapture } from './freeze';
|
|
3
|
+
|
|
4
|
+
const CAPTURE_CLASS = 'lr-share-capture';
|
|
5
|
+
const JPEG_QUALITY = 0.88;
|
|
6
|
+
const THUMB_WIDTH = 480;
|
|
7
|
+
const THUMB_HEIGHT = 270;
|
|
8
|
+
const THUMB_QUALITY = 0.8;
|
|
9
|
+
|
|
10
|
+
export type CapturedPage = { jpeg: Uint8Array; thumb: Uint8Array };
|
|
11
|
+
|
|
12
|
+
export class CaptureCancelled extends Error {
|
|
13
|
+
constructor() {
|
|
14
|
+
super('capture cancelled');
|
|
15
|
+
this.name = 'CaptureCancelled';
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function aborted(signal: AbortSignal): boolean {
|
|
20
|
+
return signal.aborted;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function toBase64(bytes: Uint8Array): string {
|
|
24
|
+
let binary = '';
|
|
25
|
+
const chunk = 0x8000;
|
|
26
|
+
for (let i = 0; i < bytes.length; i += chunk) {
|
|
27
|
+
binary += String.fromCharCode(...bytes.subarray(i, i + chunk));
|
|
28
|
+
}
|
|
29
|
+
return btoa(binary);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Draws every page off-screen and rasterises it twice: the full-size JPEG that
|
|
34
|
+
* becomes a PDF page, and a small one for the preview inside the package.
|
|
35
|
+
*/
|
|
36
|
+
export async function captureDeck(
|
|
37
|
+
slide: SlideModule,
|
|
38
|
+
opts: {
|
|
39
|
+
onPage: (current: number, total: number) => void;
|
|
40
|
+
signal: AbortSignal;
|
|
41
|
+
},
|
|
42
|
+
): Promise<CapturedPage[]> {
|
|
43
|
+
const pages = slide.default ?? [];
|
|
44
|
+
const total = pages.length;
|
|
45
|
+
if (total === 0) throw new Error('this deck has no pages');
|
|
46
|
+
const [{ mountDeckOffscreen }, { toCanvas }] = await Promise.all([
|
|
47
|
+
import('../../lib/pptx/render'),
|
|
48
|
+
import('html-to-image'),
|
|
49
|
+
]);
|
|
50
|
+
if (aborted(opts.signal)) throw new CaptureCancelled();
|
|
51
|
+
|
|
52
|
+
const mounted = await mountDeckOffscreen(slide, CAPTURE_CLASS);
|
|
53
|
+
const captured: CapturedPage[] = [];
|
|
54
|
+
try {
|
|
55
|
+
for (let i = 0; i < mounted.frames.length; i++) {
|
|
56
|
+
if (aborted(opts.signal)) throw new CaptureCancelled();
|
|
57
|
+
const frame = mounted.frames[i];
|
|
58
|
+
if (!frame) continue;
|
|
59
|
+
freezeForCapture(frame);
|
|
60
|
+
const canvas = await toCanvas(frame, {
|
|
61
|
+
width: CANVAS_WIDTH,
|
|
62
|
+
height: CANVAS_HEIGHT,
|
|
63
|
+
pixelRatio: 1,
|
|
64
|
+
backgroundColor: '#ffffff',
|
|
65
|
+
cacheBust: true,
|
|
66
|
+
});
|
|
67
|
+
const jpeg = await canvasJpeg(canvas, JPEG_QUALITY);
|
|
68
|
+
const thumb = await canvasThumb(canvas, THUMB_WIDTH, THUMB_HEIGHT, THUMB_QUALITY);
|
|
69
|
+
captured.push({ jpeg, thumb });
|
|
70
|
+
opts.onPage(i + 1, total);
|
|
71
|
+
}
|
|
72
|
+
} finally {
|
|
73
|
+
mounted.dispose();
|
|
74
|
+
}
|
|
75
|
+
return captured;
|
|
76
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import type { DeckChanges } from './compare';
|
|
2
|
+
|
|
3
|
+
/** The browser's side of the share endpoints. */
|
|
4
|
+
|
|
5
|
+
export type ShareExtra = { name: string; base64: string };
|
|
6
|
+
|
|
7
|
+
export type PackResponse = {
|
|
8
|
+
ok: true;
|
|
9
|
+
path: string;
|
|
10
|
+
name: string;
|
|
11
|
+
size: number;
|
|
12
|
+
warnings: string[];
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type PackFailure = { ok: false; error: string; paths?: string[] };
|
|
16
|
+
|
|
17
|
+
export type InspectDeck = {
|
|
18
|
+
slideId: string;
|
|
19
|
+
title: string;
|
|
20
|
+
pages: number | null;
|
|
21
|
+
thumbs: number;
|
|
22
|
+
theme: string | null;
|
|
23
|
+
existing: { slideId: string; title: string } | null;
|
|
24
|
+
idTaken: boolean;
|
|
25
|
+
changes: DeckChanges | null;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export type InspectOk = {
|
|
29
|
+
ok: true;
|
|
30
|
+
token: string;
|
|
31
|
+
kind: 'share' | 'backup';
|
|
32
|
+
app: { name: string; version: string };
|
|
33
|
+
createdAt: number;
|
|
34
|
+
decks: InspectDeck[];
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export type InspectFailure = {
|
|
38
|
+
ok: false;
|
|
39
|
+
code: string;
|
|
40
|
+
detail: string;
|
|
41
|
+
paths?: string[];
|
|
42
|
+
appVersion?: string;
|
|
43
|
+
status: number;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
async function readJson<T>(res: Response): Promise<T> {
|
|
47
|
+
return (await res.json().catch(() => ({}))) as T;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export async function appVersion(): Promise<string> {
|
|
51
|
+
const res = await fetch('/__lr/share/version', { headers: { accept: 'application/json' } });
|
|
52
|
+
const data = await readJson<{ version?: string }>(res);
|
|
53
|
+
return typeof data.version === 'string' ? data.version : '';
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export async function packShare(input: {
|
|
57
|
+
slideIds: string[];
|
|
58
|
+
pages: Record<string, number>;
|
|
59
|
+
thumbs: Record<string, string[]>;
|
|
60
|
+
extras: ShareExtra[];
|
|
61
|
+
}): Promise<PackResponse | PackFailure> {
|
|
62
|
+
const res = await fetch('/__lr/share/pack', {
|
|
63
|
+
method: 'POST',
|
|
64
|
+
headers: { 'content-type': 'application/json' },
|
|
65
|
+
body: JSON.stringify({ ...input, kind: 'share' }),
|
|
66
|
+
});
|
|
67
|
+
const data = await readJson<{
|
|
68
|
+
ok?: boolean;
|
|
69
|
+
path?: string;
|
|
70
|
+
name?: string;
|
|
71
|
+
size?: number;
|
|
72
|
+
warnings?: string[];
|
|
73
|
+
error?: string;
|
|
74
|
+
paths?: string[];
|
|
75
|
+
}>(res);
|
|
76
|
+
if (!res.ok || data.ok !== true || typeof data.path !== 'string') {
|
|
77
|
+
return { ok: false, error: data.error ?? `HTTP ${res.status}`, paths: data.paths ?? [] };
|
|
78
|
+
}
|
|
79
|
+
return {
|
|
80
|
+
ok: true,
|
|
81
|
+
path: data.path,
|
|
82
|
+
name: typeof data.name === 'string' ? data.name : '',
|
|
83
|
+
size: typeof data.size === 'number' ? data.size : 0,
|
|
84
|
+
warnings: Array.isArray(data.warnings) ? data.warnings : [],
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export async function revealShared(path: string): Promise<boolean> {
|
|
89
|
+
const res = await fetch('/__lr/share/reveal', {
|
|
90
|
+
method: 'POST',
|
|
91
|
+
headers: { 'content-type': 'application/json' },
|
|
92
|
+
body: JSON.stringify({ path }),
|
|
93
|
+
});
|
|
94
|
+
return res.ok;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export async function inspectPackage(bytes: Uint8Array): Promise<InspectOk | InspectFailure> {
|
|
98
|
+
const res = await fetch('/__lr/share/inspect', {
|
|
99
|
+
method: 'POST',
|
|
100
|
+
headers: { 'content-type': 'application/zip' },
|
|
101
|
+
body: new Blob([bytes as BlobPart], { type: 'application/zip' }),
|
|
102
|
+
});
|
|
103
|
+
const data = await readJson<{
|
|
104
|
+
ok?: boolean;
|
|
105
|
+
token?: string;
|
|
106
|
+
kind?: 'share' | 'backup';
|
|
107
|
+
app?: { name: string; version: string };
|
|
108
|
+
createdAt?: number;
|
|
109
|
+
decks?: InspectDeck[];
|
|
110
|
+
code?: string;
|
|
111
|
+
detail?: string;
|
|
112
|
+
paths?: string[];
|
|
113
|
+
appVersion?: string;
|
|
114
|
+
}>(res);
|
|
115
|
+
if (!res.ok || data.ok !== true || typeof data.token !== 'string') {
|
|
116
|
+
return {
|
|
117
|
+
ok: false,
|
|
118
|
+
status: res.status,
|
|
119
|
+
code: typeof data.code === 'string' ? data.code : 'bad-manifest',
|
|
120
|
+
detail: typeof data.detail === 'string' ? data.detail : `HTTP ${res.status}`,
|
|
121
|
+
paths: Array.isArray(data.paths) ? data.paths : [],
|
|
122
|
+
appVersion: typeof data.appVersion === 'string' ? data.appVersion : '',
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
return {
|
|
126
|
+
ok: true,
|
|
127
|
+
token: data.token,
|
|
128
|
+
kind: data.kind === 'backup' ? 'backup' : 'share',
|
|
129
|
+
app: data.app ?? { name: 'LemonRiver', version: '' },
|
|
130
|
+
createdAt: typeof data.createdAt === 'number' ? data.createdAt : 0,
|
|
131
|
+
decks: (Array.isArray(data.decks) ? data.decks : []).map((deck) => ({
|
|
132
|
+
...deck,
|
|
133
|
+
changes: deck.changes ?? null,
|
|
134
|
+
})),
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export type ImportedDeck = { from: string; slideId: string };
|
|
139
|
+
|
|
140
|
+
export async function importPackages(
|
|
141
|
+
token: string,
|
|
142
|
+
decks: { slideId: string; mode: 'copy' | 'replace' }[],
|
|
143
|
+
): Promise<{ imported: ImportedDeck[]; warnings: string[] }> {
|
|
144
|
+
const res = await fetch('/__lr/share/import', {
|
|
145
|
+
method: 'POST',
|
|
146
|
+
headers: { 'content-type': 'application/json' },
|
|
147
|
+
body: JSON.stringify({ token, decks }),
|
|
148
|
+
});
|
|
149
|
+
const data = await readJson<{
|
|
150
|
+
ok?: boolean;
|
|
151
|
+
imported?: ImportedDeck[];
|
|
152
|
+
warnings?: string[];
|
|
153
|
+
error?: string;
|
|
154
|
+
}>(res);
|
|
155
|
+
if (!res.ok || data.ok !== true) throw new Error(data.error ?? `HTTP ${res.status}`);
|
|
156
|
+
return { imported: data.imported ?? [], warnings: data.warnings ?? [] };
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function thumbUrl(token: string, slideId: string, page: number): string {
|
|
160
|
+
const name = String(page).padStart(2, '0');
|
|
161
|
+
return `/__lr/share/inbox/${encodeURIComponent(token)}/thumbs/${encodeURIComponent(slideId)}/${name}.jpg`;
|
|
162
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { en } from '../text/en';
|
|
3
|
+
import { es } from '../text/es';
|
|
4
|
+
import { ja } from '../text/ja';
|
|
5
|
+
import { zhCN } from '../text/zh-cn';
|
|
6
|
+
import { zhTW } from '../text/zh-tw';
|
|
7
|
+
import { shareErrorText } from './codes';
|
|
8
|
+
import type { ShareCode } from './model';
|
|
9
|
+
|
|
10
|
+
const CODES: ShareCode[] = [
|
|
11
|
+
'not-a-package',
|
|
12
|
+
'bad-manifest',
|
|
13
|
+
'newer-version',
|
|
14
|
+
'bad-path',
|
|
15
|
+
'blocked-file',
|
|
16
|
+
'checksum',
|
|
17
|
+
'missing-file',
|
|
18
|
+
'extra-file',
|
|
19
|
+
'too-large',
|
|
20
|
+
'too-many-files',
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
describe('shareErrorText', () => {
|
|
24
|
+
it('has a sentence for every code in every language', () => {
|
|
25
|
+
for (const text of [zhCN, zhTW, en, ja, es]) {
|
|
26
|
+
for (const code of CODES) {
|
|
27
|
+
const message = shareErrorText(text.share.errors, code, '', text.share.unknownVersion);
|
|
28
|
+
expect(message, code).not.toContain('{');
|
|
29
|
+
expect(message.length, code).toBeGreaterThan(4);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('names the version that made a package we cannot read yet', () => {
|
|
35
|
+
expect(shareErrorText(en.share.errors, 'newer-version', '9.9.9')).toBe(
|
|
36
|
+
'This package was made by a newer LemonRiver (9.9.9). Update first, then import.',
|
|
37
|
+
);
|
|
38
|
+
expect(shareErrorText(zhCN.share.errors, 'newer-version', '9.9.9')).toBe(
|
|
39
|
+
'这个包是用更新的 LemonRiver(9.9.9)做的,先更新再导入。',
|
|
40
|
+
);
|
|
41
|
+
expect(
|
|
42
|
+
shareErrorText(zhCN.share.errors, 'newer-version', '', zhCN.share.unknownVersion),
|
|
43
|
+
).toContain('未知版本');
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('treats a code it has never heard of as a broken package', () => {
|
|
47
|
+
expect(shareErrorText(en.share.errors, 'something-new')).toBe(en.share.errors.badManifest);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { format } from '../../../locale/format';
|
|
2
|
+
import type { LrText } from '../text/types';
|
|
3
|
+
import type { ShareCode } from './model';
|
|
4
|
+
|
|
5
|
+
type Errors = LrText['share']['errors'];
|
|
6
|
+
|
|
7
|
+
const KEYS: Record<ShareCode, keyof Errors> = {
|
|
8
|
+
'not-a-package': 'notAPackage',
|
|
9
|
+
'bad-manifest': 'badManifest',
|
|
10
|
+
'newer-version': 'newerVersion',
|
|
11
|
+
'bad-path': 'badPath',
|
|
12
|
+
'blocked-file': 'blockedFile',
|
|
13
|
+
checksum: 'checksum',
|
|
14
|
+
'missing-file': 'missingFile',
|
|
15
|
+
'extra-file': 'extraFile',
|
|
16
|
+
'too-large': 'tooLarge',
|
|
17
|
+
'too-many-files': 'tooManyFiles',
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export function shareErrorText(
|
|
21
|
+
errors: Errors,
|
|
22
|
+
code: string,
|
|
23
|
+
appVersion = '',
|
|
24
|
+
unknownVersion = '',
|
|
25
|
+
): string {
|
|
26
|
+
const key = KEYS[code as ShareCode] ?? 'badManifest';
|
|
27
|
+
return format(errors[key], {
|
|
28
|
+
version: appVersion !== '' ? appVersion : unknownVersion,
|
|
29
|
+
});
|
|
30
|
+
}
|