import { Show, For, createMemo } from 'solid-js'; import { marked } from '../../markdown'; import { getLayerStyle } from './hooks/dimensions'; import type { DeckStore } from './hooks/deckStore'; export interface PrintPreviewProps { store: DeckStore; onClose: () => void; onPrint: () => void; } /** * 渲染 layer 内容 */ function renderLayerContent(layer: { prop: string }, cardData: { [key: string]: string }): string { const content = cardData[layer.prop] || ''; return marked.parse(content) as string; } /** * 打印预览组件:在 A4 纸张上排列所有卡牌 */ export function PrintPreview(props: PrintPreviewProps) { const { store } = props; // A4 纸张尺寸(mm):210 x 297 const A4_WIDTH = 210; const A4_HEIGHT = 297; const PRINT_MARGIN = 5; // 打印边距 // 计算每张卡牌在 A4 纸上的位置 const pages = createMemo(() => { const cards = store.state.cards; const cardWidth = store.state.dimensions?.cardWidth || 56; const cardHeight = store.state.dimensions?.cardHeight || 88; // 每行可容纳的卡牌数量 const usableWidth = A4_WIDTH - PRINT_MARGIN * 2; const cardsPerRow = Math.floor(usableWidth / cardWidth); // 每页可容纳的行数 const usableHeight = A4_HEIGHT - PRINT_MARGIN * 2; const rowsPerPage = Math.floor(usableHeight / cardHeight); // 每页的卡牌数量 const cardsPerPage = cardsPerRow * rowsPerPage; // 分页 const result: { pageIndex: number; cards: Array<{ data: typeof cards[0]; x: number; y: number }> }[] = []; let currentPage: typeof result[0] = { pageIndex: 0, cards: [] }; for (let i = 0; i < cards.length; i++) { const pageIndex = Math.floor(i / cardsPerPage); const indexInPage = i % cardsPerPage; const row = Math.floor(indexInPage / cardsPerRow); const col = indexInPage % cardsPerRow; if (pageIndex !== currentPage.pageIndex) { result.push(currentPage); currentPage = { pageIndex, cards: [] }; } currentPage.cards.push({ data: cards[i], x: PRINT_MARGIN + col * cardWidth, y: PRINT_MARGIN + row * cardHeight }); } if (currentPage.cards.length > 0) { result.push(currentPage); } return result; }); const visibleLayers = createMemo(() => store.state.layerConfigs.filter((l) => l.visible)); return (
共 {pages().length} 页,{store.state.cards.length} 张卡牌