ttrpg-tools/src/components/md-deck/PrintPreview.tsx

157 lines
5.8 KiB
TypeScript
Raw Normal View History

2026-02-27 21:12:23 +08:00
import { For } from 'solid-js';
2026-02-27 16:02:53 +08:00
import type { DeckStore } from './hooks/deckStore';
2026-02-27 21:12:23 +08:00
import { usePageLayout } from './hooks/usePageLayout';
import { usePDFExport, type ExportOptions } from './hooks/usePDFExport';
import { PrintPreviewHeader } from './PrintPreviewHeader';
import { PrintPreviewFooter } from './PrintPreviewFooter';
import { CardLayer } from './CardLayer';
2026-02-27 16:02:53 +08:00
export interface PrintPreviewProps {
store: DeckStore;
onClose: () => void;
2026-02-27 20:27:26 +08:00
onExport: () => void;
2026-02-27 16:02:53 +08:00
}
/**
* A4
*/
export function PrintPreview(props: PrintPreviewProps) {
const { store } = props;
2026-02-27 21:12:23 +08:00
const { getA4Size, pages, cropMarks } = usePageLayout(store);
const { exportToPDF } = usePDFExport(store, props.onClose);
const visibleLayers = () => store.state.layerConfigs.filter((l) => l.visible);
const handleExport = async () => {
const options: ExportOptions = {
orientation: store.state.printOrientation,
cardWidth: store.state.dimensions?.cardWidth || 56,
cardHeight: store.state.dimensions?.cardHeight || 88,
gridOriginX: store.state.dimensions?.gridOriginX || 0,
gridOriginY: store.state.dimensions?.gridOriginY || 0,
gridAreaWidth: store.state.dimensions?.gridAreaWidth || 56,
gridAreaHeight: store.state.dimensions?.gridAreaHeight || 88,
visibleLayers: visibleLayers(),
dimensions: store.state.dimensions!
};
await exportToPDF(pages(), cropMarks(), options);
2026-02-27 18:19:37 +08:00
};
2026-02-27 16:02:53 +08:00
return (
2026-02-27 20:27:26 +08:00
<div class="fixed inset-0 bg-black/50 z-50 overflow-auto">
<div class="min-h-screen py-20 px-4">
2026-02-27 21:12:23 +08:00
<PrintPreviewHeader
store={store}
pageCount={pages().length}
onExport={handleExport}
onClose={props.onClose}
/>
2026-02-27 16:02:53 +08:00
2026-02-27 21:12:23 +08:00
<PrintPreviewFooter store={store} />
2026-02-27 21:02:33 +08:00
2026-02-27 20:27:26 +08:00
<div class="flex flex-col items-center gap-8">
2026-02-27 16:02:53 +08:00
<For each={pages()}>
{(page) => (
2026-02-27 18:19:37 +08:00
<svg
2026-02-27 20:27:26 +08:00
class="bg-white shadow-xl"
2026-02-27 18:19:37 +08:00
viewBox={`0 0 ${getA4Size().width}mm ${getA4Size().height}mm`}
2026-02-27 16:02:53 +08:00
style={{
2026-02-27 17:55:02 +08:00
width: `${getA4Size().width}mm`,
height: `${getA4Size().height}mm`
2026-02-27 16:02:53 +08:00
}}
data-page={page.pageIndex + 1}
2026-02-27 18:19:37 +08:00
xmlns="http://www.w3.org/2000/svg"
2026-02-27 16:02:53 +08:00
>
2026-02-27 18:19:37 +08:00
<rect
x={`${cropMarks()[page.pageIndex]?.frameBoundsWithMargin.x}mm`}
y={`${cropMarks()[page.pageIndex]?.frameBoundsWithMargin.y}mm`}
width={`${cropMarks()[page.pageIndex]?.frameBoundsWithMargin.width}mm`}
height={`${cropMarks()[page.pageIndex]?.frameBoundsWithMargin.height}mm`}
fill="none"
stroke="black"
stroke-width="0.2"
/>
<For each={cropMarks()[page.pageIndex]?.horizontalLines}>
{(line) => (
<>
<line
x1={`${line.xStart}mm`}
y1={`${line.y}mm`}
x2={`${page.frameBounds.minX}mm`}
y2={`${line.y}mm`}
stroke="#888"
stroke-width="0.1"
/>
<line
x1={`${page.frameBounds.maxX}mm`}
y1={`${line.y}mm`}
x2={`${line.xEnd}mm`}
y2={`${line.y}mm`}
stroke="#888"
stroke-width="0.1"
/>
</>
)}
</For>
2026-02-27 21:12:23 +08:00
2026-02-27 18:19:37 +08:00
<For each={cropMarks()[page.pageIndex]?.verticalLines}>
{(line) => (
<>
<line
x1={`${line.x}mm`}
y1={`${line.yStart}mm`}
x2={`${line.x}mm`}
y2={`${page.frameBounds.minY}mm`}
stroke="#888"
stroke-width="0.1"
/>
<line
x1={`${line.x}mm`}
y1={`${page.frameBounds.maxY}mm`}
x2={`${line.x}mm`}
y2={`${line.yEnd}mm`}
stroke="#888"
stroke-width="0.1"
/>
</>
)}
</For>
<For each={page.cards}>
2026-02-27 21:12:23 +08:00
{(card) => (
<g class="card-group">
<foreignObject
x={`${card.x}mm`}
y={`${card.y}mm`}
width={`${store.state.dimensions?.cardWidth || 56}mm`}
height={`${store.state.dimensions?.cardHeight || 88}mm`}
>
<div xmlns="http://www.w3.org/1999/xhtml" class="w-full h-full bg-white">
<div
class="absolute"
style={{
position: 'absolute',
left: `${store.state.dimensions?.gridOriginX}mm`,
top: `${store.state.dimensions?.gridOriginY}mm`,
width: `${store.state.dimensions?.gridAreaWidth}mm`,
height: `${store.state.dimensions?.gridAreaHeight}mm`
}}
>
2026-03-06 17:43:41 +08:00
<CardLayer store={store} cardData={card.data}
2026-02-27 21:12:23 +08:00
/>
</div>
</div>
</foreignObject>
</g>
)}
2026-02-27 18:19:37 +08:00
</For>
</svg>
2026-02-27 16:02:53 +08:00
)}
</For>
</div>
</div>
</div>
);
}