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

289 lines
9.2 KiB
TypeScript
Raw Normal View History

2026-03-15 00:52:00 +08:00
import { createSignal, For, Show, createMemo } from 'solid-js';
import type { PageData } from './hooks/usePDFExport';
2026-03-15 01:43:25 +08:00
import type { CardPath } from '../../plotcutter';
import type { CardShape } from '../../plotcutter';
import {
getCardShapePoints,
calculateCenter,
contourToSvgPath
} from '../../plotcutter';
import { generateTravelPaths, travelPathsToSvg } from '../../plotcutter';
2026-03-15 00:52:00 +08:00
import { pts2plotter } from '../../plotcutter';
export interface PltPreviewProps {
pages: PageData[];
cardWidth: number;
cardHeight: number;
shape: CardShape;
bleed: number;
2026-03-15 01:31:16 +08:00
cornerRadius: number;
2026-03-15 00:52:00 +08:00
onClose: () => void;
}
/**
2026-03-15 01:43:25 +08:00
*
2026-03-15 00:52:00 +08:00
*/
2026-03-15 01:43:25 +08:00
function generateCardPaths(
pages: PageData[],
cardWidth: number,
cardHeight: number,
2026-03-15 00:52:00 +08:00
shape: CardShape,
2026-03-15 01:43:25 +08:00
bleed: number,
cornerRadius: number,
2026-03-15 01:31:16 +08:00
a4Height: number
2026-03-15 01:43:25 +08:00
): CardPath[] {
2026-03-15 00:52:00 +08:00
const cardPaths: CardPath[] = [];
let pathIndex = 0;
// 计算切割尺寸(排版尺寸减去出血)
2026-03-15 01:43:25 +08:00
const cutWidth = cardWidth - bleed * 2;
const cutHeight = cardHeight - bleed * 2;
2026-03-15 00:52:00 +08:00
2026-03-15 01:43:25 +08:00
for (const page of pages) {
2026-03-15 00:52:00 +08:00
for (const card of page.cards) {
if (card.side !== 'front') continue;
2026-03-15 01:43:25 +08:00
// 生成形状轮廓点(相对于卡片左下角)
const shapePoints = getCardShapePoints(shape, cutWidth, cutHeight, cornerRadius);
// 平移到页面坐标并翻转 Y 轴
2026-03-15 00:52:00 +08:00
const pagePoints = shapePoints.map(([x, y]) => [
2026-03-15 01:43:25 +08:00
card.x + bleed + x,
a4Height - (card.y + bleed + y)
2026-03-15 00:52:00 +08:00
] as [number, number]);
const center = calculateCenter(pagePoints);
2026-03-15 01:43:25 +08:00
const pathD = contourToSvgPath(pagePoints);
2026-03-15 00:52:00 +08:00
2026-03-15 01:31:16 +08:00
// 起点和终点(对于闭合路径是同一点)
const startPoint = pagePoints[0];
const endPoint = pagePoints[pagePoints.length - 1];
2026-03-15 00:52:00 +08:00
cardPaths.push({
pageIndex: page.pageIndex,
cardIndex: pathIndex++,
points: pagePoints,
centerX: center.x,
centerY: center.y,
2026-03-15 01:31:16 +08:00
pathD,
startPoint,
endPoint
2026-03-15 00:52:00 +08:00
});
}
}
2026-03-15 01:43:25 +08:00
return cardPaths;
}
/**
* PLT -
*/
export function PltPreview(props: PltPreviewProps) {
const a4Width = 297; // 横向 A4
const a4Height = 210;
// 使用传入的圆角值,但也允许用户修改
const [cornerRadius, setCornerRadius] = createSignal(props.cornerRadius);
// 生成所有卡片路径
const cardPaths = createMemo(() =>
generateCardPaths(
props.pages,
props.cardWidth,
props.cardHeight,
props.shape,
props.bleed,
cornerRadius(),
a4Height
)
);
2026-03-15 01:31:16 +08:00
// 生成空走路径
2026-03-15 01:43:25 +08:00
const travelPathD = createMemo(() => {
const travelPaths = generateTravelPaths(cardPaths(), a4Height);
return travelPathsToSvg(travelPaths);
});
2026-03-15 01:31:16 +08:00
2026-03-15 00:52:00 +08:00
// 生成 HPGL 代码用于下载
2026-03-15 01:43:25 +08:00
const plotterCode = createMemo(() => {
const allPaths = cardPaths().map(p => p.points);
return allPaths.length > 0 ? pts2plotter(allPaths, a4Width, a4Height, 1) : '';
});
2026-03-15 00:52:00 +08:00
const handleDownload = () => {
2026-03-15 01:43:25 +08:00
if (!plotterCode()) {
2026-03-15 00:52:00 +08:00
alert('没有可导出的卡片');
return;
}
2026-03-15 01:43:25 +08:00
const blob = new Blob([plotterCode()], { type: 'application/vnd.hp-HPGL' });
2026-03-15 00:52:00 +08:00
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = `deck-export-${new Date().toISOString().slice(0, 19).replace(/:/g, '-')}.plt`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
};
2026-03-15 01:31:16 +08:00
const handleCornerRadiusChange = (e: Event) => {
const target = e.target as HTMLInputElement;
setCornerRadius(Number(target.value));
};
2026-03-15 00:52:00 +08:00
return (
<div class="fixed inset-0 bg-black/50 z-50 overflow-auto">
<div class="min-h-screen py-20 px-4">
{/* 头部控制栏 */}
<div class="fixed top-4 left-1/2 -translate-x-1/2 bg-white shadow-lg rounded-lg px-4 py-3 flex items-center gap-4 z-50">
<h2 class="text-base font-bold m-0">PLT </h2>
<div class="flex items-center gap-2">
2026-03-15 01:31:16 +08:00
<label class="text-sm text-gray-600"> (mm):</label>
<input
type="number"
min="0"
max="10"
step="0.5"
value={cornerRadius()}
onInput={handleCornerRadiusChange}
class="w-16 px-2 py-1 border border-gray-300 rounded text-sm"
/>
</div>
<div class="flex items-center gap-2 flex-1">
2026-03-15 00:52:00 +08:00
<button
onClick={handleDownload}
class="bg-blue-600 hover:bg-blue-700 text-white px-3 py-1.5 rounded text-sm font-medium cursor-pointer flex items-center gap-1"
2026-03-15 01:43:25 +08:00
disabled={cardPaths().length === 0}
2026-03-15 00:52:00 +08:00
>
📥 PLT
</button>
<button
onClick={props.onClose}
class="bg-gray-200 hover:bg-gray-300 text-gray-700 px-3 py-1.5 rounded text-sm font-medium cursor-pointer"
>
</button>
</div>
</div>
{/* 预览区域 */}
<div class="flex flex-col items-center gap-8 mt-20">
<For each={props.pages}>
{(page) => {
2026-03-15 01:43:25 +08:00
const pageCardPaths = cardPaths().filter(p => p.pageIndex === page.pageIndex);
2026-03-15 00:52:00 +08:00
return (
<svg
class="bg-white shadow-xl"
viewBox={`0 0 ${a4Width} ${a4Height}`}
style={{
width: `${a4Width}mm`,
height: `${a4Height}mm`
}}
xmlns="http://www.w3.org/2000/svg"
>
{/* A4 边框 */}
<rect
x="0"
y="0"
width={a4Width}
height={a4Height}
fill="none"
stroke="#ccc"
stroke-width="0.5"
/>
{/* 页面边框 */}
<rect
x={page.frameBounds.minX}
y={page.frameBounds.minY}
width={page.frameBounds.maxX - page.frameBounds.minX}
height={page.frameBounds.maxY - page.frameBounds.minY}
fill="none"
stroke="#888"
stroke-width="0.2"
/>
2026-03-15 01:31:16 +08:00
{/* 空走路径(虚线) */}
2026-03-15 01:43:25 +08:00
<Show when={travelPathD()}>
2026-03-15 01:31:16 +08:00
<path
2026-03-15 01:43:25 +08:00
d={travelPathD()}
2026-03-15 01:31:16 +08:00
fill="none"
stroke="#999"
stroke-width="0.2"
stroke-dasharray="2 2"
/>
</Show>
2026-03-15 00:52:00 +08:00
{/* 切割路径 */}
<For each={pageCardPaths}>
{(path) => {
return (
<g>
2026-03-15 01:19:58 +08:00
{/* 切割路径 */}
2026-03-15 00:52:00 +08:00
<path
d={path.pathD}
fill="none"
2026-03-15 01:19:58 +08:00
stroke="#3b82f6"
2026-03-15 00:52:00 +08:00
stroke-width="0.3"
/>
2026-03-15 01:19:58 +08:00
{/* 动画小球 */}
<circle
2026-03-15 01:31:16 +08:00
r="0.8"
fill="#ef4444"
2026-03-15 01:19:58 +08:00
>
<animateMotion dur="4s" repeatCount="indefinite" path={path.pathD}>
</animateMotion>
</circle>
2026-03-15 00:52:00 +08:00
{/* 序号标签 */}
<g transform={`translate(${path.centerX}, ${path.centerY})`}>
<circle
r="2"
fill="white"
2026-03-15 01:19:58 +08:00
stroke="#3b82f6"
2026-03-15 00:52:00 +08:00
stroke-width="0.1"
/>
<text
text-anchor="middle"
dominant-baseline="middle"
font-size="1.5"
2026-03-15 01:19:58 +08:00
fill="#3b82f6"
2026-03-15 00:52:00 +08:00
font-weight="bold"
>
{path.cardIndex + 1}
</text>
</g>
</g>
);
}}
</For>
</svg>
);
}}
</For>
</div>
2026-03-15 01:31:16 +08:00
{/* 图例说明 */}
<div class="fixed bottom-4 left-1/2 -translate-x-1/2 bg-white shadow-lg rounded-lg px-4 py-2 flex items-center gap-4 z-50">
<div class="flex items-center gap-2">
<div class="w-6 h-0.5" style={{ "border-bottom": "2px dashed #999" }}></div>
<span class="text-sm text-gray-600"></span>
</div>
<div class="flex items-center gap-2">
<div class="w-6 h-0.5" style={{ "border-bottom": "2px solid #3b82f6" }}></div>
<span class="text-sm text-gray-600"></span>
</div>
<div class="flex items-center gap-2">
<div class="w-4 h-4 rounded-full bg-red-500"></div>
<span class="text-sm text-gray-600"></span>
</div>
</div>
2026-03-15 00:52:00 +08:00
</div>
</div>
);
}