@open-slide/core 0.0.8 → 0.0.10
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-CXY2DSzy.js → build-DHiRlpjn.js} +1 -1
- package/dist/cli/bin.js +3 -3
- package/dist/{config-BYTf0qVz.js → config-LZM903FE.js} +742 -44
- package/dist/{dev-BxCKugi3.js → dev-B3JzCYn7.js} +1 -1
- package/dist/{preview-C1F-rHfx.js → preview-UikovHEt.js} +1 -1
- package/dist/vite/index.js +1 -1
- package/package.json +3 -1
- package/src/app/App.tsx +2 -0
- package/src/app/components/AssetView.tsx +846 -0
- package/src/app/components/ClickNavZones.tsx +2 -2
- package/src/app/components/PdfProgressToast.tsx +23 -0
- package/src/app/components/ThumbnailRail.tsx +2 -2
- package/src/app/components/inspector/CommentWidget.tsx +1 -1
- package/src/app/components/inspector/InspectOverlay.tsx +81 -41
- package/src/app/components/inspector/InspectorPanel.tsx +948 -0
- package/src/app/components/inspector/InspectorProvider.tsx +229 -13
- package/src/app/components/inspector/SaveBar.tsx +77 -0
- package/src/app/components/ui/input.tsx +21 -0
- package/src/app/components/ui/label.tsx +24 -0
- package/src/app/components/ui/progress.tsx +31 -0
- package/src/app/components/ui/select.tsx +190 -0
- package/src/app/components/ui/slider.tsx +61 -0
- package/src/app/components/ui/sonner.tsx +38 -0
- package/src/app/components/ui/textarea.tsx +18 -0
- package/src/app/components/ui/toggle-group.tsx +83 -0
- package/src/app/components/ui/toggle.tsx +45 -0
- package/src/app/components/ui/tooltip.tsx +55 -0
- package/src/app/lib/assets.ts +166 -0
- package/src/app/lib/export-pdf.ts +194 -0
- package/src/app/lib/inspector/fiber.ts +40 -5
- package/src/app/lib/inspector/useEditor.ts +62 -0
- package/src/app/lib/print-ready.ts +58 -0
- package/src/app/routes/Slide.tsx +140 -51
- package/src/app/components/inspector/CommentPopover.tsx +0 -94
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CircleCheckIcon,
|
|
3
|
+
InfoIcon,
|
|
4
|
+
Loader2Icon,
|
|
5
|
+
OctagonXIcon,
|
|
6
|
+
TriangleAlertIcon,
|
|
7
|
+
} from "lucide-react"
|
|
8
|
+
import { useTheme } from "next-themes"
|
|
9
|
+
import { Toaster as Sonner, type ToasterProps } from "sonner"
|
|
10
|
+
|
|
11
|
+
const Toaster = ({ ...props }: ToasterProps) => {
|
|
12
|
+
const { theme = "system" } = useTheme()
|
|
13
|
+
|
|
14
|
+
return (
|
|
15
|
+
<Sonner
|
|
16
|
+
theme={theme as ToasterProps["theme"]}
|
|
17
|
+
className="toaster group"
|
|
18
|
+
icons={{
|
|
19
|
+
success: <CircleCheckIcon className="size-4" />,
|
|
20
|
+
info: <InfoIcon className="size-4" />,
|
|
21
|
+
warning: <TriangleAlertIcon className="size-4" />,
|
|
22
|
+
error: <OctagonXIcon className="size-4" />,
|
|
23
|
+
loading: <Loader2Icon className="size-4 animate-spin" />,
|
|
24
|
+
}}
|
|
25
|
+
style={
|
|
26
|
+
{
|
|
27
|
+
"--normal-bg": "var(--popover)",
|
|
28
|
+
"--normal-text": "var(--popover-foreground)",
|
|
29
|
+
"--normal-border": "var(--border)",
|
|
30
|
+
"--border-radius": "var(--radius)",
|
|
31
|
+
} as React.CSSProperties
|
|
32
|
+
}
|
|
33
|
+
{...props}
|
|
34
|
+
/>
|
|
35
|
+
)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export { Toaster }
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import * as React from "react"
|
|
2
|
+
|
|
3
|
+
import { cn } from "@/lib/utils"
|
|
4
|
+
|
|
5
|
+
function Textarea({ className, ...props }: React.ComponentProps<"textarea">) {
|
|
6
|
+
return (
|
|
7
|
+
<textarea
|
|
8
|
+
data-slot="textarea"
|
|
9
|
+
className={cn(
|
|
10
|
+
"flex field-sizing-content min-h-16 w-full rounded-md border border-input bg-transparent px-3 py-2 text-base shadow-xs transition-[color,box-shadow] outline-none placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-destructive/20 md:text-sm dark:bg-input/30 dark:aria-invalid:ring-destructive/40",
|
|
11
|
+
className
|
|
12
|
+
)}
|
|
13
|
+
{...props}
|
|
14
|
+
/>
|
|
15
|
+
)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export { Textarea }
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import { type VariantProps } from "class-variance-authority"
|
|
5
|
+
import { ToggleGroup as ToggleGroupPrimitive } from "radix-ui"
|
|
6
|
+
|
|
7
|
+
import { cn } from "@/lib/utils"
|
|
8
|
+
import { toggleVariants } from "@/components/ui/toggle"
|
|
9
|
+
|
|
10
|
+
const ToggleGroupContext = React.createContext<
|
|
11
|
+
VariantProps<typeof toggleVariants> & {
|
|
12
|
+
spacing?: number
|
|
13
|
+
}
|
|
14
|
+
>({
|
|
15
|
+
size: "default",
|
|
16
|
+
variant: "default",
|
|
17
|
+
spacing: 0,
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
function ToggleGroup({
|
|
21
|
+
className,
|
|
22
|
+
variant,
|
|
23
|
+
size,
|
|
24
|
+
spacing = 0,
|
|
25
|
+
children,
|
|
26
|
+
...props
|
|
27
|
+
}: React.ComponentProps<typeof ToggleGroupPrimitive.Root> &
|
|
28
|
+
VariantProps<typeof toggleVariants> & {
|
|
29
|
+
spacing?: number
|
|
30
|
+
}) {
|
|
31
|
+
return (
|
|
32
|
+
<ToggleGroupPrimitive.Root
|
|
33
|
+
data-slot="toggle-group"
|
|
34
|
+
data-variant={variant}
|
|
35
|
+
data-size={size}
|
|
36
|
+
data-spacing={spacing}
|
|
37
|
+
style={{ "--gap": spacing } as React.CSSProperties}
|
|
38
|
+
className={cn(
|
|
39
|
+
"group/toggle-group flex w-fit items-center gap-[--spacing(var(--gap))] rounded-md data-[spacing=default]:data-[variant=outline]:shadow-xs",
|
|
40
|
+
className
|
|
41
|
+
)}
|
|
42
|
+
{...props}
|
|
43
|
+
>
|
|
44
|
+
<ToggleGroupContext.Provider value={{ variant, size, spacing }}>
|
|
45
|
+
{children}
|
|
46
|
+
</ToggleGroupContext.Provider>
|
|
47
|
+
</ToggleGroupPrimitive.Root>
|
|
48
|
+
)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function ToggleGroupItem({
|
|
52
|
+
className,
|
|
53
|
+
children,
|
|
54
|
+
variant,
|
|
55
|
+
size,
|
|
56
|
+
...props
|
|
57
|
+
}: React.ComponentProps<typeof ToggleGroupPrimitive.Item> &
|
|
58
|
+
VariantProps<typeof toggleVariants>) {
|
|
59
|
+
const context = React.useContext(ToggleGroupContext)
|
|
60
|
+
|
|
61
|
+
return (
|
|
62
|
+
<ToggleGroupPrimitive.Item
|
|
63
|
+
data-slot="toggle-group-item"
|
|
64
|
+
data-variant={context.variant || variant}
|
|
65
|
+
data-size={context.size || size}
|
|
66
|
+
data-spacing={context.spacing}
|
|
67
|
+
className={cn(
|
|
68
|
+
toggleVariants({
|
|
69
|
+
variant: context.variant || variant,
|
|
70
|
+
size: context.size || size,
|
|
71
|
+
}),
|
|
72
|
+
"w-auto min-w-0 shrink-0 px-3 focus:z-10 focus-visible:z-10",
|
|
73
|
+
"data-[spacing=0]:rounded-none data-[spacing=0]:shadow-none data-[spacing=0]:first:rounded-l-md data-[spacing=0]:last:rounded-r-md data-[spacing=0]:data-[variant=outline]:border-l-0 data-[spacing=0]:data-[variant=outline]:first:border-l",
|
|
74
|
+
className
|
|
75
|
+
)}
|
|
76
|
+
{...props}
|
|
77
|
+
>
|
|
78
|
+
{children}
|
|
79
|
+
</ToggleGroupPrimitive.Item>
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export { ToggleGroup, ToggleGroupItem }
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import * as React from "react"
|
|
2
|
+
import { cva, type VariantProps } from "class-variance-authority"
|
|
3
|
+
import { Toggle as TogglePrimitive } from "radix-ui"
|
|
4
|
+
|
|
5
|
+
import { cn } from "@/lib/utils"
|
|
6
|
+
|
|
7
|
+
const toggleVariants = cva(
|
|
8
|
+
"inline-flex items-center justify-center gap-2 rounded-md text-sm font-medium whitespace-nowrap transition-[color,box-shadow] outline-none hover:bg-muted hover:text-muted-foreground focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:pointer-events-none disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-destructive/20 data-[state=on]:bg-accent data-[state=on]:text-accent-foreground dark:aria-invalid:ring-destructive/40 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
|
|
9
|
+
{
|
|
10
|
+
variants: {
|
|
11
|
+
variant: {
|
|
12
|
+
default: "bg-transparent",
|
|
13
|
+
outline:
|
|
14
|
+
"border border-input bg-transparent shadow-xs hover:bg-accent hover:text-accent-foreground",
|
|
15
|
+
},
|
|
16
|
+
size: {
|
|
17
|
+
default: "h-9 min-w-9 px-2",
|
|
18
|
+
sm: "h-8 min-w-8 px-1.5",
|
|
19
|
+
lg: "h-10 min-w-10 px-2.5",
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
defaultVariants: {
|
|
23
|
+
variant: "default",
|
|
24
|
+
size: "default",
|
|
25
|
+
},
|
|
26
|
+
}
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
function Toggle({
|
|
30
|
+
className,
|
|
31
|
+
variant,
|
|
32
|
+
size,
|
|
33
|
+
...props
|
|
34
|
+
}: React.ComponentProps<typeof TogglePrimitive.Root> &
|
|
35
|
+
VariantProps<typeof toggleVariants>) {
|
|
36
|
+
return (
|
|
37
|
+
<TogglePrimitive.Root
|
|
38
|
+
data-slot="toggle"
|
|
39
|
+
className={cn(toggleVariants({ variant, size, className }))}
|
|
40
|
+
{...props}
|
|
41
|
+
/>
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export { Toggle, toggleVariants }
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import * as React from "react"
|
|
2
|
+
import { Tooltip as TooltipPrimitive } from "radix-ui"
|
|
3
|
+
|
|
4
|
+
import { cn } from "@/lib/utils"
|
|
5
|
+
|
|
6
|
+
function TooltipProvider({
|
|
7
|
+
delayDuration = 0,
|
|
8
|
+
...props
|
|
9
|
+
}: React.ComponentProps<typeof TooltipPrimitive.Provider>) {
|
|
10
|
+
return (
|
|
11
|
+
<TooltipPrimitive.Provider
|
|
12
|
+
data-slot="tooltip-provider"
|
|
13
|
+
delayDuration={delayDuration}
|
|
14
|
+
{...props}
|
|
15
|
+
/>
|
|
16
|
+
)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function Tooltip({
|
|
20
|
+
...props
|
|
21
|
+
}: React.ComponentProps<typeof TooltipPrimitive.Root>) {
|
|
22
|
+
return <TooltipPrimitive.Root data-slot="tooltip" {...props} />
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function TooltipTrigger({
|
|
26
|
+
...props
|
|
27
|
+
}: React.ComponentProps<typeof TooltipPrimitive.Trigger>) {
|
|
28
|
+
return <TooltipPrimitive.Trigger data-slot="tooltip-trigger" {...props} />
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function TooltipContent({
|
|
32
|
+
className,
|
|
33
|
+
sideOffset = 0,
|
|
34
|
+
children,
|
|
35
|
+
...props
|
|
36
|
+
}: React.ComponentProps<typeof TooltipPrimitive.Content>) {
|
|
37
|
+
return (
|
|
38
|
+
<TooltipPrimitive.Portal>
|
|
39
|
+
<TooltipPrimitive.Content
|
|
40
|
+
data-slot="tooltip-content"
|
|
41
|
+
sideOffset={sideOffset}
|
|
42
|
+
className={cn(
|
|
43
|
+
"z-50 w-fit origin-(--radix-tooltip-content-transform-origin) animate-in rounded-md bg-foreground px-3 py-1.5 text-xs text-balance text-background fade-in-0 zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95",
|
|
44
|
+
className
|
|
45
|
+
)}
|
|
46
|
+
{...props}
|
|
47
|
+
>
|
|
48
|
+
{children}
|
|
49
|
+
<TooltipPrimitive.Arrow className="z-50 size-2.5 translate-y-[calc(-50%_-_2px)] rotate-45 rounded-[2px] bg-foreground fill-foreground" />
|
|
50
|
+
</TooltipPrimitive.Content>
|
|
51
|
+
</TooltipPrimitive.Portal>
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export { Tooltip, TooltipTrigger, TooltipContent, TooltipProvider }
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { useCallback, useEffect, useState } from 'react';
|
|
2
|
+
|
|
3
|
+
export type AssetEntry = {
|
|
4
|
+
name: string;
|
|
5
|
+
size: number;
|
|
6
|
+
mtime: number;
|
|
7
|
+
mime: string;
|
|
8
|
+
url: string;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type UploadOptions = { overwrite?: boolean };
|
|
12
|
+
|
|
13
|
+
async function listAssets(slideId: string): Promise<AssetEntry[]> {
|
|
14
|
+
const res = await fetch(`/__assets/${slideId}`);
|
|
15
|
+
if (!res.ok) throw new Error(`GET /__assets/${slideId} ${res.status}`);
|
|
16
|
+
const data = (await res.json()) as { assets?: AssetEntry[] };
|
|
17
|
+
return data.assets ?? [];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async function uploadAsset(
|
|
21
|
+
slideId: string,
|
|
22
|
+
file: File,
|
|
23
|
+
opts: UploadOptions = {},
|
|
24
|
+
): Promise<Response> {
|
|
25
|
+
const qs = opts.overwrite ? '?overwrite=1' : '';
|
|
26
|
+
return fetch(`/__assets/${slideId}/${encodeURIComponent(file.name)}${qs}`, {
|
|
27
|
+
method: 'POST',
|
|
28
|
+
headers: {
|
|
29
|
+
'content-type': file.type || 'application/octet-stream',
|
|
30
|
+
'content-length': String(file.size),
|
|
31
|
+
},
|
|
32
|
+
body: file,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async function renameAsset(slideId: string, from: string, to: string): Promise<Response> {
|
|
37
|
+
return fetch(`/__assets/${slideId}/${encodeURIComponent(from)}`, {
|
|
38
|
+
method: 'PATCH',
|
|
39
|
+
headers: { 'content-type': 'application/json' },
|
|
40
|
+
body: JSON.stringify({ name: to }),
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function deleteAsset(slideId: string, name: string): Promise<Response> {
|
|
45
|
+
return fetch(`/__assets/${slideId}/${encodeURIComponent(name)}`, { method: 'DELETE' });
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export type SvglItem = {
|
|
49
|
+
id: number;
|
|
50
|
+
title: string;
|
|
51
|
+
category: string | string[];
|
|
52
|
+
route: string | { light: string; dark: string };
|
|
53
|
+
url: string;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export async function searchSvgl(query: string, signal?: AbortSignal): Promise<SvglItem[]> {
|
|
57
|
+
const q = query.trim();
|
|
58
|
+
const params = new URLSearchParams();
|
|
59
|
+
if (q) params.set('q', q);
|
|
60
|
+
else params.set('limit', '24');
|
|
61
|
+
const res = await fetch(`/__svgl/search?${params.toString()}`, { signal });
|
|
62
|
+
// svgl returns 404 when a search has no matches — treat it as an empty list,
|
|
63
|
+
// not an error.
|
|
64
|
+
if (res.status === 404) return [];
|
|
65
|
+
if (!res.ok) throw new Error(`svgl ${res.status}`);
|
|
66
|
+
return (await res.json()) as SvglItem[];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function svgProxyUrl(routeUrl: string): string {
|
|
70
|
+
return `/__svgl/svg?u=${encodeURIComponent(routeUrl)}`;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export async function fetchSvgAsFile(routeUrl: string, filename: string): Promise<File> {
|
|
74
|
+
const res = await fetch(svgProxyUrl(routeUrl));
|
|
75
|
+
if (!res.ok) throw new Error(`svgl route ${res.status}`);
|
|
76
|
+
const blob = await res.blob();
|
|
77
|
+
return new File([blob], filename, { type: 'image/svg+xml' });
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export type UseAssetsResult = {
|
|
81
|
+
assets: AssetEntry[];
|
|
82
|
+
loading: boolean;
|
|
83
|
+
available: boolean;
|
|
84
|
+
upload: (file: File, opts?: UploadOptions) => Promise<{ ok: boolean; status: number }>;
|
|
85
|
+
rename: (from: string, to: string) => Promise<{ ok: boolean; status: number }>;
|
|
86
|
+
remove: (name: string) => Promise<{ ok: boolean; status: number }>;
|
|
87
|
+
refresh: () => Promise<void>;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const NOOP_RESULT = { ok: false, status: 0 } as const;
|
|
91
|
+
|
|
92
|
+
export function useAssets(slideId: string): UseAssetsResult {
|
|
93
|
+
const available = import.meta.env.DEV;
|
|
94
|
+
const [assets, setAssets] = useState<AssetEntry[]>([]);
|
|
95
|
+
const [loading, setLoading] = useState(available);
|
|
96
|
+
|
|
97
|
+
const refresh = useCallback(async () => {
|
|
98
|
+
if (!available) return;
|
|
99
|
+
const next = await listAssets(slideId);
|
|
100
|
+
setAssets(next);
|
|
101
|
+
}, [slideId]);
|
|
102
|
+
|
|
103
|
+
useEffect(() => {
|
|
104
|
+
if (!available) return;
|
|
105
|
+
let cancelled = false;
|
|
106
|
+
setLoading(true);
|
|
107
|
+
listAssets(slideId)
|
|
108
|
+
.then((next) => {
|
|
109
|
+
if (!cancelled) {
|
|
110
|
+
setAssets(next);
|
|
111
|
+
setLoading(false);
|
|
112
|
+
}
|
|
113
|
+
})
|
|
114
|
+
.catch(() => {
|
|
115
|
+
if (!cancelled) setLoading(false);
|
|
116
|
+
});
|
|
117
|
+
return () => {
|
|
118
|
+
cancelled = true;
|
|
119
|
+
};
|
|
120
|
+
}, [slideId]);
|
|
121
|
+
|
|
122
|
+
useEffect(() => {
|
|
123
|
+
if (!available || !import.meta.hot) return;
|
|
124
|
+
const handler = (data: { slideId?: string } | undefined) => {
|
|
125
|
+
if (!data || data.slideId === slideId) {
|
|
126
|
+
refresh().catch(() => {});
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
import.meta.hot.on('open-slide:assets-changed', handler);
|
|
130
|
+
return () => {
|
|
131
|
+
import.meta.hot?.off('open-slide:assets-changed', handler);
|
|
132
|
+
};
|
|
133
|
+
}, [slideId, refresh]);
|
|
134
|
+
|
|
135
|
+
const upload = useCallback(
|
|
136
|
+
async (file: File, opts?: UploadOptions) => {
|
|
137
|
+
if (!available) return NOOP_RESULT;
|
|
138
|
+
const res = await uploadAsset(slideId, file, opts);
|
|
139
|
+
if (res.ok) await refresh();
|
|
140
|
+
return { ok: res.ok, status: res.status };
|
|
141
|
+
},
|
|
142
|
+
[slideId, refresh],
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
const rename = useCallback(
|
|
146
|
+
async (from: string, to: string) => {
|
|
147
|
+
if (!available) return NOOP_RESULT;
|
|
148
|
+
const res = await renameAsset(slideId, from, to);
|
|
149
|
+
if (res.ok) await refresh();
|
|
150
|
+
return { ok: res.ok, status: res.status };
|
|
151
|
+
},
|
|
152
|
+
[slideId, refresh],
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
const remove = useCallback(
|
|
156
|
+
async (name: string) => {
|
|
157
|
+
if (!available) return NOOP_RESULT;
|
|
158
|
+
const res = await deleteAsset(slideId, name);
|
|
159
|
+
if (res.ok) await refresh();
|
|
160
|
+
return { ok: res.ok, status: res.status };
|
|
161
|
+
},
|
|
162
|
+
[slideId, refresh],
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
return { assets, loading, available, upload, rename, remove, refresh };
|
|
166
|
+
}
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
// Exports a slide as a PDF via the browser's native print engine.
|
|
2
|
+
// Each page in `slide.default` becomes one PDF page at 1920×1080.
|
|
3
|
+
// Text stays selectable and inline SVG remains vector — `window.print()`
|
|
4
|
+
// preserves both. The user picks "Save as PDF" in the print dialog.
|
|
5
|
+
|
|
6
|
+
import { createElement } from 'react';
|
|
7
|
+
import { createRoot, type Root } from 'react-dom/client';
|
|
8
|
+
import { isFrameAnimationSettled, waitForDataWaitfor, waitForFonts } from './print-ready';
|
|
9
|
+
import type { SlideModule } from './sdk';
|
|
10
|
+
|
|
11
|
+
const PRINT_ROOT_ID = 'os-print-root';
|
|
12
|
+
const PRINT_STYLE_ID = 'os-print-style';
|
|
13
|
+
|
|
14
|
+
const PRINT_STYLES = `
|
|
15
|
+
@page { size: 1920px 1080px; margin: 0; }
|
|
16
|
+
|
|
17
|
+
@media screen {
|
|
18
|
+
#${PRINT_ROOT_ID} {
|
|
19
|
+
position: fixed !important;
|
|
20
|
+
left: -99999px !important;
|
|
21
|
+
top: 0 !important;
|
|
22
|
+
pointer-events: none !important;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
@media print {
|
|
27
|
+
html, body {
|
|
28
|
+
margin: 0 !important;
|
|
29
|
+
padding: 0 !important;
|
|
30
|
+
background: #fff !important;
|
|
31
|
+
}
|
|
32
|
+
body > *:not(#${PRINT_ROOT_ID}) { display: none !important; }
|
|
33
|
+
#${PRINT_ROOT_ID} {
|
|
34
|
+
position: static !important;
|
|
35
|
+
left: 0 !important;
|
|
36
|
+
top: 0 !important;
|
|
37
|
+
pointer-events: auto !important;
|
|
38
|
+
display: block !important;
|
|
39
|
+
background: #fff !important;
|
|
40
|
+
}
|
|
41
|
+
#${PRINT_ROOT_ID} .os-print-frame {
|
|
42
|
+
width: 1920px !important;
|
|
43
|
+
height: 1080px !important;
|
|
44
|
+
background: #fff;
|
|
45
|
+
color: #000;
|
|
46
|
+
overflow: hidden;
|
|
47
|
+
page-break-after: always;
|
|
48
|
+
break-after: page;
|
|
49
|
+
-webkit-print-color-adjust: exact;
|
|
50
|
+
print-color-adjust: exact;
|
|
51
|
+
}
|
|
52
|
+
#${PRINT_ROOT_ID} .os-print-frame:last-child {
|
|
53
|
+
page-break-after: auto;
|
|
54
|
+
break-after: auto;
|
|
55
|
+
}
|
|
56
|
+
/* Supersample: Chrome rasterizes filtered/composited layers (e.g. filter:
|
|
57
|
+
blur, mix-blend-mode) at the layer's CSS-pixel size, so a blurred
|
|
58
|
+
gradient on a 1920×1080 page bakes in at ~1× DPI and bands when the PDF
|
|
59
|
+
is viewed scaled up. zoom:2 doubles the layer raster size; scale(0.5)
|
|
60
|
+
composites it back to 1920×1080. Vector content (text, plain CSS
|
|
61
|
+
gradients, SVG) stays vector through both transforms. */
|
|
62
|
+
#${PRINT_ROOT_ID} .os-print-supersample {
|
|
63
|
+
width: 1920px !important;
|
|
64
|
+
height: 1080px !important;
|
|
65
|
+
zoom: 2;
|
|
66
|
+
transform: scale(0.5);
|
|
67
|
+
transform-origin: top left;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
`;
|
|
71
|
+
|
|
72
|
+
export type PdfExportProgress = {
|
|
73
|
+
phase: 'processing' | 'printing' | 'done';
|
|
74
|
+
/** Number of pages whose intro animations have finished (0..total). */
|
|
75
|
+
current: number;
|
|
76
|
+
total: number;
|
|
77
|
+
/** 0–99 while processing, 99 during printing, 100 when done. */
|
|
78
|
+
percent: number;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const ANIMATION_TIMEOUT_MS = 15_000;
|
|
82
|
+
const POLL_INTERVAL_MS = 100;
|
|
83
|
+
|
|
84
|
+
export async function exportSlideAsPdf(
|
|
85
|
+
slide: SlideModule,
|
|
86
|
+
slideId: string,
|
|
87
|
+
onProgress?: (progress: PdfExportProgress) => void,
|
|
88
|
+
): Promise<void> {
|
|
89
|
+
const pages = slide.default ?? [];
|
|
90
|
+
if (pages.length === 0) return;
|
|
91
|
+
|
|
92
|
+
const total = pages.length;
|
|
93
|
+
|
|
94
|
+
const style = document.createElement('style');
|
|
95
|
+
style.id = PRINT_STYLE_ID;
|
|
96
|
+
style.textContent = PRINT_STYLES;
|
|
97
|
+
document.head.appendChild(style);
|
|
98
|
+
|
|
99
|
+
const root = document.createElement('div');
|
|
100
|
+
root.id = PRINT_ROOT_ID;
|
|
101
|
+
root.setAttribute('aria-hidden', 'true');
|
|
102
|
+
document.body.appendChild(root);
|
|
103
|
+
|
|
104
|
+
onProgress?.({ phase: 'processing', current: 0, total, percent: 0 });
|
|
105
|
+
|
|
106
|
+
const reactRoots: Root[] = [];
|
|
107
|
+
const frames: HTMLElement[] = [];
|
|
108
|
+
for (const Page of pages) {
|
|
109
|
+
const host = document.createElement('div');
|
|
110
|
+
host.className = 'os-print-frame';
|
|
111
|
+
host.style.width = '1920px';
|
|
112
|
+
host.style.height = '1080px';
|
|
113
|
+
const inner = document.createElement('div');
|
|
114
|
+
inner.className = 'os-print-supersample';
|
|
115
|
+
inner.style.width = '1920px';
|
|
116
|
+
inner.style.height = '1080px';
|
|
117
|
+
host.appendChild(inner);
|
|
118
|
+
root.appendChild(host);
|
|
119
|
+
frames.push(host);
|
|
120
|
+
const r = createRoot(inner);
|
|
121
|
+
r.render(createElement(Page));
|
|
122
|
+
reactRoots.push(r);
|
|
123
|
+
}
|
|
124
|
+
// Yield once so React commits all pages and CSS animations actually start
|
|
125
|
+
// (queued via Web Animations API on the first paint after mount).
|
|
126
|
+
await nextPaint();
|
|
127
|
+
|
|
128
|
+
const previousTitle = document.title;
|
|
129
|
+
document.title = slide.meta?.title ?? slideId;
|
|
130
|
+
|
|
131
|
+
try {
|
|
132
|
+
await waitForFonts();
|
|
133
|
+
|
|
134
|
+
// Poll per-page animation completion. The bar tracks how many pages have
|
|
135
|
+
// settled, which matches "page X of N is being processed" mental model.
|
|
136
|
+
const deadline = performance.now() + ANIMATION_TIMEOUT_MS;
|
|
137
|
+
while (performance.now() < deadline) {
|
|
138
|
+
const settled = frames.reduce((n, frame) => (isFrameAnimationSettled(frame) ? n + 1 : n), 0);
|
|
139
|
+
onProgress?.({
|
|
140
|
+
phase: 'processing',
|
|
141
|
+
current: settled,
|
|
142
|
+
total,
|
|
143
|
+
percent: Math.min(99, (settled / total) * 99),
|
|
144
|
+
});
|
|
145
|
+
if (settled === total) break;
|
|
146
|
+
await sleep(POLL_INTERVAL_MS);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
await waitForDataWaitfor(root);
|
|
150
|
+
await sleep(100); // flush layout
|
|
151
|
+
|
|
152
|
+
onProgress?.({ phase: 'printing', current: total, total, percent: 99 });
|
|
153
|
+
const printDone = waitForAfterPrint();
|
|
154
|
+
window.print();
|
|
155
|
+
await printDone;
|
|
156
|
+
} finally {
|
|
157
|
+
onProgress?.({ phase: 'done', current: total, total, percent: 100 });
|
|
158
|
+
document.title = previousTitle;
|
|
159
|
+
for (const r of reactRoots) r.unmount();
|
|
160
|
+
root.remove();
|
|
161
|
+
style.remove();
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function sleep(ms: number): Promise<void> {
|
|
166
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function nextPaint(): Promise<void> {
|
|
170
|
+
// rAF in real tabs; setTimeout fallback for hidden/throttled headless tabs.
|
|
171
|
+
return new Promise((resolve) => {
|
|
172
|
+
let settled = false;
|
|
173
|
+
const settle = () => {
|
|
174
|
+
if (settled) return;
|
|
175
|
+
settled = true;
|
|
176
|
+
resolve();
|
|
177
|
+
};
|
|
178
|
+
requestAnimationFrame(settle);
|
|
179
|
+
setTimeout(settle, 50);
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function waitForAfterPrint(timeoutMs = 60_000): Promise<void> {
|
|
184
|
+
return new Promise((resolve) => {
|
|
185
|
+
const cleanup = () => {
|
|
186
|
+
window.removeEventListener('afterprint', onAfter);
|
|
187
|
+
clearTimeout(timer);
|
|
188
|
+
resolve();
|
|
189
|
+
};
|
|
190
|
+
const onAfter = () => cleanup();
|
|
191
|
+
const timer = setTimeout(cleanup, timeoutMs);
|
|
192
|
+
window.addEventListener('afterprint', onAfter, { once: true });
|
|
193
|
+
});
|
|
194
|
+
}
|
|
@@ -4,6 +4,13 @@ export type SlideSourceHit = {
|
|
|
4
4
|
anchor: HTMLElement;
|
|
5
5
|
};
|
|
6
6
|
|
|
7
|
+
export type FindSlideSourceOptions = {
|
|
8
|
+
// Visual editor uses this: skip component-invocation JSX (`<MyComp/>`)
|
|
9
|
+
// since most components don't forward `style`. Comments leave it off
|
|
10
|
+
// so any JSX can be annotated.
|
|
11
|
+
hostOnly?: boolean;
|
|
12
|
+
};
|
|
13
|
+
|
|
7
14
|
type FiberLike = {
|
|
8
15
|
return: FiberLike | null;
|
|
9
16
|
stateNode?: unknown;
|
|
@@ -21,17 +28,45 @@ function getSource(fiber: FiberLike) {
|
|
|
21
28
|
return fiber._debugSource ?? fiber.memoizedProps?.__source;
|
|
22
29
|
}
|
|
23
30
|
|
|
24
|
-
export function findSlideSource(
|
|
31
|
+
export function findSlideSource(
|
|
32
|
+
el: HTMLElement,
|
|
33
|
+
slideId: string,
|
|
34
|
+
opts?: FindSlideSourceOptions,
|
|
35
|
+
): SlideSourceHit | null {
|
|
36
|
+
// Primary path: the `data-slide-loc` attribute injected by the
|
|
37
|
+
// loc-tags Vite plugin. Immune to HMR-stale fiber state.
|
|
38
|
+
const tagged = el.closest<HTMLElement>('[data-slide-loc]');
|
|
39
|
+
if (tagged) {
|
|
40
|
+
const loc = tagged.dataset.slideLoc;
|
|
41
|
+
if (loc) {
|
|
42
|
+
const idx = loc.indexOf(':');
|
|
43
|
+
if (idx > 0) {
|
|
44
|
+
const line = Number(loc.slice(0, idx));
|
|
45
|
+
const column = Number(loc.slice(idx + 1));
|
|
46
|
+
if (Number.isFinite(line) && Number.isFinite(column)) {
|
|
47
|
+
return { line, column, anchor: tagged };
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Fallback for JSX rendered from imported component files (which the
|
|
54
|
+
// loc-tags plugin doesn't transform).
|
|
25
55
|
const needle = `/slides/${slideId}/index.tsx`;
|
|
26
56
|
let fiber = getFiber(el);
|
|
27
57
|
let anchor: HTMLElement = el;
|
|
28
58
|
while (fiber) {
|
|
29
59
|
const src = getSource(fiber);
|
|
30
|
-
|
|
31
|
-
|
|
60
|
+
const isHost = fiber.stateNode instanceof HTMLElement;
|
|
61
|
+
if (src?.fileName?.endsWith(needle) && src.lineNumber && (!opts?.hostOnly || isHost)) {
|
|
62
|
+
return {
|
|
63
|
+
line: src.lineNumber,
|
|
64
|
+
column: src.columnNumber ?? 0,
|
|
65
|
+
anchor: isHost ? (fiber.stateNode as HTMLElement) : anchor,
|
|
66
|
+
};
|
|
32
67
|
}
|
|
33
|
-
if (
|
|
34
|
-
anchor = fiber.stateNode;
|
|
68
|
+
if (isHost) {
|
|
69
|
+
anchor = fiber.stateNode as HTMLElement;
|
|
35
70
|
}
|
|
36
71
|
fiber = fiber.return;
|
|
37
72
|
}
|